sudo-suhas / lint-staged-multi-pkg

Example repo to demonstrate use of `lint-staged` with multi-pkg projects
MIT License
188 stars 19 forks source link

Further documentation for "Install lint-staged at the package root" #9

Closed TidyIQ closed 5 years ago

TidyIQ commented 5 years ago

What should the new package.json look like for root and each package?

Is this still the same for root package.json or should it be changed? Should everything be removed from the package package.json files since it's now handled by root instead?

  "husky": {
    "hooks": {
      "pre-commit": [
        "lerna run --concurrency 1 --stream precommit"
      ]
    }
  },

Basically, I want the following to occur for all packages:

  "lint-staged": {
    "*.{js,jsx,ts,tsx}": [
      "npm run lint:fix",
      "git add"
    ],
    "{*.{json,md}}": [
      "prettier --write",
      "git add"
    ]
  },
  "husky": {
    "hooks": {
      "pre-commit": [
        "npm run type-check && lint-staged"
      ]
    }
  },
sudo-suhas commented 5 years ago

You would configure lint-staged at the root of the project as if it wasn't a monorepo and the projects would be treated as folders instead. So lerna wouldn't be required. But prettier, eslint etc. would need to be installed in the root package.json. Note that this solution only makes sense if you cannot/do not want to use lerna. Does that answer your questions?

TidyIQ commented 5 years ago

Thanks, although I'm still a bit confused as I only just installed lerna today. Sorry if this is a stupid question...

Basically I'm following this guide to create a Gatsby theme, which uses yarn. I prefer npm so I'm looking for an alternative to yarn's workspaces, and it appears lerna is what people generally use.

My dev dependencies in the root package.json are:

  "devDependencies": {
    "@tidyiq/eslint-config": "^2.0.2",
    "eslint": "^6.0.1",
    "husky": "^3.0.0",
    "lerna": "^3.15.0",
    "lint-staged": "^9.1.0",
    "prettier": "^1.18.2",
    "typescript": "^3.5.3"
  }

And my lerna.json file is:

{
  "packages": ["dist/packages/*", "sites/*"],
  "version": "independent"
}

The root package.json is the only one that gets committed. The package.json's in dist/packages/* get published to npm.

I'm guessing I should keep lerna installed as it's a replacement for yarn workspaces and leave the lerna.json file as it is, but since only the root is being committed the root package.json is the only one that should contain:

  "lint-staged": {
    "*.{js,jsx,ts,tsx}": [
      "npm run lint:fix",
      "git add"
    ],
    "{*.{json,md}}": [
      "prettier --write",
      "git add"
    ]
  },
  "husky": {
    "hooks": {
      "pre-commit": [
        "npm run type-check && lint-staged"
      ]
    }
  },

Does that sound right?

sudo-suhas commented 5 years ago

I am not familiar with Gatsby themes and MDX. So I can't be specific in my suggestion. However, since you are using lerna, you should install husky in the root package.json as recommended in their docs. And lint-staged should be installed in each 'package'(a monorepo consists of multiple) if required. And within the context of that project, you should configure lint-staged as you would in a normal project. The readme goes into this and I would recommend you refer the section before 'What if I don't want to or can't use lerna?'. Although this example uses yarn, majority of the suggestions including the lint-staged configuration inside each project would still apply.