1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | var fs = require("fs"),
|
19 | path = require("path"),
|
20 |
|
21 | debug = require("debug"),
|
22 |
|
23 | options = require("./options"),
|
24 | CLIEngine = require("./cli-engine"),
|
25 | mkdirp = require("mkdirp");
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | debug = debug("eslint:cli");
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | function translateOptions(cliOptions) {
|
40 | return {
|
41 | envs: cliOptions.env,
|
42 | extensions: cliOptions.ext,
|
43 | rules: cliOptions.rule,
|
44 | plugins: cliOptions.plugin,
|
45 | globals: cliOptions.global,
|
46 | ignore: cliOptions.ignore,
|
47 | ignorePath: cliOptions.ignorePath,
|
48 | ignorePattern: cliOptions.ignorePattern,
|
49 | configFile: cliOptions.config,
|
50 | rulePaths: cliOptions.rulesdir,
|
51 | useEslintrc: cliOptions.eslintrc,
|
52 | parser: cliOptions.parser,
|
53 | cache: cliOptions.cache,
|
54 | cacheFile: cliOptions.cacheFile,
|
55 | fix: cliOptions.fix
|
56 | };
|
57 | }
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | function printResults(engine, results, format, outputFile) {
|
69 | var formatter,
|
70 | output,
|
71 | filePath;
|
72 |
|
73 | formatter = engine.getFormatter(format);
|
74 | if (!formatter) {
|
75 | console.error("Could not find formatter '%s'.", format);
|
76 | return false;
|
77 | }
|
78 |
|
79 | output = formatter(results);
|
80 |
|
81 | if (output) {
|
82 | if (outputFile) {
|
83 | filePath = path.resolve(process.cwd(), outputFile);
|
84 |
|
85 | if (fs.existsSync(filePath) && fs.statSync(filePath).isDirectory()) {
|
86 | console.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 | console.error("There was a problem writing the output file:\n%s", ex);
|
95 | return false;
|
96 | }
|
97 | } else {
|
98 | console.log(output);
|
99 | }
|
100 | }
|
101 |
|
102 | return true;
|
103 |
|
104 | }
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | var cli = {
|
115 |
|
116 | |
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | execute: function(args, text) {
|
123 |
|
124 | var currentOptions,
|
125 | files,
|
126 | report,
|
127 | engine,
|
128 | tooManyWarnings;
|
129 |
|
130 | try {
|
131 | currentOptions = options.parse(args);
|
132 | } catch (error) {
|
133 | console.error(error.message);
|
134 | return 1;
|
135 | }
|
136 |
|
137 | files = currentOptions._;
|
138 |
|
139 | if (currentOptions.version) {
|
140 |
|
141 | console.log("v" + require("../package.json").version);
|
142 |
|
143 | } else if (currentOptions.help || (!files.length && !text)) {
|
144 |
|
145 | console.log(options.generateHelp());
|
146 |
|
147 | } else {
|
148 |
|
149 | debug("Running on " + (text ? "text" : "files"));
|
150 |
|
151 |
|
152 | if (text && currentOptions.fix) {
|
153 | console.error("The --fix option is not available for piped-in code.");
|
154 | return 1;
|
155 | }
|
156 |
|
157 | engine = new CLIEngine(translateOptions(currentOptions));
|
158 |
|
159 | report = text ? engine.executeOnText(text, currentOptions.stdinFilename) : engine.executeOnFiles(files);
|
160 | if (currentOptions.fix) {
|
161 | debug("Fix mode enabled - applying fixes");
|
162 | CLIEngine.outputFixes(report);
|
163 | }
|
164 |
|
165 | if (currentOptions.quiet) {
|
166 | debug("Quiet mode enabled - filtering out warnings");
|
167 | report.results = CLIEngine.getErrorResults(report.results);
|
168 | }
|
169 |
|
170 | if (printResults(engine, report.results, currentOptions.format, currentOptions.outputFile)) {
|
171 | tooManyWarnings = currentOptions.maxWarnings >= 0 && report.warningCount > currentOptions.maxWarnings;
|
172 |
|
173 | if (!report.errorCount && tooManyWarnings) {
|
174 | console.error("ESLint found too many warnings (maximum: %s).", currentOptions.maxWarnings);
|
175 | }
|
176 |
|
177 | return (report.errorCount || tooManyWarnings) ? 1 : 0;
|
178 | } else {
|
179 | return 1;
|
180 | }
|
181 |
|
182 | }
|
183 |
|
184 | return 0;
|
185 | }
|
186 | };
|
187 |
|
188 | module.exports = cli;
|