说明
- 标准化 Git Commit 工作流方案(一)标准化 Git Commit 工作流实现
- 标准化 Git Commit 工作流方案(二)Lerna 项目中的 commit 工作流实现与提交规范定制化
Lerna 项目中实现
安装相关依赖
yarn add -D -W commitizen
yarn add -D -W cz-lerna-changelog # 专门为 lerna 项目设计的 commitizen 适配器
yarn add -D -W @commitlint/cli @commitlint/config-conventional husky # 安装 commitlint 校验工具及 husky
lerna.json 中开启对 CHANGELOG 的记录
{
"command": {
"version": {
"conventionalCommits": true,
"ignoreChanges": [
"*.md"
]
}
}
}
按照新的规范提交几个 commit ,然后执行
yarn lerna:publish
可以看到处理 root 层生成了一个 CHANGELOG.md 外,如果 packages 中有哪个有改动,那么这个 package 下也会有 CHANGELOG.md 变化
与 standard-version 类似 默认情况下只有 类型为 feat 、fix 的 commit 会被记录到 changelog 中,如果想要记录其他类型的 例如:docs , 可以按照如下处理
yarn add -D -W conventional-changelog-conventionalcommits # 下载一个预设的构建函数
lerna.json 配置文件中指定预设配置,更多的配置参数说明可以看这里
{
"version": {
"conventionalCommits": true,
"changelogPreset": {
"name": "conventionalcommits", // conventional-changelog- 这里的 name 是安装包的缩写
"types": [
{ "type": "feat", "section": "Features"},
{"type": "fix", "section": "Bug Fixes"},
{"type": "docs", "section": "Docs"},
// ... // 想要展示什么 type 到 changelog 中,在这里枚举
],
// 以下配置可以不写,但是在执行生成 changelog 的过程中会自动填充
"issuePrefixes": ["#"],
"issueUrlFormat": "{{host}}/{{owner}}/{{repository}}/issues/{{id}}",
"commitUrlFormat": "{{host}}/{{owner}}/{{repository}}/commit/{{hash}}",
"compareUrlFormat": "{{host}}/{{owner}}/{{repository}}/compare/{{previousTag}}...{{currentTag}}",
"userUrlFormat": "{{host}}/{{user}}"
}
}
}
}
再次执行
lerna publish
就可以得到想要的 changelog 了(注意,只有在当前 publish 会有新的版本生成的时候才会有 changelog 更新,否则是不会引起变化的)
定制化 commit message 提交规范
通过 commitizen 与 commitlint 可以快速的实现 commit message 的规范化,但是这些规范不一定适用于我们的项目,commitizen 可以通过适配器将一些社区中的成熟方案引用,除此之外我们也可以定制化一套 commit message 提交规范
定制化分为三个部分:
- commit message 的格式定制化
- commitlint 通过配置实现对定制化 commit message 的适应
- changelog 的定制化配置来实现 对 定制化 commit message 的处理
changelog 的定制化已经在上边提到了,下边说下 commitizen 与 commitlint 的定制化
1. 普通项目的定制化
如果 commitizen 使用的是默认 adapter
cz-conventional-changelog
那么可以参考 cz-conventional-changelog 文档
在 package.json 中定义
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog",
"disableScopeLowerCase": false,
"disableSubjectLowerCase": false,
"maxHeaderWidth": 100,
"maxLineWidth": 100,
"defaultType": "",
"defaultScope": "",
"defaultSubject": "",
"defaultBody": "",
"defaultIssues": "",
"types": {
"feat": {
"description": "A new feature",
"title": "Features"
}
// ... 更多的 type
}
}
}
还有一种方案是使用 cz-customizable 它也是 commitizen 的适配器,不同的时,它允许在根目录创建一个 .cz-config.js 文件,在这里可以配置commitizen 的交互信息,同时结合 commitlint 实现正确的 风格控制
yarn add -D cz-customizable
更多可配置项查看
// .cz-config.js
module.exports = {
types: [
{ value: 'init', name: 'init: 初始提交' },
{ value: 'feat', name: 'feat: 增加新功能' },
{ value: 'fix', name: 'fix: 修复bug' },
// ...
],
scopes: [],
messages: {
type: '选择更改类型:\n',
scope: '更改的范围:\n',
subject: '简短描述:\n',
body: '详细描述. 使用"|"换行:\n',
breaking: 'Breaking Changes列表:\n',
footer: '关闭的issues列表. E.g.: #31, #34:\n',
confirmCommit: '确认提交?'
},
};
commitlint 可以在 根目录的 commitlint.config.js 中定制化,更多的 rules 配置查看
module.exports = {
extends: ["@commitlint/config-conventional"],
rules: {
'type-enum': ['feat', 'fix', 'docs', 'style', 'refactor', 'test', 'revert'],
'type-case': 'sentence-case'
}
};
由于我们使用了 cz-customizable 很多规则已经变化(主要是 type 变化),受其影响 commitlint 的 config 也要改动,这样看起来比较麻烦, commitlint-config-cz 是一个 commitlint 的配置,有了它就不需要手动改了
yarn add -D commitlint-config-cz
然后改动 commitlint.config.js
// commitlint.config.js
module.exports = {
extends: ['cz']
}
2. Lerna 项目的定制化
我们的 lerna 项目使用了
cz-lerna-changelog
作为适配器,这个适配器是 forkcz-conventional-changelog
这个适配器的,cz-conventional-changelog
支持在 package.json 中通过配置做一些定制化,但是看了cz-lerna-changelog
的源码后发现他并不支持,因此,如果我们需要对其进行定制化,那么我们需要 forkcz-lerna-changelog
在其中进行一些修改, 而 commitlint 则需要按照上边手动配置一下