如何使用 Husky 版本 6 进行 lint-staged 工作

mee*_*eez 13 pre-commit pre-commit-hook eslint husky lint-staged

我尝试使用赫斯基pre-commitlint-staged.

安装了这些:

"husky": "^5.1.3",
"lint-staged": "^10.5.4",
Run Code Online (Sandbox Code Playgroud)

在 package.json 我有:

"scripts": {
    "build": "gatsby build",
    "develop": "gatsby develop",
    "format": "prettier --write \"**/*.{js,jsx,ts,tsx,json,md}\"",
    "start": "npm run develop",
    "serve": "gatsby serve",
    "clean": "gatsby clean",
    "test": "echo \"Write tests! -> https://gatsby.dev/unit-testing\" && exit 1",
    "lint": "eslint --ext .js,.jsx,.ts,.tsx src --color",
    "isready": "npm run format && npm run lint && npm run build"
  },
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged",
      "pre-push": "npm test",
    }
  },
  "lint-staged": {
    "./src/*.{js,jsx,ts,tsx}": [
      "npm run format",
      "npm run lint",
      "git add"
    ]
  },
Run Code Online (Sandbox Code Playgroud)

如果我运行npm run lint,目前我有2 problems (1 error, 1 warning). 所以当我运行时git commit,我不希望能够提交,对吗?但我可以继续并完成提交。

怎么了?

更新:

我将哈士奇降级为4.3.8

"husky": "^4.3.8",
"lint-staged": "^10.5.4",
Run Code Online (Sandbox Code Playgroud)

在里面package.json,在我的脚本中,我有:

"prettier": "prettier '**/*.{js,jsx,ts,tsx}' --write",
"lint": "eslint --ext .js,.jsx,.ts,.tsx src --color",
Run Code Online (Sandbox Code Playgroud)

和:

"husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "src/**/*.{ts,tsx,js,jsx}": [
      "npm run prettier",
      "npm run lint",
      "git add"
    ]
  },
Run Code Online (Sandbox Code Playgroud)

当我提交时,赫斯基仍然没有被解雇。怎么了?

更新 2: 我无法让赫斯基4工作,所以我升级到版本6

npm install husky@6 --save-dev \
  && npx husky-init \
  && npm exec -- github:typicode/husky-4-to-6 --remove-v4-config
Run Code Online (Sandbox Code Playgroud)

现在它工作正常。

我唯一无法开始工作的是 lint-staged。

  • 我添加了钩子 npx husky add .husky/pre-commit "lint-staged"

但后来我得到了.husky/pre-commit: line 4: lint-staged: command not found?如何lint-staged使用 Husky 版本 6?

Sey*_*don 9

基于这个husky github 问题,只需执行以下步骤即可使其工作(我们做到了并且工作正常):

  1. npx husky-init
  2. yarn
  3. npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'
  4. yarn add @commitlint/config-conventional @commitlint/cli --dev

.commitlintrc.json

{
  "extends": ["@commitlint/config-conventional"]
}
Run Code Online (Sandbox Code Playgroud)

.lintstagedrc

{
  "src/**/*.+(js|json|ts|tsx)": [
    "eslint"
  ],
  "src/**/*.{js,jsx,ts,tsx,json,css,scss,md}": [
    "prettier --write"
  ]
}
Run Code Online (Sandbox Code Playgroud)

.husky/pre-commit

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn pre-commit-lint
Run Code Online (Sandbox Code Playgroud)

最后,将pre-commit-lint脚本添加到package.json

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn pre-commit-lint
Run Code Online (Sandbox Code Playgroud)


mee*_*eez 6

使得lint-staged通过增加与哈斯基6版本的工作:

// .husky/pre-commit
npm run pre-commit
Run Code Online (Sandbox Code Playgroud)

和:

// package.json
{
  "scripts": {
    "pre-commit": "lint-staged"
  }
}
Run Code Online (Sandbox Code Playgroud)