通过 ESLint
+ Prettier
+ VSCode 配置
配合进行了处理。最终达到在保存代码时,自动规范化代码格式的目的
.prettierrc
注意:复制过去时,不能加注释!!!!
{
// 不尾随分号
"semi": false,
// 使用单引号
"singleQuote": true,
// 多行逗号分割的语法中,最后一行不加逗号
"trailingComma": "none"
}
.eslintrc.js
在 rules 下新增 'space-before-function-paren': 'off'
约定式提交
当使用 commitizen 进行代码提交(git commit)时,commitizen 会提交你在提交时填写所有必需的提交字段!
cnpm install -g commitizen
通过 cz-customizable 自定义提交规范
cnpm i --save-dev cz-customizable
"config": {
"commitizen": {
"path": "node_modules/cz-customizable"
}
}
.cz-config.js
自定义提示文件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文字长度默认是72
subjectLimit: 72
}
git cz
代替 git commit,即可看到提示内容用到的两个hooks
commit-msg
- 用来规范化标准格式
- 由 git commit 和 git merge 调用
- 可以按需指定是否要拒绝本次提交,使用 git commit --no-verify 绕过
pre-commit
- 会在提交前被调用,由
git commit
调用- 可以按需指定是否要拒绝本次提交,使用 git commit --no-verify 绕过
commitlint:用于检查提交信息
husky:是git hooks
工具
注意:npm
需要在 7.x 以上版本!!!!!
cnpm install --save-dev @commitlint/config-conventional @commitlint/cli
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]
}
}
注意:确保保存为 UTF-8
的编码格式,否则可能报错
cnpm install --save-dev husky
hooks
, 生成 .husky
文件夹npx husky install
package.json
中生成 prepare
指令( 需要 npm > 7.0 版本 )npm set-script prepare "husky install"
prepare
指令npm run prepare
执行成功,提示
添加 commitlint
的 hook
到 husky
中,并指定在 commit-msg
的 hooks
下执行 npx --no-install commitlint --edit "$1"
指令
npx husky add .husky/commit-msg ‘npx --no-install commitlint --edit “$1”’
.husky
的文件结构至此, 不符合规范的 commit 将不再可提交
强制规范化的提交要求,到现在 不符合规范的提交信息,将不可在被提交!
通过
husky
监测pre-commit
钩子,在该钩子下执行npx eslint --ext .js,.vue src
指令来去进行相关检测:
npx husky add .husky/pre-commit "npx eslint --ext .js,.vue src"
添加 commit
时的 hook
(npx eslint --ext .js,.vue src
会在执行到该 hook 时运行)npx husky add .husky/pre-commit "npx eslint --ext .js,.vue src"
pre-commit
:lint-staged 可以让你当前的代码检查 只检查本次修改更新的代码,并在出现错误的时候,自动修复并且推送
lint-staged 无需单独安装,我们生成项目时,vue-cli
已经帮助我们安装过了,所以我们直接使用就可以了
修改 package.json
配置
"lint-staged": {
"src/**/*.{js,vue}": [
"eslint --fix",
"git add"
]
}
如上配置,每次它只会在你本地 commit
之前,校验你提交的内容是否符合你本地配置的 eslint
规则(这个见文档 ESLint ),校验会出现两种结果:
eslint --fix
尝试帮你自动修复
修改 .husky/pre-commit
文件
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npx lint-staged
再次执行提交代码
发现 暂存区中 不符合 ESlint
的内容,被自动修复
对于 git
提交规范 而言我们使用了 husky
来监测 Git hooks
钩子,并且通过以下插件完成了对应的配置:
pre-commit
: git hooks
钩子