commitizen/cz-cli是一个规范化提交信息的工具,当我们在多人协作开发过程中,有必要养成良好的commit
规范,方便日后review
代码,产出CHANGELOG
说明等等;
本地安装commitizen
npm install --save-dev commitizen
定制项目的提交说明,可以使用cz-customizable适配器:
npm install cz-customizable --save-dev
在package.json
加上配置使用node_modules/cz-customizable
适配器
"config": {
"commitizen": {
"path": "node_modules/cz-customizable"
}
}
cz-customizable
默认会去读取项目中.cz-config.js
配置,所以我们还要新建文件.cz-config.js
,官方也提供了一个示例,我们只要复制里面的内容做一下修改处理即可(比如汉化 or 加上gitmoji
表情)。
处理如下:
'use strict';
module.exports = {
types: [
{
value: ':construction: WIP',
name: '💪 WIP: Work in progress'
},
{
value: ':sparkles: feat',
name: '✨ feat: A new feature'
},
{
value: ':bug: fix',
name: '🐛 fix: A bug fix'
},
{
value: ':hammer: refactor',
name: '🔨 refactor: A code change that neither fixes a bug nor adds a feature'
},
{
value: ':pencil: docs',
name: '📝 docs: Documentation only changes'
},
{
value: ':white_check_mark: test',
name: '✅ test: Add missing tests or correcting existing tests'
},
{
value: ':thought_balloon: chore',
name: '🗯 chore: Changes that don\'t modify src or test files. Such as updating build tasks, package manager'
},
{
value: ':lipstick: ui',
name: '💄 Updating the UI and style files.',
},
{
value: ':art: style',
name:
'🎨 Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)',
},
{
value: 'revert',
name: '⏪ revert: Revert to a commit'
},
{
value: ':package: dep_up',
name: '📦 Updating compiled files or packages.',
},
{
value: ':green_heart: fixci',
name: '💚 Fixing CI Build.',
},
{
value: ':truck: mv',
name: '🚚 Moving or renaming files.',
},
{
value: ':fire: prune',
name: '🔥 Removing code or files.',
},
{
value: ':bookmark: release',
name: '🔖 Releasing / Version tags.',
},
{
value: ':rocket: first release',
name: '🚀 first releast!',
}
],
scopes: [],
allowCustomScopes: true,
allowBreakingChanges: ["feat", "fix"]
};
好了,我们看下效果
> git add . // 需要add先
> npx git cz // 注意:使用npx命令
正常git
流程应该是git add .
> git commit -m 'xx'
, 这里我们用 npx git cz
取代git commit
操作
提交校验
这里分两步走,需要安装commitlint和husky
第一步 Install commitlint
为了防止项目中提交不符合规范的commit
信息,我们还需要接入另外一个工具commitlint
npm install --save-dev @commitlint/{cli,config-conventional}
使用commitlint-config-cz对定制化提交说明进行校验。
npm install commitlint-config-cz --save-dev
在根目录下新建.commitlintrc.js
,配置声明继承cz
'use strict';
module.exports = {
extends: ["cz"],
rules: {
'type-empty': [2, 'never'],
'subject-empty': [2, 'never']
}
};
第二步 Install husky
husky
是一个Git hooks
工具,内置了许多钩子处理命令,我们在git commit
的时候需要校验commit
信息,这时候husky
就派上用场了。
npm install --save-dev husky
// package.json
{
"husky": {
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
}
}
试试运行下:
为什么这里还会报错??找了下 issues#39,发现是commitlint
不支持emoji
表情,那我们把上面的.cz-config.js
里的emoji
表情去掉就可以了。
那我们想要支持emoji
表情校验怎么办?请看第三步:
第三步:支持emoji表情校验(可选)
如果
commit
信息没有表情,可以跳过此步骤
在issues
找到一个库commitlint-config-gitmoji可以解决这个问题,
npm i -D commitlint-config-gitmoji @commitlint/core
Add commitlint config for Gitmoji
'use strict';
module.exports = {
+ extends: ["./node_modules/commitlint-config-gitmoji","cz"],
rules: {
+ 'type-empty': [2, 'never',[
+ ':art:',
+ ':newspaper:',
+ ':pencil:',
+ ':memo:',
+ ':zap:',
+ ':fire:',
+ ':books:',
+ ':bug:',
+ ':ambulance:',
+ ':penguin:',
+ ':apple:',
+ ':checkered_flag:',
+ ':robot:',
+ ':green_ale:',
+ ':tractor:',
+ ':recycle:',
+ ':white_check_mark:',
+ ':microscope:',
+ ':green_heart:',
+ ':lock:',
+ ':arrow_up:',
+ ':arrow_down:',
+ ':fast_forward:',
+ ':rewind:',
+ ':rotating_light:',
+ ':lipstick:',
+ ':wheelchair:',
+ ':globe_with_meridians:',
+ ':construction:',
+ ':gem:',
+ ':bookmark:',
+ ':tada:',
+ ':loud_sound:',
+ ':mute:',
+ ':sparkles:',
+ ':speech_balloon:',
+ ':bulb:',
+ ':construction_worker:',
+ ':chart_with_upwards_trend:',
+ ':ribbon:',
+ ':rocket:',
+ ':heavy_minus_sign:',
+ ':heavy_plus_sign:',
+ ':wrench:',
+ ':hankey:',
+ ':leaves:',
+ ':bank:',
+ ':whale:',
+ ':twisted_rightwards_arrows:',
+ ':pushpin:',
+ ':busts_in_silhouette:',
+ ':children_crossing:',
+ ':iphone:',
+ ':clown_face:',
+ ':ok_hand:',
+ ':boom:',
+ ':bento:',
+ ':pencil2:',
+ ':package:',
+ ':alien:',
+ ':truck:',
+ ':age_facing_up:',
+ ':busts_in_silhouette:',
+ ':card_file_box:',
+ ':loud-sound:',
+ ':mute:',
+ ':egg:',
+ ':see-no-evil:',
+ ':camera-flash:',
+ ':alembic:',
+ ':mag:',
+ ':wheel-of-dharma:',
+ ':label:'
+ ]],
'subject-empty': [2, 'never']
}
};
生成changelog日志
当我们要发布版本的时候,通常的做法是,更新package.json
里的版本号,更新CHANGELOG
,描述了更新了什么操作,然后git add
-> git commit
,打上tag
标签,有了standard-version,可以帮我们自动跑上面操作。
安装
npm i --save-dev standard-version
执行
npx standard-version
写入script
我们将上述npx
命令写入package.json
script
中方便调用
// package.json
"scripts": {
"commit": "git-cz",
"release": "standard-version",
"push:publish": "npm run build && git push --follow-tags origin master && npm publish"
},
以上,完~
参考链接
掘金 - Cz工具集使用介绍 - 规范Git提交说明
GitHub - cz-cli工具
GitHub - cz-customizable适配器
commitlint校验
husky钩子
GitHub - commitlint-config-gitmoji 表情支持
standard-version发布版本
commitlint-config-cz/issues#39
commitlint/issues#1221