DEV Community

Alireza Razinejad
Alireza Razinejad

Posted on

Add Husky 🐶 to Angular

Code Quality Practice

It is really usual for developers to forget to run lint or test commands before committing their codes.

Husky is a library that provides us easy access to Git Hooks, we can easily manage to run some commands before committing the code.

Using it is really easy, if you are using NPM the command is

npx husky-init && npm install

It will add npm test as pre-commit by default, and here we need to add lint too, so we are going to add new pre-commit command using the following command

npx husky add ./husky/lint 'npm run lint:fix

For those who use Windows (like me), if a help message appeared, we need to use

node node_modules/.bin/husky add instead of npx

This one did not worked for me too, so what to do? 🧐

When run the command npx husky init it will add default test command for pre-commit, the configs exist under .husky directory when we open the file, it is simply fetching some bash file and then there is npm test command.

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm test
Enter fullscreen mode Exit fullscreen mode

So all we need to do next to add our command and even update the existing command.

That's it!
From now on we can be sure that code quality will be kept at every developer commit.

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run test:ci
npm run lint:fix
Enter fullscreen mode Exit fullscreen mode

Discussion (0)