git代码提交规范
1.安装commitizen cz-conventional-changelog
- 安装相关依赖
yarn add commitizen -D
yarn add cz-conventional-changelog -D
2.在相关package.json添加配置
{
"scripts": {
...
"commit": "git add . && git cz"
}
...
"config": {
"commitizen": {
"path": "node_modules/cz-conventional-changelog"
}
}
}
2.安装husky+commitlint
1.安装相关依赖
yarn add @commitlint/config-conventional @commitlint/cli husky -D
2.配置commitlint
新建commitlint.config.js文件,并添加下面的内容:
module.exports = {
extends: ['@commitlint/config-conventional'],
// 检测规则
rules: {
'type-enum': [
2,
'always',
[
'feat',
'fix',
'docs',
'style',
'refactor',
'perf',
'test',
'build',
'ci',
'chore',
'revert',
]
],
'header-max-length': [2, 'always', 72],
'header-min-length': [2, 'always', 5],
'body-min-length': [2, 'always', 5],
'type-case': [0],
'type-empty': [0],
'scope-empty': [0],
'scope-case': [0],
'subject-full-stop': [0, 'never'],
'subject-case': [0, 'never'],
}
}
3.配置husky钩子
# 激活husky钩子
npx husky install
# 添加husky的commit-msg钩子,在提交前对提交信息进行检查
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'
4.在相关package.json添加配置
{
"scripts":{
...
"prepare": "husky install"
}
}