Last active
January 18, 2017 20:10
-
-
Save ivanalejandro0/08db62ac22ae1a06660dfa1d0f163e2d to your computer and use it in GitHub Desktop.
ESLint check your code as a pre-commit hook
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/bash | |
# based on: https://gist.github.com/linhmtran168/2286aeafe747e78f53bf | |
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep "\.jsx\{0,1\}$") | |
if [[ "$STAGED_FILES" = "" ]]; then | |
exit 0 | |
fi | |
# Escape code | |
esc=`echo -en "\033"` | |
# Set colors | |
cc_normal="${esc}[0m" | |
cc_red="${esc}[0;31m" | |
cc_red_b="${esc}[0;41m" | |
cc_green="${esc}[0;32m" | |
cc_yellow="${esc}[0;33m" | |
cc_blue="${esc}[0;34m" | |
echo -e "Validating Javascript (pre-commit hook) ...\n" | |
# Check for eslint | |
which eslint &> /dev/null | |
if [[ "$?" == 1 ]]; then | |
echo "${cc_red_b}Error: please install ESlint${cc_normal}" | |
exit 1 | |
fi | |
PASS=true | |
OUTPUT="" | |
for FILE in $STAGED_FILES; do | |
echo -en "[...] checking $FILE" | |
OUT=`eslint --color "$FILE"` | |
if [[ "$?" == 0 ]]; then | |
# echo -e "\t${cc_green}[passed]${cc_normal}" | |
echo -e "\r[ ${cc_green}✔${cc_normal} ]" | |
else | |
# echo -e "\t${cc_red}[failed]${cc_normal}" | |
echo -e "\r[ ${cc_red}✘${cc_normal} ]" | |
OUTPUT="$OUTPUT $OUT" | |
PASS=false | |
fi | |
done | |
if [[ -n "$OUTPUT" ]]; then | |
echo "---------- report ----------" | |
echo -e "$OUTPUT" | |
echo "----------------------------" | |
fi | |
echo -en "\nJavascript validation complete: " | |
if ! $PASS; then | |
echo -e "${cc_red}Commit stopped${cc_normal}" | |
echo "Your commit contains files that should pass ESLint but do not." | |
echo "Please fix the ESLint errors and try again.\n" | |
exit 1 | |
else | |
echo -e "${cc_green}Commit allowed${cc_normal}\n" | |
fi | |
exit $? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment