Found Husky reminding to update version#
Always forgetful on the updation to increment your version in package.json
when building Node.js projects?
I found Husky 🐶 can woof and remind you more than it can help you automatically lint commit messages, code and run tests upon commiting or pushing to GitHub.
Introduction#
Husky is an npm package that simplifies task and help you easily create your own custom git hooks.
For example you want to execute npm test on pre-commit, and when the test fails, the git hook
will stop you from commiting your code and helps you not to commit unwanted unit test failures.
Getting Started#
Assumed you already have an existing Node.js project with git setup (can be via git init)
Husky Installation#
npm install --save-dev husky
Husky Init#
Requires git
It is recommended that you have a git setup before npx husky init.
If you don't have git setup, then first run git init.
npx husky init
It creates a pre-commit in .husky/ and updates the prepare script in package.json
Reference: https://typicode.github.io/husky/get-started.html
Customize pre-commit#
See reference: (1)
| pre-commit | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
Try It#
git commit -m "keep calm and commit"
# test script will run every time you commit
And when you forgot to update your version, it will stop your from committing your code and displays the following message:
Please increment version in package.json
husky - pre-commit script failed (code 1)
Try updating your version in package.json without updating the package-lock.json.
Then run the commit command again.
You should now be able to proceed and notice that the package-lock.json
gets automatically update as well.
🌸 Everything is Great!
Conclusion#
There are times where you forget to update the version. SO it's good that we add automation on these small tasks and save time!
Bonus#
Whenever you push your code, are you also looking a way to automate create release tags or git tag,
on your GitHub repository based on the version in your package.json?
Then see how to automatically release tag.