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