目标:通过规范的 git commit 信息生成 change log。
方案:conventional-changelog 生态
0. 安装相关模块
全局:
commitizen
Dev:
conventional-changelog-cli
@commitlint/cli
@commitlint/config-conventional
- 使用 cz 规范提交
package.json 添加 commitizen 配置:
{
“config”: {
“commitizen”: {
“path”: “cz-conventional-changelog”
}
}
}
安装配置完成后,git add 后可以使用 cz 、 git-cz 命令提交规范的 commit 信息。
但是这还不够,我们还需要保证 commit 信息的格式的一致性,要对 commit 信息也做 lint 校验。
- 添加 .commitlint.js 配置文件:
module.exports = {
extends: [‘@commitlint/config-conventional’],
rules: {
‘type-enum’: [
2,
‘always’,
[‘build’, ‘ci’, ‘chore’, ‘docs’, ‘feat’, ‘fix’, ‘perf’, ‘refactor’, ‘revert’, ‘style’, ‘test’],
],
},
}; - 添加 commit lint
{
// …
“husky”: {
“hooks”: {
// …
“commit-msg”: “commitlint —config .commitlintrc.js -E HUSKY_GIT_PARAMS”
}
}
}
4.生成 change Log
package.json 添加 scripts:
{
// …
“scripts”: {
// …
“changelog”: “conventional-changelog -p angular -i CHANGELOG.md -s -r 1”
}
}
-p 指定 change log 使用的规范, -i 读取 change log 的文件, -s 表示读写 changelog 为同一文件,-r 可以指定生成对比最近多少个 tag 的 changlog ,默认为1个。
发布完成后即可通过 yarn changelog 命令将规范的 change log 信息输出到 CHANGELOG.md 文件中。
参考:
https://git.garena.com/xinfeng.liu/react-ts-template/-/tree/master/
https://zhuanlan.zhihu.com/p/51894196
https://commitlint.js.org/#/guides-local-setup
https://github.com/conventional-changelog/commitlint#readme