husky + commitizen 规范git commit提交
安装husky
-
官网 https://typicode.github.io/husky/#/?id=yarn-2
-
npm install husky --save-dev # 安装 npx husky install npm set-script prepare "husky install"
创建钩子
-
pre-commit 勾子
-
npx husky add .husky/pre-commit "npm test"
-
以上, 成功实现在git commit 之前进行 test
-
commit-msg勾子
-
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'
-
这个时候,这个勾子会报错,继续接下来操作
规范提交
安装插件
- 用git cz 命令来创建一个规范的git commit -m
npm i -D commitizen cz-conventional-changelog # 局部安装
配置package.json
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
}
配合commitlint校验
- 安装插件
npm i -D @commitlint/config-conventional @commitlint/cli
-
在项目根目录下创建配置文件
commitlint.config.js
-
'use strict'; module.exports = { extents: [ '@commitlint/config-conventional', ], rules: { 'body-leading-blank': [ 1, 'always' ], 'footer-leading-blank': [ 1, 'always' ], 'header-max-length': [ 2, 'always', 72 ], 'scope-case': [ 2, 'always', 'lower-case' ], 'subject-case': [ 2, 'never', [ 'sentence-case', 'start-case', 'pascal-case', 'upper-case' ], ], 'subject-empty': [ 2, 'never' ], 'subject-full-stop': [ 2, 'never', '.' ], 'type-case': [ 2, 'always', 'lower-case' ], 'type-empty': [ 2, 'never' ], 'type-enum': [ 2, 'always', [ 'build', 'chore', 'ci', 'docs', 'feat', 'fix', 'improvement', 'perf', 'refactor', 'revert', 'style', 'test', ], ], }, };
-
使用
git cz
- refactor 简单代码的重构
- revert 撤销
- chore 不知道怎么分类,选这个
- are there any breaking changes? 有没有不兼容的特性?
- 这个时候,随便git commit -m ‘hello’ 就会不允许提交, 必须符合规范, 使用git cz 就会符合规范。
版权声明:本文为Joy_Huu原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。