-
-
Notifications
You must be signed in to change notification settings - Fork 169
/
add.ts
56 lines (51 loc) · 1.79 KB
/
add.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
import { Flags, Args } from '@oclif/core';
import Command from '../../../base';
import { addContext, setCurrentContext } from '../../../models/Context';
import {
MissingContextFileError,
ContextFileWrongFormatError,
} from '../../../errors/context-error';
export default class ContextAdd extends Command {
static description = 'Add a context to the store';
static flags = {
help: Flags.help({ char: 'h' }),
'set-current': Flags.boolean({
char: 's',
description: 'Set context being added as the current context',
default: false,
required: false,
})
};
static args = {
'context-name': Args.string({description: 'context name', required: true}),
'spec-file-path': Args.string({description: 'file path of the spec file', required: true}),
};
async run() {
const { args, flags } = await this.parse(ContextAdd);
const contextName = args['context-name'];
const specFilePath = args['spec-file-path'];
const setAsCurrent = flags['set-current'];
try {
await addContext(contextName, specFilePath);
this.log(
`Added context "${contextName}".\n\nYou can set it as your current context: asyncapi config context use ${contextName}\nYou can use this context when needed by passing ${contextName} as a parameter: asyncapi validate ${contextName}`
);
if (setAsCurrent) {
await setCurrentContext(contextName);
this.log(
`The newly added context "${contextName}", is set as your current context!`
);
}
} catch (e) {
if (
e instanceof (MissingContextFileError || ContextFileWrongFormatError)
) {
this.log(
'You have no context file configured. Run "asyncapi config context init" to initialize it.'
);
return;
}
throw e;
}
}
}