@asyncapi/cli 0.25.1 → 0.25.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/base.d.ts +3 -1
- package/lib/base.js +16 -3
- package/lib/commands/config/context/add.js +8 -6
- package/lib/commands/config/context/current.js +5 -3
- package/lib/commands/config/context/index.js +6 -4
- package/lib/commands/config/context/list.js +7 -5
- package/lib/commands/config/context/remove.js +12 -10
- package/lib/commands/config/context/use.js +7 -5
- package/lib/commands/config/index.js +6 -4
- package/lib/commands/convert.js +34 -32
- package/lib/commands/diff.js +87 -83
- package/lib/commands/generate/fromTemplate.d.ts +3 -3
- package/lib/commands/generate/fromTemplate.js +89 -83
- package/lib/commands/generate/index.js +5 -3
- package/lib/commands/generate/models.js +83 -81
- package/lib/commands/new.js +96 -90
- package/lib/commands/start/index.js +5 -3
- package/lib/commands/start/studio.js +7 -5
- package/lib/commands/validate.js +24 -22
- package/lib/flags.d.ts +1 -1
- package/lib/globals.js +4 -4
- package/lib/models/Context.js +88 -74
- package/lib/models/SpecificationFile.js +122 -103
- package/lib/utils/generator.js +15 -11
- package/oclif.manifest.json +1 -1
- package/package.json +17 -14
|
@@ -51,31 +51,33 @@ class Template extends base_1.default {
|
|
|
51
51
|
};
|
|
52
52
|
};
|
|
53
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
|
-
|
|
54
|
+
run() {
|
|
55
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
56
|
+
const { args, flags } = yield this.parse(Template); // NOSONAR
|
|
57
|
+
const asyncapi = args['asyncapi'];
|
|
58
|
+
const template = args['template'];
|
|
59
|
+
const output = flags.output || process.cwd();
|
|
60
|
+
const parsedFlags = this.parseFlags(flags['disable-hook'], flags['param'], flags['map-base-url']);
|
|
61
|
+
const options = {
|
|
62
|
+
forceWrite: flags['force-write'],
|
|
63
|
+
install: flags.install,
|
|
64
|
+
debug: flags.debug,
|
|
65
|
+
templateParams: parsedFlags.params,
|
|
66
|
+
noOverwriteGlobs: flags['no-overwrite'],
|
|
67
|
+
mapBaseUrlToFolder: parsedFlags.mapBaseUrlToFolder,
|
|
68
|
+
disabledHooks: parsedFlags.disableHooks,
|
|
69
|
+
};
|
|
70
|
+
const watchTemplate = flags['watch'];
|
|
71
|
+
const genOption = {};
|
|
72
|
+
if (flags['map-base-url']) {
|
|
73
|
+
genOption.resolve = { resolve: this.getMapBaseUrlToFolderResolver(parsedFlags.mapBaseUrlToFolder) };
|
|
74
|
+
}
|
|
75
|
+
yield this.generate(asyncapi, template, output, options, genOption);
|
|
76
|
+
if (watchTemplate) {
|
|
77
|
+
const watcherHandler = this.watcherHandler(asyncapi, template, output, options, genOption);
|
|
78
|
+
yield this.runWatchMode(asyncapi, template, output, watcherHandler);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
79
81
|
}
|
|
80
82
|
parseFlags(disableHooks, params, mapBaseUrl) {
|
|
81
83
|
return {
|
|
@@ -135,67 +137,71 @@ class Template extends base_1.default {
|
|
|
135
137
|
}
|
|
136
138
|
return mapBaseURLToFolder;
|
|
137
139
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
140
|
+
generate(asyncapi, template, output, options, genOption) {
|
|
141
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
142
|
+
let specification;
|
|
143
|
+
try {
|
|
144
|
+
specification = yield (0, SpecificationFile_1.load)(asyncapi);
|
|
145
|
+
}
|
|
146
|
+
catch (err) {
|
|
147
|
+
return this.error(new validation_error_1.ValidationError({
|
|
148
|
+
type: 'invalid-file',
|
|
149
|
+
filepath: asyncapi,
|
|
150
|
+
}), { exit: 1 });
|
|
151
|
+
}
|
|
152
|
+
const generator = new generator_1.default(template, output || path_1.default.resolve(os_1.default.tmpdir(), 'asyncapi-generator'), options);
|
|
153
|
+
core_1.CliUx.ux.action.start('Generation in progress. Keep calm and wait a bit');
|
|
154
|
+
try {
|
|
155
|
+
yield generator.generateFromString(specification.text(), genOption);
|
|
156
|
+
core_1.CliUx.ux.action.stop();
|
|
157
|
+
}
|
|
158
|
+
catch (err) {
|
|
159
|
+
core_1.CliUx.ux.action.stop('done\n');
|
|
160
|
+
throw new generator_error_1.GeneratorError(err);
|
|
161
|
+
}
|
|
162
|
+
console.log(`${yellow('Check out your shiny new generated files at ') + magenta(output) + yellow('.')}\n`);
|
|
163
|
+
});
|
|
160
164
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
165
|
+
runWatchMode(asyncapi, template, output, watchHandler) {
|
|
166
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
167
|
+
const specification = yield (0, SpecificationFile_1.load)(asyncapi);
|
|
168
|
+
const watchDir = path_1.default.resolve(template);
|
|
169
|
+
const outputPath = path_1.default.resolve(watchDir, output);
|
|
170
|
+
const transpiledTemplatePath = path_1.default.resolve(watchDir, generator_1.default.TRANSPILED_TEMPLATE_LOCATION);
|
|
171
|
+
const ignorePaths = [outputPath, transpiledTemplatePath];
|
|
172
|
+
const specificationFile = specification.getFilePath();
|
|
173
|
+
// Template name is needed as it is not always a part of the cli commad
|
|
174
|
+
// There is a use case that you run generator from a root of the template with `./` path
|
|
175
|
+
let templateName = '';
|
|
176
|
+
try {
|
|
177
|
+
// eslint-disable-next-line
|
|
178
|
+
templateName = require(path_1.default.resolve(watchDir, 'package.json')).name;
|
|
179
|
+
}
|
|
180
|
+
catch (err) {
|
|
181
|
+
// intentional
|
|
182
|
+
}
|
|
183
|
+
let watcher;
|
|
184
|
+
if (specificationFile) { // is local AsyncAPI file
|
|
185
|
+
this.log(`[WATCHER] Watching for changes in the template directory ${magenta(watchDir)} and in the AsyncAPI file ${magenta(specificationFile)}`);
|
|
186
|
+
watcher = new generator_2.Watcher([specificationFile, watchDir], ignorePaths);
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
this.log(`[WATCHER] Watching for changes in the template directory ${magenta(watchDir)}`);
|
|
190
|
+
watcher = new generator_2.Watcher(watchDir, ignorePaths);
|
|
191
|
+
}
|
|
192
|
+
// Must check template in its installation path in generator to use isLocalTemplate function
|
|
193
|
+
if (!(yield (0, generator_2.isLocalTemplate)(path_1.default.resolve(generator_1.default.DEFAULT_TEMPLATES_DIR, templateName)))) {
|
|
194
|
+
this.warn(`WARNING: ${template} is a remote template. Changes may be lost on subsequent installations.`);
|
|
195
|
+
}
|
|
196
|
+
watcher.watch(watchHandler, (paths) => {
|
|
197
|
+
this.error(`[WATCHER] Could not find the file path ${paths}, are you sure it still exists? If it has been deleted or moved please rerun the generator.`, {
|
|
198
|
+
exit: 1,
|
|
199
|
+
});
|
|
194
200
|
});
|
|
195
201
|
});
|
|
196
202
|
}
|
|
197
203
|
watcherHandler(asyncapi, template, output, options, genOption) {
|
|
198
|
-
return
|
|
204
|
+
return (changedFiles) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
199
205
|
console.clear();
|
|
200
206
|
console.log('[WATCHER] Change detected');
|
|
201
207
|
for (const [, value] of Object.entries(changedFiles)) {
|
|
@@ -216,12 +222,12 @@ class Template extends base_1.default {
|
|
|
216
222
|
this.log(`\t${magenta(value.path)} was ${eventText}`);
|
|
217
223
|
}
|
|
218
224
|
try {
|
|
219
|
-
|
|
225
|
+
yield this.generate(asyncapi, template, output, options, genOption);
|
|
220
226
|
}
|
|
221
227
|
catch (err) {
|
|
222
228
|
throw new generator_error_1.GeneratorError(err);
|
|
223
229
|
}
|
|
224
|
-
};
|
|
230
|
+
});
|
|
225
231
|
}
|
|
226
232
|
}
|
|
227
233
|
exports.default = Template;
|
|
@@ -4,9 +4,11 @@ const tslib_1 = require("tslib");
|
|
|
4
4
|
const base_1 = tslib_1.__importDefault(require("../../base"));
|
|
5
5
|
const core_1 = require("@oclif/core");
|
|
6
6
|
class Generate extends base_1.default {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
run() {
|
|
8
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
9
|
+
const help = new core_1.Help(this.config);
|
|
10
|
+
help.showHelp(['generate', '--help']);
|
|
11
|
+
});
|
|
10
12
|
}
|
|
11
13
|
}
|
|
12
14
|
exports.default = Generate;
|
|
@@ -17,90 +17,92 @@ var Languages;
|
|
|
17
17
|
})(Languages || (Languages = {}));
|
|
18
18
|
const possibleLanguageValues = Object.values(Languages).join(', ');
|
|
19
19
|
class Models extends base_1.default {
|
|
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
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
20
|
+
run() {
|
|
21
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
const passedArguments = yield this.parse(Models);
|
|
23
|
+
const { namespace, packageName, output } = passedArguments.flags;
|
|
24
|
+
const { language, file } = passedArguments.args;
|
|
25
|
+
const inputFile = (yield (0, SpecificationFile_1.load)(file)) || (yield (0, SpecificationFile_1.load)());
|
|
26
|
+
const parsedInput = yield (0, parser_1.parse)(inputFile.text());
|
|
27
|
+
modelina_1.Logger.setLogger({
|
|
28
|
+
info: (message) => {
|
|
29
|
+
this.log(message);
|
|
30
|
+
},
|
|
31
|
+
debug: (message) => {
|
|
32
|
+
this.debug(message);
|
|
33
|
+
},
|
|
34
|
+
warn: (message) => {
|
|
35
|
+
this.warn(message);
|
|
36
|
+
},
|
|
37
|
+
error: (message) => {
|
|
38
|
+
this.error(message);
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
let fileGenerator;
|
|
42
|
+
let fileOptions = {};
|
|
43
|
+
switch (language) {
|
|
44
|
+
case Languages.typescript:
|
|
45
|
+
fileGenerator = new modelina_1.TypeScriptFileGenerator();
|
|
46
|
+
break;
|
|
47
|
+
case Languages.csharp:
|
|
48
|
+
if (namespace === undefined) {
|
|
49
|
+
throw new Error('In order to generate models to C#, we need to know which namespace they are under. Add `--namespace=NAMESPACE` to set the desired namespace.');
|
|
50
|
+
}
|
|
51
|
+
fileGenerator = new modelina_1.CSharpFileGenerator();
|
|
52
|
+
fileOptions = {
|
|
53
|
+
namespace
|
|
54
|
+
};
|
|
55
|
+
break;
|
|
56
|
+
case Languages.golang:
|
|
57
|
+
if (packageName === undefined) {
|
|
58
|
+
throw new Error('In order to generate models to Go, we need to know which package they are under. Add `--packageName=PACKAGENAME` to set the desired package name.');
|
|
59
|
+
}
|
|
60
|
+
fileGenerator = new modelina_1.GoFileGenerator();
|
|
61
|
+
fileOptions = {
|
|
62
|
+
packageName
|
|
63
|
+
};
|
|
64
|
+
break;
|
|
65
|
+
case Languages.java:
|
|
66
|
+
if (packageName === undefined) {
|
|
67
|
+
throw new Error('In order to generate models to Java, we need to know which package they are under. Add `--packageName=PACKAGENAME` to set the desired package name.');
|
|
68
|
+
}
|
|
69
|
+
fileGenerator = new modelina_1.JavaFileGenerator();
|
|
70
|
+
fileOptions = {
|
|
71
|
+
packageName
|
|
72
|
+
};
|
|
73
|
+
break;
|
|
74
|
+
case Languages.javascript:
|
|
75
|
+
fileGenerator = new modelina_1.JavaScriptFileGenerator();
|
|
76
|
+
break;
|
|
77
|
+
case Languages.dart:
|
|
78
|
+
if (packageName === undefined) {
|
|
79
|
+
throw new Error('In order to generate models to Dart, we need to know which package they are under. Add `--packageName=PACKAGENAME` to set the desired package name.');
|
|
80
|
+
}
|
|
81
|
+
fileGenerator = new modelina_1.DartFileGenerator();
|
|
82
|
+
fileOptions = {
|
|
83
|
+
packageName
|
|
84
|
+
};
|
|
85
|
+
break;
|
|
86
|
+
default:
|
|
87
|
+
throw new Error(`Could not determine generator for language ${language}, are you using one of the following values ${possibleLanguageValues}?`);
|
|
88
|
+
}
|
|
89
|
+
let models;
|
|
90
|
+
if (output) {
|
|
91
|
+
models = yield fileGenerator.generateToFiles(parsedInput, output, Object.assign({}, fileOptions));
|
|
92
|
+
const generatedModels = models.map((model) => { return model.modelName; });
|
|
93
|
+
this.log(`Successfully generated the following models: ${generatedModels.join(', ')}`);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
models = yield fileGenerator.generateCompleteModels(parsedInput, Object.assign({}, fileOptions));
|
|
97
|
+
const generatedModels = models.map((model) => {
|
|
98
|
+
return `
|
|
98
99
|
## Model name: ${model.modelName}
|
|
99
100
|
${model.result}
|
|
100
101
|
`;
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
102
|
+
});
|
|
103
|
+
this.log(`Successfully generated the following models: ${generatedModels.join('\n')}`);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
104
106
|
}
|
|
105
107
|
}
|
|
106
108
|
exports.default = Models;
|
package/lib/commands/new.js
CHANGED
|
@@ -11,107 +11,113 @@ const { writeFile, readFile } = fs_1.promises;
|
|
|
11
11
|
const DEFAULT_ASYNCAPI_FILE_NAME = 'asyncapi.yaml';
|
|
12
12
|
const DEFAULT_ASYNCAPI_TEMPLATE = 'default-example.yaml';
|
|
13
13
|
class New extends base_1.default {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const fileName = flags['file-name'] || DEFAULT_ASYNCAPI_FILE_NAME;
|
|
21
|
-
const template = flags['example'] || DEFAULT_ASYNCAPI_TEMPLATE;
|
|
22
|
-
await this.createAsyncapiFile(fileName, template);
|
|
23
|
-
if (flags.studio) {
|
|
24
|
-
if (isTTY) {
|
|
25
|
-
(0, Studio_1.start)(fileName, flags.port || Studio_1.DEFAULT_PORT);
|
|
14
|
+
run() {
|
|
15
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
16
|
+
const { flags } = yield this.parse(New); // NOSONAR
|
|
17
|
+
const isTTY = process.stdout.isTTY;
|
|
18
|
+
if (!flags['no-tty'] && isTTY) {
|
|
19
|
+
return this.runInteractive();
|
|
26
20
|
}
|
|
27
|
-
|
|
28
|
-
|
|
21
|
+
const fileName = flags['file-name'] || DEFAULT_ASYNCAPI_FILE_NAME;
|
|
22
|
+
const template = flags['example'] || DEFAULT_ASYNCAPI_TEMPLATE;
|
|
23
|
+
yield this.createAsyncapiFile(fileName, template);
|
|
24
|
+
if (flags.studio) {
|
|
25
|
+
if (isTTY) {
|
|
26
|
+
(0, Studio_1.start)(fileName, flags.port || Studio_1.DEFAULT_PORT);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
this.warn('Warning: --studio flag was passed but the terminal is not interactive. Ignoring...');
|
|
30
|
+
}
|
|
29
31
|
}
|
|
30
|
-
}
|
|
32
|
+
});
|
|
31
33
|
}
|
|
32
34
|
/* eslint-disable sonarjs/cognitive-complexity */
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
questions.push({
|
|
42
|
-
name: 'filename',
|
|
43
|
-
message: 'name of the file?',
|
|
44
|
-
type: 'input',
|
|
45
|
-
default: DEFAULT_ASYNCAPI_FILE_NAME,
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
try {
|
|
49
|
-
const exampleFiles = await readFile((0, path_1.resolve)(__dirname, '../../assets/examples/examples.json'), { encoding: 'utf8' });
|
|
50
|
-
examples = JSON.parse(exampleFiles);
|
|
51
|
-
}
|
|
52
|
-
catch (error) {
|
|
53
|
-
// no examples found
|
|
54
|
-
}
|
|
55
|
-
if (!selectedTemplate && examples.length > 0) {
|
|
56
|
-
questions.push({
|
|
57
|
-
name: 'use-example',
|
|
58
|
-
message: 'would you like to start your new file from one of our examples?',
|
|
59
|
-
type: 'confirm',
|
|
60
|
-
default: true,
|
|
61
|
-
});
|
|
62
|
-
questions.push({
|
|
63
|
-
type: 'list',
|
|
64
|
-
name: 'selectedTemplate',
|
|
65
|
-
message: 'What example would you like to use?',
|
|
66
|
-
choices: examples,
|
|
67
|
-
when: (answers) => {
|
|
68
|
-
return answers['use-example'];
|
|
69
|
-
},
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
if (openStudio === undefined) {
|
|
73
|
-
questions.push({
|
|
74
|
-
name: 'studio',
|
|
75
|
-
message: 'open in Studio?',
|
|
76
|
-
type: 'confirm',
|
|
77
|
-
default: true,
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
if (questions.length) {
|
|
81
|
-
const answers = await inquirer.prompt(questions);
|
|
35
|
+
runInteractive() {
|
|
36
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
37
|
+
const { flags } = yield this.parse(New); // NOSONAR
|
|
38
|
+
let fileName = flags['file-name'];
|
|
39
|
+
let selectedTemplate = flags['example'];
|
|
40
|
+
let openStudio = flags.studio;
|
|
41
|
+
let examples = [];
|
|
42
|
+
const questions = [];
|
|
82
43
|
if (!fileName) {
|
|
83
|
-
|
|
44
|
+
questions.push({
|
|
45
|
+
name: 'filename',
|
|
46
|
+
message: 'name of the file?',
|
|
47
|
+
type: 'input',
|
|
48
|
+
default: DEFAULT_ASYNCAPI_FILE_NAME,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
const exampleFiles = yield readFile((0, path_1.resolve)(__dirname, '../../assets/examples/examples.json'), { encoding: 'utf8' });
|
|
53
|
+
examples = JSON.parse(exampleFiles);
|
|
84
54
|
}
|
|
85
|
-
|
|
86
|
-
|
|
55
|
+
catch (error) {
|
|
56
|
+
// no examples found
|
|
57
|
+
}
|
|
58
|
+
if (!selectedTemplate && examples.length > 0) {
|
|
59
|
+
questions.push({
|
|
60
|
+
name: 'use-example',
|
|
61
|
+
message: 'would you like to start your new file from one of our examples?',
|
|
62
|
+
type: 'confirm',
|
|
63
|
+
default: true,
|
|
64
|
+
});
|
|
65
|
+
questions.push({
|
|
66
|
+
type: 'list',
|
|
67
|
+
name: 'selectedTemplate',
|
|
68
|
+
message: 'What example would you like to use?',
|
|
69
|
+
choices: examples,
|
|
70
|
+
when: (answers) => {
|
|
71
|
+
return answers['use-example'];
|
|
72
|
+
},
|
|
73
|
+
});
|
|
87
74
|
}
|
|
88
75
|
if (openStudio === undefined) {
|
|
89
|
-
|
|
76
|
+
questions.push({
|
|
77
|
+
name: 'studio',
|
|
78
|
+
message: 'open in Studio?',
|
|
79
|
+
type: 'confirm',
|
|
80
|
+
default: true,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
if (questions.length) {
|
|
84
|
+
const answers = yield inquirer.prompt(questions);
|
|
85
|
+
if (!fileName) {
|
|
86
|
+
fileName = answers.filename;
|
|
87
|
+
}
|
|
88
|
+
if (!selectedTemplate) {
|
|
89
|
+
selectedTemplate = answers.selectedTemplate;
|
|
90
|
+
}
|
|
91
|
+
if (openStudio === undefined) {
|
|
92
|
+
openStudio = answers.studio;
|
|
93
|
+
}
|
|
90
94
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
95
|
+
fileName = fileName || DEFAULT_ASYNCAPI_FILE_NAME;
|
|
96
|
+
selectedTemplate = selectedTemplate || DEFAULT_ASYNCAPI_TEMPLATE;
|
|
97
|
+
yield this.createAsyncapiFile(fileName, selectedTemplate);
|
|
98
|
+
if (openStudio) {
|
|
99
|
+
(0, Studio_1.start)(fileName, flags.port || Studio_1.DEFAULT_PORT);
|
|
100
|
+
}
|
|
101
|
+
});
|
|
98
102
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
103
|
+
createAsyncapiFile(fileName, selectedTemplate) {
|
|
104
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
105
|
+
const asyncApiFile = yield readFile((0, path_1.resolve)(__dirname, '../../assets/examples/', selectedTemplate), { encoding: 'utf8' });
|
|
106
|
+
const fileNameHasFileExtension = fileName.includes('.');
|
|
107
|
+
const fileNameToWriteToDisk = fileNameHasFileExtension ? fileName : `${fileName}.yaml`;
|
|
108
|
+
try {
|
|
109
|
+
const content = yield readFile(fileNameToWriteToDisk, { encoding: 'utf8' });
|
|
110
|
+
if (content !== '') {
|
|
111
|
+
console.log(`File ${fileNameToWriteToDisk} already exists. Ignoring...`);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
catch (e) {
|
|
116
|
+
// File does not exist. Proceed creating it...
|
|
108
117
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
}
|
|
113
|
-
await writeFile(fileNameToWriteToDisk, asyncApiFile, { encoding: 'utf8' });
|
|
114
|
-
console.log(`Created file ${fileNameToWriteToDisk}...`);
|
|
118
|
+
yield writeFile(fileNameToWriteToDisk, asyncApiFile, { encoding: 'utf8' });
|
|
119
|
+
console.log(`Created file ${fileNameToWriteToDisk}...`);
|
|
120
|
+
});
|
|
115
121
|
}
|
|
116
122
|
}
|
|
117
123
|
exports.default = New;
|
|
@@ -4,9 +4,11 @@ const tslib_1 = require("tslib");
|
|
|
4
4
|
const base_1 = tslib_1.__importDefault(require("../../base"));
|
|
5
5
|
const core_1 = require("@oclif/core");
|
|
6
6
|
class Start extends base_1.default {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
run() {
|
|
8
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
9
|
+
const help = new core_1.Help(this.config);
|
|
10
|
+
help.showHelp(['start', '--help']);
|
|
11
|
+
});
|
|
10
12
|
}
|
|
11
13
|
}
|
|
12
14
|
exports.default = Start;
|