ng-flo
ng-flo

Reputation: 293

TSLint pre-commit hook

I've been looking around and I can see JSLint , JsHint pre-commit hooks example for Git but I cannot find one for TSLint. I tried adapting one with no success. Any help really appreciated!

Upvotes: 0

Views: 2283

Answers (1)

tomitrescak
tomitrescak

Reputation: 1110

Here you go. It works with globally installed tslint and in the console or VS Code, Atom. For some reason it does not work with SourceTree as it cannot locate your tslint. If you want to use commit through Sourcetree, you need to hard code tslint path.

#!/bin/sh

STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep ".tsx\{0,1\}$")

if [[ "$STAGED_FILES" = "" ]]; then
  exit 0
fi

PASS=true

echo "\nValidating Typescript:\n"

# Check for tslint
which tslint &> /dev/null
if [[ "$?" == 1 ]]; then
  echo "\t\033[41mPlease install TSlint\033[0m"
  exit 1
fi

for FILE in $STAGED_FILES
do
  tslint "$FILE"

  if [[ "$?" == 0 ]]; then
    echo "\t\033[32mTSLint Passed: $FILE\033[0m"
  else
    echo "\t\033[41mTSLint Failed: $FILE\033[0m"
    PASS=false
  fi
done

echo "\nTypescript validation completed!\n"

if ! $PASS; then
  echo "\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
  echo "\033[42mCOMMIT SUCCEEDED\033[0m\n"
fi

exit $?

Upvotes: 2

Related Questions