Created
August 24, 2021 22:55
-
-
Save dlaub3/2f5486d23e8d95b940bbc9b4ddbcac0d to your computer and use it in GitHub Desktop.
Better Husky Pre-Commit
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 | |
# | |
# This script lints files for a Husky pre-commit hook | |
# | |
# run with `git commit -m "foo" 2>&1 | tee husky-errors.ignore.txt` | |
# to save the errors to a file. | |
# | |
# { | |
# "hooks": { | |
# "pre-commit": "bash ./husky.ignore.sh" | |
# } | |
# } | |
# | |
# | |
STAGED=$(git diff --staged --name-only --relative --diff-filter=ACM | grep "\.[t|j]sx\?$") | |
NPM_BIN=$(npm bin) | |
T_START=$(date +%s) | |
T_ESLINT="0" | |
T_PRETTIER="0" | |
T_STYLELINT="0" | |
echo | |
echo "πͺ΅ staged files:" | |
echo "${STAGED}" | |
echo | |
if test -z "${STAGED}" | |
then | |
echo "nothing to lint π" | |
echo | |
echo | |
exit 0 | |
else | |
S_TIME=$(date +%s) | |
echo "running eslint π:" | |
ESLINT_OUT=$(${NPM_BIN}/eslint --fix ${STAGED} 2>&1) | |
git add ${STAGED} # add any changes from --fix | |
E_TIME=$(date +%s) | |
T_ESLINT=$((E_TIME-S_TIME)) | |
S_TIME=$(date +%s) | |
echo "running prettier π:" | |
PRETTIER_OUT=$(${NPM_BIN}/prettier --list-different --write ${STAGED} 2>&1) | |
git add ${STAGED} # add any changes from --write | |
E_TIME=$(date +%s) | |
T_PRETTIER=$((E_TIME-S_TIME)) | |
S_TIME=$(date +%s) | |
echo "running stylelint π:" | |
STYLELINT_OUT=$(${NPM_BIN}/stylelint --fix ${STAGED} 2>&1) | |
git add ${STAGED} # add any changes from --fix | |
E_TIME=$(date +%s) | |
T_STYLELINT=$((E_TIME-S_TIME)) | |
fi | |
T_END=$(date +%s) | |
echo | |
echo "β³ runtime: $((T_END-T_START))s" | |
echo "- eslint: ${T_ESLINT}s" | |
echo "- prettier: ${T_PRETTIER}s" | |
echo "- stylelint: ${T_STYLELINT}s" | |
echo | |
if test -z "${ESLINT_OUT}${STYLELINT_OUT}${PRETTIER_OUT}" | |
then | |
echo "π₯³ SUCCESS" | |
else | |
echo | |
echo "π¨ ERRORS:" | |
echo | |
if test "${ESLINT_OUT}" | |
then | |
echo "π΄ eslint errors:" | |
echo "${ESLINT_OUT}" | |
echo | |
fi | |
if test "${PRETTIER_OUT}" | |
then | |
echo "π΄ prettier errors:" | |
echo "${PRETTIER_OUT}" | |
echo | |
fi | |
if test "${STYLELINT_OUT}" | |
then | |
echo "π΄ stylelint errors:" | |
echo "${STYLELINT_OUT}" | |
echo | |
fi | |
echo | |
echo | |
# non-zero exit signals failure | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment