@snowtop/ent 0.2.10 → 0.2.12

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.
@@ -61,11 +61,12 @@ const isCustomType = (type) => {
61
61
  };
62
62
  exports.isCustomType = isCustomType;
63
63
  const isGraphQLScalarType = (type) => {
64
- return !!type &&
64
+ return (!isGraphQLEnumType(type) &&
65
+ !!type &&
65
66
  typeof type === "object" &&
66
67
  typeof type.serialize === "function" &&
67
68
  typeof type.parseValue === "function" &&
68
- typeof type.parseLiteral === "function";
69
+ typeof type.parseLiteral === "function");
69
70
  };
70
71
  const isGraphQLEnumType = (type) => {
71
72
  return (!!type &&
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@snowtop/ent",
3
- "version": "0.2.10",
3
+ "version": "0.2.12",
4
4
  "description": "snowtop ent framework",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -8,22 +8,22 @@
8
8
  "example": "examples"
9
9
  },
10
10
  "dependencies": {
11
- "@types/node": "^25.0.3",
12
- "dataloader": "^2.2.3",
13
- "glob": "^13.0.0",
14
- "js-yaml": "^4.1.1",
15
- "luxon": "^3.7.2",
16
- "pg": "^8.16.3",
17
- "ts-node": "^11.0.0-beta.1",
18
- "tsconfig-paths": "^4.2.0",
19
- "tslib": "^2.8.1",
20
- "typescript": "^5.9.3",
21
- "uuid": "^9.0.1"
11
+ "@types/node": "25.0.3",
12
+ "dataloader": "2.2.3",
13
+ "glob": "13.0.0",
14
+ "js-yaml": "4.1.1",
15
+ "luxon": "3.7.2",
16
+ "pg": "8.16.3",
17
+ "ts-node": "11.0.0-beta.1",
18
+ "tsconfig-paths": "4.2.0",
19
+ "tslib": "2.8.1",
20
+ "typescript": "5.9.3",
21
+ "uuid": "9.0.1"
22
22
  },
23
23
  "peerDependencies": {
24
- "@swc-node/register": "^1.6.8",
25
- "better-sqlite3": "^12.5.0",
26
- "graphql": "^16.8.1"
24
+ "@swc-node/register": "1.6.8",
25
+ "better-sqlite3": "12.5.0",
26
+ "graphql": "16.12.0"
27
27
  },
28
28
  "peerDependenciesMeta": {
29
29
  "better-sqlite3": {
package/schema/field.js CHANGED
@@ -63,13 +63,13 @@ exports.IntegerEnumListType = IntegerEnumListType;
63
63
  exports.UUIDListType = UUIDListType;
64
64
  const luxon_1 = require("luxon");
65
65
  const types_1 = require("util/types");
66
- const uuid_1 = require("uuid");
67
66
  const base_1 = require("../core/base");
68
67
  const db_1 = __importStar(require("../core/db"));
69
68
  const global_schema_1 = require("../core/global_schema");
70
69
  const logger_1 = require("../core/logger");
71
70
  const names_1 = require("../names/names");
72
71
  const schema_1 = require("./schema");
72
+ const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
73
73
  class BaseField {
74
74
  logValue(val) {
75
75
  if (this.sensitive) {
@@ -133,7 +133,7 @@ class UUIDField extends BaseField {
133
133
  return val.placeholderID !== undefined;
134
134
  }
135
135
  async valid(val) {
136
- if (typeof val === "string" && !(0, uuid_1.validate)(val)) {
136
+ if (typeof val === "string" && !UUID_REGEX.test(val)) {
137
137
  return false;
138
138
  }
139
139
  if (!this.options?.fieldEdge?.enforceSchema) {
@@ -48,6 +48,7 @@ const imports_1 = require("../imports");
48
48
  const process_1 = require("process");
49
49
  const child_process_1 = require("child_process");
50
50
  const const_1 = require("../core/const");
51
+ const stdout_1 = require("./stdout");
51
52
  const { parseArgs } = require("./parse_args");
52
53
  // need to use the GQLCapture from the package so that when we call GQLCapture.enable()
53
54
  // we're affecting the local paths as opposed to a different instance
@@ -243,23 +244,7 @@ async function processJSON(gqlCapture, json) {
243
244
  processCustomTypes(json.customTypes, gqlCapture);
244
245
  }
245
246
  }
246
- async function captureCustom(filePath, filesCsv, jsonPath, gqlCapture) {
247
- if (jsonPath !== undefined) {
248
- const json = parseJSONC(jsonPath, fs.readFileSync(jsonPath, {
249
- encoding: "utf8",
250
- }));
251
- processJSON(gqlCapture, json);
252
- return;
253
- }
254
- if (filesCsv !== undefined) {
255
- let files = filesCsv.split(",");
256
- for (let i = 0; i < files.length; i++) {
257
- // TODO fix. we have "src" in the path we get here
258
- files[i] = path.join(filePath, "..", files[i]);
259
- }
260
- await requireFiles(files);
261
- return;
262
- }
247
+ async function loadGraphQLDecoratorFiles(filePath) {
263
248
  // TODO delete all of this eventually
264
249
  // TODO configurable paths eventually
265
250
  // for now only files that are in the include path of the roots are allowed
@@ -286,11 +271,69 @@ async function captureCustom(filePath, filesCsv, jsonPath, gqlCapture) {
286
271
  .filter(fileImportsGraphQLDecorators);
287
272
  await requireFiles(files);
288
273
  }
274
+ async function captureCustom(filePath, filesCsv, jsonPath, gqlCapture) {
275
+ if (jsonPath !== undefined) {
276
+ const json = parseJSONC(jsonPath, fs.readFileSync(jsonPath, {
277
+ encoding: "utf8",
278
+ }));
279
+ await processJSON(gqlCapture, json);
280
+ await loadGraphQLDecoratorFiles(filePath);
281
+ return;
282
+ }
283
+ if (filesCsv !== undefined) {
284
+ let files = filesCsv.split(",");
285
+ for (let i = 0; i < files.length; i++) {
286
+ // TODO fix. we have "src" in the path we get here
287
+ files[i] = path.join(filePath, "..", files[i]);
288
+ }
289
+ await requireFiles(files);
290
+ return;
291
+ }
292
+ await loadGraphQLDecoratorFiles(filePath);
293
+ }
294
+ const customGraphQLDecoratorImports = new Set([
295
+ "gqlArgType",
296
+ "gqlField",
297
+ "gqlInputObjectType",
298
+ "gqlInterfaceType",
299
+ "gqlMutation",
300
+ "gqlObjectType",
301
+ "gqlQuery",
302
+ "gqlUnionType",
303
+ ]);
289
304
  function fileImportsGraphQLDecorators(file) {
290
305
  const contents = fs.readFileSync(file, {
291
306
  encoding: "utf8",
292
307
  });
293
- return /@snowtop\/ent\/graphql(?:\/graphql)?/.test(contents);
308
+ const sourceFile = typescript_1.default.createSourceFile(file, contents, typescript_1.default.ScriptTarget.ES2015, true);
309
+ for (const statement of sourceFile.statements) {
310
+ if (!typescript_1.default.isImportDeclaration(statement)) {
311
+ continue;
312
+ }
313
+ const moduleSpecifier = statement.moduleSpecifier;
314
+ if (!typescript_1.default.isStringLiteral(moduleSpecifier) ||
315
+ (moduleSpecifier.text !== "@snowtop/ent/graphql" &&
316
+ moduleSpecifier.text !== "@snowtop/ent/graphql/graphql")) {
317
+ continue;
318
+ }
319
+ if (statement.importClause?.isTypeOnly) {
320
+ continue;
321
+ }
322
+ const namedBindings = statement.importClause?.namedBindings;
323
+ if (!namedBindings || !typescript_1.default.isNamedImports(namedBindings)) {
324
+ continue;
325
+ }
326
+ for (const importSpecifier of namedBindings.elements) {
327
+ if (importSpecifier.isTypeOnly) {
328
+ continue;
329
+ }
330
+ const importedName = importSpecifier.propertyName?.text ?? importSpecifier.name.text;
331
+ if (customGraphQLDecoratorImports.has(importedName)) {
332
+ return true;
333
+ }
334
+ }
335
+ }
336
+ return false;
294
337
  }
295
338
  async function requireFiles(files) {
296
339
  for (const file of files) {
@@ -398,10 +441,6 @@ async function main() {
398
441
  importPath: "graphql-type-json",
399
442
  type: "GraphQLJSON",
400
443
  }, gqlCapture);
401
- (0, graphql_1.addCustomType)({
402
- importPath: "graphql-scalars",
403
- type: "GraphQLByte",
404
- }, gqlCapture);
405
444
  const [inputsRead, _, __, imports] = await Promise.all([
406
445
  readInputs(),
407
446
  captureCustom(options.path, options.files, options.json_path, gqlCapture),
@@ -479,7 +518,7 @@ async function main() {
479
518
  for (const k in fields) {
480
519
  buildClasses(fields[k]);
481
520
  }
482
- console.log(JSON.stringify({
521
+ await (0, stdout_1.writeJSONToStdout)({
483
522
  args,
484
523
  inputs,
485
524
  fields,
@@ -491,7 +530,7 @@ async function main() {
491
530
  unions,
492
531
  files: allFiles,
493
532
  customTypes,
494
- }));
533
+ });
495
534
  }
496
535
  main()
497
536
  .then()
@@ -39,7 +39,8 @@ const { parseArgs } = require("./parse_args");
39
39
  const parse_1 = require("../parse_schema/parse");
40
40
  const ast_1 = require("../tsc/ast");
41
41
  const names_1 = require("../names/names");
42
- function main() {
42
+ const stdout_1 = require("./stdout");
43
+ async function main() {
43
44
  const options = parseArgs(process.argv.slice(2));
44
45
  if (!options.path) {
45
46
  throw new Error("path required");
@@ -80,16 +81,10 @@ function main() {
80
81
  potentialSchemas[(0, names_1.toClassName)(schema)] = s;
81
82
  }
82
83
  // console.log(potentialSchemas);
83
- // NB: do not change this to async/await
84
- // doing so runs it buffer limit on linux (65536 bytes) and we lose data reading in go
85
- (0, parse_1.parseSchema)(potentialSchemas, globalSchema).then((result) => {
86
- console.log(JSON.stringify(result));
87
- });
88
- }
89
- try {
90
- main();
84
+ const result = await (0, parse_1.parseSchema)(potentialSchemas, globalSchema);
85
+ await (0, stdout_1.writeJSONToStdout)(result);
91
86
  }
92
- catch (err) {
87
+ main().catch((err) => {
93
88
  console.error(err);
94
89
  process.exit(1);
95
- }
90
+ });
@@ -0,0 +1 @@
1
+ export declare function writeJSONToStdout(value: unknown): Promise<void>;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.writeJSONToStdout = writeJSONToStdout;
4
+ async function writeJSONToStdout(value) {
5
+ await writeToStdout(JSON.stringify(value));
6
+ }
7
+ async function writeToStdout(payload) {
8
+ // Bun can exit before a large piped stdout write is fully consumed.
9
+ await new Promise((resolve, reject) => {
10
+ process.stdout.write(payload, (err) => {
11
+ if (err) {
12
+ reject(err);
13
+ return;
14
+ }
15
+ resolve();
16
+ });
17
+ });
18
+ }
@@ -450,8 +450,8 @@ class TempDB {
450
450
  db_1.default.initDB({
451
451
  connectionString: connStr,
452
452
  cfg: {
453
- max: 200,
454
- idleTimeoutMillis: 100,
453
+ max: 50,
454
+ idleTimeoutMillis: 10000,
455
455
  },
456
456
  });
457
457
  }
@@ -653,11 +653,22 @@ function setupPostgres(tables, opts) {
653
653
  if (!opts?.disableDeleteAfterEachTest) {
654
654
  afterEach(async () => {
655
655
  const client = await db_1.default.getInstance().getNewClient();
656
- for (const [key, _] of tdb.__getTables()) {
657
- const query = `delete from ${key}`;
658
- await client.exec(query);
656
+ let err;
657
+ let hadErr = false;
658
+ try {
659
+ for (const [key, _] of tdb.__getTables()) {
660
+ const query = `delete from ${key}`;
661
+ await client.exec(query);
662
+ }
663
+ }
664
+ catch (e) {
665
+ err = e;
666
+ hadErr = true;
667
+ throw e;
668
+ }
669
+ finally {
670
+ client.release(hadErr ? (err instanceof Error ? err : true) : undefined);
659
671
  }
660
- client.release();
661
672
  });
662
673
  }
663
674
  afterAll(async () => {
@@ -41,6 +41,9 @@ const db_1 = __importStar(require("../core/db"));
41
41
  function isSyncClient(client) {
42
42
  return client.execSync !== undefined;
43
43
  }
44
+ function releaseError(err) {
45
+ return err instanceof Error ? err : true;
46
+ }
44
47
  async function createRowForTest(options, suffix) {
45
48
  let client;
46
49
  if (db_1.Dialect.SQLite === db_1.default.getDialect()) {
@@ -49,37 +52,58 @@ async function createRowForTest(options, suffix) {
49
52
  else {
50
53
  client = await db_1.default.getInstance().getNewClient();
51
54
  }
55
+ let err;
56
+ let hadErr = false;
52
57
  try {
53
58
  if (isSyncClient(client)) {
54
59
  return (0, ent_1.createRowSync)(client, options, suffix || "");
55
60
  }
56
61
  return (0, ent_1.createRow)(client, options, suffix || "");
57
62
  }
63
+ catch (e) {
64
+ err = e;
65
+ hadErr = true;
66
+ throw e;
67
+ }
58
68
  finally {
59
- client.release();
69
+ client.release(hadErr ? releaseError(err) : undefined);
60
70
  }
61
71
  }
62
72
  async function editRowForTest(options, suffix) {
63
73
  const client = await db_1.default.getInstance().getNewClient();
74
+ let err;
75
+ let hadErr = false;
64
76
  try {
65
77
  if (isSyncClient(client)) {
66
78
  return (0, ent_1.editRowSync)(client, options, suffix || "");
67
79
  }
68
80
  return (0, ent_1.editRow)(client, options, suffix);
69
81
  }
82
+ catch (e) {
83
+ err = e;
84
+ hadErr = true;
85
+ throw e;
86
+ }
70
87
  finally {
71
- client.release();
88
+ client.release(hadErr ? releaseError(err) : undefined);
72
89
  }
73
90
  }
74
91
  async function deleteRowsForTest(options, cls) {
75
92
  const client = await db_1.default.getInstance().getNewClient();
93
+ let err;
94
+ let hadErr = false;
76
95
  try {
77
96
  if (isSyncClient(client)) {
78
97
  return (0, ent_1.deleteRowsSync)(client, options, cls);
79
98
  }
80
99
  return (0, ent_1.deleteRows)(client, options, cls);
81
100
  }
101
+ catch (e) {
102
+ err = e;
103
+ hadErr = true;
104
+ throw e;
105
+ }
82
106
  finally {
83
- client.release();
107
+ client.release(hadErr ? releaseError(err) : undefined);
84
108
  }
85
109
  }
@@ -1 +0,0 @@
1
- export {};
@@ -1,33 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const { parseArgs } = require("./parse_args");
4
- const transform_1 = require("../tsc/transform");
5
- const transform_schema_1 = require("../tsc/transform_schema");
6
- const transform_ent_1 = require("../tsc/transform_ent");
7
- const move_generated_1 = require("../tsc/move_generated");
8
- const transform_action_1 = require("../tsc/transform_action");
9
- const ast_1 = require("../tsc/ast");
10
- // todo-sqlite
11
- // 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
12
- function main() {
13
- const customInfo = (0, ast_1.getCustomInfo)();
14
- const options = parseArgs(process.argv.slice(2));
15
- // install 0.1.x dependencies
16
- // maybe provide options to make this easier if someone wants to do this in steps to see what's happening
17
- if (options.move_generated) {
18
- (0, move_generated_1.moveGenerated)(customInfo.relativeImports);
19
- }
20
- // codegen write-all --disable-custom-graphql
21
- if (options.transform_schema) {
22
- (0, transform_1.transform)(new transform_schema_1.TransformSchema(customInfo.relativeImports, options.old_base_class, options.new_schema_class, options.transform_path));
23
- }
24
- // codegen write-all --disable-custom-graphql
25
- if (options.transform_ent) {
26
- (0, transform_1.transform)(new transform_ent_1.TransformEnt());
27
- }
28
- if (options.transform_action) {
29
- (0, transform_1.transform)(new transform_action_1.TransformAction(customInfo));
30
- }
31
- // codegen write-all
32
- }
33
- main();
@@ -1 +0,0 @@
1
- export declare function moveGenerated(relativeImports: boolean): void;
@@ -1,173 +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
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- var __importDefault = (this && this.__importDefault) || function (mod) {
36
- return (mod && mod.__esModule) ? mod : { "default": mod };
37
- };
38
- Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.moveGenerated = moveGenerated;
40
- const glob = __importStar(require("glob"));
41
- const path = __importStar(require("path"));
42
- const fs = __importStar(require("fs"));
43
- const typescript_1 = __importDefault(require("typescript"));
44
- const ast_1 = require("./ast");
45
- const transform_1 = require("./transform");
46
- class MoveFiles {
47
- constructor(globPath) {
48
- this.globPath = globPath;
49
- }
50
- move() {
51
- const files = glob.sync(this.globPath);
52
- moveFiles(files);
53
- }
54
- }
55
- class TransformImports {
56
- constructor(glob, formatGlob, relativeImports, checkRelativeImportsValid) {
57
- this.glob = glob;
58
- this.formatGlob = formatGlob;
59
- this.relativeImports = relativeImports;
60
- this.checkRelativeImportsValid = checkRelativeImportsValid;
61
- this.globOptions = {
62
- ignore: ["node_modules/**"],
63
- };
64
- this.cwd = process.cwd();
65
- }
66
- traverseChild(sourceFile, contents, file, node) {
67
- if (!typescript_1.default.isImportDeclaration(node)) {
68
- return { node };
69
- }
70
- let dirPath = path.join(this.cwd, file, "..");
71
- const newImportPath = this.getNewImportPath(node, file, sourceFile, dirPath);
72
- if (!newImportPath) {
73
- return { node };
74
- }
75
- const v = (0, ast_1.updateImportPath)(contents, node, sourceFile, newImportPath);
76
- return {
77
- traversed: true,
78
- rawString: v,
79
- };
80
- }
81
- getNewImportPath(node, file, sourceFile, dirPath) {
82
- const text = node.moduleSpecifier.getText(sourceFile).slice(1, -1);
83
- // it's relative and has generated in there, continue
84
- // do relative imports path regardless of if relative imports is on or not
85
- if ((0, ast_1.isRelativeGeneratedImport)(node, sourceFile)) {
86
- const oldPath = path.join(dirPath, text);
87
- const relFromRoot = path.relative(".", oldPath);
88
- const conv = transformPath(relFromRoot);
89
- if (!conv || conv.newFile === relFromRoot) {
90
- return;
91
- }
92
- return path.relative(dirPath, conv.newFile);
93
- }
94
- if (this.checkRelativeImportsValid && (0, ast_1.isRelativeImport)(node, sourceFile)) {
95
- const parts = file.split(path.sep);
96
- if (parts.length === 5 &&
97
- parts.slice(0, 3).join(path.sep) ===
98
- ["src", "graphql", "generated"].join(path.sep)) {
99
- // we have custom graphql import paths
100
- // src/graphql/generated/mutations|resolvers/foo_type.ts
101
- // which used to be
102
- // src/graphql/mutations|resolvers/generated/foo_type.ts
103
- // we probably have a broken import.
104
- // try and fix it...
105
- let temp = parts[2];
106
- parts[2] = parts[3];
107
- parts[3] = temp;
108
- const oldPath = parts.join(path.sep);
109
- let oldDir = path.join(this.cwd, oldPath, "..");
110
- let importPath = path.join(oldDir, text);
111
- let exists = fs.statSync(importPath + ".ts", { throwIfNoEntry: false });
112
- if (!exists) {
113
- // doesn't exist. sadly has to be fixed manually. could theoretically also be a directory but we shouldn't have that
114
- return;
115
- }
116
- return path.relative(dirPath, importPath);
117
- }
118
- }
119
- // if relative imports, we done.
120
- if (this.relativeImports) {
121
- return;
122
- }
123
- // non relative, only transform src paths with generated
124
- if ((0, ast_1.isSrcGeneratedImport)(node, sourceFile)) {
125
- const conv = transformPath(text);
126
- if (!conv || conv.newFile === text) {
127
- return;
128
- }
129
- return conv.newFile;
130
- }
131
- return;
132
- }
133
- }
134
- function transformPath(old) {
135
- const parts = old.split(path.sep);
136
- if (parts.length < 3) {
137
- return;
138
- }
139
- const changedParts = parts
140
- .slice(0, 2)
141
- .concat("generated")
142
- .concat(parts.slice(2).filter((v) => v !== "generated"));
143
- const newFile = changedParts.join(path.sep);
144
- return { changedParts, newFile };
145
- }
146
- function moveFiles(files) {
147
- files.forEach((file) => {
148
- const conv = transformPath(file);
149
- if (!conv) {
150
- return;
151
- }
152
- const { changedParts, newFile } = conv;
153
- if (file === newFile) {
154
- return;
155
- }
156
- // check if directory exists, if not, create recursive dir
157
- const p = changedParts.slice(0, changedParts.length - 1).join(path.sep);
158
- const statInfo = fs.statSync(p, { throwIfNoEntry: false });
159
- if (!statInfo) {
160
- fs.mkdirSync(p, {
161
- recursive: true,
162
- });
163
- }
164
- // move file to new location
165
- fs.renameSync(file, newFile);
166
- });
167
- }
168
- function moveGenerated(relativeImports) {
169
- new MoveFiles("src/ent/**/generated/**/**.ts").move();
170
- new MoveFiles("src/graphql/**/generated/**/**.ts").move();
171
- (0, transform_1.transform)(new TransformImports("src/ent/**/*.ts", "src/ent/**/*.ts", relativeImports));
172
- (0, transform_1.transform)(new TransformImports("src/graphql/**/*.ts", "src/graphql/**/*.ts", relativeImports, true));
173
- }
@@ -1,22 +0,0 @@
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
- formatGlob: 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
- }
@@ -1,193 +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
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- var __importDefault = (this && this.__importDefault) || function (mod) {
36
- return (mod && mod.__esModule) ? mod : { "default": mod };
37
- };
38
- Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.TransformAction = void 0;
40
- const typescript_1 = __importDefault(require("typescript"));
41
- const ast_1 = require("../tsc/ast");
42
- const viewer_1 = require("../core/viewer");
43
- const path = __importStar(require("path"));
44
- const names_1 = require("../names/names");
45
- // returns input and importPath
46
- function getBaseFileInfo(file, classInfo, sourceFile) {
47
- // @ts-ignore
48
- const importStatements = sourceFile.statements.filter((stmt) => typescript_1.default.isImportDeclaration(stmt));
49
- for (const imp of importStatements) {
50
- const text = imp.moduleSpecifier.getText(sourceFile).slice(1, -1);
51
- if ((0, ast_1.isSrcGeneratedImport)(imp, sourceFile) ||
52
- (0, ast_1.isRelativeGeneratedImport)(imp, sourceFile)) {
53
- // base file and we're importing from it
54
- // e.g. in create_user_action, we're importing from create_user_action_base
55
- if (path.basename(file).slice(0, -3) + "_base" !== path.basename(text)) {
56
- continue;
57
- }
58
- const impInfo = (0, ast_1.getImportInfo)(imp, sourceFile);
59
- if (!impInfo) {
60
- continue;
61
- }
62
- let inputs = impInfo.imports
63
- .filter((imp) => imp.trim() && imp.endsWith("Input"))
64
- .map((v) => v.trim());
65
- if (inputs.length === 1) {
66
- return {
67
- input: inputs[0],
68
- importPath: impInfo.importPath,
69
- };
70
- }
71
- if (inputs.length && classInfo.name.endsWith("Action")) {
72
- const prefix = classInfo.name.slice(0, classInfo.name.length - 6);
73
- inputs = inputs.filter((imp) => imp.slice(0, imp.length - 5) === prefix);
74
- if (inputs.length === 1) {
75
- return {
76
- input: inputs[0],
77
- importPath: impInfo.importPath,
78
- };
79
- }
80
- }
81
- }
82
- }
83
- return null;
84
- }
85
- let m = {
86
- triggers: {
87
- m: "getTriggers",
88
- i: "Trigger",
89
- suffix: "Triggers",
90
- },
91
- observers: {
92
- m: "getObservers",
93
- i: "Observer",
94
- suffix: "Observers",
95
- },
96
- validators: {
97
- m: "getValidators",
98
- i: "Validator",
99
- suffix: "Validators",
100
- },
101
- };
102
- function getConversionInfo(mm, actionName) {
103
- if (mm.kind !== typescript_1.default.SyntaxKind.PropertyDeclaration) {
104
- return null;
105
- }
106
- const text = mm.name.escapedText;
107
- const v = m[text];
108
- if (v === undefined) {
109
- return null;
110
- }
111
- return {
112
- text,
113
- method: v.m,
114
- interface: v.i,
115
- // CreateFooActionTriggers etc
116
- methodType: actionName + v.suffix,
117
- };
118
- }
119
- class TransformAction {
120
- constructor(customInfo) {
121
- this.customInfo = customInfo;
122
- this.glob = "src/ent/**/actions/**/*_action.ts";
123
- this.formatGlob = "src/ent/**/actions/**.ts";
124
- }
125
- traverseChild(sourceFile, contents, file, node) {
126
- if (!typescript_1.default.isClassDeclaration(node) || !node.heritageClauses) {
127
- return { node };
128
- }
129
- let classInfo = (0, ast_1.getClassInfo)(contents, sourceFile, node);
130
- // only do classes
131
- if (!classInfo || !classInfo.default) {
132
- return;
133
- }
134
- // require action
135
- const p = require(path.join(process.cwd(), "./" + file.slice(0, -3)));
136
- const action = new p.default(new viewer_1.LoggedOutViewer(), {});
137
- const actionName = action.constructor.name;
138
- const builder = action.builder.constructor.name;
139
- const nodeName = action.builder.ent.name;
140
- const viewer = this.customInfo.viewerInfo.name;
141
- const baseInfo = getBaseFileInfo(file, classInfo, sourceFile);
142
- if (!baseInfo) {
143
- return;
144
- }
145
- let klassContents = "";
146
- let traversed = false;
147
- let newImports = [];
148
- for (const mm of node.members) {
149
- const conv = getConversionInfo(mm, actionName);
150
- if (conv !== null) {
151
- const property = mm;
152
- // if invalid, bounce
153
- if (!property.initializer) {
154
- return;
155
- }
156
- traversed = true;
157
- const pp = property.initializer.getFullText(sourceFile).trim();
158
- const code = `${conv.method}(): ${conv.methodType} {
159
- return ${pp}
160
- }`;
161
- newImports.push(conv.methodType);
162
- klassContents += (0, ast_1.getPreText)(contents, mm, sourceFile) + code;
163
- }
164
- else {
165
- klassContents += mm.getFullText(sourceFile);
166
- }
167
- }
168
- const builderPath = `src/ent/generated/${(0, names_1.toFilePath)(nodeName)}/actions/${(0, names_1.toFilePath)(builder)}`;
169
- let imports = new Map([
170
- [
171
- (0, ast_1.transformRelative)(file, this.customInfo.viewerInfo.path, this.customInfo.relativeImports),
172
- [viewer],
173
- ],
174
- [
175
- (0, ast_1.transformRelative)(file, baseInfo.importPath, this.customInfo.relativeImports),
176
- newImports,
177
- ],
178
- [
179
- (0, ast_1.transformRelative)(file, builderPath, this.customInfo.relativeImports),
180
- [builder],
181
- ],
182
- ]);
183
- // wrap comments and transform to export class Foo extends Bar { ${inner} }
184
- return {
185
- rawString: classInfo.wrapClassContents(klassContents),
186
- traversed,
187
- imports,
188
- removeImports: ["Trigger", "Observer", "Validator"],
189
- // not removing FooBuilder incase it's still somehow used in type of inline builders
190
- };
191
- }
192
- }
193
- exports.TransformAction = TransformAction;
@@ -1,17 +0,0 @@
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
- formatGlob: string;
17
- }
@@ -1,60 +0,0 @@
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 const_1 = require("../core/const");
8
- const typescript_1 = __importDefault(require("typescript"));
9
- const ast_1 = require("./ast");
10
- function isPrivacyPolicy(mm) {
11
- return (mm.kind === typescript_1.default.SyntaxKind.PropertyDeclaration &&
12
- mm.name.escapedText === "privacyPolicy");
13
- }
14
- class TransformEnt {
15
- constructor() {
16
- this.glob = "src/ent/*.ts";
17
- this.formatGlob = "src/ent/*.ts";
18
- }
19
- traverseChild(sourceFile, contents, file, node) {
20
- if (!typescript_1.default.isClassDeclaration(node) || !node.heritageClauses) {
21
- return { node };
22
- }
23
- let classInfo = (0, ast_1.getClassInfo)(contents, sourceFile, node);
24
- if (!classInfo) {
25
- return;
26
- }
27
- // different class. ignore
28
- // only do classes which extend a base class e.g. User extends UserBase
29
- // if different class (e.g. privacy rule), just return it
30
- if (classInfo.extends !== classInfo.name + "Base") {
31
- return { node };
32
- }
33
- let klassContents = "";
34
- let traversed = false;
35
- for (const mm of node.members) {
36
- if (isPrivacyPolicy(mm)) {
37
- const property = mm;
38
- // if invalid privacy policy, bounce
39
- if (!property.initializer) {
40
- return;
41
- }
42
- const pp = property.initializer.getFullText(sourceFile);
43
- const code = `getPrivacyPolicy(): PrivacyPolicy<this> {
44
- return ${pp}
45
- }`;
46
- klassContents += (0, ast_1.getPreText)(contents, mm, sourceFile) + code;
47
- traversed = true;
48
- }
49
- else {
50
- klassContents += mm.getFullText(sourceFile);
51
- }
52
- }
53
- return {
54
- rawString: classInfo.wrapClassContents(klassContents),
55
- traversed,
56
- imports: new Map([[const_1.PACKAGE, ["PrivacyPolicy"]]]),
57
- };
58
- }
59
- }
60
- exports.TransformEnt = TransformEnt;
@@ -1,27 +0,0 @@
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
- formatGlob: string;
27
- }
@@ -1,394 +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
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- var __importDefault = (this && this.__importDefault) || function (mod) {
36
- return (mod && mod.__esModule) ? mod : { "default": mod };
37
- };
38
- Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.TransformSchema = void 0;
40
- const typescript_1 = __importDefault(require("typescript"));
41
- const fs = __importStar(require("fs"));
42
- const path_1 = __importDefault(require("path"));
43
- const ast_1 = require("../tsc/ast");
44
- const const_1 = require("../core/const");
45
- function traverseClass(fileContents, sourceFile, node, transformSchema) {
46
- const ci = getTransformClassInfo(fileContents, sourceFile, node, transformSchema);
47
- if (!ci) {
48
- return;
49
- }
50
- let klassContents = `${ci.comment}const ${ci.name} = new ${transformSchema(ci.class)}({\n`;
51
- let removeImports = [];
52
- if (ci.implementsSchema) {
53
- removeImports.push("Schema");
54
- }
55
- removeImports.push(ci.class);
56
- let newImports = [transformSchema(ci.class)];
57
- for (let member of node.members) {
58
- const fInfo = getClassElementInfo(fileContents, member, sourceFile);
59
- if (!fInfo) {
60
- return;
61
- }
62
- klassContents += `${fInfo.comment}${fInfo.key}:${fInfo.value},\n`;
63
- if (fInfo.type) {
64
- removeImports.push(fInfo.type);
65
- }
66
- }
67
- klassContents += "\n})";
68
- if (ci.export && ci.default) {
69
- klassContents += `\n export default ${ci.name};`;
70
- }
71
- else if (ci.export) {
72
- klassContents = "export " + klassContents;
73
- }
74
- // console.debug(klassContents);
75
- return {
76
- rawString: klassContents,
77
- removeImports: removeImports,
78
- newImports,
79
- };
80
- }
81
- function getTransformClassInfo(fileContents, sourceFile, node, transformSchema) {
82
- const generic = (0, ast_1.getClassInfo)(fileContents, sourceFile, node);
83
- if (!generic) {
84
- return;
85
- }
86
- let className = generic.name;
87
- if (!className?.endsWith("Schema")) {
88
- className += "Schema";
89
- }
90
- let implementsSchema = generic.implements?.some((v) => v == "Schema");
91
- let classExtends = generic.extends;
92
- // nothing transformed here, so nothing to do here
93
- if (classExtends && classExtends === transformSchema(classExtends)) {
94
- return undefined;
95
- }
96
- if (!className || !node.heritageClauses || !classExtends) {
97
- return undefined;
98
- }
99
- let ci = {
100
- ...generic,
101
- name: className,
102
- class: classExtends,
103
- implementsSchema,
104
- };
105
- return ci;
106
- }
107
- // intentionally doesn't parse decorators since we don't need it
108
- function getClassElementInfo(fileContents, member, sourceFile) {
109
- if (isFieldElement(fileContents, member, sourceFile)) {
110
- return getFieldElementInfo(fileContents, member, sourceFile);
111
- }
112
- if (member.kind === typescript_1.default.SyntaxKind.Constructor) {
113
- return getConstructorElementInfo(fileContents, member, sourceFile);
114
- }
115
- if (member.kind !== typescript_1.default.SyntaxKind.PropertyDeclaration) {
116
- return;
117
- }
118
- // other properties
119
- const property = member;
120
- if (!property.initializer) {
121
- return;
122
- }
123
- const token = property.name;
124
- return {
125
- key: token.escapedText.toString(),
126
- value: property.initializer?.getFullText(sourceFile),
127
- comment: (0, ast_1.getPreText)(fileContents, member, sourceFile),
128
- type: getType(property, sourceFile),
129
- };
130
- }
131
- function getType(property, sourceFile) {
132
- let propertytype = property.type?.getText(sourceFile) || "";
133
- let ends = ["| null", "[]"];
134
- for (const end of ends) {
135
- if (propertytype.endsWith(end)) {
136
- propertytype = propertytype.slice(0, -1 * end.length);
137
- }
138
- }
139
- return propertytype;
140
- }
141
- function getFieldElementInfo(fileContents, member, sourceFile) {
142
- let fieldMap = "";
143
- // need to change to fields: {code: StringType()};
144
- const property = member;
145
- const initializer = property.initializer;
146
- fieldMap += "{";
147
- for (const element of initializer.elements) {
148
- const parsed = parseFieldElement(element, sourceFile, fileContents);
149
- if (parsed === null) {
150
- return;
151
- }
152
- const { callEx, name, nameComment, properties, suffix } = parsed;
153
- let property = "";
154
- const fieldComment = (0, ast_1.getPreText)(fileContents, element, sourceFile).trim();
155
- if (fieldComment) {
156
- property += "\n" + fieldComment + "\n";
157
- }
158
- if (nameComment) {
159
- property += nameComment + "\n";
160
- }
161
- // e.g. UUIDType, StringType etc
162
- let call = callEx.expression.getText(sourceFile);
163
- let fnCall = "";
164
- if (properties.length) {
165
- fnCall = `{${properties.join(",")}}`;
166
- }
167
- property += `${name}:${call}(${fnCall})${suffix || ""},`;
168
- fieldMap += property;
169
- }
170
- fieldMap += "}";
171
- return {
172
- key: "fields",
173
- value: fieldMap,
174
- comment: (0, ast_1.getPreText)(fileContents, member, sourceFile),
175
- type: getType(property, sourceFile),
176
- };
177
- }
178
- function getConstructorElementInfo(fileContents, member, sourceFile) {
179
- const c = member;
180
- //remove {}
181
- let fullText = c.body?.getFullText(sourceFile) || "";
182
- fullText = fullText.trim().slice(1, -1).trim();
183
- // convert something like
184
- /*
185
- constructor() {
186
- super();
187
- this.addPatterns(
188
- new Feedback(),
189
- new DayOfWeek(),
190
- new Feedback(),
191
- new DayOfWeek(),
192
- );
193
- }
194
- */
195
- // into this.addPatterns(new Feedback(),new DayOfWeek(),new Feedback(),new DayOfWeek(),)
196
- const lines = fullText
197
- .split("\n")
198
- .map((line) => line.trim())
199
- .join("")
200
- .split(";")
201
- .filter((f) => f != "super()" && f != "");
202
- // at this point there should be only line for what we handle
203
- if (lines.length != 1) {
204
- return;
205
- }
206
- const line = lines[0];
207
- const addPatterns = "this.addPatterns(";
208
- if (!line.startsWith(addPatterns)) {
209
- return;
210
- }
211
- return {
212
- key: "patterns",
213
- // remove this.addPatterns at the front, remove trailing ) at the end
214
- // if there's a trailing comma, it'll be handled by the formatter
215
- value: `[${line.slice(addPatterns.length, -1)}]`,
216
- comment: "",
217
- };
218
- }
219
- function isFieldElement(fileContents, member, sourceFile) {
220
- if (member.kind !== typescript_1.default.SyntaxKind.PropertyDeclaration) {
221
- return false;
222
- }
223
- const property = member;
224
- const token = property.name;
225
- if (token.escapedText !== "fields") {
226
- return false;
227
- }
228
- const propertytype = property.type?.getText(sourceFile);
229
- if (propertytype !== "Field[]") {
230
- return false;
231
- }
232
- if (property.initializer?.kind !== typescript_1.default.SyntaxKind.ArrayLiteralExpression) {
233
- throwErr(fileContents, member, "invalid array type");
234
- return false;
235
- }
236
- return true;
237
- }
238
- // if there's an error transforming any of the schemas, we should stop...
239
- function throwErr(fileContents, node, error) {
240
- console.error(error);
241
- throw new Error(`error transforming this field ${fileContents.substring(node.getFullStart(), node.getEnd())}`);
242
- }
243
- function parseFieldElement(element, sourceFile, fileContents, nested) {
244
- if (element.kind !== typescript_1.default.SyntaxKind.CallExpression &&
245
- element.kind !== typescript_1.default.SyntaxKind.PropertyAccessExpression) {
246
- throwErr(fileContents, element, `skipped unknown (non-call|non-property) expression ${element.kind}`);
247
- return null;
248
- }
249
- if (element.kind === typescript_1.default.SyntaxKind.PropertyAccessExpression) {
250
- const ret = parseFieldElement(element.expression, sourceFile, fileContents, true);
251
- if (ret !== null) {
252
- if (!nested) {
253
- ret.suffix = fileContents.substring(ret.callEx.getEnd(), element.getEnd());
254
- }
255
- return ret;
256
- }
257
- }
258
- let callEx = element;
259
- if (callEx.arguments.length !== 1) {
260
- // have a situation like: StringType({ name: "canonicalName" }).trim().toLowerCase(),
261
- // 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
262
- if (callEx.expression.kind === typescript_1.default.SyntaxKind.PropertyAccessExpression) {
263
- const ret = parseFieldElement(callEx.expression.expression, sourceFile, fileContents, true);
264
- if (ret !== null) {
265
- if (!nested) {
266
- ret.suffix = fileContents.substring(ret.callEx.getEnd(), callEx.getEnd());
267
- }
268
- return ret;
269
- }
270
- }
271
- throwErr(fileContents, element, "callExpression with arguments not of length 1");
272
- }
273
- let arg = callEx.arguments[0];
274
- if (arg.kind !== typescript_1.default.SyntaxKind.ObjectLiteralExpression) {
275
- // this and the check above for PropertyAccessExpression are to handle things like
276
- // FooType({
277
- /// ...
278
- // }).function(blah)
279
- const ret = parseFieldElement(callEx.expression, sourceFile, fileContents, true);
280
- if (ret !== null) {
281
- if (!nested) {
282
- ret.suffix = fileContents.substring(ret.callEx.getEnd(), callEx.getEnd());
283
- }
284
- return ret;
285
- }
286
- throwErr(fileContents, element, `not objectLiteralExpression. kind ${arg.kind}`);
287
- return null;
288
- }
289
- let expr = arg;
290
- let name = "";
291
- let propertyComment;
292
- let properties = [];
293
- for (const p of expr.properties) {
294
- const p2 = p;
295
- // found name property
296
- if (p2.name.escapedText === "name") {
297
- name = p2.initializer.getText(sourceFile);
298
- // check for any comment associated with name: "fooo"
299
- propertyComment = (0, ast_1.getPreText)(fileContents, p, sourceFile).trim();
300
- }
301
- else {
302
- properties.push(p.getFullText(sourceFile));
303
- }
304
- }
305
- if (!name) {
306
- throwErr(fileContents, element, `couldn't find name property`);
307
- return null;
308
- }
309
- // remove quotes
310
- name = name.slice(1, -1);
311
- return {
312
- callEx,
313
- name,
314
- properties,
315
- nameComment: propertyComment,
316
- };
317
- }
318
- // find which of these importPaths is being used and use that to replace
319
- function findSchemaImportPath(sourceFile) {
320
- const paths = {
321
- [const_1.PACKAGE]: true,
322
- [const_1.SCHEMA_PATH]: true,
323
- [`${const_1.SCHEMA_PATH}/`]: true,
324
- };
325
- // @ts-ignore
326
- const importStatements = sourceFile.statements.filter((stmt) => typescript_1.default.isImportDeclaration(stmt));
327
- for (const imp of importStatements) {
328
- const impInfo = (0, ast_1.getImportInfo)(imp, sourceFile);
329
- if (!impInfo) {
330
- continue;
331
- }
332
- if (paths[impInfo.importPath] !== undefined) {
333
- return impInfo.importPath;
334
- }
335
- }
336
- }
337
- class TransformSchema {
338
- // we only end up doing this once because we change the schema representation
339
- // so safe to run this multiple times
340
- constructor(relativeImports, oldBaseClass, newSchemaClass, transformPath) {
341
- this.relativeImports = relativeImports;
342
- this.oldBaseClass = oldBaseClass;
343
- this.newSchemaClass = newSchemaClass;
344
- this.transformPath = transformPath;
345
- this.glob = "src/schema/*.ts";
346
- this.formatGlob = "src/schema/*.ts";
347
- }
348
- transformSchema(className) {
349
- if (className === "BaseEntSchema" || className === "BaseEntSchemaWithTZ") {
350
- return className.substring(4);
351
- }
352
- if (className === this.oldBaseClass && this.newSchemaClass) {
353
- return this.newSchemaClass;
354
- }
355
- return className;
356
- }
357
- traverseChild(sourceFile, contents, file, node) {
358
- if (!typescript_1.default.isClassDeclaration(node)) {
359
- return { node };
360
- }
361
- // TODO address implicit schema doesn't work here...
362
- const ret = traverseClass(contents, sourceFile, node, this.transformSchema.bind(this));
363
- if (ret === undefined) {
364
- return;
365
- }
366
- let imports = new Map();
367
- const imp = findSchemaImportPath(sourceFile);
368
- if (imp) {
369
- if (this.transformPath) {
370
- imports.set(imp, []);
371
- }
372
- else {
373
- imports.set(imp, ret.newImports);
374
- }
375
- }
376
- if (this.transformPath) {
377
- // add new imports to this path
378
- imports.set((0, ast_1.transformRelative)(file, this.transformPath, this.relativeImports), ret.newImports);
379
- }
380
- return {
381
- traversed: true,
382
- rawString: ret.rawString,
383
- removeImports: ret.removeImports,
384
- imports: imports,
385
- };
386
- }
387
- fileToWrite(file) {
388
- return "src/schema/" + path_1.default.basename(file).slice(0, -3) + "_schema.ts";
389
- }
390
- postProcess(file) {
391
- fs.rmSync(file);
392
- }
393
- }
394
- exports.TransformSchema = TransformSchema;