Last active
December 23, 2015 11:39
-
-
Save dtudury/6629720 to your computer and use it in GitHub Desktop.
little nagging git hook to remind me to jshint my changes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
//install this script from the root of the local repo with something like... | |
//mkdir -p .git/hooks ; curl -L https://gist.github.com/dtudury/6629720/raw > .git/hooks/pre-commit ; chmod +x .git/hooks/pre-commit | |
var EXEC = require('child_process').exec; | |
var JSHINT = require('jshint').JSHINT; | |
var FS = require('fs'); | |
EXEC('git status --porcelain', function (error, stdout, stderr) { | |
if(error) { | |
console.log('stderr:', stderr); | |
process.exit(1); | |
} | |
var filenames = stdout.split(/\r?\n/); | |
filenames = filenames.map(function(line) { | |
var parts = line.split(/\s+/); | |
return parts.length && /[AM]/.test(parts[0]) && /\.JS$/i.test(parts[1]) && parts[1]; //if it's good, use it | |
}); | |
filenames = filenames.filter(function(filename) { | |
return filename; //remove falsey entries | |
}); | |
filenames.forEach(function(filename) { | |
if(!JSHINT(FS.readFileSync(filename).toString())) { | |
EXEC('jshint ' + filenames.join(' '), function (error, stdout, stderr) { | |
//we're expecting an error but it writes to stdout | |
console.log(stderr); | |
console.log(stdout); | |
console.error("[SHAME] jshint fails"); | |
console.error("[SHAME] run 'jshint' from root of project"); | |
console.error("[SHAME] something like: 'jshint " + filenames.join(' ') + "'"); | |
console.error("[SHAME] to ignore this say --no-verify"); | |
console.error("[SHAME] COMMIT FAILED! NO COMMIT FOR YOU!!!1!"); | |
process.exit(1); | |
}); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment