@snowtop/ent 0.1.0-alpha6 → 0.1.0-alpha63

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