-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
awslint.ts
309 lines (253 loc) · 9.28 KB
/
awslint.ts
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* eslint-disable max-len */
/* eslint-disable no-console */
import * as child_process from 'child_process';
import * as path from 'path';
import * as chalk from 'chalk';
import * as fs from 'fs-extra';
import * as reflect from 'jsii-reflect';
import * as yargs from 'yargs';
import { ALL_RULES_LINTER, DiagnosticLevel, RuleFilterSet } from '../lib';
let stackTrace = false;
async function main() {
const argv = yargs
.env('AWSLINT')
.usage('awslint [options] [command]')
.showHelpOnFail(true)
.command('$0', 'lint the current module (default)')
.command('list', 'list all available rules')
.option('include', { alias: 'i', type: 'array', desc: 'evaluate only this rule(s)', default: ['*'] })
.option('exclude', { alias: 'x', type: 'array', desc: 'do not evaluate these rules (takes priority over --include)', default: [] })
.option('save', { type: 'boolean', desc: 'updates package.json (or awslint.json if it exists) with "exclude" statements for all failing rules', default: false })
.option('verbose', { alias: 'v', type: 'boolean', desc: 'verbose output (prints all assertions)', default: false })
.option('quiet', { alias: 'q', type: 'boolean', desc: 'quiet mode - shows only errors', default: true })
.option('force', { type: 'boolean', desc: 'succeed silently if this is not a jsii module', default: true })
.option('config', { type: 'boolean', desc: 'reads options from "awslint.json" if it exists, and otherwise from the "awslint" section in package.json', default: true })
.option('debug', { type: 'boolean', desc: 'debug output', default: false })
.option('compile', { alias: 'c', type: 'boolean', desc: 'always run the jsii compiler (use "--no-compile" to never run the compiler, even if .jsii doesn\'t exist)' })
.group('include', 'Filtering')
.group('exclude', 'Filtering')
.group('config', 'Configuration')
.group('save', 'Configuration')
.group('verbose', 'Output')
.group('quiet', 'Output')
.group('debug', 'Output')
.group('compile', 'Build')
.example('awslint', 'lints the current module against all rules')
.example('awslint -v -i "resource*" -i "import*"', 'lints against all rules that start with "resource" or "import" and print successes')
.example('awslint -x "*:@aws-cdk/aws-s3*"', 'evaluate all rules in all scopes besides ones that begin with "@aws-cdk/aws-s3"')
.example('awslint --save', 'updated "awslint.json" with "exclude"s for all failing rules');
if (!process.stdout.isTTY) {
// Disable chalk color highlighting
process.env.FORCE_COLOR = '0';
}
const args = argv.argv;
stackTrace = args.verbose || args.debug;
if (args._.length > 1) {
argv.showHelp();
process.exitCode = 1;
return;
}
const command = args._[0] || 'lint';
const workdir = process.cwd();
const pkgConfig = path.join(workdir, 'package.json');
const awslintConfig = path.join(workdir, 'awslint.json');
if (command === 'list') {
for (const rule of ALL_RULES_LINTER.rules) {
console.info(`${chalk.cyan(rule.code)}: ${rule.message}`);
}
return;
}
// if no package.json and force is true (default), just don't do anything
if (!await fs.pathExists(pkgConfig) && args.force) {
return;
}
const pkg = await fs.readJSON(pkgConfig);
// if this is not a jsii module we have nothing to look for
if (!pkg.jsii) {
if (args.force) {
return; // just silently succeed
}
throw new Error(`Module in ${workdir} is not a jsii module (no "jsii" section in package.json)`);
}
// if package.json contains a `jsii` section but there is no .jsii file
// it means we haven't compiled the module.
if (await shouldCompile()) {
await shell('jsii');
}
// awslint.json takes precedence over package.json for awslint options.
const standloneOptions = await readStandaloneOptions(awslintConfig);
// read "awslint" from package.json
if (args.config) {
if (standloneOptions) {
if (pkg.awslint) {
console.warn('awslint options in package.json will be ignored since awslint.json exists.');
}
mergeOptions(args, standloneOptions);
} else {
mergeOptions(args, pkg.awslint);
}
}
if (args.debug) {
console.error('command: ' + command);
console.error('options: ' + JSON.stringify(args, undefined, 2));
}
if (command === 'lint') {
const assembly = await loadModule(workdir);
if (!assembly) {
return;
}
const excludesToSave = new Array<string>();
let errors = 0;
const includeRules = new RuleFilterSet(args.include);
const excludeRules = new RuleFilterSet(args.exclude);
const results = [];
results.push(...ALL_RULES_LINTER.eval(assembly, {
includeRules: includeRules,
excludeRules: excludeRules,
}));
// Sort errors to the top (highest severity first)
results.sort((a, b) => b.level - a.level);
// process results
for (const diag of results) {
const suppressionKey = `${diag.rule.code}:${diag.scope}`;
let color;
switch (diag.level) {
case DiagnosticLevel.Success:
if (args.verbose) {
color = chalk.gray;
}
break;
case DiagnosticLevel.Error:
errors++;
color = chalk.red;
if (args.save) {
excludesToSave.push(suppressionKey);
}
break;
case DiagnosticLevel.Warning:
if (!args.quiet) {
color = chalk.yellow;
}
break;
case DiagnosticLevel.Skipped:
if (!args.quiet) {
color = chalk.blue;
}
break;
}
if (color) {
console.error(color(`${DiagnosticLevel[diag.level].toLowerCase()}: [${chalk.bold(`awslint:${diag.rule.code}`)}:${chalk.bold(diag.scope)}] ${diag.message}`));
}
}
if (excludesToSave.length > 0) {
if (standloneOptions) {
if (!standloneOptions.exclude) {
standloneOptions.exclude = [];
}
} else {
if (!pkg.awslint) {
pkg.awslint = { };
}
if (!pkg.awslint.exclude) {
pkg.awslint.exclude = [];
}
}
const excludes: string[] = standloneOptions ? standloneOptions.exclude : pkg.awslint.exclude;
for (const exclude of excludesToSave) {
if (excludes.indexOf(exclude) === -1) {
excludes.push(exclude);
}
}
if (excludes.length > 0) {
if (standloneOptions) {
// If awslint.json exists, write the excludes there instead of the old package.json (legacy format).
if (pkg.awslint) {
console.warn('Excludes will be written to awslint.json.');
}
await fs.writeJSON(awslintConfig, standloneOptions, { spaces: 2 });
} else {
await fs.writeJSON(pkgConfig, pkg, { spaces: 2 });
}
}
}
if (errors && !args.save) {
process.exitCode = 1;
}
return;
}
argv.showHelp();
throw new Error(`Invalid command: ${command}`);
async function shouldCompile() {
// if --compile is explicitly enabled then just compile always
if (args.compile === true) {
return true;
}
// if we have a .jsii file and we are not forced to compile, then don't compile
if (await fs.pathExists(path.join(workdir, '.jsii'))) {
return false;
}
// we don't have a .jsii file, and --no-compile is explicily set, then it's an error
if (args.compile === false) {
throw new Error('No .jsii file and --no-compile is set');
}
// compile!
return true;
}
}
main().catch(e => {
console.error(chalk.red(e.message));
if (stackTrace) {
console.error(e.stack);
}
process.exitCode = 1;
});
async function loadModule(dir: string) {
const ts = new reflect.TypeSystem();
await ts.load(dir, { validate: false }); // Don't validate to save 66% of execution time (20s vs 1min).
// We run 'awslint' during build time, assemblies are guaranteed to be ok.
// We won't load any more assemblies. Lock the typesystem to benefit from performance improvements.
ts.lock();
if (ts.roots.length !== 1) {
throw new Error('Expecting only a single root assembly');
}
return ts.roots[0];
}
function mergeOptions(dest: any, pkg?: any) {
if (!pkg) { return; } // no options in package.json
for (const [key, value] of Object.entries(pkg)) {
// if this is an array option, then add values to destination
if (Array.isArray(value)) {
const arr = dest[key] || [];
arr.push(...value);
dest[key] = arr;
continue;
}
// objects are not allowed
if (typeof value === 'object') {
throw new Error(`Invalid option "${key}" in package.json: ${JSON.stringify(value)}`);
}
// primitives simply override
dest[key] = value;
}
return dest;
}
async function readStandaloneOptions(awslintConfig: string) {
if (!await fs.pathExists(awslintConfig)) {
return;
}
const options = await fs.readJSON(awslintConfig);
return options;
}
async function shell(command: string) {
const child = child_process.spawn(command, [], { stdio: ['inherit', 'inherit', 'inherit'] });
return new Promise<void>((ok, ko) => {
child.once('exit', (status: any) => {
if (status === 0) {
return ok();
} else {
return ko(new Error(`${command} exited with status ${status}`));
}
});
child.once('error', ko);
});
}