首先安装
1、npm install --save-dev husky
然后在package.json里面加入
1 "husky": { 2 "hooks": { 3 "commit-msg": "commitlint -E HUSKY_GIT_PARAMS" 4 } 5 },
2、npm install @commitlint/cli @commitlint/config-conventional --save-dev
新建文件
commitlint.config.js
1 module.exports = { 2 extends: ['@commitlint/config-conventional'], 3 rules: { 4 'type-enum': [ 5 2, 6 'always', 7 ['Feat', 'Fix', 'Doc', 'Style','Update', 'Refactor', 'Test', 'Framework', 'Revert'] 8 ], 9 'type-case': [0,'always','start-case'], 10 'type-empty': [0], 11 'scope-empty': [0], 12 'scope-case': [0], 13 'subject-full-stop': [0, 'never'], 14 'subject-case': [0, 'never'], 15 'header-max-length': [0, 'always', 72] 16 } 17 }
npm install cz-customizable --save-dev
新建文件
.cz-config.js
1 module.exports = { 2 types: [ 3 {value: 'Feat', name: '特性: 一个新的特性'}, 4 {value: 'Update', name: '更新: 更新一个功能'}, 5 {value: 'Fix', name: '修复: 修复一个Bug'}, 6 {value: 'Style', name: '样式: 变更的只有样式'}, 7 {value: 'Doc', name: '文档: 变更的只有文档'}, 8 {value: 'Refactor', name: '重构: 代码重构,注意和特性、修复区分开'}, 9 {value: 'Test', name: '测试: 添加一个测试'}, 10 {value: 'Framework', name: '框架: 开发框架变动(构建、脚手架工具等)'}, 11 {value: 'Revert', name: '回滚: 代码回退'} 12 ], 13 scopes: [ 14 {name: '系统框架'}, 15 {name: '公共组件'}, 16 ], 17 allowTicketNumber: false, 18 isTicketNumberRequired: false, 19 ticketNumberPrefix: 'TICKET-', 20 ticketNumberRegExp: '\d{1,5}', 21 22 // it needs to match the value for field type. Eg.: 'fix' 23 /* 24 scopeOverrides: { 25 fix: [ 26 27 {name: 'merge'}, 28 {name: 'style'}, 29 {name: 'e2eTest'}, 30 {name: 'unitTest'} 31 ] 32 }, 33 */ 34 // override the messages, defaults are as follows 35 messages: { 36 type: '选择一种你的提交类型:', 37 scope: '选择一个scope (可选):', 38 // used if allowCustomScopes is true 39 customScope: 'Denote the SCOPE of this change:', 40 subject: '短说明: ', 41 body: '长说明,使用"|"换行(可选): ', 42 breaking: '非兼容性说明 (可选): ', 43 footer: '关联关闭的issue,例如:#31, #34(可选): ', 44 confirmCommit: '确定提交说明?' 45 }, 46 47 allowCustomScopes: true, 48 // allowBreakingChanges: ['特性', 'Fix'], 49 // skip any questions you want 50 skipQuestions: ['body'], 51 52 // limit subject length 53 subjectLimit: 72, 54 // breaklineChar: '|', // It is supported for fields body and footer. 55 // footerPrefix : 'ISSUES CLOSED:' 56 // askForBreakingChangeFirst : true, // default is false 57 };
最后配置package.json
"commit": "node ./node_modules/cz-customizable/standalone.js"
使用git add .之后就是用npm run commit 来做本地git代码暂存
提交之前应该在做一次代码检查
"husky": { "hooks": { "commit-msg": "commitlint -E HUSKY_GIT_PARAMS", "pre-commit": "lint-staged --allow-empty" } }, "lint-staged": { "src/**/*.{ts,tsx,js,jsx}": [ "eslint --fix" ] },
加上这个配置commit的时候就会自动检测代码是否符合要求,不符合会报错终止代码提交
参考文档
https://segmentfault.com/a/1190000017790694
https://blog.csdn.net/weixin_33890526/article/details/91393527