@snowtop/ent 0.1.0-alpha1 → 0.1.0-alpha13

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.
Files changed (83) hide show
  1. package/action/action.d.ts +17 -13
  2. package/action/executor.d.ts +2 -2
  3. package/action/experimental_action.d.ts +4 -4
  4. package/action/experimental_action.js +1 -1
  5. package/action/orchestrator.d.ts +24 -9
  6. package/action/orchestrator.js +150 -39
  7. package/action/privacy.d.ts +2 -2
  8. package/core/base.d.ts +6 -2
  9. package/core/base.js +16 -0
  10. package/core/clause.d.ts +24 -3
  11. package/core/clause.js +246 -5
  12. package/core/config.d.ts +18 -0
  13. package/core/config.js +17 -0
  14. package/core/db.d.ts +3 -3
  15. package/core/db.js +2 -0
  16. package/core/ent.d.ts +2 -4
  17. package/core/ent.js +72 -25
  18. package/core/loaders/assoc_edge_loader.d.ts +1 -1
  19. package/core/loaders/assoc_edge_loader.js +5 -4
  20. package/core/loaders/index_loader.js +1 -0
  21. package/core/loaders/object_loader.d.ts +7 -2
  22. package/core/loaders/object_loader.js +59 -4
  23. package/core/privacy.d.ts +1 -1
  24. package/core/privacy.js +3 -0
  25. package/core/viewer.d.ts +1 -0
  26. package/core/viewer.js +4 -0
  27. package/graphql/builtins/connection.js +3 -3
  28. package/graphql/builtins/edge.js +2 -2
  29. package/graphql/builtins/node.js +1 -1
  30. package/graphql/graphql.d.ts +3 -2
  31. package/graphql/graphql.js +24 -23
  32. package/graphql/index.d.ts +1 -0
  33. package/graphql/index.js +3 -1
  34. package/graphql/mutations/union.d.ts +2 -0
  35. package/graphql/mutations/union.js +35 -0
  36. package/graphql/node_resolver.d.ts +0 -1
  37. package/graphql/query/connection_type.js +6 -6
  38. package/graphql/query/page_info.js +4 -4
  39. package/graphql/query/shared_assoc_test.js +2 -2
  40. package/graphql/scalars/time.d.ts +1 -1
  41. package/index.d.ts +16 -1
  42. package/index.js +18 -5
  43. package/package.json +3 -3
  44. package/parse_schema/parse.d.ts +21 -4
  45. package/parse_schema/parse.js +79 -8
  46. package/schema/base_schema.d.ts +36 -1
  47. package/schema/base_schema.js +48 -2
  48. package/schema/field.d.ts +1 -1
  49. package/schema/field.js +11 -3
  50. package/schema/index.d.ts +4 -2
  51. package/schema/index.js +10 -1
  52. package/schema/schema.d.ts +66 -4
  53. package/schema/schema.js +138 -5
  54. package/schema/struct_field.d.ts +17 -0
  55. package/schema/struct_field.js +102 -0
  56. package/schema/union_field.d.ts +23 -0
  57. package/schema/union_field.js +79 -0
  58. package/scripts/custom_graphql.js +122 -15
  59. package/scripts/move_generated.d.ts +1 -0
  60. package/scripts/move_generated.js +142 -0
  61. package/scripts/read_schema.js +15 -1
  62. package/scripts/transform_code.d.ts +1 -0
  63. package/scripts/transform_code.js +113 -0
  64. package/scripts/transform_schema.js +190 -123
  65. package/testutils/builder.d.ts +27 -18
  66. package/testutils/builder.js +82 -10
  67. package/testutils/context/test_context.d.ts +2 -2
  68. package/testutils/context/test_context.js +7 -1
  69. package/testutils/db/test_db.d.ts +2 -1
  70. package/testutils/db/test_db.js +13 -4
  71. package/testutils/ent-graphql-tests/index.d.ts +2 -0
  72. package/testutils/ent-graphql-tests/index.js +26 -17
  73. package/testutils/fake_data/fake_contact.d.ts +4 -8
  74. package/testutils/fake_data/fake_contact.js +15 -19
  75. package/testutils/fake_data/fake_event.d.ts +4 -8
  76. package/testutils/fake_data/fake_event.js +21 -25
  77. package/testutils/fake_data/fake_user.d.ts +5 -9
  78. package/testutils/fake_data/fake_user.js +23 -27
  79. package/testutils/fake_data/test_helpers.js +1 -1
  80. package/tsc/ast.d.ts +21 -0
  81. package/tsc/ast.js +154 -0
  82. package/tsc/compilerOptions.d.ts +6 -0
  83. package/tsc/compilerOptions.js +40 -1
@@ -0,0 +1,142 @@
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 path = __importStar(require("path"));
27
+ const fs = __importStar(require("fs"));
28
+ const compilerOptions_1 = require("../tsc/compilerOptions");
29
+ const typescript_1 = __importDefault(require("typescript"));
30
+ const ast_1 = require("../tsc/ast");
31
+ const child_process_1 = require("child_process");
32
+ // src/ent/generated and src/graphql/generated
33
+ function transformPath(old) {
34
+ const parts = old.split(path.sep);
35
+ if (parts.length < 3) {
36
+ return;
37
+ }
38
+ const changedParts = parts
39
+ .slice(0, 2)
40
+ .concat("generated")
41
+ .concat(parts.slice(2).filter((v) => v !== "generated"));
42
+ const newFile = changedParts.join(path.sep);
43
+ return { changedParts, newFile };
44
+ }
45
+ function moveFiles(files) {
46
+ files.forEach((file) => {
47
+ const conv = transformPath(file);
48
+ if (!conv) {
49
+ return;
50
+ }
51
+ const { changedParts, newFile } = conv;
52
+ if (file === newFile) {
53
+ return;
54
+ }
55
+ // check if directory exists, if not, create recursive dir
56
+ const p = changedParts.slice(0, changedParts.length - 1).join(path.sep);
57
+ const statInfo = fs.statSync(p, { throwIfNoEntry: false });
58
+ if (!statInfo) {
59
+ fs.mkdirSync(p, {
60
+ recursive: true,
61
+ });
62
+ }
63
+ // move file to new location
64
+ fs.renameSync(file, newFile);
65
+ });
66
+ }
67
+ function updateImports(files, target, cwd) {
68
+ files.forEach((file) => {
69
+ let { contents, sourceFile } = (0, compilerOptions_1.createSourceFile)(target, file);
70
+ let nodes = [];
71
+ let updated = false;
72
+ typescript_1.default.forEachChild(sourceFile, function (node) {
73
+ let dirPath = path.join(cwd, file, "..");
74
+ if (typescript_1.default.isImportDeclaration(node)) {
75
+ const conv = isGeneratedPath(node, sourceFile, dirPath);
76
+ if (!conv) {
77
+ nodes.push({ node });
78
+ return;
79
+ }
80
+ updated = true;
81
+ const newImportPath = path.relative(dirPath, conv.newFile);
82
+ const v = (0, ast_1.updateImportPath)(contents, node, sourceFile, newImportPath);
83
+ nodes.push({ rawString: v });
84
+ }
85
+ else {
86
+ nodes.push({ node });
87
+ }
88
+ });
89
+ let newContents = "";
90
+ for (const node of nodes) {
91
+ if (node.node) {
92
+ newContents += node.node.getFullText(sourceFile);
93
+ }
94
+ else if (node.rawString) {
95
+ newContents += node.rawString;
96
+ }
97
+ else {
98
+ throw new Error(`malformed node with no node or rawString`);
99
+ }
100
+ }
101
+ if (updated) {
102
+ fs.writeFileSync(file, newContents);
103
+ }
104
+ });
105
+ }
106
+ function main() {
107
+ const entFiles = glob_1.glob.sync("src/ent/**/generated/**/**.ts");
108
+ const graphqlFiles = glob_1.glob.sync("src/graphql/**/generated/**/**.ts");
109
+ moveFiles(entFiles);
110
+ moveFiles(graphqlFiles);
111
+ const target = (0, compilerOptions_1.getTargetFromCurrentDir)();
112
+ const entImportFiles = glob_1.glob.sync("src/ent/**/*.ts", {
113
+ ignore: ["**/generated/**", "node_modules/**"],
114
+ });
115
+ const graphqlImportFiles = glob_1.glob.sync("src/graphql/**/*.ts", {
116
+ ignore: ["**/generated/**", "node_modules/**"],
117
+ });
118
+ const cwd = process.cwd();
119
+ updateImports(entImportFiles, target, cwd);
120
+ updateImports(graphqlImportFiles, target, cwd);
121
+ (0, child_process_1.execSync)("prettier src/ent/*.ts --write");
122
+ (0, child_process_1.execSync)("prettier src/graphql/*.ts --write");
123
+ }
124
+ function isGeneratedPath(node, sourceFile, dirPath) {
125
+ const text = node.moduleSpecifier.getText(sourceFile).slice(1, -1);
126
+ // it's relative and has generated in there, continue
127
+ if (!((text.startsWith("..") || text.startsWith("./")) &&
128
+ text.indexOf("/generated") !== -1)) {
129
+ return;
130
+ }
131
+ const oldPath = path.join(dirPath, text);
132
+ const relFromRoot = path.relative(".", oldPath);
133
+ const conv = transformPath(relFromRoot);
134
+ if (!conv) {
135
+ return;
136
+ }
137
+ if (relFromRoot === conv.newFile) {
138
+ return;
139
+ }
140
+ return conv;
141
+ }
142
+ main();
@@ -44,7 +44,21 @@ function main() {
44
44
  if (!match) {
45
45
  throw new Error(`non-typescript file ${p} returned by glob`);
46
46
  }
47
- potentialSchemas[(0, pascal_case_1.pascalCase)(match[1])] = require(p).default;
47
+ let schema = match[1];
48
+ // convert foo_schema.ts -> foo
49
+ if (schema.endsWith("_schema")) {
50
+ schema = schema.slice(0, -7);
51
+ }
52
+ let relativePath;
53
+ const index = p.indexOf("src/schema");
54
+ if (index !== -1) {
55
+ relativePath = p.substring(index);
56
+ }
57
+ const s = require(p).default;
58
+ if (relativePath !== undefined) {
59
+ s.schemaPath = relativePath;
60
+ }
61
+ potentialSchemas[(0, pascal_case_1.pascalCase)(schema)] = s;
48
62
  }
49
63
  // console.log(potentialSchemas);
50
64
  const result = (0, parse_1.parseSchema)(potentialSchemas);
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,113 @@
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 compilerOptions_1 = require("../tsc/compilerOptions");
28
+ const ast_1 = require("../tsc/ast");
29
+ const child_process_1 = require("child_process");
30
+ const fs = __importStar(require("fs"));
31
+ async function main() {
32
+ let files = glob_1.glob.sync("src/ent/*.ts");
33
+ const target = (0, compilerOptions_1.getTargetFromCurrentDir)();
34
+ files.forEach((file) => {
35
+ let { contents, sourceFile } = (0, compilerOptions_1.createSourceFile)(target, file);
36
+ let traversed = false;
37
+ let nodes = [];
38
+ typescript_1.default.forEachChild(sourceFile, function (node) {
39
+ if (!typescript_1.default.isClassDeclaration(node) || !node.heritageClauses) {
40
+ nodes.push({ node });
41
+ return;
42
+ }
43
+ let classInfo = (0, ast_1.getClassInfo)(contents, sourceFile, node);
44
+ // only do classes which extend a base class e.g. User extends UserBase
45
+ if (!classInfo || classInfo.extends !== classInfo.name + "Base") {
46
+ return;
47
+ }
48
+ // need to check for PrivacyPolicy import...
49
+ traversed = true;
50
+ let klassContents = "";
51
+ for (const mm of node.members) {
52
+ if (isPrivacyPolicy(mm)) {
53
+ const property = mm;
54
+ // if invalid privacy policy, bounce
55
+ if (!property.initializer) {
56
+ traversed = false;
57
+ return;
58
+ }
59
+ const pp = property.initializer.getFullText(sourceFile);
60
+ const code = `getPrivacyPolicy(): PrivacyPolicy<this> {
61
+ return ${pp}
62
+ }`;
63
+ klassContents += code;
64
+ }
65
+ else {
66
+ klassContents += mm.getFullText(sourceFile);
67
+ }
68
+ }
69
+ // wrap comments and transform to export class Foo extends Bar { ${inner} }
70
+ nodes.push({ rawString: classInfo.wrapClassContents(klassContents) });
71
+ // console.debug(classInfo.wrapClassContents(klassContents));
72
+ });
73
+ // if traversed, overwrite.
74
+ if (!traversed) {
75
+ return;
76
+ }
77
+ let newContents = "";
78
+ for (const node of nodes) {
79
+ if (node.node) {
80
+ if (typescript_1.default.isImportDeclaration(node.node)) {
81
+ let transformed = (0, ast_1.transformImport)(contents, node.node, sourceFile, {
82
+ newImports: ["PrivacyPolicy"],
83
+ });
84
+ if (transformed) {
85
+ newContents += transformed;
86
+ continue;
87
+ }
88
+ }
89
+ newContents += node.node.getFullText(sourceFile);
90
+ }
91
+ else if (node.rawString) {
92
+ newContents += node.rawString;
93
+ }
94
+ else {
95
+ throw new Error(`malformed node with no node or rawString`);
96
+ }
97
+ fs.writeFileSync(file, newContents);
98
+ }
99
+ });
100
+ (0, child_process_1.execSync)("prettier src/ent/*.ts --write");
101
+ }
102
+ function isPrivacyPolicy(mm) {
103
+ return (mm.kind === typescript_1.default.SyntaxKind.PropertyDeclaration &&
104
+ mm.name.escapedText === "privacyPolicy");
105
+ }
106
+ main()
107
+ .then(() => {
108
+ process.exit(0);
109
+ })
110
+ .catch((err) => {
111
+ console.error(err);
112
+ process.exit(1);
113
+ });
@@ -27,51 +27,26 @@ const typescript_1 = __importDefault(require("typescript"));
27
27
  const fs = __importStar(require("fs"));
28
28
  const compilerOptions_1 = require("../tsc/compilerOptions");
29
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
- }
30
+ const path_1 = __importDefault(require("path"));
31
+ const ast_1 = require("../tsc/ast");
56
32
  async function main() {
57
- // this assumes this is being run from root of directory
58
- const options = (0, compilerOptions_1.readCompilerOptions)(".");
59
33
  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;
34
+ const target = (0, compilerOptions_1.getTargetFromCurrentDir)();
64
35
  // filter to only event.ts e.g. for comments and whitespace...
65
36
  // files = files.filter((f) => f.endsWith("event.ts"));
66
37
  files.forEach((file) => {
67
38
  // 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();
39
+ // const idx = file.lastIndexOf(".ts");
40
+ // const writeFile = file.substring(0, idx) + "2" + ".ts";
41
+ // console.debug(file);
42
+ const writeFile = "src/schema/" + path_1.default.basename(file).slice(0, -3) + "_schema.ts";
43
+ // const writeFile = file;
44
+ // console.debug(file, writeFile);
71
45
  // 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);
46
+ let { contents, sourceFile } = (0, compilerOptions_1.createSourceFile)(target, file);
73
47
  const nodes = [];
74
48
  let updateImport = false;
49
+ let removeImports = [];
75
50
  const f = {
76
51
  trackNode: function (tni) {
77
52
  nodes.push({
@@ -79,6 +54,9 @@ async function main() {
79
54
  importNode: tni.node && typescript_1.default.isImportDeclaration(tni.node),
80
55
  rawString: tni.rawString,
81
56
  });
57
+ if (tni.removeImports) {
58
+ removeImports.push(...tni.removeImports);
59
+ }
82
60
  },
83
61
  flagUpdateImport() {
84
62
  updateImport = true;
@@ -91,7 +69,10 @@ async function main() {
91
69
  for (const node of nodes) {
92
70
  if (updateImport && node.importNode) {
93
71
  const importNode = node.node;
94
- const transformedImport = transformImport(importNode, sourceFile);
72
+ const transformedImport = (0, ast_1.transformImport)(contents, importNode, sourceFile, {
73
+ removeImports,
74
+ transform: transformSchema,
75
+ });
95
76
  if (transformedImport) {
96
77
  newContents += transformedImport;
97
78
  continue;
@@ -110,6 +91,7 @@ async function main() {
110
91
  // console.debug(newContents);
111
92
  // ideally there's a flag that indicates if we write
112
93
  fs.writeFileSync(writeFile, newContents);
94
+ fs.rmSync(file);
113
95
  });
114
96
  (0, child_process_1.execSync)("prettier src/schema/*.ts --write");
115
97
  }
@@ -129,62 +111,183 @@ function traverse(fileContents, sourceFile, f) {
129
111
  });
130
112
  return traversed;
131
113
  }
132
- // TODO need to replace class field member, print that and see what happens
133
114
  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());
115
+ const ci = getTransformClassInfo(fileContents, sourceFile, node);
116
+ if (!ci) {
117
+ return false;
118
+ }
119
+ let klassContents = `${ci.comment}const ${ci.name} = new ${ci.class}({\n`;
120
+ let removeImports = [];
121
+ if (ci.implementsSchema) {
122
+ removeImports.push("Schema");
123
+ }
138
124
  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;
125
+ const fInfo = getClassElementInfo(fileContents, member, sourceFile);
126
+ if (!fInfo) {
127
+ return false;
149
128
  }
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;
129
+ klassContents += `${fInfo.comment}${fInfo.key}:${fInfo.value},\n`;
130
+ if (fInfo.type) {
131
+ removeImports.push(fInfo.type);
177
132
  }
178
- fieldMap += "}";
179
- klassContents += fieldMap;
180
133
  }
181
- klassContents += "\n}";
134
+ klassContents += "\n})";
135
+ if (ci.export && ci.default) {
136
+ klassContents += `\n export default ${ci.name};`;
137
+ }
138
+ else if (ci.export) {
139
+ klassContents = "export " + klassContents;
140
+ }
182
141
  // console.debug(klassContents);
183
- if (!updated) {
184
- return updated;
142
+ f.trackNode({ rawString: klassContents, removeImports: removeImports });
143
+ return true;
144
+ }
145
+ function transformSchema(str) {
146
+ // only do known class names
147
+ if (str === "BaseEntSchema" || str === "BaseEntSchemaWithTZ") {
148
+ return str.substring(4);
149
+ }
150
+ return str;
151
+ }
152
+ // TODO need to generify this...
153
+ // and then have a schema specific version
154
+ // may make sense to just duplicate this logic...
155
+ function getTransformClassInfo(fileContents, sourceFile, node) {
156
+ const generic = (0, ast_1.getClassInfo)(fileContents, sourceFile, node);
157
+ if (!generic) {
158
+ return;
159
+ }
160
+ let className = generic.name;
161
+ if (!className?.endsWith("Schema")) {
162
+ className += "Schema";
163
+ }
164
+ let implementsSchema = generic.implements?.some((v) => v == "Schema");
165
+ let classExtends = generic.extends;
166
+ if (classExtends && classExtends === transformSchema(classExtends)) {
167
+ return undefined;
168
+ }
169
+ if (!className || !node.heritageClauses || !classExtends) {
170
+ return undefined;
171
+ }
172
+ let ci = {
173
+ ...generic,
174
+ name: className,
175
+ class: classExtends,
176
+ implementsSchema,
177
+ };
178
+ return ci;
179
+ }
180
+ // intentionally doesn't parse decorators since we don't need it
181
+ function getClassElementInfo(fileContents, member, sourceFile) {
182
+ if (isFieldElement(member, sourceFile)) {
183
+ return getFieldElementInfo(fileContents, member, sourceFile);
184
+ }
185
+ if (member.kind === typescript_1.default.SyntaxKind.Constructor) {
186
+ return getConstructorElementInfo(fileContents, member, sourceFile);
187
+ }
188
+ if (member.kind !== typescript_1.default.SyntaxKind.PropertyDeclaration) {
189
+ return;
190
+ }
191
+ // other properties
192
+ const property = member;
193
+ if (!property.initializer) {
194
+ return;
185
195
  }
186
- f.trackNode({ rawString: klassContents });
187
- return updated;
196
+ const token = property.name;
197
+ return {
198
+ key: token.escapedText.toString(),
199
+ value: property.initializer?.getFullText(sourceFile),
200
+ comment: (0, ast_1.getPreText)(fileContents, member, sourceFile),
201
+ type: getType(property, sourceFile),
202
+ };
203
+ }
204
+ function getType(property, sourceFile) {
205
+ let propertytype = property.type?.getText(sourceFile) || "";
206
+ let ends = ["| null", "[]"];
207
+ for (const end of ends) {
208
+ if (propertytype.endsWith(end)) {
209
+ propertytype = propertytype.slice(0, -1 * end.length);
210
+ }
211
+ }
212
+ return propertytype;
213
+ }
214
+ function getFieldElementInfo(fileContents, member, sourceFile) {
215
+ let fieldMap = "";
216
+ // need to change to fields: {code: StringType()};
217
+ const property = member;
218
+ const initializer = property.initializer;
219
+ fieldMap += "{";
220
+ for (const element of initializer.elements) {
221
+ const parsed = parseFieldElement(element, sourceFile, fileContents);
222
+ if (parsed === null) {
223
+ return;
224
+ }
225
+ const { callEx, name, nameComment, properties } = parsed;
226
+ let property = "";
227
+ const fieldComment = (0, ast_1.getPreText)(fileContents, element, sourceFile).trim();
228
+ if (fieldComment) {
229
+ property += "\n" + fieldComment + "\n";
230
+ }
231
+ if (nameComment) {
232
+ property += nameComment + "\n";
233
+ }
234
+ // e.g. UUIDType, StringType etc
235
+ let call = callEx.expression.getText(sourceFile);
236
+ let fnCall = "";
237
+ if (properties.length) {
238
+ fnCall = `{${properties.join(",")}}`;
239
+ }
240
+ property += `${name}:${call}(${fnCall}),`;
241
+ fieldMap += property;
242
+ }
243
+ fieldMap += "}";
244
+ return {
245
+ key: "fields",
246
+ value: fieldMap,
247
+ comment: (0, ast_1.getPreText)(fileContents, member, sourceFile),
248
+ type: getType(property, sourceFile),
249
+ };
250
+ }
251
+ function getConstructorElementInfo(fileContents, member, sourceFile) {
252
+ const c = member;
253
+ //remove {}
254
+ let fullText = c.body?.getFullText(sourceFile) || "";
255
+ fullText = fullText.trim().slice(1, -1).trim();
256
+ // convert something like
257
+ /*
258
+ constructor() {
259
+ super();
260
+ this.addPatterns(
261
+ new Feedback(),
262
+ new DayOfWeek(),
263
+ new Feedback(),
264
+ new DayOfWeek(),
265
+ );
266
+ }
267
+ */
268
+ // into this.addPatterns(new Feedback(),new DayOfWeek(),new Feedback(),new DayOfWeek(),)
269
+ const lines = fullText
270
+ .split("\n")
271
+ .map((line) => line.trim())
272
+ .join("")
273
+ .split(";")
274
+ .filter((f) => f != "super()" && f != "");
275
+ // at this point there should be only line for what we handle
276
+ if (lines.length != 1) {
277
+ return;
278
+ }
279
+ const line = lines[0];
280
+ const addPatterns = "this.addPatterns(";
281
+ if (!line.startsWith(addPatterns)) {
282
+ return;
283
+ }
284
+ return {
285
+ key: "patterns",
286
+ // remove this.addPatterns at the front, remove trailing ) at the end
287
+ // if there's a trailing comma, it'll be handled by prettier
288
+ value: `[${line.slice(addPatterns.length, -1)}]`,
289
+ comment: "",
290
+ };
188
291
  }
189
292
  function isFieldElement(member, sourceFile) {
190
293
  if (member.kind !== typescript_1.default.SyntaxKind.PropertyDeclaration) {
@@ -230,7 +333,7 @@ function parseFieldElement(element, sourceFile, fileContents) {
230
333
  if (p2.name.escapedText === "name") {
231
334
  name = p2.initializer.getText(sourceFile);
232
335
  // check for any comment associated with name: "fooo"
233
- propertyComment = getPreText(fileContents, p, sourceFile).trim();
336
+ propertyComment = (0, ast_1.getPreText)(fileContents, p, sourceFile).trim();
234
337
  }
235
338
  else {
236
339
  properties.push(p.getFullText(sourceFile));
@@ -249,40 +352,4 @@ function parseFieldElement(element, sourceFile, fileContents) {
249
352
  nameComment: propertyComment,
250
353
  };
251
354
  }
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
355
  Promise.resolve(main());