-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathload.ts
59 lines (48 loc) · 1.67 KB
/
load.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
import { Command, Args } from '@oclif/core';
import * as commonFlags from '../common/flags';
import * as path from 'path';
import * as fs from 'fs';
import * as YAML from 'js-yaml';
import { parseDefinition } from '../common/definition';
import { CONFIG_FILENAME, Config, resolveConfigFile } from '../common/config';
export class Load extends Command {
public static description = 'Set the default definition file for a workspace (writes to .openapiconfig)';
public static examples = [
`$ openapi load ./openapi.yml`,
'$ openapi load https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore.yaml',
];
public static flags = {
...commonFlags.help(),
...commonFlags.validate(),
...commonFlags.servers(),
};
public static args = {
definition: Args.string({
description: 'input definition file',
required: true
})
}
public async run() {
const { args, flags } = await this.parse(Load);
const definition = args.definition;
// check that definition can be parsed
try {
await parseDefinition({ definition, validate: flags.validate });
} catch (err) {
this.error(err, { exit: 1 });
}
const configFile = resolveConfigFile();
// write to config file
const oldConfig: Config = configFile ? YAML.load(fs.readFileSync(configFile).toString()) : {};
const newConfig = {
...oldConfig,
definition,
};
// default to current directory
const writeTo = path.resolve(configFile || `./${CONFIG_FILENAME}`);
// write as YAML
fs.writeFileSync(writeTo, YAML.dump(newConfig));
this.log(`Wrote to ${writeTo}`);
this.log(`Loaded succesfully!`);
}
}