Search code examples
githookstslintpre-commit-hookpre-commitgit-husky

tslint precommit hook shows all the linting errors before but also allows to commit the code


I am using angular-seed & husky to add a precommit hook for git. My package.json has

"scripts": {
    "precommit": "npm test && npm run lint",
  }

When I commit the code, husky runs "npm test" & "npm run lint" fine. When npm test fails, it shows me the errors on the console & doesn't allow me to commit. But when there are errors on "npm run lint" then the console displays all the error messages but also allows to commit. How can avoid the commit to when there are linting errors? Any help is appreciated. Thank you in advance! This is how my .git\hooks\pre-commit looks:

#!/bin/sh
#husky 0.14.3

command_exists () {
  command -v "$1" >/dev/null 2>&1
}

has_hook_script () {
  [ -f package.json ] && cat package.json | grep -q "\"$1\"[[:space:]]*:"
}

cd "."

# Check if precommit script is defined, skip if not
has_hook_script precommit || exit 0

# Node standard installation
export PATH="$PATH:/c/Program Files/nodejs"

# Check that npm exists
command_exists npm || {
  echo >&2 "husky > can't find npm in PATH, skipping precommit script in package.json"
  exit 0
}

# Export Git hook params
export GIT_PARAMS="$*"

# Run npm script
echo "husky > npm run -s precommit (node `node -v`)"
echo

npm run -s precommit || {
  echo
  echo "husky > pre-commit hook failed (add --no-verify to bypass)"
  exit 1
}

Solution

  • In your seed.config.ts, you should have a boolean called FORCE_TSLINT_EMIT_ERROR. Override the value of this variable explicitly in your project.config.ts to true.