@snowtop/ent 0.1.0-alpha8 → 0.1.0-alpha80
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 +27 -25
- package/action/executor.d.ts +3 -3
- package/action/executor.js +2 -2
- package/action/experimental_action.d.ts +29 -22
- package/action/experimental_action.js +29 -6
- package/action/orchestrator.d.ts +37 -16
- package/action/orchestrator.js +184 -54
- package/action/privacy.d.ts +2 -2
- package/core/base.d.ts +38 -22
- package/core/clause.d.ts +65 -7
- package/core/clause.js +261 -54
- package/core/config.d.ts +8 -0
- package/core/context.d.ts +5 -3
- package/core/context.js +7 -2
- package/core/convert.d.ts +1 -1
- package/core/db.d.ts +1 -2
- package/core/db.js +1 -1
- package/core/ent.d.ts +76 -24
- package/core/ent.js +459 -143
- package/core/loaders/assoc_count_loader.d.ts +2 -2
- package/core/loaders/assoc_count_loader.js +6 -1
- package/core/loaders/assoc_edge_loader.d.ts +2 -2
- package/core/loaders/loader.js +5 -5
- package/core/loaders/object_loader.d.ts +4 -3
- package/core/loaders/object_loader.js +22 -5
- package/core/loaders/query_loader.d.ts +2 -2
- package/core/loaders/raw_count_loader.d.ts +2 -2
- package/core/logger.d.ts +1 -1
- package/core/logger.js +1 -0
- package/core/privacy.d.ts +26 -25
- package/core/privacy.js +21 -25
- 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/query/shared_assoc_test.d.ts +1 -1
- package/core/query/shared_assoc_test.js +17 -5
- package/core/query/shared_test.d.ts +3 -0
- package/core/query/shared_test.js +95 -17
- package/core/viewer.d.ts +3 -3
- package/core/viewer.js +1 -1
- package/graphql/builtins/connection.js +3 -3
- package/graphql/builtins/edge.js +2 -2
- package/graphql/builtins/node.js +1 -1
- package/graphql/graphql.js +8 -2
- 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 +10 -5
- package/index.js +13 -6
- package/package.json +2 -2
- package/parse_schema/parse.d.ts +14 -2
- package/parse_schema/parse.js +40 -2
- package/schema/base_schema.d.ts +1 -1
- package/schema/base_schema.js +3 -0
- package/schema/field.d.ts +36 -7
- package/schema/field.js +70 -2
- package/schema/index.d.ts +2 -2
- package/schema/json_field.d.ts +13 -1
- package/schema/json_field.js +28 -1
- package/schema/schema.d.ts +61 -10
- package/schema/schema.js +18 -4
- package/schema/struct_field.d.ts +11 -1
- package/schema/struct_field.js +43 -4
- package/scripts/custom_graphql.js +5 -1
- 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 +25 -2
- package/testutils/builder.d.ts +31 -21
- package/testutils/builder.js +82 -29
- package/testutils/db/fixture.d.ts +10 -0
- package/testutils/db/fixture.js +26 -0
- package/testutils/db/{test_db.d.ts → temp_db.d.ts} +15 -3
- package/testutils/db/{test_db.js → temp_db.js} +63 -15
- package/testutils/db/value.d.ts +6 -0
- package/testutils/db/value.js +251 -0
- package/testutils/db_time_zone.d.ts +4 -0
- package/testutils/db_time_zone.js +41 -0
- package/testutils/ent-graphql-tests/index.js +19 -12
- package/testutils/fake_data/fake_contact.d.ts +3 -3
- package/testutils/fake_data/fake_contact.js +9 -6
- package/testutils/fake_data/fake_event.d.ts +3 -3
- package/testutils/fake_data/fake_event.js +8 -5
- package/testutils/fake_data/fake_user.d.ts +4 -4
- package/testutils/fake_data/fake_user.js +16 -13
- package/testutils/fake_data/test_helpers.d.ts +2 -2
- package/testutils/fake_data/test_helpers.js +5 -5
- package/testutils/fake_data/user_query.d.ts +2 -2
- package/testutils/fake_log.d.ts +3 -3
- package/testutils/parse_sql.d.ts +6 -0
- package/testutils/parse_sql.js +16 -2
- package/testutils/test_edge_global_schema.d.ts +15 -0
- package/testutils/test_edge_global_schema.js +58 -0
- package/testutils/write.d.ts +2 -2
- package/testutils/write.js +29 -7
- package/tsc/ast.d.ts +44 -0
- package/tsc/ast.js +267 -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 +22 -0
- package/tsc/transform_action.js +179 -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 +379 -0
- package/scripts/transform_schema.js +0 -437
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { customInfo } from "../tsc/ast";
|
|
3
|
+
import { TransformFile } from "./transform";
|
|
4
|
+
export declare class TransformAction implements TransformFile {
|
|
5
|
+
private customInfo;
|
|
6
|
+
glob: string;
|
|
7
|
+
prettierGlob: string;
|
|
8
|
+
constructor(customInfo: customInfo);
|
|
9
|
+
traverseChild(sourceFile: ts.SourceFile, contents: string, file: string, node: ts.Node): {
|
|
10
|
+
node: ts.Node;
|
|
11
|
+
rawString?: undefined;
|
|
12
|
+
traversed?: undefined;
|
|
13
|
+
imports?: undefined;
|
|
14
|
+
removeImports?: undefined;
|
|
15
|
+
} | {
|
|
16
|
+
rawString: string;
|
|
17
|
+
traversed: boolean;
|
|
18
|
+
imports: Map<string, string[]>;
|
|
19
|
+
removeImports: string[];
|
|
20
|
+
node?: undefined;
|
|
21
|
+
} | undefined;
|
|
22
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
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.TransformAction = void 0;
|
|
26
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
27
|
+
const ast_1 = require("../tsc/ast");
|
|
28
|
+
const viewer_1 = require("../core/viewer");
|
|
29
|
+
const path = __importStar(require("path"));
|
|
30
|
+
const snake_case_1 = require("snake-case");
|
|
31
|
+
// returns input and importPath
|
|
32
|
+
function getBaseFileInfo(file, classInfo, sourceFile) {
|
|
33
|
+
// @ts-ignore
|
|
34
|
+
const importStatements = sourceFile.statements.filter((stmt) => typescript_1.default.isImportDeclaration(stmt));
|
|
35
|
+
for (const imp of importStatements) {
|
|
36
|
+
const text = imp.moduleSpecifier.getText(sourceFile).slice(1, -1);
|
|
37
|
+
if ((0, ast_1.isSrcGeneratedImport)(imp, sourceFile) ||
|
|
38
|
+
(0, ast_1.isRelativeGeneratedImport)(imp, sourceFile)) {
|
|
39
|
+
// base file and we're importing from it
|
|
40
|
+
// e.g. in create_user_action, we're importing from create_user_action_base
|
|
41
|
+
if (path.basename(file).slice(0, -3) + "_base" !== path.basename(text)) {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
const impInfo = (0, ast_1.getImportInfo)(imp, sourceFile);
|
|
45
|
+
if (!impInfo) {
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
let inputs = impInfo.imports
|
|
49
|
+
.filter((imp) => imp.trim() && imp.endsWith("Input"))
|
|
50
|
+
.map((v) => v.trim());
|
|
51
|
+
if (inputs.length === 1) {
|
|
52
|
+
return {
|
|
53
|
+
input: inputs[0],
|
|
54
|
+
importPath: impInfo.importPath,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
if (inputs.length && classInfo.name.endsWith("Action")) {
|
|
58
|
+
const prefix = classInfo.name.slice(0, classInfo.name.length - 6);
|
|
59
|
+
inputs = inputs.filter((imp) => imp.slice(0, imp.length - 5) === prefix);
|
|
60
|
+
if (inputs.length === 1) {
|
|
61
|
+
return {
|
|
62
|
+
input: inputs[0],
|
|
63
|
+
importPath: impInfo.importPath,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
let m = {
|
|
72
|
+
triggers: {
|
|
73
|
+
m: "getTriggers",
|
|
74
|
+
i: "Trigger",
|
|
75
|
+
suffix: "Triggers",
|
|
76
|
+
},
|
|
77
|
+
observers: {
|
|
78
|
+
m: "getObservers",
|
|
79
|
+
i: "Observer",
|
|
80
|
+
suffix: "Observers",
|
|
81
|
+
},
|
|
82
|
+
validators: {
|
|
83
|
+
m: "getValidators",
|
|
84
|
+
i: "Validator",
|
|
85
|
+
suffix: "Validators",
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
function getConversionInfo(mm, actionName) {
|
|
89
|
+
if (mm.kind !== typescript_1.default.SyntaxKind.PropertyDeclaration) {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
const text = mm.name.escapedText;
|
|
93
|
+
const v = m[text];
|
|
94
|
+
if (v === undefined) {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
return {
|
|
98
|
+
text,
|
|
99
|
+
method: v.m,
|
|
100
|
+
interface: v.i,
|
|
101
|
+
// CreateFooActionTriggers etc
|
|
102
|
+
methodType: actionName + v.suffix,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
class TransformAction {
|
|
106
|
+
constructor(customInfo) {
|
|
107
|
+
this.customInfo = customInfo;
|
|
108
|
+
this.glob = "src/ent/**/actions/**/*_action.ts";
|
|
109
|
+
this.prettierGlob = "src/ent/**/actions/**.ts";
|
|
110
|
+
}
|
|
111
|
+
traverseChild(sourceFile, contents, file, node) {
|
|
112
|
+
if (!typescript_1.default.isClassDeclaration(node) || !node.heritageClauses) {
|
|
113
|
+
return { node };
|
|
114
|
+
}
|
|
115
|
+
let classInfo = (0, ast_1.getClassInfo)(contents, sourceFile, node);
|
|
116
|
+
// only do classes
|
|
117
|
+
if (!classInfo || !classInfo.default) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
// require action
|
|
121
|
+
const p = require(path.join(process.cwd(), "./" + file.slice(0, -3)));
|
|
122
|
+
const action = new p.default(new viewer_1.LoggedOutViewer(), {});
|
|
123
|
+
const actionName = action.constructor.name;
|
|
124
|
+
const builder = action.builder.constructor.name;
|
|
125
|
+
const nodeName = action.builder.ent.name;
|
|
126
|
+
const viewer = this.customInfo.viewerInfo.name;
|
|
127
|
+
const baseInfo = getBaseFileInfo(file, classInfo, sourceFile);
|
|
128
|
+
if (!baseInfo) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
let klassContents = "";
|
|
132
|
+
let traversed = false;
|
|
133
|
+
let newImports = [];
|
|
134
|
+
for (const mm of node.members) {
|
|
135
|
+
const conv = getConversionInfo(mm, actionName);
|
|
136
|
+
if (conv !== null) {
|
|
137
|
+
const property = mm;
|
|
138
|
+
// if invalid, bounce
|
|
139
|
+
if (!property.initializer) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
traversed = true;
|
|
143
|
+
const pp = property.initializer.getFullText(sourceFile).trim();
|
|
144
|
+
const code = `${conv.method}(): ${conv.methodType} {
|
|
145
|
+
return ${pp}
|
|
146
|
+
}`;
|
|
147
|
+
newImports.push(conv.methodType);
|
|
148
|
+
klassContents += (0, ast_1.getPreText)(contents, mm, sourceFile) + code;
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
klassContents += mm.getFullText(sourceFile);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
const builderPath = `src/ent/generated/${(0, snake_case_1.snakeCase)(nodeName)}/actions/${(0, snake_case_1.snakeCase)(builder)}`;
|
|
155
|
+
let imports = new Map([
|
|
156
|
+
[
|
|
157
|
+
(0, ast_1.transformRelative)(file, this.customInfo.viewerInfo.path, this.customInfo.relativeImports),
|
|
158
|
+
[viewer],
|
|
159
|
+
],
|
|
160
|
+
[
|
|
161
|
+
(0, ast_1.transformRelative)(file, baseInfo.importPath, this.customInfo.relativeImports),
|
|
162
|
+
newImports,
|
|
163
|
+
],
|
|
164
|
+
[
|
|
165
|
+
(0, ast_1.transformRelative)(file, builderPath, this.customInfo.relativeImports),
|
|
166
|
+
[builder],
|
|
167
|
+
],
|
|
168
|
+
]);
|
|
169
|
+
// wrap comments and transform to export class Foo extends Bar { ${inner} }
|
|
170
|
+
return {
|
|
171
|
+
rawString: classInfo.wrapClassContents(klassContents),
|
|
172
|
+
traversed,
|
|
173
|
+
imports,
|
|
174
|
+
removeImports: ["Trigger", "Observer", "Validator"],
|
|
175
|
+
// not removing FooBuilder incase it's still somehow used in type of inline builders
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
exports.TransformAction = TransformAction;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { TransformFile } from "./transform";
|
|
3
|
+
export declare class TransformEnt implements TransformFile {
|
|
4
|
+
glob: string;
|
|
5
|
+
traverseChild(sourceFile: ts.SourceFile, contents: string, file: string, node: ts.Node): {
|
|
6
|
+
node: ts.Node;
|
|
7
|
+
rawString?: undefined;
|
|
8
|
+
traversed?: undefined;
|
|
9
|
+
imports?: undefined;
|
|
10
|
+
} | {
|
|
11
|
+
rawString: string;
|
|
12
|
+
traversed: boolean;
|
|
13
|
+
imports: Map<string, string[]>;
|
|
14
|
+
node?: undefined;
|
|
15
|
+
} | undefined;
|
|
16
|
+
prettierGlob: string;
|
|
17
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.TransformEnt = void 0;
|
|
7
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
8
|
+
const ast_1 = require("./ast");
|
|
9
|
+
function isPrivacyPolicy(mm) {
|
|
10
|
+
return (mm.kind === typescript_1.default.SyntaxKind.PropertyDeclaration &&
|
|
11
|
+
mm.name.escapedText === "privacyPolicy");
|
|
12
|
+
}
|
|
13
|
+
class TransformEnt {
|
|
14
|
+
constructor() {
|
|
15
|
+
this.glob = "src/ent/*.ts";
|
|
16
|
+
this.prettierGlob = "src/ent/*.ts";
|
|
17
|
+
}
|
|
18
|
+
traverseChild(sourceFile, contents, file, node) {
|
|
19
|
+
if (!typescript_1.default.isClassDeclaration(node) || !node.heritageClauses) {
|
|
20
|
+
return { node };
|
|
21
|
+
}
|
|
22
|
+
let classInfo = (0, ast_1.getClassInfo)(contents, sourceFile, node);
|
|
23
|
+
if (!classInfo) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
// different class. ignore
|
|
27
|
+
// only do classes which extend a base class e.g. User extends UserBase
|
|
28
|
+
// if different class (e.g. privacy rule), just return it
|
|
29
|
+
if (classInfo.extends !== classInfo.name + "Base") {
|
|
30
|
+
return { node };
|
|
31
|
+
}
|
|
32
|
+
let klassContents = "";
|
|
33
|
+
let traversed = false;
|
|
34
|
+
for (const mm of node.members) {
|
|
35
|
+
if (isPrivacyPolicy(mm)) {
|
|
36
|
+
const property = mm;
|
|
37
|
+
// if invalid privacy policy, bounce
|
|
38
|
+
if (!property.initializer) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const pp = property.initializer.getFullText(sourceFile);
|
|
42
|
+
const code = `getPrivacyPolicy(): PrivacyPolicy<this> {
|
|
43
|
+
return ${pp}
|
|
44
|
+
}`;
|
|
45
|
+
klassContents += (0, ast_1.getPreText)(contents, mm, sourceFile) + code;
|
|
46
|
+
traversed = true;
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
klassContents += mm.getFullText(sourceFile);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
rawString: classInfo.wrapClassContents(klassContents),
|
|
54
|
+
traversed,
|
|
55
|
+
imports: new Map([["@snowtop/ent", ["PrivacyPolicy"]]]),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
exports.TransformEnt = TransformEnt;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { TransformFile } from "./transform";
|
|
3
|
+
export declare class TransformSchema implements TransformFile {
|
|
4
|
+
private relativeImports;
|
|
5
|
+
private oldBaseClass?;
|
|
6
|
+
private newSchemaClass?;
|
|
7
|
+
private transformPath?;
|
|
8
|
+
constructor(relativeImports: boolean, oldBaseClass?: string | undefined, newSchemaClass?: string | undefined, transformPath?: string | undefined);
|
|
9
|
+
glob: string;
|
|
10
|
+
private transformSchema;
|
|
11
|
+
traverseChild(sourceFile: ts.SourceFile, contents: string, file: string, node: ts.Node): {
|
|
12
|
+
node: ts.Node;
|
|
13
|
+
traversed?: undefined;
|
|
14
|
+
rawString?: undefined;
|
|
15
|
+
removeImports?: undefined;
|
|
16
|
+
imports?: undefined;
|
|
17
|
+
} | {
|
|
18
|
+
traversed: boolean;
|
|
19
|
+
rawString: string;
|
|
20
|
+
removeImports: string[];
|
|
21
|
+
imports: Map<string, string[]>;
|
|
22
|
+
node?: undefined;
|
|
23
|
+
} | undefined;
|
|
24
|
+
fileToWrite(file: string): string;
|
|
25
|
+
postProcess(file: string): void;
|
|
26
|
+
prettierGlob: string;
|
|
27
|
+
}
|
|
@@ -0,0 +1,379 @@
|
|
|
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(fileContents, 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(fileContents, 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
|
+
throwErr(fileContents, member, "invalid array type");
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
return true;
|
|
222
|
+
}
|
|
223
|
+
// if there's an error transforming any of the schemas, we should stop...
|
|
224
|
+
function throwErr(fileContents, node, error) {
|
|
225
|
+
console.error(error);
|
|
226
|
+
throw new Error(`error transforming this field ${fileContents.substring(node.getFullStart(), node.getEnd())}`);
|
|
227
|
+
}
|
|
228
|
+
function parseFieldElement(element, sourceFile, fileContents, nested) {
|
|
229
|
+
if (element.kind !== typescript_1.default.SyntaxKind.CallExpression &&
|
|
230
|
+
element.kind !== typescript_1.default.SyntaxKind.PropertyAccessExpression) {
|
|
231
|
+
throwErr(fileContents, element, `skipped unknown (non-call|non-property) expression ${element.kind}`);
|
|
232
|
+
return null;
|
|
233
|
+
}
|
|
234
|
+
if (element.kind === typescript_1.default.SyntaxKind.PropertyAccessExpression) {
|
|
235
|
+
const ret = parseFieldElement(element.expression, sourceFile, fileContents, true);
|
|
236
|
+
if (ret !== null) {
|
|
237
|
+
if (!nested) {
|
|
238
|
+
ret.suffix = fileContents.substring(ret.callEx.getEnd(), element.getEnd());
|
|
239
|
+
}
|
|
240
|
+
return ret;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
let callEx = element;
|
|
244
|
+
if (callEx.arguments.length !== 1) {
|
|
245
|
+
// have a situation like: StringType({ name: "canonicalName" }).trim().toLowerCase(),
|
|
246
|
+
// 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
|
|
247
|
+
if (callEx.expression.kind === typescript_1.default.SyntaxKind.PropertyAccessExpression) {
|
|
248
|
+
const ret = parseFieldElement(callEx.expression.expression, sourceFile, fileContents, true);
|
|
249
|
+
if (ret !== null) {
|
|
250
|
+
if (!nested) {
|
|
251
|
+
ret.suffix = fileContents.substring(ret.callEx.getEnd(), callEx.getEnd());
|
|
252
|
+
}
|
|
253
|
+
return ret;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
throwErr(fileContents, element, "callExpression with arguments not of length 1");
|
|
257
|
+
}
|
|
258
|
+
let arg = callEx.arguments[0];
|
|
259
|
+
if (arg.kind !== typescript_1.default.SyntaxKind.ObjectLiteralExpression) {
|
|
260
|
+
// this and the check above for PropertyAccessExpression are to handle things like
|
|
261
|
+
// FooType({
|
|
262
|
+
/// ...
|
|
263
|
+
// }).function(blah)
|
|
264
|
+
const ret = parseFieldElement(callEx.expression, sourceFile, fileContents, true);
|
|
265
|
+
if (ret !== null) {
|
|
266
|
+
if (!nested) {
|
|
267
|
+
ret.suffix = fileContents.substring(ret.callEx.getEnd(), callEx.getEnd());
|
|
268
|
+
}
|
|
269
|
+
return ret;
|
|
270
|
+
}
|
|
271
|
+
throwErr(fileContents, element, `not objectLiteralExpression. kind ${arg.kind}`);
|
|
272
|
+
return null;
|
|
273
|
+
}
|
|
274
|
+
let expr = arg;
|
|
275
|
+
let name = "";
|
|
276
|
+
let propertyComment;
|
|
277
|
+
let properties = [];
|
|
278
|
+
for (const p of expr.properties) {
|
|
279
|
+
const p2 = p;
|
|
280
|
+
// found name property
|
|
281
|
+
if (p2.name.escapedText === "name") {
|
|
282
|
+
name = p2.initializer.getText(sourceFile);
|
|
283
|
+
// check for any comment associated with name: "fooo"
|
|
284
|
+
propertyComment = (0, ast_1.getPreText)(fileContents, p, sourceFile).trim();
|
|
285
|
+
}
|
|
286
|
+
else {
|
|
287
|
+
properties.push(p.getFullText(sourceFile));
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
if (!name) {
|
|
291
|
+
throwErr(fileContents, element, `couldn't find name property`);
|
|
292
|
+
return null;
|
|
293
|
+
}
|
|
294
|
+
// remove quotes
|
|
295
|
+
name = name.slice(1, -1);
|
|
296
|
+
return {
|
|
297
|
+
callEx,
|
|
298
|
+
name,
|
|
299
|
+
properties,
|
|
300
|
+
nameComment: propertyComment,
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
// find which of these importPaths is being used and use that to replace
|
|
304
|
+
function findSchemaImportPath(sourceFile) {
|
|
305
|
+
const paths = {
|
|
306
|
+
"@snowtop/ent": true,
|
|
307
|
+
"@snowtop/ent/schema": true,
|
|
308
|
+
"@snowtop/ent/schema/": true,
|
|
309
|
+
};
|
|
310
|
+
// @ts-ignore
|
|
311
|
+
const importStatements = sourceFile.statements.filter((stmt) => typescript_1.default.isImportDeclaration(stmt));
|
|
312
|
+
for (const imp of importStatements) {
|
|
313
|
+
const impInfo = (0, ast_1.getImportInfo)(imp, sourceFile);
|
|
314
|
+
if (!impInfo) {
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
if (paths[impInfo.importPath] !== undefined) {
|
|
318
|
+
return impInfo.importPath;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
class TransformSchema {
|
|
323
|
+
// we only end up doing this once because we change the schema representation
|
|
324
|
+
// so safe to run this multiple times
|
|
325
|
+
constructor(relativeImports, oldBaseClass, newSchemaClass, transformPath) {
|
|
326
|
+
this.relativeImports = relativeImports;
|
|
327
|
+
this.oldBaseClass = oldBaseClass;
|
|
328
|
+
this.newSchemaClass = newSchemaClass;
|
|
329
|
+
this.transformPath = transformPath;
|
|
330
|
+
this.glob = "src/schema/*.ts";
|
|
331
|
+
this.prettierGlob = "src/schema/*.ts";
|
|
332
|
+
}
|
|
333
|
+
transformSchema(className) {
|
|
334
|
+
if (className === "BaseEntSchema" || className === "BaseEntSchemaWithTZ") {
|
|
335
|
+
return className.substring(4);
|
|
336
|
+
}
|
|
337
|
+
if (className === this.oldBaseClass && this.newSchemaClass) {
|
|
338
|
+
return this.newSchemaClass;
|
|
339
|
+
}
|
|
340
|
+
return className;
|
|
341
|
+
}
|
|
342
|
+
traverseChild(sourceFile, contents, file, node) {
|
|
343
|
+
if (!typescript_1.default.isClassDeclaration(node)) {
|
|
344
|
+
return { node };
|
|
345
|
+
}
|
|
346
|
+
// TODO address implicit schema doesn't work here...
|
|
347
|
+
const ret = traverseClass(contents, sourceFile, node, this.transformSchema.bind(this));
|
|
348
|
+
if (ret === undefined) {
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
let imports = new Map();
|
|
352
|
+
const imp = findSchemaImportPath(sourceFile);
|
|
353
|
+
if (imp) {
|
|
354
|
+
if (this.transformPath) {
|
|
355
|
+
imports.set(imp, []);
|
|
356
|
+
}
|
|
357
|
+
else {
|
|
358
|
+
imports.set(imp, ret.newImports);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
if (this.transformPath) {
|
|
362
|
+
// add new imports to this path
|
|
363
|
+
imports.set((0, ast_1.transformRelative)(file, this.transformPath, this.relativeImports), ret.newImports);
|
|
364
|
+
}
|
|
365
|
+
return {
|
|
366
|
+
traversed: true,
|
|
367
|
+
rawString: ret.rawString,
|
|
368
|
+
removeImports: ret.removeImports,
|
|
369
|
+
imports: imports,
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
fileToWrite(file) {
|
|
373
|
+
return "src/schema/" + path_1.default.basename(file).slice(0, -3) + "_schema.ts";
|
|
374
|
+
}
|
|
375
|
+
postProcess(file) {
|
|
376
|
+
fs.rmSync(file);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
exports.TransformSchema = TransformSchema;
|