husky的用法

127 阅读1分钟

背景

常见的用法是,在package.json中配置如下,但这种方式很多时候没有起作用。

"husky": {
  "hooks": {
    "pre-commit": "lint-staged"
  }
}

在查询了很多文档资料后,发现一种方式有效,记录如下。

执行步骤

  1. 执行

    npx husky-init && yarn
    
  2. 在package.json中编写命令

    "scripts": {
      "jest": "jest",
      "lint": "./node_modules/.bin/eslint -c .eslintrc.json ./src"
    }
    
  3. 在.husky/pre-commit文件中,编写如下代码

    #!/bin/sh
    . "$(dirname "$0")/_/husky.sh"
    
    npm run lint
    npm run jest