Skip to content

Instantly share code, notes, and snippets.

@isaacl
Last active July 5, 2017 10:14
Show Gist options
  • Save isaacl/9fd78507be080116a6cf to your computer and use it in GitHub Desktop.
Save isaacl/9fd78507be080116a6cf to your computer and use it in GitHub Desktop.
#!/bin/bash -e
# option parsing.
FAST_ESLINT=0
ALLOW_WARNINGS=0
ESLINT_VERBOSE=0
OPTIND=1 # Reset in case getopts has been used previously in the shell.
while getopts ":fvw" opt; do
case "$opt" in
v) ESLINT_VERBOSE=1
;;
f) FAST_ESLINT=1
;;
w) ALLOW_WARNINGS=1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
# Use git to find src root. Jump to script dir to ensure we start in our repo.
cd "$( dirname "${BASH_SOURCE[0]}" )"
SRC_DIR="$(git rev-parse --show-toplevel)"
cd "${SRC_DIR}"
export PATH="${SRC_DIR}/node_modules/.bin:$PATH"
if [[ $ESLINT_VERBOSE -eq 1 ]]; then
export DEBUG='eslint:cli-engine'
fi
ESLINT_CMD='eslint --ext .jsx,.js --cache'
if [[ $ALLOW_WARNINGS -eq 0 ]]; then
ESLINT_CMD+=' --max-warnings=0'
fi
# DO IT.
if [[ $FAST_ESLINT -ne 1 ]]; then
exec ${ESLINT_CMD} .
else
ANCESTOR=$(git merge-base HEAD refs/removes/origin/master)
cat <(git ls-files --others --exclude-standard -z -- '*.js' '*.jsx') \
<(git diff --diff-filter='AM' --name-only --no-renames --no-color -z \
$ANCESTOR -- '*.js' '*.jsx') \
| sort -uz \
| xargs -0 ${ESLINT_CMD}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment