使用commitizen和cz-customizable规范代码提交

1.安装commitizen和cz-customizable

1
2
npm install -g commitizen@4.2.4
npm i cz-customizable@6.3.0 --save-dev

2.在package.json中进行新增

1
2
3
4
5
"config": {
"commitizen": {
"path": "node_modules/cz-customizable"
}
}

3.在根目录下新建.cz-config.js文件并写入配置 之后就可以用 git cz 来代替 git commit

4.使用husky进行强制git代码提交规范

1
2
3
npm install --save-dev @commitlint/config-conventional@12.1.4 @commitlint/cli@12.1.4
npm install husky@7.0.1 --save-dev
npx husky install

5.在package.json中新增指令

1
"prepare": "husky install"

6.并执行

1
npm run prepare

7.新增husky配置文件 并往里面写入

1
2
npx husky add .husky/commit-msg
npx --no-install commitlint --edit

8、husky规范

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// commitlint.config.js
module.exports = {
// 继承的规则
extends: ['@commitlint/config-conventional'],
// 定义规则类型
rules: {
// type 类型定义,表示 git 提交的 type 必须在以下类型范围内
'type-enum': [
2,
'always',
[
'feat', // 新功能 feature
'fix', // 修复 bug
'docs', // 文档注释
'style', // 代码格式(不影响代码运行的变动)
'refactor', // 重构(既不增加新功能,也不是修复bug)
'perf', // 性能优化
'test', // 增加测试
'chore', // 构建过程或辅助工具的变动
'revert', // 回退
'build' // 打包
]
],
// subject 大小写不做校验
'subject-case': [0]
}
}