@comet/admin-generator 8.0.0-beta.4 → 8.0.0-beta.6
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/admin-generator.js +3 -0
- package/lib/commands/generate/config/parseConfig.d.ts +1 -0
- package/lib/commands/generate/config/parseConfig.js +72 -0
- package/lib/commands/generate/config/transformConfig.d.ts +1 -0
- package/lib/commands/generate/config/transformConfig.js +239 -0
- package/lib/commands/generate/generate-command.d.ts +13 -14
- package/lib/commands/generate/generate-command.js +6 -6
- package/lib/commands/generate/generateForm/generateForm.js +51 -37
- package/lib/commands/generate/generateForm/generateFormField.js +1 -1
- package/lib/commands/generate/generateGrid/generateGrid.js +142 -54
- package/lib/commands/generate/generateGrid/generateGridToolbar.js +1 -1
- package/lib/commands/generate/utils/writeGenerated.js +63 -24
- package/package.json +11 -13
- package/lib/commands/generate/utils/tsMorphHelper.d.ts +0 -4
- package/lib/commands/generate/utils/tsMorphHelper.js +0 -216
|
@@ -1,216 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.morphTsSource = morphTsSource;
|
|
4
|
-
exports.configFromSourceFile = configFromSourceFile;
|
|
5
|
-
const ts_morph_1 = require("ts-morph");
|
|
6
|
-
const project = new ts_morph_1.Project({
|
|
7
|
-
tsConfigFilePath: "tsconfig.json",
|
|
8
|
-
skipAddingFilesFromTsConfig: true,
|
|
9
|
-
});
|
|
10
|
-
function morphTsSource(path) {
|
|
11
|
-
let tsSource = project.getSourceFile(path);
|
|
12
|
-
if (!tsSource)
|
|
13
|
-
tsSource = project.addSourceFileAtPath(path);
|
|
14
|
-
return tsSource;
|
|
15
|
-
}
|
|
16
|
-
const supportedImportPaths = [
|
|
17
|
-
"[type=grid].columns.filterOperators",
|
|
18
|
-
"[type=grid].columns.block",
|
|
19
|
-
"[type=grid].columns.component",
|
|
20
|
-
// TODO implement in generator "[type=grid].columns.renderCell",
|
|
21
|
-
//support in up to 5 levels of nested fields (eg. fieldSet)
|
|
22
|
-
...Array.from(Array(5).keys()).map((i) => `[type=form]${".fields".repeat(i + 1)}.validate`),
|
|
23
|
-
...Array.from(Array(5).keys()).map((i) => `[type=form]${".fields".repeat(i + 1)}.block`),
|
|
24
|
-
...Array.from(Array(5).keys()).map((i) => `[type=form]${".fields".repeat(i + 1)}.component`),
|
|
25
|
-
];
|
|
26
|
-
const supportedInlineCodePaths = [
|
|
27
|
-
// TODO implement in generator "[type=grid].columns.filterOperators",
|
|
28
|
-
"[type=grid].columns.renderCell",
|
|
29
|
-
//support in up to 5 levels of nested fields (eg. fieldSet)
|
|
30
|
-
...Array.from(Array(5).keys()).map((i) => `[type=form]${".fields".repeat(i + 1)}.validate`),
|
|
31
|
-
];
|
|
32
|
-
function configFromSourceFile(sourceFile) {
|
|
33
|
-
for (const [name, declarations] of Array.from(sourceFile.getExportedDeclarations().entries())) {
|
|
34
|
-
if (name == "default") {
|
|
35
|
-
if (declarations.length != 1) {
|
|
36
|
-
throw new Error(`Expected exactly one declaration for ${name}`);
|
|
37
|
-
}
|
|
38
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
39
|
-
const config = exportedDeclarationToJson(declarations[0]);
|
|
40
|
-
//console.dir(config, { depth: 10 });
|
|
41
|
-
return config;
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
//ignore this export
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
throw new Error(`No default export found, please export the GeneratorConfig as default, preferrable using defineConfig helper.`);
|
|
48
|
-
}
|
|
49
|
-
function findUsedImports(node) {
|
|
50
|
-
const imports = [];
|
|
51
|
-
node.getDescendantsOfKind(ts_morph_1.SyntaxKind.Identifier).forEach((identifier) => {
|
|
52
|
-
var _a;
|
|
53
|
-
for (const referencedSymbol of identifier.findReferences()) {
|
|
54
|
-
for (const reference of referencedSymbol.getReferences()) {
|
|
55
|
-
const referenceNode = reference.getNode();
|
|
56
|
-
const importDeclaration = referenceNode.getFirstAncestorByKind(ts_morph_1.SyntaxKind.ImportDeclaration);
|
|
57
|
-
if (importDeclaration) {
|
|
58
|
-
for (const namedImport of importDeclaration.getNamedImports()) {
|
|
59
|
-
if (((_a = (namedImport.getAliasNode() || namedImport.getNameNode())) === null || _a === void 0 ? void 0 : _a.getText()) == referenceNode.getText()) {
|
|
60
|
-
imports.push({
|
|
61
|
-
name: namedImport.getNameNode().getText(),
|
|
62
|
-
import: importDeclaration.getModuleSpecifierValue(),
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
return imports;
|
|
71
|
-
}
|
|
72
|
-
function getTypePropertyFromObjectLiteral(node) {
|
|
73
|
-
for (const property of node.getProperties()) {
|
|
74
|
-
if (property.getKind() == ts_morph_1.SyntaxKind.PropertyAssignment) {
|
|
75
|
-
const propertyAssignment = property;
|
|
76
|
-
if (propertyAssignment.getName() == "type") {
|
|
77
|
-
const propertyAssignmentInitializer = propertyAssignment.getInitializer();
|
|
78
|
-
if ((propertyAssignmentInitializer === null || propertyAssignmentInitializer === void 0 ? void 0 : propertyAssignmentInitializer.getKind()) == ts_morph_1.SyntaxKind.StringLiteral) {
|
|
79
|
-
return propertyAssignmentInitializer.getLiteralValue();
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
return null;
|
|
85
|
-
}
|
|
86
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
87
|
-
function astNodeToJson(node, path) {
|
|
88
|
-
var _a;
|
|
89
|
-
if (node.getKind() == ts_morph_1.SyntaxKind.ObjectLiteralExpression) {
|
|
90
|
-
if (path == "") {
|
|
91
|
-
// first entry of path is the type, then property names (. separated) are added
|
|
92
|
-
const typeProperty = getTypePropertyFromObjectLiteral(node);
|
|
93
|
-
path = typeProperty ? `[type=${typeProperty}]` : "";
|
|
94
|
-
}
|
|
95
|
-
const ret = {};
|
|
96
|
-
for (const property of node.getProperties()) {
|
|
97
|
-
if (property.getKind() == ts_morph_1.SyntaxKind.PropertyAssignment) {
|
|
98
|
-
const propertyAssignment = property;
|
|
99
|
-
const propertyAssignmentInitializer = propertyAssignment.getInitializer();
|
|
100
|
-
if (propertyAssignmentInitializer) {
|
|
101
|
-
ret[propertyAssignment.getName()] = astNodeToJson(propertyAssignmentInitializer, `${path}.${propertyAssignment.getName()}`);
|
|
102
|
-
}
|
|
103
|
-
else {
|
|
104
|
-
throw new Error(`Initializer is required for propertyAssignment '${propertyAssignment.getName()}'`);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
else {
|
|
108
|
-
throw new Error(`Unsupported ObjectLiteralExpression property Kind ${property.getKindName()}`);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
return ret;
|
|
112
|
-
}
|
|
113
|
-
else if (node.getKind() == ts_morph_1.SyntaxKind.StringLiteral) {
|
|
114
|
-
return node.getLiteralValue();
|
|
115
|
-
}
|
|
116
|
-
else if (node.getKind() == ts_morph_1.SyntaxKind.NumericLiteral) {
|
|
117
|
-
return node.getLiteralValue();
|
|
118
|
-
}
|
|
119
|
-
else if (node.getKind() == ts_morph_1.SyntaxKind.FalseKeyword) {
|
|
120
|
-
return false;
|
|
121
|
-
}
|
|
122
|
-
else if (node.getKind() == ts_morph_1.SyntaxKind.TrueKeyword) {
|
|
123
|
-
return true;
|
|
124
|
-
}
|
|
125
|
-
else if (node.getKind() == ts_morph_1.SyntaxKind.BinaryExpression) {
|
|
126
|
-
//what is this?
|
|
127
|
-
return node.getText();
|
|
128
|
-
}
|
|
129
|
-
else if (node.getKind() == ts_morph_1.SyntaxKind.ArrowFunction) {
|
|
130
|
-
if (supportedInlineCodePaths.includes(path)) {
|
|
131
|
-
const body = node.getBody();
|
|
132
|
-
return {
|
|
133
|
-
code: node.getText(),
|
|
134
|
-
imports: findUsedImports(body),
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
else {
|
|
138
|
-
throw new Error(`Inline Function is not allowed here and calling the function is not supported: ${path}`);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
else if (node.getKind() == ts_morph_1.SyntaxKind.ArrayLiteralExpression) {
|
|
142
|
-
const ret = [];
|
|
143
|
-
for (const element of node.getElements()) {
|
|
144
|
-
ret.push(astNodeToJson(element, path));
|
|
145
|
-
}
|
|
146
|
-
return ret;
|
|
147
|
-
}
|
|
148
|
-
else if (node.getKind() == ts_morph_1.SyntaxKind.Identifier) {
|
|
149
|
-
for (const referencedSymbol of node.findReferences()) {
|
|
150
|
-
for (const reference of referencedSymbol.getReferences()) {
|
|
151
|
-
const referenceNode = reference.getNode();
|
|
152
|
-
const importDeclaration = referenceNode.getFirstAncestorByKind(ts_morph_1.SyntaxKind.ImportDeclaration);
|
|
153
|
-
const variableDeclaration = referenceNode.getFirstAncestorByKind(ts_morph_1.SyntaxKind.VariableDeclaration);
|
|
154
|
-
if (importDeclaration) {
|
|
155
|
-
for (const namedImport of importDeclaration.getNamedImports()) {
|
|
156
|
-
if (((_a = (namedImport.getAliasNode() || namedImport.getNameNode())) === null || _a === void 0 ? void 0 : _a.getText()) == referenceNode.getText()) {
|
|
157
|
-
if (supportedImportPaths.includes(path)) {
|
|
158
|
-
return {
|
|
159
|
-
name: namedImport.getNameNode().getText(),
|
|
160
|
-
import: importDeclaration.getModuleSpecifierValue(),
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
else {
|
|
164
|
-
throw new Error(`Following the import is not supported: ${path} ${namedImport.getText()}`);
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
else if (variableDeclaration) {
|
|
170
|
-
if (variableDeclaration.getName() == node.getText()) {
|
|
171
|
-
const variableInitializer = variableDeclaration.getInitializer();
|
|
172
|
-
if (variableInitializer) {
|
|
173
|
-
return astNodeToJson(variableInitializer, path);
|
|
174
|
-
}
|
|
175
|
-
else {
|
|
176
|
-
throw new Error(`Initializer is required for variableDeclaration '${variableDeclaration.getName()}'`);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
throw new Error(`Unsupported identifier '${node.getText()}', only imports and are variable declarations with initializer are supported`);
|
|
183
|
-
}
|
|
184
|
-
else {
|
|
185
|
-
throw new Error(`Unsupported expression Kind ${node.getKindName()}`);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
189
|
-
function exportedDeclarationToJson(node) {
|
|
190
|
-
if (node.getKind() == ts_morph_1.SyntaxKind.VariableDeclaration) {
|
|
191
|
-
const variableDeclaration = node;
|
|
192
|
-
const initializer = variableDeclaration.getInitializer();
|
|
193
|
-
if (initializer) {
|
|
194
|
-
return astNodeToJson(initializer, "");
|
|
195
|
-
}
|
|
196
|
-
else {
|
|
197
|
-
throw new Error(`Initializer is required for variableDeclaration '${variableDeclaration.getName()}'`);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
else if (node.getKind() == ts_morph_1.SyntaxKind.CallExpression) {
|
|
201
|
-
const callExpression = node;
|
|
202
|
-
if (callExpression.getExpression().getText() == "defineConfig") {
|
|
203
|
-
const args = callExpression.getArguments();
|
|
204
|
-
if (args.length != 1) {
|
|
205
|
-
throw new Error(`Expected exactly one argument for defineConfig`);
|
|
206
|
-
}
|
|
207
|
-
return astNodeToJson(args[0], "");
|
|
208
|
-
}
|
|
209
|
-
else {
|
|
210
|
-
throw new Error(`Only direct call to defineConfig is supported`);
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
else {
|
|
214
|
-
throw new Error(`Unsupported declaration kind '${node.getKindName()}'`);
|
|
215
|
-
}
|
|
216
|
-
}
|