Last active
February 18, 2018 16:37
-
-
Save wwwebman/54fc234df55fe588d9ea8cd37b8a5417 to your computer and use it in GitHub Desktop.
ESLint git pre-commit hook to check Javascript code quality
This file contains hidden or 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/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