How to run husky pre-commit in child directory only
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.
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"
}
}
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.)