git hook husky+commitlint检查git提交描述是否符合规范

注意:npm 安装 npm版本需要在7.0以上

第一步:安装依赖:

yarn add @commitlint/config-conventional@12.1.4 @commitlint/cli@12.1.4 -S

第二步:创建commitlint.config.js,可以命令行执行以下代码,或者手动创建文件,并添加标红代码

echo "module.exports={extends: ['@commitlint/config-conventional']}" > commitlint.config.js

第三步:打开commitlint.config.js, 增加配置项 

注意:确保当前文件格式为 “UTF-8”的编码格式
module.exports = { // 继承的规则 extends: ['@commitlint/config-conventional'], // 定义自己的规则 roles: { // type 的类型定义:表示 git 提交的 type 必须在以下范围之内 'type-enum': [ // 当前验证的错误级别, 2: 错误级别的错误 2, // 在什么情况袭进行验证 'always', // 表示一直验证 // 泛型内容 同cz-config.js中的types一样 [ 'feat', // 新功能 feature 'fix', // 修复 bug 'docs', // 文档注释 'style', // 代码格式(不影响代码运行的变动) 'refactor', // 重构(既不增加新功能,也不是修复bug) 'perf', // 性能优化 'test', // 增加测试 'chore', // 构建过程或辅助工具的变动 'revert', // 回退 'build' // 打包 ] ], // subject 大小写不做校验 'subject-case': [0] } }

 

第四步:安装 husky

yarn add husky@7.0.1 -S

第五步:手动启动husky,生成.husky文件夹

npx husky install

第六步:在package.json中生成 prepare 指令, 命令行执行以下代码 或者 手动添加指令

npm set-script prepare "husky install"

 

第七步:执行prepare指令

yarn prepare / npm run prepare

第八步: 添加 commitlint 的 hook 到 husky 中,并指令在commit-msg 的hooks下执行 npx --no-install commitlint --edit "$1" 指令

npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'

 

 

 第九步:测试

 

posted @ 2021-12-11 23:18  Mr_R  阅读(439)  评论(0编辑  收藏  举报