@snowtop/ent 0.1.0-alpha9 → 0.1.0-alpha91

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 (135) hide show
  1. package/action/action.d.ts +36 -31
  2. package/action/action.js +2 -6
  3. package/action/executor.d.ts +3 -3
  4. package/action/executor.js +2 -2
  5. package/action/experimental_action.d.ts +29 -22
  6. package/action/experimental_action.js +29 -6
  7. package/action/orchestrator.d.ts +38 -16
  8. package/action/orchestrator.js +223 -61
  9. package/action/privacy.d.ts +2 -2
  10. package/core/base.d.ts +45 -24
  11. package/core/base.js +7 -1
  12. package/core/clause.d.ts +83 -7
  13. package/core/clause.js +334 -63
  14. package/core/config.d.ts +8 -0
  15. package/core/config.js +5 -1
  16. package/core/context.d.ts +5 -3
  17. package/core/context.js +20 -2
  18. package/core/convert.d.ts +1 -1
  19. package/core/db.d.ts +2 -2
  20. package/core/db.js +6 -2
  21. package/core/ent.d.ts +79 -24
  22. package/core/ent.js +527 -176
  23. package/core/loaders/assoc_count_loader.d.ts +3 -2
  24. package/core/loaders/assoc_count_loader.js +14 -2
  25. package/core/loaders/assoc_edge_loader.d.ts +2 -2
  26. package/core/loaders/assoc_edge_loader.js +5 -1
  27. package/core/loaders/index.d.ts +1 -1
  28. package/core/loaders/index.js +1 -3
  29. package/core/loaders/index_loader.d.ts +2 -2
  30. package/core/loaders/loader.js +5 -5
  31. package/core/loaders/object_loader.d.ts +6 -5
  32. package/core/loaders/object_loader.js +67 -59
  33. package/core/loaders/query_loader.d.ts +6 -12
  34. package/core/loaders/query_loader.js +52 -11
  35. package/core/loaders/raw_count_loader.d.ts +2 -2
  36. package/core/loaders/raw_count_loader.js +5 -1
  37. package/core/logger.d.ts +1 -1
  38. package/core/logger.js +1 -0
  39. package/core/privacy.d.ts +26 -25
  40. package/core/privacy.js +21 -25
  41. package/core/query/assoc_query.d.ts +7 -6
  42. package/core/query/assoc_query.js +9 -1
  43. package/core/query/custom_clause_query.d.ts +26 -0
  44. package/core/query/custom_clause_query.js +78 -0
  45. package/core/query/custom_query.d.ts +20 -5
  46. package/core/query/custom_query.js +87 -12
  47. package/core/query/index.d.ts +1 -0
  48. package/core/query/index.js +3 -1
  49. package/core/query/query.d.ts +8 -4
  50. package/core/query/query.js +101 -53
  51. package/core/query/shared_assoc_test.d.ts +2 -1
  52. package/core/query/shared_assoc_test.js +34 -43
  53. package/core/query/shared_test.d.ts +8 -1
  54. package/core/query/shared_test.js +470 -236
  55. package/core/viewer.d.ts +3 -3
  56. package/core/viewer.js +1 -1
  57. package/graphql/graphql.js +16 -6
  58. package/graphql/query/edge_connection.d.ts +9 -9
  59. package/graphql/query/page_info.d.ts +1 -1
  60. package/graphql/query/shared_edge_connection.js +1 -15
  61. package/imports/index.js +5 -1
  62. package/index.d.ts +11 -5
  63. package/index.js +20 -7
  64. package/package.json +1 -1
  65. package/parse_schema/parse.d.ts +12 -3
  66. package/parse_schema/parse.js +70 -11
  67. package/schema/base_schema.js +3 -0
  68. package/schema/field.d.ts +44 -8
  69. package/schema/field.js +136 -10
  70. package/schema/index.d.ts +2 -2
  71. package/schema/index.js +5 -1
  72. package/schema/json_field.d.ts +13 -1
  73. package/schema/json_field.js +28 -1
  74. package/schema/schema.d.ts +66 -11
  75. package/schema/schema.js +18 -4
  76. package/schema/struct_field.d.ts +11 -1
  77. package/schema/struct_field.js +44 -5
  78. package/scripts/custom_compiler.js +10 -6
  79. package/scripts/custom_graphql.js +13 -4
  80. package/scripts/{transform_schema.d.ts → migrate_v0.1.d.ts} +0 -0
  81. package/scripts/migrate_v0.1.js +36 -0
  82. package/scripts/read_schema.js +20 -5
  83. package/testutils/builder.d.ts +31 -21
  84. package/testutils/builder.js +83 -29
  85. package/testutils/db/fixture.d.ts +10 -0
  86. package/testutils/db/fixture.js +26 -0
  87. package/testutils/db/{test_db.d.ts → temp_db.d.ts} +20 -7
  88. package/testutils/db/{test_db.js → temp_db.js} +102 -36
  89. package/testutils/db/value.d.ts +6 -0
  90. package/testutils/db/value.js +251 -0
  91. package/testutils/db_mock.js +3 -1
  92. package/testutils/db_time_zone.d.ts +4 -0
  93. package/testutils/db_time_zone.js +41 -0
  94. package/testutils/ent-graphql-tests/index.js +8 -1
  95. package/testutils/fake_data/const.d.ts +2 -1
  96. package/testutils/fake_data/const.js +3 -0
  97. package/testutils/fake_data/fake_contact.d.ts +7 -4
  98. package/testutils/fake_data/fake_contact.js +14 -6
  99. package/testutils/fake_data/fake_event.d.ts +5 -3
  100. package/testutils/fake_data/fake_event.js +8 -5
  101. package/testutils/fake_data/fake_tag.d.ts +35 -0
  102. package/testutils/fake_data/fake_tag.js +88 -0
  103. package/testutils/fake_data/fake_user.d.ts +6 -4
  104. package/testutils/fake_data/fake_user.js +16 -13
  105. package/testutils/fake_data/index.js +5 -1
  106. package/testutils/fake_data/internal.d.ts +2 -0
  107. package/testutils/fake_data/internal.js +7 -1
  108. package/testutils/fake_data/tag_query.d.ts +13 -0
  109. package/testutils/fake_data/tag_query.js +43 -0
  110. package/testutils/fake_data/test_helpers.d.ts +11 -4
  111. package/testutils/fake_data/test_helpers.js +28 -12
  112. package/testutils/fake_data/user_query.d.ts +13 -6
  113. package/testutils/fake_data/user_query.js +54 -22
  114. package/testutils/fake_log.d.ts +3 -3
  115. package/testutils/parse_sql.d.ts +6 -0
  116. package/testutils/parse_sql.js +16 -2
  117. package/testutils/test_edge_global_schema.d.ts +15 -0
  118. package/testutils/test_edge_global_schema.js +62 -0
  119. package/testutils/write.d.ts +2 -2
  120. package/testutils/write.js +33 -7
  121. package/tsc/ast.d.ts +44 -0
  122. package/tsc/ast.js +271 -0
  123. package/tsc/compilerOptions.d.ts +6 -0
  124. package/tsc/compilerOptions.js +45 -2
  125. package/tsc/move_generated.d.ts +1 -0
  126. package/tsc/move_generated.js +164 -0
  127. package/tsc/transform.d.ts +21 -0
  128. package/tsc/transform.js +171 -0
  129. package/tsc/transform_action.d.ts +22 -0
  130. package/tsc/transform_action.js +183 -0
  131. package/tsc/transform_ent.d.ts +17 -0
  132. package/tsc/transform_ent.js +59 -0
  133. package/tsc/transform_schema.d.ts +27 -0
  134. package/tsc/transform_schema.js +383 -0
  135. package/scripts/transform_schema.js +0 -445
package/tsc/ast.js ADDED
@@ -0,0 +1,271 @@
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 (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.getCustomInfo = exports.transformRelative = exports.getImportInfo = exports.isSrcGeneratedImport = exports.isRelativeGeneratedImport = exports.isRelativeImport = exports.updateImportPath = exports.transformImport = exports.getClassInfo = exports.getPreText = void 0;
30
+ const typescript_1 = __importDefault(require("typescript"));
31
+ const path = __importStar(require("path"));
32
+ const js_yaml_1 = require("js-yaml");
33
+ const fs = __importStar(require("fs"));
34
+ function getPreText(fileContents, node, sourceFile) {
35
+ return fileContents.substring(node.getFullStart(), node.getStart(sourceFile));
36
+ }
37
+ exports.getPreText = getPreText;
38
+ function getClassInfo(fileContents, sourceFile, node) {
39
+ let className = node.name?.text;
40
+ let classExtends;
41
+ let impl = [];
42
+ if (node.heritageClauses) {
43
+ for (const hc of node.heritageClauses) {
44
+ switch (hc.token) {
45
+ case typescript_1.default.SyntaxKind.ImplementsKeyword:
46
+ for (const type of hc.types) {
47
+ impl.push(type.expression.getText(sourceFile));
48
+ }
49
+ break;
50
+ case typescript_1.default.SyntaxKind.ExtendsKeyword:
51
+ // can only extend one class
52
+ for (const type of hc.types) {
53
+ const text = type.expression.getText(sourceFile);
54
+ classExtends = text;
55
+ }
56
+ break;
57
+ }
58
+ }
59
+ }
60
+ if (!className) {
61
+ return undefined;
62
+ }
63
+ let hasExport = false;
64
+ let hasDefault = false;
65
+ let comment = getPreText(fileContents, node, sourceFile);
66
+ const wrapClassContents = (inner) => {
67
+ let ret = `${comment}`;
68
+ if (hasExport) {
69
+ ret += "export ";
70
+ }
71
+ if (hasDefault) {
72
+ ret += "default ";
73
+ }
74
+ ret += `class ${className} `;
75
+ if (classExtends) {
76
+ ret += `extends ${classExtends} `;
77
+ }
78
+ if (impl.length) {
79
+ ret += `implements ${impl.join(", ")}`;
80
+ }
81
+ return `${ret}{
82
+ ${inner}
83
+ }`;
84
+ };
85
+ if (node.modifiers) {
86
+ for (const mod of node.modifiers) {
87
+ const text = mod.getText(sourceFile);
88
+ if (text === "export") {
89
+ hasExport = true;
90
+ }
91
+ else if (text === "default") {
92
+ hasDefault = true;
93
+ }
94
+ }
95
+ }
96
+ return {
97
+ name: className,
98
+ extends: classExtends,
99
+ comment,
100
+ implements: impl,
101
+ wrapClassContents,
102
+ export: hasExport,
103
+ default: hasDefault,
104
+ };
105
+ }
106
+ exports.getClassInfo = getClassInfo;
107
+ function transformImport(fileContents, importNode, sourceFile, opts) {
108
+ // remove quotes too
109
+ const text = importNode.moduleSpecifier.getText(sourceFile).slice(1, -1);
110
+ if (text != opts.transformPath) {
111
+ return;
112
+ }
113
+ const impInfo = getImportInfo(importNode, sourceFile);
114
+ if (!impInfo) {
115
+ return;
116
+ }
117
+ let { imports, start, end, importText, default: def } = impInfo;
118
+ let removeImportsMap = {};
119
+ if (opts?.removeImports) {
120
+ opts.removeImports.forEach((imp) => {
121
+ removeImportsMap[imp] = true;
122
+ if (def === imp) {
123
+ def = "";
124
+ }
125
+ });
126
+ }
127
+ let finalImports = new Set();
128
+ for (let i = 0; i < imports.length; i++) {
129
+ let imp = imports[i].trim();
130
+ if (imp === "") {
131
+ continue;
132
+ }
133
+ if (opts?.transform) {
134
+ imp = opts.transform(imp);
135
+ }
136
+ if (removeImportsMap[imp]) {
137
+ continue;
138
+ }
139
+ finalImports.add(imp);
140
+ }
141
+ if (opts?.newImports) {
142
+ opts.newImports.forEach((imp) => finalImports.add(imp));
143
+ }
144
+ const comment = getPreText(fileContents, importNode, sourceFile);
145
+ return (comment +
146
+ "import " +
147
+ // add default
148
+ (def || "") +
149
+ // should probably always be "{" now that we support default
150
+ (start >= 0 ? importText.substring(0, start + 1) : "{") +
151
+ Array.from(finalImports).join(", ") +
152
+ // should probably always be "}"
153
+ (end >= 0 ? importText.substring(end) : "}") +
154
+ ' from "' +
155
+ text +
156
+ '";');
157
+ }
158
+ exports.transformImport = transformImport;
159
+ function updateImportPath(fileContents, importNode, sourceFile, newPath) {
160
+ const comment = getPreText(fileContents, importNode, sourceFile);
161
+ // all this copied from above...
162
+ const importText = importNode.importClause?.getText(sourceFile) || "";
163
+ const start = importText.indexOf("{");
164
+ const end = importText.lastIndexOf("}");
165
+ if (start === -1 || end === -1) {
166
+ return;
167
+ }
168
+ const imports = importText
169
+ .substring(start + 1, end)
170
+ // .trim()
171
+ .split(",");
172
+ return (comment +
173
+ "import " +
174
+ importText.substring(0, start + 1) +
175
+ Array.from(imports).join(", ") +
176
+ importText.substring(end) +
177
+ ' from "' +
178
+ newPath +
179
+ '";');
180
+ }
181
+ exports.updateImportPath = updateImportPath;
182
+ function isRelativeImport(node, sourceFile) {
183
+ const text = node.moduleSpecifier.getText(sourceFile).slice(1, -1);
184
+ return text.startsWith("..") || text.startsWith("./");
185
+ }
186
+ exports.isRelativeImport = isRelativeImport;
187
+ function isRelativeGeneratedImport(node, sourceFile) {
188
+ const text = node.moduleSpecifier.getText(sourceFile).slice(1, -1);
189
+ return ((text.startsWith("..") || text.startsWith("./")) &&
190
+ text.indexOf("/generated") !== -1);
191
+ }
192
+ exports.isRelativeGeneratedImport = isRelativeGeneratedImport;
193
+ function isSrcGeneratedImport(node, sourceFile) {
194
+ const text = node.moduleSpecifier.getText(sourceFile).slice(1, -1);
195
+ return text.startsWith("src") && text.includes("/generated");
196
+ }
197
+ exports.isSrcGeneratedImport = isSrcGeneratedImport;
198
+ // TODO doesn't support default + {} yet
199
+ function getImportInfo(imp, sourceFile) {
200
+ const importText = imp.importClause?.getText(sourceFile) || "";
201
+ const start = importText.indexOf("{");
202
+ const end = importText.lastIndexOf("}");
203
+ const text = imp.moduleSpecifier.getText(sourceFile).slice(1, -1);
204
+ if ((start === -1 || end === -1) && !importText.length) {
205
+ return;
206
+ }
207
+ let imports = [];
208
+ let def;
209
+ if (start !== -1 && end !== -1) {
210
+ imports = importText
211
+ .substring(start + 1, end)
212
+ //.trim()
213
+ .split(",");
214
+ }
215
+ else {
216
+ def = importText;
217
+ }
218
+ return {
219
+ importPath: text,
220
+ importText,
221
+ start,
222
+ end,
223
+ imports,
224
+ default: def,
225
+ };
226
+ }
227
+ exports.getImportInfo = getImportInfo;
228
+ function transformRelative(file, importPath, relative) {
229
+ if (!relative || !importPath.startsWith("src")) {
230
+ return importPath;
231
+ }
232
+ const fileFullPath = path.join(process.cwd(), file);
233
+ const impFullPath = path.join(process.cwd(), importPath);
234
+ // relative path is from directory
235
+ return normalizePath(path.relative(path.dirname(fileFullPath), impFullPath));
236
+ }
237
+ exports.transformRelative = transformRelative;
238
+ function normalizePath(p) {
239
+ if (p.endsWith("..")) {
240
+ return p + "/";
241
+ }
242
+ return p;
243
+ }
244
+ // also used in parse schema logic
245
+ function getCustomInfo() {
246
+ let yaml = {};
247
+ let relativeImports = false;
248
+ try {
249
+ yaml = (0, js_yaml_1.load)(fs.readFileSync(path.join(process.cwd(), "ent.yml"), {
250
+ encoding: "utf8",
251
+ }));
252
+ relativeImports = yaml?.codegen?.relativeImports || false;
253
+ if (yaml?.codegen?.templatizedViewer) {
254
+ return {
255
+ viewerInfo: yaml.codegen.templatizedViewer,
256
+ relativeImports,
257
+ globalSchemaPath: yaml.globalSchemaPath,
258
+ };
259
+ }
260
+ }
261
+ catch (e) { }
262
+ return {
263
+ viewerInfo: {
264
+ path: "@snowtop/ent",
265
+ name: "Viewer",
266
+ },
267
+ relativeImports,
268
+ globalSchemaPath: yaml.globalSchemaPath,
269
+ };
270
+ }
271
+ exports.getCustomInfo = getCustomInfo;
@@ -1,2 +1,8 @@
1
1
  import ts from "typescript";
2
2
  export declare function readCompilerOptions(filePath: string): ts.CompilerOptions;
3
+ export declare function getTarget(target?: string): ts.ScriptTarget;
4
+ export declare function getTargetFromCurrentDir(): ts.ScriptTarget;
5
+ export declare function createSourceFile(target: ts.ScriptTarget, file: string): {
6
+ contents: string;
7
+ sourceFile: ts.SourceFile;
8
+ };
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[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);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -22,7 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
22
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
23
27
  };
24
28
  Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.readCompilerOptions = void 0;
29
+ exports.createSourceFile = exports.getTargetFromCurrentDir = exports.getTarget = exports.readCompilerOptions = void 0;
26
30
  const fs = __importStar(require("fs"));
27
31
  const json5_1 = __importDefault(require("json5"));
28
32
  const typescript_1 = __importDefault(require("typescript"));
@@ -59,3 +63,42 @@ function readCompilerOptions(filePath) {
59
63
  return options;
60
64
  }
61
65
  exports.readCompilerOptions = readCompilerOptions;
66
+ function getTarget(target) {
67
+ switch (target?.toLowerCase()) {
68
+ case "es2015":
69
+ return typescript_1.default.ScriptTarget.ES2015;
70
+ case "es2016":
71
+ return typescript_1.default.ScriptTarget.ES2016;
72
+ case "es2017":
73
+ return typescript_1.default.ScriptTarget.ES2017;
74
+ case "es2018":
75
+ return typescript_1.default.ScriptTarget.ES2018;
76
+ case "es2019":
77
+ return typescript_1.default.ScriptTarget.ES2019;
78
+ case "es2020":
79
+ return typescript_1.default.ScriptTarget.ES2020;
80
+ case "es2021":
81
+ return typescript_1.default.ScriptTarget.ES2021;
82
+ case "es3":
83
+ return typescript_1.default.ScriptTarget.ES3;
84
+ case "es5":
85
+ return typescript_1.default.ScriptTarget.ES5;
86
+ case "esnext":
87
+ return typescript_1.default.ScriptTarget.ESNext;
88
+ default:
89
+ return typescript_1.default.ScriptTarget.ESNext;
90
+ }
91
+ }
92
+ exports.getTarget = getTarget;
93
+ function getTargetFromCurrentDir() {
94
+ const options = readCompilerOptions(".");
95
+ return getTarget(options.target?.toString());
96
+ }
97
+ exports.getTargetFromCurrentDir = getTargetFromCurrentDir;
98
+ function createSourceFile(target, file) {
99
+ let contents = fs.readFileSync(file).toString();
100
+ // go through the file and print everything back if not starting immediately after other position
101
+ const sourceFile = typescript_1.default.createSourceFile(file, contents, target, false, typescript_1.default.ScriptKind.TS);
102
+ return { contents, sourceFile };
103
+ }
104
+ exports.createSourceFile = createSourceFile;
@@ -0,0 +1 @@
1
+ export declare function moveGenerated(relativeImports: boolean): void;
@@ -0,0 +1,164 @@
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 (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.moveGenerated = void 0;
30
+ const glob_1 = require("glob");
31
+ const path = __importStar(require("path"));
32
+ const fs = __importStar(require("fs"));
33
+ const typescript_1 = __importDefault(require("typescript"));
34
+ const ast_1 = require("./ast");
35
+ const transform_1 = require("./transform");
36
+ class MoveFiles {
37
+ constructor(globPath) {
38
+ this.globPath = globPath;
39
+ }
40
+ move() {
41
+ const files = glob_1.glob.sync(this.globPath);
42
+ moveFiles(files);
43
+ }
44
+ }
45
+ class TransformImports {
46
+ constructor(glob, prettierGlob, relativeImports, checkRelativeImportsValid) {
47
+ this.glob = glob;
48
+ this.prettierGlob = prettierGlob;
49
+ this.relativeImports = relativeImports;
50
+ this.checkRelativeImportsValid = checkRelativeImportsValid;
51
+ this.globOptions = {
52
+ ignore: ["node_modules/**"],
53
+ };
54
+ this.cwd = process.cwd();
55
+ }
56
+ traverseChild(sourceFile, contents, file, node) {
57
+ if (!typescript_1.default.isImportDeclaration(node)) {
58
+ return { node };
59
+ }
60
+ let dirPath = path.join(this.cwd, file, "..");
61
+ const newImportPath = this.getNewImportPath(node, file, sourceFile, dirPath);
62
+ if (!newImportPath) {
63
+ return { node };
64
+ }
65
+ const v = (0, ast_1.updateImportPath)(contents, node, sourceFile, newImportPath);
66
+ return {
67
+ traversed: true,
68
+ rawString: v,
69
+ };
70
+ }
71
+ getNewImportPath(node, file, sourceFile, dirPath) {
72
+ const text = node.moduleSpecifier.getText(sourceFile).slice(1, -1);
73
+ // it's relative and has generated in there, continue
74
+ // do relative imports path regardless of if relative imports is on or not
75
+ if ((0, ast_1.isRelativeGeneratedImport)(node, sourceFile)) {
76
+ const oldPath = path.join(dirPath, text);
77
+ const relFromRoot = path.relative(".", oldPath);
78
+ const conv = transformPath(relFromRoot);
79
+ if (!conv || conv.newFile === relFromRoot) {
80
+ return;
81
+ }
82
+ return path.relative(dirPath, conv.newFile);
83
+ }
84
+ if (this.checkRelativeImportsValid && (0, ast_1.isRelativeImport)(node, sourceFile)) {
85
+ const parts = file.split(path.sep);
86
+ if (parts.length === 5 &&
87
+ parts.slice(0, 3).join(path.sep) ===
88
+ ["src", "graphql", "generated"].join(path.sep)) {
89
+ // we have custom graphql import paths
90
+ // src/graphql/generated/mutations|resolvers/foo_type.ts
91
+ // which used to be
92
+ // src/graphql/mutations|resolvers/generated/foo_type.ts
93
+ // we probably have a broken import.
94
+ // try and fix it...
95
+ let temp = parts[2];
96
+ parts[2] = parts[3];
97
+ parts[3] = temp;
98
+ const oldPath = parts.join(path.sep);
99
+ let oldDir = path.join(this.cwd, oldPath, "..");
100
+ let importPath = path.join(oldDir, text);
101
+ let exists = fs.statSync(importPath + ".ts", { throwIfNoEntry: false });
102
+ if (!exists) {
103
+ // doesn't exist. sadly has to be fixed manually. could theoretically also be a directory but we shouldn't have that
104
+ return;
105
+ }
106
+ return path.relative(dirPath, importPath);
107
+ }
108
+ }
109
+ // if relative imports, we done.
110
+ if (this.relativeImports) {
111
+ return;
112
+ }
113
+ // non relative, only transform src paths with generated
114
+ if ((0, ast_1.isSrcGeneratedImport)(node, sourceFile)) {
115
+ const conv = transformPath(text);
116
+ if (!conv || conv.newFile === text) {
117
+ return;
118
+ }
119
+ return conv.newFile;
120
+ }
121
+ return;
122
+ }
123
+ }
124
+ function transformPath(old) {
125
+ const parts = old.split(path.sep);
126
+ if (parts.length < 3) {
127
+ return;
128
+ }
129
+ const changedParts = parts
130
+ .slice(0, 2)
131
+ .concat("generated")
132
+ .concat(parts.slice(2).filter((v) => v !== "generated"));
133
+ const newFile = changedParts.join(path.sep);
134
+ return { changedParts, newFile };
135
+ }
136
+ function moveFiles(files) {
137
+ files.forEach((file) => {
138
+ const conv = transformPath(file);
139
+ if (!conv) {
140
+ return;
141
+ }
142
+ const { changedParts, newFile } = conv;
143
+ if (file === newFile) {
144
+ return;
145
+ }
146
+ // check if directory exists, if not, create recursive dir
147
+ const p = changedParts.slice(0, changedParts.length - 1).join(path.sep);
148
+ const statInfo = fs.statSync(p, { throwIfNoEntry: false });
149
+ if (!statInfo) {
150
+ fs.mkdirSync(p, {
151
+ recursive: true,
152
+ });
153
+ }
154
+ // move file to new location
155
+ fs.renameSync(file, newFile);
156
+ });
157
+ }
158
+ function moveGenerated(relativeImports) {
159
+ new MoveFiles("src/ent/**/generated/**/**.ts").move();
160
+ new MoveFiles("src/graphql/**/generated/**/**.ts").move();
161
+ (0, transform_1.transform)(new TransformImports("src/ent/**/*.ts", "src/ent/**/*.ts", relativeImports));
162
+ (0, transform_1.transform)(new TransformImports("src/graphql/**/*.ts", "src/graphql/**/*.ts", relativeImports, true));
163
+ }
164
+ exports.moveGenerated = moveGenerated;
@@ -0,0 +1,21 @@
1
+ import { IOptions } from "glob";
2
+ import ts from "typescript";
3
+ interface TraverseChildResponse {
4
+ node?: ts.Node;
5
+ rawString?: string;
6
+ traversed?: boolean;
7
+ imports?: Map<string, string[]>;
8
+ removeImports?: string[];
9
+ }
10
+ export interface TransformFile {
11
+ glob: string;
12
+ globOptions?: IOptions;
13
+ preprocessFile?: (contents: string, file: string, sourceFile: ts.SourceFile) => boolean;
14
+ traverseChild(sourceFile: ts.SourceFile, contents: string, file: string, node: ts.Node): TraverseChildResponse | undefined;
15
+ filter?(files: string[]): string[];
16
+ fileToWrite?(file: string): string;
17
+ postProcess?(file: string): void;
18
+ prettierGlob?: string;
19
+ }
20
+ export declare function transform(transform: TransformFile): void;
21
+ export {};