81165836aa4d5f617815e05d08da0b3e1569299e.svn-base
4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
var fs = require('fs'),
path = require('path'),
cli = require('cli').enable('glob', 'help'),
hint = require('./hint');
function existsSync() {
var obj = fs.existsSync ? fs : path;
return obj.existsSync.apply(obj, arguments);
}
function _removeJsComments(str) {
str = str || '';
// replace everything between "/* */" in a non-greedy way
// The English version of the regex is:
// match '/*'
// then match 0 or more instances of any character (including newlines)
// except for instances of '*/'
// then match '*/'
str = str.replace(/\/\*(?:(?!\*\/)[\s\S])*\*\//g, '');
str = str.replace(/\/\/[^\n\r]*/g, ''); //everything after "//"
return str;
}
function _loadAndParseConfig(filePath) {
return filePath && existsSync(filePath) ?
JSON.parse(_removeJsComments(fs.readFileSync(filePath, "utf-8"))) : {};
}
/**
* This function searches for a file with a specified name, it starts
* with the dir passed, and traverses up the filesystem until it either
* finds the file, or hits the root
*
* @param {String} name Filename to search for (.jshintrc, .jshintignore)
* @param {String} dir Defaults to process.cwd()
*/
function _searchFile(name, dir) {
dir = dir || process.cwd();
var filename = path.normalize(path.join(dir, name)),
parent = path.resolve(dir, "..");
if (existsSync(filename)) {
return filename;
}
return dir === parent ? null : _searchFile(name, parent);
}
function _findConfig() {
var name = ".jshintrc",
projectConfig = _searchFile(name),
homeConfig = path.normalize(path.join(process.env.HOME, name));
if (projectConfig) {
return projectConfig;
}
// if no project config, check $HOME
if (existsSync(homeConfig)) {
return homeConfig;
}
return false;
}
function _print(results) {
function exit() {
process.exit(results.length > 0 ? 1 : 0);
}
// avoid stdout cutoff in node 0.4.x, also supports 0.5.x
// see https://github.com/joyent/node/issues/1669
try {
if (!process.stdout.flush()) {
process.stdout.once("drain", exit);
} else {
exit();
}
} catch (e) {
exit();
}
}
module.exports = {
interpret: function (args) {
var config, reporter, options,
customConfig, customReporter,
ignoreFile, ignores, extraExtensionList;
cli.setArgv(args);
cli.options = {};
options = cli.parse({
'version': ['v', 'display package version', 'boolean', false],
'config': ['config', 'custom config file', 'string', false],
'reporter': ['reporter', 'custom reporter', 'string', undefined],
'jslint-reporter': ['jslint-reporter', 'use a jslint compatible xml reporter'],
'checkstyle-reporter': ['checkstyle-reporter', 'use a CheckStyle compatible xml reporter'],
'show-non-errors': ['show-non-errors', 'show additional data generated by jshint'],
'extra-ext': ['extra-ext', 'comma-separated list of file extensions to use (.js is default)', 'string', '']
});
customConfig = options.config;
customReporter = options.reporter ? path.resolve(process.cwd(), options.reporter) : null;
extraExtensionList = options["extra-ext"];
if (options.version) {
cli.setApp(path.resolve(__dirname + "/../package.json"));
process.stdout.write(cli.version + "\n");
return;
}
if (options.help || !cli.args.length) {
cli.getUsage();
process.exit();
return;
}
if (options['jslint-reporter']) {
customReporter = "./reporters/jslint_xml.js";
}
if (options['checkstyle-reporter']) {
customReporter = "./reporters/checkstyle.js";
}
if (options['show-non-errors']) {
customReporter = "./reporters/non_error.js";
}
config = _loadAndParseConfig(customConfig ? customConfig : _findConfig());
if (customReporter) {
try {
reporter = require(customReporter).reporter;
} catch (r) {
process.stdout.write("Error opening reporter file: " + customReporter);
process.stdout.write(r + "\n");
process.exit(1);
}
}
ignoreFile = _searchFile(".jshintignore");
if (ignoreFile) {
ignores = fs.readFileSync(ignoreFile, "utf8").split("\n")
.filter(function (line) {
return !!line.trim();
})
.map(function (line) {
return path.resolve(path.dirname(ignoreFile), line.trim());
});
}
_print(hint.hint(cli.args, config, reporter, ignores, extraExtensionList));
}
};