As a follow on to my post about using a Git hook with PHP Code Sniffer to make sure that you’re committing good PHP code, this post is going to cover doing the same thing to make sure that your JavaScript passing a JSLint check.
The only pre-requisite to get this to work is to install JSLint using npm. Install npm and then run npm install jslint in your project root folder.
A now the .git/hook/pre-commit code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# Pre-commit hook passing files through jslint # # This ensures that all js, html and json files are valid and conform # to expectations. ROOT_DIR=$(git rev-parse --show-toplevel) JSLINT="${ROOT_DIR}/node_modules/.bin/jslint --indent 4 --white true" for file in $(git diff-index --name-only --diff-filter=ACM --cached HEAD -- | grep -P '\.((js)|(html)|(json))$'); do if node $JSLINT $file 2>&1 | grep 'No errors found' ; then echo "jslint passed ${file}" exit 0 else node $JSLINT $file exit 1 fi done |
You will want to make sure that the path to JSLink is correct on line 7. If you want to change what files get checked, you can change the extensions in line 9.
Code source: http://stackoverflow.com/questions/1837681/pre-commit-hook-for-jslint-in-mercurial-and-git