Last active
July 1, 2023 13:52
-
-
Save haggen/76bb74294b19aea029bd254c71969bdb to your computer and use it in GitHub Desktop.
Git hook to lint (eslint) and format (prettier) staged files and abort the commit if anything has changed.
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
{ | |
"scripts": { | |
"prepare": "test -f .git/hooks/pre-commit || cp scripts/pre-commit .git/hooks", | |
"format": "prettier . --cache", | |
"lint": "eslint . --report-unused-disable-directives --max-warnings 0 --cache", | |
} | |
} |
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
#!/usr/bin/env sh | |
set -ue | |
# shellcheck disable=SC2046 | |
md5sum $(git diff --diff-filter=ACMR --cached --name-only | xargs) > /tmp/pre-commit-summary | |
echo "---> Checking code… ⚠️" >&2 | |
( | |
npm run-script --silent format -- --write | |
npm run-script --silent lint -- --fix | |
) >/dev/null 2>&1 || : | |
if ! md5sum --status --check /tmp/pre-commit-summary; then | |
echo "---> The pre-commit hook has changed some of the files being commited. Please review these changes and try again. ❌" >&2 | |
exit 1 | |
fi | |
echo "---> All good. ✅" >&2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment