-
-
Notifications
You must be signed in to change notification settings - Fork 240
/
lint.ts
287 lines (254 loc) · 9.33 KB
/
lint.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
import { DiagnosticSeverity, Dictionary } from '@stoplight/types';
import { isPlainObject } from '@stoplight/json';
import { getDiagnosticSeverity, IRuleResult } from '@stoplight/spectral-core';
import { difference, isError, pick } from 'lodash';
import type { ReadStream } from 'tty';
import type { CommandModule } from 'yargs';
import * as process from 'process';
import chalk from 'chalk';
import type { ErrorWithCause } from 'pony-cause';
import StackTracey from 'stacktracey';
import { lint } from '../services/linter';
import { formatOutput, writeOutput } from '../services/output';
import { FailSeverity, ILintConfig, OutputFormat } from '../services/config';
import { CLIError } from '../errors';
const formatOptions = Object.values(OutputFormat);
const lintCommand: CommandModule = {
describe: 'lint JSON/YAML documents from files or URLs',
command: 'lint [documents..]',
builder: yargs =>
yargs
.strict()
.positional('documents', {
description:
'Location of JSON/YAML documents. Can be either a file, a glob or fetchable resource(s) on the web.',
coerce(values) {
if (Array.isArray(values) && values.length > 0) {
return values as unknown[];
}
// https://stackoverflow.com/questions/39801643/detect-if-node-receives-stdin
// https://twitter.com/MylesBorins/status/782009479382626304
// https://nodejs.org/dist/latest/docs/api/tty.html#tty_readstream_istty
if (process.stdin.isTTY) {
return [];
}
return [(process.stdin as ReadStream & { fd: 0 }).fd];
},
})
.middleware((argv: Dictionary<unknown>) => {
const formats = argv.format as [string, ...string[]];
if (argv.output === void 0) {
argv.output = { [formats[0]]: '<stdout>' };
} else if (typeof argv.output === 'string') {
argv.output = { [formats[0]]: argv.output };
} else {
const output = argv.output as Dictionary<unknown>;
if (Object.keys(output).length >= formats.length) {
return;
}
const firstMissingFormat = formats.find(f => !(f in output));
if (firstMissingFormat !== void 0) {
output[firstMissingFormat] = '<stdout>';
}
}
})
.check((argv: Dictionary<unknown>) => {
if (!Array.isArray(argv.documents) || argv.documents.length === 0) {
throw new CLIError('No documents provided.');
}
const format = argv.format as string[] & { 0: string };
const output = argv.output as Dictionary<unknown> | undefined;
if (format.length === 1) {
if (output === void 0 || Object.keys(output).length === 1) {
return true;
}
throw new CLIError('Output must be either string or unspecified when a single format is specified');
}
if (!isPlainObject(output)) {
throw new CLIError('Multiple outputs have to be provided when more than a single format is specified');
}
const keys = Object.keys(output);
if (format.length !== keys.length) {
throw new CLIError('The number of outputs must match the number of formats');
}
const diff = difference(format, keys);
if (diff.length !== 0) {
throw new CLIError(`Missing outputs for the following formats: ${diff.join(', ')}`);
}
return true;
})
.options({
encoding: {
alias: 'e',
description: 'text encoding to use',
type: 'string',
default: 'utf8',
choices: ['utf8', 'ascii', 'utf-8', 'utf16le', 'ucs2', 'ucs-2', 'base64', 'latin1'],
},
format: {
alias: 'f',
description:
'formatters to use for outputting results, more than one can be provided by using multiple flags',
choices: formatOptions,
default: OutputFormat.STYLISH,
type: 'string',
coerce(values: string | string[]) {
return Array.isArray(values) ? values : [values];
},
},
output: {
alias: 'o',
description: `where to output results, can be a single file name, multiple "output.<format>" or missing to print to stdout`,
type: 'string',
},
'stdin-filepath': {
description: 'path to a file to pretend that stdin comes from',
type: 'string',
},
resolver: {
description: 'path to custom json-ref-resolver instance',
type: 'string',
},
ruleset: {
alias: 'r',
description: 'path/URL to a ruleset file',
type: 'string',
},
'fail-severity': {
alias: 'F',
description: 'results of this level or above will trigger a failure exit code',
choices: ['error', 'warn', 'info', 'hint'],
default: 'error',
type: 'string',
},
'display-only-failures': {
alias: 'D',
description: 'only output results equal to or greater than --fail-severity',
type: 'boolean',
default: false,
},
'ignore-unknown-format': {
description: 'do not warn about unmatched formats',
type: 'boolean',
default: false,
},
'fail-on-unmatched-globs': {
description: 'fail on unmatched glob patterns',
type: 'boolean',
default: false,
},
verbose: {
alias: 'v',
description: 'increase verbosity',
type: 'boolean',
},
quiet: {
alias: 'q',
description: 'no logging - output only',
type: 'boolean',
},
}),
async handler(args) {
const {
documents,
failSeverity,
displayOnlyFailures,
ruleset,
stdinFilepath,
format,
output,
encoding,
ignoreUnknownFormat,
failOnUnmatchedGlobs,
...config
} = args as unknown as ILintConfig & {
documents: Array<number | string>;
failSeverity: FailSeverity;
displayOnlyFailures: boolean;
};
try {
const linterResult = await lint(documents, {
format,
output,
encoding,
ignoreUnknownFormat,
failOnUnmatchedGlobs,
ruleset,
stdinFilepath,
...pick<Partial<ILintConfig>, keyof ILintConfig>(config, ['verbose', 'quiet', 'resolver']),
});
if (displayOnlyFailures) {
linterResult.results = filterResultsBySeverity(linterResult.results, failSeverity);
}
await Promise.all(
format.map(f => {
const formattedOutput = formatOutput(
linterResult.results,
f,
{ failSeverity: getDiagnosticSeverity(failSeverity) },
linterResult.resolvedRuleset,
);
return writeOutput(formattedOutput, output?.[f] ?? '<stdout>');
}),
);
if (linterResult.results.length > 0) {
process.exit(severeEnoughToFail(linterResult.results, failSeverity) ? 1 : 0);
} else if (config.quiet !== true) {
const isErrorSeverity = getDiagnosticSeverity(failSeverity) === DiagnosticSeverity.Error;
process.stdout.write(
`No results with a severity of '${failSeverity}' ${isErrorSeverity ? '' : 'or higher '}found!\n`,
);
}
} catch (ex) {
fail(isError(ex) ? ex : new Error(String(ex)), config.verbose === true);
}
},
};
const fail = (error: Error | ErrorWithCause<unknown> | AggregateError, verbose: boolean): void => {
if (error instanceof CLIError) {
process.stderr.write(chalk.red(error.message));
process.exit(2);
}
const errors: unknown[] = 'errors' in error ? error.errors : [error];
process.stderr.write(chalk.red('Error running Spectral!\n'));
if (!verbose) {
process.stderr.write(chalk.red('Use --verbose flag to print the error stack.\n'));
}
for (const [i, error] of errors.entries()) {
const actualError: unknown = isError(error) && 'cause' in error ? (error as ErrorWithCause<unknown>).cause : error;
const message = isError(actualError) ? actualError.message : String(actualError);
const info = `Error #${i + 1}: `;
process.stderr.write(`${info}${chalk.red(message)}\n`);
if (verbose && isError(actualError)) {
process.stderr.write(`${chalk.red(printErrorStacks(actualError, info.length))}\n`);
}
}
process.exit(2);
};
function getWidth(ratio: number): number {
return Math.min(20, Math.floor(ratio * process.stderr.columns));
}
function printErrorStacks(error: Error, padding: number): string {
return new StackTracey(error)
.slice(0, 5)
.withSources()
.asTable({
maxColumnWidths: {
callee: getWidth(0.2),
file: getWidth(0.4),
sourceLine: getWidth(0.4),
},
})
.split('\n')
.map(error => `${' '.repeat(padding)}${error}`)
.join('\n');
}
const filterResultsBySeverity = (results: IRuleResult[], failSeverity: FailSeverity): IRuleResult[] => {
const diagnosticSeverity = getDiagnosticSeverity(failSeverity);
return results.filter(r => r.severity <= diagnosticSeverity);
};
export const severeEnoughToFail = (results: IRuleResult[], failSeverity: FailSeverity): boolean => {
const diagnosticSeverity = getDiagnosticSeverity(failSeverity);
return results.some(r => r.severity <= diagnosticSeverity);
};
export default lintCommand;