【问题标题】:Pre Commit Hook fails预提交挂钩失败
【发布时间】:2019-06-02 16:38:06
【问题描述】:

我正在为我的新项目使用 NestJs

我正在使用此命令添加所有文件。 git add .

当我在添加所有文件后提交时,husky 阻止提交并向我显示此错误。

[文件路径]/.spec.ts' 不包含在项目中。

husky > pre-commit hook 失败(添加 --no-verify 绕过)

我隐式添加了该文件,但它仍然向我抛出该错误。

我的 tsconfig.json 文件

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": false,
    "noImplicitAny": false,
    "removeComments": true,
    "noLib": false,
    "allowSyntheticDefaultImports": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es6",
    "sourceMap": true,
    "allowJs": true,
    "outDir": "./dist",
    "baseUrl": "./src",
    "lib": ["dom", "es2018", "esnext"]
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "**/*.spec.ts"]
}

这就是我在 package.json 文件中添加 husky 命令的方式

 "scripts": {
    "lint": "tslint -p tsconfig.json -c tslint.json",
  },


"husky": {
    "hooks": {
      "pre-commit": "lint-staged",
      "post-commit": "git push origin HEAD"
    }
  },
  "lint-staged": {
    "*.ts": [
      "tslint -p tsconfig.json -c tslint.json",
      "git add"
    ]
  },

【问题讨论】:

  • 您是否在pre-commit 挂钩中运行lint-staged 命令并期望它们更改将要提交的内容?
  • 我用这个解决方案解决了我的问题stackoverflow.com/a/63948896/1862590

标签: git tslint nestjs husky


【解决方案1】:

如果您使用的是 Windows 操作系统,那么将包含在您的 package.json 中的 hooks 脚本应该 是下面这种形式

"husky": {
    "hooks": {
      "pre-push": "set CI=true&&npm test"
    }
  },

对于其他操作系统使用下面的sn-p

 "husky": {
 "hooks": {
 "pre-push": "CI=true npm test"}},

更多信息: https://facebook.github.io/create-react-app/docs/running-tests#on-your-own-environment

【讨论】:

    【解决方案2】:

    可能是因为您的 tsconfig.json 文件中的 exclude 属性中有 "**/*.spec.ts" 作为值,因此您的预提交挂钩失败了吗?

    你能像这样编辑文件并测试它吗:

    {
      "compilerOptions": {
        "module": "commonjs",
        "declaration": false,
        "noImplicitAny": false,
        "removeComments": true,
        "noLib": false,
        "allowSyntheticDefaultImports": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "target": "es6",
        "sourceMap": true,
        "allowJs": true,
        "outDir": "./dist",
        "baseUrl": "./src",
        "lib": ["dom", "es2018", "esnext"]
      },
      "include": ["src/**/*"],
      "exclude": ["node_modules"]
    }
    

    【讨论】:

      猜你喜欢
      • 2015-07-08
      • 1970-01-01
      • 2020-02-06
      • 2013-08-26
      • 2013-03-17
      • 1970-01-01
      • 2012-07-11
      • 1970-01-01
      相关资源
      最近更新 更多