15

We have an enterprise application with a folder structure like the following:

/project
  .git
  /sub1
    ...Java project
  /sub2
    package.json
    ...Javascript Backbone project
  /sub3
    ...Java project
  /sub4
    ...Java project
  /sub5
    package.json
    ...Javascript React project

I currently have Husky set up both in sub2 and sub5 projects, which causes conflicts (requires an npm install whenever I switch projects). Also, the Java developers are reporting that when they commit code in projects sub1, sub3 and sub4, the Husky git hooks are being executed.

Is it possible to only have the hooks run if you are IN a certain folder when you commit?

JoshT
  • 187
  • 1
  • 1
  • 6

5 Answers5

13

While this question is a few years old. Hopefully someone will benefit from an updated answer provided by the folks from husky.

TL;DR

See the new guidance here.

For those that w@ntz answ3rz n0wz!!

New guidance that is provided in the typicode/husky-init README.md suggests the following when "your package.json file and .git directory are not at the same level. For example, project/.git and project/front/package.json"

You can change directory during prepare script and pass a subdirectory:

// package.json
{
  "scripts": {
    "prepare": "cd .. && husky install front/.husky"
  }
}
abest
  • 724
  • 6
  • 16
7

I faced the same issue. I fixed it by installing husky in the main directory.
my setup:

/project
    .git
    package.json
    /frontend
        package.json
    /frontend-admin
        package.json

In /project/package.json:

{
  "license": "MIT",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "frontend": "cd frontend && yarn lint"
    "frontend:admin": "cd frontend-admin && yarn lint",
  },
  "husky": {
    "hooks": {
      "pre-commit": "npm-run-all frontend frontend:admin"
    }
  },
  // In the main folder you should install husky 
  // and npm-run-all to execute multiple commands
  "dependencies": {
    "husky": "^4.3.0",
    "npm-run-all": "^4.1.5"
  }
}

Your subdirectory should have a lint script to execute.
When one of the scripts crashes husky will give a report and abort immediately.

user3379167
  • 127
  • 2
  • 7
4

Is it possible to only have the hooks run if you are IN a certain folder when you commit?

No, but the hook itself can determine which directory it was in when it was run. This is not properly documented, but GIT_PREFIX is set in the environment before Git chdir-s to the $GIT_WORK_TREE or $GIT_DIR directory. (This has been the case since Git version 1.7.8.)

torek
  • 448,244
  • 59
  • 642
  • 775
  • can we get an example of how to use this? – Mauro Colella Apr 27 '21 at 04:49
  • 2
    @MauroColella: In shell code, use: `loc=${GIT_PREFIX:-.}` and then the directory you were in is in `$loc`. For instance, after `cd`-ing to the top level of the work-tree, `$loc` is just `.`, but after a subsequent `cd dir1/dir2`, `$loc` is `dir1/dir2`. – torek Apr 27 '21 at 06:52
1

I had the same issue solved it by Installing and Running husky in /root directory. (This is required as the husky needs to be in the same directory as .git folder.

npm install husky
npx husky install #use this command to create a .husky folder

Search for 'pre-commit' file

cd ./frontend && npm run lint && npm run build
cd ..
cd ./backend && npm run lint && npm run test

Note: Enter the same commands in order for you to execute each sub-directory/project Thank You!

NevetsKuro
  • 604
  • 7
  • 14
0

These projects usually come along with monorepos tools. In my case with lerna, a simple config can do what you need:

/root_workspace
    .git
    lerna.json
    package.json
    /sub-project-1
        package.json
    /sub-project-2
        package.json
  • ~/root_workspace/.husky/precommit: use proper lerna command for your project.
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

# run script in a certain project
lerna exec npm run project_1_script --scope sub-project-1

# run script (one by one) in changed projects
lerna exec --concurrency 1 npm run some_script --since HEAD

# run script (one by one) in all projects
lerna exec --concurrency 1 npm run another_script
  • ~/root_workspace/sub-project-1/package.json
// ...
"scripts": {
    "project_1_script": "echo \"project 1 script\"",
    "some_script": "echo \"script is triggered by changed project\"",
    "another_script": "echo \"script is triggered by all projects\""
}
Hoang Dao
  • 775
  • 5
  • 16