要完成以上两个目标,我们需要使用两个工具:
1、commitlint: 用于检查提交信息
2、husky: 是gits hook 工具
注意:npm 需要在7.x以上版本!!!!
npm install --save-dev @commitlint/config-conventional@12.1.4 @commitlint/cli@12.1.4
echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js
module.exports = {
// 继承的规则
extends: ['@commitlint/config-conventional'],
// 定义规则类型
rules: {
// type类型定义,表示 git 提交的 type 必须在以下类型范围内
'type-enum': [
2,
'always',
[
'feat', // 新功能 feature
'fix', // 修复bug
'docs', // 文档注释
'style', // 代码格式(不影响代码运行的变动)
'refactor', // 重构(既不增加新功能,也不是修复bug)
'perf', // 性能优化
'test', // 增加测试
'chore', // 构建过程或者辅助工具的变更
'revert', // 回退
'build' // 打包
]
],
// subject 大小写不做校验
'subject-case': [0],
// subject建议150字以内
'subject-max-length': [1, 'always', 150]
}
}
npm install husky@7.0.1 --save-dev
npx husky install

prepare 指令(需要npm >7.0 版本)npm set-script prepare "husky install"

npm run prepare

npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'

至此,不符合规范的 commit 将不再可提交:

我们期望通过husky 监测 pre-commit 钩子,在该钩子下执行 npx eslint --ext .js,.vue src 指令来进行相关检测:


用到一个插件 lint-staged,无需单独安装,我们生成项目时, vue-cli 已经帮我们安装过了,所以我们直接使用就可以了
"lint-staged":{
"src/**/*.{js,vue}":[
"eslint --fix",
"git add"
]
}
