Last active
January 16, 2017 03:47
-
-
Save crashbell/82f1136abf819a212f5c5f993c313c24 to your computer and use it in GitHub Desktop.
Trigger `npm run lint` before pushing code to repo. Save as `pre-commit` or `pre-push` in .git/hooks. And `chmod 775` that file to make it executable.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| HAS_NPM=`which npm 2> /dev/null` | |
| # | |
| # There are some issues with Source Tree because paths are not set correctly for | |
| # the given environment. Sourcing the bash_profile seems to resolve this for bash users, | |
| # sourcing the zshrc for zshell users. | |
| # | |
| # https://answers.atlassian.com/questions/140339/sourcetree-hook-failing-because-paths-don-t-seem-to-be-set-correctly | |
| # | |
| function source_home_file { | |
| file="$HOME/$1" | |
| [[ -f "${file}" ]] && source "${file}" | |
| } | |
| if [[ -z "$HAS_NPM" ]]; then | |
| source_home_file ".bash_profile" || source_home_file ".zshrc" || source_home_file ".bashrc" || true | |
| fi | |
| NPM=`which npm 2> /dev/null` | |
| LOCAL="/usr/local/bin/npm" | |
| BINARY= | |
| OUTPUT= | |
| pass=true | |
| # | |
| # Figure out which binary we need to use for our script execution. | |
| # | |
| if [[ -n "$NPM" ]]; then | |
| BINARY="$NPM" | |
| elif [[ -x "$LOCAL" ]]; then | |
| BINARY="$LOCAL" | |
| fi | |
| # | |
| # Add --dry-run cli flag support so we can execute this hook without side affects | |
| # and see if it works in the current environment | |
| # | |
| if [[ $* == *--dry-run* ]]; then | |
| if [[ -z "$BINARY" ]]; then | |
| exit 1 | |
| fi | |
| else | |
| OUTPUT=$("$BINARY" run lint) | |
| result=$(echo "$OUTPUT" | grep error) | |
| if [ "$result" != "" ]; then | |
| pass=false | |
| fi | |
| fi | |
| echo "$OUTPUT" | grep --color=always -E "error|$" | |
| if ! $pass; then | |
| echo "FAILED" | |
| exit 1 | |
| else | |
| echo "SUCCEEDED" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment