Setup lint-staged on a Monorepo
TLDR: Here's the repo I setup where is everything for you to check out.
For my new project (more news soon) I need it to setup a monorepo (M O N O R E P O) and I wanted to check some tooling in order to validate and check multiple things before pushing to Github.
What I found out was that in order to setup
husky correctly, it needs to be installed only in teh root of the project. If you have both tools installed on each package, it will not run when you commit. Thanks to yarn workspaces, I can still run my pre commit scripts on each package from the root, al automated using husky and lint-stages. Here's how:
- go to your monorepo root
yarn add lint-staged husky --ignore-workspace-root-check(you need the
--ignore-workspace-root-checkflag so the packages will be installed in the root of the project)
- Setup lint-staged and husky the way you prefer (either in the
package.jsonfile or with separate rc files:
.lintstagedrc). You can see more about these tools on their own repos
- setup your pre-commit script on every package. the script name needs to be the same. In this example I'm using
lintas my pre commit script.
- setup a
lintscript in the root's
yarn workspaces run lint.
and that's it!.
lint-staged will run the root's
lint script, and Yarn workspaces will run each package
lint script using the
yarn workspaces run lint command.