主题
husky 使用
shell
npm install husky@8 @commitlint/cli @commitlint/config-conventional --save-dev
# 生成目录
npx husky install
# 或者
npm run prepare并在 package.json 添加:
每次 npm install 都会自动执行
json
{
"scripts": {
"prepare": "husky install"
}
}Git 提交类型限制
- 生成
.husky/commit-msg目录
shell
npx husky add .husky/commit-msg "npx --no-install commitlint --edit $1"- 根目录新建
commitlint.config.js
js
export default {
extends: ['@commitlint/config-conventional'],
// 以下是我们自定义的规则
rules: {
'type-enum': [
2,
'always',
[
'build', // 构建相关
'chore', // 辅助工具的变动
'ci', // 自动化构建
'docs', // 文档(documentation)
'bug', // 此项特别针对bug号,用于向测试反馈bug列表的bug修改情况
'feat', // 新功能(feature)
'fix', // 修补bug
'style', // 格式(不影响代码运行的变动)
'refactor', // 重构(即不是新增功能,也不是修改bug的代码变动)
'test', // 增加测试
'revert', // feat(pencil): add ‘graphiteWidth’ option (撤销之前的commit)
'types', // 定义规则
],
],
},
};- 测试
bash
git add .
# 提交成功
git commit -m 'feat: xxx'
# 提交错误
git commit -m 'xxx
# Running commit-msg...
# ⧗ input: xxx
# ✖ subject may not be empty [subject-empty]
# ✖ type may not be empty [type-empty]
# ✖ found 2 problems, 0 warningsGit 推送前执行一些命令
- 创建
.husky/pre-push目录
shell
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
# 提交前执行
npm run docs:deploy- 验证
shell
git add .
git commit -m "测试 pre-push"
git pushWARNING
git push 触发 hook,执行 npm run docs:deploy
如果命令失败,push 会被阻止