项目首次配置,不需要每次都添加
- 1.安装代码校验依赖
npm install -D @commitlint/cli husky lint-staged
npm set-script prepare "husky install" # 在package.json中添加脚本
npm run prepare # 初始化husky,将 git hooks 钩子交由,husky执行
npx husky add .husky/pre-commit "npx lint-staged"
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'
根目录中增加以下两个文件
.lintstagedrc.json github.com/okonet/lint…
如果添加 git add ,eslint 或者 其他校验中间报错也会保存eslint的结果,不加 git add 会撤回所有 eslint 结果。多条命令的时候流程会变得混乱,不推荐加 git add
{
"*.{vue,js,jsx,ts,tsx}": "eslint --fix"
}
包含 stylelint prettier
{
"*.{vue,js,jsx,ts,tsx}": "eslint --fix",
"*.{html,vue,css,sass,scss}": "stylelint --fix",
"*": "prettier --ignore-unknown --write"
}
Mac 中需要执行以下命令才能生效
chmod 777 .husky/pre-commit
chmod 777 .husky/commit-msg
commitlint.config.js 校验规则
module.exports = {
rules: {
'arc-rule': [2, 'always'],
'type-rule': [2, 'always'],
'scope-rule': [2, 'always'],
'subject-rule': [2, 'always'],
},
plugins: [
// 定义插件
{
rules: {
// 定义规则
'arc-rule': ({ raw }) => {
const message =
raw
.replace(/^\s*|\s*$/g, '')
.replace(/\s+/g, ' ')
.split(' ')[0] || ''
const reg = /^T\d+$/
return [
reg.test(message),
`必须已TaskId开头;\n格式为<tid> <type>(<scope>): <subject>`,
]
},
'type-rule': ({ raw }) => {
const message =
raw
.replace(/^\s*|\s*$/g, '')
.replace(/\s+/g, ' ')
.split(' ')[1] || ''
const typeMes = message.split('(')[0]
const type = [
'feat',
'fix',
'docs',
'style',
'refactor',
'test',
'perf',
'build',
'cli',
'chore',
'revert',
'merge',
'deps',
]
const reg = new RegExp('^(' + type.join('|') + ')$')
return [
reg.test(typeMes),
`type类型必须为以下一种: ${type.join(
','
)};\n格式为<tid> <type>(<scope>): <subject>`,
]
},
'scope-rule': ({ raw }) => {
const message =
raw
.replace(/^\s*|\s*$/g, '')
.replace(/\s+/g, ' ')
.split(' ')[1] || ''
const scopeMes = message.split('(')[1]
const reg = /^\S+\):$/
return [
reg.test(scopeMes),
`scope必须填写:修改文件范围;\n格式为<tid> <type>(<scope>): <subject>`,
]
},
'subject-rule': ({ raw }) => {
const message =
raw
.replace(/^\s*|\s*$/g, '')
.replace(/\s+/g, ' ')
// .split(' ')[2] || ''
.split('): ')[1] || ''
return [
message.length >= 10,
`subject:请填写修改具体内容,不少于10个字母;\n格式为<tid> <type>(<scope>): <subject>`,
]
},
},
},
],
}