Setup Husky + Commitlint + Commitizen + Lintstaged
Idea
We use
huskyto add git hook.pre-commit: before commit, we can run any action here likelintandtest.commit-msg: we addcommitlinthere to lint the commit message.
We use
commmitzento have a cli to easily write the legit commit message, so that it can passcommitlint.Then we just need to type
git add -Aandnpm run commitin the future.
Husky
https://typicode.github.io/husky
Installation Steps
npx husky-init && npm installnpx husky add .husky/commit-msg 'npx --no -- commitlint --edit "$1"'is-ci
npm install is-ci --save-dev
// package.json
{
"scripts": {
"prepare": "is-ci || husky install"
}
}
Commitlint
npm install --save-dev @commitlint/{cli,config-conventional}echo "module.exports = { extends: ['@commitlint/config-conventional'] };" > commitlint.config.js
Refs
Commitizen
http://commitizen.github.io/cz-cli/
npm install --save-dev commitizennpx commitizen init cz-conventional-changelog --save-dev --save-exact
...
"scripts": {
"commit": "cz"
}
Then,
npm run commit
lint-staged
- install
lint-stagedin root folder. - add
.lintstagedrcin root folder
{
"**/*.{js,jsx,ts,tsx}": ["npx prettier --write", "npx eslint --fix"]
}
- add npm-script in each package
For the --no-stash, refering this
"scripts":{
...
"lint-staged": "lint-staged --no-stash"
}
- in file
.husky/pre-commit, add this
npm run lint-staged