@snowtop/ent 0.1.0-alpha93 → 0.1.0-alpha95
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/core/ent.js +1 -0
- package/package.json +3 -2
- package/parse_schema/parse.d.ts +11 -0
- package/parse_schema/parse.js +48 -1
- package/schema/field.d.ts +1 -0
- package/schema/field.js +1 -0
- package/schema/schema.d.ts +1 -0
- package/scripts/move_types.d.ts +1 -0
- package/scripts/move_types.js +117 -0
- package/testutils/ent-graphql-tests/index.js +2 -2
- package/tsc/ast.js +6 -0
- package/tsc/transform.d.ts +1 -0
- package/tsc/transform.js +12 -2
package/core/ent.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@snowtop/ent",
|
|
3
|
-
"version": "0.1.0-
|
|
3
|
+
"version": "0.1.0-alpha95",
|
|
4
4
|
"description": "snowtop ent framework",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -10,8 +10,9 @@
|
|
|
10
10
|
"dependencies": {
|
|
11
11
|
"@types/node": "^15.0.2",
|
|
12
12
|
"camel-case": "^4.1.2",
|
|
13
|
+
"cosmiconfig": "^7.0.1",
|
|
13
14
|
"dataloader": "^2.0.0",
|
|
14
|
-
"glob": "^
|
|
15
|
+
"glob": "^8.0.3",
|
|
15
16
|
"graph-data-structure": "^1.12.0",
|
|
16
17
|
"js-yaml": "^4.1.0",
|
|
17
18
|
"json5": "^2.1.3",
|
package/parse_schema/parse.d.ts
CHANGED
|
@@ -63,11 +63,22 @@ interface Result {
|
|
|
63
63
|
schemas: schemasDict;
|
|
64
64
|
patterns: patternsDict;
|
|
65
65
|
globalSchema?: ProcessedGlobalSchema;
|
|
66
|
+
config?: {
|
|
67
|
+
rome?: RomeConfig;
|
|
68
|
+
};
|
|
66
69
|
}
|
|
67
70
|
declare type PotentialSchemas = {
|
|
68
71
|
[key: string]: any;
|
|
69
72
|
};
|
|
70
73
|
export declare function parseSchema(potentialSchemas: PotentialSchemas, globalSchema?: GlobalSchema): Promise<Result>;
|
|
74
|
+
interface RomeConfig {
|
|
75
|
+
indentStyle?: string;
|
|
76
|
+
lineWidth?: number;
|
|
77
|
+
indentSize?: number;
|
|
78
|
+
quoteStyle?: string;
|
|
79
|
+
quoteProperties?: string;
|
|
80
|
+
trailingComma?: string;
|
|
81
|
+
}
|
|
71
82
|
interface ProcessedGlobalSchema {
|
|
72
83
|
globalEdges: ProcessedAssocEdge[];
|
|
73
84
|
extraEdgeFields: ProcessedField[];
|
package/parse_schema/parse.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.parseSchema = void 0;
|
|
4
|
+
const cosmiconfig_1 = require("cosmiconfig");
|
|
4
5
|
async function processFields(src, patternName) {
|
|
5
6
|
const ret = [];
|
|
6
7
|
let m = {};
|
|
@@ -274,9 +275,55 @@ async function parseSchema(potentialSchemas, globalSchema) {
|
|
|
274
275
|
}
|
|
275
276
|
schemas[key] = processedSchema;
|
|
276
277
|
}
|
|
277
|
-
|
|
278
|
+
const rome = translatePrettier();
|
|
279
|
+
return {
|
|
280
|
+
schemas,
|
|
281
|
+
patterns,
|
|
282
|
+
globalSchema: parsedGlobalSchema,
|
|
283
|
+
config: {
|
|
284
|
+
rome,
|
|
285
|
+
},
|
|
286
|
+
};
|
|
278
287
|
}
|
|
279
288
|
exports.parseSchema = parseSchema;
|
|
289
|
+
function translatePrettier() {
|
|
290
|
+
const r = (0, cosmiconfig_1.cosmiconfigSync)("prettier").search();
|
|
291
|
+
if (!r) {
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
const ret = {};
|
|
295
|
+
if (r.config.printWidth !== undefined) {
|
|
296
|
+
ret.lineWidth = parseInt(r.config.printWidth);
|
|
297
|
+
}
|
|
298
|
+
if (r.config.useTabs) {
|
|
299
|
+
ret.indentStyle = "tab";
|
|
300
|
+
}
|
|
301
|
+
else {
|
|
302
|
+
ret.indentStyle = "space";
|
|
303
|
+
}
|
|
304
|
+
if (r.config.tabWidth !== undefined) {
|
|
305
|
+
ret.indentSize = parseInt(r.config.tabWidth);
|
|
306
|
+
}
|
|
307
|
+
if (r.config.singleQuote) {
|
|
308
|
+
ret.quoteStyle = "single";
|
|
309
|
+
}
|
|
310
|
+
else {
|
|
311
|
+
ret.quoteStyle = "double";
|
|
312
|
+
}
|
|
313
|
+
if (r.config.quoteProps !== undefined) {
|
|
314
|
+
if (r.config.quoteProps === "consistent") {
|
|
315
|
+
// rome doesn't support this
|
|
316
|
+
ret.quoteProperties = "as-needed";
|
|
317
|
+
}
|
|
318
|
+
else {
|
|
319
|
+
ret.quoteProperties = r.config.quoteProps;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
if (r.config.trailingComma !== undefined) {
|
|
323
|
+
ret.trailingComma = r.config.trailingComma;
|
|
324
|
+
}
|
|
325
|
+
return ret;
|
|
326
|
+
}
|
|
280
327
|
async function parseGlobalSchema(s) {
|
|
281
328
|
const ret = {
|
|
282
329
|
globalEdges: [],
|
package/schema/field.d.ts
CHANGED
package/schema/field.js
CHANGED
|
@@ -514,6 +514,7 @@ class EnumField extends BaseField {
|
|
|
514
514
|
enumMap: options.map,
|
|
515
515
|
type: options.tsType,
|
|
516
516
|
graphQLType: options.graphQLType,
|
|
517
|
+
disableUnknownType: options.disableUnknownType,
|
|
517
518
|
};
|
|
518
519
|
if (!options.foreignKey) {
|
|
519
520
|
if (!options.values && !options.map) {
|
package/schema/schema.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
const transform_1 = require("../tsc/transform");
|
|
30
|
+
const ast_1 = require("../tsc/ast");
|
|
31
|
+
const typescript_1 = __importStar(require("typescript"));
|
|
32
|
+
const path_1 = __importDefault(require("path"));
|
|
33
|
+
class GatherExportsInGeneratedTypes {
|
|
34
|
+
constructor() {
|
|
35
|
+
this.glob = "src/ent/generated/types.ts";
|
|
36
|
+
this.names = [];
|
|
37
|
+
}
|
|
38
|
+
traverseChild(sourceFile, contents, file, node) {
|
|
39
|
+
const exported = node.modifiers?.filter((mod) => mod.getText(sourceFile) === "export");
|
|
40
|
+
if (exported?.length) {
|
|
41
|
+
if (typescript_1.default.isEnumDeclaration(node) ||
|
|
42
|
+
typescript_1.default.isInterfaceDeclaration(node) ||
|
|
43
|
+
typescript_1.default.isTypeAliasDeclaration(node)) {
|
|
44
|
+
this.names.push(node.name.text);
|
|
45
|
+
}
|
|
46
|
+
if (typescript_1.default.isFunctionDeclaration(node) && node.name?.text) {
|
|
47
|
+
this.names.push(node.name.text);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return { node };
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
class TransformImports {
|
|
54
|
+
constructor() {
|
|
55
|
+
this.glob = "src/**/*.ts";
|
|
56
|
+
this.prettierGlob = "src/**/*.ts";
|
|
57
|
+
this.impsToMove = new Map();
|
|
58
|
+
this.cwd = "";
|
|
59
|
+
this.relative = false;
|
|
60
|
+
this.cwd = process.cwd();
|
|
61
|
+
const gt = new GatherExportsInGeneratedTypes();
|
|
62
|
+
(0, transform_1.transform)(gt);
|
|
63
|
+
gt.names.forEach((v) => this.impsToMove.set(v, true));
|
|
64
|
+
this.relative = (0, ast_1.getCustomInfo)().relativeImports ?? this.relative;
|
|
65
|
+
}
|
|
66
|
+
getSrcPath(file, text) {
|
|
67
|
+
if (text.startsWith("src")) {
|
|
68
|
+
return text;
|
|
69
|
+
}
|
|
70
|
+
const dir = path_1.default.dirname(path_1.default.join(this.cwd, file));
|
|
71
|
+
const fullPath = path_1.default.join(dir, text);
|
|
72
|
+
return path_1.default.relative(this.cwd, fullPath);
|
|
73
|
+
}
|
|
74
|
+
traverseChild(sourceFile, contents, file, node) {
|
|
75
|
+
if (!(0, typescript_1.isImportDeclaration)(node)) {
|
|
76
|
+
return { node };
|
|
77
|
+
}
|
|
78
|
+
const impInfo = (0, ast_1.getImportInfo)(node, sourceFile);
|
|
79
|
+
if (!impInfo) {
|
|
80
|
+
return { node };
|
|
81
|
+
}
|
|
82
|
+
const text = node.moduleSpecifier.getText(sourceFile).slice(1, -1);
|
|
83
|
+
const pathToWrite = (0, ast_1.transformRelative)(file, "src/ent/generated/types", this.relative);
|
|
84
|
+
const current = (0, ast_1.transformRelative)(file, text, this.relative);
|
|
85
|
+
const srcPath = this.getSrcPath(file, text);
|
|
86
|
+
// nothing to do here
|
|
87
|
+
if (!srcPath.startsWith("src/ent") || pathToWrite === current) {
|
|
88
|
+
return { node };
|
|
89
|
+
}
|
|
90
|
+
// let's see if we can simplify
|
|
91
|
+
let seenImports = [];
|
|
92
|
+
for (let imp of impInfo.imports) {
|
|
93
|
+
imp = imp.trim();
|
|
94
|
+
if (this.impsToMove.has(imp)) {
|
|
95
|
+
seenImports.push(imp);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (!seenImports.length) {
|
|
99
|
+
return { node };
|
|
100
|
+
}
|
|
101
|
+
let imports = new Map([[pathToWrite, seenImports]]);
|
|
102
|
+
return {
|
|
103
|
+
rawString: (0, ast_1.transformImport)(contents, node, sourceFile, {
|
|
104
|
+
removeImports: seenImports,
|
|
105
|
+
transformPath: text,
|
|
106
|
+
}),
|
|
107
|
+
traversed: true,
|
|
108
|
+
imports,
|
|
109
|
+
allowSeenImportsAdded: true,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
// ts-node-script --swc --project ./tsconfig.json -r tsconfig-paths/register ../../ts/src/scripts/move_types.ts
|
|
114
|
+
function main() {
|
|
115
|
+
(0, transform_1.transform)(new TransformImports());
|
|
116
|
+
}
|
|
117
|
+
main();
|
|
@@ -164,7 +164,7 @@ function buildTreeFromQueryPaths(schema, fieldType, ...options) {
|
|
|
164
164
|
let parts = [];
|
|
165
165
|
let match = fragmentRegex.exec(path);
|
|
166
166
|
if (match) {
|
|
167
|
-
// fragment, keep the part of the fragment e.g.
|
|
167
|
+
// fragment, keep the part of the fragment e.g. `...on User`, and then split the rest....
|
|
168
168
|
parts = [match[0], ...match[2].split(".")];
|
|
169
169
|
const typ = schema.getType(match[1]);
|
|
170
170
|
if (!typ) {
|
|
@@ -223,7 +223,7 @@ function buildTreeFromQueryPaths(schema, fieldType, ...options) {
|
|
|
223
223
|
}
|
|
224
224
|
// only spread out if an object
|
|
225
225
|
const [typ, _] = getInnerType(subField.type, true);
|
|
226
|
-
return (0, graphql_1.isScalarType)(typ);
|
|
226
|
+
return (0, graphql_1.isScalarType)(typ) || (0, graphql_1.isEnumType)(typ);
|
|
227
227
|
}
|
|
228
228
|
if (i === parts.length - 1 && typeof option[1] === "object") {
|
|
229
229
|
if (!isScalarField(part)) {
|
package/tsc/ast.js
CHANGED
|
@@ -142,6 +142,9 @@ function transformImport(fileContents, importNode, sourceFile, opts) {
|
|
|
142
142
|
opts.newImports.forEach((imp) => finalImports.add(imp));
|
|
143
143
|
}
|
|
144
144
|
const comment = getPreText(fileContents, importNode, sourceFile);
|
|
145
|
+
if (!finalImports.size) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
145
148
|
return (comment +
|
|
146
149
|
"import " +
|
|
147
150
|
// add default
|
|
@@ -239,6 +242,9 @@ function normalizePath(p) {
|
|
|
239
242
|
if (p.endsWith("..")) {
|
|
240
243
|
return p + "/";
|
|
241
244
|
}
|
|
245
|
+
if (!p.startsWith("..")) {
|
|
246
|
+
return "./" + p;
|
|
247
|
+
}
|
|
242
248
|
return p;
|
|
243
249
|
}
|
|
244
250
|
// also used in parse schema logic
|
package/tsc/transform.d.ts
CHANGED
package/tsc/transform.js
CHANGED
|
@@ -51,6 +51,7 @@ function transform(transform) {
|
|
|
51
51
|
let imports = new Map();
|
|
52
52
|
let removeImports = [];
|
|
53
53
|
let traversed = false;
|
|
54
|
+
let allowSeenImportsAdded = false;
|
|
54
55
|
let seenImports = new Map();
|
|
55
56
|
typescript_1.default.forEachChild(sourceFile, function (node) {
|
|
56
57
|
if (typescript_1.default.isImportDeclaration(node)) {
|
|
@@ -80,6 +81,9 @@ function transform(transform) {
|
|
|
80
81
|
if (ret.removeImports?.length) {
|
|
81
82
|
removeImports.push(...ret.removeImports);
|
|
82
83
|
}
|
|
84
|
+
if (ret.allowSeenImportsAdded) {
|
|
85
|
+
allowSeenImportsAdded = ret.allowSeenImportsAdded;
|
|
86
|
+
}
|
|
83
87
|
});
|
|
84
88
|
if (!traversed) {
|
|
85
89
|
return;
|
|
@@ -91,14 +95,20 @@ function transform(transform) {
|
|
|
91
95
|
// do this for the first non-import node we see
|
|
92
96
|
// we want to add new imports to end of imports and there's an assumption that imports are ordered
|
|
93
97
|
// at top of file
|
|
98
|
+
// TODO account for placement after first comment
|
|
94
99
|
if (!afterProcessed) {
|
|
95
100
|
for (const [imp, list] of imports) {
|
|
96
101
|
if (seen.has(imp)) {
|
|
97
102
|
continue;
|
|
98
103
|
}
|
|
99
|
-
|
|
104
|
+
// this says only add things you haven't seen
|
|
105
|
+
// we want to add something we've seen in this case
|
|
106
|
+
let final = list;
|
|
107
|
+
if (!allowSeenImportsAdded) {
|
|
108
|
+
final = list.filter((v) => !seenImports.has(v));
|
|
109
|
+
}
|
|
100
110
|
if (final.length) {
|
|
101
|
-
newContents += `\nimport { ${final.join(", ")} } from "${imp}"`;
|
|
111
|
+
newContents += `\nimport { ${final.join(", ")} } from "${imp}";\n`;
|
|
102
112
|
}
|
|
103
113
|
}
|
|
104
114
|
afterProcessed = true;
|