DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Utilizing Database Hooks Like a Pro in Node.js
  • Node.js Microservices Architecture: A Complete Guide
  • Refresh Token Rotation in Node.js: Stopping Token Theft Without Logging Users Out
  • Stop Writing Dialect-Specific SQL: A Unified Query Builder for Node.js

Trending

  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • Beyond Manual Mapping: AI-Orchestrated Governance for Hybrid Cloud
  • Build a Voice Assistant With Gemini Live API
  • Member Spotlight: Shai Almong

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Bitbucket Pre-Commit Hooks With Husky for Better Code Quality

Bitbucket Pre-Commit Hooks With Husky for Better Code Quality

BitBucket is Git Management solution from Atlassian. It is free for small teams up to 5 active team members and support all Git repository features.

By 
Tomasz J-ski
·
Aug. 20, 20 · Analysis
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
17.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

BitBucket is Git Management solution from Atlassian. It is free for small teams up to 5 active team members and support all Git repository features. More about Bitbucket could be fount at their website https://bitbucket.org/ .

Pre-commit hooks are powerful functionality to execute own scripts after staging git changes but before commit them to the local repository. Thanks to this solution developers are able to execute code quality checks and code tests before the code officially lands at the repository. Pre-commit hook is not the only hook supported by Git. Other common hooks are:

  • applypatch-msg

  • pre-applypatch

  • post-applypatch

  • pre-commit

  • prepare-commit-msg

  • commit-msg

  • post-commit

  • pre-rebase

  • post-checkout

  • post-merge

  • pre-receive

  • update

  • post-receive

  • post-update

  • pre-auto-gc

  • post-rewrite

  • pre-push

But the most important from code quality and test perspective are pre-commit and pre-push ones.

Husky is node js tool written by Typicode and supporters as open source support for bad git commit prevention.Project homepage is https://github.com/typicode/husky and of course https://www.npmjs.com/package/husky.

Putting all together. The goal is to perform code quality checks and test before committing them to local code repository to ensure best possible code stability.

First step like always for npm libraries and tools is installation.

npm install husky –save-dev or npm i husky 

depends on your need.

Next step is adding configuration to package.json

JSON
 




x
15


 
1
// package.json
2
{
3

          
4
 "husky": {
5

          
6
 "hooks": {
7

          
8
 "pre-commit": "npm test",
9

          
10
"pre-push": "npm test"
11

          
12
}
13

          
14
  }
15
}



General approach is “name of the hook” : “script / command to be executed”.
And that’s all!

You have full support to pre-commit hook and can execute any bash script like npm test.

Of course husky is not the only tool that I’m using but this article focus only on husky. Typically for node js (and general JavaScript) development it is worth to add eslint and Prettier for checking the code and auto formatting tools.

Husky supports out of the box git event system – hooks and do not force developer to manually create them in Git folder structure. It is awesome supporting tool which keeps all needed configuration is standard package.json file which is always first step to investigate what node js application does.

Hook Node.js

Published at DZone with permission of Tomasz J-ski. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Utilizing Database Hooks Like a Pro in Node.js
  • Node.js Microservices Architecture: A Complete Guide
  • Refresh Token Rotation in Node.js: Stopping Token Theft Without Logging Users Out
  • Stop Writing Dialect-Specific SQL: A Unified Query Builder for Node.js

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook