@opra/cli 1.22.0 → 1.22.2
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/cli",
|
|
3
|
-
"version": "1.22.
|
|
3
|
+
"version": "1.22.2",
|
|
4
4
|
"description": "Opra CLI tools",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
@@ -14,8 +14,8 @@
|
|
|
14
14
|
"tslib": "^2.8.1"
|
|
15
15
|
},
|
|
16
16
|
"peerDependencies": {
|
|
17
|
-
"@opra/client": "^1.22.
|
|
18
|
-
"@opra/common": "^1.22.
|
|
17
|
+
"@opra/client": "^1.22.2",
|
|
18
|
+
"@opra/common": "^1.22.2"
|
|
19
19
|
},
|
|
20
20
|
"type": "module",
|
|
21
21
|
"module": "./index.js",
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ArrayType, ComplexType, DataType, EnumType, MappedType, MixinType, SimpleType, UnionType } from '@opra/common';
|
|
2
|
+
import { CodeBlock } from '../../code-block.js';
|
|
2
3
|
import { TsFile } from '../ts-file.js';
|
|
3
4
|
import type { TsGenerator } from '../ts-generator';
|
|
4
5
|
type Intent = 'root' | 'extends' | 'typeDef';
|
|
@@ -17,33 +18,33 @@ export declare function generateDataType(this: TsGenerator, dataType: DataType,
|
|
|
17
18
|
/**
|
|
18
19
|
*
|
|
19
20
|
*/
|
|
20
|
-
export declare function _generateTypeCode(this: TsGenerator, currentFile: TsFile, dataType: DataType, intent?: Intent): Promise<
|
|
21
|
+
export declare function _generateTypeCode(this: TsGenerator, currentFile: TsFile, dataType: DataType, codeBlock: CodeBlock, intent?: Intent): Promise<void>;
|
|
21
22
|
/**
|
|
22
23
|
*
|
|
23
24
|
*/
|
|
24
|
-
export declare function _generateArrayTypeCode(this: TsGenerator, currentFile: TsFile, dataType: ArrayType, intent?: Intent): Promise<
|
|
25
|
+
export declare function _generateArrayTypeCode(this: TsGenerator, currentFile: TsFile, dataType: ArrayType, codeBlock: CodeBlock, intent?: Intent): Promise<void>;
|
|
25
26
|
/**
|
|
26
27
|
*
|
|
27
28
|
*/
|
|
28
|
-
export declare function _generateComplexTypeCode(this: TsGenerator, currentFile: TsFile, dataType: ComplexType, intent?: Intent): Promise<
|
|
29
|
+
export declare function _generateComplexTypeCode(this: TsGenerator, currentFile: TsFile, dataType: ComplexType, codeBlock: CodeBlock, intent?: Intent): Promise<void>;
|
|
29
30
|
/**
|
|
30
31
|
*
|
|
31
32
|
*/
|
|
32
|
-
export declare function _generateEnumTypeCode(this: TsGenerator, currentFile: TsFile, dataType: EnumType, intent?: Intent): Promise<
|
|
33
|
+
export declare function _generateEnumTypeCode(this: TsGenerator, currentFile: TsFile, dataType: EnumType, codeBlock: CodeBlock, intent?: Intent): Promise<void>;
|
|
33
34
|
/**
|
|
34
35
|
*
|
|
35
36
|
*/
|
|
36
|
-
export declare function _generateMappedTypeCode(this: TsGenerator, currentFile: TsFile, dataType: MappedType, intent?: Intent): Promise<
|
|
37
|
+
export declare function _generateMappedTypeCode(this: TsGenerator, currentFile: TsFile, dataType: MappedType, codeBlock: CodeBlock, intent?: Intent): Promise<void>;
|
|
37
38
|
/**
|
|
38
39
|
*
|
|
39
40
|
*/
|
|
40
|
-
export declare function _generateMixinTypeCode(this: TsGenerator, currentFile: TsFile, dataType: MixinType, intent?: Intent): Promise<
|
|
41
|
+
export declare function _generateMixinTypeCode(this: TsGenerator, currentFile: TsFile, dataType: MixinType, codeBlock: CodeBlock, intent?: Intent): Promise<void>;
|
|
41
42
|
/**
|
|
42
43
|
*
|
|
43
44
|
*/
|
|
44
|
-
export declare function _generateSimpleTypeCode(this: TsGenerator, currentFile: TsFile, dataType: SimpleType, intent?: Intent): Promise<
|
|
45
|
+
export declare function _generateSimpleTypeCode(this: TsGenerator, currentFile: TsFile, dataType: SimpleType, codeBlock: CodeBlock, intent?: Intent): Promise<void>;
|
|
45
46
|
/**
|
|
46
47
|
*
|
|
47
48
|
*/
|
|
48
|
-
export declare function _generateUnionTypeCode(this: TsGenerator, currentFile: TsFile, dataType: UnionType, intent?: Intent): Promise<
|
|
49
|
+
export declare function _generateUnionTypeCode(this: TsGenerator, currentFile: TsFile, dataType: UnionType, codeBlock: CodeBlock, intent?: Intent): Promise<void>;
|
|
49
50
|
export {};
|
|
@@ -46,13 +46,13 @@ export async function generateDataType(dataType, intent, currentFile) {
|
|
|
46
46
|
const indexTs = this.addFile('/index.ts', true);
|
|
47
47
|
indexTs.addExport(typesIndexTs.filename, undefined, this._typesNamespace);
|
|
48
48
|
const codeBlock = (file.code['type_' + typeName] = new CodeBlock());
|
|
49
|
-
codeBlock.
|
|
50
|
-
codeBlock.
|
|
51
|
-
* @url ${path.posix.join(doc.url || this.serviceUrl, '$schema', '#types/' + typeName)}
|
|
52
|
-
|
|
53
|
-
export `;
|
|
54
|
-
|
|
55
|
-
|
|
49
|
+
codeBlock.header_start = `/**\n`;
|
|
50
|
+
codeBlock.header = ` * ${wrapJSDocString(dataType.description || '')}\n`;
|
|
51
|
+
codeBlock.header += ` * @url ${path.posix.join(doc.url || this.serviceUrl, '$schema', '#types/' + typeName)}\n`;
|
|
52
|
+
codeBlock.header_end = `*/\n`;
|
|
53
|
+
codeBlock.type_start = `export `;
|
|
54
|
+
await this._generateTypeCode(file, dataType, codeBlock, 'root');
|
|
55
|
+
codeBlock.type_end = '\n\n';
|
|
56
56
|
typesIndexTs.addExport(file.filename);
|
|
57
57
|
if (currentFile)
|
|
58
58
|
currentFile.addImport(file.filename, [typeName]);
|
|
@@ -60,8 +60,9 @@ export `;
|
|
|
60
60
|
}
|
|
61
61
|
if (!currentFile)
|
|
62
62
|
throw new TypeError(`You must provide currentFile to generate data type`);
|
|
63
|
-
const
|
|
64
|
-
|
|
63
|
+
const codeBlock = new CodeBlock();
|
|
64
|
+
await this._generateTypeCode(currentFile, dataType, codeBlock, intent);
|
|
65
|
+
return { kind: 'embedded', code: codeBlock.toString() };
|
|
65
66
|
}
|
|
66
67
|
catch (e) {
|
|
67
68
|
e.message = `(${dataType.name}) ` + e.message;
|
|
@@ -71,30 +72,32 @@ export `;
|
|
|
71
72
|
/**
|
|
72
73
|
*
|
|
73
74
|
*/
|
|
74
|
-
export async function _generateTypeCode(currentFile, dataType, intent) {
|
|
75
|
+
export async function _generateTypeCode(currentFile, dataType, codeBlock, intent) {
|
|
75
76
|
if (intent === 'root' && !dataType.name) {
|
|
76
77
|
throw new TypeError(`Name required to generate data type code to root intent`);
|
|
77
78
|
}
|
|
78
79
|
if (dataType instanceof ArrayType) {
|
|
79
|
-
|
|
80
|
+
await this._generateArrayTypeCode(currentFile, dataType, codeBlock, intent);
|
|
81
|
+
return;
|
|
80
82
|
}
|
|
81
83
|
if (dataType instanceof ComplexType) {
|
|
82
|
-
|
|
84
|
+
await this._generateComplexTypeCode(currentFile, dataType, codeBlock, intent);
|
|
85
|
+
return;
|
|
83
86
|
}
|
|
84
87
|
if (dataType instanceof EnumType) {
|
|
85
|
-
return await this._generateEnumTypeCode(currentFile, dataType, intent);
|
|
88
|
+
return await this._generateEnumTypeCode(currentFile, dataType, codeBlock, intent);
|
|
86
89
|
}
|
|
87
90
|
if (dataType instanceof MappedType) {
|
|
88
|
-
return await this._generateMappedTypeCode(currentFile, dataType, intent);
|
|
91
|
+
return await this._generateMappedTypeCode(currentFile, dataType, codeBlock, intent);
|
|
89
92
|
}
|
|
90
93
|
if (dataType instanceof MixinType) {
|
|
91
|
-
return await this._generateMixinTypeCode(currentFile, dataType, intent);
|
|
94
|
+
return await this._generateMixinTypeCode(currentFile, dataType, codeBlock, intent);
|
|
92
95
|
}
|
|
93
96
|
if (dataType instanceof SimpleType) {
|
|
94
|
-
return await this._generateSimpleTypeCode(currentFile, dataType, intent);
|
|
97
|
+
return await this._generateSimpleTypeCode(currentFile, dataType, codeBlock, intent);
|
|
95
98
|
}
|
|
96
99
|
if (dataType instanceof UnionType) {
|
|
97
|
-
return await this._generateUnionTypeCode(currentFile, dataType, intent);
|
|
100
|
+
return await this._generateUnionTypeCode(currentFile, dataType, codeBlock, intent);
|
|
98
101
|
}
|
|
99
102
|
/* istanbul ignore next */
|
|
100
103
|
throw new TypeError(`${dataType.kind} data types can not be directly exported`);
|
|
@@ -102,7 +105,7 @@ export async function _generateTypeCode(currentFile, dataType, intent) {
|
|
|
102
105
|
/**
|
|
103
106
|
*
|
|
104
107
|
*/
|
|
105
|
-
export async function _generateArrayTypeCode(currentFile, dataType, intent) {
|
|
108
|
+
export async function _generateArrayTypeCode(currentFile, dataType, codeBlock, intent) {
|
|
106
109
|
if (intent === 'extends')
|
|
107
110
|
throw new TypeError('Array types can not be extended');
|
|
108
111
|
let out = '';
|
|
@@ -115,14 +118,13 @@ export async function _generateArrayTypeCode(currentFile, dataType, intent) {
|
|
|
115
118
|
}
|
|
116
119
|
else
|
|
117
120
|
out = x.typeName;
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
return `${out}[]`;
|
|
121
|
+
codeBlock.typeDef =
|
|
122
|
+
intent === 'root' ? `type ${dataType.name} = ${out}[]` : `${out}[]`;
|
|
121
123
|
}
|
|
122
124
|
/**
|
|
123
125
|
*
|
|
124
126
|
*/
|
|
125
|
-
export async function _generateComplexTypeCode(currentFile, dataType, intent) {
|
|
127
|
+
export async function _generateComplexTypeCode(currentFile, dataType, codeBlock, intent) {
|
|
126
128
|
let out = intent === 'root' ? `interface ${dataType.name} ` : '';
|
|
127
129
|
const ownFields = [...dataType.fields('*')].filter(f => f.origin === dataType);
|
|
128
130
|
if (dataType.base) {
|
|
@@ -135,39 +137,56 @@ export async function _generateComplexTypeCode(currentFile, dataType, intent) {
|
|
|
135
137
|
out += `extends ${baseDef} `;
|
|
136
138
|
else {
|
|
137
139
|
out += baseDef;
|
|
138
|
-
if (!ownFields.length)
|
|
139
|
-
|
|
140
|
+
if (!ownFields.length) {
|
|
141
|
+
codeBlock.typeDef = out;
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
140
144
|
out += ' & ';
|
|
141
145
|
}
|
|
142
146
|
}
|
|
143
147
|
out += '{\n\t';
|
|
144
148
|
let i = 0;
|
|
145
149
|
for (const field of ownFields) {
|
|
150
|
+
const fieldCode = new CodeBlock();
|
|
146
151
|
if (i++)
|
|
147
152
|
out += '\n';
|
|
148
153
|
// Print JSDoc
|
|
149
|
-
|
|
154
|
+
fieldCode.header_start = '/**\n';
|
|
155
|
+
fieldCode.header = '';
|
|
156
|
+
if (field.description)
|
|
157
|
+
fieldCode.header += ` * ${wrapJSDocString(field.description)}\n`;
|
|
158
|
+
fieldCode.header_attr = '';
|
|
150
159
|
if (field.default)
|
|
151
|
-
|
|
160
|
+
fieldCode.header_attr += ` * @default ` + field.default + '\n';
|
|
152
161
|
// if (field.format)
|
|
153
162
|
// jsDoc += ` * @format ` + field.format + '\n';
|
|
154
163
|
if (field.exclusive)
|
|
155
|
-
|
|
164
|
+
fieldCode.header_attr += ` * @exclusive\n`;
|
|
156
165
|
if (field.readonly)
|
|
157
|
-
|
|
166
|
+
fieldCode.header_attr += ` * @readonly\n`;
|
|
158
167
|
if (field.writeonly)
|
|
159
|
-
|
|
168
|
+
fieldCode.header_attr += ` * @writeonly\n`;
|
|
160
169
|
if (field.deprecated) {
|
|
161
|
-
|
|
170
|
+
fieldCode.header_attr +=
|
|
162
171
|
` * @deprecated ` +
|
|
163
172
|
(typeof field.deprecated === 'string' ? field.deprecated : '') +
|
|
164
173
|
'\n';
|
|
165
174
|
}
|
|
166
|
-
|
|
175
|
+
fieldCode.header_end = ' */\n';
|
|
176
|
+
if (field.type instanceof SimpleType && field.type.properties) {
|
|
177
|
+
let s = '';
|
|
178
|
+
for (const [k, v] of Object.entries(field.type.properties)) {
|
|
179
|
+
if (v != null)
|
|
180
|
+
s += ` * ${k}: ${v}\n`;
|
|
181
|
+
}
|
|
182
|
+
if (s)
|
|
183
|
+
fieldCode.header += ' * Attributes:\n' + s;
|
|
184
|
+
}
|
|
167
185
|
// Print field name
|
|
186
|
+
fieldCode.def = '';
|
|
168
187
|
if (field.readonly)
|
|
169
|
-
|
|
170
|
-
|
|
188
|
+
fieldCode.def += 'readonly ';
|
|
189
|
+
fieldCode.def += `${field.name}${field.required ? '' : '?'}: `;
|
|
171
190
|
let typ = '';
|
|
172
191
|
if (field.fixed) {
|
|
173
192
|
const t = typeof field.fixed;
|
|
@@ -178,19 +197,22 @@ export async function _generateComplexTypeCode(currentFile, dataType, intent) {
|
|
|
178
197
|
typ = x.kind === 'embedded' ? x.code : x.typeName;
|
|
179
198
|
}
|
|
180
199
|
if (field.isArray && !(field.type instanceof ArrayType)) {
|
|
181
|
-
|
|
200
|
+
fieldCode.def += /[a-zA-Z]\w*/.test(typ)
|
|
201
|
+
? typ + '[];\n'
|
|
202
|
+
: '(' + typ + ')[];\n';
|
|
182
203
|
}
|
|
183
204
|
else
|
|
184
|
-
|
|
205
|
+
fieldCode.def += typ + ';\n';
|
|
206
|
+
out += fieldCode.toString();
|
|
185
207
|
}
|
|
186
208
|
if (dataType.additionalFields)
|
|
187
209
|
out += '[key: string]: any;\n';
|
|
188
|
-
|
|
210
|
+
codeBlock.typeDef = out + '\b}';
|
|
189
211
|
}
|
|
190
212
|
/**
|
|
191
213
|
*
|
|
192
214
|
*/
|
|
193
|
-
export async function _generateEnumTypeCode(currentFile, dataType, intent) {
|
|
215
|
+
export async function _generateEnumTypeCode(currentFile, dataType, codeBlock, intent) {
|
|
194
216
|
if (intent === 'root') {
|
|
195
217
|
let out = `enum ${dataType.name} {\n\t`;
|
|
196
218
|
for (const [value, info] of Object.entries(dataType.attributes)) {
|
|
@@ -207,18 +229,20 @@ export async function _generateEnumTypeCode(currentFile, dataType, intent) {
|
|
|
207
229
|
: "'" + String(value).replace("'", "\\'") + "'");
|
|
208
230
|
out += ',\n\n';
|
|
209
231
|
}
|
|
210
|
-
|
|
232
|
+
codeBlock.typeDef = out + '\b}';
|
|
233
|
+
return;
|
|
211
234
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
.
|
|
215
|
-
|
|
216
|
-
|
|
235
|
+
codeBlock.typeDef =
|
|
236
|
+
'(' +
|
|
237
|
+
Object.keys(dataType.attributes)
|
|
238
|
+
.map(t => `'${t}'`)
|
|
239
|
+
.join(' | ') +
|
|
240
|
+
')';
|
|
217
241
|
}
|
|
218
242
|
/**
|
|
219
243
|
*
|
|
220
244
|
*/
|
|
221
|
-
export async function _generateMappedTypeCode(currentFile, dataType, intent) {
|
|
245
|
+
export async function _generateMappedTypeCode(currentFile, dataType, codeBlock, intent) {
|
|
222
246
|
let out = intent === 'root' ? `type ${dataType.name} = ` : '';
|
|
223
247
|
const base = await this.generateDataType(dataType.base, 'typeDef', currentFile);
|
|
224
248
|
const typeDef = base.kind === 'embedded' ? base.code : base.typeName;
|
|
@@ -232,8 +256,10 @@ export async function _generateMappedTypeCode(currentFile, dataType, intent) {
|
|
|
232
256
|
(Array.isArray(dataType.required) && dataType.required.length > 0)
|
|
233
257
|
? dataType.required
|
|
234
258
|
: undefined;
|
|
235
|
-
if (!(pick || omit || partial || required))
|
|
236
|
-
|
|
259
|
+
if (!(pick || omit || partial || required)) {
|
|
260
|
+
codeBlock.typeDef = typeDef;
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
237
263
|
if (partial === true)
|
|
238
264
|
out += 'Partial<';
|
|
239
265
|
else if (partial) {
|
|
@@ -271,12 +297,12 @@ export async function _generateMappedTypeCode(currentFile, dataType, intent) {
|
|
|
271
297
|
}
|
|
272
298
|
out += '>';
|
|
273
299
|
}
|
|
274
|
-
|
|
300
|
+
codeBlock.typeDef = out;
|
|
275
301
|
}
|
|
276
302
|
/**
|
|
277
303
|
*
|
|
278
304
|
*/
|
|
279
|
-
export async function _generateMixinTypeCode(currentFile, dataType, intent) {
|
|
305
|
+
export async function _generateMixinTypeCode(currentFile, dataType, codeBlock, intent) {
|
|
280
306
|
const outArray = [];
|
|
281
307
|
for (const t of dataType.types) {
|
|
282
308
|
const x = await this.generateDataType(t, 'typeDef', currentFile);
|
|
@@ -288,25 +314,37 @@ export async function _generateMixinTypeCode(currentFile, dataType, intent) {
|
|
|
288
314
|
else
|
|
289
315
|
outArray.push(x.typeName);
|
|
290
316
|
}
|
|
291
|
-
if (intent === 'root')
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
317
|
+
if (intent === 'root') {
|
|
318
|
+
codeBlock.typeDef = `type ${dataType.name} = ${outArray.join(' & ')}`;
|
|
319
|
+
}
|
|
320
|
+
else if (intent === 'extends') {
|
|
321
|
+
codeBlock.typeDef = outArray.join(', ');
|
|
322
|
+
}
|
|
323
|
+
else
|
|
324
|
+
codeBlock.typeDef = outArray.join(' & ');
|
|
296
325
|
}
|
|
297
326
|
/**
|
|
298
327
|
*
|
|
299
328
|
*/
|
|
300
|
-
export async function _generateSimpleTypeCode(currentFile, dataType, intent) {
|
|
329
|
+
export async function _generateSimpleTypeCode(currentFile, dataType, codeBlock, intent) {
|
|
330
|
+
let s = '';
|
|
331
|
+
if (dataType.properties) {
|
|
332
|
+
for (const [k, v] of Object.entries(dataType.properties)) {
|
|
333
|
+
if (v != null)
|
|
334
|
+
s += ` * ${k}: ${v}\n`;
|
|
335
|
+
}
|
|
336
|
+
if (s)
|
|
337
|
+
codeBlock.header += ' * Attributes:\n' + s;
|
|
338
|
+
}
|
|
301
339
|
let out = intent === 'root' ? `type ${dataType.name} = ` : '';
|
|
302
340
|
const nameMapping = dataType.nameMappings.js || 'any';
|
|
303
341
|
out += nameMapping === 'Date' ? 'string' : nameMapping;
|
|
304
|
-
|
|
342
|
+
codeBlock.typeDef = intent === 'root' ? out + ';' : out;
|
|
305
343
|
}
|
|
306
344
|
/**
|
|
307
345
|
*
|
|
308
346
|
*/
|
|
309
|
-
export async function _generateUnionTypeCode(currentFile, dataType, intent) {
|
|
347
|
+
export async function _generateUnionTypeCode(currentFile, dataType, codeBlock, intent) {
|
|
310
348
|
if (intent === 'extends')
|
|
311
349
|
throw new TypeError('Union types can not be extended');
|
|
312
350
|
const outArray = [];
|
|
@@ -321,6 +359,7 @@ export async function _generateUnionTypeCode(currentFile, dataType, intent) {
|
|
|
321
359
|
outArray.push(x.typeName);
|
|
322
360
|
}
|
|
323
361
|
if (intent === 'root')
|
|
324
|
-
|
|
325
|
-
|
|
362
|
+
codeBlock.typeDef = `type ${dataType.name} = ${outArray.join(' | ')}`;
|
|
363
|
+
else
|
|
364
|
+
codeBlock.typeDef = outArray.join(' | ');
|
|
326
365
|
}
|
|
@@ -41,6 +41,7 @@ export class TsGenerator extends EventEmitter {
|
|
|
41
41
|
referenceNamespaces: init.referenceNamespaces,
|
|
42
42
|
};
|
|
43
43
|
this._documentsMap = new Map();
|
|
44
|
+
this._documentRoot = './';
|
|
44
45
|
this._filesMap = new WeakMap();
|
|
45
46
|
this.on('log', (message, ...args) => init.logger?.log?.(message, ...args));
|
|
46
47
|
this.on('error', (message, ...args) => init.logger?.error?.(message, ...args));
|