Does this support multiple package.json files. I have 2 folders API and UI both have their own package.json. Does lint-staged support that.
Yes it does. You configure lint-staged as you normally would if the git directory and the project root were the same.
Consider a project with the following file structure:
|-- .git
`-- packages
|-- prj-1
| |-- .eslintrc.yml
| |-- .gitignore
| |-- .lintstagedrc.yml <- here's the config file
| |-- package.json
| |-- src
| | `-- index.js
| `-- yarn.lock
`-- prj-2
`-- file
lint-staged can be setup for prj-1 with the following config:
.lintstaged.yml
linters:
src/*.js:
- eslint --fix
- git add
cool. I didn't find a mention of this in the docs. I may have missed it. If i have not, can this be updated.
You are quite right. Actually, before lint-staged@5 release, you would have had to configure the gitDir for your project and specify the globs relative to the git root. I have created a PR which adds the relevant documentation to the readme. Thank you.
just clarifying. Say i have 2 folders each with it's own package.json. If i commit a code in folder A it triggers package.json of folder A. If i commit a code in folder b it triggers a package.json in folder b ??
I had not tested this myself. While lint-staged works for multi package projects, husky, which is used to setup the pre-commit hooks, doesn't. There is a PR open for this though - typicode/husky#13 and some relevant discussion in typicode/husky#160.
@awebdeveloper Also see typicode/husky:docs.md@dev#multi-package-repository-monorepo. However, this documentation is for the beta version and I am not completely sure how this can be used with lint-staged + lerna. I might try this out later today if I get some time.
Hey @awebdeveloper I have created a repo which demonstrates use of lint-staged in a multi package monorepo with lerna and husky - https://github.com/sudo-suhas/lint-staged-multi-pkg. Hope it helps.
thanks @sudo-suhas for everything. Much appreciated. Is Lerna really needed
If you try to install husky in each package, the last package in which you installed it, is the only one for which the pre-commit hook will be run. Which is why you need to install husky in the root folder. After that, you could configure lint-staged from the root folder with a config which might look like this:
linters:
prj-1/src/*.js:
- eslint --fix
- git add
prj-2/lib/*.js:
- eslint --fix
- git add
This also means that you have to install eslint, or any other linters you want to use, in the root package.
If you want to configure lint-staged for each package, you need a way to execute the precommit script for each package. This is where lerna comes in. While you may not _need_ it, it makes your job easier.
Most helpful comment
Hey @awebdeveloper I have created a repo which demonstrates use of
lint-stagedin a multi package monorepo withlernaandhusky- https://github.com/sudo-suhas/lint-staged-multi-pkg. Hope it helps.