datagrok-tools 4.14.30 → 4.14.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/bin/grok.js +2 -4
- package/bin/utils/func-generation.js +11 -2
- package/package.json +1 -1
- package/plugins/func-gen-plugin.js +139 -133
package/bin/grok.js
CHANGED
|
@@ -26,12 +26,10 @@ if (command in commands) {
|
|
|
26
26
|
if (argv['help']) {
|
|
27
27
|
console.log(help[command]);
|
|
28
28
|
exitWithCode(1);
|
|
29
|
-
}
|
|
30
|
-
else if (argv.all && onPackageCommandNames.includes(command)) {
|
|
29
|
+
} else if (argv.all && onPackageCommandNames.includes(command)) {
|
|
31
30
|
runAllCommand(process.cwd(),
|
|
32
31
|
`grok ${process.argv.slice(2).join(' ')}`.replace('--all', ''), {});
|
|
33
|
-
}
|
|
34
|
-
else if (!commands[command](argv)) {
|
|
32
|
+
} else if (!commands[command](argv)) {
|
|
35
33
|
console.log(help[command]);
|
|
36
34
|
exitWithCode(1);
|
|
37
35
|
}
|
|
@@ -20,7 +20,7 @@ let pseudoParams = exports.pseudoParams = /*#__PURE__*/function (pseudoParams) {
|
|
|
20
20
|
pseudoParams["INPUT_TYPE"] = "inputType";
|
|
21
21
|
return pseudoParams;
|
|
22
22
|
}({});
|
|
23
|
-
const nonMetaData = ['sidebar', 'editor', 'friendlyName', 'helpUrl'];
|
|
23
|
+
const nonMetaData = ['sidebar', 'editor', 'friendlyName', 'helpUrl', 'condition', 'top-menu', 'cache', 'cache.invalidateOn'];
|
|
24
24
|
const decoratorOptionToAnnotation = new Map([['initialValue', 'default']]);
|
|
25
25
|
const dgAnnotationTypes = exports.dgAnnotationTypes = {
|
|
26
26
|
INT: 'int',
|
|
@@ -145,17 +145,23 @@ function getFuncAnnotation(data, comment = '//', sep = '\n') {
|
|
|
145
145
|
} else type = 'dynamic';
|
|
146
146
|
const options = input?.options?.options ? buildStringOfOptions(input.options.options ?? {}) : '';
|
|
147
147
|
const functionName = (input.options?.name ? input?.options?.name : ` ${input.name?.replaceAll('.', '')}`)?.trim();
|
|
148
|
+
|
|
149
|
+
// eslint-disable-next-line max-len
|
|
148
150
|
s += comment + 'input: ' + type + ' ' + functionName + (input.defaultValue !== undefined ? `= ${input.defaultValue}` : '') + ' ' + options.replaceAll('"', '\'') + sep;
|
|
149
151
|
}
|
|
150
152
|
if (data.outputs) {
|
|
151
153
|
for (const output of data.outputs) {
|
|
152
|
-
if (output.type !== 'void')
|
|
154
|
+
if (output.type !== 'void') {
|
|
155
|
+
// eslint-disable-next-line max-len
|
|
156
|
+
s += comment + 'output: ' + output.type + (output.name ? ` ${output.name}${output.options ? ` ${buildStringOfOptions(output.options)}` : ''}` : '') + sep;
|
|
157
|
+
}
|
|
153
158
|
}
|
|
154
159
|
}
|
|
155
160
|
if (data.meta) {
|
|
156
161
|
for (const entry of Object.entries(data.meta)) s += `${comment}meta.${entry[0]}: ${entry[1]}${sep}`;
|
|
157
162
|
}
|
|
158
163
|
for (const parameter in data) {
|
|
164
|
+
// eslint-disable-next-line max-len
|
|
159
165
|
if (parameter === pseudoParams.EXTENSION || parameter === pseudoParams.INPUT_TYPE || parameter === 'meta' || parameter === 'isAsync' || parameter === 'test') continue;else if (parameter === pseudoParams.EXTENSIONS) {
|
|
160
166
|
if (isFileViewer) continue;
|
|
161
167
|
s += `${comment}meta.ext: ${data[parameter]}${sep}`;
|
|
@@ -431,8 +437,11 @@ const primitives = new Set(['string', 'string[]', 'number', 'number[]', 'boolean
|
|
|
431
437
|
|
|
432
438
|
/** Generates a DG function. */
|
|
433
439
|
function generateFunc(annotation, funcName, sep = '\n', className = '', inputs = [], isAsync = false) {
|
|
440
|
+
// eslint-disable-next-line max-len
|
|
434
441
|
const funcSigNature = inputs.map(e => `${e.name}: ${primitives.has(e.type ?? '') && !typesToAny.includes(e.type ?? '') ? e.type : typesToAnnotation[e.type?.replace('[]', '') ?? ''] && !typesToAny.includes(e.type ?? '') ? e.type : 'any'}`).join(', ');
|
|
435
442
|
const funcArguments = inputs.map(e => e.name).join(', ');
|
|
443
|
+
|
|
444
|
+
// eslint-disable-next-line max-len
|
|
436
445
|
return sep + annotation + `export ${isAsync ? 'async ' : ''}function ${funcName}(${funcSigNature}) {${sep} return ${className.length > 0 ? `${className}.` : ''}${funcName}(${funcArguments});${sep}}${sep}`;
|
|
437
446
|
}
|
|
438
447
|
function generateImport(className, path, sep = '\n') {
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
const fs = require(
|
|
2
|
-
const path = require(
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
3
|
|
|
4
|
-
const tsParser = require(
|
|
5
|
-
const generate = require(
|
|
4
|
+
const tsParser = require('@typescript-eslint/typescript-estree');
|
|
5
|
+
const generate = require('@babel/generator').default;
|
|
6
6
|
|
|
7
7
|
const {
|
|
8
8
|
reservedDecorators,
|
|
@@ -11,48 +11,49 @@ const {
|
|
|
11
11
|
generateExport,
|
|
12
12
|
typesToAnnotation,
|
|
13
13
|
dgAnnotationTypes,
|
|
14
|
-
} = require(
|
|
14
|
+
} = require('../bin/utils/func-generation');
|
|
15
15
|
|
|
16
|
-
const baseImport =
|
|
16
|
+
const baseImport = 'import * as DG from \'datagrok-api/dg\';\n';
|
|
17
|
+
// eslint-disable-next-line max-len
|
|
17
18
|
const annotationForGeneratedFile = `/**\nThis file is auto-generated by the webpack command.\nIf you notice any changes, please push them to the repository.\nDo not edit this file manually.\n*/\n`;
|
|
18
19
|
|
|
19
20
|
class FuncGeneratorPlugin {
|
|
20
|
-
constructor(options = {
|
|
21
|
+
constructor(options = {outputPath: './src/package.g.ts'}) {
|
|
21
22
|
this.options = options;
|
|
22
23
|
}
|
|
23
24
|
|
|
24
25
|
apply(compiler) {
|
|
25
|
-
const srcDirPath = path.join(compiler.context,
|
|
26
|
-
let packageFilePath = path.join(srcDirPath,
|
|
26
|
+
const srcDirPath = path.join(compiler.context, 'src');
|
|
27
|
+
let packageFilePath = path.join(srcDirPath, 'package.ts');
|
|
27
28
|
if (!fs.existsSync(packageFilePath))
|
|
28
|
-
packageFilePath = path.join(srcDirPath,
|
|
29
|
+
packageFilePath = path.join(srcDirPath, 'package.js');
|
|
29
30
|
const tsFiles = this._getTsFiles(srcDirPath);
|
|
30
31
|
const genImports = [];
|
|
31
32
|
const genExports = [];
|
|
32
33
|
|
|
33
|
-
compiler.hooks.compilation.tap(
|
|
34
|
+
compiler.hooks.compilation.tap('FuncGeneratorPlugin', (_compilation) => {
|
|
34
35
|
this._clearGeneratedFile();
|
|
35
36
|
|
|
36
37
|
for (const file of tsFiles) {
|
|
37
|
-
const content = fs.readFileSync(file,
|
|
38
|
-
if (!content.includes(
|
|
38
|
+
const content = fs.readFileSync(file, 'utf-8');
|
|
39
|
+
if (!content.includes('@grok.decorators.')) continue;
|
|
39
40
|
|
|
40
41
|
if (!content) continue;
|
|
41
42
|
const ast = tsParser.parse(content, {
|
|
42
|
-
sourceType:
|
|
43
|
+
sourceType: 'module',
|
|
43
44
|
plugins: [
|
|
44
|
-
[
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
['decorators', {decoratorsBeforeExport: true}],
|
|
46
|
+
'classProperties',
|
|
47
|
+
'typescript',
|
|
47
48
|
],
|
|
48
49
|
});
|
|
49
50
|
const functions = [];
|
|
50
|
-
|
|
51
|
+
const imports = new Set();
|
|
51
52
|
this._walk(ast, (node, parentClass) => {
|
|
52
53
|
const decorators = node.decorators;
|
|
53
54
|
if (!decorators || decorators.length === 0) return;
|
|
54
55
|
|
|
55
|
-
if (node?.type ===
|
|
56
|
+
if (node?.type === 'ClassDeclaration') {
|
|
56
57
|
this._addNodeData(
|
|
57
58
|
node,
|
|
58
59
|
file,
|
|
@@ -60,10 +61,11 @@ class FuncGeneratorPlugin {
|
|
|
60
61
|
functions,
|
|
61
62
|
imports,
|
|
62
63
|
genImports,
|
|
63
|
-
genExports
|
|
64
|
+
genExports,
|
|
64
65
|
);
|
|
66
|
+
}
|
|
65
67
|
|
|
66
|
-
if (node?.type ===
|
|
68
|
+
if (node?.type === 'MethodDefinition') {
|
|
67
69
|
this._addNodeData(
|
|
68
70
|
node,
|
|
69
71
|
file,
|
|
@@ -72,11 +74,12 @@ class FuncGeneratorPlugin {
|
|
|
72
74
|
imports,
|
|
73
75
|
genImports,
|
|
74
76
|
genExports,
|
|
75
|
-
parentClass
|
|
77
|
+
parentClass,
|
|
76
78
|
);
|
|
79
|
+
}
|
|
77
80
|
});
|
|
78
81
|
this._insertImports([...imports]);
|
|
79
|
-
fs.appendFileSync(this.options.outputPath, functions.join(
|
|
82
|
+
fs.appendFileSync(this.options.outputPath, functions.join(''), 'utf-8');
|
|
80
83
|
}
|
|
81
84
|
this._checkPackageFileForDecoratorsExport(packageFilePath);
|
|
82
85
|
// Uncommment to add obvious import/export
|
|
@@ -92,7 +95,7 @@ class FuncGeneratorPlugin {
|
|
|
92
95
|
imports,
|
|
93
96
|
genImports,
|
|
94
97
|
genExports,
|
|
95
|
-
parent = undefined
|
|
98
|
+
parent = undefined,
|
|
96
99
|
) {
|
|
97
100
|
if (
|
|
98
101
|
!node.decorators ||
|
|
@@ -105,7 +108,7 @@ class FuncGeneratorPlugin {
|
|
|
105
108
|
const relativePath = path.relative(dirPath, filePath);
|
|
106
109
|
return `./${relativePath
|
|
107
110
|
.slice(0, relativePath.length - 3)
|
|
108
|
-
.replace(/\\/g,
|
|
111
|
+
.replace(/\\/g, '/')}`;
|
|
109
112
|
}
|
|
110
113
|
|
|
111
114
|
const decorator = node.decorators[0];
|
|
@@ -117,57 +120,57 @@ class FuncGeneratorPlugin {
|
|
|
117
120
|
if (!name) return;
|
|
118
121
|
|
|
119
122
|
const decoratorOptions = this._readDecoratorOptions(
|
|
120
|
-
exp.arguments[0].properties
|
|
123
|
+
exp.arguments[0].properties,
|
|
121
124
|
);
|
|
122
125
|
|
|
123
|
-
decoratorOptions.set(
|
|
124
|
-
...(reservedDecorators[name][
|
|
125
|
-
...(decoratorOptions.get(
|
|
126
|
+
decoratorOptions.set('tags', [
|
|
127
|
+
...(reservedDecorators[name]['metadata']['tags'] ?? []),
|
|
128
|
+
...(decoratorOptions.get('tags') ?? []),
|
|
126
129
|
]);
|
|
127
130
|
const functionParams =
|
|
128
|
-
node?.type ===
|
|
129
|
-
const annotationByReturnObj = node?.type ===
|
|
131
|
+
node?.type === 'MethodDefinition' ? this._readMethodParamas(node) : [];
|
|
132
|
+
const annotationByReturnObj = node?.type === 'MethodDefinition' ? this._readOutputsFromReturnType(node) : undefined;
|
|
130
133
|
const isMethodAsync = this._isMethodAsync(node);
|
|
131
|
-
|
|
132
|
-
node?.type ===
|
|
133
|
-
modifyImportPath(path.dirname(this.options.outputPath), file)
|
|
134
|
+
const importString = generateImport(
|
|
135
|
+
node?.type === 'MethodDefinition' ? className : identifierName,
|
|
136
|
+
modifyImportPath(path.dirname(this.options.outputPath), file),
|
|
134
137
|
);
|
|
135
138
|
imports.add(importString);
|
|
136
139
|
const funcName = `${
|
|
137
|
-
node?.type ===
|
|
140
|
+
node?.type === 'MethodDefinition' ? '' : '_'
|
|
138
141
|
}${identifierName}`;
|
|
139
142
|
const funcAnnotaionOptions = {
|
|
140
|
-
...reservedDecorators[name][
|
|
141
|
-
...(annotationByReturnObj
|
|
142
|
-
|
|
143
|
-
|
|
143
|
+
...reservedDecorators[name]['metadata'],
|
|
144
|
+
...(annotationByReturnObj ?
|
|
145
|
+
{outputs: annotationByReturnObj ?? []} :
|
|
146
|
+
{}),
|
|
144
147
|
...Object.fromEntries(decoratorOptions),
|
|
145
|
-
...{
|
|
146
|
-
...{
|
|
148
|
+
...{inputs: functionParams},
|
|
149
|
+
...{isAsync: isMethodAsync},
|
|
147
150
|
};
|
|
148
151
|
if (!funcAnnotaionOptions.name) funcAnnotaionOptions.name = identifierName;
|
|
149
152
|
functions.push(
|
|
150
|
-
reservedDecorators[name][
|
|
153
|
+
reservedDecorators[name]['genFunc'](
|
|
151
154
|
getFuncAnnotation(funcAnnotaionOptions),
|
|
152
155
|
identifierName,
|
|
153
|
-
|
|
154
|
-
className ??
|
|
156
|
+
'\n',
|
|
157
|
+
className ?? '',
|
|
155
158
|
functionParams,
|
|
156
|
-
funcAnnotaionOptions.isAsync ?? false
|
|
157
|
-
)
|
|
159
|
+
funcAnnotaionOptions.isAsync ?? false,
|
|
160
|
+
),
|
|
158
161
|
);
|
|
159
162
|
genImports.push(
|
|
160
163
|
generateImport(
|
|
161
164
|
funcName,
|
|
162
|
-
modifyImportPath(srcDirPath, this.options.outputPath)
|
|
163
|
-
)
|
|
165
|
+
modifyImportPath(srcDirPath, this.options.outputPath),
|
|
166
|
+
),
|
|
164
167
|
);
|
|
165
168
|
genExports.push(generateExport(funcName));
|
|
166
169
|
}
|
|
167
170
|
|
|
168
171
|
_isMethodAsync(node) {
|
|
169
172
|
let result = false;
|
|
170
|
-
if (node.type ===
|
|
173
|
+
if (node.type === 'MethodDefinition') result = node.value.async;
|
|
171
174
|
return result;
|
|
172
175
|
}
|
|
173
176
|
|
|
@@ -176,86 +179,88 @@ class FuncGeneratorPlugin {
|
|
|
176
179
|
const results = [];
|
|
177
180
|
|
|
178
181
|
let match;
|
|
179
|
-
while ((match = importRegex.exec(content)) !== null)
|
|
182
|
+
while ((match = importRegex.exec(content)) !== null)
|
|
180
183
|
results.push(`${match[1]}\n`);
|
|
181
|
-
|
|
184
|
+
|
|
182
185
|
return results;
|
|
183
186
|
}
|
|
184
187
|
|
|
185
188
|
_readDecoratorOptions(properties) {
|
|
186
189
|
const resultMap = new Map();
|
|
190
|
+
console.log(properties);
|
|
187
191
|
|
|
188
|
-
for (
|
|
189
|
-
resultMap.set(prop.key.name, this._evalLiteral(prop.value));
|
|
192
|
+
for (const prop of properties)
|
|
193
|
+
resultMap.set(prop.key.name ?? prop.key.value, this._evalLiteral(prop.value));
|
|
190
194
|
return resultMap;
|
|
191
195
|
}
|
|
192
196
|
|
|
193
197
|
_evalLiteral(node) {
|
|
194
198
|
if (!node) return null;
|
|
195
199
|
switch (node.type) {
|
|
196
|
-
|
|
197
|
-
|
|
200
|
+
case 'Literal':
|
|
201
|
+
return node.value;
|
|
198
202
|
|
|
199
|
-
|
|
200
|
-
|
|
203
|
+
case 'ArrayExpression':
|
|
204
|
+
return node.elements.map((el) => this._evalLiteral(el));
|
|
201
205
|
|
|
202
|
-
|
|
203
|
-
|
|
206
|
+
case 'MemberExpression':
|
|
207
|
+
return dgAnnotationTypes[node?.property?.name ?? ''] ?? 'dynamic';
|
|
204
208
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
209
|
+
case 'ObjectExpression':
|
|
210
|
+
return Object.fromEntries(
|
|
211
|
+
node.properties.map((p) => {
|
|
212
|
+
return [p.key.name || p.key.value, this._evalLiteral(p.value)];
|
|
213
|
+
}),
|
|
214
|
+
);
|
|
211
215
|
|
|
212
|
-
|
|
213
|
-
|
|
216
|
+
default:
|
|
217
|
+
return '';
|
|
214
218
|
}
|
|
215
219
|
}
|
|
216
220
|
|
|
217
221
|
_readMethodParamas(node) {
|
|
218
222
|
const params = node?.value?.params?.map((param) => {
|
|
219
223
|
let baseParam =
|
|
220
|
-
param.type ===
|
|
224
|
+
param.type === 'TSNonNullExpression' ? param.expression : param;
|
|
221
225
|
const options =
|
|
222
|
-
param.decorators?.length > 0
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
226
|
+
param.decorators?.length > 0 ?
|
|
227
|
+
Object.fromEntries(
|
|
228
|
+
this._readDecoratorOptions(
|
|
229
|
+
param.decorators[0]?.expression?.arguments[0].properties,
|
|
230
|
+
),
|
|
231
|
+
) :
|
|
232
|
+
undefined;
|
|
229
233
|
|
|
230
234
|
// Commented code finds value by default of function's variable
|
|
231
235
|
// let defaultValue = undefined;
|
|
232
|
-
if (baseParam.type ===
|
|
236
|
+
if (baseParam.type === 'AssignmentPattern') {
|
|
233
237
|
// if (baseParam?.right?.type === 'Literal')
|
|
234
238
|
// defaultValue = baseParam?.right?.raw;
|
|
235
239
|
baseParam = baseParam?.left;
|
|
236
240
|
}
|
|
237
241
|
|
|
238
|
-
if (baseParam.type ===
|
|
242
|
+
if (baseParam.type === 'RestElement' || baseParam.type === 'Identifier') {
|
|
239
243
|
let name =
|
|
240
|
-
baseParam.type ===
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
if (baseParam?.argument?.typeAnnotation)
|
|
244
|
+
baseParam.type === 'RestElement' ?
|
|
245
|
+
`...${baseParam.argument.name}` :
|
|
246
|
+
baseParam.name;
|
|
247
|
+
if (baseParam?.argument?.typeAnnotation) {
|
|
244
248
|
name +=
|
|
245
|
-
|
|
249
|
+
': ' +
|
|
246
250
|
generate(baseParam.argument.typeAnnotation.typeAnnotation).code;
|
|
251
|
+
}
|
|
247
252
|
|
|
248
|
-
let type =
|
|
253
|
+
let type = '';
|
|
249
254
|
if (baseParam?.typeAnnotation?.typeAnnotation)
|
|
250
255
|
type = generate(baseParam.typeAnnotation.typeAnnotation).code;
|
|
251
|
-
else type =
|
|
256
|
+
else type = 'any';
|
|
252
257
|
|
|
253
|
-
|
|
258
|
+
const params =
|
|
254
259
|
baseParam.typeAnnotation.typeAnnotation.typeArguments?.params;
|
|
255
|
-
if (type !==
|
|
256
|
-
type += `<${params.map((e) => e.typeName?.name ?? 'any').join(
|
|
260
|
+
if (type !== 'any' && params && params.length > 0)
|
|
261
|
+
type += `<${params.map((e) => e.typeName?.name ?? 'any').join(',')}>`;
|
|
257
262
|
|
|
258
|
-
return {
|
|
263
|
+
return {name: name, type: type, options: options};
|
|
259
264
|
}
|
|
260
265
|
// Commented code belove sets more strong types for ObjectPatterns and ArrayPatterns
|
|
261
266
|
// else if (baseParam.type === 'ObjectPattern' || baseParam.type === 'ArrayPattern') {
|
|
@@ -290,7 +295,7 @@ class FuncGeneratorPlugin {
|
|
|
290
295
|
|
|
291
296
|
// return { name: name, type: type, options: options };
|
|
292
297
|
// }
|
|
293
|
-
return {
|
|
298
|
+
return {name: 'value', type: 'any', options: undefined};
|
|
294
299
|
});
|
|
295
300
|
return params;
|
|
296
301
|
}
|
|
@@ -298,7 +303,7 @@ class FuncGeneratorPlugin {
|
|
|
298
303
|
_getTsFiles(root) {
|
|
299
304
|
const tsFiles = [];
|
|
300
305
|
const extPattern = /\.tsx?$/;
|
|
301
|
-
const excludedFiles = [
|
|
306
|
+
const excludedFiles = ['package-test.ts', 'package.g.ts'];
|
|
302
307
|
|
|
303
308
|
function findFiles(dir) {
|
|
304
309
|
const files = fs.readdirSync(dir);
|
|
@@ -315,7 +320,7 @@ class FuncGeneratorPlugin {
|
|
|
315
320
|
}
|
|
316
321
|
|
|
317
322
|
_walk(node, visitor, parent = null) {
|
|
318
|
-
if (!node || typeof node !==
|
|
323
|
+
if (!node || typeof node !== 'object') return;
|
|
319
324
|
|
|
320
325
|
visitor(node, parent);
|
|
321
326
|
|
|
@@ -323,40 +328,39 @@ class FuncGeneratorPlugin {
|
|
|
323
328
|
const value = node[key];
|
|
324
329
|
if (Array.isArray(value)) {
|
|
325
330
|
value.forEach((child) => {
|
|
326
|
-
if (child && typeof child.type ===
|
|
331
|
+
if (child && typeof child.type === 'string') {
|
|
327
332
|
this._walk(
|
|
328
333
|
child,
|
|
329
334
|
visitor,
|
|
330
|
-
node.type ===
|
|
335
|
+
node.type === 'ClassDeclaration' ? node : parent,
|
|
331
336
|
);
|
|
332
337
|
}
|
|
333
338
|
});
|
|
334
|
-
} else if (value && typeof value.type ===
|
|
339
|
+
} else if (value && typeof value.type === 'string') {
|
|
335
340
|
this._walk(
|
|
336
341
|
value,
|
|
337
342
|
visitor,
|
|
338
|
-
node.type ===
|
|
343
|
+
node.type === 'ClassDeclaration' ? node : parent,
|
|
339
344
|
);
|
|
340
345
|
}
|
|
341
346
|
}
|
|
342
347
|
}
|
|
343
348
|
|
|
344
349
|
_readReturnType(annotation) {
|
|
345
|
-
let resultType =
|
|
350
|
+
let resultType = 'dynamic';
|
|
346
351
|
let isArray = false;
|
|
347
352
|
if (
|
|
348
353
|
annotation &&
|
|
349
|
-
annotation.type !==
|
|
350
|
-
annotation.type !==
|
|
354
|
+
annotation.type !== 'TSUnionType' &&
|
|
355
|
+
annotation.type !== 'TSIntersectionType'
|
|
351
356
|
) {
|
|
352
357
|
|
|
353
|
-
if (annotation.typeName || annotation.type ===
|
|
358
|
+
if (annotation.typeName || annotation.type === 'TSTypeReference') {
|
|
354
359
|
resultType =
|
|
355
360
|
annotation.typeName?.right?.name ?? annotation.typeName?.name;
|
|
356
|
-
else if (annotation.type !==
|
|
361
|
+
} else if (annotation.type !== 'TSArrayType')
|
|
357
362
|
resultType = this._getTypeNameFromNode(annotation);
|
|
358
|
-
|
|
359
|
-
else if (annotation.elementType.type !== "TSTypeReference") {
|
|
363
|
+
else if (annotation.elementType.type !== 'TSTypeReference') {
|
|
360
364
|
isArray = true;
|
|
361
365
|
resultType = this._getTypeNameFromNode(annotation?.elementType);
|
|
362
366
|
} else {
|
|
@@ -378,18 +382,17 @@ class FuncGeneratorPlugin {
|
|
|
378
382
|
if (node?.type === 'ClassDeclaration')
|
|
379
383
|
return [];
|
|
380
384
|
|
|
381
|
-
if (annotation?.typeName?.name ===
|
|
385
|
+
if (annotation?.typeName?.name === 'Promise') {
|
|
382
386
|
const argumnets = annotation.typeArguments?.params;
|
|
383
|
-
if (argumnets && argumnets.length === 1)
|
|
387
|
+
if (argumnets && argumnets.length === 1)
|
|
384
388
|
annotation = argumnets[0];
|
|
385
|
-
|
|
389
|
+
else annotation = {};
|
|
386
390
|
}
|
|
387
391
|
|
|
388
|
-
if (annotation?.type ===
|
|
392
|
+
if (annotation?.type === 'TSTypeLiteral')
|
|
389
393
|
results = this._readOutputsFromReturnTypeObject(annotation);
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
let resultType = this._readReturnType(annotation);
|
|
394
|
+
else {
|
|
395
|
+
const resultType = this._readReturnType(annotation);
|
|
393
396
|
results.push({name: 'result', type: resultType});
|
|
394
397
|
if (resultType === 'void')
|
|
395
398
|
results = [];
|
|
@@ -399,8 +402,8 @@ class FuncGeneratorPlugin {
|
|
|
399
402
|
|
|
400
403
|
_readOutputsFromReturnTypeObject(node) {
|
|
401
404
|
let i = 0;
|
|
402
|
-
|
|
403
|
-
for (
|
|
405
|
+
const results = [];
|
|
406
|
+
for (const member of node.members) {
|
|
404
407
|
results.push({
|
|
405
408
|
name: member?.key?.name ?? `result${i}`,
|
|
406
409
|
type: this._readReturnType(member.typeAnnotation.typeAnnotation),
|
|
@@ -411,19 +414,19 @@ class FuncGeneratorPlugin {
|
|
|
411
414
|
}
|
|
412
415
|
|
|
413
416
|
_getTypeNameFromNode(typeNode) {
|
|
414
|
-
if (typeNode.type ===
|
|
417
|
+
if (typeNode.type === 'TSTypeReference')
|
|
415
418
|
return typeNode.typeName.name;
|
|
416
|
-
|
|
417
|
-
return
|
|
418
|
-
|
|
419
|
-
return
|
|
420
|
-
|
|
421
|
-
return
|
|
422
|
-
|
|
423
|
-
return
|
|
424
|
-
|
|
419
|
+
else if (typeNode.type === 'TSVoidKeyword')
|
|
420
|
+
return 'void';
|
|
421
|
+
else if (typeNode.type === 'TSNumberKeyword')
|
|
422
|
+
return 'number';
|
|
423
|
+
else if (typeNode.type === 'TSStringKeyword')
|
|
424
|
+
return 'string';
|
|
425
|
+
else if (typeNode.type === 'TSBooleanKeyword')
|
|
426
|
+
return 'boolean';
|
|
427
|
+
else
|
|
425
428
|
return typeNode.type;
|
|
426
|
-
|
|
429
|
+
|
|
427
430
|
}
|
|
428
431
|
|
|
429
432
|
_clearGeneratedFile() {
|
|
@@ -431,38 +434,41 @@ class FuncGeneratorPlugin {
|
|
|
431
434
|
}
|
|
432
435
|
|
|
433
436
|
_checkPackageFileForDecoratorsExport(packagePath) {
|
|
434
|
-
const content = fs.readFileSync(packagePath,
|
|
437
|
+
const content = fs.readFileSync(packagePath, 'utf-8');
|
|
435
438
|
const decoratorsExportRegex = /export\s*\*\s*from\s*'\.\/package\.g';/;
|
|
436
|
-
if (!decoratorsExportRegex.test(content))
|
|
439
|
+
if (!decoratorsExportRegex.test(content)) {
|
|
437
440
|
console.warn(
|
|
438
|
-
|
|
441
|
+
// eslint-disable-next-line max-len
|
|
442
|
+
`\nWARNING: Your package doesn't export package.g.ts file to package.ts \n please add 'export * from './package.g';' to the package.ts file.\n`,
|
|
439
443
|
);
|
|
444
|
+
}
|
|
440
445
|
}
|
|
441
446
|
|
|
442
447
|
_writeToPackageFile(filePath, imports, exp) {
|
|
443
448
|
if (imports.length !== exp.length) return;
|
|
444
|
-
let content = fs.readFileSync(filePath,
|
|
449
|
+
let content = fs.readFileSync(filePath, 'utf-8');
|
|
445
450
|
for (let i = 0; i < imports.length; i++) {
|
|
446
451
|
const importStatement = imports[i];
|
|
447
452
|
const exportStatement = exp[i];
|
|
448
453
|
if (!content.includes(importStatement.trim()))
|
|
449
454
|
content = annotationForGeneratedFile + importStatement + content + exportStatement;
|
|
450
455
|
}
|
|
451
|
-
fs.writeFileSync(filePath, content,
|
|
456
|
+
fs.writeFileSync(filePath, content, 'utf-8');
|
|
452
457
|
}
|
|
453
458
|
|
|
454
459
|
_insertImports(importArray) {
|
|
455
460
|
if (fs.existsSync(this.options.outputPath)) {
|
|
456
|
-
const content = fs.readFileSync(this.options.outputPath,
|
|
461
|
+
const content = fs.readFileSync(this.options.outputPath, 'utf-8');
|
|
457
462
|
if (content) importArray.push(content);
|
|
458
|
-
const output = importArray.join(
|
|
459
|
-
fs.writeFileSync(this.options.outputPath, `${output}`,
|
|
460
|
-
} else
|
|
463
|
+
const output = importArray.join('');
|
|
464
|
+
fs.writeFileSync(this.options.outputPath, `${output}`, 'utf-8');
|
|
465
|
+
} else {
|
|
461
466
|
fs.writeFileSync(
|
|
462
467
|
this.options.outputPath,
|
|
463
|
-
`${annotationForGeneratedFile}${baseImport}\n${importArray.join(
|
|
464
|
-
|
|
468
|
+
`${annotationForGeneratedFile}${baseImport}\n${importArray.join('')}`,
|
|
469
|
+
'utf-8',
|
|
465
470
|
);
|
|
471
|
+
}
|
|
466
472
|
}
|
|
467
473
|
}
|
|
468
474
|
|