Skip to content

Instantly share code, notes, and snippets.

@malko
Forked from mebibou/pre-commit
Last active December 15, 2015 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malko/373e9b7d6e219d1a6bac to your computer and use it in GitHub Desktop.
Save malko/373e9b7d6e219d1a6bac to your computer and use it in GitHub Desktop.
JSHint and JSCS pre-commit hook
#!/bin/sh
# pre-commit git hook.
files=$(git diff --cached --name-only --diff-filter=ACMR -- \*.js **/*.js)
pass=true
errorJscs=0
errorJshint=0
if [ "$files" != "" ]; then
for file in ${files}; do
# Run JSHint validation
result=$(jshint ${file})
if [ "$result" != "" ]; then
echo "jshint: \033[31m$result\033[0m"
pass=false
errorJshint=$((errorJshint + 1))
fi
# Run JSCS validation
result=$(jscs ${file})
#if [ "$result" != "No code style errors found." ]; then
if [ "$result" != "" ]; then
echo "jscs: \033[31m'$result'\033[0m"
pass=false
errorJscs=$((errorJscs + 1))
fi
done
fi
if $pass; then
echo "\033[1;32mOk\033[0m"
exit 0
else
echo ""
echo "\033[1;31mCOMMIT FAILED:\033[0m"
echo "Some JavaScript files are invalid. Please fix $(($errorJscs + $errorJshint)) error(s) and try committing again."
if [ $errorJscs -gt 0 ]; then
echo "$errorJscs Jscs errors";
fi
if [ $errorJshint -gt 0 ]; then
echo "$errorJshint Jshint errors";
fi
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment