husky安装和配置

husky 文档

husky 安装

1
npm install husky --save-dev

husky (7.0 +)配置

package.json

1
2
3
4
5
6
7
8
9
10
11
"scripts": {
# "eslint": "eslint --cache --ext .js src --",
# "stylelint": "stylelint **/*.scss",
# "lint": "npm run eslint && npm run stylelint",
"husky": "cd ../../ && husky install theme1/.husky && cd theme1 && npx husky add .husky/pre-commit 'cd themes/myThemes && npm run lint'",
}
# 备注:
# 1. npm run lint,需要配置以上三个: "lint", "eslint", "stylelint";
# 2. .husky文件夹要与.git文件夹同级,
# 3. 'cd ../../', 是cd 到.git文件父级目录,theme1是.git文件父级目录名称,
# 4. 'cd themes/myThemes" 是到package.json父级目录, 用来跑package.json里配置的: npm run lint

husky 使用

commit时跑lint, 代码通过eslint或stylelint验证,则commit成功, 否则不能提交代码.

1
2
git add .husky/pre-commit
git commit -m "Keep calm and commit"

husky 停用

删除 .husky/pre-commit文件就可以,
下次想启用再npm run husky,会再生成.husky/pre-commit文件,commit时又可以用了.\

husky 卸载

1
npm uninstall husky && git config --unset core.hooksPath