Giter Club home page Giter Club logo

lint-staged-multi-pkg's Introduction

lint-staged-multi-pkg

Example repo to demostrate use of lint-staged in multi package project with lerna and husky.

pre-commit hook

husky is installed in the root package.json as recommended in husky docs.

The pre-commit hook is configured with the script lerna run --concurrency 1 --stream precommit --since HEAD --exclude-dependents. This executes the precommit script for each package (if it exists). Execution is limited to only those packages with modified files, however the --since HEAD --exclude-dependents option does not consider whether the file is staged. To further limit precommit to only staged files please look at the discussion in sudo-suhas/lint-staged-multi-pkg#4. Furthermore, concurrent execution is disabled because it can cause problems during git add (see okonet/lint-staged#225).

lint-staged configuration

Starting with v5.0, lint-staged automatically resolves the git root without any additional configuration. You configure lint-staged as you normally would, if your project root and git root were the same directory.

Project structure:

.
|-- .git
|-- .gitattributes
|-- .gitignore
|-- lerna.json
|-- license
|-- package.json
|-- packages
|   |-- say-bye
|   |   |-- .eslintrc.yml
|   |   |-- .lintstagedrc.yml
|   |   |-- lib
|   |   |   `-- index.js
|   |   |-- package.json
|   |   `-- yarn.lock
|   `-- say-hi
|       |-- .eslintrc.yml
|       |-- .lintstagedrc.yml
|       |-- package.json
|       |-- src
|       |   `-- index.js
|       `-- yarn.lock
|-- readme.md
`-- yarn.lock

lint-staged config for package say-hi, .lintstagedrc.yml:

linters:
  src/*.js:
    - eslint --fix
    - git add

Note: lint-staged has not been upgraded to v8 as it requires Node >= 8.6.

Output

Example output from the git hook execution:

$ git commit
husky > pre-commit (node v11.9.0)
lerna notice cli v3.13.1
lerna info Executing command in 2 packages: "yarn run precommit"
say-bye: yarn run v1.13.0
say-bye: $ lint-staged
say-bye: Running tasks for lib/*.js [started]
say-bye: eslint --fix [started]
say-bye: eslint --fix [completed]
say-bye: git add [started]
say-bye: git add [completed]
say-bye: Running tasks for lib/*.js [completed]
say-bye: Done in 1.05s.
say-hi: yarn run v1.13.0
say-hi: $ lint-staged
say-hi: Running tasks for src/*.js [started]
say-hi: eslint --fix [started]
say-hi: eslint --fix [completed]
say-hi: git add [started]
say-hi: git add [completed]
say-hi: Running tasks for src/*.js [completed]
say-hi: Done in 1.03s.
lerna success run Ran npm script 'precommit' in 2 packages in 2.5s:
lerna success - say-bye
lerna success - say-hi

What if I don't want to or can't use lerna?

You might not want to use lerna either because you think it is overkill or if you have a different monorepo setup than the one required by lerna. You can still use lint-staged, though not as easily:

Install lint-staged at the package root

Install lint-staged and husky at the project root and configure all linters at the root:

Project structure:

.
|-- .git
|-- .gitignore
|-- .lintstagedrc.yml ๐Ÿข€ Our `lint-staged` config at the monorepo root.
|-- package.json
|-- prj-1
|   |-- .eslintrc.yml
|   |-- .gitignore
|   |-- package.json
|   |-- src
|   |   `-- index.js
|   `-- yarn.lock
`-- prj-2
    |-- .eslintrc.yml
    |-- .gitignore
    |-- package.json
    |-- src
    |   `-- index.js
    `-- yarn.lock

.lintstagedrc.yml:

linters:
  prj-1/*.js:
    - eslint --fix
    - some-other-cmd
    - git add
  prj-2/**/*.js:
    - eslint --fix
    - git add

Note: All linters would have to be installed in the root package.

Manually call precommit in each package

Install husky at the project root and setup the pre-commit hook to execute the precommit script in each package. npm-run-all could also be used to make things a bit easier:

// package.json
{
  "...": "...",
  "scripts": {
    "precommit:prj-1": "cd prj-1 && npm run precommit",
    "precommit:prj-2": "cd prj-2 && npm run precommit",
    "precommit": "npm-run-all precommit:*"
  },
}

Project structure:

.
|-- .gitignore
|-- package.json
|-- prj-1
|   |-- .eslintrc.yml
|   |-- .gitignore
|   |-- .lintstagedrc.yml ๐Ÿข€ Each project has it's own config.
|   |-- package.json
|   |-- src
|   |   `-- index.js
|   `-- yarn.lock
`-- prj-2
    |-- .eslintrc.yml
    |-- .gitignore
    |-- .lintstagedrc.yml
    |-- package.json
    |-- src
    |   `-- index.js
    `-- yarn.lock

License

MIT ยฉ Suhas Karanth

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.