在有了Husky赋能以后,咱们有能力在Git的钩子里作一些事情,首先不得不提的是代码的提交规范和规范的校验,优雅的提交,方便团队协做和快速定位问题。首推Commitlint,另外@加神 推荐了Gitmoji也是一个颇有意思的工具。git
npm install --save-dev @commitlint/config-conventional @commitlint/cli // 生成配置文件commitlint.config.js,固然也能够是 .commitlintrc.js echo "module.exports = {extends: ['@commitlint/config-conventional']};" > commitlint.config.js
在husky的配置加入CommitlIint配置,v1.0.1
版本之后为HUSKY_GIT_PARAMS
,v0.14.3
为GIT_PARAMS
github
"husky": { "hooks": { "pre-commit": "npm run test", "commit-msg": "commitlint -e $HUSKY_GIT_PARAMS" } },
<type>: <subject>
例子:npm
git commit -m 'feat: 增长 xxx 功能' git commit -m 'bug: 修复 xxx 功能'
subject是 commit 目的的简短描述,能够作一些配置,如最大长度限制。数组
rule配置说明::rule由name和配置数组组成,如:'name:[0, 'always', 72]'
,数组中第一位为level,可选0,1,2
,0为disable,1为warning,2为error,第二位为应用与否,可选always|never
,第三位该rule的值。具体配置例子以下:工具
module.exports = { extends: [ "@commitlint/config-conventional" ], rules: { 'type-enum': [2, 'always', [ 'upd', 'feat', 'fix', 'refactor', 'docs', 'chore', 'style', 'revert' ]], 'type-case': [0], 'type-empty': [0], 'scope-empty': [0], 'scope-case': [0], 'subject-full-stop': [0, 'never'], 'subject-case': [0, 'never'], 'header-max-length': [0, 'always', 72] } };
这里列出了大部分经常使用的配置,其它的能够参考Commitlint网站,具体使用例子:测试
这里咱们使用错误的提交方式,最上面的是自动测试的脚本,你们能够忽略,husky给出了commit-msg的input为xxx,触发了subject-empty
,type-empty
两个规则,提交不符合规范,被拦了下来。若是是正确的提交,例子以下:网站
关于Commitlint的使用就到这里了。spa