Skip to content

Instantly share code, notes, and snippets.

@dtudury
Last active December 23, 2015 11:39
Show Gist options
  • Save dtudury/6629720 to your computer and use it in GitHub Desktop.
Save dtudury/6629720 to your computer and use it in GitHub Desktop.
little nagging git hook to remind me to jshint my changes
#!/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