-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathmodels.ts
197 lines (187 loc) · 6.55 KB
/
models.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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
194
195
196
197
import { CSharpFileGenerator, JavaFileGenerator, JavaScriptFileGenerator, TypeScriptFileGenerator, GoFileGenerator, Logger, DartFileGenerator, PythonFileGenerator, RustFileGenerator } from '@asyncapi/modelina';
import { Flags } from '@oclif/core';
import Command from '../../base';
import { load } from '../../models/SpecificationFile';
import { parse, validationFlags } from '../../parser';
import type { AbstractGenerator, AbstractFileGenerator } from '@asyncapi/modelina';
enum Languages {
typescript = 'typescript',
csharp = 'csharp',
golang = 'golang',
java = 'java',
javascript = 'javascript',
dart = 'dart',
python = 'python',
rust = 'rust'
}
const possibleLanguageValues = Object.values(Languages).join(', ');
export default class Models extends Command {
static description = 'Generates typed models';
static args = [
{
name: 'language',
description: 'The language you want the typed models generated for.',
options: Object.keys(Languages),
required: true
},
{ name: 'file', description: 'Path or URL to the AsyncAPI document, or context-name', required: true },
];
static flags = {
help: Flags.help({ char: 'h' }),
output: Flags.string({
char: 'o',
description: 'The output directory where the models should be written to. Omitting this flag will write the models to `stdout`.',
required: false
}),
/**
* TypeScript specific options
*/
tsModelType: Flags.string({
type: 'option',
options: ['class', 'interface'],
description: 'TypeScript specific, define which type of model needs to be generated.',
required: false,
default: 'class',
}),
tsEnumType: Flags.string({
type: 'option',
options: ['enum', 'union'],
description: 'TypeScript specific, define which type of enums needs to be generated.',
required: false,
default: 'enum',
}),
tsModuleSystem: Flags.string({
type: 'option',
options: ['ESM', 'CJS'],
description: 'TypeScript specific, define the module system to be used.',
required: false,
default: 'ESM',
}),
tsExportType: Flags.string({
type: 'option',
options: ['default', 'named'],
description: 'TypeScript specific, define which type of export needs to be generated.',
required: false,
default: 'default',
}),
/**
* Go and Java specific package name to use for the generated models
*/
packageName: Flags.string({
description: 'Go and Java specific, define the package to use for the generated models. This is required when language is `go` or `java`.',
required: false
}),
/**
* C# specific options
*/
namespace: Flags.string({
description: 'C# specific, define the namespace to use for the generated models. This is required when language is `csharp`.',
required: false
}),
...validationFlags({ logDiagnostics: false }),
};
async run() {
const { args, flags } = await this.parse(Models);
const { tsModelType, tsEnumType, tsModuleSystem, tsExportType, namespace, packageName, output } = flags;
const { language, file } = args;
const inputFile = (await load(file)) || (await load());
const { document, status } = await parse(this, inputFile, flags);
if (!document || status === 'invalid') {
return;
}
Logger.setLogger({
info: (message) => {
this.log(message);
},
debug: (message) => {
this.debug(message);
},
warn: (message) => {
this.warn(message);
},
error: (message) => {
this.error(message);
},
});
let fileGenerator: AbstractGenerator<any, any> & AbstractFileGenerator<any>;
let fileOptions: any = {};
switch (language) {
case Languages.typescript:
fileGenerator = new TypeScriptFileGenerator({
modelType: tsModelType as 'class' | 'interface',
enumType: tsEnumType as 'enum' | 'union',
});
fileOptions = {
moduleSystem: tsModuleSystem,
exportType: tsExportType
};
break;
case Languages.python:
fileGenerator = new PythonFileGenerator();
break;
case Languages.rust:
fileGenerator = new RustFileGenerator();
break;
case Languages.csharp:
if (namespace === undefined) {
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.');
}
fileGenerator = new CSharpFileGenerator();
fileOptions = {
namespace
};
break;
case Languages.golang:
if (packageName === undefined) {
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.');
}
fileGenerator = new GoFileGenerator();
fileOptions = {
packageName
};
break;
case Languages.java:
if (packageName === undefined) {
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.');
}
fileGenerator = new JavaFileGenerator();
fileOptions = {
packageName
};
break;
case Languages.javascript:
fileGenerator = new JavaScriptFileGenerator();
break;
case Languages.dart:
if (packageName === undefined) {
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.');
}
fileGenerator = new DartFileGenerator();
fileOptions = {
packageName
};
break;
default:
throw new Error(`Could not determine generator for language ${language}, are you using one of the following values ${possibleLanguageValues}?`);
}
if (output) {
const models = await fileGenerator.generateToFiles(
document as any,
output,
{ ...fileOptions, } as any);
const generatedModels = models.map((model) => { return model.modelName; });
this.log(`Successfully generated the following models: ${generatedModels.join(', ')}`);
return;
}
const models = await fileGenerator.generateCompleteModels(
document as any,
{ ...fileOptions } as any);
const generatedModels = models.map((model) => {
return `
## Model name: ${model.modelName}
${model.result}
`;
});
this.log(`Successfully generated the following models: ${generatedModels.join('\n')}`);
}
}