-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
init.ts
88 lines (78 loc) · 2.6 KB
/
init.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
import { Command, flags } from '@oclif/command';
import { OutputFormat, parseDefinition, stringifyDocument } from '../common/definition';
import * as commonFlags from '../common/flags';
import { Document } from '@apidevtools/swagger-parser';
import { OpenAPIV3 } from 'openapi-types';
import * as deepMerge from 'deepmerge'
export default class Init extends Command {
public static description = 'Initialise a definition file from scratch';
public static examples = [`$ openapi init --title 'My API' > openapi.yml`];
public static flags = {
...commonFlags.help(),
title: flags.string({ char: 'T', description: 'The title for the API', default: 'My API' }),
description: flags.string({ char: 'd', description: 'Description for the API' }),
version: flags.string({ char: 'v', description: 'Version of the API', default: '0.0.1' }),
terms: flags.string({ description: 'A URL to the Terms of Service for the API.' }),
license: flags.string({ description: 'The license for the API', options: ['mit', 'apache2'] }),
...commonFlags.servers(),
...commonFlags.inject(),
...commonFlags.outputFormat(),
};
public static args = [];
public async run() {
const { flags } = this.parse(Init);
const { title, version, server, inject, license, description, terms } = flags;
const OPENAPI_VERSION = '3.0.0';
const info: OpenAPIV3.InfoObject = {
title,
version,
};
if (description) {
info.description = description;
}
if (terms) {
info.termsOfService = terms;
}
if (license) {
switch (license) {
case 'apache2':
info.license = {
name: 'Apache 2.0',
url: 'http://www.apache.org/licenses/LICENSE-2.0.html',
};
case 'mit':
info.license = {
name: 'MIT',
url: 'https://opensource.org/licenses/MIT',
};
}
}
let document: Document = {
openapi: OPENAPI_VERSION,
info,
paths: {},
};
// merge injected JSON
if (inject) {
for (const json of inject) {
try {
const parsed = JSON.parse(json);
document = deepMerge(document, parsed);
} catch (err) {
console.error('Could not parse inject JSON');
throw err;
}
}
}
if (server) {
const { paths, ...d } = document;
document = {
...d,
servers: server.map((url) => ({ url })),
paths,
};
}
const format = flags.format === 'json' || flags.json ? OutputFormat.JSON : OutputFormat.YAML;
this.log(stringifyDocument({ document, format }));
}
}