@snowtop/ent 0.1.0-alpha2 → 0.1.0-alpha20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/action/action.d.ts +28 -24
- package/action/executor.d.ts +4 -4
- package/action/executor.js +2 -2
- package/action/experimental_action.d.ts +21 -20
- package/action/experimental_action.js +11 -5
- package/action/orchestrator.d.ts +31 -16
- package/action/orchestrator.js +156 -43
- package/action/privacy.d.ts +2 -2
- package/core/base.d.ts +25 -21
- package/core/base.js +16 -0
- package/core/clause.d.ts +24 -3
- package/core/clause.js +246 -5
- package/core/config.d.ts +23 -0
- package/core/config.js +17 -0
- package/core/context.d.ts +2 -2
- package/core/db.d.ts +3 -3
- package/core/db.js +2 -0
- package/core/ent.d.ts +19 -21
- package/core/ent.js +76 -25
- package/core/loaders/assoc_count_loader.d.ts +2 -2
- package/core/loaders/assoc_edge_loader.d.ts +3 -3
- package/core/loaders/assoc_edge_loader.js +5 -4
- package/core/loaders/index_loader.js +1 -0
- package/core/loaders/object_loader.d.ts +10 -5
- package/core/loaders/object_loader.js +59 -4
- package/core/loaders/query_loader.d.ts +2 -2
- package/core/loaders/raw_count_loader.d.ts +2 -2
- package/core/privacy.d.ts +25 -25
- package/core/privacy.js +3 -0
- package/core/query/assoc_query.d.ts +6 -6
- package/core/query/custom_query.d.ts +5 -5
- package/core/query/query.d.ts +1 -1
- package/core/viewer.d.ts +4 -3
- package/core/viewer.js +4 -0
- package/graphql/builtins/connection.js +3 -3
- package/graphql/builtins/edge.js +2 -2
- package/graphql/builtins/node.js +1 -1
- package/graphql/graphql.d.ts +3 -2
- package/graphql/graphql.js +24 -23
- package/graphql/index.d.ts +1 -0
- package/graphql/index.js +3 -1
- package/graphql/mutations/union.d.ts +2 -0
- package/graphql/mutations/union.js +35 -0
- package/graphql/node_resolver.d.ts +0 -1
- package/graphql/query/connection_type.js +6 -6
- package/graphql/query/edge_connection.d.ts +9 -9
- package/graphql/query/page_info.d.ts +1 -1
- package/graphql/query/page_info.js +4 -4
- package/graphql/query/shared_assoc_test.js +2 -2
- package/graphql/scalars/time.d.ts +1 -1
- package/index.d.ts +16 -1
- package/index.js +18 -5
- package/package.json +3 -3
- package/parse_schema/parse.d.ts +23 -4
- package/parse_schema/parse.js +68 -6
- package/schema/base_schema.d.ts +36 -1
- package/schema/base_schema.js +51 -2
- package/schema/field.d.ts +1 -1
- package/schema/field.js +11 -3
- package/schema/index.d.ts +4 -2
- package/schema/index.js +10 -1
- package/schema/schema.d.ts +64 -2
- package/schema/schema.js +125 -5
- package/schema/struct_field.d.ts +17 -0
- package/schema/struct_field.js +102 -0
- package/schema/union_field.d.ts +23 -0
- package/schema/union_field.js +79 -0
- package/scripts/custom_graphql.js +122 -15
- package/scripts/{transform_schema.d.ts → migrate_v0.1.d.ts} +0 -0
- package/scripts/migrate_v0.1.js +43 -0
- package/scripts/read_schema.js +15 -1
- package/testutils/builder.d.ts +31 -21
- package/testutils/builder.js +98 -13
- package/testutils/context/test_context.d.ts +2 -2
- package/testutils/context/test_context.js +7 -1
- package/testutils/db/test_db.d.ts +2 -1
- package/testutils/db/test_db.js +13 -4
- package/testutils/ent-graphql-tests/index.d.ts +2 -0
- package/testutils/ent-graphql-tests/index.js +26 -17
- package/testutils/fake_data/fake_contact.d.ts +4 -8
- package/testutils/fake_data/fake_contact.js +15 -19
- package/testutils/fake_data/fake_event.d.ts +4 -8
- package/testutils/fake_data/fake_event.js +21 -25
- package/testutils/fake_data/fake_user.d.ts +5 -9
- package/testutils/fake_data/fake_user.js +23 -27
- package/testutils/fake_data/test_helpers.js +1 -1
- package/testutils/fake_data/user_query.d.ts +2 -2
- package/testutils/fake_log.d.ts +3 -3
- package/tsc/ast.d.ts +33 -0
- package/tsc/ast.js +191 -0
- package/tsc/compilerOptions.d.ts +6 -0
- package/tsc/compilerOptions.js +40 -1
- package/tsc/move_generated.d.ts +1 -0
- package/tsc/move_generated.js +175 -0
- package/tsc/transform.d.ts +21 -0
- package/tsc/transform.js +144 -0
- package/tsc/transform_action.d.ts +27 -0
- package/tsc/transform_action.js +204 -0
- package/tsc/transform_ent.d.ts +17 -0
- package/tsc/transform_ent.js +59 -0
- package/tsc/transform_schema.d.ts +21 -0
- package/tsc/transform_schema.js +341 -0
- package/scripts/transform_schema.js +0 -288
|
@@ -11,6 +11,7 @@ const viewer_1 = require("../../core/viewer");
|
|
|
11
11
|
const test_db_1 = require("../db/test_db");
|
|
12
12
|
const loaders_1 = require("../../core/loaders");
|
|
13
13
|
const convert_1 = require("../../core/convert");
|
|
14
|
+
const action_1 = require("../../action");
|
|
14
15
|
class ViewerWithAccessToken extends viewer_1.IDViewer {
|
|
15
16
|
constructor(viewerID, opts) {
|
|
16
17
|
super(viewerID, opts);
|
|
@@ -26,7 +27,18 @@ class FakeUser {
|
|
|
26
27
|
constructor(viewer, data) {
|
|
27
28
|
this.viewer = viewer;
|
|
28
29
|
this.nodeType = const_1.NodeType.FakeUser;
|
|
29
|
-
this.
|
|
30
|
+
this.data = data;
|
|
31
|
+
this.id = data.id;
|
|
32
|
+
this.createdAt = (0, convert_1.convertDate)(data.created_at);
|
|
33
|
+
this.updatedAt = (0, convert_1.convertDate)(data.updated_at);
|
|
34
|
+
this.firstName = data.first_name;
|
|
35
|
+
this.lastName = data.last_name;
|
|
36
|
+
this.emailAddress = data.email_address;
|
|
37
|
+
this.phoneNumber = data.phone_number;
|
|
38
|
+
this.password = data.password;
|
|
39
|
+
}
|
|
40
|
+
getPrivacyPolicy() {
|
|
41
|
+
return {
|
|
30
42
|
rules: [
|
|
31
43
|
privacy_1.AllowIfViewerRule,
|
|
32
44
|
//can view user if friends
|
|
@@ -48,15 +60,6 @@ class FakeUser {
|
|
|
48
60
|
privacy_1.AlwaysDenyRule,
|
|
49
61
|
],
|
|
50
62
|
};
|
|
51
|
-
this.data = data;
|
|
52
|
-
this.id = data.id;
|
|
53
|
-
this.createdAt = (0, convert_1.convertDate)(data.created_at);
|
|
54
|
-
this.updatedAt = (0, convert_1.convertDate)(data.updated_at);
|
|
55
|
-
this.firstName = data.first_name;
|
|
56
|
-
this.lastName = data.last_name;
|
|
57
|
-
this.emailAddress = data.email_address;
|
|
58
|
-
this.phoneNumber = data.phone_number;
|
|
59
|
-
this.password = data.password;
|
|
60
63
|
}
|
|
61
64
|
static getFields() {
|
|
62
65
|
return [
|
|
@@ -89,22 +92,15 @@ class FakeUser {
|
|
|
89
92
|
}
|
|
90
93
|
}
|
|
91
94
|
exports.FakeUser = FakeUser;
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
password: (0, schema_1.StringType)({
|
|
102
|
-
nullable: true,
|
|
103
|
-
}),
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
exports.FakeUserSchema = FakeUserSchema;
|
|
95
|
+
exports.FakeUserSchema = (0, builder_1.getBuilderSchemaFromFields)({
|
|
96
|
+
firstName: (0, schema_1.StringType)(),
|
|
97
|
+
lastName: (0, schema_1.StringType)(),
|
|
98
|
+
emailAddress: (0, schema_1.StringType)(),
|
|
99
|
+
phoneNumber: (0, schema_1.StringType)(),
|
|
100
|
+
password: (0, schema_1.StringType)({
|
|
101
|
+
nullable: true,
|
|
102
|
+
}),
|
|
103
|
+
}, FakeUser);
|
|
108
104
|
function getUserBuilder(viewer, input) {
|
|
109
105
|
const action = getUserAction(viewer, input);
|
|
110
106
|
return action.builder;
|
|
@@ -115,7 +111,7 @@ function getUserAction(viewer, input) {
|
|
|
115
111
|
for (const key in input) {
|
|
116
112
|
m.set(key, input[key]);
|
|
117
113
|
}
|
|
118
|
-
const action = new builder_1.SimpleAction(viewer,
|
|
114
|
+
const action = new builder_1.SimpleAction(viewer, exports.FakeUserSchema, m, action_1.WriteOperation.Insert, null);
|
|
119
115
|
action.viewerForEntLoad = (data) => {
|
|
120
116
|
// load the created ent using a VC of the newly created user.
|
|
121
117
|
return new viewer_1.IDViewer(data.id);
|
|
@@ -107,7 +107,7 @@ async function createUserPlusFriendRequests(input, slice) {
|
|
|
107
107
|
return createTestUser(input);
|
|
108
108
|
}));
|
|
109
109
|
expect(friendRequests.length).toBe(userInputs.length);
|
|
110
|
-
await addEdge(user,
|
|
110
|
+
await addEdge(user, _1.FakeUserSchema, _1.EdgeType.UserToFriendRequests, true, ...friendRequests);
|
|
111
111
|
return [user, friendRequests];
|
|
112
112
|
}
|
|
113
113
|
exports.createUserPlusFriendRequests = createUserPlusFriendRequests;
|
|
@@ -53,7 +53,7 @@ export declare class UserToFriendRequestsQuery extends AssocEdgeQueryBase<FakeUs
|
|
|
53
53
|
}
|
|
54
54
|
export declare class UserToIncomingFriendRequestsQuery extends AssocEdgeQueryBase<FakeUser, FakeUser, AssocEdge> {
|
|
55
55
|
constructor(viewer: Viewer, src: EdgeQuerySource<FakeUser, FakeUser>);
|
|
56
|
-
getPrivacyPolicy(): import("../../core/base").PrivacyPolicy<Ent
|
|
56
|
+
getPrivacyPolicy(): import("../../core/base").PrivacyPolicy<Ent<Viewer<Ent<any> | null, ID | null>>, Viewer<Ent<any> | null, ID | null>>;
|
|
57
57
|
sourceEnt(id: ID): Promise<FakeUser | null>;
|
|
58
58
|
static query(viewer: Viewer, src: EdgeQuerySource<FakeUser, FakeUser>): UserToIncomingFriendRequestsQuery;
|
|
59
59
|
queryContacts(): UserToContactsQuery;
|
|
@@ -90,7 +90,7 @@ export declare class UserToEventsInNextWeekQuery extends CustomEdgeQueryBase<Fak
|
|
|
90
90
|
constructor(viewer: Viewer, src: ID | FakeUser);
|
|
91
91
|
static query(viewer: Viewer, src: FakeUser | ID): UserToEventsInNextWeekQuery;
|
|
92
92
|
sourceEnt(id: ID): Promise<FakeUser | null>;
|
|
93
|
-
getPrivacyPolicy(): import("../../core/base").PrivacyPolicy<Ent
|
|
93
|
+
getPrivacyPolicy(): import("../../core/base").PrivacyPolicy<Ent<Viewer<Ent<any> | null, ID | null>>, Viewer<Ent<any> | null, ID | null>>;
|
|
94
94
|
}
|
|
95
95
|
export declare class UserToFollowingQuery extends AssocEdgeQueryBase<FakeUser, Ent, AssocEdge> {
|
|
96
96
|
constructor(viewer: Viewer, src: EdgeQuerySource<FakeUser, FakeUser>);
|
package/testutils/fake_log.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Ent } from "../core/base";
|
|
1
|
+
import { Ent, Viewer } from "../core/base";
|
|
2
2
|
import { Builder } from "./../action";
|
|
3
3
|
export declare class FakeLogger {
|
|
4
4
|
private static logs;
|
|
@@ -8,6 +8,6 @@ export declare class FakeLogger {
|
|
|
8
8
|
static contains(line: string): boolean;
|
|
9
9
|
static clear(): void;
|
|
10
10
|
}
|
|
11
|
-
export declare class EntCreationObserver<
|
|
12
|
-
observe(builder: Builder<
|
|
11
|
+
export declare class EntCreationObserver<TEnt extends Ent<TViewer>, TViewer extends Viewer = Viewer> {
|
|
12
|
+
observe(builder: Builder<TEnt, TViewer>): Promise<void>;
|
|
13
13
|
}
|
package/tsc/ast.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
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
|
+
}
|
|
32
|
+
export declare function getImportInfo(imp: ts.ImportDeclaration, sourceFile: ts.SourceFile): importInfo | undefined;
|
|
33
|
+
export {};
|
package/tsc/ast.js
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
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.getImportInfo = exports.isSrcGeneratedImport = exports.isRelativeGeneratedImport = exports.isRelativeImport = exports.updateImportPath = exports.transformImport = exports.getClassInfo = exports.getPreText = void 0;
|
|
7
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
8
|
+
function getPreText(fileContents, node, sourceFile) {
|
|
9
|
+
return fileContents.substring(node.getFullStart(), node.getStart(sourceFile));
|
|
10
|
+
}
|
|
11
|
+
exports.getPreText = getPreText;
|
|
12
|
+
function getClassInfo(fileContents, sourceFile, node) {
|
|
13
|
+
let className = node.name?.text;
|
|
14
|
+
let classExtends;
|
|
15
|
+
let impl = [];
|
|
16
|
+
if (node.heritageClauses) {
|
|
17
|
+
for (const hc of node.heritageClauses) {
|
|
18
|
+
switch (hc.token) {
|
|
19
|
+
case typescript_1.default.SyntaxKind.ImplementsKeyword:
|
|
20
|
+
for (const type of hc.types) {
|
|
21
|
+
impl.push(type.expression.getText(sourceFile));
|
|
22
|
+
}
|
|
23
|
+
break;
|
|
24
|
+
case typescript_1.default.SyntaxKind.ExtendsKeyword:
|
|
25
|
+
// can only extend one class
|
|
26
|
+
for (const type of hc.types) {
|
|
27
|
+
const text = type.expression.getText(sourceFile);
|
|
28
|
+
classExtends = text;
|
|
29
|
+
}
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (!className) {
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
let hasExport = false;
|
|
38
|
+
let hasDefault = false;
|
|
39
|
+
let comment = getPreText(fileContents, node, sourceFile);
|
|
40
|
+
const wrapClassContents = (inner) => {
|
|
41
|
+
let ret = `${comment}`;
|
|
42
|
+
if (hasExport) {
|
|
43
|
+
ret += "export ";
|
|
44
|
+
}
|
|
45
|
+
if (hasDefault) {
|
|
46
|
+
ret += "default ";
|
|
47
|
+
}
|
|
48
|
+
ret += `class ${className} `;
|
|
49
|
+
if (classExtends) {
|
|
50
|
+
ret += `extends ${classExtends} `;
|
|
51
|
+
}
|
|
52
|
+
if (impl.length) {
|
|
53
|
+
ret += `implements ${impl.join(", ")}`;
|
|
54
|
+
}
|
|
55
|
+
return `${ret}{
|
|
56
|
+
${inner}
|
|
57
|
+
}`;
|
|
58
|
+
};
|
|
59
|
+
if (node.modifiers) {
|
|
60
|
+
for (const mod of node.modifiers) {
|
|
61
|
+
const text = mod.getText(sourceFile);
|
|
62
|
+
if (text === "export") {
|
|
63
|
+
hasExport = true;
|
|
64
|
+
}
|
|
65
|
+
else if (text === "default") {
|
|
66
|
+
hasDefault = true;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
name: className,
|
|
72
|
+
extends: classExtends,
|
|
73
|
+
comment,
|
|
74
|
+
implements: impl,
|
|
75
|
+
wrapClassContents,
|
|
76
|
+
export: hasExport,
|
|
77
|
+
default: hasDefault,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
exports.getClassInfo = getClassInfo;
|
|
81
|
+
function transformImport(fileContents, importNode, sourceFile, opts) {
|
|
82
|
+
// remove quotes too
|
|
83
|
+
const text = importNode.moduleSpecifier.getText(sourceFile).slice(1, -1);
|
|
84
|
+
if (opts?.transformPath) {
|
|
85
|
+
if (text !== opts.transformPath) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
if (text !== "@snowtop/ent" &&
|
|
91
|
+
text !== "@snowtop/ent/schema" &&
|
|
92
|
+
text !== "@snowtop/ent/schema/") {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const impInfo = getImportInfo(importNode, sourceFile);
|
|
97
|
+
if (!impInfo) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const { imports, start, end, importText } = impInfo;
|
|
101
|
+
let removeImportsMap = {};
|
|
102
|
+
if (opts?.removeImports) {
|
|
103
|
+
opts.removeImports.forEach((imp) => (removeImportsMap[imp] = true));
|
|
104
|
+
}
|
|
105
|
+
let finalImports = new Set();
|
|
106
|
+
for (let i = 0; i < imports.length; i++) {
|
|
107
|
+
let imp = imports[i].trim();
|
|
108
|
+
if (imp === "") {
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
if (opts?.transform) {
|
|
112
|
+
imp = opts.transform(imp);
|
|
113
|
+
}
|
|
114
|
+
if (removeImportsMap[imp]) {
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
finalImports.add(imp);
|
|
118
|
+
}
|
|
119
|
+
if (opts?.newImports) {
|
|
120
|
+
opts.newImports.forEach((imp) => finalImports.add(imp));
|
|
121
|
+
}
|
|
122
|
+
const comment = getPreText(fileContents, importNode, sourceFile);
|
|
123
|
+
return (comment +
|
|
124
|
+
"import " +
|
|
125
|
+
importText.substring(0, start + 1) +
|
|
126
|
+
Array.from(finalImports).join(", ") +
|
|
127
|
+
importText.substring(end) +
|
|
128
|
+
' from "' +
|
|
129
|
+
text +
|
|
130
|
+
'";');
|
|
131
|
+
}
|
|
132
|
+
exports.transformImport = transformImport;
|
|
133
|
+
function updateImportPath(fileContents, importNode, sourceFile, newPath) {
|
|
134
|
+
const comment = getPreText(fileContents, importNode, sourceFile);
|
|
135
|
+
// all this copied from above...
|
|
136
|
+
const importText = importNode.importClause?.getText(sourceFile) || "";
|
|
137
|
+
const start = importText.indexOf("{");
|
|
138
|
+
const end = importText.lastIndexOf("}");
|
|
139
|
+
if (start === -1 || end === -1) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
const imports = importText
|
|
143
|
+
.substring(start + 1, end)
|
|
144
|
+
// .trim()
|
|
145
|
+
.split(",");
|
|
146
|
+
return (comment +
|
|
147
|
+
"import " +
|
|
148
|
+
importText.substring(0, start + 1) +
|
|
149
|
+
Array.from(imports).join(", ") +
|
|
150
|
+
importText.substring(end) +
|
|
151
|
+
' from "' +
|
|
152
|
+
newPath +
|
|
153
|
+
'";');
|
|
154
|
+
}
|
|
155
|
+
exports.updateImportPath = updateImportPath;
|
|
156
|
+
function isRelativeImport(node, sourceFile) {
|
|
157
|
+
const text = node.moduleSpecifier.getText(sourceFile).slice(1, -1);
|
|
158
|
+
return text.startsWith("..") || text.startsWith("./");
|
|
159
|
+
}
|
|
160
|
+
exports.isRelativeImport = isRelativeImport;
|
|
161
|
+
function isRelativeGeneratedImport(node, sourceFile) {
|
|
162
|
+
const text = node.moduleSpecifier.getText(sourceFile).slice(1, -1);
|
|
163
|
+
return ((text.startsWith("..") || text.startsWith("./")) &&
|
|
164
|
+
text.indexOf("/generated") !== -1);
|
|
165
|
+
}
|
|
166
|
+
exports.isRelativeGeneratedImport = isRelativeGeneratedImport;
|
|
167
|
+
function isSrcGeneratedImport(node, sourceFile) {
|
|
168
|
+
const text = node.moduleSpecifier.getText(sourceFile).slice(1, -1);
|
|
169
|
+
return text.startsWith("src") && text.includes("/generated");
|
|
170
|
+
}
|
|
171
|
+
exports.isSrcGeneratedImport = isSrcGeneratedImport;
|
|
172
|
+
function getImportInfo(imp, sourceFile) {
|
|
173
|
+
const importText = imp.importClause?.getText(sourceFile) || "";
|
|
174
|
+
const start = importText.indexOf("{");
|
|
175
|
+
const end = importText.lastIndexOf("}");
|
|
176
|
+
const text = imp.moduleSpecifier.getText(sourceFile).slice(1, -1);
|
|
177
|
+
if (start === -1 || end === -1) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
return {
|
|
181
|
+
importPath: text,
|
|
182
|
+
importText,
|
|
183
|
+
start,
|
|
184
|
+
end,
|
|
185
|
+
imports: importText
|
|
186
|
+
.substring(start + 1, end)
|
|
187
|
+
//.trim()
|
|
188
|
+
.split(","),
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
exports.getImportInfo = getImportInfo;
|
package/tsc/compilerOptions.d.ts
CHANGED
|
@@ -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
|
+
};
|
package/tsc/compilerOptions.js
CHANGED
|
@@ -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(): void;
|
|
@@ -0,0 +1,175 @@
|
|
|
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
|
+
const js_yaml_1 = require("js-yaml");
|
|
33
|
+
class MoveFiles {
|
|
34
|
+
constructor(globPath) {
|
|
35
|
+
this.globPath = globPath;
|
|
36
|
+
}
|
|
37
|
+
move() {
|
|
38
|
+
const files = glob_1.glob.sync(this.globPath);
|
|
39
|
+
moveFiles(files);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
class TransformImports {
|
|
43
|
+
constructor(glob, prettierGlob, checkRelativeImportsValid) {
|
|
44
|
+
this.glob = glob;
|
|
45
|
+
this.prettierGlob = prettierGlob;
|
|
46
|
+
this.checkRelativeImportsValid = checkRelativeImportsValid;
|
|
47
|
+
this.relativeImports = false;
|
|
48
|
+
this.globOptions = {
|
|
49
|
+
ignore: ["node_modules/**"],
|
|
50
|
+
};
|
|
51
|
+
this.cwd = process.cwd();
|
|
52
|
+
this.relativeImports = relativeImports();
|
|
53
|
+
}
|
|
54
|
+
traverseChild(sourceFile, contents, file, node) {
|
|
55
|
+
if (!typescript_1.default.isImportDeclaration(node)) {
|
|
56
|
+
return { node };
|
|
57
|
+
}
|
|
58
|
+
let dirPath = path.join(this.cwd, file, "..");
|
|
59
|
+
const newImportPath = this.getNewImportPath(node, file, sourceFile, dirPath);
|
|
60
|
+
if (!newImportPath) {
|
|
61
|
+
return { node };
|
|
62
|
+
}
|
|
63
|
+
const v = (0, ast_1.updateImportPath)(contents, node, sourceFile, newImportPath);
|
|
64
|
+
return {
|
|
65
|
+
traversed: true,
|
|
66
|
+
rawString: v,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
getNewImportPath(node, file, sourceFile, dirPath) {
|
|
70
|
+
const text = node.moduleSpecifier.getText(sourceFile).slice(1, -1);
|
|
71
|
+
// it's relative and has generated in there, continue
|
|
72
|
+
// do relative imports path regardless of if relative imports is on or not
|
|
73
|
+
if ((0, ast_1.isRelativeGeneratedImport)(node, sourceFile)) {
|
|
74
|
+
const oldPath = path.join(dirPath, text);
|
|
75
|
+
const relFromRoot = path.relative(".", oldPath);
|
|
76
|
+
const conv = transformPath(relFromRoot);
|
|
77
|
+
if (!conv || conv.newFile === relFromRoot) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
return path.relative(dirPath, conv.newFile);
|
|
81
|
+
}
|
|
82
|
+
if (this.checkRelativeImportsValid && (0, ast_1.isRelativeImport)(node, sourceFile)) {
|
|
83
|
+
const parts = file.split(path.sep);
|
|
84
|
+
if (parts.length === 5 &&
|
|
85
|
+
parts.slice(0, 3).join(path.sep) ===
|
|
86
|
+
["src", "graphql", "generated"].join(path.sep)) {
|
|
87
|
+
// we have custom graphql import paths
|
|
88
|
+
// src/graphql/generated/mutations|resolvers/foo_type.ts
|
|
89
|
+
// which used to be
|
|
90
|
+
// src/graphql/mutations|resolvers/generated/foo_type.ts
|
|
91
|
+
// we probably have a broken import.
|
|
92
|
+
// try and fix it...
|
|
93
|
+
let temp = parts[2];
|
|
94
|
+
parts[2] = parts[3];
|
|
95
|
+
parts[3] = temp;
|
|
96
|
+
const oldPath = parts.join(path.sep);
|
|
97
|
+
let oldDir = path.join(this.cwd, oldPath, "..");
|
|
98
|
+
let importPath = path.join(oldDir, text);
|
|
99
|
+
let exists = fs.statSync(importPath + ".ts", { throwIfNoEntry: false });
|
|
100
|
+
if (!exists) {
|
|
101
|
+
// doesn't exist. sadly has to be fixed manually. could theoretically also be a directory but we shouldn't have that
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
return path.relative(dirPath, importPath);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
// if relative imports, we done.
|
|
108
|
+
if (this.relativeImports) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
// non relative, only transform src paths with generated
|
|
112
|
+
if ((0, ast_1.isSrcGeneratedImport)(node, sourceFile)) {
|
|
113
|
+
const conv = transformPath(text);
|
|
114
|
+
if (!conv || conv.newFile === text) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
return conv.newFile;
|
|
118
|
+
}
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
function transformPath(old) {
|
|
123
|
+
const parts = old.split(path.sep);
|
|
124
|
+
if (parts.length < 3) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
const changedParts = parts
|
|
128
|
+
.slice(0, 2)
|
|
129
|
+
.concat("generated")
|
|
130
|
+
.concat(parts.slice(2).filter((v) => v !== "generated"));
|
|
131
|
+
const newFile = changedParts.join(path.sep);
|
|
132
|
+
return { changedParts, newFile };
|
|
133
|
+
}
|
|
134
|
+
function moveFiles(files) {
|
|
135
|
+
files.forEach((file) => {
|
|
136
|
+
const conv = transformPath(file);
|
|
137
|
+
if (!conv) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const { changedParts, newFile } = conv;
|
|
141
|
+
if (file === newFile) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
// check if directory exists, if not, create recursive dir
|
|
145
|
+
const p = changedParts.slice(0, changedParts.length - 1).join(path.sep);
|
|
146
|
+
const statInfo = fs.statSync(p, { throwIfNoEntry: false });
|
|
147
|
+
if (!statInfo) {
|
|
148
|
+
fs.mkdirSync(p, {
|
|
149
|
+
recursive: true,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
// move file to new location
|
|
153
|
+
fs.renameSync(file, newFile);
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
function relativeImports() {
|
|
157
|
+
let yaml = {};
|
|
158
|
+
let relativeImports = false;
|
|
159
|
+
try {
|
|
160
|
+
yaml = (0, js_yaml_1.load)(fs.readFileSync(path.join(process.cwd(), "ent.yml"), {
|
|
161
|
+
encoding: "utf8",
|
|
162
|
+
}));
|
|
163
|
+
relativeImports = yaml?.codegen?.relativeImports || false;
|
|
164
|
+
return yaml?.codegen?.relativeImports || false;
|
|
165
|
+
}
|
|
166
|
+
catch (e) { }
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
function moveGenerated() {
|
|
170
|
+
new MoveFiles("src/ent/**/generated/**/**.ts").move();
|
|
171
|
+
new MoveFiles("src/graphql/**/generated/**/**.ts").move();
|
|
172
|
+
(0, transform_1.transform)(new TransformImports("src/ent/**/*.ts", "src/ent/**.ts"));
|
|
173
|
+
(0, transform_1.transform)(new TransformImports("src/graphql/**/*.ts", "src/graphql/**.ts", true));
|
|
174
|
+
}
|
|
175
|
+
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 {};
|