Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smart monorepo #719

Closed
nathanredblur opened this issue Apr 22, 2020 · 2 comments
Closed

Smart monorepo #719

nathanredblur opened this issue Apr 22, 2020 · 2 comments
Labels

Comments

@nathanredblur
Copy link

this is a suggestion / feature:

I have a a repo with 4 micro-services each with his own package.json. I wanted to run some lints rules before do a commit in /app micro-services, but I have the problem that if some of my workmates works in other micro-service, the the hook was triggered when is not required.

so i did this small script and change my package.json configuration to run it.

"husky": {
    "hooks": {
      "pre-push": "sh husky.sh"
    }
  },
#!/bin/bash

SRC_PATTERN="app/"

if git diff @{push} --cached --name-only | grep --quiet "$SRC_PATTERN"
then
  npm run lint && npm run type-check
else
  exit 0
fi

this will check if the changes to be pushed is part of the folder that this hook belong.
that would be the ideal solution.

  1. If you have a monoRepo, the hook would check where the husky was installed or where you have packages.json with a valid husky configuration and save this locations for later use.
  2. husky will then check which paths changes happen and trigger the task in each package.json where the files where modify.
    e.g.
    this is your repo structure
-app
  |- app.js
  |- package.json // pre-push: "npm lint"
- api
  |- api.js
  |- package.json  // pre-push: "npm test"
- utils
  |- index.js
  |- package.json  // no hook

  • if you modify api.js only npm test is execute,
  • if you modify any file inside /app then npm lint is execute
  • if you change any file in /utils nothing happen
  • if you modify app.js, api.js and utils/index.js, then npm lint and npm lint is executed

thanks

@bnbarak
Copy link

bnbarak commented May 27, 2020

First, cool sh script, but you can use https://github.com/okonet/lint-staged - and this is proven to work with monorepos. Example of package.json:

"husky": {
    "hooks": {
      "pre-push": "lint-staged"
    }
  },
  "lint-staged": {
    "*.+(ts|tsx|js|jsx)": [
      "eslint --fix"
    ]
  }

Also, there is an issue with workspaces/monorepos and husky right now #677

@stale
Copy link

stale bot commented Jul 2, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants