Skip to content

Instantly share code, notes, and snippets.

@eseceve
Created October 20, 2015 14:45
Show Gist options
  • Save eseceve/0643e1126a31c3bdb9be to your computer and use it in GitHub Desktop.
Save eseceve/0643e1126a31c3bdb9be to your computer and use it in GitHub Desktop.
Git pre-commit hook to run ESlint
#!/bin/sh
STAGED_FILES=$(git diff --cached --name-only --dif-filter=ACM | grep ".jsx\{0,1\}$")
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 "ESlint passed: $FILE"
else
echo "$?"
PASS=false
fi
done
echo "Javascript validation completed!"
if ! $PASS; then
echo "Commmit failed: your commit contains files that should pass ESlint but do not"
exit 1
else
echo "Commit succeeded"
fi
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment