Skip to content

Instantly share code, notes, and snippets.

@twhid
Last active November 13, 2015 18:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twhid/f6cb51ee3dcede364dfa to your computer and use it in GitHub Desktop.
Save twhid/f6cb51ee3dcede364dfa to your computer and use it in GitHub Desktop.
Run JSHint validation before commit.
#!/bin/sh
#
# Run JSHint validation before commit.
#
# Originally: http://stackoverflow.com/a/21238963/302550
#
# Requires Node (http://nodejs.org/) and jshint (https://www.npmjs.org/package/jshint)
# be globally installed (e.g.: npm install jshint -g).
#
# Edited slightly to make it easier to work with Git GUIs (e.g. PHPStorm IDE, Tower).
# End user must edit the BIN_PATH to ensure the node binary is found and
# the NPM_MODULES_PATH to ensure the global jshint node module is found.
#
# If you plan to use only on your CLI, then the first 4 lines may be removed.
#
BIN_PATH="/usr/local/bin"
NPM_MODULES_PATH="/usr/local/share/npm/bin"
PATH="$BIN_PATH:$NPM_MODULES_PATH:$PATH"
files=$(git diff --cached --name-only --diff-filter=ACMR -- *.js **/*.js)
pass=true
if [ "$files" != "" ]; then
for file in ${files}; do
result=$(jshint ${file} --config=/Users/username/projects/my-awesome-project/.jshintrc)
if [ "$result" != "" ]; then
echo "$result"
pass=false
fi
done
fi
if $pass; then
exit 0
else
echo ""
echo "COMMIT FAILED:"
echo "Some JavaScript files are invalid. Please fix errors and try again."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment