Pre-commits with husky
rhydderchc / August 30, 2021
2 min read
I'm pretty sure all of us are habituated with using
npm run script every time to either lint or format our project. In big projects it's kinda necessary to maintain standards just to keep all of the code neat and clean. Therefore to tend not to forget or to maintain the rules at the time of pull requests are very necessary. Therefore pre-commit hooks are one of the more valid choices.
I've faced this issue a lot of times myself, and therefore, I've adopted using husky for pre-commit hooks since I've learnt about it. Husky is a pretty understandable & easy to configure hook. You can check husky out on their github.
Configuring husky is a very straightforward process.
You can either install husky globally with
npm i -g husky
and then use the install command to install/configure it on to the system.
npx husky install
Now, to add a
pre-commit hook just do
husky add .husky/pre-commit "npm run format" # it can be any executable process.
npx husky add .husky/pre-commit "npm run format" # it can be any executable process.
You can keep on adding pre-commit hooks depending upon your needs. It's pretty much that simple.
Every time you're doing
git commit -m "feat(husky): sample message"
it runs the hooks before actually committing.
Alternatives to Husky
Husky isn't the only thing I'd actually suggest you to use. Pre-commit is an awesome alternative to husky that manages multi-language pre-commit hooks, you can read more about them in their documentation.