1.安装commitizen和cz-customizable
npm install -g commitizen@4.2.4
npm i cz-customizable@6.3.0 --save-dev
2.在package.json中进行新增
"config": {"commitizen": {"path": "node_modules/cz-customizable"}
}
{"name": "item_3","version": "0.1.0","private": true,"scripts": {"serve": "vue-cli-service serve","build": "vue-cli-service build","lint": "vue-cli-service lint","prepare": "husky install"},"dependencies": {"axios": "^0.24.0","core-js": "^3.6.5","vue": "^3.2.8","vue-i18n": "^9.2.0-beta.26","vue-router": "^4.0.11","vuex": "^4.0.2"},"devDependencies": {"@commitlint/cli": "^12.1.4","@commitlint/config-conventional": "^12.1.4","@vue/cli-plugin-babel": "~4.5.0","@vue/cli-plugin-eslint": "~4.5.0","@vue/cli-plugin-router": "~4.5.0","@vue/cli-plugin-vuex": "~4.5.0","@vue/cli-service": "~4.5.0","@vue/compiler-sfc": "^3.0.0","@vue/eslint-config-standard": "^5.1.2","babel-eslint": "^10.1.0","cz-customizable": "^6.3.0","eslint": "^6.7.2","eslint-plugin-import": "^2.20.2","eslint-plugin-node": "^11.1.0","eslint-plugin-promise": "^4.2.1","eslint-plugin-standard": "^4.0.0","eslint-plugin-vue": "^7.0.0","husky": "^7.0.1","sass": "^1.26.5","sass-loader": "^8.0.2","svg-sprite-loader": "^6.0.9","unplugin-auto-import": "^0.5.11","unplugin-vue-components": "^0.17.11"},"eslintConfig": {"root": true,"env": {"node": true},"extends": ["plugin:vue/vue3-essential","@vue/standard"],"parserOptions": {"parser": "@babel/eslint-parser"},"rules": {}},"browserslist": ["> 1%","last 2 versions","not dead","not ie 11"],"config": {"commitizen": {"path": "node_modules/cz-customizable"}},"lint-staged": {"src/**/*.{js,vue}": ["eslint --fix","git add"]}
}
3.在根目录下新建.cz-config.js文件并写入配置 之后就可以用 git cz 来代替 git commit
module.exports = {// 可选类型types: [{ value: 'feat', name: 'feat: 新功能' },{ value: 'fix', name: 'fix: 修复' },{ value: 'docs', name: 'docs: 文档变更' },{ value: 'style', name: 'style: 代码格式(不影响代码运行的变动)' },{value: 'refactor',name: 'refactor: 重构(既不是增加feature,也不是修复bug)'},{ value: 'perf', name: 'perf: 性能优化' },{ value: 'test', name: 'test: 增加测试' },{ value: 'chore', name: 'chore: 构建过程或辅助工具的变动' },{ value: 'revert', name: 'revert: 回退' },{ value: 'build', name: 'build: 打包' }],// 消息步骤messages: {type: '请选择提交类型:',customScope: '请输入修改范围(可选):',subject: '请简要描述提交(必填):',body: '请输入详细描述(可选):',footer: '请输入要关闭的issue(可选):',confirmCommit: '确认使用以上信息提交?(y/n/e/h)'},// 跳过问题skipQuestions: ['body', 'footer'],// subject文字长度默认是72subjectLimit: 72
}

1.使用husky进行强制git代码提交规范
npm install --save-dev @commitlint/config-conventional@12.1.4 @commitlint/cli@12.1.4
npm install husky@7.0.1 --save-dev
npx husky install
多出一个.husky文件
2.导入配置文件

```javascript
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]}
}
3.在package.json中新增指令
"prepare": "husky install"

4.并执行
npm run prepare
5.新增husky配置文件 并往里面写入
npx husky add .husky/commit-msg
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"npx --no-install commitlint --edit
写
npx --no-install commitlint --edit

效果

5.使用husky强制代码格式化 创建配置文件
npx husky add .husky/pre-commit
6.往第六步生成的文件中写入
npx lint-staged
8.把package.json文件的lint-staged修改为
"lint-staged": {"src/**/*.{js,vue}": [ //src目录下所有的js和vue文件"eslint --fix", // 自动修复"git add" // 自动提交时修复]}