Skip to content

Instantly share code, notes, and snippets.

@olemb
Created September 5, 2011 10:46
Show Gist options
  • Save olemb/1194680 to your computer and use it in GitHub Desktop.
Save olemb/1194680 to your computer and use it in GitHub Desktop.
First stab at a CLI frontend for jshint.js
#!/usr/bin/env node
/*
* CLI frontend for jshint.js
*
* When I wrote this, I didn't know there is
*
* a much more mature project at:
* https://github.com/jshint/node-jshint
*
* I have shut down my own project and will be
* contributing to that one instead. I leave this
* gist here just in case there is something in
* here that can be reused.
*
* I put this code in the public domain. Do
* whatever you want with it.
*
* - Ole Martin
* http://nerdly.info/ole/
*
*/
wrapper = (function () {
// 'use strict';
var fs = require('fs');
var sys = require('sys');
var path = require('path');
// require.paths.push();
var modulefile = path.dirname(__filename) + '/jshint.js';
var jshint = require(modulefile);
var lint = jshint.JSHINT;
function readfile(filename, callback) {
var source = '';
var s = fs.createReadStream(filename);
s.on('data', function(data) {
source += data;
});
s.on('end', function() {
callback(source);
});
}
function checkfile(filename, options) {
function run_jshint(source) {
var i;
var j;
var err;
if (lint(source, options) === false) {
for (i = 0; i < lint.errors.length; i++) {
err = lint.errors[i];
if (err === null) {
break; // Too many errors
}
sys.puts(filename + ':' + err.line + ': ' + err.reason);
if (err.evidence === undefined) {
sys.puts('');
} else {
sys.puts(' ' + err.evidence);
}
// Print caret (point to the problem)
for(j = 0; j < (1 + err.character); j++) {
sys.print(' ');
}
sys.puts('^');
}
}
}
readfile(filename, run_jshint);
}
var options = {};
var descriptions = {};
readfile(modulefile, function (text) {
// Read options from jshint.js
var re = /([a-z]+)\s+:\s+(true|false), \/\/ (.*)\n/g;
var match;
while (match = re.exec(text)) {
options[match[1]] = eval(match[2]);
descriptions[match[1]] = match[3];
}
var args = process.argv.slice(2);
var opt;
var name;
var errors = false;
// Check for options (comma-separated list of options to turn off)
if(args[0] == '-o' || args[0] == '--options') {
args[1].split(',').forEach(function (opt) {
if (options[opt] == undefined) {
sys.print('Unknown option ' + opt + '\n');
errors = true;
} else {
options[opt] = false;
}
});
args = args.slice(2); // Remove this option
}
if(args[0] == '-h' || args[0] == '--help') {
var p = sys.print;
p('Usage: jshint [OPTIONS] FILE [FILE ...]\n');
p('\n')
p("JSHint detects errors and potential problems in JavaScript\n")
p("code, and helps to enforce your team's coding conventions.\n")
p('\n')
p(' -h --help print this help text\n')
p(' -o --off comma separated list of options to turn off (see below)\n')
p('\n')
for (opt in options) {
name = opt;
while (name.length < 12) {
name = ' ' + name; // Ugly. Should use ' '.repeat() or something
}
p(name + ' ' + descriptions[opt] + '\n'); // Todo: print explanation
}
args = args.slice(1);
process.exit(code=0);
}
if (errors) {
process.exit(1);
}
args.forEach(function (filename) {
checkfile(filename, options);
});
});
}) ();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment