@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.
@@ -22,109 +22,27 @@ 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
- const glob_1 = require("glob");
25
+ exports.TransformSchema = void 0;
26
26
  const typescript_1 = __importDefault(require("typescript"));
27
27
  const fs = __importStar(require("fs"));
28
- const compilerOptions_1 = require("../tsc/compilerOptions");
29
- const child_process_1 = require("child_process");
30
28
  const path_1 = __importDefault(require("path"));
31
29
  const ast_1 = require("../tsc/ast");
32
- async function main() {
33
- let files = glob_1.glob.sync("src/schema/*.ts");
34
- const target = (0, compilerOptions_1.getTargetFromCurrentDir)();
35
- // filter to only event.ts e.g. for comments and whitespace...
36
- // files = files.filter((f) => f.endsWith("event.ts"));
37
- files.forEach((file) => {
38
- // assume valid file since we do glob above
39
- // const idx = file.lastIndexOf(".ts");
40
- // const writeFile = file.substring(0, idx) + "2" + ".ts";
41
- // console.debug(file);
42
- const writeFile = "src/schema/" + path_1.default.basename(file).slice(0, -3) + "_schema.ts";
43
- // const writeFile = file;
44
- // console.debug(file, writeFile);
45
- // go through the file and print everything back if not starting immediately after other position
46
- let { contents, sourceFile } = (0, compilerOptions_1.createSourceFile)(target, file);
47
- const nodes = [];
48
- let updateImport = false;
49
- let removeImports = [];
50
- const f = {
51
- trackNode: function (tni) {
52
- nodes.push({
53
- node: tni.node,
54
- importNode: tni.node && typescript_1.default.isImportDeclaration(tni.node),
55
- rawString: tni.rawString,
56
- });
57
- if (tni.removeImports) {
58
- removeImports.push(...tni.removeImports);
59
- }
60
- },
61
- flagUpdateImport() {
62
- updateImport = true;
63
- },
64
- };
65
- if (!traverse(contents, sourceFile, f)) {
66
- return;
67
- }
68
- let newContents = "";
69
- for (const node of nodes) {
70
- if (updateImport && node.importNode) {
71
- const importNode = node.node;
72
- const transformedImport = (0, ast_1.transformImport)(contents, importNode, sourceFile, {
73
- removeImports,
74
- transform: transformSchema,
75
- });
76
- if (transformedImport) {
77
- newContents += transformedImport;
78
- continue;
79
- }
80
- }
81
- if (node.node) {
82
- newContents += node.node.getFullText(sourceFile);
83
- }
84
- else if (node.rawString) {
85
- newContents += node.rawString;
86
- }
87
- else {
88
- console.error("invalid node");
89
- }
90
- }
91
- // console.debug(newContents);
92
- // ideally there's a flag that indicates if we write
93
- fs.writeFileSync(writeFile, newContents);
94
- fs.rmSync(file);
95
- });
96
- (0, child_process_1.execSync)("prettier src/schema/*.ts --write");
97
- }
98
- function traverse(fileContents, sourceFile, f) {
99
- let traversed = false;
100
- typescript_1.default.forEachChild(sourceFile, function (node) {
101
- if (typescript_1.default.isClassDeclaration(node)) {
102
- traversed = true;
103
- // TODO address implicit schema doesn't work here...
104
- // console.debug(sourceFile.fileName, node.kind);
105
- if (traverseClass(fileContents, sourceFile, node, f)) {
106
- f.flagUpdateImport();
107
- return;
108
- }
109
- }
110
- f.trackNode({ node });
111
- });
112
- return traversed;
113
- }
114
- function traverseClass(fileContents, sourceFile, node, f) {
30
+ function traverseClass(fileContents, sourceFile, node) {
115
31
  const ci = getTransformClassInfo(fileContents, sourceFile, node);
116
32
  if (!ci) {
117
- return false;
33
+ return;
118
34
  }
119
- let klassContents = `${ci.comment}const ${ci.name} = new ${ci.class}({\n`;
35
+ let klassContents = `${ci.comment}const ${ci.name} = new ${transformSchema(ci.class)}({\n`;
120
36
  let removeImports = [];
121
37
  if (ci.implementsSchema) {
122
38
  removeImports.push("Schema");
123
39
  }
40
+ removeImports.push(ci.class);
41
+ let newImports = [transformSchema(ci.class)];
124
42
  for (let member of node.members) {
125
43
  const fInfo = getClassElementInfo(fileContents, member, sourceFile);
126
44
  if (!fInfo) {
127
- return false;
45
+ return;
128
46
  }
129
47
  klassContents += `${fInfo.comment}${fInfo.key}:${fInfo.value},\n`;
130
48
  if (fInfo.type) {
@@ -139,8 +57,11 @@ function traverseClass(fileContents, sourceFile, node, f) {
139
57
  klassContents = "export " + klassContents;
140
58
  }
141
59
  // console.debug(klassContents);
142
- f.trackNode({ rawString: klassContents, removeImports: removeImports });
143
- return true;
60
+ return {
61
+ rawString: klassContents,
62
+ removeImports: removeImports,
63
+ newImports,
64
+ };
144
65
  }
145
66
  function transformSchema(str) {
146
67
  // only do known class names
@@ -222,7 +143,7 @@ function getFieldElementInfo(fileContents, member, sourceFile) {
222
143
  if (parsed === null) {
223
144
  return;
224
145
  }
225
- const { callEx, name, nameComment, properties } = parsed;
146
+ const { callEx, name, nameComment, properties, suffix } = parsed;
226
147
  let property = "";
227
148
  const fieldComment = (0, ast_1.getPreText)(fileContents, element, sourceFile).trim();
228
149
  if (fieldComment) {
@@ -237,7 +158,7 @@ function getFieldElementInfo(fileContents, member, sourceFile) {
237
158
  if (properties.length) {
238
159
  fnCall = `{${properties.join(",")}}`;
239
160
  }
240
- property += `${name}:${call}(${fnCall}),`;
161
+ property += `${name}:${call}(${fnCall})${suffix || ""},`;
241
162
  fieldMap += property;
242
163
  }
243
164
  fieldMap += "}";
@@ -308,13 +229,24 @@ function isFieldElement(member, sourceFile) {
308
229
  }
309
230
  return true;
310
231
  }
311
- function parseFieldElement(element, sourceFile, fileContents) {
232
+ function parseFieldElement(element, sourceFile, fileContents, nested) {
312
233
  if (element.kind !== typescript_1.default.SyntaxKind.CallExpression) {
313
234
  console.error("skipped non-call expression");
314
235
  return null;
315
236
  }
316
237
  let callEx = element;
317
238
  if (callEx.arguments.length !== 1) {
239
+ // have a situation like: StringType({ name: "canonicalName" }).trim().toLowerCase(),
240
+ // need to keep calling this until we find what we want and then get the suffix we should just add to the end of the transformed code
241
+ if (callEx.expression.kind === typescript_1.default.SyntaxKind.PropertyAccessExpression) {
242
+ const ret = parseFieldElement(callEx.expression.expression, sourceFile, fileContents, true);
243
+ if (ret !== null) {
244
+ if (!nested) {
245
+ ret.suffix = fileContents.substring(ret.callEx.getEnd(), callEx.getEnd());
246
+ }
247
+ return ret;
248
+ }
249
+ }
318
250
  console.error("callExpression with arguments not of length 1");
319
251
  return null;
320
252
  }
@@ -352,4 +284,58 @@ function parseFieldElement(element, sourceFile, fileContents) {
352
284
  nameComment: propertyComment,
353
285
  };
354
286
  }
355
- Promise.resolve(main());
287
+ // find which of these importPaths is being used and use that to replace
288
+ function findImportPath(sourceFile) {
289
+ const paths = {
290
+ "@snowtop/ent": true,
291
+ "@snowtop/ent/schema": true,
292
+ "@snowtop/ent/schema/": true,
293
+ };
294
+ // @ts-ignore
295
+ const importStatements = sourceFile.statements.filter((stmt) => typescript_1.default.isImportDeclaration(stmt));
296
+ for (const imp of importStatements) {
297
+ const impInfo = (0, ast_1.getImportInfo)(imp, sourceFile);
298
+ if (!impInfo) {
299
+ continue;
300
+ }
301
+ if (paths[impInfo.importPath] !== undefined) {
302
+ return impInfo.importPath;
303
+ }
304
+ }
305
+ }
306
+ class TransformSchema {
307
+ constructor() {
308
+ // we only end up doing this once because we change the schema representation
309
+ // so safe to run this multiple times
310
+ this.glob = "src/schema/*.ts";
311
+ this.prettierGlob = "src/schema/*.ts";
312
+ }
313
+ traverseChild(sourceFile, contents, file, node) {
314
+ if (!typescript_1.default.isClassDeclaration(node)) {
315
+ return { node };
316
+ }
317
+ // TODO address implicit schema doesn't work here...
318
+ const ret = traverseClass(contents, sourceFile, node);
319
+ if (ret === undefined) {
320
+ return;
321
+ }
322
+ let imports = new Map();
323
+ const imp = findImportPath(sourceFile);
324
+ if (imp) {
325
+ imports.set(imp, ret.newImports);
326
+ }
327
+ return {
328
+ traversed: true,
329
+ rawString: ret.rawString,
330
+ removeImports: ret.removeImports,
331
+ imports: imports,
332
+ };
333
+ }
334
+ fileToWrite(file) {
335
+ return "src/schema/" + path_1.default.basename(file).slice(0, -3) + "_schema.ts";
336
+ }
337
+ postProcess(file) {
338
+ fs.rmSync(file);
339
+ }
340
+ }
341
+ exports.TransformSchema = TransformSchema;
@@ -1,142 +0,0 @@
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 path = __importStar(require("path"));
27
- const fs = __importStar(require("fs"));
28
- const compilerOptions_1 = require("../tsc/compilerOptions");
29
- const typescript_1 = __importDefault(require("typescript"));
30
- const ast_1 = require("../tsc/ast");
31
- const child_process_1 = require("child_process");
32
- // src/ent/generated and src/graphql/generated
33
- function transformPath(old) {
34
- const parts = old.split(path.sep);
35
- if (parts.length < 3) {
36
- return;
37
- }
38
- const changedParts = parts
39
- .slice(0, 2)
40
- .concat("generated")
41
- .concat(parts.slice(2).filter((v) => v !== "generated"));
42
- const newFile = changedParts.join(path.sep);
43
- return { changedParts, newFile };
44
- }
45
- function moveFiles(files) {
46
- files.forEach((file) => {
47
- const conv = transformPath(file);
48
- if (!conv) {
49
- return;
50
- }
51
- const { changedParts, newFile } = conv;
52
- if (file === newFile) {
53
- return;
54
- }
55
- // check if directory exists, if not, create recursive dir
56
- const p = changedParts.slice(0, changedParts.length - 1).join(path.sep);
57
- const statInfo = fs.statSync(p, { throwIfNoEntry: false });
58
- if (!statInfo) {
59
- fs.mkdirSync(p, {
60
- recursive: true,
61
- });
62
- }
63
- // move file to new location
64
- fs.renameSync(file, newFile);
65
- });
66
- }
67
- function updateImports(files, target, cwd) {
68
- files.forEach((file) => {
69
- let { contents, sourceFile } = (0, compilerOptions_1.createSourceFile)(target, file);
70
- let nodes = [];
71
- let updated = false;
72
- typescript_1.default.forEachChild(sourceFile, function (node) {
73
- let dirPath = path.join(cwd, file, "..");
74
- if (typescript_1.default.isImportDeclaration(node)) {
75
- const conv = isGeneratedPath(node, sourceFile, dirPath);
76
- if (!conv) {
77
- nodes.push({ node });
78
- return;
79
- }
80
- updated = true;
81
- const newImportPath = path.relative(dirPath, conv.newFile);
82
- const v = (0, ast_1.updateImportPath)(contents, node, sourceFile, newImportPath);
83
- nodes.push({ rawString: v });
84
- }
85
- else {
86
- nodes.push({ node });
87
- }
88
- });
89
- let newContents = "";
90
- for (const node of nodes) {
91
- if (node.node) {
92
- newContents += node.node.getFullText(sourceFile);
93
- }
94
- else if (node.rawString) {
95
- newContents += node.rawString;
96
- }
97
- else {
98
- throw new Error(`malformed node with no node or rawString`);
99
- }
100
- }
101
- if (updated) {
102
- fs.writeFileSync(file, newContents);
103
- }
104
- });
105
- }
106
- function main() {
107
- const entFiles = glob_1.glob.sync("src/ent/**/generated/**/**.ts");
108
- const graphqlFiles = glob_1.glob.sync("src/graphql/**/generated/**/**.ts");
109
- moveFiles(entFiles);
110
- moveFiles(graphqlFiles);
111
- const target = (0, compilerOptions_1.getTargetFromCurrentDir)();
112
- const entImportFiles = glob_1.glob.sync("src/ent/**/*.ts", {
113
- ignore: ["**/generated/**", "node_modules/**"],
114
- });
115
- const graphqlImportFiles = glob_1.glob.sync("src/graphql/**/*.ts", {
116
- ignore: ["**/generated/**", "node_modules/**"],
117
- });
118
- const cwd = process.cwd();
119
- updateImports(entImportFiles, target, cwd);
120
- updateImports(graphqlImportFiles, target, cwd);
121
- (0, child_process_1.execSync)("prettier src/ent/*.ts --write");
122
- (0, child_process_1.execSync)("prettier src/graphql/*.ts --write");
123
- }
124
- function isGeneratedPath(node, sourceFile, dirPath) {
125
- const text = node.moduleSpecifier.getText(sourceFile).slice(1, -1);
126
- // it's relative and has generated in there, continue
127
- if (!((text.startsWith("..") || text.startsWith("./")) &&
128
- text.indexOf("/generated") !== -1)) {
129
- return;
130
- }
131
- const oldPath = path.join(dirPath, text);
132
- const relFromRoot = path.relative(".", oldPath);
133
- const conv = transformPath(relFromRoot);
134
- if (!conv) {
135
- return;
136
- }
137
- if (relFromRoot === conv.newFile) {
138
- return;
139
- }
140
- return conv;
141
- }
142
- main();
@@ -1 +0,0 @@
1
- export {};
@@ -1,113 +0,0 @@
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
- async function main() {
32
- let files = glob_1.glob.sync("src/ent/*.ts");
33
- const target = (0, compilerOptions_1.getTargetFromCurrentDir)();
34
- files.forEach((file) => {
35
- let { contents, sourceFile } = (0, compilerOptions_1.createSourceFile)(target, file);
36
- let traversed = false;
37
- let nodes = [];
38
- typescript_1.default.forEachChild(sourceFile, function (node) {
39
- if (!typescript_1.default.isClassDeclaration(node) || !node.heritageClauses) {
40
- nodes.push({ node });
41
- return;
42
- }
43
- let classInfo = (0, ast_1.getClassInfo)(contents, sourceFile, node);
44
- // only do classes which extend a base class e.g. User extends UserBase
45
- if (!classInfo || classInfo.extends !== classInfo.name + "Base") {
46
- return;
47
- }
48
- // need to check for PrivacyPolicy import...
49
- traversed = true;
50
- let klassContents = "";
51
- for (const mm of node.members) {
52
- if (isPrivacyPolicy(mm)) {
53
- const property = mm;
54
- // if invalid privacy policy, bounce
55
- if (!property.initializer) {
56
- traversed = false;
57
- return;
58
- }
59
- const pp = property.initializer.getFullText(sourceFile);
60
- const code = `getPrivacyPolicy(): PrivacyPolicy<this> {
61
- return ${pp}
62
- }`;
63
- klassContents += code;
64
- }
65
- else {
66
- klassContents += mm.getFullText(sourceFile);
67
- }
68
- }
69
- // wrap comments and transform to export class Foo extends Bar { ${inner} }
70
- nodes.push({ rawString: classInfo.wrapClassContents(klassContents) });
71
- // console.debug(classInfo.wrapClassContents(klassContents));
72
- });
73
- // if traversed, overwrite.
74
- if (!traversed) {
75
- return;
76
- }
77
- let newContents = "";
78
- for (const node of nodes) {
79
- if (node.node) {
80
- if (typescript_1.default.isImportDeclaration(node.node)) {
81
- let transformed = (0, ast_1.transformImport)(contents, node.node, sourceFile, {
82
- newImports: ["PrivacyPolicy"],
83
- });
84
- if (transformed) {
85
- newContents += transformed;
86
- continue;
87
- }
88
- }
89
- newContents += node.node.getFullText(sourceFile);
90
- }
91
- else if (node.rawString) {
92
- newContents += node.rawString;
93
- }
94
- else {
95
- throw new Error(`malformed node with no node or rawString`);
96
- }
97
- fs.writeFileSync(file, newContents);
98
- }
99
- });
100
- (0, child_process_1.execSync)("prettier src/ent/*.ts --write");
101
- }
102
- function isPrivacyPolicy(mm) {
103
- return (mm.kind === typescript_1.default.SyntaxKind.PropertyDeclaration &&
104
- mm.name.escapedText === "privacyPolicy");
105
- }
106
- main()
107
- .then(() => {
108
- process.exit(0);
109
- })
110
- .catch((err) => {
111
- console.error(err);
112
- process.exit(1);
113
- });
@@ -1 +0,0 @@
1
- export {};