-
-
Save cadebward/c26e218220d653385d876f9a81308140 to your computer and use it in GitHub Desktop.
Pre-commit hook to format using prettier-eslint
This file contains 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 | |
# | |
# 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