Repository navigation
Expand file tree
/
Copy pathcli.js
More file actions
195 lines (156 loc) · 5.45 KB
/
Copy pathcli.js
File metadata and controls
195 lines (156 loc) · 5.45 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* @fileoverview Main CLI object.
* @author Nicholas C. Zakas
*/
"use strict";
/*
* The CLI object should *not* call process.exit() directly. It should only return
* exit codes. This allows other programs to use the CLI object and still control
* when the program exits.
*/
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
var fs = require("fs"),
path = require("path"),
debug = require("debug"),
options = require("./options"),
CLIEngine = require("./cli-engine"),
mkdirp = require("mkdirp");
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
debug = debug("eslint:cli");
/**
* Translates the CLI options into the options expected by the CLIEngine.
* @param {Object} cliOptions The CLI options to translate.
* @returns {CLIEngineOptions} The options object for the CLIEngine.
* @private
*/
function translateOptions(cliOptions) {
return {
envs: cliOptions.env,
extensions: cliOptions.ext,
rules: cliOptions.rule,
plugins: cliOptions.plugin,
globals: cliOptions.global,
ignore: cliOptions.ignore,
ignorePath: cliOptions.ignorePath,
configFile: cliOptions.config,
rulePaths: cliOptions.rulesdir,
reset: cliOptions.reset,
useEslintrc: cliOptions.eslintrc
};
}
/**
* Outputs the results of the linting.
* @param {CLIEngine} engine The CLIEngine to use.
* @param {LintResult[]} results The results to print.
* @param {string} format The name of the formatter to use or the path to the formatter.
* @param {string} outputFile The path for the output file.
* @returns {boolean} True if the printing succeeds, false if not.
* @private
*/
function printResults(engine, results, format, outputFile) {
var formatter,
output,
filePath;
formatter = engine.getFormatter(format);
if (!formatter) {
console.error("Could not find formatter '%s'.", format);
return false;
}
output = formatter(results);
if (output) {
if (outputFile) {
filePath = path.resolve(process.cwd(), outputFile);
if (fs.existsSync(filePath) && fs.statSync(filePath).isDirectory()) {
console.error("Cannot write to output file path, it is a directory: %s", outputFile);
return false;
}
try {
mkdirp.sync(path.dirname(filePath));
fs.writeFileSync(filePath, output);
} catch (ex) {
console.error("There was a problem writing the output file:\n%s", ex);
return false;
}
} else {
console.log(output);
}
}
return true;
}
/**
* Checks if the given message is an error message.
* @param {object} message The message to check.
* @returns {boolean} Whether or not the message is an error message.
*/
function isErrorMessage(message) {
return message.severity === 2;
}
/**
* Returns results that only contains errors.
* @param {LintResult[]} results The results to filter.
* @returns {LintResult[]} The filtered results.
*/
function getErrorResults(results) {
var filtered = [];
results.forEach(function (result) {
var filteredMessages = result.messages.filter(isErrorMessage);
if (filteredMessages.length > 0) {
filtered.push({
filePath: result.filePath,
messages: filteredMessages
});
}
});
return filtered;
}
//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------
/**
* Encapsulates all CLI behavior for eslint. Makes it easier to test as well as
* for other Node.js programs to effectively run the CLI.
*/
var cli = {
/**
* Executes the CLI based on an array of arguments that is passed in.
* @param {string|Array|Object} args The arguments to process.
* @param {string} [text] The text to lint (used for TTY).
* @returns {int} The exit code for the operation.
*/
execute: function(args, text) {
var currentOptions,
files,
result,
engine;
try {
currentOptions = options.parse(args);
} catch (error) {
console.error(error.message);
return 1;
}
files = currentOptions._;
if (currentOptions.version) { // version from package.json
console.log("v" + require("../package.json").version);
} else if (currentOptions.help || (!files.length && !text)) {
console.log(options.generateHelp());
} else {
engine = new CLIEngine(translateOptions(currentOptions));
debug("Running on " + (text ? "text" : "files"));
result = text ? engine.executeOnText(text) : engine.executeOnFiles(files);
if (currentOptions.quiet) {
result.results = getErrorResults(result.results);
}
if (printResults(engine, result.results, currentOptions.format, currentOptions.outputFile)) {
return result.errorCount ? 1 : 0;
} else {
return 1;
}
}
return 0;
}
};
module.exports = cli;