如何在vue2项目中正确设置prettier与husky和lint-staged?

问题描述 投票:0回答:1

在我的 Vue 项目中,我想在每次提交之前在我的暂存文件上运行 prettier。为了做到这一点,我

  1. 已安装
    prettier
    husky
    lint-staged
    ,并使用
    husky
    初始化
    npx husky-init
  2. 将以下内容添加到我的
    package.json
  "lint-staged": {
    "*.{js,vue,css,scss,json,md}": [
      "prettier --write",
      "git add"
    ]
  }
  1. 创建了一个文件
    .husky/pre-commit
    ,其中包含以下内容:
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx lint-staged

暂存文件后(我可以使用

git status
进行验证),我运行
git commit -m "test pre-commit"
并获取

✖ Failed to get staged files!
husky - pre-commit hook exited with code 1 (error)

跑步

npx lint-staged --debug
直接给了我

  lint-staged:bin Running `[email protected]` on Node.js v20.11.1 (win32) +0ms
  lint-staged:bin Options parsed from command-line: { allowEmpty: false, concurrent: true, configPath: undefined, cwd: undefined, debug: true, diff: undefined, diffFilter: undefined, maxArgLength: undefined, quiet: false, relative: false, shell: false, stash: true, hidePartiallyStaged: true, verbose: false } +1ms
  lint-staged:execGit Running git command [ 'version', '--build-options' ] +0ms
  lint-staged git version 2.43.0.windows.1
cpu: x86_64
built from commit: 4b968f3ea3b32a7bc50846bab49f3f381841d297
sizeof-long: 4
sizeof-size_t: 8
shell-path: /bin/sh
feature: fsmonitor--daemon +0ms
  lint-staged:validateOptions Validating options... +0ms
  lint-staged:validateOptions Validated options: { allowEmpty: false, concurrent: true, configObject: undefined, configPath: undefined, cwd: undefined, debug: true, diff: undefined, diffFilter: undefined, maxArgLength: 4095.5, quiet: false, relative: false, shell: false, stash: true, hidePartiallyStaged: true, verbose: false } +0ms
  lint-staged Unset GIT_LITERAL_PATHSPECS (was `undefined`) +2ms
  lint-staged:runAll Running all linter scripts... +0ms
  lint-staged:runAll Using working directory `C:\Users\julian.germek\Repos\aia-website` +1ms
  lint-staged:resolveGitRepo Resolving git repo from `C:\Users\julian.germek\Repos\aia-website` +0ms
  lint-staged:resolveGitRepo Unset GIT_DIR (was `undefined`) +0ms
  lint-staged:resolveGitRepo Unset GIT_WORK_TREE (was `undefined`) +0ms
  lint-staged:execGit Running git command [ 'rev-parse', '--show-cdup' ] +102ms
  lint-staged:resolveGitRepo Resolved git repository top-level directory to be `C:/Users/julian.germek/Repos/aia-website` +95ms
  lint-staged:execGit Running git command [ 'rev-parse', '--show-toplevel' ] +96ms
  lint-staged:execGit Running git command [ 'rev-parse', '--absolute-git-dir' ] +25ms
  lint-staged:resolveGitRepo Resolved git config directory to be `C:/Users/julian.germek/Repos/aia-website/.git` +130ms
  lint-staged:execGit Running git command [ 'log', '-1' ] +104ms
  lint-staged:execGit Running git command [ 'diff', '--name-only', '-z', '--diff-filter=ACMR', '--staged' ] +109ms
  lint-staged:execGit Running git command [ 'submodule', '--quiet', 'foreach', 'echo $displaypath' ] +106ms
✖ Failed to get staged files!

错误消息并没有真正的帮助。您能帮我找到问题并解决它吗?

谢谢!

vue.js prettier lint-staged
1个回答
0
投票

我和你有同样的问题。我一一手动运行命令,并且在这个命令“git submodule --quiet”上遇到了问题。这是一些带有 git repo 的子文件夹。我删除了该文件夹,它开始工作。

© www.soinside.com 2019 - 2024. All rights reserved.