Skip to content

Instantly share code, notes, and snippets.

@wwwebman
Last active February 18, 2018 16:37
Show Gist options
  • Save wwwebman/54fc234df55fe588d9ea8cd37b8a5417 to your computer and use it in GitHub Desktop.
Save wwwebman/54fc234df55fe588d9ea8cd37b8a5417 to your computer and use it in GitHub Desktop.
ESLint git pre-commit hook to check Javascript code quality
#!/bin/sh
# 1.
# Code Quality Check
# @scope ./react
#
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM "src/react/*.js" "react/*.jsx" | tr '\n' ' ')
ESLINT="$(git rev-parse --show-toplevel)/react/node_modules/.bin/eslint"
[ -z "$STAGED_FILES" ] && exit 0
# Check if Eslint installed
[ ! -x "$ESLINT" ] && echo "\033[41m Please install ESlint \033[0m (cd react && npm i -D eslint)" && exit 1
# Start validation
VALIDATION_PASSED=true
echo "$STAGED_FILES \n Validating Javascript:"
for FILE in $STAGED_FILES
do
"$ESLINT" "$FILE"
if [ -n "$?" ]; then
VALIDATION_PASSED=false
fi
done
if ! $VALIDATION_PASSED; then
echo "\033[41m COMMIT FAILED: \033[0m
Your commit contains files that should pass ESLint but do not.
Please fix the ESLint errors and try again."
exit 1
fi
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment