2 * @fileoverview Main CLI object.
3 * @author Nicholas C. Zakas
9 * NOTE: 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 { FlatESLint
, shouldUseFlatConfig
} = require("./eslint/flat-eslint"),
23 createCLIOptions
= require("./options"),
24 log
= require("./shared/logging"),
25 RuntimeInfo
= require("./shared/runtime-info");
26 const { Legacy
: { naming
} } = require("@eslint/eslintrc");
27 const { ModuleImporter
} = require("@humanwhocodes/module-importer");
29 const debug
= require("debug")("eslint:cli");
31 //------------------------------------------------------------------------------
33 //------------------------------------------------------------------------------
35 /** @typedef {import("./eslint/eslint").ESLintOptions} ESLintOptions */
36 /** @typedef {import("./eslint/eslint").LintMessage} LintMessage */
37 /** @typedef {import("./eslint/eslint").LintResult} LintResult */
38 /** @typedef {import("./options").ParsedCLIOptions} ParsedCLIOptions */
39 /** @typedef {import("./shared/types").ResultsMeta} ResultsMeta */
41 //------------------------------------------------------------------------------
43 //------------------------------------------------------------------------------
45 const mkdir
= promisify(fs
.mkdir
);
46 const stat
= promisify(fs
.stat
);
47 const writeFile
= promisify(fs
.writeFile
);
50 * Predicate function for whether or not to apply fixes in quiet mode.
51 * If a message is a warning, do not apply a fix.
52 * @param {LintMessage} message The lint result.
53 * @returns {boolean} True if the lint message is an error (and thus should be
54 * autofixed), false otherwise.
56 function quietFixPredicate(message
) {
57 return message
.severity
=== 2;
61 * Translates the CLI options into the options expected by the ESLint constructor.
62 * @param {ParsedCLIOptions} cliOptions The CLI options to translate.
63 * @param {"flat"|"eslintrc"} [configType="eslintrc"] The format of the
65 * @returns {Promise<ESLintOptions>} The options object for the ESLint constructor.
68 async
function translateOptions({
76 errorOnUnmatchedPattern
,
91 reportUnusedDisableDirectives
,
92 resolvePluginsRelativeTo
,
97 let overrideConfig
, overrideConfigFile
;
98 const importer
= new ModuleImporter();
100 if (configType
=== "flat") {
101 overrideConfigFile
= (typeof config
=== "string") ? config
: !configLookup
;
102 if (overrideConfigFile
=== false) {
103 overrideConfigFile
= void 0;
109 globals
= global
.reduce((obj
, name
) => {
110 if (name
.endsWith(":true")) {
111 obj
[name
.slice(0, -5)] = "writable";
113 obj
[name
] = "readonly";
122 parserOptions
: parserOptions
|| {}
124 rules
: rule
? rule
: {}
128 overrideConfig
[0].languageOptions
.parser
= await importer
.import(parser
);
134 for (const pluginName
of plugin
) {
136 const shortName
= naming
.getShorthandName(pluginName
, "eslint-plugin");
137 const longName
= naming
.normalizePackageName(pluginName
, "eslint-plugin");
139 plugins
[shortName
] = await importer
.import(longName
);
142 overrideConfig
[0].plugins
= plugins
;
146 overrideConfigFile
= config
;
149 env
: env
&& env
.reduce((obj
, name
) => {
153 globals
: global
&& global
.reduce((obj
, name
) => {
154 if (name
.endsWith(":true")) {
155 obj
[name
.slice(0, -5)] = "writable";
157 obj
[name
] = "readonly";
161 ignorePatterns
: ignorePattern
,
170 allowInlineConfig
: inlineConfig
,
172 cacheLocation
: cacheLocation
|| cacheFile
,
174 errorOnUnmatchedPattern
,
175 fix
: (fix
|| fixDryRun
) && (quiet
? quietFixPredicate
: true),
180 reportUnusedDisableDirectives
: reportUnusedDisableDirectives
? "error" : void 0
183 if (configType
=== "flat") {
184 options
.ignorePatterns
= ignorePattern
;
186 options
.resolvePluginsRelativeTo
= resolvePluginsRelativeTo
;
187 options
.rulePaths
= rulesdir
;
188 options
.useEslintrc
= eslintrc
;
189 options
.extensions
= ext
;
190 options
.ignorePath
= ignorePath
;
197 * Count error messages.
198 * @param {LintResult[]} results The lint results.
199 * @returns {{errorCount:number;fatalErrorCount:number,warningCount:number}} The number of error messages.
201 function countErrors(results
) {
203 let fatalErrorCount
= 0;
204 let warningCount
= 0;
206 for (const result
of results
) {
207 errorCount
+= result
.errorCount
;
208 fatalErrorCount
+= result
.fatalErrorCount
;
209 warningCount
+= result
.warningCount
;
212 return { errorCount
, fatalErrorCount
, warningCount
};
216 * Check if a given file path is a directory or not.
217 * @param {string} filePath The path to a file to check.
218 * @returns {Promise<boolean>} `true` if the given path is a directory.
220 async
function isDirectory(filePath
) {
222 return (await
stat(filePath
)).isDirectory();
224 if (error
.code
=== "ENOENT" || error
.code
=== "ENOTDIR") {
232 * Outputs the results of the linting.
233 * @param {ESLint} engine The ESLint instance to use.
234 * @param {LintResult[]} results The results to print.
235 * @param {string} format The name of the formatter to use or the path to the formatter.
236 * @param {string} outputFile The path for the output file.
237 * @param {ResultsMeta} resultsMeta Warning count and max threshold.
238 * @returns {Promise<boolean>} True if the printing succeeds, false if not.
241 async
function printResults(engine
, results
, format
, outputFile
, resultsMeta
) {
245 formatter
= await engine
.loadFormatter(format
);
247 log
.error(e
.message
);
251 const output
= await formatter
.format(results
, resultsMeta
);
255 const filePath
= path
.resolve(process
.cwd(), outputFile
);
257 if (await
isDirectory(filePath
)) {
258 log
.error("Cannot write to output file path, it is a directory: %s", outputFile
);
263 await
mkdir(path
.dirname(filePath
), { recursive
: true });
264 await
writeFile(filePath
, output
);
266 log
.error("There was a problem writing the output file:\n%s", ex
);
277 //------------------------------------------------------------------------------
279 //------------------------------------------------------------------------------
282 * Encapsulates all CLI behavior for eslint. Makes it easier to test as well as
283 * for other Node.js programs to effectively run the CLI.
288 * Executes the CLI based on an array of arguments that is passed in.
289 * @param {string|Array|Object} args The arguments to process.
290 * @param {string} [text] The text to lint (used for TTY).
291 * @param {boolean} [allowFlatConfig] Whether or not to allow flat config.
292 * @returns {Promise<number>} The exit code for the operation.
294 async
execute(args
, text
, allowFlatConfig
) {
295 if (Array
.isArray(args
)) {
296 debug("CLI args: %o", args
.slice(2));
300 * Before doing anything, we need to see if we are using a
301 * flat config file. If so, then we need to change the way command
302 * line args are parsed. This is temporary, and when we fully
303 * switch to flat config we can remove this logic.
306 const usingFlatConfig
= allowFlatConfig
&& await
shouldUseFlatConfig();
308 debug("Using flat config?", usingFlatConfig
);
310 const CLIOptions
= createCLIOptions(usingFlatConfig
);
312 /** @type {ParsedCLIOptions} */
316 options
= CLIOptions
.parse(args
);
318 debug("Error parsing CLI options:", error
.message
);
319 log
.error(error
.message
);
323 const files
= options
._
;
324 const useStdin
= typeof text
=== "string";
327 log
.info(CLIOptions
.generateHelp());
330 if (options
.version
) {
331 log
.info(RuntimeInfo
.version());
334 if (options
.envInfo
) {
336 log
.info(RuntimeInfo
.environment());
339 debug("Error retrieving environment info");
340 log
.error(err
.message
);
345 if (options
.printConfig
) {
347 log
.error("The --print-config option must be used with exactly one file name.");
351 log
.error("The --print-config option is not available for piped-in code.");
355 const engine
= usingFlatConfig
356 ? new FlatESLint(await
translateOptions(options
, "flat"))
357 : new ESLint(await
translateOptions(options
));
359 await engine
.calculateConfigForFile(options
.printConfig
);
361 log
.info(JSON
.stringify(fileConfig
, null, " "));
365 debug(`Running on ${useStdin ? "text" : "files"}`);
367 if (options
.fix
&& options
.fixDryRun
) {
368 log
.error("The --fix option and the --fix-dry-run option cannot be used together.");
371 if (useStdin
&& options
.fix
) {
372 log
.error("The --fix option is not available for piped-in code; use --fix-dry-run instead.");
375 if (options
.fixType
&& !options
.fix
&& !options
.fixDryRun
) {
376 log
.error("The --fix-type option requires either --fix or --fix-dry-run.");
380 const ActiveESLint
= usingFlatConfig
? FlatESLint
: ESLint
;
382 const engine
= new ActiveESLint(await
translateOptions(options
, usingFlatConfig
? "flat" : "eslintrc"));
386 results
= await engine
.lintText(text
, {
387 filePath
: options
.stdinFilename
,
391 results
= await engine
.lintFiles(files
);
395 debug("Fix mode enabled - applying fixes");
396 await ActiveESLint
.outputFixes(results
);
399 let resultsToPrint
= results
;
402 debug("Quiet mode enabled - filtering out warnings");
403 resultsToPrint
= ActiveESLint
.getErrorResults(resultsToPrint
);
406 const resultCounts
= countErrors(results
);
407 const tooManyWarnings
= options
.maxWarnings
>= 0 && resultCounts
.warningCount
> options
.maxWarnings
;
408 const resultsMeta
= tooManyWarnings
410 maxWarningsExceeded
: {
411 maxWarnings
: options
.maxWarnings
,
412 foundWarnings
: resultCounts
.warningCount
417 if (await
printResults(engine
, resultsToPrint
, options
.format
, options
.outputFile
, resultsMeta
)) {
419 // Errors and warnings from the original unfiltered results should determine the exit code
420 const shouldExitForFatalErrors
=
421 options
.exitOnFatalError
&& resultCounts
.fatalErrorCount
> 0;
423 if (!resultCounts
.errorCount
&& tooManyWarnings
) {
425 "ESLint found too many warnings (maximum: %s).",
430 if (shouldExitForFatalErrors
) {
434 return (resultCounts
.errorCount
|| tooManyWarnings
) ? 1 : 0;
441 module
.exports
= cli
;