datagrok-tools 4.14.30 → 4.14.32
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-template/package.json +3 -3
- package/package.json +1 -1
- package/plugins/func-gen-plugin.js +138 -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') {
|
|
@@ -4,13 +4,13 @@
|
|
|
4
4
|
"version": "0.0.1",
|
|
5
5
|
"description": "",
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"datagrok-api": "^1.
|
|
7
|
+
"datagrok-api": "^1.26.0",
|
|
8
8
|
"cash-dom": "^8.1.5",
|
|
9
9
|
"dayjs": "^1.11.13",
|
|
10
|
-
"@datagrok-libraries/utils": "^4.
|
|
10
|
+
"@datagrok-libraries/utils": "^4.6.0"
|
|
11
11
|
},
|
|
12
12
|
"devDependencies": {
|
|
13
|
-
"datagrok-tools": "^4.14.
|
|
13
|
+
"datagrok-tools": "^4.14.32",
|
|
14
14
|
"webpack": "^5.95.0",
|
|
15
15
|
"webpack-cli": "^5.1.4"
|
|
16
16
|
},
|
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]
|
|
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,87 @@ 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();
|
|
187
190
|
|
|
188
|
-
for (
|
|
189
|
-
resultMap.set(prop.key.name, this._evalLiteral(prop.value));
|
|
191
|
+
for (const prop of properties)
|
|
192
|
+
resultMap.set(prop.key.name ?? prop.key.value, this._evalLiteral(prop.value));
|
|
190
193
|
return resultMap;
|
|
191
194
|
}
|
|
192
195
|
|
|
193
196
|
_evalLiteral(node) {
|
|
194
197
|
if (!node) return null;
|
|
195
198
|
switch (node.type) {
|
|
196
|
-
|
|
197
|
-
|
|
199
|
+
case 'Literal':
|
|
200
|
+
return node.value;
|
|
198
201
|
|
|
199
|
-
|
|
200
|
-
|
|
202
|
+
case 'ArrayExpression':
|
|
203
|
+
return node.elements.map((el) => this._evalLiteral(el));
|
|
201
204
|
|
|
202
|
-
|
|
203
|
-
|
|
205
|
+
case 'MemberExpression':
|
|
206
|
+
return dgAnnotationTypes[node?.property?.name ?? ''] ?? 'dynamic';
|
|
204
207
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
208
|
+
case 'ObjectExpression':
|
|
209
|
+
return Object.fromEntries(
|
|
210
|
+
node.properties.map((p) => {
|
|
211
|
+
return [p.key.name || p.key.value, this._evalLiteral(p.value)];
|
|
212
|
+
}),
|
|
213
|
+
);
|
|
211
214
|
|
|
212
|
-
|
|
213
|
-
|
|
215
|
+
default:
|
|
216
|
+
return '';
|
|
214
217
|
}
|
|
215
218
|
}
|
|
216
219
|
|
|
217
220
|
_readMethodParamas(node) {
|
|
218
221
|
const params = node?.value?.params?.map((param) => {
|
|
219
222
|
let baseParam =
|
|
220
|
-
param.type ===
|
|
223
|
+
param.type === 'TSNonNullExpression' ? param.expression : param;
|
|
221
224
|
const options =
|
|
222
|
-
param.decorators?.length > 0
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
225
|
+
param.decorators?.length > 0 ?
|
|
226
|
+
Object.fromEntries(
|
|
227
|
+
this._readDecoratorOptions(
|
|
228
|
+
param.decorators[0]?.expression?.arguments[0].properties,
|
|
229
|
+
),
|
|
230
|
+
) :
|
|
231
|
+
undefined;
|
|
229
232
|
|
|
230
233
|
// Commented code finds value by default of function's variable
|
|
231
234
|
// let defaultValue = undefined;
|
|
232
|
-
if (baseParam.type ===
|
|
235
|
+
if (baseParam.type === 'AssignmentPattern') {
|
|
233
236
|
// if (baseParam?.right?.type === 'Literal')
|
|
234
237
|
// defaultValue = baseParam?.right?.raw;
|
|
235
238
|
baseParam = baseParam?.left;
|
|
236
239
|
}
|
|
237
240
|
|
|
238
|
-
if (baseParam.type ===
|
|
241
|
+
if (baseParam.type === 'RestElement' || baseParam.type === 'Identifier') {
|
|
239
242
|
let name =
|
|
240
|
-
baseParam.type ===
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
if (baseParam?.argument?.typeAnnotation)
|
|
243
|
+
baseParam.type === 'RestElement' ?
|
|
244
|
+
`...${baseParam.argument.name}` :
|
|
245
|
+
baseParam.name;
|
|
246
|
+
if (baseParam?.argument?.typeAnnotation) {
|
|
244
247
|
name +=
|
|
245
|
-
|
|
248
|
+
': ' +
|
|
246
249
|
generate(baseParam.argument.typeAnnotation.typeAnnotation).code;
|
|
250
|
+
}
|
|
247
251
|
|
|
248
|
-
let type =
|
|
252
|
+
let type = '';
|
|
249
253
|
if (baseParam?.typeAnnotation?.typeAnnotation)
|
|
250
254
|
type = generate(baseParam.typeAnnotation.typeAnnotation).code;
|
|
251
|
-
else type =
|
|
255
|
+
else type = 'any';
|
|
252
256
|
|
|
253
|
-
|
|
257
|
+
const params =
|
|
254
258
|
baseParam.typeAnnotation.typeAnnotation.typeArguments?.params;
|
|
255
|
-
if (type !==
|
|
256
|
-
type += `<${params.map((e) => e.typeName?.name ?? 'any').join(
|
|
259
|
+
if (type !== 'any' && params && params.length > 0)
|
|
260
|
+
type += `<${params.map((e) => e.typeName?.name ?? 'any').join(',')}>`;
|
|
257
261
|
|
|
258
|
-
return {
|
|
262
|
+
return {name: name, type: type, options: options};
|
|
259
263
|
}
|
|
260
264
|
// Commented code belove sets more strong types for ObjectPatterns and ArrayPatterns
|
|
261
265
|
// else if (baseParam.type === 'ObjectPattern' || baseParam.type === 'ArrayPattern') {
|
|
@@ -290,7 +294,7 @@ class FuncGeneratorPlugin {
|
|
|
290
294
|
|
|
291
295
|
// return { name: name, type: type, options: options };
|
|
292
296
|
// }
|
|
293
|
-
return {
|
|
297
|
+
return {name: 'value', type: 'any', options: undefined};
|
|
294
298
|
});
|
|
295
299
|
return params;
|
|
296
300
|
}
|
|
@@ -298,7 +302,7 @@ class FuncGeneratorPlugin {
|
|
|
298
302
|
_getTsFiles(root) {
|
|
299
303
|
const tsFiles = [];
|
|
300
304
|
const extPattern = /\.tsx?$/;
|
|
301
|
-
const excludedFiles = [
|
|
305
|
+
const excludedFiles = ['package-test.ts', 'package.g.ts'];
|
|
302
306
|
|
|
303
307
|
function findFiles(dir) {
|
|
304
308
|
const files = fs.readdirSync(dir);
|
|
@@ -315,7 +319,7 @@ class FuncGeneratorPlugin {
|
|
|
315
319
|
}
|
|
316
320
|
|
|
317
321
|
_walk(node, visitor, parent = null) {
|
|
318
|
-
if (!node || typeof node !==
|
|
322
|
+
if (!node || typeof node !== 'object') return;
|
|
319
323
|
|
|
320
324
|
visitor(node, parent);
|
|
321
325
|
|
|
@@ -323,40 +327,39 @@ class FuncGeneratorPlugin {
|
|
|
323
327
|
const value = node[key];
|
|
324
328
|
if (Array.isArray(value)) {
|
|
325
329
|
value.forEach((child) => {
|
|
326
|
-
if (child && typeof child.type ===
|
|
330
|
+
if (child && typeof child.type === 'string') {
|
|
327
331
|
this._walk(
|
|
328
332
|
child,
|
|
329
333
|
visitor,
|
|
330
|
-
node.type ===
|
|
334
|
+
node.type === 'ClassDeclaration' ? node : parent,
|
|
331
335
|
);
|
|
332
336
|
}
|
|
333
337
|
});
|
|
334
|
-
} else if (value && typeof value.type ===
|
|
338
|
+
} else if (value && typeof value.type === 'string') {
|
|
335
339
|
this._walk(
|
|
336
340
|
value,
|
|
337
341
|
visitor,
|
|
338
|
-
node.type ===
|
|
342
|
+
node.type === 'ClassDeclaration' ? node : parent,
|
|
339
343
|
);
|
|
340
344
|
}
|
|
341
345
|
}
|
|
342
346
|
}
|
|
343
347
|
|
|
344
348
|
_readReturnType(annotation) {
|
|
345
|
-
let resultType =
|
|
349
|
+
let resultType = 'dynamic';
|
|
346
350
|
let isArray = false;
|
|
347
351
|
if (
|
|
348
352
|
annotation &&
|
|
349
|
-
annotation.type !==
|
|
350
|
-
annotation.type !==
|
|
353
|
+
annotation.type !== 'TSUnionType' &&
|
|
354
|
+
annotation.type !== 'TSIntersectionType'
|
|
351
355
|
) {
|
|
352
356
|
|
|
353
|
-
if (annotation.typeName || annotation.type ===
|
|
357
|
+
if (annotation.typeName || annotation.type === 'TSTypeReference') {
|
|
354
358
|
resultType =
|
|
355
359
|
annotation.typeName?.right?.name ?? annotation.typeName?.name;
|
|
356
|
-
else if (annotation.type !==
|
|
360
|
+
} else if (annotation.type !== 'TSArrayType')
|
|
357
361
|
resultType = this._getTypeNameFromNode(annotation);
|
|
358
|
-
|
|
359
|
-
else if (annotation.elementType.type !== "TSTypeReference") {
|
|
362
|
+
else if (annotation.elementType.type !== 'TSTypeReference') {
|
|
360
363
|
isArray = true;
|
|
361
364
|
resultType = this._getTypeNameFromNode(annotation?.elementType);
|
|
362
365
|
} else {
|
|
@@ -378,18 +381,17 @@ class FuncGeneratorPlugin {
|
|
|
378
381
|
if (node?.type === 'ClassDeclaration')
|
|
379
382
|
return [];
|
|
380
383
|
|
|
381
|
-
if (annotation?.typeName?.name ===
|
|
384
|
+
if (annotation?.typeName?.name === 'Promise') {
|
|
382
385
|
const argumnets = annotation.typeArguments?.params;
|
|
383
|
-
if (argumnets && argumnets.length === 1)
|
|
386
|
+
if (argumnets && argumnets.length === 1)
|
|
384
387
|
annotation = argumnets[0];
|
|
385
|
-
|
|
388
|
+
else annotation = {};
|
|
386
389
|
}
|
|
387
390
|
|
|
388
|
-
if (annotation?.type ===
|
|
391
|
+
if (annotation?.type === 'TSTypeLiteral')
|
|
389
392
|
results = this._readOutputsFromReturnTypeObject(annotation);
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
let resultType = this._readReturnType(annotation);
|
|
393
|
+
else {
|
|
394
|
+
const resultType = this._readReturnType(annotation);
|
|
393
395
|
results.push({name: 'result', type: resultType});
|
|
394
396
|
if (resultType === 'void')
|
|
395
397
|
results = [];
|
|
@@ -399,8 +401,8 @@ class FuncGeneratorPlugin {
|
|
|
399
401
|
|
|
400
402
|
_readOutputsFromReturnTypeObject(node) {
|
|
401
403
|
let i = 0;
|
|
402
|
-
|
|
403
|
-
for (
|
|
404
|
+
const results = [];
|
|
405
|
+
for (const member of node.members) {
|
|
404
406
|
results.push({
|
|
405
407
|
name: member?.key?.name ?? `result${i}`,
|
|
406
408
|
type: this._readReturnType(member.typeAnnotation.typeAnnotation),
|
|
@@ -411,19 +413,19 @@ class FuncGeneratorPlugin {
|
|
|
411
413
|
}
|
|
412
414
|
|
|
413
415
|
_getTypeNameFromNode(typeNode) {
|
|
414
|
-
if (typeNode.type ===
|
|
416
|
+
if (typeNode.type === 'TSTypeReference')
|
|
415
417
|
return typeNode.typeName.name;
|
|
416
|
-
|
|
417
|
-
return
|
|
418
|
-
|
|
419
|
-
return
|
|
420
|
-
|
|
421
|
-
return
|
|
422
|
-
|
|
423
|
-
return
|
|
424
|
-
|
|
418
|
+
else if (typeNode.type === 'TSVoidKeyword')
|
|
419
|
+
return 'void';
|
|
420
|
+
else if (typeNode.type === 'TSNumberKeyword')
|
|
421
|
+
return 'number';
|
|
422
|
+
else if (typeNode.type === 'TSStringKeyword')
|
|
423
|
+
return 'string';
|
|
424
|
+
else if (typeNode.type === 'TSBooleanKeyword')
|
|
425
|
+
return 'boolean';
|
|
426
|
+
else
|
|
425
427
|
return typeNode.type;
|
|
426
|
-
|
|
428
|
+
|
|
427
429
|
}
|
|
428
430
|
|
|
429
431
|
_clearGeneratedFile() {
|
|
@@ -431,38 +433,41 @@ class FuncGeneratorPlugin {
|
|
|
431
433
|
}
|
|
432
434
|
|
|
433
435
|
_checkPackageFileForDecoratorsExport(packagePath) {
|
|
434
|
-
const content = fs.readFileSync(packagePath,
|
|
436
|
+
const content = fs.readFileSync(packagePath, 'utf-8');
|
|
435
437
|
const decoratorsExportRegex = /export\s*\*\s*from\s*'\.\/package\.g';/;
|
|
436
|
-
if (!decoratorsExportRegex.test(content))
|
|
438
|
+
if (!decoratorsExportRegex.test(content)) {
|
|
437
439
|
console.warn(
|
|
438
|
-
|
|
440
|
+
// eslint-disable-next-line max-len
|
|
441
|
+
`\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
442
|
);
|
|
443
|
+
}
|
|
440
444
|
}
|
|
441
445
|
|
|
442
446
|
_writeToPackageFile(filePath, imports, exp) {
|
|
443
447
|
if (imports.length !== exp.length) return;
|
|
444
|
-
let content = fs.readFileSync(filePath,
|
|
448
|
+
let content = fs.readFileSync(filePath, 'utf-8');
|
|
445
449
|
for (let i = 0; i < imports.length; i++) {
|
|
446
450
|
const importStatement = imports[i];
|
|
447
451
|
const exportStatement = exp[i];
|
|
448
452
|
if (!content.includes(importStatement.trim()))
|
|
449
453
|
content = annotationForGeneratedFile + importStatement + content + exportStatement;
|
|
450
454
|
}
|
|
451
|
-
fs.writeFileSync(filePath, content,
|
|
455
|
+
fs.writeFileSync(filePath, content, 'utf-8');
|
|
452
456
|
}
|
|
453
457
|
|
|
454
458
|
_insertImports(importArray) {
|
|
455
459
|
if (fs.existsSync(this.options.outputPath)) {
|
|
456
|
-
const content = fs.readFileSync(this.options.outputPath,
|
|
460
|
+
const content = fs.readFileSync(this.options.outputPath, 'utf-8');
|
|
457
461
|
if (content) importArray.push(content);
|
|
458
|
-
const output = importArray.join(
|
|
459
|
-
fs.writeFileSync(this.options.outputPath, `${output}`,
|
|
460
|
-
} else
|
|
462
|
+
const output = importArray.join('');
|
|
463
|
+
fs.writeFileSync(this.options.outputPath, `${output}`, 'utf-8');
|
|
464
|
+
} else {
|
|
461
465
|
fs.writeFileSync(
|
|
462
466
|
this.options.outputPath,
|
|
463
|
-
`${annotationForGeneratedFile}${baseImport}\n${importArray.join(
|
|
464
|
-
|
|
467
|
+
`${annotationForGeneratedFile}${baseImport}\n${importArray.join('')}`,
|
|
468
|
+
'utf-8',
|
|
465
469
|
);
|
|
470
|
+
}
|
|
466
471
|
}
|
|
467
472
|
}
|
|
468
473
|
|