Skip to content

Instantly share code, notes, and snippets.

@pzi
Last active May 13, 2019 06:33
Show Gist options
  • Save pzi/256d9bfca1df2d699dc90ae27e9dc3c3 to your computer and use it in GitHub Desktop.
Save pzi/256d9bfca1df2d699dc90ae27e9dc3c3 to your computer and use it in GitHub Desktop.
Pre Commit hook to run TSLint before committing. Based off https://gist.github.com/rashtay/328da46a99a9d7c746636df1cf769675 ---- add to `.git/hooks/` as `pre-commit` and run `chmod +x .git/hooks/pre-commit`.
#!/bin/sh
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep ".[j|t]sx\{0,1\}$")
TSLINT="$(git rev-parse --show-toplevel)/WebClient/node_modules/.bin/tslint"
if [[ "$STAGED_FILES" = "" ]]; then
exit 0
fi
PASS=true
printf "\nValidating Javascript:\n"
# Check for tslint
if [[ ! -x "$TSLINT" ]]; then
printf "\t\033[41mPlease install TSLint\033[0m (yarn i --save-dev tslint)"
exit 1
fi
for FILE in $STAGED_FILES
do
"$TSLINT" "$FILE"
if [[ "$?" == 0 ]]; then
printf "\t\033[32mTSLint Passed: $FILE\033[0m"
else
printf "\t\033[41mTSLint Failed: $FILE\033[0m"
PASS=false
fi
done
printf "\nJavascript validation completed!\n"
if ! $PASS; then
printf "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass TSLint but do not. Please fix the TSLint 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