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

Posted

技术标签:

【中文标题】与 lint-staged 一起使用时,预提交规则失败【英文标题】:pre commit rules failing when used with lint-staged 【发布时间】:2019-10-27 22:09:06 【问题描述】:

我正在尝试为 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 运行 stylelint", 这两个命令运行正常但是 npm run lint 有错误

【问题讨论】:

【参考方案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

【讨论】:

以上是关于与 lint-staged 一起使用时,预提交规则失败的主要内容,如果未能解决你的问题,请参考以下文章

lint-staged 正在为所有更改的文件运行 ng -lint,而不仅仅是暂存文件

“测试”的 husky 预提交钩子未完成

husky+lint-staged 在非暂存文件上运行

15分钟快速配置eslint,prettier,lint-staged,husky,commitizen实现前端项目代码规范化

参数列表太长 - lint-staged -> tslint

在 Intellij idea 中运行“ng lint --fix”后,“lint-staged”包未提交更新的文件