1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
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 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | function quietFixPredicate(lintResult) {
|
39 | return lintResult.severity === 2;
|
40 | }
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
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 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
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 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 | const cli = {
|
140 |
|
141 | |
142 |
|
143 |
|
144 |
|
145 |
|
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) {
|
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;
|