Skip to content

Instantly share code, notes, and snippets.

@cadebward
Forked from linhmtran168/pre-commit-eslint
Last active May 10, 2017 15:20
Show Gist options
  • Save cadebward/c26e218220d653385d876f9a81308140 to your computer and use it in GitHub Desktop.
Save cadebward/c26e218220d653385d876f9a81308140 to your computer and use it in GitHub Desktop.
Pre-commit hook to format using prettier-eslint
#!/bin/sh
#
# Place in `.git/hooks/pre-commit`
# Make it executable: `chmod +x .git/hooks/pre-commit`
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep "\(.js\)\{1\}$")
if [[ "$STAGED_FILES" = "" ]]; then
echo "\nNo JS files to format.\n"
exit 0
fi
PASS=true
echo "\nFormatting JS files...\n"
# Check for eslint
which prettier-eslint &> /dev/null
if [[ "$?" == 1 ]]; then
echo "\t\033[41mPlease install prettier-eslint\033[0m"
exit 1
fi
for FILE in $STAGED_FILES
do
prettier-eslint "$FILE" --write --prettier.bracketSpacing=true --prettier.semi=false
if [[ "$?" == 0 ]]; then
echo "\t\033[32mFormatted: $FILE\033[0m"
else
echo "\t\033[41mFailed: $FILE\033[0m"
PASS=false
fi
done
echo "\nComplete.\n"
if ! $PASS; then
echo "\033[41mCOMMIT FAILED:\033[0m Your commit contains errors.\n"
exit 1
else
echo "\033[42mCOMMIT SUCCEEDED\033[0m\n"
fi
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment