Make pre-commit check in subdirectory of repository?


I’m trying to get a pre-commit check to run a command (in npm) not in the root of the git repository (I have a folder "frontend" that contains the npm installation). This sounds trivial, but so far I have found no pre-commit package that supports this directly. I came up with a workaround, where pre-commit launches a script, then the script makes a change of directory and then it runs the command, but I would like something more direct. Pre-commit packages I have tried:

None of these seem to support this directly

Any alternative packages to these would be welcome, or perhaps if I missed a setting in these somewhere.

A similar question was asked here for husky, but the answer again relied on a shell script: Husky npm package Pre-Commit hook not working in sub directory repository

UPDATE: Following comments, I will revise the emphasis of this question. I would really like to find out how to do this in one the three options I specified, although other solutions are welcome.


Found a solution to this problem with the module Husky:

By design, husky install must be run in the same directory as .git, but you can change directory during prepare script and pass a subdirectory. Assuming your subdirectory is frontend:

// package.json

  "scripts": {
    "prepare": "cd .. && husky install frontend/.husky"

In your hooks, you’ll also need to change directory:

# .husky/pre-commit
# ...
cd frontend
npm run command

Solution from the husky documentation:

Answered By – Mark

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published