Skip to content

Instantly share code, notes, and snippets.

@cismous
Last active April 13, 2019 04:49
Show Gist options
  • Save cismous/dba576d1e2b6463073a3fae6446b1f86 to your computer and use it in GitHub Desktop.
Save cismous/dba576d1e2b6463073a3fae6446b1f86 to your computer and use it in GitHub Desktop.
typescript pre commit example
#!/bin/sh
PATH=$PATH:/usr/local/bin
PROJECT_ROOT=$(pwd)
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep ".tsx\{0,1\}$")
ESLINT="$(git rev-parse --show-toplevel)/node_modules/.bin/eslint"
if [[ "$STAGED_FILES" = "" ]]; then
exit 0
fi
PASS=true
printf "\nValidating Typescript:\n"
# Check for eslint
if [[ ! -x "$ESLINT" ]]; then
printf "\t\033[41mPlease install ESlint\033[0m (yarn add eslint --dev)"
exit 1
fi
for FILE in $STAGED_FILES
do
npx eslint "$PROJECT_ROOT/$FILE"
if [[ "$?" == 0 ]]; then
printf "\t\033[32mESLint Passed: $FILE\033[0m"
else
printf "\t\033[41mESLint Failed: $FILE\033[0m"
PASS=false
fi
done
printf "\nTypescript validation completed!\n"
if ! $PASS; then
printf "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass ESLint but do not. Please fix the ESLint errors and try again.\n"
exit 1
else
printf "\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