-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathcli.js
219 lines (200 loc) · 7.41 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
'use strict';
const nibbler = require('./nibbler');
const fmt = require('./config/formatters');
const chalk = require('chalk');
const inquirer = require('inquirer');
const options = require('./config/options');
const { version } = require('../package.json');
const cli = {
async execute(args) {
let currentOptions,
files,
extensions,
configFile,
resolvePluginsRelativeTo,
cache,
cacheLocation,
allowedRules,
rulesDir,
includeWarnings,
isInteractive,
isMulti,
format,
fixableOnly;
// Parse options
try {
currentOptions = options.parse(args);
files = currentOptions._;
extensions = currentOptions.ext;
configFile = currentOptions.config;
resolvePluginsRelativeTo = currentOptions.resolvePluginsRelativeTo;
cache = currentOptions.cache;
cacheLocation = currentOptions.cacheLocation;
allowedRules = currentOptions.rule;
rulesDir = currentOptions.rulesdir;
includeWarnings = currentOptions.warnings;
isInteractive = currentOptions.interactive;
isMulti = currentOptions.multi;
format = currentOptions.format;
fixableOnly = currentOptions.fixableOnly;
} catch (error) {
console.error(error.message);
return 1;
}
// Decide what to do based on options
if (currentOptions.version) {
// Show version from package.json
console.log('v' + version);
} else if (currentOptions.help || (!files.length)) {
// Show help
console.log(options.generateHelp());
} else {
const configuration = { };
if (extensions) {
configuration.extensions = extensions;
}
if (configFile) {
configuration.overrideConfigFile = configFile;
}
if (resolvePluginsRelativeTo) {
configuration.resolvePluginsRelativeTo = resolvePluginsRelativeTo;
}
if (cache) {
configuration.cache = cache;
}
if (cacheLocation) {
configuration.cacheLocation = cacheLocation;
}
if (rulesDir) {
configuration.rulePaths = rulesDir;
}
nibbler.configure(configuration);
let report = await nibbler.nibbleOnFiles(files);
if (report && (report.errorCount > 0 || report.warningCount > 0)) {
// Check if there was a fatal error
const fatalReport = nibbler.getFatalResults(report);
if (fatalReport) {
const errors = await nibbler.getFormattedResults(fatalReport, 'stylish');
console.log(errors);
console.error('Fatal error(s) were detected. Please correct and try again.');
return 2;
}
if (report && !includeWarnings) {
report = nibbler.getSeverityResults(report, 2);
}
if (report && fixableOnly) {
report = nibbler.getFixableResults(report);
}
// Calculate stats array
const stats = await nibbler.getFormattedResults(report, fmt.stats);
// Create an array of choices from the stats
// (filter removes empty stat at end)
const results = stats
.split('\n')
.filter(stat => stat)
.map(stat => {
const ruleName = stat.split(':')[0];
return {
name : stat,
value: ruleName,
short: ruleName
};
})
// Only include allowed rules, if given
.filter(stat => allowedRules ? allowedRules.includes(stat.value) : true);
if (!results.length) {
// If all stats were filtered out due to provided `--rule` options…
if (allowedRules && allowedRules.length) {
console.log(chalk.yellow(`\nNo lint failures found for rule(s): ${allowedRules.join(', ')}`));
console.log('Try running again without "--rule"');
return 0;
}
// Or maybe they were filtered out because they were all warnings,
// and the user didn't want to check warnings
if (!includeWarnings) {
console.log(chalk.green('Great job, no lint rules reporting errors.'));
return 0;
}
// Or if all stats were filtered out due to a provided `--fixable` flag
if (fixableOnly) {
console.log(chalk.yellow('\nNo fixable lint failures found.'));
console.log('Try running again without "--fixable-only"');
return 0;
}
}
if (!isInteractive) {
const finalReport = allowedRules
? nibbler.getRuleResults(report, allowedRules)
: report;
// Just give an exit code based on having any errors, no interactive menu
const output = await nibbler.getFormattedResults(finalReport, format);
console.log(output);
return report.errorCount > 0 ? 1 : 0;
}
// Show summary
const summary = await nibbler.getFormattedResults(report, fmt.summary);
console.log(summary);
// Ask user for the rule to narrow in on
inquirer.prompt([{
name : 'rule',
type : isMulti ? 'checkbox' : 'list',
message : isMulti ? 'Which rule(s) would you like to view?' : 'Which rule would you like to view?',
choices : results,
pageSize: results.length
},
{
name : 'fix',
type : 'confirm',
message: 'Would you like to attempt to auto-fix?',
default: false,
when(answers) {
const ruleReport = nibbler.getRuleResults(report, answers.rule);
return ruleReport.fixableErrorCount > 0 || ruleReport.fixableWarningCount > 0;
}
},
{
name : 'fixWarnings',
type : 'confirm',
message: 'Autofix warnings?',
default: true,
when(answers) {
if (!answers.fix) return false;
const ruleReport = nibbler.getRuleResults(report, answers.rule);
return ruleReport.fixableWarningCount > 0;
}
}])
.then(async function gotInput(answers) {
// Display detailed error reports
if (answers.fix) {
const fixOptions = {
rules : isMulti ? answers.rule : [answers.rule],
warnings: answers.fixWarnings
};
const fixedReport = await nibbler.fixNibbles(files, fixOptions, configuration);
const ruleResults = nibbler.getRuleResults(fixedReport, answers.rule);
if (ruleResults.errorCount > 0 || ruleResults.warningCount > 0) {
const detailed = await nibbler.getFormattedResults(ruleResults, fmt.detailed);
console.log(detailed);
} else {
if (isMulti) {
console.log(chalk.green(`Fixes applied: ${answers.rule.join(', ')} now passing`));
} else {
console.log(chalk.green(`Fixes applied, ${answers.rule} is now passing`));
}
}
} else {
const ruleResults = nibbler.getRuleResults(report, answers.rule);
const detailed = await nibbler.getFormattedResults(ruleResults, fmt.detailed);
console.log(detailed);
}
});
// No report or not any errors or warnings
} else {
console.log(chalk.green('Great job, all lint rules passed.'));
return 0;
}
}
return 0;
}
};
module.exports = cli;