Last active
February 13, 2018 22:53
-
-
Save andrewmartin/9a175694be5132fde2531830a185ab93 to your computer and use it in GitHub Desktop.
pre-commit
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
#!/usr/bin/env bash | |
STAGED_FILES=$(git diff --name-only --cached --diff-filter=ACM -- *.jsx *.js) | |
# STAGED_FILES=$(git diff -- *.jsx --cached --name-only --diff-filter=ACM) | |
BIN_PATH="$(git rev-parse --show-toplevel)/node_modules/.bin" | |
eslint() { | |
ESLINT="$BIN_PATH/eslint" | |
# Check for eslint | |
if [[ ! -x "$ESLINT" ]]; then | |
printf "\t\033[41mPlease install ESLint\033[0m\n" | |
exit 1 | |
fi | |
echo "Linting modified files" | |
echo $STAGED_FILES | xargs $ESLINT --cache --cache-location=.cache/eslint -c "./.eslintrc.js" | |
if [[ $? == 0 ]]; then | |
printf "\n\033[1;32mLint Passed\033[0m\n" | |
else | |
printf "\n\033[41mLint Failed:\033[0m Fix lint errors and try again!\n" | |
exit 1 | |
fi | |
} | |
eslintSpec() { | |
ESLINT="$BIN_PATH/eslint" | |
# Check for eslint | |
if [[ ! -x "$ESLINT" ]]; then | |
printf "\t\033[41mPlease install ESLint\033[0m\n" | |
exit 1 | |
fi | |
echo "Linting modified test files" | |
echo $STAGED_FILES | xargs $ESLINT --cache --cache-location=.cache/eslint "-c" "./.eslintrc.spec.js" "--ext" ".spec.js,.spec.jsx" "." | |
if [[ $? == 0 ]]; then | |
printf "\n\033[1;32mSpec Lint Passed\033[0m\n" | |
else | |
printf "\n\033[41mLint Failed:\033[0m Fix lint errors and try again!\n" | |
exit 1 | |
fi | |
} | |
jest() { | |
JEST="$BIN_PATH/jest" | |
# Check for eslint | |
if [[ ! -x "$JEST" ]]; then | |
printf "\t\033[41mPlease install Jest\033[0m\n" | |
exit 1 | |
fi | |
echo "Running tests related to modified files" | |
$JEST --bail --findRelatedTests $STAGED_FILES | |
if [[ $? == 0 ]]; then | |
printf "\n\033[1;32mTest Passed\033[0m\n" | |
else | |
printf "\n\033[41mTest Failed:\033[0m Fix test errors and try again!\n" | |
exit 1 | |
fi | |
} | |
# Exit if no files modified | |
if [[ "$STAGED_FILES" = "" ]]; then | |
exit 0 | |
fi | |
eslint | |
eslintSpec | |
jest | |
exit $? |
Just modified this to only run on .js
or .jsx
files for now.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A nice config to lint/run tests before you commit