1、使用husky添加git hooks(基于husky新版6.0.0进行git hooks)
1、npm install husky --save-dev
2、在package.json中添加prepare脚本
“prepare”: “husky install”
3、执行npm run prepare,本地会创建.husky目录
4、添加git hooks —(如果不需要git提交进行其他操作,直接跳过)
创建一条 pre-commit hook
npx husky add .husky/pre-commit "npm run test"
会在.husky下找到这个文件,此处可以执行命令
6,添加commit-msg钩子,规范commit message信息
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'
创建了hook之后,在每次执行 git commit 时,都会先触发hook,来执行指定命令
2、添加commitlint校验
1,添加对应约束,防止随意提交
npm install --save-dev @commitlint/config-conventional @commitlint/cli
2,在项目根目录下建立配置文件 .commitlint.config.js
module.exports = {
extents:[
"@commitlint/config-conventional"
],
rules:{
'body-leading-blank': [1, 'always'],
'footer-leading-blank': [1, 'always'],
'header-max-length': [2, 'always', 72],
'scope-case': [2, 'always', 'lower-case