-
-
Notifications
You must be signed in to change notification settings - Fork 175
/
edit.ts
44 lines (41 loc) · 1.6 KB
/
edit.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
import { Args } from '@oclif/core';
import Command from '../../../core/base';
import { editContext, CONTEXT_FILE_PATH } from '../../../core/models/Context';
import {
MissingContextFileError,
ContextFileWrongFormatError,
ContextFileEmptyError,
} from '../../../core/errors/context-error';
import { helpFlag } from '../../../core/flags/global.flags';
export default class ContextEdit extends Command {
static description = 'Edit a context in the store';
static flags = helpFlag();
static args = {
'context-name': Args.string({description: 'context name', required: true}),
'new-spec-file-path': Args.string({description: 'file path of the spec file', required: true}),
};
async run() {
const { args } = await this.parse(ContextEdit);
const contextName = args['context-name'];
const newSpecFilePath = args['new-spec-file-path'];
try {
await editContext(contextName, newSpecFilePath);
this.log(
`Edited 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}`
);
} catch (e) {
if (
e instanceof (MissingContextFileError || ContextFileWrongFormatError)
) {
this.log(
'You have no context file configured. Run "asyncapi config context init" to initialize it.'
);
return;
} else if (e instanceof ContextFileEmptyError) {
this.log(`Context file "${CONTEXT_FILE_PATH}" is empty.`);
return;
}
throw e;
}
}
}