Last active
August 29, 2015 14:13
-
-
Save dalemanthei/508d3d75498b8f13c9ba to your computer and use it in GitHub Desktop.
NodeJS version of pre-commit to run jshint, jscs, and simple scan
This file contains 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/local/bin/node | |
var cp = require('child_process'); | |
module.paths.push('/usr/local/lib/node_modules'); // hacky hacky hacky to get to async | |
var async = require('async'); | |
var getJsFiles = 'git diff --cached --name-only --diff-filter=ACM | grep ".js$"'; | |
cp.exec(getJsFiles, function (err, stdout, stderr) { | |
var failCount = 0; | |
var jsFiles = []; | |
if (err) { | |
console.log('stderr: ' + stderr); | |
process.exit(1); | |
} | |
stdout.split(/\r?\n/).forEach(function (line) { | |
if (line.length > 0) { | |
jsFiles.push(line); | |
} | |
}); | |
console.log('\n\033[36mJavaScript validation:\033[0m'); | |
async.forEachSeries(jsFiles, function (file, callback) { | |
var jsFile = __dirname + '/' + file; | |
async.series([ | |
function (tcb) { | |
process.stdout.write('\n\t\033[36mChecking ' + file + '\033[0m '); | |
tcb(); | |
}, | |
function (tcb) { | |
cp.exec('jshint ' + jsFile, function (err, stdout, stderr) { | |
if (err) { | |
fail('JSHint'); | |
failCount += 1; | |
} else { | |
pass('JSHint'); | |
} | |
tcb(); | |
}); | |
}, | |
function (tcb) { | |
cp.exec('jscs ' + jsFile, function (err, stdout, stderr) { | |
if (err) { | |
fail('JSCS'); | |
failCount += 1; | |
} else { | |
pass('JSCS'); | |
} | |
tcb(); | |
}); | |
}, | |
function (tcb) { | |
cp.exec('egrep -e \'console.log\' -e debugger ' + jsFile + ' -m 1 | wc -l', function (err, stdout, stderr) { | |
if (stdout.trim() !== '0') { | |
fail('Scan'); | |
failCount += 1; | |
} else { | |
pass('Scan'); | |
} | |
tcb(); | |
}); | |
} | |
], function (err) { | |
callback(); | |
}); | |
}, | |
function (err) { | |
console.log('\n\n\033[36mJavaScript validation complete\033[0m\n'); | |
if (failCount > 0) { | |
console.log('\033[41mCOMMIT FAILED:\033[0m Your commit contains files that do not pass one or more checks. ' + | |
'Please fix the errors and try again.\n'); | |
} else { | |
console.log('\033[32m 🍺 COMMIT SUCCEEDED\033[0m\n'); | |
} | |
process.exit(failCount); | |
}); | |
}); | |
function pass(message) { | |
process.stdout.write('\033[32m' + message + '\033[0m '); | |
} | |
function fail(message) { | |
process.stdout.write('\033[31m' + message + '\033[0m '); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment