首先看下
Angular规范 Commit message 和 Change log 编写指南 - 阮一峰的网络日志
约定式提交https://www.conventionalcommits.org/zh-hans/v1.0.0/
1. cz工具集 https://github.com/leoforfree/cz-customizable
1) cz适配器: commitizen cz-conventional-changelog
2) 定制提交规范: cz-customizable
commitlint-config-cz .cz-config.js
3) 提交校验:commitlint @commitlint/config-conventional @commitlint/cli commitlint.config.js
4) husky====git钩子工具 捕获commit 本配置是在commit 信息时 进行规则检查
5) 配置 hooks Git - githooks Documentation
2. vuecli3 可以直接使用插件https://github.com/ziyi2
3. vscode 安装commitizen 不使用命令
默认格式:
type(scope?): subject
body? // 'Provide a LONGER description of the change (optional). Use "|" to break new line:\n'
footer?
1. 安装
局部安装 :npm install -D commitizen cz-conventional-changelog
全局安装: npm install -g commitizen cz-conventional-changelog
2. package.json 配置
"scripts": {
.........
"commit": "git-cz"
},
"config": {
"commitizen": {
"path": "node_modules/cz-conventional-changelog"
}
}
需要注意 全局安装才可以使用git cz , npm run commit 两种情况都可以
3. 提交你的代码 git cz / npm run commit
4. 自定义校验规则 官方提供的校验规则:cz-config-EXAMPLE.js
1) 安装 cz-customizable
npm install cz-customizable -D
2)将之前符合Angular规范的cz-conventional-changelog适配器路径改成cz-customizable适配器路径:cz-customizable
默认会去读取项目中 .cz-config.js
配置
"config": {
"commitizen": {
"path": "node_modules/cz-customizable"
}
}
2) 创建:根目录下 .cz-config.js 复制官网文件 可以进行修改
skipQuestions: ['scope','body', 'footer'], // 表示要跳过哪些步骤, 比如 影响范围 scope , body 十分详细的描述,我们一般不需要 只保留subject 短表述就足够了 公司的代码不需要关联issues 那么可以不要footer
5. 本地使用进行了配置 校验 commitlint - Lint commit messages
1) 安装
npm install --save-dev @commitlint/config-conventional @commitlint/cli
创建commitlint.config.js文件 配置信息:使用下面命令
echo "module.exports = { extends: ['@commitlint/config-conventional'] };" > commitlint.config.js
2)安装 husky =》 捕获commit
https://github.com/typicode/huskyhttps://github.com/typicode/husky
npm install husky --save-dev
package.json 配置husky
"husky": {
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
},
3) 校验: 如果提交符合规范
否则报错:
快速生成日志命令 :
package.json 添加后执行npm run version
"version": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0 && git add CHANGELOG.md"
另外:
6. commitlint-config-cz
如果是使用cz-customizable适配器做了破坏Angular风格的提交说明配置,那么不能使用**@commitlint/config-conventional**规则进行提交说明校验,可以使用commitlint-config-cz对定制化提交说明进行校验。安装校验规则:
npm install commitlint-config-cz --save-dev
比如汉化 .cz-config.js 配置中进行了汉化
需要修改commitlint.config.js 文件配置
module.exports = {
// extends: ['@commitlint/config-conventional']
extends: ['cz']
};
这样就可以成功提交了
为什么使用约定式提交
- 自动化生产 CHANGELOG。
- 基于提交的类型,自动决定语义化的版本变更。
- 向同事、公众与其他利益关系者传达变化的性质。
- 触发构建和部署流程。
- 让人们探索一个更加结构化的提交历史,以便降低对你的项目作出贡献的难度。