-
Notifications
You must be signed in to change notification settings - Fork 1
Pre commit hook for linters
chetanyakan edited this page May 10, 2018
·
1 revision
This is a pre commit hook to automatically run linters (tslint and stylelint) before a commit can be made.
- If you have never added any pre commit hooks to this project:
- Add the code for the hook to
[project]/.git/hooks/pre-commit.sample
- Rename [
project]/.git/hooks/pre-commit.sample
to[project]/.git/hooks/pre-commit
(remove the .sample)
- Add the code for the hook to
- If you have added pre commit hooks to this project:
- Add the code for the hook to your already existing
pre-commit
file in.git/hooks
- Add the code for the hook to your already existing
NOTE: It is usually a better idea to actually rename the sample file rather than create a new file and name it pre-commit
because the permissions have to change. If you do decide to create a new file you need to update the permissions of it with chmod +x pre-commit
.
You should now have a pre-commit file inside of .git/hooks that contains the given hook.
#!/bin/sh
pass=true
RED='\033[1;31m'
GREEN='\033[0;32m'
NC='\033[0m'
echo "Running Linters:"
# Run tslint and get the output and return code
tslint=$(npm run tslint)
ret_code=$?
# If it didn't pass, announce it failed and print the output
if [ $ret_code != 0 ]; then
printf "\n${RED}tslint failed:${NC}"
echo "$tslint\n"
pass=false
else
printf "${GREEN}tslint passed.${NC}\n"
fi
# Run stylelint and get the output and return code
stylelint=$(npm run stylelint)
ret_code=$?
if [ $ret_code != 0 ]; then
printf "${RED}stylelint failed:${NC}"
echo "$stylelint\n"
pass=false
else
printf "${GREEN}stylelint passed.${NC}\n"
fi
# If there were no failures, it is good to commit
if $pass; then
exit 0
fi
exit 1