Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame^] | 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 | |
| 18 | const fs = require("fs"), |
| 19 | path = require("path"), |
| 20 | mkdirp = require("mkdirp"), |
| 21 | { CLIEngine } = require("./cli-engine"), |
| 22 | options = require("./options"), |
| 23 | log = require("./shared/logging"); |
| 24 | |
| 25 | const debug = require("debug")("eslint:cli"); |
| 26 | |
| 27 | //------------------------------------------------------------------------------ |
| 28 | // Helpers |
| 29 | //------------------------------------------------------------------------------ |
| 30 | |
| 31 | /** |
| 32 | * Predicate function for whether or not to apply fixes in quiet mode. |
| 33 | * If a message is a warning, do not apply a fix. |
| 34 | * @param {LintResult} lintResult The lint result. |
| 35 | * @returns {boolean} True if the lint message is an error (and thus should be |
| 36 | * autofixed), false otherwise. |
| 37 | */ |
| 38 | function quietFixPredicate(lintResult) { |
| 39 | return lintResult.severity === 2; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Translates the CLI options into the options expected by the CLIEngine. |
| 44 | * @param {Object} cliOptions The CLI options to translate. |
| 45 | * @returns {CLIEngineOptions} The options object for the CLIEngine. |
| 46 | * @private |
| 47 | */ |
| 48 | function translateOptions(cliOptions) { |
| 49 | return { |
| 50 | envs: cliOptions.env, |
| 51 | extensions: cliOptions.ext, |
| 52 | rules: cliOptions.rule, |
| 53 | plugins: cliOptions.plugin, |
| 54 | globals: cliOptions.global, |
| 55 | ignore: cliOptions.ignore, |
| 56 | ignorePath: cliOptions.ignorePath, |
| 57 | ignorePattern: cliOptions.ignorePattern, |
| 58 | configFile: cliOptions.config, |
| 59 | rulePaths: cliOptions.rulesdir, |
| 60 | useEslintrc: cliOptions.eslintrc, |
| 61 | parser: cliOptions.parser, |
| 62 | parserOptions: cliOptions.parserOptions, |
| 63 | cache: cliOptions.cache, |
| 64 | cacheFile: cliOptions.cacheFile, |
| 65 | cacheLocation: cliOptions.cacheLocation, |
| 66 | fix: (cliOptions.fix || cliOptions.fixDryRun) && (cliOptions.quiet ? quietFixPredicate : true), |
| 67 | fixTypes: cliOptions.fixType, |
| 68 | allowInlineConfig: cliOptions.inlineConfig, |
| 69 | reportUnusedDisableDirectives: cliOptions.reportUnusedDisableDirectives, |
| 70 | resolvePluginsRelativeTo: cliOptions.resolvePluginsRelativeTo |
| 71 | }; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Outputs the results of the linting. |
| 76 | * @param {CLIEngine} engine The CLIEngine to use. |
| 77 | * @param {LintResult[]} results The results to print. |
| 78 | * @param {string} format The name of the formatter to use or the path to the formatter. |
| 79 | * @param {string} outputFile The path for the output file. |
| 80 | * @returns {boolean} True if the printing succeeds, false if not. |
| 81 | * @private |
| 82 | */ |
| 83 | function printResults(engine, results, format, outputFile) { |
| 84 | let formatter; |
| 85 | let rulesMeta; |
| 86 | |
| 87 | try { |
| 88 | formatter = engine.getFormatter(format); |
| 89 | } catch (e) { |
| 90 | log.error(e.message); |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | const output = formatter(results, { |
| 95 | get rulesMeta() { |
| 96 | if (!rulesMeta) { |
| 97 | rulesMeta = {}; |
| 98 | for (const [ruleId, rule] of engine.getRules()) { |
| 99 | rulesMeta[ruleId] = rule.meta; |
| 100 | } |
| 101 | } |
| 102 | return rulesMeta; |
| 103 | } |
| 104 | }); |
| 105 | |
| 106 | if (output) { |
| 107 | if (outputFile) { |
| 108 | const filePath = path.resolve(process.cwd(), outputFile); |
| 109 | |
| 110 | if (fs.existsSync(filePath) && fs.statSync(filePath).isDirectory()) { |
| 111 | log.error("Cannot write to output file path, it is a directory: %s", outputFile); |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | try { |
| 116 | mkdirp.sync(path.dirname(filePath)); |
| 117 | fs.writeFileSync(filePath, output); |
| 118 | } catch (ex) { |
| 119 | log.error("There was a problem writing the output file:\n%s", ex); |
| 120 | return false; |
| 121 | } |
| 122 | } else { |
| 123 | log.info(output); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | return true; |
| 128 | |
| 129 | } |
| 130 | |
| 131 | //------------------------------------------------------------------------------ |
| 132 | // Public Interface |
| 133 | //------------------------------------------------------------------------------ |
| 134 | |
| 135 | /** |
| 136 | * Encapsulates all CLI behavior for eslint. Makes it easier to test as well as |
| 137 | * for other Node.js programs to effectively run the CLI. |
| 138 | */ |
| 139 | const cli = { |
| 140 | |
| 141 | /** |
| 142 | * Executes the CLI based on an array of arguments that is passed in. |
| 143 | * @param {string|Array|Object} args The arguments to process. |
| 144 | * @param {string} [text] The text to lint (used for TTY). |
| 145 | * @returns {int} The exit code for the operation. |
| 146 | */ |
| 147 | execute(args, text) { |
| 148 | if (Array.isArray(args)) { |
| 149 | debug("CLI args: %o", args.slice(2)); |
| 150 | } |
| 151 | |
| 152 | let currentOptions; |
| 153 | |
| 154 | try { |
| 155 | currentOptions = options.parse(args); |
| 156 | } catch (error) { |
| 157 | log.error(error.message); |
| 158 | return 2; |
| 159 | } |
| 160 | |
| 161 | const files = currentOptions._; |
| 162 | |
| 163 | const useStdin = typeof text === "string"; |
| 164 | |
| 165 | if (currentOptions.version) { // version from package.json |
| 166 | |
| 167 | log.info(`v${require("../package.json").version}`); |
| 168 | |
| 169 | } else if (currentOptions.printConfig) { |
| 170 | if (files.length) { |
| 171 | log.error("The --print-config option must be used with exactly one file name."); |
| 172 | return 2; |
| 173 | } |
| 174 | if (useStdin) { |
| 175 | log.error("The --print-config option is not available for piped-in code."); |
| 176 | return 2; |
| 177 | } |
| 178 | |
| 179 | const engine = new CLIEngine(translateOptions(currentOptions)); |
| 180 | |
| 181 | const fileConfig = engine.getConfigForFile(currentOptions.printConfig); |
| 182 | |
| 183 | log.info(JSON.stringify(fileConfig, null, " ")); |
| 184 | return 0; |
| 185 | } else if (currentOptions.help || (!files.length && !useStdin)) { |
| 186 | |
| 187 | log.info(options.generateHelp()); |
| 188 | |
| 189 | } else { |
| 190 | |
| 191 | debug(`Running on ${useStdin ? "text" : "files"}`); |
| 192 | |
| 193 | if (currentOptions.fix && currentOptions.fixDryRun) { |
| 194 | log.error("The --fix option and the --fix-dry-run option cannot be used together."); |
| 195 | return 2; |
| 196 | } |
| 197 | |
| 198 | if (useStdin && currentOptions.fix) { |
| 199 | log.error("The --fix option is not available for piped-in code; use --fix-dry-run instead."); |
| 200 | return 2; |
| 201 | } |
| 202 | |
| 203 | if (currentOptions.fixType && !currentOptions.fix && !currentOptions.fixDryRun) { |
| 204 | log.error("The --fix-type option requires either --fix or --fix-dry-run."); |
| 205 | return 2; |
| 206 | } |
| 207 | |
| 208 | const engine = new CLIEngine(translateOptions(currentOptions)); |
| 209 | const report = useStdin ? engine.executeOnText(text, currentOptions.stdinFilename, true) : engine.executeOnFiles(files); |
| 210 | |
| 211 | if (currentOptions.fix) { |
| 212 | debug("Fix mode enabled - applying fixes"); |
| 213 | CLIEngine.outputFixes(report); |
| 214 | } |
| 215 | |
| 216 | if (currentOptions.quiet) { |
| 217 | debug("Quiet mode enabled - filtering out warnings"); |
| 218 | report.results = CLIEngine.getErrorResults(report.results); |
| 219 | } |
| 220 | |
| 221 | if (printResults(engine, report.results, currentOptions.format, currentOptions.outputFile)) { |
| 222 | const tooManyWarnings = currentOptions.maxWarnings >= 0 && report.warningCount > currentOptions.maxWarnings; |
| 223 | |
| 224 | if (!report.errorCount && tooManyWarnings) { |
| 225 | log.error("ESLint found too many warnings (maximum: %s).", currentOptions.maxWarnings); |
| 226 | } |
| 227 | |
| 228 | return (report.errorCount || tooManyWarnings) ? 1 : 0; |
| 229 | } |
| 230 | return 2; |
| 231 | |
| 232 | |
| 233 | } |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | }; |
| 238 | |
| 239 | module.exports = cli; |