Last active
July 5, 2017 14:34
-
-
Save peterkuiper/0ba00efc1fcb6517111c85fd96ca5632 to your computer and use it in GitHub Desktop.
Git pre-commit hook that runs ESLint
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
#!/bin/bash | |
TOPLEVEL=$(git rev-parse --show-toplevel) | |
ESLINT_CMD="${TOPLEVEL}/node_modules/.bin/eslint" | |
if [[ ! -x "$ESLINT_CMD" ]]; then | |
echo "\t\033[41mPlease install ESlint\033[0m (npm i --save --save-exact --dev eslint)" | |
exit 1 | |
fi | |
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep ".jsx\{0,1\}$") | |
if [[ "$STAGED_FILES" = "" ]]; then | |
exit 0 | |
fi | |
echo "ESLint'ing ${#STAGED_FILES[@]} files" | |
$ESLINT_CMD "${STAGED_FILES[@]}" | |
ESLINT_EXIT="$?" | |
if [[ "${ESLINT_EXIT}" == 0 ]]; then | |
printf "\n\033[42m\e[90mCOMMIT SUCCEEDED\033[0m\n" | |
else | |
git reset HEAD ${STAGED_FILES} | |
printf "\n\033[41m\e[90mCOMMIT FAILED\033[0m Fix ESLint errors and try again\n" | |
exit 1 | |
fi | |
exit $? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment