-
-
Save agentmilindu/b61f4f8e23742d7cc394290d8a8c2378 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 | |
cd "$(git rev-parse --show-toplevel)" | |
ESLINT="node_modules/.bin/eslint" | |
pwd | |
if [[ ! -x "$ESLINT" ]]; then | |
printf "\t\033[41mPlease install ESlint\033[0m (npm install eslint)\n" | |
exit 1 | |
fi | |
STAGED_FILES=($(git diff --cached --name-only --diff-filter=ACM | grep ".jsx\{0,1\}$")) | |
echo "ESLint'ing ${#STAGED_FILES[@]} files" | |
if [[ "$STAGED_FILES" = "" ]]; then | |
exit 0 | |
fi | |
$ESLINT "${STAGED_FILES[@]}" | |
ESLINT_EXIT="$?" | |
# Re-add files since they may have been fixed | |
git add "${STAGED_FILES[@]}" | |
if [[ "${ESLINT_EXIT}" == 0 ]]; then | |
printf "\n\033[42mCOMMIT SUCCEEDED\033[0m\n" | |
else | |
printf "\n\033[41mCOMMIT 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