1

我正在尝试为 angular5 项目设置预提交 git 挂钩。我正在使用带有 lint-staged 的​​哈士奇。Prettier 和 stylelint linters 运行良好。但是当 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 中的 husky 和 ​​lint-staged 配置

    "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 有错误

4

1 回答 1

-1

花了几个小时后,我发现 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

于 2019-06-13T16:33:16.990 回答