@snowtop/ent 0.1.0-alpha2 → 0.1.0-alpha25
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/action/action.d.ts +28 -24
- package/action/executor.d.ts +4 -4
- package/action/executor.js +2 -2
- package/action/experimental_action.d.ts +21 -20
- package/action/experimental_action.js +11 -5
- package/action/orchestrator.d.ts +31 -16
- package/action/orchestrator.js +156 -43
- package/action/privacy.d.ts +2 -2
- package/core/base.d.ts +25 -21
- package/core/base.js +16 -0
- package/core/clause.d.ts +24 -3
- package/core/clause.js +246 -5
- package/core/config.d.ts +23 -0
- package/core/config.js +17 -0
- package/core/context.d.ts +2 -2
- package/core/db.d.ts +3 -3
- package/core/db.js +2 -0
- package/core/ent.d.ts +19 -21
- package/core/ent.js +76 -25
- package/core/loaders/assoc_count_loader.d.ts +2 -2
- package/core/loaders/assoc_edge_loader.d.ts +3 -3
- package/core/loaders/assoc_edge_loader.js +5 -4
- package/core/loaders/index_loader.js +1 -0
- package/core/loaders/object_loader.d.ts +10 -5
- package/core/loaders/object_loader.js +59 -4
- package/core/loaders/query_loader.d.ts +2 -2
- package/core/loaders/raw_count_loader.d.ts +2 -2
- package/core/privacy.d.ts +25 -25
- package/core/privacy.js +3 -0
- package/core/query/assoc_query.d.ts +6 -6
- package/core/query/custom_query.d.ts +5 -5
- package/core/query/query.d.ts +1 -1
- package/core/viewer.d.ts +4 -3
- package/core/viewer.js +4 -0
- package/graphql/builtins/connection.js +3 -3
- package/graphql/builtins/edge.js +2 -2
- package/graphql/builtins/node.js +1 -1
- package/graphql/graphql.d.ts +3 -2
- package/graphql/graphql.js +24 -23
- package/graphql/index.d.ts +1 -0
- package/graphql/index.js +3 -1
- package/graphql/mutations/union.d.ts +2 -0
- package/graphql/mutations/union.js +35 -0
- package/graphql/node_resolver.d.ts +0 -1
- package/graphql/query/connection_type.js +6 -6
- package/graphql/query/edge_connection.d.ts +9 -9
- package/graphql/query/page_info.d.ts +1 -1
- package/graphql/query/page_info.js +4 -4
- package/graphql/query/shared_assoc_test.js +2 -2
- package/graphql/scalars/time.d.ts +1 -1
- package/index.d.ts +16 -1
- package/index.js +18 -5
- package/package.json +3 -3
- package/parse_schema/parse.d.ts +23 -4
- package/parse_schema/parse.js +68 -6
- package/schema/base_schema.d.ts +36 -1
- package/schema/base_schema.js +51 -2
- package/schema/field.d.ts +2 -2
- package/schema/field.js +11 -3
- package/schema/index.d.ts +4 -2
- package/schema/index.js +10 -1
- package/schema/schema.d.ts +64 -2
- package/schema/schema.js +126 -5
- package/schema/struct_field.d.ts +17 -0
- package/schema/struct_field.js +102 -0
- package/schema/union_field.d.ts +23 -0
- package/schema/union_field.js +79 -0
- package/scripts/custom_graphql.js +122 -15
- package/scripts/{transform_schema.d.ts → migrate_v0.1.d.ts} +0 -0
- package/scripts/migrate_v0.1.js +36 -0
- package/scripts/read_schema.js +15 -1
- package/testutils/builder.d.ts +31 -21
- package/testutils/builder.js +98 -13
- package/testutils/context/test_context.d.ts +2 -2
- package/testutils/context/test_context.js +7 -1
- package/testutils/db/test_db.d.ts +2 -1
- package/testutils/db/test_db.js +13 -4
- package/testutils/ent-graphql-tests/index.d.ts +2 -0
- package/testutils/ent-graphql-tests/index.js +26 -17
- package/testutils/fake_data/fake_contact.d.ts +4 -8
- package/testutils/fake_data/fake_contact.js +15 -19
- package/testutils/fake_data/fake_event.d.ts +4 -8
- package/testutils/fake_data/fake_event.js +21 -25
- package/testutils/fake_data/fake_user.d.ts +5 -9
- package/testutils/fake_data/fake_user.js +23 -27
- package/testutils/fake_data/test_helpers.js +1 -1
- package/testutils/fake_data/user_query.d.ts +2 -2
- package/testutils/fake_log.d.ts +3 -3
- package/tsc/ast.d.ts +43 -0
- package/tsc/ast.js +264 -0
- package/tsc/compilerOptions.d.ts +6 -0
- package/tsc/compilerOptions.js +40 -1
- package/tsc/move_generated.d.ts +1 -0
- package/tsc/move_generated.js +160 -0
- package/tsc/transform.d.ts +21 -0
- package/tsc/transform.js +167 -0
- package/tsc/transform_action.d.ts +20 -0
- package/tsc/transform_action.js +169 -0
- package/tsc/transform_ent.d.ts +17 -0
- package/tsc/transform_ent.js +59 -0
- package/tsc/transform_schema.d.ts +27 -0
- package/tsc/transform_schema.js +354 -0
- package/scripts/transform_schema.js +0 -288
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
22
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
23
|
+
};
|
|
24
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
+
exports.TransformSchema = void 0;
|
|
26
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
27
|
+
const fs = __importStar(require("fs"));
|
|
28
|
+
const path_1 = __importDefault(require("path"));
|
|
29
|
+
const ast_1 = require("../tsc/ast");
|
|
30
|
+
function traverseClass(fileContents, sourceFile, node, transformSchema) {
|
|
31
|
+
const ci = getTransformClassInfo(fileContents, sourceFile, node, transformSchema);
|
|
32
|
+
if (!ci) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
let klassContents = `${ci.comment}const ${ci.name} = new ${transformSchema(ci.class)}({\n`;
|
|
36
|
+
let removeImports = [];
|
|
37
|
+
if (ci.implementsSchema) {
|
|
38
|
+
removeImports.push("Schema");
|
|
39
|
+
}
|
|
40
|
+
removeImports.push(ci.class);
|
|
41
|
+
let newImports = [transformSchema(ci.class)];
|
|
42
|
+
for (let member of node.members) {
|
|
43
|
+
const fInfo = getClassElementInfo(fileContents, member, sourceFile);
|
|
44
|
+
if (!fInfo) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
klassContents += `${fInfo.comment}${fInfo.key}:${fInfo.value},\n`;
|
|
48
|
+
if (fInfo.type) {
|
|
49
|
+
removeImports.push(fInfo.type);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
klassContents += "\n})";
|
|
53
|
+
if (ci.export && ci.default) {
|
|
54
|
+
klassContents += `\n export default ${ci.name};`;
|
|
55
|
+
}
|
|
56
|
+
else if (ci.export) {
|
|
57
|
+
klassContents = "export " + klassContents;
|
|
58
|
+
}
|
|
59
|
+
// console.debug(klassContents);
|
|
60
|
+
return {
|
|
61
|
+
rawString: klassContents,
|
|
62
|
+
removeImports: removeImports,
|
|
63
|
+
newImports,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function getTransformClassInfo(fileContents, sourceFile, node, transformSchema) {
|
|
67
|
+
const generic = (0, ast_1.getClassInfo)(fileContents, sourceFile, node);
|
|
68
|
+
if (!generic) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
let className = generic.name;
|
|
72
|
+
if (!className?.endsWith("Schema")) {
|
|
73
|
+
className += "Schema";
|
|
74
|
+
}
|
|
75
|
+
let implementsSchema = generic.implements?.some((v) => v == "Schema");
|
|
76
|
+
let classExtends = generic.extends;
|
|
77
|
+
// nothing transformed here, so nothing to do here
|
|
78
|
+
if (classExtends && classExtends === transformSchema(classExtends)) {
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
if (!className || !node.heritageClauses || !classExtends) {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
let ci = {
|
|
85
|
+
...generic,
|
|
86
|
+
name: className,
|
|
87
|
+
class: classExtends,
|
|
88
|
+
implementsSchema,
|
|
89
|
+
};
|
|
90
|
+
return ci;
|
|
91
|
+
}
|
|
92
|
+
// intentionally doesn't parse decorators since we don't need it
|
|
93
|
+
function getClassElementInfo(fileContents, member, sourceFile) {
|
|
94
|
+
if (isFieldElement(member, sourceFile)) {
|
|
95
|
+
return getFieldElementInfo(fileContents, member, sourceFile);
|
|
96
|
+
}
|
|
97
|
+
if (member.kind === typescript_1.default.SyntaxKind.Constructor) {
|
|
98
|
+
return getConstructorElementInfo(fileContents, member, sourceFile);
|
|
99
|
+
}
|
|
100
|
+
if (member.kind !== typescript_1.default.SyntaxKind.PropertyDeclaration) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
// other properties
|
|
104
|
+
const property = member;
|
|
105
|
+
if (!property.initializer) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
const token = property.name;
|
|
109
|
+
return {
|
|
110
|
+
key: token.escapedText.toString(),
|
|
111
|
+
value: property.initializer?.getFullText(sourceFile),
|
|
112
|
+
comment: (0, ast_1.getPreText)(fileContents, member, sourceFile),
|
|
113
|
+
type: getType(property, sourceFile),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
function getType(property, sourceFile) {
|
|
117
|
+
let propertytype = property.type?.getText(sourceFile) || "";
|
|
118
|
+
let ends = ["| null", "[]"];
|
|
119
|
+
for (const end of ends) {
|
|
120
|
+
if (propertytype.endsWith(end)) {
|
|
121
|
+
propertytype = propertytype.slice(0, -1 * end.length);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return propertytype;
|
|
125
|
+
}
|
|
126
|
+
function getFieldElementInfo(fileContents, member, sourceFile) {
|
|
127
|
+
let fieldMap = "";
|
|
128
|
+
// need to change to fields: {code: StringType()};
|
|
129
|
+
const property = member;
|
|
130
|
+
const initializer = property.initializer;
|
|
131
|
+
fieldMap += "{";
|
|
132
|
+
for (const element of initializer.elements) {
|
|
133
|
+
const parsed = parseFieldElement(element, sourceFile, fileContents);
|
|
134
|
+
if (parsed === null) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
const { callEx, name, nameComment, properties, suffix } = parsed;
|
|
138
|
+
let property = "";
|
|
139
|
+
const fieldComment = (0, ast_1.getPreText)(fileContents, element, sourceFile).trim();
|
|
140
|
+
if (fieldComment) {
|
|
141
|
+
property += "\n" + fieldComment + "\n";
|
|
142
|
+
}
|
|
143
|
+
if (nameComment) {
|
|
144
|
+
property += nameComment + "\n";
|
|
145
|
+
}
|
|
146
|
+
// e.g. UUIDType, StringType etc
|
|
147
|
+
let call = callEx.expression.getText(sourceFile);
|
|
148
|
+
let fnCall = "";
|
|
149
|
+
if (properties.length) {
|
|
150
|
+
fnCall = `{${properties.join(",")}}`;
|
|
151
|
+
}
|
|
152
|
+
property += `${name}:${call}(${fnCall})${suffix || ""},`;
|
|
153
|
+
fieldMap += property;
|
|
154
|
+
}
|
|
155
|
+
fieldMap += "}";
|
|
156
|
+
return {
|
|
157
|
+
key: "fields",
|
|
158
|
+
value: fieldMap,
|
|
159
|
+
comment: (0, ast_1.getPreText)(fileContents, member, sourceFile),
|
|
160
|
+
type: getType(property, sourceFile),
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
function getConstructorElementInfo(fileContents, member, sourceFile) {
|
|
164
|
+
const c = member;
|
|
165
|
+
//remove {}
|
|
166
|
+
let fullText = c.body?.getFullText(sourceFile) || "";
|
|
167
|
+
fullText = fullText.trim().slice(1, -1).trim();
|
|
168
|
+
// convert something like
|
|
169
|
+
/*
|
|
170
|
+
constructor() {
|
|
171
|
+
super();
|
|
172
|
+
this.addPatterns(
|
|
173
|
+
new Feedback(),
|
|
174
|
+
new DayOfWeek(),
|
|
175
|
+
new Feedback(),
|
|
176
|
+
new DayOfWeek(),
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
*/
|
|
180
|
+
// into this.addPatterns(new Feedback(),new DayOfWeek(),new Feedback(),new DayOfWeek(),)
|
|
181
|
+
const lines = fullText
|
|
182
|
+
.split("\n")
|
|
183
|
+
.map((line) => line.trim())
|
|
184
|
+
.join("")
|
|
185
|
+
.split(";")
|
|
186
|
+
.filter((f) => f != "super()" && f != "");
|
|
187
|
+
// at this point there should be only line for what we handle
|
|
188
|
+
if (lines.length != 1) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
const line = lines[0];
|
|
192
|
+
const addPatterns = "this.addPatterns(";
|
|
193
|
+
if (!line.startsWith(addPatterns)) {
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
return {
|
|
197
|
+
key: "patterns",
|
|
198
|
+
// remove this.addPatterns at the front, remove trailing ) at the end
|
|
199
|
+
// if there's a trailing comma, it'll be handled by prettier
|
|
200
|
+
value: `[${line.slice(addPatterns.length, -1)}]`,
|
|
201
|
+
comment: "",
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
function isFieldElement(member, sourceFile) {
|
|
205
|
+
if (member.kind !== typescript_1.default.SyntaxKind.PropertyDeclaration) {
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
208
|
+
const property = member;
|
|
209
|
+
const token = property.name;
|
|
210
|
+
if (token.escapedText !== "fields") {
|
|
211
|
+
return false;
|
|
212
|
+
}
|
|
213
|
+
const propertytype = property.type?.getText(sourceFile);
|
|
214
|
+
if (propertytype !== "Field[]") {
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
217
|
+
if (property.initializer?.kind !== typescript_1.default.SyntaxKind.ArrayLiteralExpression) {
|
|
218
|
+
console.error("invalid array type");
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
return true;
|
|
222
|
+
}
|
|
223
|
+
function parseFieldElement(element, sourceFile, fileContents, nested) {
|
|
224
|
+
if (element.kind !== typescript_1.default.SyntaxKind.CallExpression) {
|
|
225
|
+
console.error("skipped non-call expression");
|
|
226
|
+
return null;
|
|
227
|
+
}
|
|
228
|
+
let callEx = element;
|
|
229
|
+
if (callEx.arguments.length !== 1) {
|
|
230
|
+
// have a situation like: StringType({ name: "canonicalName" }).trim().toLowerCase(),
|
|
231
|
+
// need to keep calling this until we find what we want and then get the suffix we should just add to the end of the transformed code
|
|
232
|
+
if (callEx.expression.kind === typescript_1.default.SyntaxKind.PropertyAccessExpression) {
|
|
233
|
+
const ret = parseFieldElement(callEx.expression.expression, sourceFile, fileContents, true);
|
|
234
|
+
if (ret !== null) {
|
|
235
|
+
if (!nested) {
|
|
236
|
+
ret.suffix = fileContents.substring(ret.callEx.getEnd(), callEx.getEnd());
|
|
237
|
+
}
|
|
238
|
+
return ret;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
console.error("callExpression with arguments not of length 1");
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
let arg = callEx.arguments[0];
|
|
245
|
+
if (arg.kind !== typescript_1.default.SyntaxKind.ObjectLiteralExpression) {
|
|
246
|
+
console.error("not objectLiteralExpression");
|
|
247
|
+
return null;
|
|
248
|
+
}
|
|
249
|
+
let expr = arg;
|
|
250
|
+
let name = "";
|
|
251
|
+
let propertyComment;
|
|
252
|
+
let properties = [];
|
|
253
|
+
for (const p of expr.properties) {
|
|
254
|
+
const p2 = p;
|
|
255
|
+
// found name property
|
|
256
|
+
if (p2.name.escapedText === "name") {
|
|
257
|
+
name = p2.initializer.getText(sourceFile);
|
|
258
|
+
// check for any comment associated with name: "fooo"
|
|
259
|
+
propertyComment = (0, ast_1.getPreText)(fileContents, p, sourceFile).trim();
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
properties.push(p.getFullText(sourceFile));
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
if (!name) {
|
|
266
|
+
console.error(`couldn't find name property`);
|
|
267
|
+
return null;
|
|
268
|
+
}
|
|
269
|
+
// remove quotes
|
|
270
|
+
name = name.slice(1, -1);
|
|
271
|
+
return {
|
|
272
|
+
callEx,
|
|
273
|
+
name,
|
|
274
|
+
properties,
|
|
275
|
+
nameComment: propertyComment,
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
// find which of these importPaths is being used and use that to replace
|
|
279
|
+
function findSchemaImportPath(sourceFile) {
|
|
280
|
+
const paths = {
|
|
281
|
+
"@snowtop/ent": true,
|
|
282
|
+
"@snowtop/ent/schema": true,
|
|
283
|
+
"@snowtop/ent/schema/": true,
|
|
284
|
+
};
|
|
285
|
+
// @ts-ignore
|
|
286
|
+
const importStatements = sourceFile.statements.filter((stmt) => typescript_1.default.isImportDeclaration(stmt));
|
|
287
|
+
for (const imp of importStatements) {
|
|
288
|
+
const impInfo = (0, ast_1.getImportInfo)(imp, sourceFile);
|
|
289
|
+
if (!impInfo) {
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
if (paths[impInfo.importPath] !== undefined) {
|
|
293
|
+
return impInfo.importPath;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
class TransformSchema {
|
|
298
|
+
// we only end up doing this once because we change the schema representation
|
|
299
|
+
// so safe to run this multiple times
|
|
300
|
+
constructor(relativeImports, oldBaseClass, newSchemaClass, transformPath) {
|
|
301
|
+
this.relativeImports = relativeImports;
|
|
302
|
+
this.oldBaseClass = oldBaseClass;
|
|
303
|
+
this.newSchemaClass = newSchemaClass;
|
|
304
|
+
this.transformPath = transformPath;
|
|
305
|
+
this.glob = "src/schema/*.ts";
|
|
306
|
+
this.prettierGlob = "src/schema/*.ts";
|
|
307
|
+
}
|
|
308
|
+
transformSchema(className) {
|
|
309
|
+
if (className === "BaseEntSchema" || className === "BaseEntSchemaWithTZ") {
|
|
310
|
+
return className.substring(4);
|
|
311
|
+
}
|
|
312
|
+
if (className === this.oldBaseClass && this.newSchemaClass) {
|
|
313
|
+
return this.newSchemaClass;
|
|
314
|
+
}
|
|
315
|
+
return className;
|
|
316
|
+
}
|
|
317
|
+
traverseChild(sourceFile, contents, file, node) {
|
|
318
|
+
if (!typescript_1.default.isClassDeclaration(node)) {
|
|
319
|
+
return { node };
|
|
320
|
+
}
|
|
321
|
+
// TODO address implicit schema doesn't work here...
|
|
322
|
+
const ret = traverseClass(contents, sourceFile, node, this.transformSchema.bind(this));
|
|
323
|
+
if (ret === undefined) {
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
let imports = new Map();
|
|
327
|
+
const imp = findSchemaImportPath(sourceFile);
|
|
328
|
+
if (imp) {
|
|
329
|
+
if (this.transformPath) {
|
|
330
|
+
imports.set(imp, []);
|
|
331
|
+
}
|
|
332
|
+
else {
|
|
333
|
+
imports.set(imp, ret.newImports);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
if (this.transformPath) {
|
|
337
|
+
// add new imports to this path
|
|
338
|
+
imports.set((0, ast_1.transformRelative)(file, this.transformPath, this.relativeImports), ret.newImports);
|
|
339
|
+
}
|
|
340
|
+
return {
|
|
341
|
+
traversed: true,
|
|
342
|
+
rawString: ret.rawString,
|
|
343
|
+
removeImports: ret.removeImports,
|
|
344
|
+
imports: imports,
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
fileToWrite(file) {
|
|
348
|
+
return "src/schema/" + path_1.default.basename(file).slice(0, -3) + "_schema.ts";
|
|
349
|
+
}
|
|
350
|
+
postProcess(file) {
|
|
351
|
+
fs.rmSync(file);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
exports.TransformSchema = TransformSchema;
|
|
@@ -1,288 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
-
}) : (function(o, m, k, k2) {
|
|
6
|
-
if (k2 === undefined) k2 = k;
|
|
7
|
-
o[k2] = m[k];
|
|
8
|
-
}));
|
|
9
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
-
}) : function(o, v) {
|
|
12
|
-
o["default"] = v;
|
|
13
|
-
});
|
|
14
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
-
if (mod && mod.__esModule) return mod;
|
|
16
|
-
var result = {};
|
|
17
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
-
__setModuleDefault(result, mod);
|
|
19
|
-
return result;
|
|
20
|
-
};
|
|
21
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
22
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
23
|
-
};
|
|
24
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
-
const glob_1 = require("glob");
|
|
26
|
-
const typescript_1 = __importDefault(require("typescript"));
|
|
27
|
-
const fs = __importStar(require("fs"));
|
|
28
|
-
const compilerOptions_1 = require("../tsc/compilerOptions");
|
|
29
|
-
const child_process_1 = require("child_process");
|
|
30
|
-
function getTarget(target) {
|
|
31
|
-
switch (target.toLowerCase()) {
|
|
32
|
-
case "es2015":
|
|
33
|
-
return typescript_1.default.ScriptTarget.ES2015;
|
|
34
|
-
case "es2016":
|
|
35
|
-
return typescript_1.default.ScriptTarget.ES2016;
|
|
36
|
-
case "es2017":
|
|
37
|
-
return typescript_1.default.ScriptTarget.ES2017;
|
|
38
|
-
case "es2018":
|
|
39
|
-
return typescript_1.default.ScriptTarget.ES2018;
|
|
40
|
-
case "es2019":
|
|
41
|
-
return typescript_1.default.ScriptTarget.ES2019;
|
|
42
|
-
case "es2020":
|
|
43
|
-
return typescript_1.default.ScriptTarget.ES2020;
|
|
44
|
-
case "es2021":
|
|
45
|
-
return typescript_1.default.ScriptTarget.ES2021;
|
|
46
|
-
case "es3":
|
|
47
|
-
return typescript_1.default.ScriptTarget.ES3;
|
|
48
|
-
case "es5":
|
|
49
|
-
return typescript_1.default.ScriptTarget.ES5;
|
|
50
|
-
case "esnext":
|
|
51
|
-
return typescript_1.default.ScriptTarget.ESNext;
|
|
52
|
-
default:
|
|
53
|
-
return typescript_1.default.ScriptTarget.ESNext;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
async function main() {
|
|
57
|
-
// this assumes this is being run from root of directory
|
|
58
|
-
const options = (0, compilerOptions_1.readCompilerOptions)(".");
|
|
59
|
-
let files = glob_1.glob.sync("src/schema/*.ts");
|
|
60
|
-
const target = options.target
|
|
61
|
-
? // @ts-ignore
|
|
62
|
-
getTarget(options.target)
|
|
63
|
-
: typescript_1.default.ScriptTarget.ESNext;
|
|
64
|
-
// filter to only event.ts e.g. for comments and whitespace...
|
|
65
|
-
// files = files.filter((f) => f.endsWith("event.ts"));
|
|
66
|
-
files.forEach((file) => {
|
|
67
|
-
// assume valid file since we do glob above
|
|
68
|
-
const idx = file.lastIndexOf(".ts");
|
|
69
|
-
const writeFile = file.substring(0, idx) + "2" + ".ts";
|
|
70
|
-
let contents = fs.readFileSync(file).toString();
|
|
71
|
-
// go through the file and print everything back if not starting immediately after other position
|
|
72
|
-
const sourceFile = typescript_1.default.createSourceFile(file, contents, target, false, typescript_1.default.ScriptKind.TS);
|
|
73
|
-
const nodes = [];
|
|
74
|
-
let updateImport = false;
|
|
75
|
-
const f = {
|
|
76
|
-
trackNode: function (tni) {
|
|
77
|
-
nodes.push({
|
|
78
|
-
node: tni.node,
|
|
79
|
-
importNode: tni.node && typescript_1.default.isImportDeclaration(tni.node),
|
|
80
|
-
rawString: tni.rawString,
|
|
81
|
-
});
|
|
82
|
-
},
|
|
83
|
-
flagUpdateImport() {
|
|
84
|
-
updateImport = true;
|
|
85
|
-
},
|
|
86
|
-
};
|
|
87
|
-
if (!traverse(contents, sourceFile, f)) {
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
let newContents = "";
|
|
91
|
-
for (const node of nodes) {
|
|
92
|
-
if (updateImport && node.importNode) {
|
|
93
|
-
const importNode = node.node;
|
|
94
|
-
const transformedImport = transformImport(importNode, sourceFile);
|
|
95
|
-
if (transformedImport) {
|
|
96
|
-
newContents += transformedImport;
|
|
97
|
-
continue;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
if (node.node) {
|
|
101
|
-
newContents += node.node.getFullText(sourceFile);
|
|
102
|
-
}
|
|
103
|
-
else if (node.rawString) {
|
|
104
|
-
newContents += node.rawString;
|
|
105
|
-
}
|
|
106
|
-
else {
|
|
107
|
-
console.error("invalid node");
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
// console.debug(newContents);
|
|
111
|
-
// ideally there's a flag that indicates if we write
|
|
112
|
-
fs.writeFileSync(writeFile, newContents);
|
|
113
|
-
});
|
|
114
|
-
(0, child_process_1.execSync)("prettier src/schema/*.ts --write");
|
|
115
|
-
}
|
|
116
|
-
function traverse(fileContents, sourceFile, f) {
|
|
117
|
-
let traversed = false;
|
|
118
|
-
typescript_1.default.forEachChild(sourceFile, function (node) {
|
|
119
|
-
if (typescript_1.default.isClassDeclaration(node)) {
|
|
120
|
-
traversed = true;
|
|
121
|
-
// TODO address implicit schema doesn't work here...
|
|
122
|
-
// console.debug(sourceFile.fileName, node.kind);
|
|
123
|
-
if (traverseClass(fileContents, sourceFile, node, f)) {
|
|
124
|
-
f.flagUpdateImport();
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
f.trackNode({ node });
|
|
129
|
-
});
|
|
130
|
-
return traversed;
|
|
131
|
-
}
|
|
132
|
-
// TODO need to replace class field member, print that and see what happens
|
|
133
|
-
function traverseClass(fileContents, sourceFile, node, f) {
|
|
134
|
-
let updated = false;
|
|
135
|
-
// beginning of class...
|
|
136
|
-
// including comment
|
|
137
|
-
let klassContents = fileContents.substring(node.getFullStart(), node.members[0].getFullStart());
|
|
138
|
-
for (let member of node.members) {
|
|
139
|
-
if (!isFieldElement(member, sourceFile)) {
|
|
140
|
-
klassContents += member.getFullText(sourceFile);
|
|
141
|
-
continue;
|
|
142
|
-
}
|
|
143
|
-
// intentionally doesn't parse decorators since we don't need it
|
|
144
|
-
let fieldMap = "";
|
|
145
|
-
// fieldMapComment...
|
|
146
|
-
const comment = getPreText(fileContents, member, sourceFile);
|
|
147
|
-
if (comment) {
|
|
148
|
-
fieldMap += comment;
|
|
149
|
-
}
|
|
150
|
-
updated = true;
|
|
151
|
-
// need to change to fields: FieldMap = {code: StringType()};
|
|
152
|
-
const property = member;
|
|
153
|
-
const initializer = property.initializer;
|
|
154
|
-
fieldMap += "fields: FieldMap = {";
|
|
155
|
-
for (const element of initializer.elements) {
|
|
156
|
-
const parsed = parseFieldElement(element, sourceFile, fileContents);
|
|
157
|
-
if (parsed === null) {
|
|
158
|
-
return false;
|
|
159
|
-
}
|
|
160
|
-
const { callEx, name, nameComment, properties } = parsed;
|
|
161
|
-
let property = "";
|
|
162
|
-
const fieldComment = getPreText(fileContents, element, sourceFile).trim();
|
|
163
|
-
if (fieldComment) {
|
|
164
|
-
property += "\n" + fieldComment + "\n";
|
|
165
|
-
}
|
|
166
|
-
if (nameComment) {
|
|
167
|
-
property += nameComment + "\n";
|
|
168
|
-
}
|
|
169
|
-
// e.g. UUIDType, StringType etc
|
|
170
|
-
let call = callEx.expression.getText(sourceFile);
|
|
171
|
-
let fnCall = "";
|
|
172
|
-
if (properties.length) {
|
|
173
|
-
fnCall = `{${properties.join(",")}}`;
|
|
174
|
-
}
|
|
175
|
-
property += `${name}:${call}(${fnCall}),`;
|
|
176
|
-
fieldMap += property;
|
|
177
|
-
}
|
|
178
|
-
fieldMap += "}";
|
|
179
|
-
klassContents += fieldMap;
|
|
180
|
-
}
|
|
181
|
-
klassContents += "\n}";
|
|
182
|
-
// console.debug(klassContents);
|
|
183
|
-
if (!updated) {
|
|
184
|
-
return updated;
|
|
185
|
-
}
|
|
186
|
-
f.trackNode({ rawString: klassContents });
|
|
187
|
-
return updated;
|
|
188
|
-
}
|
|
189
|
-
function isFieldElement(member, sourceFile) {
|
|
190
|
-
if (member.kind !== typescript_1.default.SyntaxKind.PropertyDeclaration) {
|
|
191
|
-
return false;
|
|
192
|
-
}
|
|
193
|
-
const property = member;
|
|
194
|
-
const token = property.name;
|
|
195
|
-
if (token.escapedText !== "fields") {
|
|
196
|
-
return false;
|
|
197
|
-
}
|
|
198
|
-
const propertytype = property.type?.getText(sourceFile);
|
|
199
|
-
if (propertytype !== "Field[]") {
|
|
200
|
-
return false;
|
|
201
|
-
}
|
|
202
|
-
if (property.initializer?.kind !== typescript_1.default.SyntaxKind.ArrayLiteralExpression) {
|
|
203
|
-
console.error("invalid array type");
|
|
204
|
-
return false;
|
|
205
|
-
}
|
|
206
|
-
return true;
|
|
207
|
-
}
|
|
208
|
-
function parseFieldElement(element, sourceFile, fileContents) {
|
|
209
|
-
if (element.kind !== typescript_1.default.SyntaxKind.CallExpression) {
|
|
210
|
-
console.error("skipped non-call expression");
|
|
211
|
-
return null;
|
|
212
|
-
}
|
|
213
|
-
let callEx = element;
|
|
214
|
-
if (callEx.arguments.length !== 1) {
|
|
215
|
-
console.error("callExpression with arguments not of length 1");
|
|
216
|
-
return null;
|
|
217
|
-
}
|
|
218
|
-
let arg = callEx.arguments[0];
|
|
219
|
-
if (arg.kind !== typescript_1.default.SyntaxKind.ObjectLiteralExpression) {
|
|
220
|
-
console.error("not objectLiteralExpression");
|
|
221
|
-
return null;
|
|
222
|
-
}
|
|
223
|
-
let expr = arg;
|
|
224
|
-
let name = "";
|
|
225
|
-
let propertyComment;
|
|
226
|
-
let properties = [];
|
|
227
|
-
for (const p of expr.properties) {
|
|
228
|
-
const p2 = p;
|
|
229
|
-
// found name property
|
|
230
|
-
if (p2.name.escapedText === "name") {
|
|
231
|
-
name = p2.initializer.getText(sourceFile);
|
|
232
|
-
// check for any comment associated with name: "fooo"
|
|
233
|
-
propertyComment = getPreText(fileContents, p, sourceFile).trim();
|
|
234
|
-
}
|
|
235
|
-
else {
|
|
236
|
-
properties.push(p.getFullText(sourceFile));
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
if (!name) {
|
|
240
|
-
console.error(`couldn't find name property`);
|
|
241
|
-
return null;
|
|
242
|
-
}
|
|
243
|
-
// remove quotes
|
|
244
|
-
name = name.slice(1, -1);
|
|
245
|
-
return {
|
|
246
|
-
callEx,
|
|
247
|
-
name,
|
|
248
|
-
properties,
|
|
249
|
-
nameComment: propertyComment,
|
|
250
|
-
};
|
|
251
|
-
}
|
|
252
|
-
function transformImport(importNode, sourceFile) {
|
|
253
|
-
// remove quotes too
|
|
254
|
-
const text = importNode.moduleSpecifier.getText(sourceFile).slice(1, -1);
|
|
255
|
-
if (text !== "@snowtop/ent" &&
|
|
256
|
-
text !== "@snowtop/ent/schema" &&
|
|
257
|
-
text !== "@snowtop/ent/schema/") {
|
|
258
|
-
return;
|
|
259
|
-
}
|
|
260
|
-
const importText = importNode.importClause?.getText(sourceFile) || "";
|
|
261
|
-
const start = importText.indexOf("{");
|
|
262
|
-
const end = importText.lastIndexOf("}");
|
|
263
|
-
if (start === -1 || end === -1) {
|
|
264
|
-
return;
|
|
265
|
-
}
|
|
266
|
-
const imports = importText
|
|
267
|
-
.substring(start + 1, end)
|
|
268
|
-
// .trim()
|
|
269
|
-
.split(",");
|
|
270
|
-
for (let i = 0; i < imports.length; i++) {
|
|
271
|
-
const imp = imports[i].trim();
|
|
272
|
-
if (imp === "Field") {
|
|
273
|
-
imports[i] = "FieldMap";
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
// TODO better to update node instead of doing this but this works for now
|
|
277
|
-
return ("import " +
|
|
278
|
-
importText.substring(0, start + 1) +
|
|
279
|
-
imports.join(", ") +
|
|
280
|
-
importText.substring(end) +
|
|
281
|
-
' from "' +
|
|
282
|
-
text +
|
|
283
|
-
'"');
|
|
284
|
-
}
|
|
285
|
-
function getPreText(fileContents, node, sourceFile) {
|
|
286
|
-
return fileContents.substring(node.getFullStart(), node.getStart(sourceFile));
|
|
287
|
-
}
|
|
288
|
-
Promise.resolve(main());
|