xonecas.io
1510
Cn

cli-node-jshint / files

cli-node-jshint / cli-jshint

1.6 KB
1#! /usr/bin/env node
2/*jshint node:true, curly:true, onevar:true, maxlen:80 */
3
4var jshint  = require('./jshint/jshint.js').JSHINT,
5   fs       = require('fs'),
6   path     = require('path'),
7   args     = process.argv.slice(2),
8   paths    = [],
9   options  = {},
10   files    = {},
11   hasProp  = Object.prototype.hasOwnProperty,
12   keyValuePair;
13
14args.forEach(function (arg) {
15   if (arg.indexOf(':') === -1) {
16      /* path to js file */
17      paths.push(path.resolve(arg));
18   } else {
19      /* jshint option */
20      keyValuePair = arg.split(':');
21      options[keyValuePair[0]] = keyValuePair[1];
22   }
23});
24
25function reportErrors (file, errors) {
26   var str = "";
27
28   errors.forEach(function (err) {
29      /* Same error reporting syntax as @brentlintner's
30         so that it works with 3rd party plugins (vim/emacs?) */
31      str += file + ': line '+ err.line + ', col ' + err.character +
32         ', ' + err.reason + '\n';
33   });
34
35   console.log(str +"\n"+ errors.length + ' error(s)');
36}
37
38function parseFiles() {
39   var prop;
40
41   for (prop in files) {
42      if (hasProp.call(files, prop)) {
43         if (jshint(files[prop], options)) {
44            console.log("Hooray, no lint!");
45            process.exit(0);
46         } else {
47            reportErrors(prop, jshint.errors);
48            process.exit(1);
49         }
50      }
51   }
52}
53
54(function readAll () {
55   var filepath;
56
57   if (paths.length > 0) {
58      filepath = paths.shift();
59      fs.readFile(filepath, 'utf8', function (err, out) {
60         if (err) {
61            throw err;
62         }
63
64         files[filepath] = out;
65   
66         readAll();
67      });
68   } else {
69      parseFiles();
70   }
71}) ();