@opra/cli 0.15.0 → 0.16.1
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/esm/api-exporter/api-exporter.js +46 -41
- package/esm/api-exporter/file-writer.js +8 -3
- package/esm/api-exporter/index.js +4 -1
- package/esm/api-exporter/process-resources.js +16 -11
- package/esm/api-exporter/process-types.js +40 -28
- package/esm/api-exporter/ts-file.js +24 -17
- package/esm/index.js +4 -1
- package/esm/interfaces/file-writer.interface.js +2 -1
- package/esm/interfaces/logger.interface.js +2 -1
- package/esm/interfaces/service-generation-context.interface.js +2 -1
- package/esm/oprimp-cli.js +18 -15
- package/esm/utils/get-caller-file.util.js +5 -1
- package/esm/utils/string-utils.js +14 -6
- package/package.json +4 -3
- /package/{esm → types}/api-exporter/api-exporter.d.ts +0 -0
- /package/{esm → types}/api-exporter/file-writer.d.ts +0 -0
- /package/{esm → types}/api-exporter/index.d.ts +0 -0
- /package/{esm → types}/api-exporter/process-resources.d.ts +0 -0
- /package/{esm → types}/api-exporter/process-types.d.ts +0 -0
- /package/{esm → types}/api-exporter/ts-file.d.ts +0 -0
- /package/{esm → types}/index.d.ts +0 -0
- /package/{esm → types}/interfaces/file-writer.interface.d.ts +0 -0
- /package/{esm → types}/interfaces/logger.interface.d.ts +0 -0
- /package/{esm → types}/interfaces/service-generation-context.interface.d.ts +0 -0
- /package/{esm → types}/oprimp-cli.d.ts +0 -0
- /package/{esm → types}/utils/get-caller-file.util.d.ts +0 -0
- /package/{esm → types}/utils/string-utils.d.ts +0 -0
|
@@ -1,18 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ApiExporter = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const chalk_1 = tslib_1.__importDefault(require("chalk"));
|
|
6
|
+
const node_fs_1 = tslib_1.__importDefault(require("node:fs"));
|
|
7
|
+
const node_path_1 = tslib_1.__importDefault(require("node:path"));
|
|
8
|
+
const node_process_1 = tslib_1.__importDefault(require("node:process"));
|
|
9
|
+
const node_client_1 = require("@opra/node-client");
|
|
10
|
+
const file_writer_js_1 = require("./file-writer.js");
|
|
11
|
+
const process_resources_js_1 = require("./process-resources.js");
|
|
12
|
+
const process_types_js_1 = require("./process-types.js");
|
|
13
|
+
const ts_file_js_1 = require("./ts-file.js");
|
|
14
|
+
class ApiExporter {
|
|
11
15
|
constructor(config) {
|
|
12
16
|
this.files = {};
|
|
13
|
-
this.client = new OpraHttpClient(config.serviceUrl);
|
|
14
|
-
this.cwd = config.cwd ||
|
|
15
|
-
this.outDir =
|
|
17
|
+
this.client = new node_client_1.OpraHttpClient(config.serviceUrl);
|
|
18
|
+
this.cwd = config.cwd || node_process_1.default.cwd();
|
|
19
|
+
this.outDir = node_path_1.default.resolve(this.cwd, config.outDir);
|
|
16
20
|
this.logger = config.logger || {
|
|
17
21
|
log: () => void 0,
|
|
18
22
|
error: () => void 0,
|
|
@@ -22,13 +26,13 @@ export class ApiExporter {
|
|
|
22
26
|
};
|
|
23
27
|
this.fileHeader = config.fileHeader || '';
|
|
24
28
|
this.importExt = config.importExt || '';
|
|
25
|
-
this.writer = config.writer || new FileWriter();
|
|
29
|
+
this.writer = config.writer || new file_writer_js_1.FileWriter();
|
|
26
30
|
// this.nsMap = nsMap || new ResponsiveMap(); // implement references later
|
|
27
31
|
}
|
|
28
32
|
async execute() {
|
|
29
|
-
this.logger.log(
|
|
33
|
+
this.logger.log(chalk_1.default.yellow('Fetching service metadata from'), chalk_1.default.whiteBright(this.client.serviceUrl));
|
|
30
34
|
this.document = await this.client.getMetadata();
|
|
31
|
-
this.logger.log(
|
|
35
|
+
this.logger.log(chalk_1.default.yellow('Retrieved service info:\n'), chalk_1.default.white('Title:'), chalk_1.default.magenta(this.document.info.title), '\n', chalk_1.default.white('Version:'), chalk_1.default.magenta(this.document.info.version), '\n', chalk_1.default.white('Resources:'), chalk_1.default.magenta(this.document.resources.size), 'resources found\n', chalk_1.default.white('Types:'), chalk_1.default.magenta(this.document.types.size), 'types found\n');
|
|
32
36
|
this.name = (this.name || this.document.info.title || 'Service1').replace(/[^\w_$]*/g, '');
|
|
33
37
|
this.name = this.name.charAt(0).toUpperCase() + this.name.substring(1);
|
|
34
38
|
this.fileHeader += `/*
|
|
@@ -36,21 +40,21 @@ export class ApiExporter {
|
|
|
36
40
|
* Version ${this.document.info.version}
|
|
37
41
|
* ${this.client.serviceUrl}
|
|
38
42
|
*/`;
|
|
39
|
-
this.logger.log(
|
|
43
|
+
this.logger.log(chalk_1.default.yellow('Removing old files..'));
|
|
40
44
|
this.cleanDirectory(this.outDir);
|
|
41
|
-
this.logger.log(
|
|
42
|
-
|
|
45
|
+
this.logger.log(chalk_1.default.yellow(`Generating service interface ( ${chalk_1.default.whiteBright(this.name)} )`));
|
|
46
|
+
node_fs_1.default.mkdirSync(this.outDir, { recursive: true });
|
|
43
47
|
await this.processTypes();
|
|
44
48
|
await this.processResources();
|
|
45
49
|
// Write files
|
|
46
50
|
for (const file of Object.values(this.files)) {
|
|
47
|
-
const targetDir =
|
|
48
|
-
|
|
51
|
+
const targetDir = node_path_1.default.dirname(file.filename);
|
|
52
|
+
node_fs_1.default.mkdirSync(targetDir, { recursive: true });
|
|
49
53
|
await this.writer.writeFile(file.filename, file.generate({ importExt: this.importExt }));
|
|
50
54
|
}
|
|
51
55
|
}
|
|
52
56
|
getFile(filePath) {
|
|
53
|
-
return this.files[
|
|
57
|
+
return this.files[node_path_1.default.resolve(node_path_1.default.join(this.outDir, filePath))];
|
|
54
58
|
}
|
|
55
59
|
addFile(filePath, returnExists) {
|
|
56
60
|
let file = this.getFile(filePath);
|
|
@@ -59,27 +63,27 @@ export class ApiExporter {
|
|
|
59
63
|
return file;
|
|
60
64
|
throw new Error(`File "${filePath}" already exists`);
|
|
61
65
|
}
|
|
62
|
-
file = new TsFile(
|
|
66
|
+
file = new ts_file_js_1.TsFile(node_path_1.default.resolve(node_path_1.default.join(this.outDir, filePath)));
|
|
63
67
|
file.header = this.fileHeader;
|
|
64
68
|
this.files[file.filename] = file;
|
|
65
69
|
return file;
|
|
66
70
|
}
|
|
67
71
|
cleanDirectory(dirname) {
|
|
68
|
-
if (!
|
|
72
|
+
if (!node_fs_1.default.existsSync(dirname))
|
|
69
73
|
return;
|
|
70
|
-
const files =
|
|
74
|
+
const files = node_fs_1.default.readdirSync(dirname);
|
|
71
75
|
for (const f of files) {
|
|
72
|
-
const absolutePath =
|
|
73
|
-
if (
|
|
76
|
+
const absolutePath = node_path_1.default.join(dirname, f);
|
|
77
|
+
if (node_fs_1.default.statSync(absolutePath).isDirectory()) {
|
|
74
78
|
this.cleanDirectory(absolutePath);
|
|
75
|
-
if (!
|
|
76
|
-
|
|
79
|
+
if (!node_fs_1.default.readdirSync(absolutePath).length)
|
|
80
|
+
node_fs_1.default.rmdirSync(absolutePath);
|
|
77
81
|
continue;
|
|
78
82
|
}
|
|
79
|
-
if (
|
|
80
|
-
const contents =
|
|
83
|
+
if (node_path_1.default.extname(f) === '.ts') {
|
|
84
|
+
const contents = node_fs_1.default.readFileSync(absolutePath, 'utf-8');
|
|
81
85
|
if (contents.includes('#!oprimp_auto_generated!#')) {
|
|
82
|
-
|
|
86
|
+
node_fs_1.default.unlinkSync(absolutePath);
|
|
83
87
|
}
|
|
84
88
|
}
|
|
85
89
|
}
|
|
@@ -89,14 +93,15 @@ export class ApiExporter {
|
|
|
89
93
|
await exporter.execute();
|
|
90
94
|
}
|
|
91
95
|
}
|
|
96
|
+
exports.ApiExporter = ApiExporter;
|
|
92
97
|
(() => {
|
|
93
|
-
ApiExporter.prototype.processResources = processResources;
|
|
94
|
-
ApiExporter.prototype.processTypes = processTypes;
|
|
95
|
-
ApiExporter.prototype.generateTypeFile = generateTypeFile;
|
|
96
|
-
ApiExporter.prototype.generateComplexTypeDefinition = generateComplexTypeDefinition;
|
|
97
|
-
ApiExporter.prototype.generateSimpleTypeDefinition = generateSimpleTypeDefinition;
|
|
98
|
-
ApiExporter.prototype.resolveTypeNameOrDef = resolveTypeNameOrDef;
|
|
99
|
-
ApiExporter.prototype.generateEnumTypeDefinition = generateEnumTypeDefinition;
|
|
100
|
-
ApiExporter.prototype.generateUnionTypeDefinition = generateUnionTypeDefinition;
|
|
101
|
-
ApiExporter.prototype.generateMappedTypeDefinition = generateMappedTypeDefinition;
|
|
98
|
+
ApiExporter.prototype.processResources = process_resources_js_1.processResources;
|
|
99
|
+
ApiExporter.prototype.processTypes = process_types_js_1.processTypes;
|
|
100
|
+
ApiExporter.prototype.generateTypeFile = process_types_js_1.generateTypeFile;
|
|
101
|
+
ApiExporter.prototype.generateComplexTypeDefinition = process_types_js_1.generateComplexTypeDefinition;
|
|
102
|
+
ApiExporter.prototype.generateSimpleTypeDefinition = process_types_js_1.generateSimpleTypeDefinition;
|
|
103
|
+
ApiExporter.prototype.resolveTypeNameOrDef = process_types_js_1.resolveTypeNameOrDef;
|
|
104
|
+
ApiExporter.prototype.generateEnumTypeDefinition = process_types_js_1.generateEnumTypeDefinition;
|
|
105
|
+
ApiExporter.prototype.generateUnionTypeDefinition = process_types_js_1.generateUnionTypeDefinition;
|
|
106
|
+
ApiExporter.prototype.generateMappedTypeDefinition = process_types_js_1.generateMappedTypeDefinition;
|
|
102
107
|
})();
|
|
@@ -1,6 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FileWriter = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const fs_1 = tslib_1.__importDefault(require("fs"));
|
|
6
|
+
class FileWriter {
|
|
3
7
|
writeFile(filename, contents) {
|
|
4
|
-
|
|
8
|
+
fs_1.default.writeFileSync(filename, contents, 'utf-8');
|
|
5
9
|
}
|
|
6
10
|
}
|
|
11
|
+
exports.FileWriter = FileWriter;
|
|
@@ -1,24 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.processResources = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const chalk_1 = tslib_1.__importDefault(require("chalk"));
|
|
6
|
+
const node_path_1 = tslib_1.__importDefault(require("node:path"));
|
|
7
|
+
const common_1 = require("@opra/common");
|
|
8
|
+
const string_utils_js_1 = require("../utils/string-utils.js");
|
|
5
9
|
/**
|
|
6
10
|
*
|
|
7
11
|
* @param targetDir
|
|
8
12
|
*/
|
|
9
|
-
|
|
10
|
-
this.logger.log(
|
|
13
|
+
async function processResources(targetDir = '') {
|
|
14
|
+
this.logger.log(chalk_1.default.yellow('Processing resources'));
|
|
11
15
|
const { document } = this;
|
|
12
|
-
const serviceTs = this.addFile(
|
|
16
|
+
const serviceTs = this.addFile(node_path_1.default.join(targetDir, this.name + '.ts'));
|
|
13
17
|
serviceTs.addImportPackage('@opra/client', ['HttpServiceBase']);
|
|
14
18
|
const indexTs = this.addFile('/index.ts', true);
|
|
15
19
|
indexTs.addExportFile(serviceTs.filename);
|
|
16
20
|
serviceTs.content = `\nexport class ${this.name} extends HttpServiceBase {\n`;
|
|
17
21
|
for (const resource of document.resources.values()) {
|
|
18
|
-
serviceTs.content += `\n/**\n * ${wrapJSDocString(resource.description || resource.name)}
|
|
19
|
-
* @url ${joinPath(this.client.serviceUrl, '$metadata#resources/' + resource.name)}
|
|
22
|
+
serviceTs.content += `\n/**\n * ${(0, string_utils_js_1.wrapJSDocString)(resource.description || resource.name)}
|
|
23
|
+
* @url ${(0, common_1.joinPath)(this.client.serviceUrl, '$metadata#resources/' + resource.name)}
|
|
20
24
|
*/`;
|
|
21
|
-
if (resource instanceof Collection) {
|
|
25
|
+
if (resource instanceof common_1.Collection) {
|
|
22
26
|
const typeName = resource.type.name || '';
|
|
23
27
|
serviceTs.addImportPackage('@opra/client', ['HttpCollectionNode']);
|
|
24
28
|
serviceTs.addImportFile('types/' + typeName, [typeName]);
|
|
@@ -29,7 +33,7 @@ export async function processResources(targetDir = '') {
|
|
|
29
33
|
return this.$client.collection('${resource.name}');
|
|
30
34
|
}\n`;
|
|
31
35
|
}
|
|
32
|
-
else if (resource instanceof Singleton) {
|
|
36
|
+
else if (resource instanceof common_1.Singleton) {
|
|
33
37
|
const typeName = resource.type.name || '';
|
|
34
38
|
serviceTs.addImportPackage('@opra/client', ['HttpSingletonNode']);
|
|
35
39
|
serviceTs.addImportFile('types/' + typeName, [typeName]);
|
|
@@ -43,3 +47,4 @@ export async function processResources(targetDir = '') {
|
|
|
43
47
|
}
|
|
44
48
|
serviceTs.content += '}';
|
|
45
49
|
}
|
|
50
|
+
exports.processResources = processResources;
|
|
@@ -1,58 +1,63 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateMappedTypeDefinition = exports.generateUnionTypeDefinition = exports.generateEnumTypeDefinition = exports.generateSimpleTypeDefinition = exports.generateComplexTypeDefinition = exports.resolveTypeNameOrDef = exports.generateTypeFile = exports.processTypes = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const chalk_1 = tslib_1.__importDefault(require("chalk"));
|
|
6
|
+
const node_path_1 = tslib_1.__importDefault(require("node:path"));
|
|
7
|
+
const common_1 = require("@opra/common");
|
|
8
|
+
const string_utils_js_1 = require("../utils/string-utils.js");
|
|
5
9
|
const internalTypeNames = ['boolean', 'bigint', 'number', 'null', 'string'];
|
|
6
10
|
/**
|
|
7
11
|
*
|
|
8
12
|
* @param targetDir
|
|
9
13
|
*/
|
|
10
|
-
|
|
11
|
-
this.logger.log(
|
|
14
|
+
async function processTypes(targetDir = '') {
|
|
15
|
+
this.logger.log(chalk_1.default.yellow('Processing types'));
|
|
12
16
|
const { document } = this;
|
|
13
|
-
const typesTs = this.addFile(
|
|
17
|
+
const typesTs = this.addFile(node_path_1.default.join(targetDir, 'types.ts'));
|
|
14
18
|
for (const dataType of document.types.values()) {
|
|
15
19
|
const expFile = await this.generateTypeFile(dataType, targetDir);
|
|
16
20
|
typesTs.addExportFile(expFile.filename);
|
|
17
21
|
}
|
|
18
22
|
}
|
|
23
|
+
exports.processTypes = processTypes;
|
|
19
24
|
/**
|
|
20
25
|
*
|
|
21
26
|
* @param dataType
|
|
22
27
|
* @param targetDir
|
|
23
28
|
*/
|
|
24
|
-
|
|
29
|
+
async function generateTypeFile(dataType, targetDir = '') {
|
|
25
30
|
const typeName = dataType.name;
|
|
26
31
|
if (!typeName)
|
|
27
32
|
throw new TypeError(`DataType has no name`);
|
|
28
33
|
let filePath;
|
|
29
|
-
if (dataType instanceof SimpleType)
|
|
34
|
+
if (dataType instanceof common_1.SimpleType)
|
|
30
35
|
filePath = '/simple-types.ts';
|
|
31
|
-
else if (dataType instanceof ComplexType)
|
|
36
|
+
else if (dataType instanceof common_1.ComplexType)
|
|
32
37
|
filePath = `/types/${typeName}.ts`;
|
|
33
|
-
else if (dataType instanceof EnumType) {
|
|
38
|
+
else if (dataType instanceof common_1.EnumType) {
|
|
34
39
|
filePath = `/enums/${typeName}.ts`;
|
|
35
40
|
}
|
|
36
41
|
else
|
|
37
42
|
throw new TypeError(`Unimplemented DataType (${dataType.kind})`);
|
|
38
|
-
const file = this.addFile(
|
|
43
|
+
const file = this.addFile(node_path_1.default.join(targetDir, filePath), true);
|
|
39
44
|
if (file.exportTypes.includes(typeName))
|
|
40
45
|
return file;
|
|
41
46
|
file.exportTypes.push(typeName);
|
|
42
47
|
const indexTs = this.addFile('/index.ts', true);
|
|
43
48
|
indexTs.addExportFile(file.filename);
|
|
44
|
-
file.content += `\n/**\n * ${wrapJSDocString(dataType.description || typeName)}
|
|
49
|
+
file.content += `\n/**\n * ${(0, string_utils_js_1.wrapJSDocString)(dataType.description || typeName)}
|
|
45
50
|
* @type ${typeName}
|
|
46
51
|
* @kind ${dataType.kind}
|
|
47
|
-
* @url ${joinPath(this.client.serviceUrl, '$metadata#types/' + typeName)}
|
|
52
|
+
* @url ${(0, common_1.joinPath)(this.client.serviceUrl, '$metadata#types/' + typeName)}
|
|
48
53
|
*/\n`;
|
|
49
|
-
if (dataType instanceof SimpleType) {
|
|
54
|
+
if (dataType instanceof common_1.SimpleType) {
|
|
50
55
|
file.content += `export type ${typeName} = ` + await this.generateSimpleTypeDefinition(file, dataType);
|
|
51
56
|
}
|
|
52
|
-
else if (dataType instanceof EnumType) {
|
|
57
|
+
else if (dataType instanceof common_1.EnumType) {
|
|
53
58
|
file.content += `export enum ${typeName} ` + await this.generateEnumTypeDefinition(file, dataType);
|
|
54
59
|
}
|
|
55
|
-
else if (dataType instanceof ComplexType) {
|
|
60
|
+
else if (dataType instanceof common_1.ComplexType) {
|
|
56
61
|
file.content += `export class ${typeName} {
|
|
57
62
|
constructor(init?: Partial<I${typeName}>) {
|
|
58
63
|
if (init)
|
|
@@ -68,13 +73,14 @@ interface I${typeName} ${await this.generateComplexTypeDefinition(file, dataType
|
|
|
68
73
|
}
|
|
69
74
|
return file;
|
|
70
75
|
}
|
|
76
|
+
exports.generateTypeFile = generateTypeFile;
|
|
71
77
|
/**
|
|
72
78
|
*
|
|
73
79
|
* @param file
|
|
74
80
|
* @param dataType
|
|
75
81
|
* @param forInterface
|
|
76
82
|
*/
|
|
77
|
-
|
|
83
|
+
async function resolveTypeNameOrDef(file, dataType, forInterface) {
|
|
78
84
|
if (dataType.name) {
|
|
79
85
|
if (internalTypeNames.includes(dataType.name))
|
|
80
86
|
return dataType.name;
|
|
@@ -82,25 +88,26 @@ export async function resolveTypeNameOrDef(file, dataType, forInterface) {
|
|
|
82
88
|
file.addImportFile(f.filename, [dataType.name]);
|
|
83
89
|
return dataType.name;
|
|
84
90
|
}
|
|
85
|
-
if (dataType instanceof ComplexType)
|
|
91
|
+
if (dataType instanceof common_1.ComplexType)
|
|
86
92
|
return this.generateComplexTypeDefinition(file, dataType, forInterface);
|
|
87
|
-
if (dataType instanceof SimpleType)
|
|
93
|
+
if (dataType instanceof common_1.SimpleType)
|
|
88
94
|
return this.generateSimpleTypeDefinition(file, dataType);
|
|
89
|
-
if (dataType instanceof EnumType)
|
|
95
|
+
if (dataType instanceof common_1.EnumType)
|
|
90
96
|
return this.generateEnumTypeDefinition(file, dataType);
|
|
91
|
-
if (dataType instanceof UnionType)
|
|
97
|
+
if (dataType instanceof common_1.UnionType)
|
|
92
98
|
return this.generateUnionTypeDefinition(file, dataType, forInterface);
|
|
93
|
-
if (dataType instanceof MappedType)
|
|
99
|
+
if (dataType instanceof common_1.MappedType)
|
|
94
100
|
return this.generateMappedTypeDefinition(file, dataType, forInterface);
|
|
95
101
|
return 'xxx';
|
|
96
102
|
}
|
|
103
|
+
exports.resolveTypeNameOrDef = resolveTypeNameOrDef;
|
|
97
104
|
/**
|
|
98
105
|
*
|
|
99
106
|
* @param file
|
|
100
107
|
* @param dataType
|
|
101
108
|
* @param forInterface
|
|
102
109
|
*/
|
|
103
|
-
|
|
110
|
+
async function generateComplexTypeDefinition(file, dataType, forInterface) {
|
|
104
111
|
let out = '';
|
|
105
112
|
if (dataType.base) {
|
|
106
113
|
const base = await this.resolveTypeNameOrDef(file, dataType.base, forInterface);
|
|
@@ -135,12 +142,13 @@ export async function generateComplexTypeDefinition(file, dataType, forInterface
|
|
|
135
142
|
out += '[key: string]: any;\n';
|
|
136
143
|
return out + '\b}';
|
|
137
144
|
}
|
|
145
|
+
exports.generateComplexTypeDefinition = generateComplexTypeDefinition;
|
|
138
146
|
/**
|
|
139
147
|
*
|
|
140
148
|
* @param file
|
|
141
149
|
* @param dataType
|
|
142
150
|
*/
|
|
143
|
-
|
|
151
|
+
async function generateSimpleTypeDefinition(file, dataType) {
|
|
144
152
|
if (dataType.ctor === Boolean)
|
|
145
153
|
return 'boolean';
|
|
146
154
|
if (dataType.ctor === String)
|
|
@@ -155,12 +163,13 @@ export async function generateSimpleTypeDefinition(file, dataType) {
|
|
|
155
163
|
return 'object';
|
|
156
164
|
return 'any';
|
|
157
165
|
}
|
|
166
|
+
exports.generateSimpleTypeDefinition = generateSimpleTypeDefinition;
|
|
158
167
|
/**
|
|
159
168
|
*
|
|
160
169
|
* @param file
|
|
161
170
|
* @param dataType
|
|
162
171
|
*/
|
|
163
|
-
|
|
172
|
+
async function generateEnumTypeDefinition(file, dataType) {
|
|
164
173
|
let out = '{\n\t';
|
|
165
174
|
for (const [k, v] of Object.entries(dataType.values)) {
|
|
166
175
|
// Print JSDoc
|
|
@@ -176,24 +185,26 @@ export async function generateEnumTypeDefinition(file, dataType) {
|
|
|
176
185
|
}
|
|
177
186
|
return out + '\b}';
|
|
178
187
|
}
|
|
188
|
+
exports.generateEnumTypeDefinition = generateEnumTypeDefinition;
|
|
179
189
|
/**
|
|
180
190
|
*
|
|
181
191
|
* @param file
|
|
182
192
|
* @param dataType
|
|
183
193
|
* @param forInterface
|
|
184
194
|
*/
|
|
185
|
-
|
|
195
|
+
async function generateUnionTypeDefinition(file, dataType, forInterface) {
|
|
186
196
|
// let out = '';
|
|
187
197
|
return (await Promise.all(dataType.types
|
|
188
198
|
.map(t => this.resolveTypeNameOrDef(file, t, forInterface)))).join(forInterface ? ', ' : ' & ');
|
|
189
199
|
}
|
|
200
|
+
exports.generateUnionTypeDefinition = generateUnionTypeDefinition;
|
|
190
201
|
/**
|
|
191
202
|
*
|
|
192
203
|
* @param file
|
|
193
204
|
* @param dataType
|
|
194
205
|
* @param forInterface
|
|
195
206
|
*/
|
|
196
|
-
|
|
207
|
+
async function generateMappedTypeDefinition(file, dataType, forInterface) {
|
|
197
208
|
return (dataType.pick ? 'Pick<' : 'Omit<') +
|
|
198
209
|
(await this.resolveTypeNameOrDef(file, dataType.type, forInterface)) +
|
|
199
210
|
', ' +
|
|
@@ -201,3 +212,4 @@ export async function generateMappedTypeDefinition(file, dataType, forInterface)
|
|
|
201
212
|
.map(x => `'${x}'`).join(' | ') +
|
|
202
213
|
'>';
|
|
203
214
|
}
|
|
215
|
+
exports.generateMappedTypeDefinition = generateMappedTypeDefinition;
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.setExt = exports.relativePath = exports.TsFile = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const path_1 = tslib_1.__importDefault(require("path"));
|
|
6
|
+
const putil_flattentext_1 = tslib_1.__importDefault(require("putil-flattentext"));
|
|
7
|
+
class TsFile {
|
|
4
8
|
constructor(filename) {
|
|
5
9
|
this.filename = filename;
|
|
6
10
|
this.importFiles = {};
|
|
@@ -16,7 +20,7 @@ export class TsFile {
|
|
|
16
20
|
});
|
|
17
21
|
};
|
|
18
22
|
this.addImportFile = (filename, types) => {
|
|
19
|
-
filename =
|
|
23
|
+
filename = path_1.default.resolve(this.dirname, filename);
|
|
20
24
|
this.importFiles[filename] = this.importFiles[filename] || [];
|
|
21
25
|
types?.forEach(x => {
|
|
22
26
|
if (!this.importFiles[filename].includes(x))
|
|
@@ -24,7 +28,7 @@ export class TsFile {
|
|
|
24
28
|
});
|
|
25
29
|
};
|
|
26
30
|
this.addExportFile = (filename, types) => {
|
|
27
|
-
filename =
|
|
31
|
+
filename = path_1.default.resolve(this.dirname, filename);
|
|
28
32
|
if (filename.endsWith('.ts') || filename.endsWith('.js'))
|
|
29
33
|
filename = setExt(filename, '');
|
|
30
34
|
this.exportFiles[filename] = this.exportFiles[filename] || [];
|
|
@@ -33,18 +37,18 @@ export class TsFile {
|
|
|
33
37
|
this.exportFiles[filename].push(x);
|
|
34
38
|
});
|
|
35
39
|
};
|
|
36
|
-
this.dirname =
|
|
40
|
+
this.dirname = path_1.default.dirname(filename);
|
|
37
41
|
}
|
|
38
42
|
generate(options) {
|
|
39
|
-
const dirname =
|
|
43
|
+
const dirname = path_1.default.dirname(this.filename);
|
|
40
44
|
let output = '/* #!oprimp_auto_generated!# !! Do NOT remove this line */\n' +
|
|
41
|
-
(this.header ?
|
|
45
|
+
(this.header ? (0, putil_flattentext_1.default)(this.header) + '\n\n' : '\n');
|
|
42
46
|
const importStr = Object.keys(this.importFiles)
|
|
43
47
|
.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()))
|
|
44
48
|
.map(filename => {
|
|
45
49
|
const types = this.importFiles[filename];
|
|
46
50
|
let relFile = filename;
|
|
47
|
-
if (
|
|
51
|
+
if (path_1.default.isAbsolute(filename)) {
|
|
48
52
|
relFile = relativePath(dirname, filename);
|
|
49
53
|
if (options?.importExt)
|
|
50
54
|
relFile = setExt(relFile, options.importExt);
|
|
@@ -53,14 +57,14 @@ export class TsFile {
|
|
|
53
57
|
})
|
|
54
58
|
.join('\n');
|
|
55
59
|
if (importStr)
|
|
56
|
-
output +=
|
|
57
|
-
output +=
|
|
60
|
+
output += (0, putil_flattentext_1.default)(importStr) + '\n';
|
|
61
|
+
output += (0, putil_flattentext_1.default)(this.content);
|
|
58
62
|
const exportStr = Object.keys(this.exportFiles)
|
|
59
63
|
.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()))
|
|
60
64
|
.map(filename => {
|
|
61
65
|
const types = this.exportFiles[filename];
|
|
62
66
|
let relFile = filename;
|
|
63
|
-
if (
|
|
67
|
+
if (path_1.default.isAbsolute(filename)) {
|
|
64
68
|
relFile = relativePath(dirname, filename);
|
|
65
69
|
if (options?.importExt)
|
|
66
70
|
relFile = setExt(relFile, options.importExt);
|
|
@@ -69,15 +73,18 @@ export class TsFile {
|
|
|
69
73
|
})
|
|
70
74
|
.join('\n');
|
|
71
75
|
if (exportStr)
|
|
72
|
-
output +=
|
|
76
|
+
output += (0, putil_flattentext_1.default)(exportStr) + '\n';
|
|
73
77
|
return output;
|
|
74
78
|
}
|
|
75
79
|
}
|
|
76
|
-
|
|
77
|
-
|
|
80
|
+
exports.TsFile = TsFile;
|
|
81
|
+
function relativePath(from, to) {
|
|
82
|
+
const s = path_1.default.relative(from, to);
|
|
78
83
|
return s.startsWith('.') ? s : ('./' + s);
|
|
79
84
|
}
|
|
80
|
-
|
|
81
|
-
|
|
85
|
+
exports.relativePath = relativePath;
|
|
86
|
+
function setExt(filename, ext) {
|
|
87
|
+
const e = path_1.default.extname(filename);
|
|
82
88
|
return filename.substring(0, filename.length - e.length) + (ext ? '.' + ext : '');
|
|
83
89
|
}
|
|
90
|
+
exports.setExt = setExt;
|
package/esm/index.js
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
package/esm/oprimp-cli.js
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const chalk_1 = tslib_1.__importDefault(require("chalk"));
|
|
5
|
+
const commander_1 = require("commander");
|
|
6
|
+
const console = tslib_1.__importStar(require("console"));
|
|
7
|
+
const fs = tslib_1.__importStar(require("fs"));
|
|
8
|
+
const path_1 = tslib_1.__importDefault(require("path"));
|
|
9
|
+
const process = tslib_1.__importStar(require("process"));
|
|
10
|
+
const api_exporter_js_1 = require("./api-exporter/api-exporter.js");
|
|
11
|
+
const get_caller_file_util_js_1 = require("./utils/get-caller-file.util.js");
|
|
12
|
+
const dirname = path_1.default.dirname((0, get_caller_file_util_js_1.getCallerFile)());
|
|
13
|
+
const pkgJson = JSON.parse(fs.readFileSync(path_1.default.resolve(dirname, '../package.json'), 'utf-8'));
|
|
14
|
+
commander_1.program
|
|
12
15
|
.version(pkgJson.version)
|
|
13
16
|
.argument('<serviceUrl>', 'OPRA service url')
|
|
14
17
|
.argument('<outDir>', 'Output directory')
|
|
@@ -17,8 +20,8 @@ program
|
|
|
17
20
|
.option('--no-color', 'Disables colors in logs messages')
|
|
18
21
|
.action(async (serviceUrl, outDir, options) => {
|
|
19
22
|
if (!options.color)
|
|
20
|
-
|
|
21
|
-
await ApiExporter.execute({
|
|
23
|
+
chalk_1.default.level = 0;
|
|
24
|
+
await api_exporter_js_1.ApiExporter.execute({
|
|
22
25
|
serviceUrl,
|
|
23
26
|
logger: console,
|
|
24
27
|
outDir,
|
|
@@ -27,6 +30,6 @@ program
|
|
|
27
30
|
fileHeader: '/* Generated by OPRA Service Generator, Version ' + pkgJson.version + '*/\n' +
|
|
28
31
|
'/* eslint-disable import/extensions,simple-import-sort/imports */\n'
|
|
29
32
|
});
|
|
30
|
-
console.log(
|
|
33
|
+
console.log(chalk_1.default.greenBright('Completed'));
|
|
31
34
|
});
|
|
32
|
-
program.parse(process.argv);
|
|
35
|
+
commander_1.program.parse(process.argv);
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getCallerFile = void 0;
|
|
1
4
|
const PATH_PATTERN = /^(?:file:\/\/)?(.+)$/;
|
|
2
|
-
|
|
5
|
+
function getCallerFile(position = 1) {
|
|
3
6
|
if (position >= Error.stackTraceLimit) {
|
|
4
7
|
throw new TypeError('getCallerFile(position) requires position be less then Error.stackTraceLimit but position was: `' +
|
|
5
8
|
position + '` and Error.stackTraceLimit was: `' + Error.stackTraceLimit + '`');
|
|
@@ -18,3 +21,4 @@ export function getCallerFile(position = 1) {
|
|
|
18
21
|
}
|
|
19
22
|
return '';
|
|
20
23
|
}
|
|
24
|
+
exports.getCallerFile = getCallerFile;
|
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.wrapTypeArray = exports.wrapStringArray = exports.wrapQuotedString = exports.wrapJSDocString = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const js_string_escape_1 = tslib_1.__importDefault(require("js-string-escape"));
|
|
6
|
+
function wrapJSDocString(s, indent, currentColumn) {
|
|
3
7
|
const arr = (s || '')
|
|
4
8
|
.split(/[ \n\r]/)
|
|
5
9
|
.map((x, i, a) => i < a.length - 1 ? x + ' ' : x);
|
|
@@ -10,8 +14,9 @@ export function wrapJSDocString(s, indent, currentColumn) {
|
|
|
10
14
|
lineEnd: ''
|
|
11
15
|
});
|
|
12
16
|
}
|
|
13
|
-
|
|
14
|
-
|
|
17
|
+
exports.wrapJSDocString = wrapJSDocString;
|
|
18
|
+
function wrapQuotedString(s, indent, currentColumn) {
|
|
19
|
+
const arr = (0, js_string_escape_1.default)(s || '')
|
|
15
20
|
.split(' ')
|
|
16
21
|
.map((x, i, a) => i < a.length - 1 ? x + ' ' : x);
|
|
17
22
|
return '\'' + _printLines(arr, {
|
|
@@ -21,16 +26,19 @@ export function wrapQuotedString(s, indent, currentColumn) {
|
|
|
21
26
|
lineEnd: '\' +',
|
|
22
27
|
}) + '\'';
|
|
23
28
|
}
|
|
24
|
-
|
|
29
|
+
exports.wrapQuotedString = wrapQuotedString;
|
|
30
|
+
function wrapStringArray(arr, indent, currentColumn) {
|
|
25
31
|
const ar1 = arr
|
|
26
32
|
.map((x, i, a) => ('\'' + x + '\'') + (i < a.length - 1 ? ', ' : ''));
|
|
27
33
|
return '[' + _printLines(ar1, { indent, currentColumn }) + ']';
|
|
28
34
|
}
|
|
29
|
-
|
|
35
|
+
exports.wrapStringArray = wrapStringArray;
|
|
36
|
+
function wrapTypeArray(arr, indent, currentColumn) {
|
|
30
37
|
const ar1 = arr
|
|
31
38
|
.map((x, i, a) => x + (i < a.length - 1 ? ' | ' : ''));
|
|
32
39
|
return _printLines(ar1, { indent, currentColumn });
|
|
33
40
|
}
|
|
41
|
+
exports.wrapTypeArray = wrapTypeArray;
|
|
34
42
|
function _printLines(arr, opts = {}) {
|
|
35
43
|
let s = '';
|
|
36
44
|
let line = '';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.1",
|
|
4
4
|
"description": "Opra CLI tools",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"clean:cover": "rimraf ../../coverage/client"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@opra/node-client": "^0.
|
|
31
|
+
"@opra/node-client": "^0.16.1",
|
|
32
32
|
"chalk": "^5.2.0",
|
|
33
33
|
"commander": "^10.0.1",
|
|
34
34
|
"js-string-escape": "^1.0.1",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"putil-varhelpers": "^1.6.5"
|
|
37
37
|
},
|
|
38
38
|
"type": "module",
|
|
39
|
-
"types": "
|
|
39
|
+
"types": "types/index.d.ts",
|
|
40
40
|
"exports": {
|
|
41
41
|
".": {
|
|
42
42
|
"require": "./cjs/index.js",
|
|
@@ -54,6 +54,7 @@
|
|
|
54
54
|
"bin/",
|
|
55
55
|
"cjs/",
|
|
56
56
|
"esm/",
|
|
57
|
+
"types/",
|
|
57
58
|
"LICENSE",
|
|
58
59
|
"README.md"
|
|
59
60
|
],
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|