13aec281adc64eec15680face589e99a79b9f940
[framework/web/web-ui-fw.git] / build-tools / bin / jslint
1 #!/usr/bin/env node
2 /* original
3 var linter = require("../lib/linter");
4 var reporter = require("../lib/reporter");
5 var nopt = require("nopt");
6 var fs = require("fs");
7 */
8 var linter = require("../lib/jslint/linter"),
9         reporter = require("../lib/jslint/reporter"),
10         nopt = require("../lib/jslint/nopt/nopt"),
11         fs = require("fs");
12
13 function commandOptions() {
14     'use strict';
15     var flags = [
16             'anon', 'bitwise', 'browser', 'cap', 'continue', 'css',
17             'debug', 'devel', 'eqeq', 'es5', 'evil', 'forin', 'fragment',
18             'newcap', 'node', 'nomen', 'on', 'passfail', 'plusplus',
19             'properties', 'regexp', 'rhino', 'undef', 'unparam',
20             'sloppy', 'sub', 'vars', 'white', 'widget', 'windows',
21             'json', 'color', 'jqmspace'
22         ],
23         commandOpts = {
24             'indent' : Number,
25             'maxerr' : Number,
26             'maxlen' : Number,
27             'predef' : [String, Array]
28         };
29
30     flags.forEach(function (option) {
31         commandOpts[option] = Boolean;
32     });
33
34     return commandOpts;
35 }
36
37 var options = commandOptions();
38
39 var parsed = nopt(options);
40
41 function die(why) {
42     'use strict';
43     console.warn(why);
44     console.warn("Usage: " + process.argv[1] +
45         " [--" + Object.keys(options).join("] [--") +
46         "] [--] <scriptfile>...");
47     process.exit(1);
48 }
49
50 if (!parsed.argv.remain.length) {
51     die("No files specified.");
52 }
53
54
55 // If there are no more files to be processed, exit with the value 1
56 // if any of the files contains any lint.
57 var maybeExit = (function () {
58     'use strict';
59     var filesLeft = parsed.argv.remain.length,
60         ok = true;
61
62     return function (lint) {
63         filesLeft -= 1;
64         ok = lint.ok && ok;
65
66         if (filesLeft === 0) {
67             // This was the last file.
68             process.exit(ok ? 0 : 1);
69         }
70     };
71 }());
72
73
74 function lintFile(file) {
75     'use strict';
76     fs.readFile(file, function (err, data) {
77         if (err) {
78             throw err;
79         }
80
81         // Fix UTF8 with BOM
82         if (0xEF === data[0] && 0xBB === data[1] && 0xBF === data[2]) {
83             data = data.slice(3);
84         }
85
86         data = data.toString("utf8");
87         var lint = linter.lint(data, parsed);
88
89         if (parsed.json) {
90             console.log(JSON.stringify([file, lint.errors]));
91         } else {
92             reporter.report(file, lint, parsed.color);
93         }
94
95         maybeExit(lint);
96     });
97 }
98
99 parsed.argv.remain.forEach(lintFile);