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.
Join the DZone community and get the full member experience.
Join For FreeBitBucket 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
// package.json
{
"husky": {
"hooks": {
"pre-commit": "npm test",
"pre-push": "npm test"
}
}
}
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.
Published at DZone with permission of Tomasz J-ski. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments