与lint-staged一起使用时,预提交规则失败

时间:2019-06-13 10:20:30

标签: pre-commit-hook pre prettier husky lint-staged

我正在尝试为angular5项目设置预提交git挂钩。我使用的是沙哑的绒毛。更漂亮和更轻巧的短绒运行良好。但是当lint-staged遇到“ ng lint”时,它只会引发错误:


    Project '/Users/sneha.vantamuri/Documents/customerportal/aviportal-ui/src/app/components/dashboard/dashboard.component.ts' could not befound in workspace.
    Error: Project '/Users/sneha.vantamuri/Documents/customerportal/aviportal-ui/src/app/components/dashboard/dashboard.component.ts' couldnot be found in workspace.
      at Workspace.getProject (/Users/sneha.vantamuri/Documents/customerportal/aviportal-ui/node_modules/@angular-devkit/core/src/workspace/workspace.js:83:19)
      at Architect.getBuilderConfiguration (/Users/sneha.vantamuri/Documents/customerportal/aviportal-ui/node_modules/@angular-devkit/architect/src/architect.js:96:41)
      at MergeMapSubscriber._loadWorkspaceAndArchitect.pipe.operators_1.concatMap [as project] (/Users/sneha.vantamuri/Documents/customerportal/aviportal-ui/node_modules/@angular/cli/models/architect-command.js:64:55)
      at MergeMapSubscriber._tryNext (/Users/sneha.vantamuri/Documents/customerportal/aviportal-ui/node_modules/@angular/cli/node_modules/rxjs/internal/operators/mergeMap.js:69:27)
      at MergeMapSubscriber._next (/Users/sneha.vantamuri/Documents/customerportal/aviportal-ui/node_modules/@angular/cli/node_modules/rxjs/internal/operators/mergeMap.js:59:18)
      at MergeMapSubscriber.Subscriber.next (/Users/sneha.vantamuri/Documents/customerportal/aviportal-ui/node_modules/@angular/cli/node_modules/rxjs/internal/Subscriber.js:67:18)
      at TapSubscriber._next (/Users/sneha.vantamuri/Documents/customerportal/aviportal-ui/node_modules/@angular/cli/node_modules/rxjs/internal/operators/tap.js:65:26)
      at TapSubscriber.Subscriber.next (/Users/sneha.vantamuri/Documents/customerportal/aviportal-ui/node_modules/@angular/cli/node_modules/rxjs/internal/Subscriber.js:67:18)
      at MergeMapSubscriber.notifyNext (/Users/sneha.vantamuri/Documents/customerportal/aviportal-ui/node_modules/@angular/cli/node_modules/rxjs/internal/operators/mergeMap.js:92:26)
      at InnerSubscriber._next (/Users/sneha.vantamuri/Documents/customerportal/aviportal-ui/node_modules/@angular/cli/node_modules/rxjs/internal/InnerSubscriber.js:28:21)
    npm ERR! code ELIFECYCLE
    npm ERR! errno 1
    npm ERR! avi-portal-ui@0.0.0 lint: `ng lint "/Users/sneha.vantamuri/Documents/customerportal/aviportal-ui/src/app/components/dashboard/dashboard.component.ts"`
    npm ERR! Exit status 1
    npm ERR!
    npm ERR! Failed at the avi-portal-ui@0.0.0 lint script.
    npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

这是package.json中我多壳的分阶段配置

    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "*.{ts,scss,json}": [
      "npm run prettier:check",
      "npm run stylelint",
      "npm run lint",
      "git add"
    ]
  }

还:

"scripts":{
"lint": "ng lint",
 "prettier:check": "prettier --config ./.prettierrc --check './src/**/*.{ts,scss,json}'",
 "stylelint": "stylelint \"src/**/*.scss\"",
}

“ npm run prettier:check”,  “ npm run stylelint”, 这两个命令运行正常,但是npm run lint出现错误

1 个答案:

答案 0 :(得分:0)

花了几个小时后,我才发现projectName没有正确传递。这就是为什么Workspace.getProject()给出异常的原因。还知道,angular-cli> 5需要使用ng lint命令显式指定项目名称,以使其与lint-staged一起使用。

为面临相同问题的人共享解决方案:

这是解决方案:

    "*.{ts,scss,json}": [
      "npm run prettier:check",
      "npm run stylelint",
      "npm run lint **<yourProjectNamefromAngular.jsonfile> --files**",
      "git add"
    ]
  }

对于裁判:https://github.com/okonet/lint-staged/pull/534/files