Skip to content

Instantly share code, notes, and snippets.

@dcurletti
Created September 27, 2018 23:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dcurletti/89edd6a5b329fa01faf39148ffafa71a to your computer and use it in GitHub Desktop.
Save dcurletti/89edd6a5b329fa01faf39148ffafa71a to your computer and use it in GitHub Desktop.
Bash script to typecheck project on TypeScript file changes
#!/bin/bash
# You can make the script more generic by changing the matching pattern
SRC_PATTERN=".*\.(ts|tsx)$"
# needed bc env vars change when run inside a git hook
# https://stackoverflow.com/questions/3542854/calling-git-pull-from-a-git-post-update-hook
# https://serverfault.com/questions/107608/git-post-receive-hook-with-git-pull-failed-to-find-a-valid-git-directory
unset $(git rev-parse --local-env-vars)
if git diff --cached --name-only | grep --quiet -E "$SRC_PATTERN"
then
# This block executes only when there are staged files matching the pattern above
echo "type-checking typescript files"
yarn type-check
exit 0
fi
@JM-Mendez
Copy link

Thanks! I was able to use this script with lint-staged. I made one minor change tho:

# from 
exit 0

# to
exit $?

$? captures the result of the last executed command. This way lint-stage fails the pre-commit hook if it finds an error.

@ScreamZ
Copy link

ScreamZ commented Jul 10, 2019

Can you provide yarn type-check command ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment