纵观那些 GitHub 上 star 不少的开源库,开发流程都是极其规范的,Commitizen 即是用来规范化 git 提交信息的一个好工具。node
commitizen
一般要与适配器一块儿使用,通俗点来讲是须要一个 commit message 模板,目前主流的是符合 Angular 规范的 cz-conventional-changelog
。git
npm i -g commitizen
npm i -g cz-conventional-changelog
echo '{ "path": "cz-conventional-changelog" }' > ~/.czrc
复制代码
以上命令表明全局使用 cz-conventional-changelog
适配器,你也能够经过如下命令来局部安装适配器github
# 这种方式是使用npm来安装
commitizen init cz-conventional-changelog --save-dev --save-exact
# 这种方式是使用yarn来安装
commitizen init cz-conventional-changelog --yarn --dev --exact
复制代码
假如你已经全局安装了适配器,那么上面的命令会报 A previous adapter is already configured. Use --force to override,如它所说,只须要加上 --force
参数便可强制使用局部适配器,成功后会在本地局部安装 cz-conventional-changelog
,并在 package.json
中写入如下内容npm
{
"devDependencies": {
"cz-conventional-changelog": "^2.1.0"
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
}
}
复制代码
安装成功后便可经过命令 git-cz
来代替 git commit
进行提交了json
git-cz
具备 git commit
一切参数,就像这样: git-cz -a
bash
VSCode
用户能够安装一个 Visual Studio Code Commitizen Support 扩展以使用更友好的 commit 界面, 按下 F1
,输入 conventional commit
,效果以下ide
每次更新版本后会自动将 package.json(et al.)
和 CHANGELOG.md
提交工具
必须确保 Commit Message 符合 Conventional Commits 规范哦!上面使用的
cz-conventional-changelog
是符合这个规范的。post
这里我选择全局安装优化
npm i -g standard-version
复制代码
方便起见,首先新增一个 npm run script
{
"version": "1.0.0",
"scripts": {
"release": "standard-version"
}
}
复制代码
发布初版时运行如下命令,这条命令不会修改版本号
npm run release -- -f
# or
standard-version -f
复制代码
npm run release
# or
standard-version
复制代码
npm run release -- -p
# or
standard-version -p
复制代码
会获得一个相似 1.0.1-0
1.0.1-1
... 这种版本 若是但愿为预发布版本命名,能够经过 --prerelease <name>
指定名称。 例如,假设您的预发行版应该包含 alpha 前缀
npm run release -- -p alpha
# or
standard-version -p alpha
复制代码
这将获得一个 1.0.1-alpha.0
版本
standard-version
遵循 Semver 语义化版本规范,假如你上次 commit 时选择的是 fix
,这时默认应该更新的是个 path
版本,若是你想手动选择版本,能够这样
npm run release -- -r minor
# or
standard-version -r 1.1.0
复制代码
当 release 后,程序会自动将修改后的 package.json
和 CHANGELOG.md
文件 commit 掉,因此当使用 pre-commit
这种钩子时候可能就会报错,能够经过 --no-verify
跳过检测
npm run release -- --no-verify
# or
standard-version -n
复制代码
standard-version
支持一些生命周期脚本
prerelease
: 发布以前prebump / postbump
: 版本号更新以前 / 以后prechangelog / postchangelog
: changelog 生成以前 / 以后precommit / postcommit
: package.json 和 changelog 文件提交以前 / 提交以后pretag / posttag
: 打 tag 以前 / 以后就像这样,你能够在某个声明周期中偷摸作点事情 🤡
{
"standard-version": {
"scripts": {
"prebump": "echo 9.9.9"
}
}
}
复制代码
changelog
中 issue 地址默认是 GitHub 上的,若是想修改为 Jira
的地址,能够经过 postchangelog
配合 replace 库来修改它的连接地址
{
"standard-version": {
"scripts": {
"postchangelog": "replace 'https://github.com/myproject/issues/' 'https://myjira/browse/' CHANGELOG.md"
}
}
}
复制代码
你也能够跳过某些生命周期 (bump
, changelog
, commit
, tag
) ,假如不想它自动打 tag
,你能够这样
{
"standard-version": {
"skip": {
"tag": true
}
}
}
复制代码
默认打的 tag 前缀是 v,生成的 tag 都是相似 v1.0.0 这种,能够经过 -t
设置前缀
npm run release -- -t @scope/package\@
# or
standard-version -t @scope/package\@
复制代码
生成的 tag 看起来像这样 @scope/package@2.0.0
npm run release -- -h
# or
standard-version -h
复制代码
除此以外,还能够利用 husky
+ prettier
eslint
commitlint
lint-staged
等工具进一步优化 git 流程,利用 husky
的 pre-commit
钩子,能够在 commit 以前处理一些事情,就像