@snowtop/ent 0.1.0-alpha15 → 0.1.0-alpha16
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/package.json +1 -1
- package/scripts/move_generated.js +2 -3
- package/scripts/transform_actions.d.ts +1 -0
- package/scripts/transform_actions.js +266 -0
- package/scripts/transform_code.js +1 -3
- package/testutils/builder.d.ts +3 -3
- package/testutils/builder.js +9 -3
- package/tsc/ast.d.ts +11 -0
- package/tsc/ast.js +46 -13
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/package.json
CHANGED
|
@@ -122,12 +122,11 @@ function main() {
|
|
|
122
122
|
(0, child_process_1.execSync)("prettier src/graphql/*.ts --write");
|
|
123
123
|
}
|
|
124
124
|
function isGeneratedPath(node, sourceFile, dirPath) {
|
|
125
|
-
const text = node.moduleSpecifier.getText(sourceFile).slice(1, -1);
|
|
126
125
|
// it's relative and has generated in there, continue
|
|
127
|
-
if (!(
|
|
128
|
-
text.indexOf("/generated") !== -1)) {
|
|
126
|
+
if (!(0, ast_1.isRelativeGeneratedImport)(node, sourceFile)) {
|
|
129
127
|
return;
|
|
130
128
|
}
|
|
129
|
+
const text = node.moduleSpecifier.getText(sourceFile).slice(1, -1);
|
|
131
130
|
const oldPath = path.join(dirPath, text);
|
|
132
131
|
const relFromRoot = path.relative(".", oldPath);
|
|
133
132
|
const conv = transformPath(relFromRoot);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,266 @@
|
|
|
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
|
+
const glob_1 = require("glob");
|
|
26
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
27
|
+
const compilerOptions_1 = require("../tsc/compilerOptions");
|
|
28
|
+
const ast_1 = require("../tsc/ast");
|
|
29
|
+
const child_process_1 = require("child_process");
|
|
30
|
+
const fs = __importStar(require("fs"));
|
|
31
|
+
const action_1 = require("../action");
|
|
32
|
+
const viewer_1 = require("../core/viewer");
|
|
33
|
+
const path = __importStar(require("path"));
|
|
34
|
+
const js_yaml_1 = require("js-yaml");
|
|
35
|
+
function transformRelative(file, importPath, relative) {
|
|
36
|
+
if (!relative || !importPath.startsWith("src")) {
|
|
37
|
+
return importPath;
|
|
38
|
+
}
|
|
39
|
+
const fileFullPath = path.join(process.cwd(), file);
|
|
40
|
+
const impFullPath = path.join(process.cwd(), importPath);
|
|
41
|
+
// relative path is from directory
|
|
42
|
+
return normalizePath(path.relative(path.dirname(fileFullPath), impFullPath));
|
|
43
|
+
}
|
|
44
|
+
function normalizePath(p) {
|
|
45
|
+
if (p.endsWith("..")) {
|
|
46
|
+
return p + "/";
|
|
47
|
+
}
|
|
48
|
+
return p;
|
|
49
|
+
}
|
|
50
|
+
function findInput(file, sourceFile) {
|
|
51
|
+
// @ts-ignore
|
|
52
|
+
const importStatements = sourceFile.statements.filter((stmt) => typescript_1.default.isImportDeclaration(stmt));
|
|
53
|
+
for (const imp of importStatements) {
|
|
54
|
+
const text = imp.moduleSpecifier.getText(sourceFile).slice(1, -1);
|
|
55
|
+
if ((0, ast_1.isRelativeGeneratedImport)(imp, sourceFile)) {
|
|
56
|
+
// base file and we're importing from it
|
|
57
|
+
// e.g. in create_user_action, we're importing from create_user_action_base
|
|
58
|
+
if (path.basename(file).slice(0, -3) + "_base" !== path.basename(text)) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
const impInfo = (0, ast_1.getImportInfo)(imp, sourceFile);
|
|
62
|
+
if (!impInfo) {
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
const inputs = impInfo.imports
|
|
66
|
+
.filter((imp) => imp.trim() && imp.endsWith("Input"))
|
|
67
|
+
.map((v) => v.trim());
|
|
68
|
+
if (inputs.length === 1) {
|
|
69
|
+
return inputs[0];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
function getCustomInfo() {
|
|
76
|
+
let yaml = {};
|
|
77
|
+
let relativeImports = false;
|
|
78
|
+
try {
|
|
79
|
+
yaml = (0, js_yaml_1.load)(fs.readFileSync(path.join(process.cwd(), "ent.yml"), {
|
|
80
|
+
encoding: "utf8",
|
|
81
|
+
}));
|
|
82
|
+
relativeImports = yaml?.codegen?.relativeImports || false;
|
|
83
|
+
if (yaml?.codegen?.templatizedViewer) {
|
|
84
|
+
return {
|
|
85
|
+
viewerInfo: yaml.codegen.templatizedViewer,
|
|
86
|
+
relativeImports,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
catch (e) { }
|
|
91
|
+
return {
|
|
92
|
+
viewerInfo: {
|
|
93
|
+
path: "@snowtop/ent",
|
|
94
|
+
name: "Viewer",
|
|
95
|
+
},
|
|
96
|
+
relativeImports,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
async function main() {
|
|
100
|
+
let files = glob_1.glob.sync("src/ent/**/actions/**/*_action.ts");
|
|
101
|
+
const target = (0, compilerOptions_1.getTargetFromCurrentDir)();
|
|
102
|
+
const customInfo = getCustomInfo();
|
|
103
|
+
const viewerInfo = customInfo.viewerInfo;
|
|
104
|
+
files.forEach((file) => {
|
|
105
|
+
let { contents, sourceFile } = (0, compilerOptions_1.createSourceFile)(target, file);
|
|
106
|
+
let traversed = false;
|
|
107
|
+
let nodes = [];
|
|
108
|
+
// require action
|
|
109
|
+
const p = require(path.join(process.cwd(), "./" + file.slice(0, -3)));
|
|
110
|
+
const action = new p.default(new viewer_1.LoggedOutViewer(), {});
|
|
111
|
+
const builder = action.builder.constructor.name;
|
|
112
|
+
const nodeName = action.builder.ent.name;
|
|
113
|
+
const existingEnt = action.builder.operation === action_1.WriteOperation.Insert
|
|
114
|
+
? `${nodeName} | null`
|
|
115
|
+
: nodeName;
|
|
116
|
+
const viewer = customInfo.viewerInfo.name;
|
|
117
|
+
const input = findInput(file, sourceFile);
|
|
118
|
+
if (!input) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
let newImports = [];
|
|
122
|
+
typescript_1.default.forEachChild(sourceFile, function (node) {
|
|
123
|
+
if (!typescript_1.default.isClassDeclaration(node) || !node.heritageClauses) {
|
|
124
|
+
nodes.push({ node });
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
let classInfo = (0, ast_1.getClassInfo)(contents, sourceFile, node);
|
|
128
|
+
// only do classes
|
|
129
|
+
if (!classInfo) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
let klassContents = "";
|
|
133
|
+
for (const mm of node.members) {
|
|
134
|
+
const conv = getConversionInfo(mm);
|
|
135
|
+
if (conv !== null) {
|
|
136
|
+
const property = mm;
|
|
137
|
+
// if invalid, bounce
|
|
138
|
+
if (!property.initializer) {
|
|
139
|
+
traversed = false;
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
traversed = true;
|
|
143
|
+
const pp = property.initializer.getFullText(sourceFile).trim();
|
|
144
|
+
const code = `${conv.method}(): ${conv.interface}<${nodeName}, ${builder}<${input}, ${existingEnt}>, ${viewer}, ${input}, ${existingEnt}>[] {
|
|
145
|
+
return ${pp}
|
|
146
|
+
}`;
|
|
147
|
+
newImports.push(conv.interface);
|
|
148
|
+
klassContents += (0, ast_1.getPreText)(contents, mm, sourceFile) + code;
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
klassContents += mm.getFullText(sourceFile);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
// wrap comments and transform to export class Foo extends Bar { ${inner} }
|
|
155
|
+
nodes.push({ rawString: classInfo.wrapClassContents(klassContents) });
|
|
156
|
+
});
|
|
157
|
+
// if traversed, overwrite.
|
|
158
|
+
if (!traversed) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
let newContents = "";
|
|
162
|
+
let afterProcessed = false;
|
|
163
|
+
let imports = new Map([
|
|
164
|
+
[
|
|
165
|
+
transformRelative(file, viewerInfo.path, customInfo.relativeImports),
|
|
166
|
+
[viewer],
|
|
167
|
+
],
|
|
168
|
+
[
|
|
169
|
+
transformRelative(file, "src/ent", customInfo.relativeImports),
|
|
170
|
+
[nodeName],
|
|
171
|
+
],
|
|
172
|
+
["@snowtop/ent/action", newImports],
|
|
173
|
+
]);
|
|
174
|
+
let seen = new Map();
|
|
175
|
+
const processAfterImport = () => {
|
|
176
|
+
// do this for the first non-import node we see
|
|
177
|
+
// we want to add new imports to end of imports and there's an assumption that imports are ordered
|
|
178
|
+
// at top of file
|
|
179
|
+
if (!afterProcessed) {
|
|
180
|
+
for (const [imp, list] of imports) {
|
|
181
|
+
if (seen.has(imp)) {
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
newContents += `\nimport { ${list.join(", ")} } from "${imp}"`;
|
|
185
|
+
}
|
|
186
|
+
afterProcessed = true;
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
for (const node of nodes) {
|
|
190
|
+
if (node.node) {
|
|
191
|
+
if (typescript_1.default.isImportDeclaration(node.node)) {
|
|
192
|
+
const impInfo = (0, ast_1.getImportInfo)(node.node, sourceFile);
|
|
193
|
+
if (impInfo) {
|
|
194
|
+
const impPath = normalizePath(impInfo.importPath);
|
|
195
|
+
// normalize paths...
|
|
196
|
+
const list = imports.get(impPath);
|
|
197
|
+
if (list) {
|
|
198
|
+
let transformed = (0, ast_1.transformImport)(contents, node.node, sourceFile, {
|
|
199
|
+
newImports: list,
|
|
200
|
+
// don't use normalized path here, we wanna use the path that's in code...
|
|
201
|
+
transformPath: impInfo.importPath,
|
|
202
|
+
});
|
|
203
|
+
if (transformed) {
|
|
204
|
+
newContents += transformed;
|
|
205
|
+
seen.set(impPath, true);
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
if (!typescript_1.default.isExportDeclaration(node.node)) {
|
|
213
|
+
processAfterImport();
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
newContents += node.node.getFullText(sourceFile);
|
|
217
|
+
}
|
|
218
|
+
else if (node.rawString) {
|
|
219
|
+
processAfterImport();
|
|
220
|
+
newContents += node.rawString;
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
throw new Error(`malformed node with no node or rawString`);
|
|
224
|
+
}
|
|
225
|
+
fs.writeFileSync(file, newContents);
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
(0, child_process_1.execSync)("prettier src/ent/**/actions/**/*.ts --write");
|
|
229
|
+
}
|
|
230
|
+
let m = {
|
|
231
|
+
triggers: {
|
|
232
|
+
m: "getTriggers",
|
|
233
|
+
i: "Trigger",
|
|
234
|
+
},
|
|
235
|
+
observers: {
|
|
236
|
+
m: "getObservers",
|
|
237
|
+
i: "Observer",
|
|
238
|
+
},
|
|
239
|
+
validators: {
|
|
240
|
+
m: "getValidators",
|
|
241
|
+
i: "Validator",
|
|
242
|
+
},
|
|
243
|
+
};
|
|
244
|
+
function getConversionInfo(mm) {
|
|
245
|
+
if (mm.kind !== typescript_1.default.SyntaxKind.PropertyDeclaration) {
|
|
246
|
+
return null;
|
|
247
|
+
}
|
|
248
|
+
const text = mm.name.escapedText;
|
|
249
|
+
const v = m[text];
|
|
250
|
+
if (v === undefined) {
|
|
251
|
+
return null;
|
|
252
|
+
}
|
|
253
|
+
return {
|
|
254
|
+
text,
|
|
255
|
+
method: v.m,
|
|
256
|
+
interface: v.i,
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
main()
|
|
260
|
+
.then(() => {
|
|
261
|
+
process.exit(0);
|
|
262
|
+
})
|
|
263
|
+
.catch((err) => {
|
|
264
|
+
console.error(err);
|
|
265
|
+
process.exit(1);
|
|
266
|
+
});
|
|
@@ -45,7 +45,6 @@ async function main() {
|
|
|
45
45
|
if (!classInfo || classInfo.extends !== classInfo.name + "Base") {
|
|
46
46
|
return;
|
|
47
47
|
}
|
|
48
|
-
// need to check for PrivacyPolicy import...
|
|
49
48
|
traversed = true;
|
|
50
49
|
let klassContents = "";
|
|
51
50
|
for (const mm of node.members) {
|
|
@@ -60,7 +59,7 @@ async function main() {
|
|
|
60
59
|
const code = `getPrivacyPolicy(): PrivacyPolicy<this> {
|
|
61
60
|
return ${pp}
|
|
62
61
|
}`;
|
|
63
|
-
klassContents += code;
|
|
62
|
+
klassContents += (0, ast_1.getPreText)(contents, mm, sourceFile) + code;
|
|
64
63
|
}
|
|
65
64
|
else {
|
|
66
65
|
klassContents += mm.getFullText(sourceFile);
|
|
@@ -68,7 +67,6 @@ async function main() {
|
|
|
68
67
|
}
|
|
69
68
|
// wrap comments and transform to export class Foo extends Bar { ${inner} }
|
|
70
69
|
nodes.push({ rawString: classInfo.wrapClassContents(klassContents) });
|
|
71
|
-
// console.debug(classInfo.wrapClassContents(klassContents));
|
|
72
70
|
});
|
|
73
71
|
// if traversed, overwrite.
|
|
74
72
|
if (!traversed) {
|
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,18 @@ 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
|
+
interface importInfo {
|
|
25
|
+
imports: string[];
|
|
26
|
+
start: number;
|
|
27
|
+
end: number;
|
|
28
|
+
importText: string;
|
|
29
|
+
importPath: string;
|
|
30
|
+
}
|
|
31
|
+
export declare function getImportInfo(imp: ts.ImportDeclaration, sourceFile: ts.SourceFile): importInfo | undefined;
|
|
21
32
|
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.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));
|
|
@@ -82,21 +82,23 @@ exports.getClassInfo = getClassInfo;
|
|
|
82
82
|
function transformImport(fileContents, importNode, sourceFile, opts) {
|
|
83
83
|
// remove quotes too
|
|
84
84
|
const text = importNode.moduleSpecifier.getText(sourceFile).slice(1, -1);
|
|
85
|
-
if (
|
|
86
|
-
text !==
|
|
87
|
-
|
|
88
|
-
|
|
85
|
+
if (opts?.transformPath) {
|
|
86
|
+
if (text !== opts.transformPath) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
89
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
90
|
+
else {
|
|
91
|
+
if (text !== "@snowtop/ent" &&
|
|
92
|
+
text !== "@snowtop/ent/schema" &&
|
|
93
|
+
text !== "@snowtop/ent/schema/") {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
const impInfo = getImportInfo(importNode, sourceFile);
|
|
98
|
+
if (!impInfo) {
|
|
94
99
|
return;
|
|
95
100
|
}
|
|
96
|
-
const imports =
|
|
97
|
-
.substring(start + 1, end)
|
|
98
|
-
// .trim()
|
|
99
|
-
.split(",");
|
|
101
|
+
const { imports, start, end, importText } = impInfo;
|
|
100
102
|
let removeImportsMap = {};
|
|
101
103
|
if (opts?.removeImports) {
|
|
102
104
|
opts.removeImports.forEach((imp) => (removeImportsMap[imp] = true));
|
|
@@ -152,3 +154,34 @@ function updateImportPath(fileContents, importNode, sourceFile, newPath) {
|
|
|
152
154
|
'";');
|
|
153
155
|
}
|
|
154
156
|
exports.updateImportPath = updateImportPath;
|
|
157
|
+
function isRelativeImport(node, sourceFile) {
|
|
158
|
+
const text = node.moduleSpecifier.getText(sourceFile).slice(1, -1);
|
|
159
|
+
return text.startsWith("..") || text.startsWith("./");
|
|
160
|
+
}
|
|
161
|
+
exports.isRelativeImport = isRelativeImport;
|
|
162
|
+
function isRelativeGeneratedImport(node, sourceFile) {
|
|
163
|
+
const text = node.moduleSpecifier.getText(sourceFile).slice(1, -1);
|
|
164
|
+
return ((text.startsWith("..") || text.startsWith("./")) &&
|
|
165
|
+
text.indexOf("/generated") !== -1);
|
|
166
|
+
}
|
|
167
|
+
exports.isRelativeGeneratedImport = isRelativeGeneratedImport;
|
|
168
|
+
function getImportInfo(imp, sourceFile) {
|
|
169
|
+
const importText = imp.importClause?.getText(sourceFile) || "";
|
|
170
|
+
const start = importText.indexOf("{");
|
|
171
|
+
const end = importText.lastIndexOf("}");
|
|
172
|
+
const text = imp.moduleSpecifier.getText(sourceFile).slice(1, -1);
|
|
173
|
+
if (start === -1 || end === -1) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
return {
|
|
177
|
+
importPath: text,
|
|
178
|
+
importText,
|
|
179
|
+
start,
|
|
180
|
+
end,
|
|
181
|
+
imports: importText
|
|
182
|
+
.substring(start + 1, end)
|
|
183
|
+
//.trim()
|
|
184
|
+
.split(","),
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
exports.getImportInfo = getImportInfo;
|