Created
July 30, 2018 21:12
-
-
Save RobertFischer/cd7d8988d1ca7ec9ace8febfd2cae5fc to your computer and use it in GitHub Desktop.
ESLint executing on changed files as a git precommit 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 -u | |
# | |
# Run eslint on js and jsx files that are being committed, including doing a round of fixing up | |
# those files. | |
# | |
# Based on @dahjelle/pre-commit.sh and @jancimajek's one liner in that Gist. | |
# | |
NAMES=`git diff --diff-filter=d --cached --name-only -- '*.js' '*.jsx' | wc -l` | |
if [ ${NAMES} -eq 0 ]; then | |
echo "No files to check with eslint" | |
else | |
# Ensures that eslint exists and die if it is not | |
which eslint || ( echo "Please install eslint"; exit -1 ) | |
# ESLint staged changes within patient-mobile/bsncAppExpo only | |
git diff --diff-filter=d --cached --name-only -z -- '*.js' '*.jsx' \ | |
| xargs -0 -I % sh -c 'eslint --cache --report-unused-disable-directives --quiet --color --fix "%" 1>/dev/null; git add "%";' | |
git diff --diff-filter=d --cached --name-only -z -- '*.js' '*.jsx' \ | |
| xargs -0 -I % sh -c 'git show ":%" | eslint --cache --color --quiet --stdin --stdin-filename "%";' | |
eslint_exit=$? | |
if [ ${eslint_exit} -eq 0 ]; then | |
git diff --diff-filter=d --cached --name-only -z -- '*.js' '*.jsx' \ | |
| xargs -0 -I % sh -c 'git show ":%" | eslint --max-warnings=0 --cache --color --stdin --stdin-filename "%";' | |
eslint_exit=$? | |
if [ ${eslint_exit} -eq 0 ]; then | |
echo "✓ ESLint passed" | |
else | |
echo "✘ ESLint failed with warnings!" 1>&2 | |
echo "But we're letting it run anyway...just warnings." | |
fi | |
else | |
echo "✘ ESLint failed with errors!" 1>&2 | |
exit ${eslint_exit} | |
fi | |
fi | |
# | |
# Run jq on json files that are being committed. | |
# | |
NAMES=`git diff --diff-filter=d --cached --name-only -- '*.json' '*.json5' | wc -l` | |
if [ ${NAMES} -eq 0 ]; then | |
echo "No files to check with jq" | |
else | |
# Ensures that eslint exists and die if it is not | |
which jq || ( echo "Please install 'jq' for analyzing/verifying JSON files"; exit -1 ) | |
# ESLint staged changes within patient-mobile/bsncAppExpo only | |
git diff --diff-filter=d --cached --name-only -z -- '*.json' '*.json5' \ | |
| xargs -0 -I % sh -c 'git show ":%" | jq . 1>/dev/null' | |
jq_exit=$? | |
if [ ${jq_exit} -eq 0 ]; then | |
echo "✓ JSON syntax check passed" | |
else | |
echo "✘ JSON syntax check failed with errors!" 1>&2 | |
exit ${jq_exit} | |
fi | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment