安装
npm i -D husky
添加prepare脚本
在package.json中添加 "prepare": "husky install" 脚本
// package.json
"script":{
"prepare": "husky install",
}
生成.husky目录
npm install
创建commit-msg hook文件
// 执行完该命令后,会看到.husky目录下新增了一个commit-msg的shell脚本
npx husky add .husky/commit-msg
将一下内容复制到到commit-msg脚本中,添加校验规则
#!/bin/sh
# 用 `` 可以将命令的输出结果赋值给变量
# 获取当前提交的 commit msg
commit_msg=`cat $1`
msg_re="^(feat|fix|docs|style|refactor|perf|test|workflow|build|ci|chore|release|workflow)(\(.+\))?:.{1,100}"
if [[ ! $commit_msg =~ $msg_re ]]
then
echo "不合法的 commit 消息提交格式,请使用正确的格式:"
echo ""
echo "feat 新功能(feature)"
echo "fix 修补bug"
echo "docs 文档(新增or修改)"
echo "style 格式(不影响代码运行的变动)"
echo "refactor 重构(即不是新增功能,也不是修改bug的代码变动)"
echo "perf 性能优化"
echo "test 增加测试"
echo "chore 构建过程或辅助工具的变动"
echo ""
echo "详情请查看 git commit 提交规范:https://github.com/woai3c/Front-end-articles/blob/master/git%20commit%20style.md"
# 异常退出
exit 1
fi