Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

initial commit

  • Loading branch information...
commit 1c5ab20420740a07809c0095636829b25bdca83b 0 parents
brentlintner authored
3  .gitmodules
... ... @@ -0,0 +1,3 @@
  1 +[submodule "packages/jshint"]
  2 + path = packages/jshint
  3 + url = git://github.com/jshint/jshint.git
5 HELP
... ... @@ -0,0 +1,5 @@
  1 +Usage: node-hint path path2 [options]
  2 +
  3 +Options:
  4 +
  5 + --config custom config file
3  Jakefile
... ... @@ -0,0 +1,3 @@
  1 +task('default', [], require('./build/test'));
  2 +task('test', [], require('./build/test'));
  3 +task('lint', [], require('./build/lint'));
9 LICENSE
... ... @@ -0,0 +1,9 @@
  1 +** Licensed Under **
  2 +
  3 +The MIT License
  4 +http://www.opensource.org/licenses/mit-license.php
  5 +
  6 +Copyright 2010 Brent Lintner
  7 +
  8 +Contributors:
  9 + Brent Lintner (http://github.com/brentlintner)
32 README.md
Source Rendered
... ... @@ -0,0 +1,32 @@
  1 +# node-hint
  2 +
  3 +A command line interface for jshint.
  4 +
  5 +## Install
  6 +
  7 + npm install node-hint
  8 +
  9 +## Using
  10 +
  11 + node-hint path path2 [options]
  12 +
  13 +## Options
  14 +
  15 + --config path/to/config.json
  16 +
  17 +## Running Tests
  18 +
  19 + npm install sinon nodeunit
  20 +
  21 +## Jake Commands
  22 +
  23 + // run tests
  24 + jake test
  25 +
  26 + // run jshint
  27 + jake lint
  28 +
  29 +# TODO
  30 +
  31 +* move bulk of code in cli to a separate module and put under test (tdd fail)
  32 +* add custom reporters
2  bin/node-hint
... ... @@ -0,0 +1,2 @@
  1 +#!/usr/bin/env node
  2 +require('./../lib/cli').interpret(process.argv);
8 build/lint.js
... ... @@ -0,0 +1,8 @@
  1 +module.exports = function (files) {
  2 + var args = files && files.length > 0 ? files : ["build", "test", "lib"],
  3 + spawn = require('child_process').spawn,
  4 + cmd = spawn("node-hint", args),
  5 + sys = require('sys');
  6 + cmd.stdout.on('data', sys.print);
  7 + cmd.stderr.on('data', sys.print);
  8 +};
9 build/test.js
... ... @@ -0,0 +1,9 @@
  1 +module.exports = function () {
  2 + var args = Array.prototype.map.call(arguments, function (test) {
  3 + return "test/" + test + ".js";
  4 + }), cmd,
  5 + sys = require('sys');
  6 + cmd = require('child_process').spawn("nodeunit", args.length > 0 ? args : ["test/"]);
  7 + cmd.stdout.on('data', sys.print);
  8 + cmd.stderr.on('data', sys.print);
  9 +};
88 lib/cli.js
... ... @@ -0,0 +1,88 @@
  1 +// inspired by github.com/tav/nodelint
  2 +var _fs = require('fs'),
  3 + _sys = require('sys'),
  4 + _path = require('path'),
  5 + _jshint = require('./../packages/jshint/jshint.js').JSHINT,
  6 + _config;
  7 +
  8 +function _collect(path, files) {
  9 + if (_fs.statSync(path).isDirectory()) {
  10 + _fs.readdirSync(path).forEach(function (item) {
  11 + _collect(_path.join(path, item), files);
  12 + });
  13 + } else if (path.match(/\.js$/)) {
  14 + files.push(path);
  15 + }
  16 +}
  17 +
  18 +function _reporter(results) {
  19 + var len = results.length,
  20 + str = '',
  21 + file, error;
  22 +
  23 + results.forEach(function (result) {
  24 + file = result.file;
  25 + error = result.error;
  26 + str += file + ': line ' + error.line + ', col ' +
  27 + error.character + ', ' + error.reason + '\n';
  28 + });
  29 +
  30 + _sys.puts(len > 0 ? (str + "\n" + len + ' error' + ((len === 1) ? '' : 's')) : "Lint Free!");
  31 +}
  32 +
  33 +function _lint(file, results) {
  34 + var buffer;
  35 +
  36 + try {
  37 + buffer = _fs.readFileSync(file, 'utf8');
  38 + } catch (e) {
  39 + _sys.puts("Error: Cant open: " + file);
  40 + _sys.puts(e + '\n');
  41 + }
  42 +
  43 + if (!_jshint(buffer, _config)) {
  44 + _jshint.errors.forEach(function (error) {
  45 + if (error) results.push({file: file, error: error});
  46 + });
  47 + }
  48 +}
  49 +
  50 +function _help() {
  51 + _sys.print(_fs.readFileSync(__dirname + "/../HELP", "utf-8"));
  52 +}
  53 +
  54 +module.exports = {
  55 + interpret: function (args) {
  56 + var files = [],
  57 + results = [],
  58 + options = require('argsparser').parse(args),
  59 + customConfig = options["--config"],
  60 + targets = typeof options.node === "string" ?
  61 + null : options.node.slice(1);
  62 +
  63 + if (customConfig) {
  64 + try {
  65 + _config = JSON.parse(_fs.readFileSync(customConfig));
  66 + } catch (e) {
  67 + _sys.puts("Error opening config file: " + options["--config"]);
  68 + _sys.puts(e + "\n");
  69 + process.exit(1);
  70 + }
  71 + }
  72 +
  73 + if (!targets || options["--help"]) {
  74 + _help();
  75 + return;
  76 + }
  77 +
  78 + targets.forEach(function (target) {
  79 + _collect(target, files);
  80 + });
  81 +
  82 + files.forEach(function (file) {
  83 + _lint(file, results);
  84 + });
  85 +
  86 + _reporter(results);
  87 + }
  88 +};
27 package.json
... ... @@ -0,0 +1,27 @@
  1 +{
  2 + "name": "node-hint",
  3 + "version": "0.1.0",
  4 + "description": "jshint as a npm package + cli",
  5 + "homepage": "http://github.com/brentlintner/node-hint",
  6 + "author": {
  7 + "name": "Brent Lintner",
  8 + "email": "brent.lintner@gmail.com",
  9 + "url": "http://github.com/brentlintner"
  10 + },
  11 + "licenses": [{
  12 + "type": "MIT",
  13 + "url": "http://www.opensource.org/licenses/mit-license.php"
  14 + }],
  15 + "bin": "./bin/node-hint",
  16 + "main": "./lib",
  17 + "files": [
  18 + "packages/jshint/README.markdown",
  19 + "packages/jshint/jshint.js",
  20 + "README.md",
  21 + "LICENSE",
  22 + "HELP"
  23 + ],
  24 + "dependencies": {
  25 + "argsparser": ">=0.0.3"
  26 + }
  27 +}
1  packages/jshint
... ... @@ -0,0 +1 @@
  1 +Subproject commit 521230dae2c9ca94b6c6e8047497b2f34dbe5b90
33 test/cli.js
... ... @@ -0,0 +1,33 @@
  1 +var s,
  2 + sys = require('sys'),
  3 + cli = require('./../lib/cli');
  4 +
  5 +module.exports = require('nodeunit').testCase({
  6 +
  7 + setUp: function (done) {
  8 + s = require('sinon').sandbox.create();
  9 + done();
  10 + },
  11 +
  12 + tearDown: function (done) {
  13 + s.verifyAndRestore();
  14 + done();
  15 + },
  16 +
  17 + "interprets --help with no args": function (test) {
  18 + var txt = require('fs').readFileSync(__dirname + "/../HELP", "utf-8");
  19 + s.mock(sys).expects("print").once().withExactArgs(txt);
  20 + s.stub(process, "exit");
  21 + cli.interpret(["node", "file.js"]);
  22 + test.done();
  23 + },
  24 +
  25 + "interprets --help": function (test) {
  26 + var txt = require('fs').readFileSync(__dirname + "/../HELP", "utf-8");
  27 + s.mock(sys).expects("print").once().withExactArgs(txt);
  28 + s.stub(process, "exit");
  29 + cli.interpret(["node", "file.js", "--help"]);
  30 + test.done();
  31 + }
  32 +
  33 +});

0 comments on commit 1c5ab20

Please sign in to comment.
Something went wrong with that request. Please try again.