@snowtop/ent 0.1.0-alpha20 → 0.1.0-alpha24

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@snowtop/ent",
3
- "version": "0.1.0-alpha20",
3
+ "version": "0.1.0-alpha24",
4
4
  "description": "snowtop ent framework",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
package/schema/schema.js CHANGED
@@ -121,6 +121,7 @@ function getFieldsWithPrivacy(value, fieldMap) {
121
121
  m.set(info.dbCol, privacyPolicy);
122
122
  }
123
123
  }
124
+ return;
124
125
  }
125
126
  for (const name in fields) {
126
127
  const field = fields[name];
@@ -9,35 +9,28 @@ const transform_ent_1 = require("../tsc/transform_ent");
9
9
  const move_generated_1 = require("../tsc/move_generated");
10
10
  const transform_action_1 = require("../tsc/transform_action");
11
11
  const minimist_1 = __importDefault(require("minimist"));
12
- // call from docker
13
- // since clients [sh/w]ouldn't have ts-node...
14
- // locally
15
- // ts-node --swc --project ./tsconfig.json -r tsconfig-paths/register ../../../ts/src/scripts/migrate_v0.1.ts
16
- async function main() {
12
+ const ast_1 = require("../tsc/ast");
13
+ // todo-sqlite
14
+ // ts-node-script --swc --project ./tsconfig.json -r tsconfig-paths/register ../../ts/src/scripts/migrate_v0.1.ts --transform_schema --old_base_class BaseEntTodoSchema --new_schema_class TodoEntSchema --transform_path src/schema/patterns/base
15
+ function main() {
16
+ const customInfo = (0, ast_1.getCustomInfo)();
17
17
  const options = (0, minimist_1.default)(process.argv.slice(2));
18
18
  // install 0.1.x dependencies
19
19
  // maybe provide options to make this easier if someone wants to do this in steps to see what's happening
20
20
  if (options.move_generated) {
21
- (0, move_generated_1.moveGenerated)();
21
+ (0, move_generated_1.moveGenerated)(customInfo.relativeImports);
22
22
  }
23
23
  // codegen write-all --disable-custom-graphql
24
24
  if (options.transform_schema) {
25
- (0, transform_1.transform)(new transform_schema_1.TransformSchema());
25
+ (0, transform_1.transform)(new transform_schema_1.TransformSchema(customInfo.relativeImports, options.old_base_class, options.new_schema_class, options.transform_path));
26
26
  }
27
27
  // codegen write-all --disable-custom-graphql
28
28
  if (options.transform_ent) {
29
29
  (0, transform_1.transform)(new transform_ent_1.TransformEnt());
30
30
  }
31
31
  if (options.transform_action) {
32
- (0, transform_1.transform)(new transform_action_1.TransformAction());
32
+ (0, transform_1.transform)(new transform_action_1.TransformAction(customInfo));
33
33
  }
34
34
  // codegen write-all
35
35
  }
36
- main()
37
- .then(() => {
38
- process.exit(0);
39
- })
40
- .catch((err) => {
41
- console.error(err);
42
- process.exit(1);
43
- });
36
+ main();
package/tsc/ast.d.ts CHANGED
@@ -15,9 +15,9 @@ interface transformOpts {
15
15
  removeImports?: string[];
16
16
  newImports?: string[];
17
17
  transform?: transformImportFn;
18
- transformPath?: string;
18
+ transformPath: string;
19
19
  }
20
- export declare function transformImport(fileContents: string, importNode: ts.ImportDeclaration, sourceFile: ts.SourceFile, opts?: transformOpts): string | undefined;
20
+ export declare function transformImport(fileContents: string, importNode: ts.ImportDeclaration, sourceFile: ts.SourceFile, opts: transformOpts): string | undefined;
21
21
  export declare function updateImportPath(fileContents: string, importNode: ts.ImportDeclaration, sourceFile: ts.SourceFile, newPath: string): string | undefined;
22
22
  export declare function isRelativeImport(node: ts.ImportDeclaration, sourceFile: ts.SourceFile): boolean;
23
23
  export declare function isRelativeGeneratedImport(node: ts.ImportDeclaration, sourceFile: ts.SourceFile): boolean;
@@ -28,6 +28,16 @@ interface importInfo {
28
28
  end: number;
29
29
  importText: string;
30
30
  importPath: string;
31
+ default?: string;
31
32
  }
32
33
  export declare function getImportInfo(imp: ts.ImportDeclaration, sourceFile: ts.SourceFile): importInfo | undefined;
34
+ export declare function transformRelative(file: string, importPath: string, relative?: boolean): string;
35
+ export interface customInfo {
36
+ viewerInfo: {
37
+ path: string;
38
+ name: string;
39
+ };
40
+ relativeImports?: boolean;
41
+ }
42
+ export declare function getCustomInfo(): customInfo;
33
43
  export {};
package/tsc/ast.js CHANGED
@@ -1,10 +1,32 @@
1
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
+ };
2
21
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
22
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
23
  };
5
24
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getImportInfo = exports.isSrcGeneratedImport = exports.isRelativeGeneratedImport = exports.isRelativeImport = exports.updateImportPath = exports.transformImport = exports.getClassInfo = exports.getPreText = void 0;
25
+ exports.getCustomInfo = exports.transformRelative = exports.getImportInfo = exports.isSrcGeneratedImport = exports.isRelativeGeneratedImport = exports.isRelativeImport = exports.updateImportPath = exports.transformImport = exports.getClassInfo = exports.getPreText = void 0;
7
26
  const typescript_1 = __importDefault(require("typescript"));
27
+ const path = __importStar(require("path"));
28
+ const js_yaml_1 = require("js-yaml");
29
+ const fs = __importStar(require("fs"));
8
30
  function getPreText(fileContents, node, sourceFile) {
9
31
  return fileContents.substring(node.getFullStart(), node.getStart(sourceFile));
10
32
  }
@@ -81,26 +103,22 @@ exports.getClassInfo = getClassInfo;
81
103
  function transformImport(fileContents, importNode, sourceFile, opts) {
82
104
  // remove quotes too
83
105
  const text = importNode.moduleSpecifier.getText(sourceFile).slice(1, -1);
84
- if (opts?.transformPath) {
85
- if (text !== opts.transformPath) {
86
- return;
87
- }
88
- }
89
- else {
90
- if (text !== "@snowtop/ent" &&
91
- text !== "@snowtop/ent/schema" &&
92
- text !== "@snowtop/ent/schema/") {
93
- return;
94
- }
106
+ if (text != opts.transformPath) {
107
+ return;
95
108
  }
96
109
  const impInfo = getImportInfo(importNode, sourceFile);
97
110
  if (!impInfo) {
98
111
  return;
99
112
  }
100
- const { imports, start, end, importText } = impInfo;
113
+ let { imports, start, end, importText, default: def } = impInfo;
101
114
  let removeImportsMap = {};
102
115
  if (opts?.removeImports) {
103
- opts.removeImports.forEach((imp) => (removeImportsMap[imp] = true));
116
+ opts.removeImports.forEach((imp) => {
117
+ removeImportsMap[imp] = true;
118
+ if (def === imp) {
119
+ def = "";
120
+ }
121
+ });
104
122
  }
105
123
  let finalImports = new Set();
106
124
  for (let i = 0; i < imports.length; i++) {
@@ -122,9 +140,13 @@ function transformImport(fileContents, importNode, sourceFile, opts) {
122
140
  const comment = getPreText(fileContents, importNode, sourceFile);
123
141
  return (comment +
124
142
  "import " +
125
- importText.substring(0, start + 1) +
143
+ // add default
144
+ (def || "") +
145
+ // should probably always be "{" now that we support default
146
+ (start >= 0 ? importText.substring(0, start + 1) : "{") +
126
147
  Array.from(finalImports).join(", ") +
127
- importText.substring(end) +
148
+ // should probably always be "}"
149
+ (end >= 0 ? importText.substring(end) : "}") +
128
150
  ' from "' +
129
151
  text +
130
152
  '";');
@@ -169,23 +191,74 @@ function isSrcGeneratedImport(node, sourceFile) {
169
191
  return text.startsWith("src") && text.includes("/generated");
170
192
  }
171
193
  exports.isSrcGeneratedImport = isSrcGeneratedImport;
194
+ // TODO doesn't support default + {} yet
172
195
  function getImportInfo(imp, sourceFile) {
173
196
  const importText = imp.importClause?.getText(sourceFile) || "";
174
197
  const start = importText.indexOf("{");
175
198
  const end = importText.lastIndexOf("}");
176
199
  const text = imp.moduleSpecifier.getText(sourceFile).slice(1, -1);
177
- if (start === -1 || end === -1) {
200
+ if ((start === -1 || end === -1) && !importText.length) {
178
201
  return;
179
202
  }
203
+ let imports = [];
204
+ let def;
205
+ if (start !== -1 && end !== -1) {
206
+ imports = importText
207
+ .substring(start + 1, end)
208
+ //.trim()
209
+ .split(",");
210
+ }
211
+ else {
212
+ def = importText;
213
+ }
180
214
  return {
181
215
  importPath: text,
182
216
  importText,
183
217
  start,
184
218
  end,
185
- imports: importText
186
- .substring(start + 1, end)
187
- //.trim()
188
- .split(","),
219
+ imports,
220
+ default: def,
189
221
  };
190
222
  }
191
223
  exports.getImportInfo = getImportInfo;
224
+ function transformRelative(file, importPath, relative) {
225
+ if (!relative || !importPath.startsWith("src")) {
226
+ return importPath;
227
+ }
228
+ const fileFullPath = path.join(process.cwd(), file);
229
+ const impFullPath = path.join(process.cwd(), importPath);
230
+ // relative path is from directory
231
+ return normalizePath(path.relative(path.dirname(fileFullPath), impFullPath));
232
+ }
233
+ exports.transformRelative = transformRelative;
234
+ function normalizePath(p) {
235
+ if (p.endsWith("..")) {
236
+ return p + "/";
237
+ }
238
+ return p;
239
+ }
240
+ function getCustomInfo() {
241
+ let yaml = {};
242
+ let relativeImports = false;
243
+ try {
244
+ yaml = (0, js_yaml_1.load)(fs.readFileSync(path.join(process.cwd(), "ent.yml"), {
245
+ encoding: "utf8",
246
+ }));
247
+ relativeImports = yaml?.codegen?.relativeImports || false;
248
+ if (yaml?.codegen?.templatizedViewer) {
249
+ return {
250
+ viewerInfo: yaml.codegen.templatizedViewer,
251
+ relativeImports,
252
+ };
253
+ }
254
+ }
255
+ catch (e) { }
256
+ return {
257
+ viewerInfo: {
258
+ path: "@snowtop/ent",
259
+ name: "Viewer",
260
+ },
261
+ relativeImports,
262
+ };
263
+ }
264
+ exports.getCustomInfo = getCustomInfo;
@@ -1 +1 @@
1
- export declare function moveGenerated(): void;
1
+ export declare function moveGenerated(relativeImports: boolean): void;
@@ -29,7 +29,6 @@ const fs = __importStar(require("fs"));
29
29
  const typescript_1 = __importDefault(require("typescript"));
30
30
  const ast_1 = require("./ast");
31
31
  const transform_1 = require("./transform");
32
- const js_yaml_1 = require("js-yaml");
33
32
  class MoveFiles {
34
33
  constructor(globPath) {
35
34
  this.globPath = globPath;
@@ -40,16 +39,15 @@ class MoveFiles {
40
39
  }
41
40
  }
42
41
  class TransformImports {
43
- constructor(glob, prettierGlob, checkRelativeImportsValid) {
42
+ constructor(glob, prettierGlob, relativeImports, checkRelativeImportsValid) {
44
43
  this.glob = glob;
45
44
  this.prettierGlob = prettierGlob;
45
+ this.relativeImports = relativeImports;
46
46
  this.checkRelativeImportsValid = checkRelativeImportsValid;
47
- this.relativeImports = false;
48
47
  this.globOptions = {
49
48
  ignore: ["node_modules/**"],
50
49
  };
51
50
  this.cwd = process.cwd();
52
- this.relativeImports = relativeImports();
53
51
  }
54
52
  traverseChild(sourceFile, contents, file, node) {
55
53
  if (!typescript_1.default.isImportDeclaration(node)) {
@@ -153,23 +151,10 @@ function moveFiles(files) {
153
151
  fs.renameSync(file, newFile);
154
152
  });
155
153
  }
156
- function relativeImports() {
157
- let yaml = {};
158
- let relativeImports = false;
159
- try {
160
- yaml = (0, js_yaml_1.load)(fs.readFileSync(path.join(process.cwd(), "ent.yml"), {
161
- encoding: "utf8",
162
- }));
163
- relativeImports = yaml?.codegen?.relativeImports || false;
164
- return yaml?.codegen?.relativeImports || false;
165
- }
166
- catch (e) { }
167
- return false;
168
- }
169
- function moveGenerated() {
154
+ function moveGenerated(relativeImports) {
170
155
  new MoveFiles("src/ent/**/generated/**/**.ts").move();
171
156
  new MoveFiles("src/graphql/**/generated/**/**.ts").move();
172
- (0, transform_1.transform)(new TransformImports("src/ent/**/*.ts", "src/ent/**.ts"));
173
- (0, transform_1.transform)(new TransformImports("src/graphql/**/*.ts", "src/graphql/**.ts", true));
157
+ (0, transform_1.transform)(new TransformImports("src/ent/**/*.ts", "src/ent/**/*.ts", relativeImports));
158
+ (0, transform_1.transform)(new TransformImports("src/graphql/**/*.ts", "src/graphql/**/*.ts", relativeImports, true));
174
159
  }
175
160
  exports.moveGenerated = moveGenerated;
package/tsc/transform.js CHANGED
@@ -47,7 +47,17 @@ function transform(transform) {
47
47
  let imports = new Map();
48
48
  let removeImports = [];
49
49
  let traversed = false;
50
+ let seenImports = new Map();
50
51
  typescript_1.default.forEachChild(sourceFile, function (node) {
52
+ if (typescript_1.default.isImportDeclaration(node)) {
53
+ const imp = (0, ast_1.getImportInfo)(node, sourceFile);
54
+ if (imp) {
55
+ imp.imports.forEach((v) => {
56
+ v = v.trim();
57
+ v && seenImports.set(v, true);
58
+ });
59
+ }
60
+ }
51
61
  const ret = transform.traverseChild(sourceFile, contents, file, node);
52
62
  if (!ret) {
53
63
  return;
@@ -82,7 +92,10 @@ function transform(transform) {
82
92
  if (seen.has(imp)) {
83
93
  continue;
84
94
  }
85
- newContents += `\nimport { ${list.join(", ")} } from "${imp}"`;
95
+ const final = list.filter((v) => !seenImports.has(v));
96
+ if (final.length) {
97
+ newContents += `\nimport { ${final.join(", ")} } from "${imp}"`;
98
+ }
86
99
  }
87
100
  afterProcessed = true;
88
101
  }
@@ -91,10 +104,12 @@ function transform(transform) {
91
104
  if (node.node) {
92
105
  if (typescript_1.default.isImportDeclaration(node.node)) {
93
106
  const impInfo = (0, ast_1.getImportInfo)(node.node, sourceFile);
107
+ // console.debug(impInfo);
94
108
  if (impInfo) {
95
109
  const impPath = normalizePath(impInfo.importPath);
96
110
  const list = imports.get(impPath);
97
- if (list) {
111
+ // path exists, we care about it
112
+ if (list !== undefined) {
98
113
  let transformed = (0, ast_1.transformImport)(contents, node.node, sourceFile, {
99
114
  // if we've done this path before, don't try and add
100
115
  // but still try and do any removals since we don't know which of the imports
@@ -113,7 +128,8 @@ function transform(transform) {
113
128
  }
114
129
  }
115
130
  else {
116
- if (!typescript_1.default.isExportDeclaration(node.node)) {
131
+ if (!typescript_1.default.isExportDeclaration(node.node) &&
132
+ !isRequireStatement(node.node, sourceFile)) {
117
133
  processAfterImport();
118
134
  }
119
135
  }
@@ -131,7 +147,6 @@ function transform(transform) {
131
147
  if (transform.fileToWrite) {
132
148
  writeFile = transform.fileToWrite(file);
133
149
  }
134
- // TODO new file
135
150
  fs.writeFileSync(writeFile, newContents);
136
151
  if (transform.postProcess) {
137
152
  transform.postProcess(file);
@@ -142,3 +157,12 @@ function transform(transform) {
142
157
  }
143
158
  }
144
159
  exports.transform = transform;
160
+ function isRequireStatement(node, sourceFile) {
161
+ if (node.kind !== typescript_1.default.SyntaxKind.VariableStatement) {
162
+ return false;
163
+ }
164
+ const v = node;
165
+ // v.declarationList
166
+ const text = v.declarationList.declarations[0].getFullText(sourceFile);
167
+ return text.includes("require(");
168
+ }
@@ -1,17 +1,11 @@
1
1
  import ts from "typescript";
2
+ import { customInfo } from "../tsc/ast";
2
3
  import { TransformFile } from "./transform";
3
- interface customInfo {
4
- viewerInfo: {
5
- path: string;
6
- name: string;
7
- };
8
- relativeImports?: boolean;
9
- }
10
4
  export declare class TransformAction implements TransformFile {
5
+ private customInfo;
11
6
  glob: string;
12
- customInfo: customInfo;
13
7
  prettierGlob: string;
14
- constructor();
8
+ constructor(customInfo: customInfo);
15
9
  traverseChild(sourceFile: ts.SourceFile, contents: string, file: string, node: ts.Node): {
16
10
  node: ts.Node;
17
11
  rawString?: undefined;
@@ -24,4 +18,3 @@ export declare class TransformAction implements TransformFile {
24
18
  node?: undefined;
25
19
  } | undefined;
26
20
  }
27
- export {};
@@ -25,35 +25,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
25
25
  exports.TransformAction = void 0;
26
26
  const typescript_1 = __importDefault(require("typescript"));
27
27
  const ast_1 = require("../tsc/ast");
28
- const fs = __importStar(require("fs"));
29
28
  const action_1 = require("../action");
30
29
  const viewer_1 = require("../core/viewer");
31
30
  const path = __importStar(require("path"));
32
- const js_yaml_1 = require("js-yaml");
33
- function getCustomInfo() {
34
- let yaml = {};
35
- let relativeImports = false;
36
- try {
37
- yaml = (0, js_yaml_1.load)(fs.readFileSync(path.join(process.cwd(), "ent.yml"), {
38
- encoding: "utf8",
39
- }));
40
- relativeImports = yaml?.codegen?.relativeImports || false;
41
- if (yaml?.codegen?.templatizedViewer) {
42
- return {
43
- viewerInfo: yaml.codegen.templatizedViewer,
44
- relativeImports,
45
- };
46
- }
47
- }
48
- catch (e) { }
49
- return {
50
- viewerInfo: {
51
- path: "@snowtop/ent",
52
- name: "Viewer",
53
- },
54
- relativeImports,
55
- };
56
- }
31
+ const snake_case_1 = require("snake-case");
57
32
  function findInput(file, classInfo, sourceFile) {
58
33
  // @ts-ignore
59
34
  const importStatements = sourceFile.statements.filter((stmt) => typescript_1.default.isImportDeclaration(stmt));
@@ -116,26 +91,11 @@ function getConversionInfo(mm) {
116
91
  interface: v.i,
117
92
  };
118
93
  }
119
- function transformRelative(file, importPath, relative) {
120
- if (!relative || !importPath.startsWith("src")) {
121
- return importPath;
122
- }
123
- const fileFullPath = path.join(process.cwd(), file);
124
- const impFullPath = path.join(process.cwd(), importPath);
125
- // relative path is from directory
126
- return normalizePath(path.relative(path.dirname(fileFullPath), impFullPath));
127
- }
128
- function normalizePath(p) {
129
- if (p.endsWith("..")) {
130
- return p + "/";
131
- }
132
- return p;
133
- }
134
94
  class TransformAction {
135
- constructor() {
95
+ constructor(customInfo) {
96
+ this.customInfo = customInfo;
136
97
  this.glob = "src/ent/**/actions/**/*_action.ts";
137
98
  this.prettierGlob = "src/ent/**/actions/**.ts";
138
- this.customInfo = getCustomInfo();
139
99
  }
140
100
  traverseChild(sourceFile, contents, file, node) {
141
101
  if (!typescript_1.default.isClassDeclaration(node) || !node.heritageClauses) {
@@ -182,16 +142,21 @@ class TransformAction {
182
142
  klassContents += mm.getFullText(sourceFile);
183
143
  }
184
144
  }
145
+ const builderPath = `src/ent/generated/${(0, snake_case_1.snakeCase)(nodeName)}/actions/${(0, snake_case_1.snakeCase)(builder)}.ts`;
185
146
  let imports = new Map([
186
147
  [
187
- transformRelative(file, this.customInfo.viewerInfo.path, this.customInfo.relativeImports),
148
+ (0, ast_1.transformRelative)(file, this.customInfo.viewerInfo.path, this.customInfo.relativeImports),
188
149
  [viewer],
189
150
  ],
190
151
  [
191
- transformRelative(file, "src/ent", this.customInfo.relativeImports),
152
+ (0, ast_1.transformRelative)(file, "src/ent", this.customInfo.relativeImports),
192
153
  [nodeName],
193
154
  ],
194
155
  ["@snowtop/ent/action", newImports],
156
+ [
157
+ (0, ast_1.transformRelative)(file, builderPath, this.customInfo.relativeImports),
158
+ [builder],
159
+ ],
195
160
  ]);
196
161
  // wrap comments and transform to export class Foo extends Bar { ${inner} }
197
162
  return {
@@ -1,7 +1,13 @@
1
1
  import ts from "typescript";
2
2
  import { TransformFile } from "./transform";
3
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);
4
9
  glob: string;
10
+ private transformSchema;
5
11
  traverseChild(sourceFile: ts.SourceFile, contents: string, file: string, node: ts.Node): {
6
12
  node: ts.Node;
7
13
  traversed?: undefined;
@@ -27,8 +27,8 @@ const typescript_1 = __importDefault(require("typescript"));
27
27
  const fs = __importStar(require("fs"));
28
28
  const path_1 = __importDefault(require("path"));
29
29
  const ast_1 = require("../tsc/ast");
30
- function traverseClass(fileContents, sourceFile, node) {
31
- const ci = getTransformClassInfo(fileContents, sourceFile, node);
30
+ function traverseClass(fileContents, sourceFile, node, transformSchema) {
31
+ const ci = getTransformClassInfo(fileContents, sourceFile, node, transformSchema);
32
32
  if (!ci) {
33
33
  return;
34
34
  }
@@ -63,17 +63,7 @@ function traverseClass(fileContents, sourceFile, node) {
63
63
  newImports,
64
64
  };
65
65
  }
66
- function transformSchema(str) {
67
- // only do known class names
68
- if (str === "BaseEntSchema" || str === "BaseEntSchemaWithTZ") {
69
- return str.substring(4);
70
- }
71
- return str;
72
- }
73
- // TODO need to generify this...
74
- // and then have a schema specific version
75
- // may make sense to just duplicate this logic...
76
- function getTransformClassInfo(fileContents, sourceFile, node) {
66
+ function getTransformClassInfo(fileContents, sourceFile, node, transformSchema) {
77
67
  const generic = (0, ast_1.getClassInfo)(fileContents, sourceFile, node);
78
68
  if (!generic) {
79
69
  return;
@@ -84,6 +74,7 @@ function getTransformClassInfo(fileContents, sourceFile, node) {
84
74
  }
85
75
  let implementsSchema = generic.implements?.some((v) => v == "Schema");
86
76
  let classExtends = generic.extends;
77
+ // nothing transformed here, so nothing to do here
87
78
  if (classExtends && classExtends === transformSchema(classExtends)) {
88
79
  return undefined;
89
80
  }
@@ -285,7 +276,7 @@ function parseFieldElement(element, sourceFile, fileContents, nested) {
285
276
  };
286
277
  }
287
278
  // find which of these importPaths is being used and use that to replace
288
- function findImportPath(sourceFile) {
279
+ function findSchemaImportPath(sourceFile) {
289
280
  const paths = {
290
281
  "@snowtop/ent": true,
291
282
  "@snowtop/ent/schema": true,
@@ -304,25 +295,47 @@ function findImportPath(sourceFile) {
304
295
  }
305
296
  }
306
297
  class TransformSchema {
307
- constructor() {
308
- // we only end up doing this once because we change the schema representation
309
- // so safe to run this multiple times
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;
310
305
  this.glob = "src/schema/*.ts";
311
306
  this.prettierGlob = "src/schema/*.ts";
312
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
+ }
313
317
  traverseChild(sourceFile, contents, file, node) {
314
318
  if (!typescript_1.default.isClassDeclaration(node)) {
315
319
  return { node };
316
320
  }
317
321
  // TODO address implicit schema doesn't work here...
318
- const ret = traverseClass(contents, sourceFile, node);
322
+ const ret = traverseClass(contents, sourceFile, node, this.transformSchema.bind(this));
319
323
  if (ret === undefined) {
320
324
  return;
321
325
  }
322
326
  let imports = new Map();
323
- const imp = findImportPath(sourceFile);
327
+ const imp = findSchemaImportPath(sourceFile);
324
328
  if (imp) {
325
- imports.set(imp, ret.newImports);
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);
326
339
  }
327
340
  return {
328
341
  traversed: true,