@snowtop/ent 0.1.0-alpha15 → 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 +3 -3
- package/action/executor.js +2 -2
- package/action/experimental_action.d.ts +3 -3
- package/action/experimental_action.js +10 -4
- package/action/orchestrator.js +6 -4
- package/core/config.d.ts +5 -0
- package/core/ent.d.ts +4 -4
- package/core/ent.js +4 -0
- package/package.json +1 -1
- package/scripts/{move_generated.d.ts → migrate_v0.1.d.ts} +0 -0
- package/scripts/migrate_v0.1.js +43 -0
- package/testutils/builder.d.ts +3 -3
- package/testutils/builder.js +9 -3
- package/tsc/ast.d.ts +12 -0
- package/tsc/ast.js +52 -15
- 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/{scripts → tsc}/transform_schema.js +81 -95
- package/scripts/move_generated.js +0 -142
- package/scripts/transform_code.d.ts +0 -1
- package/scripts/transform_code.js +0 -113
- package/scripts/transform_schema.d.ts +0 -1
package/action/action.d.ts
CHANGED
|
@@ -49,9 +49,9 @@ export interface Action<TEnt extends Ent<TViewer>, TBuilder extends Builder<TEnt
|
|
|
49
49
|
changeset(): Promise<Changeset>;
|
|
50
50
|
builder: TBuilder;
|
|
51
51
|
getPrivacyPolicy(): PrivacyPolicy<TEnt>;
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
getTriggers?(): Trigger<TEnt, TBuilder, TViewer, TInput, TExistingEnt>[];
|
|
53
|
+
getObservers?(): Observer<TEnt, TBuilder, TViewer, TInput, TExistingEnt>[];
|
|
54
|
+
getValidators?(): Validator<TEnt, TBuilder, TViewer, TInput, TExistingEnt>[];
|
|
55
55
|
getInput(): TInput;
|
|
56
56
|
transformWrite?: (stmt: UpdateOperation<TEnt>) => Promise<TransformedUpdateOperation<TEnt>> | TransformedUpdateOperation<TEnt> | null;
|
|
57
57
|
valid(): Promise<boolean>;
|
package/action/executor.js
CHANGED
|
@@ -47,11 +47,11 @@ class ListBasedExecutor {
|
|
|
47
47
|
}
|
|
48
48
|
async executeObservers() {
|
|
49
49
|
const action = this.options?.action;
|
|
50
|
-
if (!this.options || !action || !action.
|
|
50
|
+
if (!this.options || !action || !action.getObservers) {
|
|
51
51
|
return;
|
|
52
52
|
}
|
|
53
53
|
const builder = this.options.builder;
|
|
54
|
-
await Promise.all(action.
|
|
54
|
+
await Promise.all(action.getObservers().map(async (observer) => {
|
|
55
55
|
await observer.observe(builder, action.getInput());
|
|
56
56
|
}));
|
|
57
57
|
}
|
|
@@ -19,10 +19,10 @@ export declare class BaseAction<TEnt extends Ent<TViewer>, TViewer extends Viewe
|
|
|
19
19
|
builderCtr: BuilderConstructor<TEnt, TViewer, TInput>;
|
|
20
20
|
builder: EntBuilder<TEnt, TViewer, TInput>;
|
|
21
21
|
private input;
|
|
22
|
-
triggers: Trigger<TEnt, EntBuilder<TEnt, TViewer, TInput>, TViewer, TInput>[];
|
|
23
|
-
observers: Observer<TEnt, EntBuilder<TEnt, TViewer, TInput>, TViewer, TInput>[];
|
|
24
|
-
validators: Validator<TEnt, EntBuilder<TEnt, TViewer, TInput>, TViewer, TInput>[];
|
|
25
22
|
getPrivacyPolicy(): import("../core/base").PrivacyPolicy<Ent<Viewer<Ent<any> | null, import("../core/base").ID | null>>, Viewer<Ent<any> | null, import("../core/base").ID | null>>;
|
|
23
|
+
getTriggers(): Trigger<TEnt, EntBuilder<TEnt, TViewer, TInput>, TViewer, TInput>[];
|
|
24
|
+
getObservers(): Observer<TEnt, EntBuilder<TEnt, TViewer, TInput>, TViewer, TInput>[];
|
|
25
|
+
getValidators(): Validator<TEnt, EntBuilder<TEnt, TViewer, TInput>, TViewer, TInput>[];
|
|
26
26
|
constructor(viewer: TViewer, builderCtr: BuilderConstructor<TEnt, TViewer, TInput>, options?: ActionOptions<TEnt, TInput> | null);
|
|
27
27
|
static createBuilder<TEnt extends Ent<TViewer>, TViewer extends Viewer, TInput extends Data>(viewer: Viewer, builderCtr: BuilderConstructor<TEnt, TViewer, TInput>, options?: ActionOptions<TEnt, TInput> | null): Builder<TEnt>;
|
|
28
28
|
static bulkAction<TEnt extends Ent<TViewer>, TViewer extends Viewer, TInput extends Data>(ent: TEnt, builderCtr: BuilderConstructor<TEnt, TViewer, TInput>, ...actions: Action<Ent, Builder<Ent, any>>[]): BaseAction<TEnt, TViewer, TInput>;
|
|
@@ -7,9 +7,6 @@ class BaseAction {
|
|
|
7
7
|
constructor(viewer, builderCtr, options) {
|
|
8
8
|
this.viewer = viewer;
|
|
9
9
|
this.builderCtr = builderCtr;
|
|
10
|
-
this.triggers = [];
|
|
11
|
-
this.observers = [];
|
|
12
|
-
this.validators = [];
|
|
13
10
|
let operation = options?.operation;
|
|
14
11
|
if (!operation) {
|
|
15
12
|
if (options?.existingEnt) {
|
|
@@ -25,6 +22,15 @@ class BaseAction {
|
|
|
25
22
|
getPrivacyPolicy() {
|
|
26
23
|
return privacy_1.AlwaysAllowPrivacyPolicy;
|
|
27
24
|
}
|
|
25
|
+
getTriggers() {
|
|
26
|
+
return [];
|
|
27
|
+
}
|
|
28
|
+
getObservers() {
|
|
29
|
+
return [];
|
|
30
|
+
}
|
|
31
|
+
getValidators() {
|
|
32
|
+
return [];
|
|
33
|
+
}
|
|
28
34
|
static createBuilder(viewer, builderCtr, options) {
|
|
29
35
|
let action = new BaseAction(viewer, builderCtr, options);
|
|
30
36
|
return action.builder;
|
|
@@ -35,7 +41,7 @@ class BaseAction {
|
|
|
35
41
|
let action = new BaseAction(ent.viewer, builderCtr, {
|
|
36
42
|
existingEnt: ent,
|
|
37
43
|
});
|
|
38
|
-
action.
|
|
44
|
+
action.getTriggers = () => [
|
|
39
45
|
{
|
|
40
46
|
changeset: () => {
|
|
41
47
|
return actions.map((action) => action.changeset());
|
package/action/orchestrator.js
CHANGED
|
@@ -317,11 +317,13 @@ class Orchestrator {
|
|
|
317
317
|
}
|
|
318
318
|
// have to run triggers which update fields first before field and other validators
|
|
319
319
|
// so running this first to build things up
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
320
|
+
if (action?.getTriggers) {
|
|
321
|
+
await this.triggers(action, builder, action.getTriggers());
|
|
322
|
+
}
|
|
323
|
+
let validators = [];
|
|
324
|
+
if (action?.getValidators) {
|
|
325
|
+
validators = action.getValidators();
|
|
323
326
|
}
|
|
324
|
-
let validators = action?.validators || [];
|
|
325
327
|
// not ideal we're calling this twice. fix...
|
|
326
328
|
// needed for now. may need to rewrite some of this?
|
|
327
329
|
const editedFields2 = await this.options.editedFields();
|
package/core/config.d.ts
CHANGED
|
@@ -35,6 +35,7 @@ interface CodegenConfig {
|
|
|
35
35
|
schemaSQLFilePath?: boolean;
|
|
36
36
|
databaseToCompareTo?: string;
|
|
37
37
|
fieldPrivacyEvaluated?: fieldPrivacyEvaluated;
|
|
38
|
+
templatizedViewer?: templatizedViewer;
|
|
38
39
|
}
|
|
39
40
|
interface PrettierConfig {
|
|
40
41
|
custom?: boolean;
|
|
@@ -45,5 +46,9 @@ interface PrivacyConfig {
|
|
|
45
46
|
policyName: string;
|
|
46
47
|
class?: boolean;
|
|
47
48
|
}
|
|
49
|
+
interface templatizedViewer {
|
|
50
|
+
path: string;
|
|
51
|
+
name: string;
|
|
52
|
+
}
|
|
48
53
|
export declare function loadConfig(file?: string | Buffer | Config): void;
|
|
49
54
|
export {};
|
package/core/ent.d.ts
CHANGED
|
@@ -145,7 +145,7 @@ export declare class AssocEdgeData {
|
|
|
145
145
|
edgeTable: string;
|
|
146
146
|
constructor(data: Data);
|
|
147
147
|
}
|
|
148
|
-
export declare const assocEdgeLoader: DataLoader<
|
|
148
|
+
export declare const assocEdgeLoader: DataLoader<ID, Data | null, ID>;
|
|
149
149
|
export declare function loadEdgeData(edgeType: string): Promise<AssocEdgeData | null>;
|
|
150
150
|
export declare function loadEdgeDatas(...edgeTypes: string[]): Promise<Map<string, AssocEdgeData>>;
|
|
151
151
|
export interface AssocEdgeConstructor<T extends AssocEdge> {
|
|
@@ -172,8 +172,8 @@ interface loadEdgeForIDOptions<T extends AssocEdge> extends loadCustomEdgesOptio
|
|
|
172
172
|
}
|
|
173
173
|
export declare function loadEdgeForID2<T extends AssocEdge>(options: loadEdgeForIDOptions<T>): Promise<T | undefined>;
|
|
174
174
|
export declare function loadNodesByEdge<T extends Ent>(viewer: Viewer, id1: ID, edgeType: string, options: LoadEntOptions<T>): Promise<T[]>;
|
|
175
|
-
export declare function applyPrivacyPolicyForRow<
|
|
176
|
-
export declare function applyPrivacyPolicyForRowX<
|
|
177
|
-
export declare function applyPrivacyPolicyForRows<
|
|
175
|
+
export declare function applyPrivacyPolicyForRow<TEnt extends Ent<TViewer>, TViewer extends Viewer>(viewer: TViewer, options: LoadEntOptions<TEnt, TViewer>, row: Data | null): Promise<TEnt | null>;
|
|
176
|
+
export declare function applyPrivacyPolicyForRowX<TEnt extends Ent<TViewer>, TViewer extends Viewer>(viewer: TViewer, options: LoadEntOptions<TEnt, TViewer>, row: Data): Promise<TEnt>;
|
|
177
|
+
export declare function applyPrivacyPolicyForRows<TEnt extends Ent<TViewer>, TViewer extends Viewer>(viewer: TViewer, rows: Data[], options: LoadEntOptions<TEnt, TViewer>): Promise<Map<ID, TEnt>>;
|
|
178
178
|
export declare function getEdgeTypeInGroup<T extends string>(viewer: Viewer, id1: ID, id2: ID, m: Map<T, string>): Promise<[T, AssocEdge] | undefined>;
|
|
179
179
|
export {};
|
package/core/ent.js
CHANGED
|
@@ -61,6 +61,7 @@ function createDataLoader(options) {
|
|
|
61
61
|
if ((0, logger_1.logEnabled)("query")) {
|
|
62
62
|
loaderOptions.cacheMap = new cacheMap(options);
|
|
63
63
|
}
|
|
64
|
+
// something here brokwn with strict:true
|
|
64
65
|
return new dataloader_1.default(async (ids) => {
|
|
65
66
|
if (!ids.length) {
|
|
66
67
|
return [];
|
|
@@ -436,6 +437,7 @@ class EditNodeOperation {
|
|
|
436
437
|
constructor(options, existingEnt = null) {
|
|
437
438
|
this.options = options;
|
|
438
439
|
this.existingEnt = existingEnt;
|
|
440
|
+
this.row = null;
|
|
439
441
|
this.placeholderID = options.placeholderID;
|
|
440
442
|
}
|
|
441
443
|
resolve(executor) {
|
|
@@ -474,6 +476,7 @@ class EditNodeOperation {
|
|
|
474
476
|
this.row = await editRow(queryer, options, this.existingEnt.id, "RETURNING *");
|
|
475
477
|
}
|
|
476
478
|
else {
|
|
479
|
+
// @ts-ignore
|
|
477
480
|
this.row = this.existingEnt["data"];
|
|
478
481
|
}
|
|
479
482
|
}
|
|
@@ -524,6 +527,7 @@ class EditNodeOperation {
|
|
|
524
527
|
this.reloadRow(queryer, this.existingEnt.id, options);
|
|
525
528
|
}
|
|
526
529
|
else {
|
|
530
|
+
// @ts-ignore
|
|
527
531
|
this.row = this.existingEnt["data"];
|
|
528
532
|
}
|
|
529
533
|
}
|
package/package.json
CHANGED
|
File without changes
|
|
@@ -0,0 +1,43 @@
|
|
|
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
|
+
const transform_1 = require("../tsc/transform");
|
|
7
|
+
const transform_schema_1 = require("../tsc/transform_schema");
|
|
8
|
+
const transform_ent_1 = require("../tsc/transform_ent");
|
|
9
|
+
const move_generated_1 = require("../tsc/move_generated");
|
|
10
|
+
const transform_action_1 = require("../tsc/transform_action");
|
|
11
|
+
const minimist_1 = __importDefault(require("minimist"));
|
|
12
|
+
// call from docker
|
|
13
|
+
// since clients [sh/w]ouldn't have ts-node...
|
|
14
|
+
// locally
|
|
15
|
+
// ts-node --swc --project ./tsconfig.json -r tsconfig-paths/register ../../../ts/src/scripts/migrate_v0.1.ts
|
|
16
|
+
async function main() {
|
|
17
|
+
const options = (0, minimist_1.default)(process.argv.slice(2));
|
|
18
|
+
// install 0.1.x dependencies
|
|
19
|
+
// maybe provide options to make this easier if someone wants to do this in steps to see what's happening
|
|
20
|
+
if (options.move_generated) {
|
|
21
|
+
(0, move_generated_1.moveGenerated)();
|
|
22
|
+
}
|
|
23
|
+
// codegen write-all --disable-custom-graphql
|
|
24
|
+
if (options.transform_schema) {
|
|
25
|
+
(0, transform_1.transform)(new transform_schema_1.TransformSchema());
|
|
26
|
+
}
|
|
27
|
+
// codegen write-all --disable-custom-graphql
|
|
28
|
+
if (options.transform_ent) {
|
|
29
|
+
(0, transform_1.transform)(new transform_ent_1.TransformEnt());
|
|
30
|
+
}
|
|
31
|
+
if (options.transform_action) {
|
|
32
|
+
(0, transform_1.transform)(new transform_action_1.TransformAction());
|
|
33
|
+
}
|
|
34
|
+
// codegen write-all
|
|
35
|
+
}
|
|
36
|
+
main()
|
|
37
|
+
.then(() => {
|
|
38
|
+
process.exit(0);
|
|
39
|
+
})
|
|
40
|
+
.catch((err) => {
|
|
41
|
+
console.error(err);
|
|
42
|
+
process.exit(1);
|
|
43
|
+
});
|
package/testutils/builder.d.ts
CHANGED
|
@@ -98,11 +98,11 @@ export declare class SimpleAction<T extends Ent, TExistingEnt extends TMaybleNul
|
|
|
98
98
|
viewer: Viewer;
|
|
99
99
|
private fields;
|
|
100
100
|
builder: SimpleBuilder<T, TExistingEnt>;
|
|
101
|
-
validators: Validator<T, SimpleBuilder<T>>[];
|
|
102
|
-
triggers: Trigger<T, SimpleBuilder<T>>[];
|
|
103
|
-
observers: Observer<T, SimpleBuilder<T>>[];
|
|
104
101
|
viewerForEntLoad: viewerEntLoadFunc | undefined;
|
|
105
102
|
constructor(viewer: Viewer, schema: BuilderSchema<T>, fields: Map<string, any>, operation: WriteOperation | undefined, existingEnt: TExistingEnt);
|
|
103
|
+
getTriggers(): Trigger<T, SimpleBuilder<T>>[];
|
|
104
|
+
getValidators(): Validator<T, SimpleBuilder<T>>[];
|
|
105
|
+
getObservers(): Observer<T, SimpleBuilder<T>>[];
|
|
106
106
|
getPrivacyPolicy(): PrivacyPolicy<Ent<Viewer<Ent<any> | null, ID | null>>, Viewer<Ent<any> | null, ID | null>>;
|
|
107
107
|
getInput(): Data;
|
|
108
108
|
changeset(): Promise<Changeset>;
|
package/testutils/builder.js
CHANGED
|
@@ -257,11 +257,17 @@ class SimpleAction {
|
|
|
257
257
|
constructor(viewer, schema, fields, operation = action_1.WriteOperation.Insert, existingEnt) {
|
|
258
258
|
this.viewer = viewer;
|
|
259
259
|
this.fields = fields;
|
|
260
|
-
this.validators = [];
|
|
261
|
-
this.triggers = [];
|
|
262
|
-
this.observers = [];
|
|
263
260
|
this.builder = new SimpleBuilder(this.viewer, schema, fields, operation, existingEnt, this);
|
|
264
261
|
}
|
|
262
|
+
getTriggers() {
|
|
263
|
+
return [];
|
|
264
|
+
}
|
|
265
|
+
getValidators() {
|
|
266
|
+
return [];
|
|
267
|
+
}
|
|
268
|
+
getObservers() {
|
|
269
|
+
return [];
|
|
270
|
+
}
|
|
265
271
|
getPrivacyPolicy() {
|
|
266
272
|
return privacy_1.AlwaysAllowPrivacyPolicy;
|
|
267
273
|
}
|
package/tsc/ast.d.ts
CHANGED
|
@@ -15,7 +15,19 @@ interface transformOpts {
|
|
|
15
15
|
removeImports?: string[];
|
|
16
16
|
newImports?: string[];
|
|
17
17
|
transform?: transformImportFn;
|
|
18
|
+
transformPath?: string;
|
|
18
19
|
}
|
|
19
20
|
export declare function transformImport(fileContents: string, importNode: ts.ImportDeclaration, sourceFile: ts.SourceFile, opts?: transformOpts): string | undefined;
|
|
20
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;
|
|
21
33
|
export {};
|
package/tsc/ast.js
CHANGED
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.updateImportPath = exports.transformImport = exports.getClassInfo = exports.getPreText = void 0;
|
|
6
|
+
exports.getImportInfo = exports.isSrcGeneratedImport = exports.isRelativeGeneratedImport = exports.isRelativeImport = exports.updateImportPath = exports.transformImport = exports.getClassInfo = exports.getPreText = void 0;
|
|
7
7
|
const typescript_1 = __importDefault(require("typescript"));
|
|
8
8
|
function getPreText(fileContents, node, sourceFile) {
|
|
9
9
|
return fileContents.substring(node.getFullStart(), node.getStart(sourceFile));
|
|
@@ -31,8 +31,7 @@ function getClassInfo(fileContents, sourceFile, node) {
|
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
|
-
|
|
35
|
-
if (!className || !node.heritageClauses || !classExtends) {
|
|
34
|
+
if (!className) {
|
|
36
35
|
return undefined;
|
|
37
36
|
}
|
|
38
37
|
let hasExport = false;
|
|
@@ -82,21 +81,23 @@ exports.getClassInfo = getClassInfo;
|
|
|
82
81
|
function transformImport(fileContents, importNode, sourceFile, opts) {
|
|
83
82
|
// remove quotes too
|
|
84
83
|
const text = importNode.moduleSpecifier.getText(sourceFile).slice(1, -1);
|
|
85
|
-
if (
|
|
86
|
-
text !==
|
|
87
|
-
|
|
88
|
-
|
|
84
|
+
if (opts?.transformPath) {
|
|
85
|
+
if (text !== opts.transformPath) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
89
88
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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) {
|
|
94
98
|
return;
|
|
95
99
|
}
|
|
96
|
-
const imports =
|
|
97
|
-
.substring(start + 1, end)
|
|
98
|
-
// .trim()
|
|
99
|
-
.split(",");
|
|
100
|
+
const { imports, start, end, importText } = impInfo;
|
|
100
101
|
let removeImportsMap = {};
|
|
101
102
|
if (opts?.removeImports) {
|
|
102
103
|
opts.removeImports.forEach((imp) => (removeImportsMap[imp] = true));
|
|
@@ -152,3 +153,39 @@ function updateImportPath(fileContents, importNode, sourceFile, newPath) {
|
|
|
152
153
|
'";');
|
|
153
154
|
}
|
|
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;
|
|
@@ -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 {};
|