Skip to content

Instantly share code, notes, and snippets.

@hamidzr
Forked from linhmtran168/pre-commit-eslint
Last active October 27, 2017 20:27
Show Gist options
  • Save hamidzr/d5095c5d27ed8730809647c39962459f to your computer and use it in GitHub Desktop.
Save hamidzr/d5095c5d27ed8730809647c39962459f to your computer and use it in GitHub Desktop.
Pre-commit hook to check for Javascript using ESLint
#!/bin/bash
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep ".jsx\{0,1\}$")
# def colors
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
if [[ "$STAGED_FILES" = "" ]]; then
exit 0
fi
PASS=true
# echo "Validating Javascript:"
# Check for eslint
which eslint &> /dev/null
if [[ "$?" == 1 ]]; then
echo "Please install ESlint"
exit 1
fi
for FILE in $STAGED_FILES
do
eslint "$FILE"
if [[ "$?" != 0 ]]; then
# echo "$GREEN ESLint Passed: $FILE $NC"
# else
# echo -e "$RED ESLint Failed: $FILE $NC"
PASS=false
fi
done
# echo "Javascript validation completed!"
if ! $PASS; then
echo -e "$RED COMMIT ABORTED $NC"
exit 1
# else
# echo "$GREEN COMMIT SUCCEEDED $NC"
fi
exit $?
@hamidzr
Copy link
Author

hamidzr commented Oct 26, 2017

copy/append to .git/hooks/pre-commit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment