-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathread.ts
60 lines (52 loc) · 1.71 KB
/
read.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
import { Command, Args } from '@oclif/core';
import { parseDefinition, OutputFormat, stringifyDocument, resolveDefinition } from '../common/definition';
import * as commonFlags from '../common/flags';
import { Document } from '@apidevtools/swagger-parser';
export class Read extends Command {
public static description = 'Read and manipulate definition files';
public static examples = [
'$ openapi read https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore.yaml',
`$ openapi read ./openapi.yml -f json > openapi.json`,
];
public static flags = {
...commonFlags.help(),
...commonFlags.parseOpts(),
...commonFlags.outputFormat(),
};
public static args = {
definition: Args.string({
description: 'input definition file'
})
}
public async run() {
const { args, flags } = await this.parse(Read);
const { dereference, validate, bundle, header, root } = flags;
const definition = resolveDefinition(args.definition);
if (!definition) {
this.error('Please load a definition file', { exit: 1 });
}
let document: Document;
try {
document = await parseDefinition({
definition,
dereference,
bundle,
validate,
inject: flags.inject,
strip: flags.strip,
excludeExt: flags?.['exclude-ext'],
servers: flags.server,
header,
root,
});
} catch (err) {
this.error(err, { exit: 1 });
}
const format = flags.format === 'json' || flags.json ? OutputFormat.JSON : OutputFormat.YAML;
if (format === OutputFormat.JSON) {
this.logJson(document)
} else {
this.log(stringifyDocument({ document, format }));
}
}
}