Skip to content

Instantly share code, notes, and snippets.

@brianmriley
Created September 18, 2013 15:40
Show Gist options
  • Save brianmriley/6611101 to your computer and use it in GitHub Desktop.
Save brianmriley/6611101 to your computer and use it in GitHub Desktop.
Git Pre-Commit shell script hook that executes Grunt tasks for JSHint && Karma Unit Tests before committing. Currently setup to change the directory as first step to the dir with your Gruntfile.js. Also adds PATH to grunt executable to current shell's PATH.
#!/bin/sh
#
# Pre-commit hooks
######################################################################
# Environment Setup
# 1) Change directory to build dir so we can run grunt tasks.
# 2) Make sure path is extended to include grunt task executable
# dir, as this commit shell is executed in the git
# client's own shell; ie Tower and WebStorm have own shell path.
######################################################################
cd build
PATH=$PATH:~/usr/local/bin
PATH=$PATH:/usr/local/bin
######################################################################
# JSHint: Lint stuff before committing
######################################################################
grunt jshint
EXIT_CODE=$?
# echo ${EXIT_CODE}
if [[ ${EXIT_CODE} -ne 0 ]]; then
echo "[ERRROR] code = " ${EXIT_CODE}
echo "JSHint detected syntax problems."
echo "Commit aborted."
exit 1
else
echo "JSHint completed successfully\n"
fi
######################################################################
# Unit Tests: Run unit tests/specs before committing
######################################################################
grunt karma:continuous
EXIT_CODE=$?
if [[ ${EXIT_CODE} -ne 0 ]]; then
echo "[ERRROR] code = " ${EXIT_CODE}
echo "Karma Unit Tests failed."
echo "Commit aborted."
exit 1
else
echo "Karma Unit Tests completed successfully\n"
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment