1

I am trying to set the pre commit git hooks for angular5 project. I am using husky with lint-staged. Prettier and stylelint linters run fine. But when lint-staged encounters 'ng lint', it just throws error:


    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.

here is my husky and lint-staged configuration in package.json

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

also:

"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", These 2 commands run properly but npm run lint has error

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

1 Answers1

-1

After spending few hours I figured out that, projectName was not getting passed properly. That is why Workspace.getProject() was giving the exception. Also came to know that, angular-cli > 5, needs projectname to be specified explicitly the ng lint command to make it work with lint-staged.

Sharing the solution for someone facing the same issue:

So here is the solution:

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

For ref: https://github.com/okonet/lint-staged/pull/534/files