通过commit-message规范评审,在业界经常使用的:atom,eslint和Angular等规范中, 能够选择最经常使用的Angular规范做为咱们平常项目中的提交规范node
每条提交记录包含三个部分:header,body和footergit
<header> <BLANK LINE> <body> <BLANK LINE> <footer>
Commit Message Headergithub
<type>(<scope>): <short summary> │ │ │ │ │ └─⫸ Summary in present tense. Not capitalized. No period at the end. │ │ │ └─⫸ Commit Scope: animations|bazel|benchpress|common|compiler|compiler-cli|core| │ elements|forms|http|language-service|localize|platform-browser| │ platform-browser-dynamic|platform-server|router|service-worker| │ upgrade|zone.js|packaging|changelog|dev-infra|docs-infra|migrations| │ ngcc|ve │ └─⫸ Commit Type: build|ci|docs|feat|fix|perf|refactor|test
其中<type>和<summary>是必要的 <scope>是可选的shell
经过commitizen进行交互式提交,husky + commit-msg hook进行提交校验,cz-customizable来自定义交互提交流程和文案
安装commitizennpm
npm i -D commitizen
package.json中添加对应的npm scriptjson
"commit":"cz"
改动添加到暂存区后执行commit提交api
npm run commit
安装husky , commitlint 和 符合angular提交规范的配置bash
npm i -D husky commitlint @commitlint/config-conventional
添加git hooks工具
npx husky install
package.json中添加prepare的npm hook, 在每次install自动执行(yarn v2+不支持prepare)性能
"prepare": "husky install"
执行添加commit-msg hook
若是使用husk v4.x版本(推荐升级到新版本),直接在package.json中或.huskyrc.json中新增commit-msg钩子便可
package.json
"husky": { "hooks": { "commit-msg": "commitlint --edit $1" } }
.huskyrc
,.huskyrc.json
,.huskyrc.js
或husky.config.js
"hooks": { "commit-msg": "commitlint --edit $1" }
若是使用husky v6+版本,须要添加对应的shell调用方式(husky v6对添加方式作了改动,因此没法经过添加配置到package.json中运行)
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit $1'
添加commintlint配置(也能够放到package.json中指定)
echo "module.exports = { extends: ['@commitlint/config-conventional'] };" > commitlint.config.js
package.json中添加commitlint配置
"commitlint": { "extends": [ "@commitlint/config-conventional" ] }
commitizen提供的交互式默认是英文的,若是改为中文或者对交互流程进行改动,可使用cz-customizable进行扩展和自定义
安装cz-customizable
npm i -D cz-customizable
package.json中添加配置
"config": { "commitizen": { "path": "node_modules/cz-customizable" }, "cz-customizable": { "config": ".cz-config.js" } }
.cz-config.js就是cz-customizable配置的具体文件了,能够参考CZ-Config-Example并进行改动, 能够把文案翻译成中文,自定义修改提示等。
也能够经过fork cz-customizable建立内封配置文件的npm包
npm i -D your-own-package
"config": { "commitizen": { "path": "node_modules/your-own-package" } }
配置文件能够自定义交互内容,好比能够只保留type scope breakchange confirm
配置文件中设置skipQuestions: ['body','customScope','scope','footer'],便可忽略其余选项
allowBreakingChanges: ['feat', 'fix'], 仅在feat和fix时提示 breakchange
经过npm script进行commit,若是eslint没有经过(在pre-commit 钩子中作了eslint检测),可是又想提交能够经过加'––'来向npm script传参
npm run commit -- --no-verify # or npm run commit -- -n