Skip to content

Instantly share code, notes, and snippets.

@pgpbpadilla
Created April 6, 2014 00:47
Show Gist options
  • Save pgpbpadilla/9999991 to your computer and use it in GitHub Desktop.
Save pgpbpadilla/9999991 to your computer and use it in GitHub Desktop.
Git pre-commit hook: Run JSLint for NodeJS projects
#!/bin/sh
# find node
NODE=$(which node)
# if it cannot find a node installation
if [[ -z $NODE ]]; then
#NodeJS is not installed
echo "Please install NodeJS first."
exit 1
fi
# make sure jslint is installed
if [[ ! -d ./node_modules/jslint ]]; then
#install jslint locally
npm install jslint
fi
# run JSLint before every commit, don't commit until
# JSLint is OK
for file in $(git diff --name-only --cached | egrep '.js$');
do
$NODE ./node_modules/jslint/bin/jslint.js $file
# If the result of the previous command is NOT zero
if [ $? -ne 0 ]; then
echo "*** ERROR *** : JSLint failed for file: $file, please fix code and recommit"
exit 1
fi
done
# Everything is ok
exit 0
@pgpbpadilla
Copy link
Author

  • Place this file under .git/hooks
  • The file must be named: pre-commit
  • It must be executable: $ chmod a+x .git/hooks/pre-commit

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