javascript - lint-staged 未在预提交时运行

标签 javascript git pre-commit-hook prettier lint-staged

prettier 未在预提交时运行。这在其他项目中使用相同的配置,所以我很困惑为什么这次不工作。

这是我的 package.json 文件的相关部分:

"scripts": {
    "precommit": "lint-staged"
  },
"lint-staged": {
  "*.{js,json,css,scss,html,md}": [
    "prettier --write",
    "git add"
  ]
},

编辑。以下是相关的 devDependencies:

"devDependencies": {
  "husky": "^0.14.3",
  "lint-staged": "^7.0.4",
  "prettier": "1.12.0"
},

最佳答案

2021 年

有时 husky 不会添加钩子(Hook),因此您需要使用简单的 hack 添加它。

在安装 husky 的 V4 之后,您需要先卸载 husky,因为它确保您的钩子(Hook)已正确安装,然后再安装最新版本的 husky,这样您就可以得到最新动态。

NPM

npm uninstall husky

npm install -D husky@4

npm install -D husky

yarn

yarn remove husky

yarn add -D husky@4

yarn add -D husky

如果有时上面的技巧不起作用,那么让我们将钩子(Hook)添加到 husky 中,下面提到的方法仅在 V6 中使用,我正在用 展示 husky lint-staged 示例。

NPM

npm install -D husky

npm set-script prepare "husky install" && npm run prepare

npx husky add .husky/pre-commit "npx lint-staged"

git commit -m "added husky and lint-stagged" // here you will notice the lint-staged checking the files with help of husky

yarn

yarn add -D husky

npm set-script prepare "husky install" && yarn prepare

npx husky add .husky/pre-commit "yarn lint-staged"

git commit -m "added husky and lint-stagged" // here you will notice the lint-staged checking the files with help of husky

关于javascript - lint-staged 未在预提交时运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50048717/

相关文章:

javascript - 网络音频 - 更改现场麦克风的音调

javascript - React.jS 中内联所有样式的性能问题?

Git merge 不使用默认 merge 消息,使用默认消息打开编辑器

django - 预提交 Hook 以检查 django 迁移

django - 使用 REGEX 在 pre-commit-config.yaml 中排除 Django 自动生成的文件

javascript - 保留表单提交上的下拉值

javascript - 如何将现有回调 API 转换为 Promise?

git - 在使用 Android Studio 时,仅从包含 android 应用程序的 Git 子文件夹中 check out 作为新项目

gitignore 文件夹中的所有内容(包含其所有内容的文件夹除外)

git - 为什么 git end-of-file-fixer 不断在我的文件末尾添加未知字符?