UNPKG

5.83 kBJavaScriptView Raw
1/**
2 * @fileoverview Main CLI object.
3 * @author Nicholas C. Zakas
4 */
5
6"use strict";
7
8/*
9 * The CLI object should *not* call process.exit() directly. It should only return
10 * exit codes. This allows other programs to use the CLI object and still control
11 * when the program exits.
12 */
13
14//------------------------------------------------------------------------------
15// Requirements
16//------------------------------------------------------------------------------
17
18var fs = require("fs"),
19 path = require("path"),
20
21 debug = require("debug"),
22
23 options = require("./options"),
24 CLIEngine = require("./cli-engine"),
25 mkdirp = require("mkdirp");
26
27//------------------------------------------------------------------------------
28// Helpers
29//------------------------------------------------------------------------------
30
31debug = debug("eslint:cli");
32
33/**
34 * Translates the CLI options into the options expected by the CLIEngine.
35 * @param {Object} cliOptions The CLI options to translate.
36 * @returns {CLIEngineOptions} The options object for the CLIEngine.
37 * @private
38 */
39function translateOptions(cliOptions) {
40 return {
41 envs: cliOptions.env,
42 extensions: cliOptions.ext,
43 rules: cliOptions.rule,
44 plugins: cliOptions.plugin,
45 globals: cliOptions.global,
46 ignore: cliOptions.ignore,
47 ignorePath: cliOptions.ignorePath,
48 ignorePattern: cliOptions.ignorePattern,
49 configFile: cliOptions.config,
50 rulePaths: cliOptions.rulesdir,
51 useEslintrc: cliOptions.eslintrc,
52 parser: cliOptions.parser,
53 cache: cliOptions.cache,
54 cacheFile: cliOptions.cacheFile,
55 fix: cliOptions.fix
56 };
57}
58
59/**
60 * Outputs the results of the linting.
61 * @param {CLIEngine} engine The CLIEngine to use.
62 * @param {LintResult[]} results The results to print.
63 * @param {string} format The name of the formatter to use or the path to the formatter.
64 * @param {string} outputFile The path for the output file.
65 * @returns {boolean} True if the printing succeeds, false if not.
66 * @private
67 */
68function printResults(engine, results, format, outputFile) {
69 var formatter,
70 output,
71 filePath;
72
73 formatter = engine.getFormatter(format);
74 if (!formatter) {
75 console.error("Could not find formatter '%s'.", format);
76 return false;
77 }
78
79 output = formatter(results);
80
81 if (output) {
82 if (outputFile) {
83 filePath = path.resolve(process.cwd(), outputFile);
84
85 if (fs.existsSync(filePath) && fs.statSync(filePath).isDirectory()) {
86 console.error("Cannot write to output file path, it is a directory: %s", outputFile);
87 return false;
88 }
89
90 try {
91 mkdirp.sync(path.dirname(filePath));
92 fs.writeFileSync(filePath, output);
93 } catch (ex) {
94 console.error("There was a problem writing the output file:\n%s", ex);
95 return false;
96 }
97 } else {
98 console.log(output);
99 }
100 }
101
102 return true;
103
104}
105
106//------------------------------------------------------------------------------
107// Public Interface
108//------------------------------------------------------------------------------
109
110/**
111 * Encapsulates all CLI behavior for eslint. Makes it easier to test as well as
112 * for other Node.js programs to effectively run the CLI.
113 */
114var cli = {
115
116 /**
117 * Executes the CLI based on an array of arguments that is passed in.
118 * @param {string|Array|Object} args The arguments to process.
119 * @param {string} [text] The text to lint (used for TTY).
120 * @returns {int} The exit code for the operation.
121 */
122 execute: function(args, text) {
123
124 var currentOptions,
125 files,
126 report,
127 engine,
128 tooManyWarnings;
129
130 try {
131 currentOptions = options.parse(args);
132 } catch (error) {
133 console.error(error.message);
134 return 1;
135 }
136
137 files = currentOptions._;
138
139 if (currentOptions.version) { // version from package.json
140
141 console.log("v" + require("../package.json").version);
142
143 } else if (currentOptions.help || (!files.length && !text)) {
144
145 console.log(options.generateHelp());
146
147 } else {
148
149 debug("Running on " + (text ? "text" : "files"));
150
151 // disable --fix for piped-in code until we know how to do it correctly
152 if (text && currentOptions.fix) {
153 console.error("The --fix option is not available for piped-in code.");
154 return 1;
155 }
156
157 engine = new CLIEngine(translateOptions(currentOptions));
158
159 report = text ? engine.executeOnText(text, currentOptions.stdinFilename) : engine.executeOnFiles(files);
160 if (currentOptions.fix) {
161 debug("Fix mode enabled - applying fixes");
162 CLIEngine.outputFixes(report);
163 }
164
165 if (currentOptions.quiet) {
166 debug("Quiet mode enabled - filtering out warnings");
167 report.results = CLIEngine.getErrorResults(report.results);
168 }
169
170 if (printResults(engine, report.results, currentOptions.format, currentOptions.outputFile)) {
171 tooManyWarnings = currentOptions.maxWarnings >= 0 && report.warningCount > currentOptions.maxWarnings;
172
173 if (!report.errorCount && tooManyWarnings) {
174 console.error("ESLint found too many warnings (maximum: %s).", currentOptions.maxWarnings);
175 }
176
177 return (report.errorCount || tooManyWarnings) ? 1 : 0;
178 } else {
179 return 1;
180 }
181
182 }
183
184 return 0;
185 }
186};
187
188module.exports = cli;