Skip to content

Instantly share code, notes, and snippets.

@t0mtaylor
Forked from rtircher/jshint-runner.js
Last active December 19, 2015 19:48
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 t0mtaylor/6008621 to your computer and use it in GitHub Desktop.
Save t0mtaylor/6008621 to your computer and use it in GitHub Desktop.
Include external json config file for JSHINT Options, also added XML Checkstyle output for use with CI such as Jenkins - create a task to merge the start and end xml files with the output data from jshint to create your checkstyle report (see comments)
{
"smarttabs" : true,
"-W099" : true
}
// PhantomJS doesn't support bind yet
Function.prototype.bind = Function.prototype.bind || function (thisp) {
var fn = this;
return function () {
return fn.apply(thisp, arguments);
};
};
(function(p) {
if (p.args.length < 2) {
console.log('Usage: phantomjs phantom-jshint-runner.js jshint-2.1.4.js path/to/optional/jshint-config.json path/to/optional/jshint-reports-out.xml');
p.exit(1);
}
var fs = require('fs'),
jshintfile = p.args[0],
jsfile = p.args[1];
jshintconfig = p.args[2] || "",
jshintresults = p.args[3] || "",
options = {},
results = "",
hasPassed = true;
console.log('phantomjs> jshint> Checking file: '+jsfile);
if (!fs.isReadable(jsfile)) {
console.log('phantomjs> jshint> Unreadable file: '+jsfile);
p.exit(1);
}
if (!p.injectJs(jshintfile)) {
console.log('phantomjs> jshint> Could not inject jshint.');
p.exit(1);
}
if (jshintconfig.length > 0) {
if (!fs.isReadable(jshintconfig)) {
console.log('phantomjs> jshint> Could not read (optional) jshint json object config that was set!');
p.exit(1);
}
options = JSON.parse(fs.read(jshintconfig));
}
JSHINT(fs.read(jsfile), options);
/* If running against a single file enable the
* starting syntax of the xml report to be written,
* Otherwise build your report file with your
* task runner and combine a start and end xml file
* with the results data for all files reported!
*
results += '<?xml version="1.0" encoding="utf-8"?>\n'
+ '<checkstyle version="4.3">\n';
*/
if (JSHINT.errors.length > 0) {
results += '<file name="' + jsfile.split(/(\\|\/)/g).pop() + '">\n';
JSHINT.errors.forEach(function(err) {
results += '<error line="' + err.line + '" column="' + err.character + '" severity="error" message="' + err.reason + '"/>\n';
console.log("phantomjs> jshint> Failed: "+jsfile + ':' + err.line + ' [' + err.character + '] ' + err.reason);
});
results += '</file>\n';
hasPassed = false;
} else {
console.log("phantomjs> jshint> Passed!");
}
/* If running against a single file enable the
* ending syntax of the xml report to be written,
* Otherwise build your report file with your
* task runner and combine a start and end xml file
* with the results data for all files reported!
*
results += '</checkstyle>';
*/
if (jshintresults.length > 0) {
try {
console.log("phantomjs> jshint> writing coverage data...");
f = fs.open(jshintresults, "a");
f.write(results);
f.close();
console.log("phantomjs> jshint> writing coverage data completed!");
} catch(e) {
console.log(e);
console.log("phantomjs> jshint> Unable to save coverage data!");
hasPassed = false;
}
}
p.exit(hasPassed ? 0 : 1);
}(phantom));
<?xml version="1.0" encoding="utf-8"?>
<checkstyle version="4.3">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment