我使用 Vue CLI 创建了一个新的 Vue3 应用程序,并为我的 linter 配置选择了 Prettier。我想使用 commitlint、husky 和 lint-staged 来验证提交消息并在推送之前对代码进行 lint。
我做了什么
基于https://commitlint.js.org/#/guides-local-setup我用 husky 设置 commitlint
npm install --save-dev @commitlint/{cli,config-conventional}
echo "module.exports = { extends: ['@commitlint/config-conventional'] };" > commitlint.config.js
npm install husky --save-dev
npx husky install
npx husky add .husky/commit-msg 'npx --no -- commitlint --edit $1'
基于https://github.com/okonet/lint-staged#installation-and-setup我设置了 lint-staged
npx mrm@2 lint-staged
在 package.json 我替换
"lint-staged": {
"*.js": "eslint --cache --fix"
}
和
"lint-staged": {
"*": "npm run lint"
}
问题
将项目中的README.md文件修改为
# my-repo
---
new commit
并尝试提交我收到以下错误消息
> git -c user.useConfigOnly=true commit --quiet --allow-empty-message --file -
[STARTED] Preparing...
[SUCCESS] Preparing...
[STARTED] Running tasks...
[STARTED] Running tasks for *
[STARTED] npm run lint
[FAILED] npm run lint [FAILED]
[SUCCESS] Running tasks...
[STARTED] Applying modifications...
[SKIPPED] Skipped because of errors from tasks.
[STARTED] Reverting to original state because of errors...
[SUCCESS] Reverting to original state because of errors...
[STARTED] Cleaning up...
[SUCCESS] Cleaning up...
✖ npm run lint:
> my-repo@0.1.0 lint
> vue-cli-service lint "/home/.../my-repo/README.md"
error: Parsing error: Invalid character at README.md:1:1:
> 1 | # my-repo
| ^
2 |
3 | ---
4 |
1 error found.
npm ERR! code 1
npm ERR! path /home/my-repo
npm ERR! command failed
npm ERR! command sh -c lint-staged
npm ERR! A complete log of this run can be found in:
npm ERR! /home/.../.npm/_logs/2021-12-27T10_07_27_498Z-debug.log
husky - pre-commit hook exited with code 1 (error)
它应该做什么
仅修复已修改的文件。linter 知道它能够修复的文件(js、ts、vue、html,...)。
当有一个修改过的降价文件时,我打开终端并运行时没有错误npm run lint
。但是在此设置中使用 lint-staged 时确实会出错"*": "npm run lint"
lint-staged 仅对“lintable”文件进行 lint 的正确设置是什么?