@opra/cli 1.0.0-alpha.29 → 1.0.0-alpha.31
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/cjs/oprimp-cli.js +2 -0
- package/cjs/ts-generator/generators/generate-data-type.js +1 -1
- package/cjs/ts-generator/generators/generate-document.js +5 -2
- package/cjs/ts-generator/ts-file.js +13 -7
- package/cjs/ts-generator/ts-generator.js +2 -2
- package/esm/oprimp-cli.js +2 -0
- package/esm/ts-generator/generators/generate-data-type.js +1 -1
- package/esm/ts-generator/generators/generate-document.js +6 -3
- package/esm/ts-generator/ts-file.js +13 -7
- package/esm/ts-generator/ts-generator.js +2 -2
- package/package.json +5 -4
- package/types/ts-generator/ts-file.d.ts +6 -2
- package/types/ts-generator/ts-generator.d.ts +5 -2
package/cjs/oprimp-cli.js
CHANGED
|
@@ -16,6 +16,7 @@ commander_1.program
|
|
|
16
16
|
.argument('<serviceUrl>', 'OPRA service url')
|
|
17
17
|
.argument('<outDir>', 'Output directory')
|
|
18
18
|
.option('--ext', 'Adds js extension to imports')
|
|
19
|
+
.option('--refns', 'Exports references with namespaces')
|
|
19
20
|
.option('--no-color', 'Disables colors in logs messages')
|
|
20
21
|
.action(async (serviceUrl, outDir, options) => {
|
|
21
22
|
if (!options.color)
|
|
@@ -25,6 +26,7 @@ commander_1.program
|
|
|
25
26
|
logger: console,
|
|
26
27
|
outDir,
|
|
27
28
|
importExt: options.ext,
|
|
29
|
+
referenceNamespaces: options.refns,
|
|
28
30
|
fileHeader: `/* Generated by OPRA Code Generator, Version ${pkgJson.version} */`,
|
|
29
31
|
});
|
|
30
32
|
await generator
|
|
@@ -45,7 +45,7 @@ async function generateDataType(dataType, intent, currentFile) {
|
|
|
45
45
|
file.exportTypes.push(typeName);
|
|
46
46
|
const typesIndexTs = this.addFile(path_1.default.join(this._typesRoot, 'index.ts'), true);
|
|
47
47
|
const indexTs = this.addFile('/index.ts', true);
|
|
48
|
-
indexTs.addExport(typesIndexTs.filename);
|
|
48
|
+
indexTs.addExport(typesIndexTs.filename, undefined, this._typesNamespace);
|
|
49
49
|
const codeBlock = (file.code['type_' + typeName] = new code_block_js_1.CodeBlock());
|
|
50
50
|
codeBlock.head = `/**\n * ${(0, string_utils_js_1.wrapJSDocString)(dataType.description || '')}\n *`;
|
|
51
51
|
codeBlock.head += `
|
|
@@ -29,13 +29,16 @@ async function generateDocument(document, options) {
|
|
|
29
29
|
this._documentsMap.set(document.id, out);
|
|
30
30
|
this.emit('log', chalk_1.default.white('[' + document.id + '] ') + chalk_1.default.cyan('Processing document ') + chalk_1.default.magenta(document.info.title));
|
|
31
31
|
if (document.references.size) {
|
|
32
|
+
let refIdGenerator = options?.refIdGenerator || 1;
|
|
32
33
|
this.emit('log', chalk_1.default.white('[' + document.id + '] ') + chalk_1.default.cyan(`Processing references`));
|
|
33
34
|
for (const ref of document.references.values()) {
|
|
34
35
|
const generator = this.extend();
|
|
35
36
|
generator._document = ref;
|
|
36
|
-
|
|
37
|
+
const typesNamespace = ref.api?.name || (ref.info.title ? (0, putil_varhelpers_1.pascalCase)(ref.info.title) : `Reference${refIdGenerator++}`);
|
|
38
|
+
generator._documentRoot = '/references/' + typesNamespace;
|
|
37
39
|
generator._typesRoot = path_1.default.join(generator._documentRoot, 'models');
|
|
38
|
-
|
|
40
|
+
generator._typesNamespace = !this.options.referenceNamespaces || ref[common_1.BUILTIN] ? '' : typesNamespace;
|
|
41
|
+
await generator.generateDocument(ref, { typesOnly: true, refIdGenerator });
|
|
39
42
|
}
|
|
40
43
|
}
|
|
41
44
|
this._fileHeaderDocInfo = `/*
|
|
@@ -35,7 +35,7 @@ class TsFile {
|
|
|
35
35
|
imp.items.push(x);
|
|
36
36
|
});
|
|
37
37
|
}
|
|
38
|
-
addExport(filename, types) {
|
|
38
|
+
addExport(filename, types, namespace) {
|
|
39
39
|
if (isLocalFile(filename)) {
|
|
40
40
|
filename = path_1.default.relative(this.dirname, path_1.default.resolve(this.dirname, filename));
|
|
41
41
|
if (!filename.startsWith('.'))
|
|
@@ -45,10 +45,11 @@ class TsFile {
|
|
|
45
45
|
filename = filename.substring(0, filename.length - 5);
|
|
46
46
|
if (filename.endsWith('.ts') || filename.endsWith('.js'))
|
|
47
47
|
filename = filename.substring(0, filename.length - 3);
|
|
48
|
-
|
|
48
|
+
const key = (namespace ? namespace + ':' : '') + filename;
|
|
49
|
+
this.exportFiles[key] = this.exportFiles[key] || { filename, items: [], namespace };
|
|
49
50
|
types?.forEach(x => {
|
|
50
|
-
if (!this.exportFiles[filename].includes(x))
|
|
51
|
-
this.exportFiles[filename].push(x);
|
|
51
|
+
if (!this.exportFiles[filename].items.includes(x))
|
|
52
|
+
this.exportFiles[filename].items.push(x);
|
|
52
53
|
});
|
|
53
54
|
}
|
|
54
55
|
generate(options) {
|
|
@@ -76,13 +77,18 @@ class TsFile {
|
|
|
76
77
|
.join('\n');
|
|
77
78
|
this.code.exports = Object.keys(this.exportFiles)
|
|
78
79
|
.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()))
|
|
79
|
-
.map(
|
|
80
|
-
const
|
|
80
|
+
.map(key => {
|
|
81
|
+
const exportFile = this.exportFiles[key];
|
|
82
|
+
const types = exportFile.items;
|
|
83
|
+
let filename = exportFile.filename;
|
|
81
84
|
if (!isPackageName(filename)) {
|
|
82
85
|
if (options?.importExt)
|
|
83
86
|
filename += '.js';
|
|
84
87
|
}
|
|
85
|
-
|
|
88
|
+
let out = `export ${types.length ? '{ ' + types.join(', ') + ' }' : '*'}`;
|
|
89
|
+
if (exportFile.namespace)
|
|
90
|
+
out += ` as ${exportFile.namespace}`;
|
|
91
|
+
return out + ` from '${filename}';`;
|
|
86
92
|
})
|
|
87
93
|
.join('\n');
|
|
88
94
|
if (this.code.imports || this.code.exports)
|
|
@@ -24,14 +24,14 @@ class TsGenerator extends node_events_1.EventEmitter {
|
|
|
24
24
|
*/
|
|
25
25
|
constructor(init) {
|
|
26
26
|
super();
|
|
27
|
-
this._started = false;
|
|
28
27
|
this._files = {};
|
|
28
|
+
this._started = false;
|
|
29
29
|
this.serviceUrl = init.serviceUrl;
|
|
30
30
|
this.cwd = init.cwd || node_process_1.default.cwd();
|
|
31
31
|
this.outDir = init.outDir ? node_path_1.default.resolve(this.cwd, init.outDir) : this.cwd;
|
|
32
32
|
this.fileHeader = init.fileHeader || '';
|
|
33
33
|
this.writer = init.writer || new file_writer_js_1.FileWriter();
|
|
34
|
-
this.options = { importExt: !!init.importExt };
|
|
34
|
+
this.options = { importExt: !!init.importExt, referenceNamespaces: init.referenceNamespaces };
|
|
35
35
|
this._documentsMap = new Map();
|
|
36
36
|
this._filesMap = new WeakMap();
|
|
37
37
|
this.on('log', (message, ...args) => init.logger?.log?.(message, ...args));
|
package/esm/oprimp-cli.js
CHANGED
|
@@ -13,6 +13,7 @@ program
|
|
|
13
13
|
.argument('<serviceUrl>', 'OPRA service url')
|
|
14
14
|
.argument('<outDir>', 'Output directory')
|
|
15
15
|
.option('--ext', 'Adds js extension to imports')
|
|
16
|
+
.option('--refns', 'Exports references with namespaces')
|
|
16
17
|
.option('--no-color', 'Disables colors in logs messages')
|
|
17
18
|
.action(async (serviceUrl, outDir, options) => {
|
|
18
19
|
if (!options.color)
|
|
@@ -22,6 +23,7 @@ program
|
|
|
22
23
|
logger: console,
|
|
23
24
|
outDir,
|
|
24
25
|
importExt: options.ext,
|
|
26
|
+
referenceNamespaces: options.refns,
|
|
25
27
|
fileHeader: `/* Generated by OPRA Code Generator, Version ${pkgJson.version} */`,
|
|
26
28
|
});
|
|
27
29
|
await generator
|
|
@@ -35,7 +35,7 @@ export async function generateDataType(dataType, intent, currentFile) {
|
|
|
35
35
|
file.exportTypes.push(typeName);
|
|
36
36
|
const typesIndexTs = this.addFile(path.join(this._typesRoot, 'index.ts'), true);
|
|
37
37
|
const indexTs = this.addFile('/index.ts', true);
|
|
38
|
-
indexTs.addExport(typesIndexTs.filename);
|
|
38
|
+
indexTs.addExport(typesIndexTs.filename, undefined, this._typesNamespace);
|
|
39
39
|
const codeBlock = (file.code['type_' + typeName] = new CodeBlock());
|
|
40
40
|
codeBlock.head = `/**\n * ${wrapJSDocString(dataType.description || '')}\n *`;
|
|
41
41
|
codeBlock.head += `
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { OpraHttpClient } from '@opra/client';
|
|
2
|
-
import { HttpApi } from '@opra/common';
|
|
2
|
+
import { BUILTIN, HttpApi } from '@opra/common';
|
|
3
3
|
import chalk from 'chalk';
|
|
4
4
|
import path from 'path';
|
|
5
5
|
import { pascalCase } from 'putil-varhelpers';
|
|
@@ -25,13 +25,16 @@ export async function generateDocument(document, options) {
|
|
|
25
25
|
this._documentsMap.set(document.id, out);
|
|
26
26
|
this.emit('log', chalk.white('[' + document.id + '] ') + chalk.cyan('Processing document ') + chalk.magenta(document.info.title));
|
|
27
27
|
if (document.references.size) {
|
|
28
|
+
let refIdGenerator = options?.refIdGenerator || 1;
|
|
28
29
|
this.emit('log', chalk.white('[' + document.id + '] ') + chalk.cyan(`Processing references`));
|
|
29
30
|
for (const ref of document.references.values()) {
|
|
30
31
|
const generator = this.extend();
|
|
31
32
|
generator._document = ref;
|
|
32
|
-
|
|
33
|
+
const typesNamespace = ref.api?.name || (ref.info.title ? pascalCase(ref.info.title) : `Reference${refIdGenerator++}`);
|
|
34
|
+
generator._documentRoot = '/references/' + typesNamespace;
|
|
33
35
|
generator._typesRoot = path.join(generator._documentRoot, 'models');
|
|
34
|
-
|
|
36
|
+
generator._typesNamespace = !this.options.referenceNamespaces || ref[BUILTIN] ? '' : typesNamespace;
|
|
37
|
+
await generator.generateDocument(ref, { typesOnly: true, refIdGenerator });
|
|
35
38
|
}
|
|
36
39
|
}
|
|
37
40
|
this._fileHeaderDocInfo = `/*
|
|
@@ -31,7 +31,7 @@ export class TsFile {
|
|
|
31
31
|
imp.items.push(x);
|
|
32
32
|
});
|
|
33
33
|
}
|
|
34
|
-
addExport(filename, types) {
|
|
34
|
+
addExport(filename, types, namespace) {
|
|
35
35
|
if (isLocalFile(filename)) {
|
|
36
36
|
filename = path.relative(this.dirname, path.resolve(this.dirname, filename));
|
|
37
37
|
if (!filename.startsWith('.'))
|
|
@@ -41,10 +41,11 @@ export class TsFile {
|
|
|
41
41
|
filename = filename.substring(0, filename.length - 5);
|
|
42
42
|
if (filename.endsWith('.ts') || filename.endsWith('.js'))
|
|
43
43
|
filename = filename.substring(0, filename.length - 3);
|
|
44
|
-
|
|
44
|
+
const key = (namespace ? namespace + ':' : '') + filename;
|
|
45
|
+
this.exportFiles[key] = this.exportFiles[key] || { filename, items: [], namespace };
|
|
45
46
|
types?.forEach(x => {
|
|
46
|
-
if (!this.exportFiles[filename].includes(x))
|
|
47
|
-
this.exportFiles[filename].push(x);
|
|
47
|
+
if (!this.exportFiles[filename].items.includes(x))
|
|
48
|
+
this.exportFiles[filename].items.push(x);
|
|
48
49
|
});
|
|
49
50
|
}
|
|
50
51
|
generate(options) {
|
|
@@ -72,13 +73,18 @@ export class TsFile {
|
|
|
72
73
|
.join('\n');
|
|
73
74
|
this.code.exports = Object.keys(this.exportFiles)
|
|
74
75
|
.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()))
|
|
75
|
-
.map(
|
|
76
|
-
const
|
|
76
|
+
.map(key => {
|
|
77
|
+
const exportFile = this.exportFiles[key];
|
|
78
|
+
const types = exportFile.items;
|
|
79
|
+
let filename = exportFile.filename;
|
|
77
80
|
if (!isPackageName(filename)) {
|
|
78
81
|
if (options?.importExt)
|
|
79
82
|
filename += '.js';
|
|
80
83
|
}
|
|
81
|
-
|
|
84
|
+
let out = `export ${types.length ? '{ ' + types.join(', ') + ' }' : '*'}`;
|
|
85
|
+
if (exportFile.namespace)
|
|
86
|
+
out += ` as ${exportFile.namespace}`;
|
|
87
|
+
return out + ` from '${filename}';`;
|
|
82
88
|
})
|
|
83
89
|
.join('\n');
|
|
84
90
|
if (this.code.imports || this.code.exports)
|
|
@@ -20,14 +20,14 @@ export class TsGenerator extends EventEmitter {
|
|
|
20
20
|
*/
|
|
21
21
|
constructor(init) {
|
|
22
22
|
super();
|
|
23
|
-
this._started = false;
|
|
24
23
|
this._files = {};
|
|
24
|
+
this._started = false;
|
|
25
25
|
this.serviceUrl = init.serviceUrl;
|
|
26
26
|
this.cwd = init.cwd || process.cwd();
|
|
27
27
|
this.outDir = init.outDir ? path.resolve(this.cwd, init.outDir) : this.cwd;
|
|
28
28
|
this.fileHeader = init.fileHeader || '';
|
|
29
29
|
this.writer = init.writer || new FileWriter();
|
|
30
|
-
this.options = { importExt: !!init.importExt };
|
|
30
|
+
this.options = { importExt: !!init.importExt, referenceNamespaces: init.referenceNamespaces };
|
|
31
31
|
this._documentsMap = new Map();
|
|
32
32
|
this._filesMap = new WeakMap();
|
|
33
33
|
this.on('log', (message, ...args) => init.logger?.log?.(message, ...args));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/cli",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.31",
|
|
4
4
|
"description": "Opra CLI tools",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
@@ -32,13 +32,14 @@
|
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"@browsery/type-is": "^1.6.18-r3",
|
|
35
|
-
"@opra/client": "^1.0.0-alpha.
|
|
36
|
-
"@opra/common": "^1.0.0-alpha.
|
|
35
|
+
"@opra/client": "^1.0.0-alpha.31",
|
|
36
|
+
"@opra/common": "^1.0.0-alpha.31",
|
|
37
37
|
"chalk": "^5.3.0",
|
|
38
38
|
"commander": "^12.0.0",
|
|
39
39
|
"js-string-escape": "^1.0.1",
|
|
40
40
|
"putil-flattentext": "^2.1.1",
|
|
41
|
-
"putil-varhelpers": "^1.6.5"
|
|
41
|
+
"putil-varhelpers": "^1.6.5",
|
|
42
|
+
"tslib": "^2.6.3"
|
|
42
43
|
},
|
|
43
44
|
"type": "module",
|
|
44
45
|
"module": "./esm/index.js",
|
|
@@ -6,12 +6,16 @@ export declare class TsFile {
|
|
|
6
6
|
items: string[];
|
|
7
7
|
typeImport?: boolean;
|
|
8
8
|
}>;
|
|
9
|
-
exportFiles: Record<string,
|
|
9
|
+
exportFiles: Record<string, {
|
|
10
|
+
filename: string;
|
|
11
|
+
items: string[];
|
|
12
|
+
namespace?: string;
|
|
13
|
+
}>;
|
|
10
14
|
exportTypes: string[];
|
|
11
15
|
code: CodeBlock;
|
|
12
16
|
constructor(filename: string);
|
|
13
17
|
addImport(filename: string, items?: string[], typeImport?: boolean): void;
|
|
14
|
-
addExport(filename: string, types?: string[]): void;
|
|
18
|
+
addExport(filename: string, types?: string[], namespace?: string): void;
|
|
15
19
|
generate(options?: {
|
|
16
20
|
importExt?: boolean;
|
|
17
21
|
}): string;
|
|
@@ -20,6 +20,7 @@ export declare namespace TsGenerator {
|
|
|
20
20
|
writer?: IFileWriter;
|
|
21
21
|
fileHeader?: string;
|
|
22
22
|
importExt?: boolean;
|
|
23
|
+
referenceNamespaces?: boolean;
|
|
23
24
|
}
|
|
24
25
|
}
|
|
25
26
|
/**
|
|
@@ -37,13 +38,14 @@ export declare class TsGenerator extends EventEmitter {
|
|
|
37
38
|
protected _generateMixinTypeCode: typeof _generateMixinTypeCode;
|
|
38
39
|
protected generateHttpApi: typeof generateHttpApi;
|
|
39
40
|
protected generateHttpController: typeof generateHttpController;
|
|
40
|
-
protected _started: boolean;
|
|
41
|
-
protected _document?: ApiDocument;
|
|
42
41
|
protected _documentRoot: string;
|
|
43
42
|
protected _typesRoot: string;
|
|
43
|
+
protected _typesNamespace: string;
|
|
44
44
|
protected _apiPath: string;
|
|
45
45
|
protected _fileHeaderDocInfo: string;
|
|
46
46
|
protected _files: Record<string, TsFile>;
|
|
47
|
+
protected _started: boolean;
|
|
48
|
+
protected _document?: ApiDocument;
|
|
47
49
|
protected _documentsMap: Map<string, {
|
|
48
50
|
document: ApiDocument;
|
|
49
51
|
generator: TsGenerator;
|
|
@@ -55,6 +57,7 @@ export declare class TsGenerator extends EventEmitter {
|
|
|
55
57
|
readonly writer: IFileWriter;
|
|
56
58
|
readonly options: {
|
|
57
59
|
importExt: boolean;
|
|
60
|
+
referenceNamespaces?: boolean;
|
|
58
61
|
};
|
|
59
62
|
fileHeader: string;
|
|
60
63
|
/**
|