2 * @fileoverview Main CLI object.
3 * @author Nicholas C. Zakas
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.
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
18 const fs
= require("fs"),
19 path
= require("path"),
20 { promisify
} = require("util"),
21 { ESLint
} = require("./eslint"),
22 CLIOptions
= require("./options"),
23 log
= require("./shared/logging"),
24 RuntimeInfo
= require("./shared/runtime-info");
26 const debug
= require("debug")("eslint:cli");
28 //------------------------------------------------------------------------------
30 //------------------------------------------------------------------------------
32 /** @typedef {import("./eslint/eslint").ESLintOptions} ESLintOptions */
33 /** @typedef {import("./eslint/eslint").LintMessage} LintMessage */
34 /** @typedef {import("./eslint/eslint").LintResult} LintResult */
35 /** @typedef {import("./options").ParsedCLIOptions} ParsedCLIOptions */
37 //------------------------------------------------------------------------------
39 //------------------------------------------------------------------------------
41 const mkdir = promisify(fs.mkdir);
42 const stat = promisify(fs.stat);
43 const writeFile = promisify(fs.writeFile);
46 * Predicate function for whether or not to apply fixes in quiet mode.
47 * If a message is a warning, do not apply a fix.
48 * @param {LintMessage} message The lint result.
49 * @returns {boolean} True if the lint message is an error (and thus should be
50 * autofixed), false otherwise.
52 function quietFixPredicate(message
) {
53 return message
.severity
=== 2;
57 * Translates the CLI options into the options expected by the CLIEngine.
58 * @param {ParsedCLIOptions} cliOptions The CLI options to translate.
59 * @returns {ESLintOptions} The options object for the CLIEngine.
62 function translateOptions({
69 errorOnUnmatchedPattern
,
84 reportUnusedDisableDirectives
,
85 resolvePluginsRelativeTo
,
90 allowInlineConfig
: inlineConfig
,
92 cacheLocation
: cacheLocation
|| cacheFile
,
94 errorOnUnmatchedPattern
,
96 fix
: (fix
|| fixDryRun
) && (quiet
? quietFixPredicate
: true),
101 env
: env
&& env
.reduce((obj
, name
) => {
105 globals
: global
&& global
.reduce((obj
, name
) => {
106 if (name
.endsWith(":true")) {
107 obj
[name
.slice(0, -5)] = "writable";
109 obj
[name
] = "readonly";
113 ignorePatterns
: ignorePattern
,
119 overrideConfigFile
: config
,
120 reportUnusedDisableDirectives
: reportUnusedDisableDirectives
? "error" : void 0,
121 resolvePluginsRelativeTo
,
123 useEslintrc
: eslintrc
128 * Count error messages.
129 * @param {LintResult[]} results The lint results.
130 * @returns {{errorCount:number;warningCount:number}} The number of error messages.
132 function countErrors(results
) {
134 let fatalErrorCount
= 0;
135 let warningCount
= 0;
137 for (const result
of results
) {
138 errorCount
+= result
.errorCount
;
139 fatalErrorCount
+= result
.fatalErrorCount
;
140 warningCount
+= result
.warningCount
;
143 return { errorCount
, fatalErrorCount
, warningCount
};
147 * Check if a given file path is a directory or not.
148 * @param {string} filePath The path to a file to check.
149 * @returns {Promise<boolean>} `true` if the given path is a directory.
151 async
function isDirectory(filePath
) {
153 return (await
stat(filePath
)).isDirectory();
155 if (error
.code
=== "ENOENT" || error
.code
=== "ENOTDIR") {
163 * Outputs the results of the linting.
164 * @param {ESLint} engine The ESLint instance to use.
165 * @param {LintResult[]} results The results to print.
166 * @param {string} format The name of the formatter to use or the path to the formatter.
167 * @param {string} outputFile The path for the output file.
168 * @returns {Promise<boolean>} True if the printing succeeds, false if not.
171 async
function printResults(engine
, results
, format
, outputFile
) {
175 formatter
= await engine
.loadFormatter(format
);
177 log
.error(e
.message
);
181 const output
= formatter
.format(results
);
185 const filePath
= path
.resolve(process
.cwd(), outputFile
);
187 if (await
isDirectory(filePath
)) {
188 log
.error("Cannot write to output file path, it is a directory: %s", outputFile
);
193 await
mkdir(path
.dirname(filePath
), { recursive
: true });
194 await
writeFile(filePath
, output
);
196 log
.error("There was a problem writing the output file:\n%s", ex
);
207 //------------------------------------------------------------------------------
209 //------------------------------------------------------------------------------
212 * Encapsulates all CLI behavior for eslint. Makes it easier to test as well as
213 * for other Node.js programs to effectively run the CLI.
218 * Executes the CLI based on an array of arguments that is passed in.
219 * @param {string|Array|Object} args The arguments to process.
220 * @param {string} [text] The text to lint (used for TTY).
221 * @returns {Promise<number>} The exit code for the operation.
223 async
execute(args
, text
) {
224 if (Array
.isArray(args
)) {
225 debug("CLI args: %o", args
.slice(2));
228 /** @type {ParsedCLIOptions} */
232 options
= CLIOptions
.parse(args
);
234 log
.error(error
.message
);
238 const files
= options
._
;
239 const useStdin
= typeof text
=== "string";
242 log
.info(CLIOptions
.generateHelp());
245 if (options
.version
) {
246 log
.info(RuntimeInfo
.version());
249 if (options
.envInfo
) {
251 log
.info(RuntimeInfo
.environment());
254 log
.error(err
.message
);
259 if (options
.printConfig
) {
261 log
.error("The --print-config option must be used with exactly one file name.");
265 log
.error("The --print-config option is not available for piped-in code.");
269 const engine
= new ESLint(translateOptions(options
));
271 await engine
.calculateConfigForFile(options
.printConfig
);
273 log
.info(JSON
.stringify(fileConfig
, null, " "));
277 debug(`Running on ${useStdin ? "text" : "files"}`);
279 if (options
.fix
&& options
.fixDryRun
) {
280 log
.error("The --fix option and the --fix-dry-run option cannot be used together.");
283 if (useStdin
&& options
.fix
) {
284 log
.error("The --fix option is not available for piped-in code; use --fix-dry-run instead.");
287 if (options
.fixType
&& !options
.fix
&& !options
.fixDryRun
) {
288 log
.error("The --fix-type option requires either --fix or --fix-dry-run.");
292 const engine
= new ESLint(translateOptions(options
));
296 results
= await engine
.lintText(text
, {
297 filePath
: options
.stdinFilename
,
301 results
= await engine
.lintFiles(files
);
305 debug("Fix mode enabled - applying fixes");
306 await ESLint
.outputFixes(results
);
309 let resultsToPrint
= results
;
312 debug("Quiet mode enabled - filtering out warnings");
313 resultsToPrint
= ESLint
.getErrorResults(resultsToPrint
);
316 if (await
printResults(engine
, resultsToPrint
, options
.format
, options
.outputFile
)) {
318 // Errors and warnings from the original unfiltered results should determine the exit code
319 const { errorCount
, fatalErrorCount
, warningCount
} = countErrors(results
);
321 const tooManyWarnings
=
322 options
.maxWarnings
>= 0 && warningCount
> options
.maxWarnings
;
323 const shouldExitForFatalErrors
=
324 options
.exitOnFatalError
&& fatalErrorCount
> 0;
326 if (!errorCount
&& tooManyWarnings
) {
328 "ESLint found too many warnings (maximum: %s).",
333 if (shouldExitForFatalErrors
) {
337 return (errorCount
|| tooManyWarnings
) ? 1 : 0;
344 module
.exports
= cli
;