UNPKG

6.45 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
18const fs = require("fs"),
19 path = require("path"),
20 options = require("./options"),
21 CLIEngine = require("./cli-engine"),
22 mkdirp = require("mkdirp"),
23 log = require("./logging");
24
25const debug = require("debug")("eslint:cli");
26
27//------------------------------------------------------------------------------
28// Helpers
29//------------------------------------------------------------------------------
30
31/**
32 * Translates the CLI options into the options expected by the CLIEngine.
33 * @param {Object} cliOptions The CLI options to translate.
34 * @returns {CLIEngineOptions} The options object for the CLIEngine.
35 * @private
36 */
37function translateOptions(cliOptions) {
38 return {
39 envs: cliOptions.env,
40 extensions: cliOptions.ext,
41 rules: cliOptions.rule,
42 plugins: cliOptions.plugin,
43 globals: cliOptions.global,
44 ignore: cliOptions.ignore,
45 ignorePath: cliOptions.ignorePath,
46 ignorePattern: cliOptions.ignorePattern,
47 configFile: cliOptions.config,
48 rulePaths: cliOptions.rulesdir,
49 useEslintrc: cliOptions.eslintrc,
50 parser: cliOptions.parser,
51 parserOptions: cliOptions.parserOptions,
52 cache: cliOptions.cache,
53 cacheFile: cliOptions.cacheFile,
54 cacheLocation: cliOptions.cacheLocation,
55 fix: cliOptions.fix,
56 allowInlineConfig: cliOptions.inlineConfig
57 };
58}
59
60/**
61 * Outputs the results of the linting.
62 * @param {CLIEngine} engine The CLIEngine to use.
63 * @param {LintResult[]} results The results to print.
64 * @param {string} format The name of the formatter to use or the path to the formatter.
65 * @param {string} outputFile The path for the output file.
66 * @returns {boolean} True if the printing succeeds, false if not.
67 * @private
68 */
69function printResults(engine, results, format, outputFile) {
70 let formatter;
71
72 try {
73 formatter = engine.getFormatter(format);
74 } catch (e) {
75 log.error(e.message);
76 return false;
77 }
78
79 const output = formatter(results);
80
81 if (output) {
82 if (outputFile) {
83 const filePath = path.resolve(process.cwd(), outputFile);
84
85 if (fs.existsSync(filePath) && fs.statSync(filePath).isDirectory()) {
86 log.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 log.error("There was a problem writing the output file:\n%s", ex);
95 return false;
96 }
97 } else {
98 log.info(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 */
114const 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(args, text) {
123
124 let currentOptions;
125
126 try {
127 currentOptions = options.parse(args);
128 } catch (error) {
129 log.error(error.message);
130 return 1;
131 }
132
133 const files = currentOptions._;
134
135 if (currentOptions.version) { // version from package.json
136
137 log.info(`v${require("../package.json").version}`);
138
139 } else if (currentOptions.printConfig) {
140 if (files.length) {
141 log.error("The --print-config option must be used with exactly one file name.");
142 return 1;
143 } else if (text) {
144 log.error("The --print-config option is not available for piped-in code.");
145 return 1;
146 }
147
148 const engine = new CLIEngine(translateOptions(currentOptions));
149
150 const fileConfig = engine.getConfigForFile(currentOptions.printConfig);
151
152 log.info(JSON.stringify(fileConfig, null, " "));
153 return 0;
154 } else if (currentOptions.help || (!files.length && !text)) {
155
156 log.info(options.generateHelp());
157
158 } else {
159
160 debug(`Running on ${text ? "text" : "files"}`);
161
162 // disable --fix for piped-in code until we know how to do it correctly
163 if (text && currentOptions.fix) {
164 log.error("The --fix option is not available for piped-in code.");
165 return 1;
166 }
167
168 const engine = new CLIEngine(translateOptions(currentOptions));
169
170 const report = text ? engine.executeOnText(text, currentOptions.stdinFilename, true) : engine.executeOnFiles(files);
171
172 if (currentOptions.fix) {
173 debug("Fix mode enabled - applying fixes");
174 CLIEngine.outputFixes(report);
175 }
176
177 if (currentOptions.quiet) {
178 debug("Quiet mode enabled - filtering out warnings");
179 report.results = CLIEngine.getErrorResults(report.results);
180 }
181
182 if (printResults(engine, report.results, currentOptions.format, currentOptions.outputFile)) {
183 const tooManyWarnings = currentOptions.maxWarnings >= 0 && report.warningCount > currentOptions.maxWarnings;
184
185 if (!report.errorCount && tooManyWarnings) {
186 log.error("ESLint found too many warnings (maximum: %s).", currentOptions.maxWarnings);
187 }
188
189 return (report.errorCount || tooManyWarnings) ? 1 : 0;
190 }
191 return 1;
192
193
194 }
195
196 return 0;
197 }
198};
199
200module.exports = cli;