Skip to content

Instantly share code, notes, and snippets.

@peterkuiper
Last active July 5, 2017 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterkuiper/0ba00efc1fcb6517111c85fd96ca5632 to your computer and use it in GitHub Desktop.
Save peterkuiper/0ba00efc1fcb6517111c85fd96ca5632 to your computer and use it in GitHub Desktop.
Git pre-commit hook that runs ESLint
#!/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