Last active
June 17, 2024 07:55
-
-
Save btoo/ee33b4490e3d2efbb2c5962871e12b9b to your computer and use it in GitHub Desktop.
husky pre-commit hook for handling ONLY staged files that will prevent commit if there are linting errors and auto-fix them
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @see {@tutorial https://github.com/typicode/husky} | |
* _Requires Node >= 10 and Git >= 2.13.0._ | |
*/ | |
module.exports = { | |
hooks: { | |
/** | |
* @see {@tutorial https://stackoverflow.com/a/15656652/3942699} | |
* | |
* ___only works with files staged in git___ | |
* because doing on the entire repo is overkill atm | |
* | |
* if linting succeeds, proceed to committing the staged files. | |
* if linting fails, prevent the commit from happening and fix the auto-fixable errors | |
*/ | |
'pre-commit': ` | |
stagedFiles=$(git diff --diff-filter=d --cached --name-only); | |
if [ -n "$stagedFiles" ]; then # at least one file is staged | |
{ | |
npx eslint $stagedFiles | |
} || { | |
npx eslint --fix $stagedFiles && exit 1 | |
} | |
fi | |
`, | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment