TSLint预提交挂钩

时间:2016-05-06 20:05:29

标签: git typescript angular

我一直在环顾四周,我可以看到GSL的JSLint,JsHint预提交钩子示例,但我找不到TSLint的一个。我尝试调整一个没有成功。任何帮助真的很感激!

1 个答案:

答案 0 :(得分:2)

你走了。它适用于全局安装的tslint以及控制台或VS Code,Atom。由于某种原因,它无法与SourceTree一起使用,因为它无法找到您的tslint。如果你想通过Sourcetree使用commit,你需要硬编码tslint路径。

#!/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 $?