sniff-qa 0.3.0 → 0.4.0
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/README.md +41 -0
- package/dist/classifier-XEA6ZGE7.js +673 -0
- package/dist/classifier-XEA6ZGE7.js.map +1 -0
- package/dist/cli/index.js +80 -4
- package/dist/cli/index.js.map +1 -1
- package/dist/discover-BLUSO42Q.js +228 -0
- package/dist/discover-BLUSO42Q.js.map +1 -0
- package/dist/domain-UQ34YE4B.js +1817 -0
- package/dist/domain-UQ34YE4B.js.map +1 -0
- package/dist/edge-cases-ZADKDZHL.js +246 -0
- package/dist/edge-cases-ZADKDZHL.js.map +1 -0
- package/dist/flakiness-OLJAY7CS.js +53 -0
- package/dist/flakiness-OLJAY7CS.js.map +1 -0
- package/dist/llm-VJAI6H6G.js +126 -0
- package/dist/llm-VJAI6H6G.js.map +1 -0
- package/dist/mcp/server.js +89 -0
- package/dist/mcp/server.js.map +1 -1
- package/dist/persistence-KTCRIGHU.js +270 -0
- package/dist/persistence-KTCRIGHU.js.map +1 -0
- package/dist/{repo-analyzer-WK5WP7NT.js → repo-analyzer-4OURSITY.js} +1 -1
- package/dist/report-SDZWX25B.js +280 -0
- package/dist/report-SDZWX25B.js.map +1 -0
- package/dist/{report-WLDV72FM.js → report-STC6CAVJ.js} +1 -1
- package/dist/runner-OYTN2NFD.js +996 -0
- package/dist/runner-OYTN2NFD.js.map +1 -0
- package/dist/scenarios-25UAZY6F.js +982 -0
- package/dist/scenarios-25UAZY6F.js.map +1 -0
- package/dist/tiebreak-L5UFNKU2.js +60 -0
- package/dist/tiebreak-L5UFNKU2.js.map +1 -0
- package/dist/{unified-7RKNG3EI.js → unified-2PM442OY.js} +2 -2
- package/package.json +1 -1
- /package/dist/{repo-analyzer-WK5WP7NT.js.map → repo-analyzer-4OURSITY.js.map} +0 -0
- /package/dist/{report-WLDV72FM.js.map → report-STC6CAVJ.js.map} +0 -0
- /package/dist/{unified-7RKNG3EI.js.map → unified-2PM442OY.js.map} +0 -0
|
@@ -0,0 +1,1817 @@
|
|
|
1
|
+
// src/discovery/domain/index.ts
|
|
2
|
+
import { basename, join as join4 } from "path";
|
|
3
|
+
import { performance } from "perf_hooks";
|
|
4
|
+
|
|
5
|
+
// src/discovery/domain/prisma.ts
|
|
6
|
+
import { readFile, access } from "fs/promises";
|
|
7
|
+
import { join, relative } from "path";
|
|
8
|
+
var PRISMA_SCALARS = /* @__PURE__ */ new Set([
|
|
9
|
+
"String",
|
|
10
|
+
"Int",
|
|
11
|
+
"BigInt",
|
|
12
|
+
"Float",
|
|
13
|
+
"Decimal",
|
|
14
|
+
"Boolean",
|
|
15
|
+
"DateTime",
|
|
16
|
+
"Json",
|
|
17
|
+
"Bytes"
|
|
18
|
+
]);
|
|
19
|
+
function mapPrismaScalar(prismaType) {
|
|
20
|
+
switch (prismaType) {
|
|
21
|
+
case "String":
|
|
22
|
+
return { type: "string" };
|
|
23
|
+
case "Int":
|
|
24
|
+
return { type: "number", format: "integer" };
|
|
25
|
+
case "BigInt":
|
|
26
|
+
return { type: "number", format: "integer" };
|
|
27
|
+
case "Float":
|
|
28
|
+
return { type: "number", format: "decimal" };
|
|
29
|
+
case "Decimal":
|
|
30
|
+
return { type: "number", format: "decimal" };
|
|
31
|
+
case "Boolean":
|
|
32
|
+
return { type: "boolean" };
|
|
33
|
+
case "DateTime":
|
|
34
|
+
return { type: "date", format: "iso-date" };
|
|
35
|
+
case "Json":
|
|
36
|
+
return { type: "object" };
|
|
37
|
+
case "Bytes":
|
|
38
|
+
return { type: "unknown" };
|
|
39
|
+
default:
|
|
40
|
+
return { type: "unknown" };
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function inferFormatFromName(fieldName, type) {
|
|
44
|
+
if (type !== "string") return void 0;
|
|
45
|
+
const n = fieldName.toLowerCase();
|
|
46
|
+
if (n === "email" || n.endsWith("email")) return "email";
|
|
47
|
+
if (n === "url" || n.endsWith("url") || n.endsWith("uri")) return "url";
|
|
48
|
+
if (n === "phone" || n.includes("phone")) return "phone";
|
|
49
|
+
if (n === "password" || n.endsWith("password")) return "password";
|
|
50
|
+
if (n === "id" || n.endsWith("id")) return "uuid";
|
|
51
|
+
return void 0;
|
|
52
|
+
}
|
|
53
|
+
function extractEnums(content) {
|
|
54
|
+
const enums = /* @__PURE__ */ new Map();
|
|
55
|
+
const enumRegex = /enum\s+(\w+)\s*\{([^}]*)\}/g;
|
|
56
|
+
let match;
|
|
57
|
+
while ((match = enumRegex.exec(content)) !== null) {
|
|
58
|
+
const name = match[1];
|
|
59
|
+
const body = match[2];
|
|
60
|
+
const values = body.split("\n").map((l) => l.trim()).filter((l) => l && !l.startsWith("//") && !l.startsWith("@@")).map((l) => l.split(/\s+/)[0]).filter(Boolean);
|
|
61
|
+
enums.set(name, values);
|
|
62
|
+
}
|
|
63
|
+
return enums;
|
|
64
|
+
}
|
|
65
|
+
function extractModels(content) {
|
|
66
|
+
const models = [];
|
|
67
|
+
const modelRegex = /model\s+(\w+)\s*\{([^}]*)\}/g;
|
|
68
|
+
let match;
|
|
69
|
+
while ((match = modelRegex.exec(content)) !== null) {
|
|
70
|
+
const startLine = content.slice(0, match.index).split("\n").length;
|
|
71
|
+
const name = match[1];
|
|
72
|
+
const body = match[2];
|
|
73
|
+
const lines = body.split("\n");
|
|
74
|
+
const rawFields = lines.map((line, idx) => ({ line: line.trim(), lineNumber: startLine + idx + 1 })).filter((f) => f.line && !f.line.startsWith("//") && !f.line.startsWith("@@"));
|
|
75
|
+
models.push({ name, rawFields });
|
|
76
|
+
}
|
|
77
|
+
return models;
|
|
78
|
+
}
|
|
79
|
+
function parseFieldLine(line, modelNames, enums) {
|
|
80
|
+
const tokens = line.split(/\s+/);
|
|
81
|
+
if (tokens.length < 2) return { field: null, isId: false };
|
|
82
|
+
const name = tokens[0];
|
|
83
|
+
let typeToken = tokens[1];
|
|
84
|
+
const isList = typeToken.endsWith("[]");
|
|
85
|
+
if (isList) typeToken = typeToken.slice(0, -2);
|
|
86
|
+
const isOptional = typeToken.endsWith("?");
|
|
87
|
+
if (isOptional) typeToken = typeToken.slice(0, -1);
|
|
88
|
+
const rest = tokens.slice(2).join(" ");
|
|
89
|
+
const isId = /\@id\b/.test(rest);
|
|
90
|
+
const hasRelation = /\@relation\b/.test(rest);
|
|
91
|
+
if (modelNames.has(typeToken)) {
|
|
92
|
+
const cardinality = isList ? "1:N" : "1:1";
|
|
93
|
+
const field = {
|
|
94
|
+
name,
|
|
95
|
+
type: "ref",
|
|
96
|
+
refTo: typeToken,
|
|
97
|
+
required: !isOptional
|
|
98
|
+
};
|
|
99
|
+
return { field, isId, relation: { to: typeToken, cardinality } };
|
|
100
|
+
}
|
|
101
|
+
if (enums.has(typeToken)) {
|
|
102
|
+
const field = {
|
|
103
|
+
name,
|
|
104
|
+
type: "enum",
|
|
105
|
+
enum: enums.get(typeToken),
|
|
106
|
+
required: !isOptional
|
|
107
|
+
};
|
|
108
|
+
return { field, isId };
|
|
109
|
+
}
|
|
110
|
+
if (PRISMA_SCALARS.has(typeToken)) {
|
|
111
|
+
const mapped = mapPrismaScalar(typeToken);
|
|
112
|
+
const inferredFormat = inferFormatFromName(name, mapped.type);
|
|
113
|
+
const field = {
|
|
114
|
+
name,
|
|
115
|
+
type: isList ? "list" : mapped.type,
|
|
116
|
+
required: !isOptional && !isList,
|
|
117
|
+
...mapped.format ?? inferredFormat ? { format: inferredFormat ?? mapped.format } : {}
|
|
118
|
+
};
|
|
119
|
+
return { field, isId };
|
|
120
|
+
}
|
|
121
|
+
if (hasRelation) return { field: null, isId: false };
|
|
122
|
+
return {
|
|
123
|
+
field: { name, type: "unknown", required: !isOptional },
|
|
124
|
+
isId
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
async function parsePrismaSchema(rootDir) {
|
|
128
|
+
const candidates = [
|
|
129
|
+
"prisma/schema.prisma",
|
|
130
|
+
"schema.prisma",
|
|
131
|
+
"db/schema.prisma"
|
|
132
|
+
];
|
|
133
|
+
for (const candidate of candidates) {
|
|
134
|
+
const path = join(rootDir, candidate);
|
|
135
|
+
try {
|
|
136
|
+
await access(path);
|
|
137
|
+
const content = await readFile(path, "utf-8");
|
|
138
|
+
return parsePrismaContent(content, relative(rootDir, path));
|
|
139
|
+
} catch {
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return { entities: [], relations: [] };
|
|
144
|
+
}
|
|
145
|
+
function parsePrismaContent(content, filePath) {
|
|
146
|
+
const enums = extractEnums(content);
|
|
147
|
+
const models = extractModels(content);
|
|
148
|
+
const modelNames = new Set(models.map((m) => m.name));
|
|
149
|
+
const entities = [];
|
|
150
|
+
const relations = [];
|
|
151
|
+
for (const model of models) {
|
|
152
|
+
const fields = [];
|
|
153
|
+
const identifiers = [];
|
|
154
|
+
for (const { line } of model.rawFields) {
|
|
155
|
+
const parsed = parseFieldLine(line, modelNames, enums);
|
|
156
|
+
if (!parsed.field) continue;
|
|
157
|
+
fields.push(parsed.field);
|
|
158
|
+
if (parsed.isId) identifiers.push(parsed.field.name);
|
|
159
|
+
if (parsed.relation) {
|
|
160
|
+
relations.push({
|
|
161
|
+
from: model.name,
|
|
162
|
+
to: parsed.relation.to,
|
|
163
|
+
cardinality: parsed.relation.cardinality,
|
|
164
|
+
field: parsed.field.name
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
entities.push({
|
|
169
|
+
name: model.name,
|
|
170
|
+
source: "prisma",
|
|
171
|
+
filePath,
|
|
172
|
+
fields,
|
|
173
|
+
...identifiers.length > 0 ? { identifiers } : {}
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
return { entities, relations };
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// src/discovery/domain/drizzle.ts
|
|
180
|
+
import { readFile as readFile2 } from "fs/promises";
|
|
181
|
+
import { relative as relative2 } from "path";
|
|
182
|
+
import fg from "fast-glob";
|
|
183
|
+
import { parse as babelParse } from "@babel/parser";
|
|
184
|
+
import _traverse from "@babel/traverse";
|
|
185
|
+
var traverse = typeof _traverse === "function" ? _traverse : _traverse.default;
|
|
186
|
+
var TABLE_FACTORIES = /* @__PURE__ */ new Set(["pgTable", "mysqlTable", "sqliteTable"]);
|
|
187
|
+
var STRING_COLUMNS = /* @__PURE__ */ new Set(["text", "varchar", "char", "string", "citext"]);
|
|
188
|
+
var NUMBER_INT_COLUMNS = /* @__PURE__ */ new Set(["serial", "integer", "bigint", "smallint", "tinyint"]);
|
|
189
|
+
var NUMBER_DEC_COLUMNS = /* @__PURE__ */ new Set(["decimal", "numeric", "real", "doublePrecision", "double", "float"]);
|
|
190
|
+
var BOOL_COLUMNS = /* @__PURE__ */ new Set(["boolean", "bool"]);
|
|
191
|
+
var DATE_COLUMNS = /* @__PURE__ */ new Set(["timestamp", "timestamptz", "date", "time", "datetime"]);
|
|
192
|
+
var JSON_COLUMNS = /* @__PURE__ */ new Set(["json", "jsonb"]);
|
|
193
|
+
var UUID_COLUMNS = /* @__PURE__ */ new Set(["uuid"]);
|
|
194
|
+
function mapDrizzleColumn(columnFn) {
|
|
195
|
+
if (STRING_COLUMNS.has(columnFn)) return { type: "string" };
|
|
196
|
+
if (NUMBER_INT_COLUMNS.has(columnFn)) return { type: "number", format: "integer" };
|
|
197
|
+
if (NUMBER_DEC_COLUMNS.has(columnFn)) return { type: "number", format: "decimal" };
|
|
198
|
+
if (BOOL_COLUMNS.has(columnFn)) return { type: "boolean" };
|
|
199
|
+
if (DATE_COLUMNS.has(columnFn)) return { type: "date", format: "iso-date" };
|
|
200
|
+
if (JSON_COLUMNS.has(columnFn)) return { type: "object" };
|
|
201
|
+
if (UUID_COLUMNS.has(columnFn)) return { type: "string", format: "uuid" };
|
|
202
|
+
if (columnFn === "enum" || columnFn === "pgEnum") return { type: "enum" };
|
|
203
|
+
return { type: "unknown" };
|
|
204
|
+
}
|
|
205
|
+
function inferFormatFromName2(fieldName, type) {
|
|
206
|
+
if (type !== "string") return void 0;
|
|
207
|
+
const n = fieldName.toLowerCase();
|
|
208
|
+
if (n === "email" || n.endsWith("email")) return "email";
|
|
209
|
+
if (n === "url" || n.endsWith("url") || n.endsWith("uri")) return "url";
|
|
210
|
+
if (n === "phone" || n.includes("phone")) return "phone";
|
|
211
|
+
if (n === "password" || n.endsWith("password")) return "password";
|
|
212
|
+
return void 0;
|
|
213
|
+
}
|
|
214
|
+
function inspectColumnExpression(node) {
|
|
215
|
+
const modifiers = /* @__PURE__ */ new Set();
|
|
216
|
+
let referencesEntity;
|
|
217
|
+
let referencesField;
|
|
218
|
+
let cursor = node;
|
|
219
|
+
while (cursor && cursor.type === "CallExpression") {
|
|
220
|
+
const call = cursor;
|
|
221
|
+
const callee = call.callee;
|
|
222
|
+
if (callee.type === "MemberExpression" && callee.property.type === "Identifier") {
|
|
223
|
+
modifiers.add(callee.property.name);
|
|
224
|
+
if (callee.property.name === "references" && call.arguments.length > 0) {
|
|
225
|
+
const arg = call.arguments[0];
|
|
226
|
+
if (arg.type === "ArrowFunctionExpression") {
|
|
227
|
+
const body = arg.body;
|
|
228
|
+
if (body.type === "MemberExpression" && body.object.type === "Identifier" && body.property.type === "Identifier") {
|
|
229
|
+
referencesEntity = body.object.name;
|
|
230
|
+
referencesField = body.property.name;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
cursor = callee.object;
|
|
235
|
+
} else if (callee.type === "Identifier") {
|
|
236
|
+
return { baseFn: callee.name, modifiers, referencesEntity, referencesField };
|
|
237
|
+
} else {
|
|
238
|
+
break;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return { baseFn: "unknown", modifiers };
|
|
242
|
+
}
|
|
243
|
+
function getStringLiteralName(declarator) {
|
|
244
|
+
if (declarator.id.type !== "Identifier") return null;
|
|
245
|
+
return declarator.id.name;
|
|
246
|
+
}
|
|
247
|
+
function getColumnsObject(call) {
|
|
248
|
+
if (call.arguments.length < 2) return null;
|
|
249
|
+
const columnsArg = call.arguments[1];
|
|
250
|
+
if (columnsArg.type !== "ObjectExpression") return null;
|
|
251
|
+
return columnsArg;
|
|
252
|
+
}
|
|
253
|
+
function pascalCase(s) {
|
|
254
|
+
return s.split(/[_\s-]+/).filter(Boolean).map((w) => w.charAt(0).toUpperCase() + w.slice(1)).join("");
|
|
255
|
+
}
|
|
256
|
+
function singularize(s) {
|
|
257
|
+
if (s.endsWith("ies") && s.length > 3) return s.slice(0, -3) + "y";
|
|
258
|
+
if (s.endsWith("ses") || s.endsWith("xes") || s.endsWith("zes")) return s.slice(0, -2);
|
|
259
|
+
if (s.endsWith("s") && !s.endsWith("ss")) return s.slice(0, -1);
|
|
260
|
+
return s;
|
|
261
|
+
}
|
|
262
|
+
function parseDrizzleContent(content, filePath) {
|
|
263
|
+
const entities = [];
|
|
264
|
+
const relations = [];
|
|
265
|
+
const tableVarToEntity = /* @__PURE__ */ new Map();
|
|
266
|
+
let ast;
|
|
267
|
+
try {
|
|
268
|
+
ast = babelParse(content, { sourceType: "module", plugins: ["typescript"] });
|
|
269
|
+
} catch {
|
|
270
|
+
return { entities: [], relations: [] };
|
|
271
|
+
}
|
|
272
|
+
const tableCalls = [];
|
|
273
|
+
traverse(ast, {
|
|
274
|
+
VariableDeclarator(path) {
|
|
275
|
+
const init = path.node.init;
|
|
276
|
+
if (!init || init.type !== "CallExpression") return;
|
|
277
|
+
if (init.callee.type !== "Identifier" || !TABLE_FACTORIES.has(init.callee.name)) return;
|
|
278
|
+
const varName = getStringLiteralName(path.node);
|
|
279
|
+
if (!varName) return;
|
|
280
|
+
const columnsObj = getColumnsObject(init);
|
|
281
|
+
if (!columnsObj) return;
|
|
282
|
+
const entityName = pascalCase(singularize(varName));
|
|
283
|
+
tableVarToEntity.set(varName, entityName);
|
|
284
|
+
tableCalls.push({ varName, entityName, columnsObj });
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
for (const { varName, entityName, columnsObj } of tableCalls) {
|
|
288
|
+
const fields = [];
|
|
289
|
+
const identifiers = [];
|
|
290
|
+
for (const property of columnsObj.properties) {
|
|
291
|
+
if (property.type !== "ObjectProperty") continue;
|
|
292
|
+
const prop = property;
|
|
293
|
+
let fieldName;
|
|
294
|
+
if (prop.key.type === "Identifier") fieldName = prop.key.name;
|
|
295
|
+
else if (prop.key.type === "StringLiteral") fieldName = prop.key.value;
|
|
296
|
+
else continue;
|
|
297
|
+
const inspection = inspectColumnExpression(prop.value);
|
|
298
|
+
const isRequired = inspection.modifiers.has("notNull") || inspection.modifiers.has("primaryKey");
|
|
299
|
+
const isId = inspection.modifiers.has("primaryKey");
|
|
300
|
+
if (inspection.referencesEntity) {
|
|
301
|
+
const targetEntity = tableVarToEntity.get(inspection.referencesEntity) ?? pascalCase(singularize(inspection.referencesEntity));
|
|
302
|
+
fields.push({
|
|
303
|
+
name: fieldName,
|
|
304
|
+
type: "ref",
|
|
305
|
+
refTo: targetEntity,
|
|
306
|
+
required: isRequired
|
|
307
|
+
});
|
|
308
|
+
relations.push({
|
|
309
|
+
from: entityName,
|
|
310
|
+
to: targetEntity,
|
|
311
|
+
cardinality: "1:1",
|
|
312
|
+
field: fieldName
|
|
313
|
+
});
|
|
314
|
+
if (isId) identifiers.push(fieldName);
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
const mapped = mapDrizzleColumn(inspection.baseFn);
|
|
318
|
+
const inferredFormat = inferFormatFromName2(fieldName, mapped.type);
|
|
319
|
+
const finalFormat = inferredFormat ?? mapped.format;
|
|
320
|
+
fields.push({
|
|
321
|
+
name: fieldName,
|
|
322
|
+
type: mapped.type,
|
|
323
|
+
required: isRequired,
|
|
324
|
+
...finalFormat ? { format: finalFormat } : {}
|
|
325
|
+
});
|
|
326
|
+
if (isId) identifiers.push(fieldName);
|
|
327
|
+
}
|
|
328
|
+
entities.push({
|
|
329
|
+
name: entityName,
|
|
330
|
+
source: "drizzle",
|
|
331
|
+
filePath,
|
|
332
|
+
fields,
|
|
333
|
+
...identifiers.length > 0 ? { identifiers } : {}
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
return { entities, relations };
|
|
337
|
+
}
|
|
338
|
+
var DRIZZLE_GLOBS = [
|
|
339
|
+
"src/db/**/*.{ts,js}",
|
|
340
|
+
"db/**/*.{ts,js}",
|
|
341
|
+
"src/schema/**/*.{ts,js}",
|
|
342
|
+
"schema/**/*.{ts,js}",
|
|
343
|
+
"src/lib/db/**/*.{ts,js}",
|
|
344
|
+
"lib/db/**/*.{ts,js}",
|
|
345
|
+
"src/database/**/*.{ts,js}",
|
|
346
|
+
"database/**/*.{ts,js}"
|
|
347
|
+
];
|
|
348
|
+
async function parseDrizzleSchemas(rootDir) {
|
|
349
|
+
const files = await fg(DRIZZLE_GLOBS, {
|
|
350
|
+
cwd: rootDir,
|
|
351
|
+
absolute: true,
|
|
352
|
+
ignore: ["node_modules/**", "dist/**", "build/**"]
|
|
353
|
+
});
|
|
354
|
+
const allEntities = [];
|
|
355
|
+
const allRelations = [];
|
|
356
|
+
const seenEntities = /* @__PURE__ */ new Set();
|
|
357
|
+
for (const file of files) {
|
|
358
|
+
let content;
|
|
359
|
+
try {
|
|
360
|
+
content = await readFile2(file, "utf-8");
|
|
361
|
+
} catch {
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
364
|
+
if (!content.includes("drizzle-orm") && !TABLE_FACTORIES.has("pgTable")) continue;
|
|
365
|
+
if (!/\b(pgTable|mysqlTable|sqliteTable)\b/.test(content)) continue;
|
|
366
|
+
const result = parseDrizzleContent(content, relative2(rootDir, file));
|
|
367
|
+
for (const entity of result.entities) {
|
|
368
|
+
if (!seenEntities.has(entity.name)) {
|
|
369
|
+
seenEntities.add(entity.name);
|
|
370
|
+
allEntities.push(entity);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
allRelations.push(...result.relations);
|
|
374
|
+
}
|
|
375
|
+
return { entities: allEntities, relations: allRelations };
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// src/discovery/domain/typeorm.ts
|
|
379
|
+
import { readFile as readFile3 } from "fs/promises";
|
|
380
|
+
import { relative as relative3 } from "path";
|
|
381
|
+
import fg2 from "fast-glob";
|
|
382
|
+
import { parse as babelParse2 } from "@babel/parser";
|
|
383
|
+
import _traverse2 from "@babel/traverse";
|
|
384
|
+
var traverse2 = typeof _traverse2 === "function" ? _traverse2 : _traverse2.default;
|
|
385
|
+
var RELATION_DECORATORS = /* @__PURE__ */ new Set(["OneToOne", "OneToMany", "ManyToOne", "ManyToMany"]);
|
|
386
|
+
var ID_DECORATORS = /* @__PURE__ */ new Set([
|
|
387
|
+
"PrimaryColumn",
|
|
388
|
+
"PrimaryGeneratedColumn",
|
|
389
|
+
"ObjectIdColumn"
|
|
390
|
+
]);
|
|
391
|
+
var COLUMN_TYPE_MAP = {
|
|
392
|
+
varchar: { type: "string" },
|
|
393
|
+
char: { type: "string" },
|
|
394
|
+
text: { type: "string" },
|
|
395
|
+
uuid: { type: "string", format: "uuid" },
|
|
396
|
+
int: { type: "number", format: "integer" },
|
|
397
|
+
integer: { type: "number", format: "integer" },
|
|
398
|
+
bigint: { type: "number", format: "integer" },
|
|
399
|
+
smallint: { type: "number", format: "integer" },
|
|
400
|
+
tinyint: { type: "number", format: "integer" },
|
|
401
|
+
float: { type: "number", format: "decimal" },
|
|
402
|
+
double: { type: "number", format: "decimal" },
|
|
403
|
+
decimal: { type: "number", format: "decimal" },
|
|
404
|
+
numeric: { type: "number", format: "decimal" },
|
|
405
|
+
real: { type: "number", format: "decimal" },
|
|
406
|
+
boolean: { type: "boolean" },
|
|
407
|
+
bool: { type: "boolean" },
|
|
408
|
+
date: { type: "date", format: "iso-date" },
|
|
409
|
+
datetime: { type: "date", format: "iso-date" },
|
|
410
|
+
timestamp: { type: "date", format: "iso-date" },
|
|
411
|
+
time: { type: "date", format: "iso-date" },
|
|
412
|
+
json: { type: "object" },
|
|
413
|
+
jsonb: { type: "object" },
|
|
414
|
+
enum: { type: "enum" }
|
|
415
|
+
};
|
|
416
|
+
function inferFormatFromName3(fieldName, type) {
|
|
417
|
+
if (type !== "string") return void 0;
|
|
418
|
+
const n = fieldName.toLowerCase();
|
|
419
|
+
if (n === "email" || n.endsWith("email")) return "email";
|
|
420
|
+
if (n === "url" || n.endsWith("url") || n.endsWith("uri")) return "url";
|
|
421
|
+
if (n === "phone" || n.includes("phone")) return "phone";
|
|
422
|
+
if (n === "password" || n.endsWith("password")) return "password";
|
|
423
|
+
return void 0;
|
|
424
|
+
}
|
|
425
|
+
function mapTsTypeAnnotation(tsType) {
|
|
426
|
+
if (!tsType) return { type: "unknown" };
|
|
427
|
+
switch (tsType.type) {
|
|
428
|
+
case "TSStringKeyword":
|
|
429
|
+
return { type: "string" };
|
|
430
|
+
case "TSNumberKeyword":
|
|
431
|
+
return { type: "number" };
|
|
432
|
+
case "TSBooleanKeyword":
|
|
433
|
+
return { type: "boolean" };
|
|
434
|
+
case "TSArrayType":
|
|
435
|
+
return { type: "list" };
|
|
436
|
+
case "TSTypeReference": {
|
|
437
|
+
if (tsType.typeName.type === "Identifier") {
|
|
438
|
+
const name = tsType.typeName.name;
|
|
439
|
+
if (name === "Date") return { type: "date", format: "iso-date" };
|
|
440
|
+
if (name === "Array") return { type: "list" };
|
|
441
|
+
}
|
|
442
|
+
return { type: "unknown" };
|
|
443
|
+
}
|
|
444
|
+
default:
|
|
445
|
+
return { type: "unknown" };
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
function getDecoratorName(decorator) {
|
|
449
|
+
const expr = decorator.expression;
|
|
450
|
+
if (expr.type === "CallExpression" && expr.callee.type === "Identifier") {
|
|
451
|
+
return expr.callee.name;
|
|
452
|
+
}
|
|
453
|
+
if (expr.type === "Identifier") return expr.name;
|
|
454
|
+
return null;
|
|
455
|
+
}
|
|
456
|
+
function findDecorator(decorators, name) {
|
|
457
|
+
if (!decorators) return void 0;
|
|
458
|
+
return decorators.find((d) => getDecoratorName(d) === name);
|
|
459
|
+
}
|
|
460
|
+
function findDecoratorByNames(decorators, names) {
|
|
461
|
+
if (!decorators) return void 0;
|
|
462
|
+
return decorators.find((d) => {
|
|
463
|
+
const name = getDecoratorName(d);
|
|
464
|
+
return name !== null && names.has(name);
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
function getObjectPropertyString(obj, propName) {
|
|
468
|
+
for (const prop of obj.properties) {
|
|
469
|
+
if (prop.type !== "ObjectProperty") continue;
|
|
470
|
+
const key = prop.key;
|
|
471
|
+
const matches = key.type === "Identifier" && key.name === propName || key.type === "StringLiteral" && key.value === propName;
|
|
472
|
+
if (!matches) continue;
|
|
473
|
+
if (prop.value.type === "StringLiteral") return prop.value.value;
|
|
474
|
+
}
|
|
475
|
+
return null;
|
|
476
|
+
}
|
|
477
|
+
function getObjectPropertyBoolean(obj, propName) {
|
|
478
|
+
for (const prop of obj.properties) {
|
|
479
|
+
if (prop.type !== "ObjectProperty") continue;
|
|
480
|
+
const key = prop.key;
|
|
481
|
+
const matches = key.type === "Identifier" && key.name === propName || key.type === "StringLiteral" && key.value === propName;
|
|
482
|
+
if (!matches) continue;
|
|
483
|
+
if (prop.value.type === "BooleanLiteral") return prop.value.value;
|
|
484
|
+
}
|
|
485
|
+
return null;
|
|
486
|
+
}
|
|
487
|
+
function parseColumnDecorator(decorator) {
|
|
488
|
+
if (decorator.expression.type !== "CallExpression") return {};
|
|
489
|
+
const args = decorator.expression.arguments;
|
|
490
|
+
if (args.length === 0) return {};
|
|
491
|
+
const result = {};
|
|
492
|
+
if (args[0].type === "StringLiteral") {
|
|
493
|
+
result.columnType = args[0].value.toLowerCase();
|
|
494
|
+
}
|
|
495
|
+
const optionsArg = args.find((a) => a.type === "ObjectExpression");
|
|
496
|
+
if (optionsArg) {
|
|
497
|
+
const typeStr = getObjectPropertyString(optionsArg, "type");
|
|
498
|
+
if (typeStr) result.columnType = typeStr.toLowerCase();
|
|
499
|
+
const nullable = getObjectPropertyBoolean(optionsArg, "nullable");
|
|
500
|
+
if (nullable !== null) result.nullable = nullable;
|
|
501
|
+
}
|
|
502
|
+
return result;
|
|
503
|
+
}
|
|
504
|
+
function parseRelationDecorator(decorator) {
|
|
505
|
+
let isMany = false;
|
|
506
|
+
const name = getDecoratorName(decorator);
|
|
507
|
+
if (name === "OneToMany" || name === "ManyToMany") isMany = true;
|
|
508
|
+
if (decorator.expression.type !== "CallExpression") return { isMany };
|
|
509
|
+
const args = decorator.expression.arguments;
|
|
510
|
+
if (args.length === 0) return { isMany };
|
|
511
|
+
const targetArg = args[0];
|
|
512
|
+
if (targetArg.type === "ArrowFunctionExpression") {
|
|
513
|
+
const body = targetArg.body;
|
|
514
|
+
if (body.type === "Identifier") return { target: body.name, isMany };
|
|
515
|
+
}
|
|
516
|
+
return { isMany };
|
|
517
|
+
}
|
|
518
|
+
function parseTypeOrmContent(content, filePath) {
|
|
519
|
+
const entities = [];
|
|
520
|
+
const relations = [];
|
|
521
|
+
let ast;
|
|
522
|
+
try {
|
|
523
|
+
ast = babelParse2(content, {
|
|
524
|
+
sourceType: "module",
|
|
525
|
+
plugins: [
|
|
526
|
+
"typescript",
|
|
527
|
+
["decorators", { decoratorsBeforeExport: true }],
|
|
528
|
+
"classProperties"
|
|
529
|
+
]
|
|
530
|
+
});
|
|
531
|
+
} catch {
|
|
532
|
+
return { entities: [], relations: [] };
|
|
533
|
+
}
|
|
534
|
+
traverse2(ast, {
|
|
535
|
+
ClassDeclaration(path) {
|
|
536
|
+
const classNode = path.node;
|
|
537
|
+
const decorators = classNode.decorators;
|
|
538
|
+
const entityDecorator = findDecorator(decorators, "Entity");
|
|
539
|
+
if (!entityDecorator) return;
|
|
540
|
+
if (!classNode.id || classNode.id.type !== "Identifier") return;
|
|
541
|
+
const entityName = classNode.id.name;
|
|
542
|
+
const fields = [];
|
|
543
|
+
const identifiers = [];
|
|
544
|
+
for (const member of classNode.body.body) {
|
|
545
|
+
if (member.type !== "ClassProperty" && member.type !== "ClassAccessorProperty") continue;
|
|
546
|
+
const property = member;
|
|
547
|
+
if (property.key.type !== "Identifier") continue;
|
|
548
|
+
const fieldName = property.key.name;
|
|
549
|
+
const propDecorators = property.decorators;
|
|
550
|
+
if (!propDecorators || propDecorators.length === 0) continue;
|
|
551
|
+
const idDecorator = findDecoratorByNames(propDecorators, ID_DECORATORS);
|
|
552
|
+
const columnDecorator = findDecorator(propDecorators, "Column");
|
|
553
|
+
const relationDecorator = findDecoratorByNames(propDecorators, RELATION_DECORATORS);
|
|
554
|
+
const isOptional = property.optional ?? false;
|
|
555
|
+
if (relationDecorator) {
|
|
556
|
+
const { target, isMany } = parseRelationDecorator(relationDecorator);
|
|
557
|
+
if (!target) continue;
|
|
558
|
+
fields.push({
|
|
559
|
+
name: fieldName,
|
|
560
|
+
type: "ref",
|
|
561
|
+
refTo: target,
|
|
562
|
+
required: !isOptional
|
|
563
|
+
});
|
|
564
|
+
relations.push({
|
|
565
|
+
from: entityName,
|
|
566
|
+
to: target,
|
|
567
|
+
cardinality: isMany ? "1:N" : "1:1",
|
|
568
|
+
field: fieldName
|
|
569
|
+
});
|
|
570
|
+
continue;
|
|
571
|
+
}
|
|
572
|
+
if (idDecorator) {
|
|
573
|
+
const tsMapped = mapTsTypeAnnotation(property.typeAnnotation?.type === "TSTypeAnnotation" ? property.typeAnnotation.typeAnnotation : null);
|
|
574
|
+
const format = tsMapped.format ?? inferFormatFromName3(fieldName, tsMapped.type);
|
|
575
|
+
fields.push({
|
|
576
|
+
name: fieldName,
|
|
577
|
+
type: tsMapped.type,
|
|
578
|
+
required: true,
|
|
579
|
+
...format ? { format } : {}
|
|
580
|
+
});
|
|
581
|
+
identifiers.push(fieldName);
|
|
582
|
+
continue;
|
|
583
|
+
}
|
|
584
|
+
if (columnDecorator) {
|
|
585
|
+
const colInfo = parseColumnDecorator(columnDecorator);
|
|
586
|
+
let mapped;
|
|
587
|
+
if (colInfo.columnType && COLUMN_TYPE_MAP[colInfo.columnType]) {
|
|
588
|
+
mapped = COLUMN_TYPE_MAP[colInfo.columnType];
|
|
589
|
+
} else {
|
|
590
|
+
mapped = mapTsTypeAnnotation(property.typeAnnotation?.type === "TSTypeAnnotation" ? property.typeAnnotation.typeAnnotation : null);
|
|
591
|
+
}
|
|
592
|
+
const required = !(isOptional || colInfo.nullable === true);
|
|
593
|
+
const format = mapped.format ?? inferFormatFromName3(fieldName, mapped.type);
|
|
594
|
+
fields.push({
|
|
595
|
+
name: fieldName,
|
|
596
|
+
type: mapped.type,
|
|
597
|
+
required,
|
|
598
|
+
...format ? { format } : {}
|
|
599
|
+
});
|
|
600
|
+
continue;
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
entities.push({
|
|
604
|
+
name: entityName,
|
|
605
|
+
source: "typeorm",
|
|
606
|
+
filePath,
|
|
607
|
+
fields,
|
|
608
|
+
...identifiers.length > 0 ? { identifiers } : {}
|
|
609
|
+
});
|
|
610
|
+
}
|
|
611
|
+
});
|
|
612
|
+
return { entities, relations };
|
|
613
|
+
}
|
|
614
|
+
var TYPEORM_GLOBS = [
|
|
615
|
+
"src/entities/**/*.{ts,js}",
|
|
616
|
+
"src/entity/**/*.{ts,js}",
|
|
617
|
+
"entities/**/*.{ts,js}",
|
|
618
|
+
"entity/**/*.{ts,js}",
|
|
619
|
+
"src/models/**/*.{ts,js}",
|
|
620
|
+
"models/**/*.{ts,js}"
|
|
621
|
+
];
|
|
622
|
+
async function parseTypeOrmEntities(rootDir) {
|
|
623
|
+
const files = await fg2(TYPEORM_GLOBS, {
|
|
624
|
+
cwd: rootDir,
|
|
625
|
+
absolute: true,
|
|
626
|
+
ignore: ["node_modules/**", "dist/**", "build/**"]
|
|
627
|
+
});
|
|
628
|
+
const allEntities = [];
|
|
629
|
+
const allRelations = [];
|
|
630
|
+
const seenEntities = /* @__PURE__ */ new Set();
|
|
631
|
+
for (const file of files) {
|
|
632
|
+
let content;
|
|
633
|
+
try {
|
|
634
|
+
content = await readFile3(file, "utf-8");
|
|
635
|
+
} catch {
|
|
636
|
+
continue;
|
|
637
|
+
}
|
|
638
|
+
if (!/from\s+['"]typeorm['"]/.test(content) && !/@Entity\b/.test(content)) continue;
|
|
639
|
+
const result = parseTypeOrmContent(content, relative3(rootDir, file));
|
|
640
|
+
for (const entity of result.entities) {
|
|
641
|
+
if (!seenEntities.has(entity.name)) {
|
|
642
|
+
seenEntities.add(entity.name);
|
|
643
|
+
allEntities.push(entity);
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
allRelations.push(...result.relations);
|
|
647
|
+
}
|
|
648
|
+
return { entities: allEntities, relations: allRelations };
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
// src/discovery/domain/zod.ts
|
|
652
|
+
import { readFile as readFile4 } from "fs/promises";
|
|
653
|
+
import { relative as relative4 } from "path";
|
|
654
|
+
import fg3 from "fast-glob";
|
|
655
|
+
import { parse as babelParse3 } from "@babel/parser";
|
|
656
|
+
import _traverse3 from "@babel/traverse";
|
|
657
|
+
var traverse3 = typeof _traverse3 === "function" ? _traverse3 : _traverse3.default;
|
|
658
|
+
function inspectZodChain(expr) {
|
|
659
|
+
const modifiers = /* @__PURE__ */ new Set();
|
|
660
|
+
let baseMethod = "unknown";
|
|
661
|
+
let arrayInner;
|
|
662
|
+
let enumValues;
|
|
663
|
+
let cursor = expr;
|
|
664
|
+
while (cursor.type === "CallExpression") {
|
|
665
|
+
const call = cursor;
|
|
666
|
+
const callee = call.callee;
|
|
667
|
+
if (callee.type === "MemberExpression" && callee.property.type === "Identifier") {
|
|
668
|
+
const name = callee.property.name;
|
|
669
|
+
if (callee.object.type === "Identifier" && callee.object.name === "z") {
|
|
670
|
+
baseMethod = name;
|
|
671
|
+
if (name === "array" && call.arguments[0]?.type === "CallExpression") {
|
|
672
|
+
const inner = inspectZodChain(call.arguments[0]);
|
|
673
|
+
arrayInner = inner.baseMethod;
|
|
674
|
+
}
|
|
675
|
+
if (name === "enum" && call.arguments[0]?.type === "ArrayExpression") {
|
|
676
|
+
enumValues = call.arguments[0].elements.filter((el) => el !== null && el.type === "StringLiteral").map((el) => el.value);
|
|
677
|
+
}
|
|
678
|
+
break;
|
|
679
|
+
}
|
|
680
|
+
modifiers.add(name);
|
|
681
|
+
cursor = callee.object;
|
|
682
|
+
continue;
|
|
683
|
+
}
|
|
684
|
+
break;
|
|
685
|
+
}
|
|
686
|
+
return { baseMethod, modifiers, arrayInner, enumValues };
|
|
687
|
+
}
|
|
688
|
+
function mapZodBase(base) {
|
|
689
|
+
switch (base) {
|
|
690
|
+
case "string":
|
|
691
|
+
return { type: "string" };
|
|
692
|
+
case "number":
|
|
693
|
+
return { type: "number" };
|
|
694
|
+
case "bigint":
|
|
695
|
+
return { type: "number", format: "integer" };
|
|
696
|
+
case "boolean":
|
|
697
|
+
return { type: "boolean" };
|
|
698
|
+
case "date":
|
|
699
|
+
return { type: "date", format: "iso-date" };
|
|
700
|
+
case "object":
|
|
701
|
+
return { type: "object" };
|
|
702
|
+
case "array":
|
|
703
|
+
return { type: "list" };
|
|
704
|
+
case "enum":
|
|
705
|
+
return { type: "enum" };
|
|
706
|
+
case "nativeEnum":
|
|
707
|
+
return { type: "enum" };
|
|
708
|
+
case "record":
|
|
709
|
+
return { type: "object" };
|
|
710
|
+
case "literal":
|
|
711
|
+
return { type: "string" };
|
|
712
|
+
default:
|
|
713
|
+
return { type: "unknown" };
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
function modifierToFormat(modifiers) {
|
|
717
|
+
if (modifiers.has("email")) return "email";
|
|
718
|
+
if (modifiers.has("url")) return "url";
|
|
719
|
+
if (modifiers.has("uuid")) return "uuid";
|
|
720
|
+
if (modifiers.has("int")) return "integer";
|
|
721
|
+
return void 0;
|
|
722
|
+
}
|
|
723
|
+
function inferFormatFromName4(fieldName, type) {
|
|
724
|
+
if (type !== "string") return void 0;
|
|
725
|
+
const n = fieldName.toLowerCase();
|
|
726
|
+
if (n === "email" || n.endsWith("email")) return "email";
|
|
727
|
+
if (n === "url" || n.endsWith("url") || n.endsWith("uri")) return "url";
|
|
728
|
+
if (n === "phone" || n.includes("phone")) return "phone";
|
|
729
|
+
if (n === "password" || n.endsWith("password")) return "password";
|
|
730
|
+
return void 0;
|
|
731
|
+
}
|
|
732
|
+
function stripEntitySuffix(name) {
|
|
733
|
+
return name.replace(/Schema$/, "").replace(/DTO$/, "").replace(/Dto$/, "").replace(/Shape$/, "");
|
|
734
|
+
}
|
|
735
|
+
function fieldFromZodChain(fieldName, chain) {
|
|
736
|
+
const mapped = mapZodBase(chain.baseMethod);
|
|
737
|
+
const modifierFormat = modifierToFormat(chain.modifiers);
|
|
738
|
+
const inferredFormat = inferFormatFromName4(fieldName, mapped.type);
|
|
739
|
+
const required = !chain.modifiers.has("optional") && !chain.modifiers.has("nullish");
|
|
740
|
+
const finalFormat = modifierFormat ?? inferredFormat ?? mapped.format;
|
|
741
|
+
const field = {
|
|
742
|
+
name: fieldName,
|
|
743
|
+
type: mapped.type,
|
|
744
|
+
required
|
|
745
|
+
};
|
|
746
|
+
if (finalFormat) field.format = finalFormat;
|
|
747
|
+
if (chain.enumValues) field.enum = chain.enumValues;
|
|
748
|
+
return field;
|
|
749
|
+
}
|
|
750
|
+
function parseZodContent(content, filePath) {
|
|
751
|
+
const entities = [];
|
|
752
|
+
let ast;
|
|
753
|
+
try {
|
|
754
|
+
ast = babelParse3(content, { sourceType: "module", plugins: ["typescript"] });
|
|
755
|
+
} catch {
|
|
756
|
+
return { entities: [] };
|
|
757
|
+
}
|
|
758
|
+
traverse3(ast, {
|
|
759
|
+
VariableDeclarator(path) {
|
|
760
|
+
const init = path.node.init;
|
|
761
|
+
if (!init) return;
|
|
762
|
+
if (path.node.id.type !== "Identifier") return;
|
|
763
|
+
let objectCall = null;
|
|
764
|
+
let cursor = init;
|
|
765
|
+
while (cursor && cursor.type === "CallExpression") {
|
|
766
|
+
const callee = cursor.callee;
|
|
767
|
+
if (callee.type === "MemberExpression" && callee.object.type === "Identifier" && callee.object.name === "z" && callee.property.type === "Identifier" && callee.property.name === "object") {
|
|
768
|
+
objectCall = cursor;
|
|
769
|
+
break;
|
|
770
|
+
}
|
|
771
|
+
if (callee.type === "MemberExpression") {
|
|
772
|
+
cursor = callee.object;
|
|
773
|
+
continue;
|
|
774
|
+
}
|
|
775
|
+
break;
|
|
776
|
+
}
|
|
777
|
+
if (!objectCall) return;
|
|
778
|
+
const shapeArg = objectCall.arguments[0];
|
|
779
|
+
if (!shapeArg || shapeArg.type !== "ObjectExpression") return;
|
|
780
|
+
const rawName = path.node.id.name;
|
|
781
|
+
const entityName = stripEntitySuffix(rawName);
|
|
782
|
+
const fields = [];
|
|
783
|
+
for (const property of shapeArg.properties) {
|
|
784
|
+
if (property.type !== "ObjectProperty") continue;
|
|
785
|
+
const prop = property;
|
|
786
|
+
let fieldName;
|
|
787
|
+
if (prop.key.type === "Identifier") fieldName = prop.key.name;
|
|
788
|
+
else if (prop.key.type === "StringLiteral") fieldName = prop.key.value;
|
|
789
|
+
else continue;
|
|
790
|
+
if (prop.value.type !== "CallExpression" && prop.value.type !== "MemberExpression") continue;
|
|
791
|
+
const chain = inspectZodChain(prop.value);
|
|
792
|
+
fields.push(fieldFromZodChain(fieldName, chain));
|
|
793
|
+
}
|
|
794
|
+
if (fields.length === 0) return;
|
|
795
|
+
entities.push({
|
|
796
|
+
name: entityName,
|
|
797
|
+
source: "zod",
|
|
798
|
+
filePath,
|
|
799
|
+
fields
|
|
800
|
+
});
|
|
801
|
+
}
|
|
802
|
+
});
|
|
803
|
+
return { entities };
|
|
804
|
+
}
|
|
805
|
+
var ZOD_GLOBS = [
|
|
806
|
+
"src/schemas/**/*.{ts,tsx,js,jsx}",
|
|
807
|
+
"schemas/**/*.{ts,tsx,js,jsx}",
|
|
808
|
+
"src/types/**/*.{ts,tsx,js,jsx}",
|
|
809
|
+
"types/**/*.{ts,tsx,js,jsx}",
|
|
810
|
+
"src/validators/**/*.{ts,tsx,js,jsx}",
|
|
811
|
+
"validators/**/*.{ts,tsx,js,jsx}",
|
|
812
|
+
"src/models/**/*.{ts,tsx,js,jsx}",
|
|
813
|
+
"src/lib/**/*.{ts,tsx,js,jsx}",
|
|
814
|
+
"app/**/*.{ts,tsx,js,jsx}",
|
|
815
|
+
"src/app/**/*.{ts,tsx,js,jsx}"
|
|
816
|
+
];
|
|
817
|
+
async function parseZodSchemas(rootDir) {
|
|
818
|
+
const files = await fg3(ZOD_GLOBS, {
|
|
819
|
+
cwd: rootDir,
|
|
820
|
+
absolute: true,
|
|
821
|
+
ignore: ["node_modules/**", "dist/**", "build/**", ".next/**"]
|
|
822
|
+
});
|
|
823
|
+
const allEntities = [];
|
|
824
|
+
const seenEntities = /* @__PURE__ */ new Set();
|
|
825
|
+
for (const file of files) {
|
|
826
|
+
let content;
|
|
827
|
+
try {
|
|
828
|
+
content = await readFile4(file, "utf-8");
|
|
829
|
+
} catch {
|
|
830
|
+
continue;
|
|
831
|
+
}
|
|
832
|
+
if (!content.includes("zod")) continue;
|
|
833
|
+
if (!/\bz\s*\.\s*object\s*\(/.test(content)) continue;
|
|
834
|
+
const result = parseZodContent(content, relative4(rootDir, file));
|
|
835
|
+
for (const entity of result.entities) {
|
|
836
|
+
if (!seenEntities.has(entity.name)) {
|
|
837
|
+
seenEntities.add(entity.name);
|
|
838
|
+
allEntities.push(entity);
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
return { entities: allEntities };
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
// src/discovery/domain/ts-types.ts
|
|
846
|
+
import { readFile as readFile5 } from "fs/promises";
|
|
847
|
+
import { relative as relative5 } from "path";
|
|
848
|
+
import fg4 from "fast-glob";
|
|
849
|
+
import { parse as babelParse4 } from "@babel/parser";
|
|
850
|
+
import _traverse4 from "@babel/traverse";
|
|
851
|
+
var traverse4 = typeof _traverse4 === "function" ? _traverse4 : _traverse4.default;
|
|
852
|
+
function inferFormatFromName5(fieldName, type) {
|
|
853
|
+
if (type !== "string") return void 0;
|
|
854
|
+
const n = fieldName.toLowerCase();
|
|
855
|
+
if (n === "email" || n.endsWith("email")) return "email";
|
|
856
|
+
if (n === "url" || n.endsWith("url") || n.endsWith("uri")) return "url";
|
|
857
|
+
if (n === "phone" || n.includes("phone")) return "phone";
|
|
858
|
+
if (n === "password" || n.endsWith("password")) return "password";
|
|
859
|
+
if (n === "id" || n.endsWith("id") || n.endsWith("Id")) return "uuid";
|
|
860
|
+
return void 0;
|
|
861
|
+
}
|
|
862
|
+
function mapTsType(tsType, knownEntities) {
|
|
863
|
+
switch (tsType.type) {
|
|
864
|
+
case "TSStringKeyword":
|
|
865
|
+
return { type: "string" };
|
|
866
|
+
case "TSNumberKeyword":
|
|
867
|
+
return { type: "number" };
|
|
868
|
+
case "TSBooleanKeyword":
|
|
869
|
+
return { type: "boolean" };
|
|
870
|
+
case "TSBigIntKeyword":
|
|
871
|
+
return { type: "number", format: "integer" };
|
|
872
|
+
case "TSNullKeyword":
|
|
873
|
+
return { type: "unknown" };
|
|
874
|
+
case "TSUndefinedKeyword":
|
|
875
|
+
return { type: "unknown" };
|
|
876
|
+
case "TSAnyKeyword":
|
|
877
|
+
return { type: "unknown" };
|
|
878
|
+
case "TSUnknownKeyword":
|
|
879
|
+
return { type: "unknown" };
|
|
880
|
+
case "TSArrayType":
|
|
881
|
+
return { type: "list" };
|
|
882
|
+
case "TSTypeReference": {
|
|
883
|
+
if (tsType.typeName.type !== "Identifier") return { type: "unknown" };
|
|
884
|
+
const name = tsType.typeName.name;
|
|
885
|
+
if (name === "Date") return { type: "date", format: "iso-date" };
|
|
886
|
+
if (name === "Array") return { type: "list" };
|
|
887
|
+
if (name === "Record") return { type: "object" };
|
|
888
|
+
if (knownEntities.has(name)) return { type: "ref", refTo: name };
|
|
889
|
+
return { type: "unknown" };
|
|
890
|
+
}
|
|
891
|
+
case "TSUnionType": {
|
|
892
|
+
const nonNull = tsType.types.filter(
|
|
893
|
+
(t) => t.type !== "TSNullKeyword" && t.type !== "TSUndefinedKeyword"
|
|
894
|
+
);
|
|
895
|
+
const allStringLiterals = nonNull.every((t) => t.type === "TSLiteralType" && t.literal.type === "StringLiteral");
|
|
896
|
+
if (allStringLiterals && nonNull.length > 0) {
|
|
897
|
+
const values = nonNull.filter((t) => t.type === "TSLiteralType").map((t) => t.literal.type === "StringLiteral" ? t.literal.value : null).filter((v) => v !== null);
|
|
898
|
+
return { type: "enum", enumValues: values };
|
|
899
|
+
}
|
|
900
|
+
if (nonNull.length === 1) return mapTsType(nonNull[0], knownEntities);
|
|
901
|
+
return { type: "unknown" };
|
|
902
|
+
}
|
|
903
|
+
case "TSLiteralType":
|
|
904
|
+
if (tsType.literal.type === "StringLiteral") return { type: "string" };
|
|
905
|
+
if (tsType.literal.type === "NumericLiteral") return { type: "number" };
|
|
906
|
+
if (tsType.literal.type === "BooleanLiteral") return { type: "boolean" };
|
|
907
|
+
return { type: "unknown" };
|
|
908
|
+
case "TSTypeLiteral":
|
|
909
|
+
return { type: "object" };
|
|
910
|
+
default:
|
|
911
|
+
return { type: "unknown" };
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
function interfaceMembers(node) {
|
|
915
|
+
return node.body.body;
|
|
916
|
+
}
|
|
917
|
+
function typeAliasMembers(node) {
|
|
918
|
+
const annotation = node.typeAnnotation;
|
|
919
|
+
if (annotation.type !== "TSTypeLiteral") return null;
|
|
920
|
+
return annotation.members;
|
|
921
|
+
}
|
|
922
|
+
function extractFieldsFromMembers(members, knownEntities) {
|
|
923
|
+
const fields = [];
|
|
924
|
+
const refs = [];
|
|
925
|
+
for (const member of members) {
|
|
926
|
+
if (member.type !== "TSPropertySignature") continue;
|
|
927
|
+
const sig = member;
|
|
928
|
+
if (sig.key.type !== "Identifier") continue;
|
|
929
|
+
const fieldName = sig.key.name;
|
|
930
|
+
const required = sig.optional !== true;
|
|
931
|
+
const annotation = sig.typeAnnotation;
|
|
932
|
+
if (!annotation || annotation.type !== "TSTypeAnnotation") {
|
|
933
|
+
fields.push({ name: fieldName, type: "unknown", required });
|
|
934
|
+
continue;
|
|
935
|
+
}
|
|
936
|
+
const tsType = annotation.typeAnnotation;
|
|
937
|
+
const mapped = mapTsType(tsType, knownEntities);
|
|
938
|
+
if (mapped.type === "list" && tsType.type === "TSArrayType") {
|
|
939
|
+
const elementType = tsType.elementType;
|
|
940
|
+
const inner = mapTsType(elementType, knownEntities);
|
|
941
|
+
if (inner.type === "ref" && inner.refTo) {
|
|
942
|
+
fields.push({ name: fieldName, type: "ref", refTo: inner.refTo, required });
|
|
943
|
+
refs.push({ field: fieldName, to: inner.refTo, isList: true });
|
|
944
|
+
continue;
|
|
945
|
+
}
|
|
946
|
+
fields.push({ name: fieldName, type: "list", required });
|
|
947
|
+
continue;
|
|
948
|
+
}
|
|
949
|
+
if (mapped.type === "ref" && mapped.refTo) {
|
|
950
|
+
fields.push({ name: fieldName, type: "ref", refTo: mapped.refTo, required });
|
|
951
|
+
refs.push({ field: fieldName, to: mapped.refTo, isList: false });
|
|
952
|
+
continue;
|
|
953
|
+
}
|
|
954
|
+
const inferredFormat = inferFormatFromName5(fieldName, mapped.type);
|
|
955
|
+
const finalFormat = mapped.format ?? inferredFormat;
|
|
956
|
+
const field = { name: fieldName, type: mapped.type, required };
|
|
957
|
+
if (finalFormat) field.format = finalFormat;
|
|
958
|
+
if (mapped.enumValues) field.enum = mapped.enumValues;
|
|
959
|
+
fields.push(field);
|
|
960
|
+
}
|
|
961
|
+
return { fields, refs };
|
|
962
|
+
}
|
|
963
|
+
function parseTsTypesContent(content, filePath) {
|
|
964
|
+
let ast;
|
|
965
|
+
try {
|
|
966
|
+
ast = babelParse4(content, { sourceType: "module", plugins: ["typescript", "jsx"] });
|
|
967
|
+
} catch {
|
|
968
|
+
return { entities: [], relations: [] };
|
|
969
|
+
}
|
|
970
|
+
const candidates = [];
|
|
971
|
+
traverse4(ast, {
|
|
972
|
+
TSInterfaceDeclaration(path) {
|
|
973
|
+
const name = path.node.id.name;
|
|
974
|
+
candidates.push({ name, filePath, members: interfaceMembers(path.node) });
|
|
975
|
+
},
|
|
976
|
+
TSTypeAliasDeclaration(path) {
|
|
977
|
+
const name = path.node.id.name;
|
|
978
|
+
const members = typeAliasMembers(path.node);
|
|
979
|
+
if (!members) return;
|
|
980
|
+
candidates.push({ name, filePath, members });
|
|
981
|
+
}
|
|
982
|
+
});
|
|
983
|
+
const knownEntities = new Set(candidates.map((c) => c.name));
|
|
984
|
+
const entities = [];
|
|
985
|
+
const relations = [];
|
|
986
|
+
for (const candidate of candidates) {
|
|
987
|
+
const { fields, refs } = extractFieldsFromMembers(candidate.members, knownEntities);
|
|
988
|
+
if (fields.length === 0) continue;
|
|
989
|
+
const identifiers = fields.filter((f) => f.name === "id" || f.format === "uuid").map((f) => f.name).slice(0, 1);
|
|
990
|
+
entities.push({
|
|
991
|
+
name: candidate.name,
|
|
992
|
+
source: "ts",
|
|
993
|
+
filePath: candidate.filePath,
|
|
994
|
+
fields,
|
|
995
|
+
...identifiers.length > 0 ? { identifiers } : {}
|
|
996
|
+
});
|
|
997
|
+
for (const ref of refs) {
|
|
998
|
+
relations.push({
|
|
999
|
+
from: candidate.name,
|
|
1000
|
+
to: ref.to,
|
|
1001
|
+
cardinality: ref.isList ? "1:N" : "1:1",
|
|
1002
|
+
field: ref.field
|
|
1003
|
+
});
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
return { entities, relations };
|
|
1007
|
+
}
|
|
1008
|
+
var TS_TYPE_GLOBS = [
|
|
1009
|
+
"src/models/**/*.{ts,tsx}",
|
|
1010
|
+
"models/**/*.{ts,tsx}",
|
|
1011
|
+
"src/types/**/*.{ts,tsx}",
|
|
1012
|
+
"types/**/*.{ts,tsx}",
|
|
1013
|
+
"src/schemas/**/*.{ts,tsx}",
|
|
1014
|
+
"schemas/**/*.{ts,tsx}",
|
|
1015
|
+
"src/entities/**/*.{ts,tsx}",
|
|
1016
|
+
"entities/**/*.{ts,tsx}",
|
|
1017
|
+
"src/domain/**/*.{ts,tsx}",
|
|
1018
|
+
"domain/**/*.{ts,tsx}"
|
|
1019
|
+
];
|
|
1020
|
+
var COMMON_UTILITY_NAMES = /* @__PURE__ */ new Set([
|
|
1021
|
+
"Props",
|
|
1022
|
+
"State",
|
|
1023
|
+
"Config",
|
|
1024
|
+
"Options",
|
|
1025
|
+
"Params",
|
|
1026
|
+
"Query",
|
|
1027
|
+
"Context",
|
|
1028
|
+
"Theme",
|
|
1029
|
+
"Variant"
|
|
1030
|
+
]);
|
|
1031
|
+
function looksLikeEntityName(name) {
|
|
1032
|
+
if (!/^[A-Z]/.test(name)) return false;
|
|
1033
|
+
if (COMMON_UTILITY_NAMES.has(name)) return false;
|
|
1034
|
+
if (name.endsWith("Props") || name.endsWith("State") || name.endsWith("Config")) return false;
|
|
1035
|
+
return true;
|
|
1036
|
+
}
|
|
1037
|
+
async function parseTsTypes(rootDir) {
|
|
1038
|
+
const files = await fg4(TS_TYPE_GLOBS, {
|
|
1039
|
+
cwd: rootDir,
|
|
1040
|
+
absolute: true,
|
|
1041
|
+
ignore: ["node_modules/**", "dist/**", "build/**", ".next/**", "**/*.test.ts", "**/*.spec.ts"]
|
|
1042
|
+
});
|
|
1043
|
+
const allEntities = [];
|
|
1044
|
+
const allRelations = [];
|
|
1045
|
+
const seenEntities = /* @__PURE__ */ new Set();
|
|
1046
|
+
for (const file of files) {
|
|
1047
|
+
let content;
|
|
1048
|
+
try {
|
|
1049
|
+
content = await readFile5(file, "utf-8");
|
|
1050
|
+
} catch {
|
|
1051
|
+
continue;
|
|
1052
|
+
}
|
|
1053
|
+
const result = parseTsTypesContent(content, relative5(rootDir, file));
|
|
1054
|
+
for (const entity of result.entities) {
|
|
1055
|
+
if (!looksLikeEntityName(entity.name)) continue;
|
|
1056
|
+
if (seenEntities.has(entity.name)) continue;
|
|
1057
|
+
seenEntities.add(entity.name);
|
|
1058
|
+
allEntities.push(entity);
|
|
1059
|
+
}
|
|
1060
|
+
for (const relation of result.relations) {
|
|
1061
|
+
if (!looksLikeEntityName(relation.from) || !looksLikeEntityName(relation.to)) continue;
|
|
1062
|
+
allRelations.push(relation);
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
return { entities: allEntities, relations: allRelations };
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
// src/discovery/domain/graphql.ts
|
|
1069
|
+
import { readFile as readFile6 } from "fs/promises";
|
|
1070
|
+
import { relative as relative6 } from "path";
|
|
1071
|
+
import fg5 from "fast-glob";
|
|
1072
|
+
var SCALAR_MAP = {
|
|
1073
|
+
String: { type: "string" },
|
|
1074
|
+
Int: { type: "number", format: "integer" },
|
|
1075
|
+
Float: { type: "number", format: "decimal" },
|
|
1076
|
+
Boolean: { type: "boolean" },
|
|
1077
|
+
ID: { type: "string", format: "uuid" },
|
|
1078
|
+
DateTime: { type: "date", format: "iso-date" },
|
|
1079
|
+
Date: { type: "date", format: "iso-date" },
|
|
1080
|
+
Email: { type: "string", format: "email" },
|
|
1081
|
+
URL: { type: "string", format: "url" },
|
|
1082
|
+
UUID: { type: "string", format: "uuid" },
|
|
1083
|
+
JSON: { type: "object" }
|
|
1084
|
+
};
|
|
1085
|
+
function extractTypeBlocks(sdl) {
|
|
1086
|
+
const result = [];
|
|
1087
|
+
const pattern = /\btype\s+([A-Z]\w*)(?:\s+implements\s+[\w\s&,]+)?\s*\{([^}]*)\}/g;
|
|
1088
|
+
let match;
|
|
1089
|
+
while ((match = pattern.exec(sdl)) !== null) {
|
|
1090
|
+
result.push({ name: match[1], rawBody: match[2] });
|
|
1091
|
+
}
|
|
1092
|
+
return result;
|
|
1093
|
+
}
|
|
1094
|
+
function extractEnumBlocks(sdl) {
|
|
1095
|
+
const result = [];
|
|
1096
|
+
const pattern = /\benum\s+([A-Z]\w*)\s*\{([^}]*)\}/g;
|
|
1097
|
+
let match;
|
|
1098
|
+
while ((match = pattern.exec(sdl)) !== null) {
|
|
1099
|
+
const values = match[2].split("\n").map((l) => l.trim()).filter((l) => l && !l.startsWith("#") && !l.startsWith('"')).map((l) => l.split(/\s+/)[0]).filter(Boolean);
|
|
1100
|
+
result.push({ name: match[1], values });
|
|
1101
|
+
}
|
|
1102
|
+
return result;
|
|
1103
|
+
}
|
|
1104
|
+
function stripComments(block) {
|
|
1105
|
+
return block.split("\n").filter((l) => !l.trim().startsWith("#") && !l.trim().startsWith('"""')).join("\n");
|
|
1106
|
+
}
|
|
1107
|
+
function parseFieldType(raw) {
|
|
1108
|
+
let s = raw.trim();
|
|
1109
|
+
let required = false;
|
|
1110
|
+
if (s.endsWith("!")) {
|
|
1111
|
+
required = true;
|
|
1112
|
+
s = s.slice(0, -1);
|
|
1113
|
+
}
|
|
1114
|
+
if (s.startsWith("[") && s.endsWith("]")) {
|
|
1115
|
+
const inner = s.slice(1, -1).trim();
|
|
1116
|
+
const innerRequired = inner.endsWith("!");
|
|
1117
|
+
const innerName = innerRequired ? inner.slice(0, -1).trim() : inner;
|
|
1118
|
+
return { typeName: innerName, isList: true, required };
|
|
1119
|
+
}
|
|
1120
|
+
return { typeName: s, isList: false, required };
|
|
1121
|
+
}
|
|
1122
|
+
function inferFormatFromName6(fieldName, type) {
|
|
1123
|
+
if (type !== "string") return void 0;
|
|
1124
|
+
const n = fieldName.toLowerCase();
|
|
1125
|
+
if (n === "email" || n.endsWith("email")) return "email";
|
|
1126
|
+
if (n === "url" || n.endsWith("url") || n.endsWith("uri")) return "url";
|
|
1127
|
+
if (n === "phone" || n.includes("phone")) return "phone";
|
|
1128
|
+
return void 0;
|
|
1129
|
+
}
|
|
1130
|
+
var INTROSPECTION_TYPES = /* @__PURE__ */ new Set(["Query", "Mutation", "Subscription", "Schema"]);
|
|
1131
|
+
function parseGraphQlContent(content, filePath) {
|
|
1132
|
+
const typeBlocks = extractTypeBlocks(content);
|
|
1133
|
+
const enumBlocks = extractEnumBlocks(content);
|
|
1134
|
+
const enumsByName = new Map(enumBlocks.map((e) => [e.name, e.values]));
|
|
1135
|
+
const typeNames = new Set(typeBlocks.map((t) => t.name));
|
|
1136
|
+
const entities = [];
|
|
1137
|
+
const relations = [];
|
|
1138
|
+
for (const block of typeBlocks) {
|
|
1139
|
+
if (INTROSPECTION_TYPES.has(block.name)) continue;
|
|
1140
|
+
const fields = [];
|
|
1141
|
+
const identifiers = [];
|
|
1142
|
+
const lines = stripComments(block.rawBody).split("\n").map((l) => l.trim()).filter(Boolean);
|
|
1143
|
+
for (const line of lines) {
|
|
1144
|
+
const colon = line.indexOf(":");
|
|
1145
|
+
if (colon < 0) continue;
|
|
1146
|
+
const namePart = line.slice(0, colon).trim();
|
|
1147
|
+
const typePart = line.slice(colon + 1).trim();
|
|
1148
|
+
const fieldName = namePart.replace(/\(.*/, "").trim();
|
|
1149
|
+
if (!fieldName || !/^[a-zA-Z_]\w*$/.test(fieldName)) continue;
|
|
1150
|
+
const cleanTypePart = typePart.replace(/@[\w]+(\([^)]*\))?/g, "").trim();
|
|
1151
|
+
const parsed = parseFieldType(cleanTypePart);
|
|
1152
|
+
if (enumsByName.has(parsed.typeName)) {
|
|
1153
|
+
fields.push({
|
|
1154
|
+
name: fieldName,
|
|
1155
|
+
type: "enum",
|
|
1156
|
+
required: parsed.required,
|
|
1157
|
+
enum: enumsByName.get(parsed.typeName)
|
|
1158
|
+
});
|
|
1159
|
+
continue;
|
|
1160
|
+
}
|
|
1161
|
+
if (typeNames.has(parsed.typeName)) {
|
|
1162
|
+
fields.push({
|
|
1163
|
+
name: fieldName,
|
|
1164
|
+
type: "ref",
|
|
1165
|
+
refTo: parsed.typeName,
|
|
1166
|
+
required: parsed.required
|
|
1167
|
+
});
|
|
1168
|
+
relations.push({
|
|
1169
|
+
from: block.name,
|
|
1170
|
+
to: parsed.typeName,
|
|
1171
|
+
cardinality: parsed.isList ? "1:N" : "1:1",
|
|
1172
|
+
field: fieldName
|
|
1173
|
+
});
|
|
1174
|
+
continue;
|
|
1175
|
+
}
|
|
1176
|
+
const scalar = SCALAR_MAP[parsed.typeName];
|
|
1177
|
+
if (scalar) {
|
|
1178
|
+
const inferredFormat = inferFormatFromName6(fieldName, scalar.type);
|
|
1179
|
+
const field = {
|
|
1180
|
+
name: fieldName,
|
|
1181
|
+
type: parsed.isList ? "list" : scalar.type,
|
|
1182
|
+
required: parsed.required
|
|
1183
|
+
};
|
|
1184
|
+
const format = inferredFormat ?? scalar.format;
|
|
1185
|
+
if (format) field.format = format;
|
|
1186
|
+
fields.push(field);
|
|
1187
|
+
if (parsed.typeName === "ID") identifiers.push(fieldName);
|
|
1188
|
+
continue;
|
|
1189
|
+
}
|
|
1190
|
+
fields.push({
|
|
1191
|
+
name: fieldName,
|
|
1192
|
+
type: parsed.isList ? "list" : "unknown",
|
|
1193
|
+
required: parsed.required
|
|
1194
|
+
});
|
|
1195
|
+
}
|
|
1196
|
+
if (fields.length === 0) continue;
|
|
1197
|
+
entities.push({
|
|
1198
|
+
name: block.name,
|
|
1199
|
+
source: "graphql",
|
|
1200
|
+
filePath,
|
|
1201
|
+
fields,
|
|
1202
|
+
...identifiers.length > 0 ? { identifiers } : {}
|
|
1203
|
+
});
|
|
1204
|
+
}
|
|
1205
|
+
return { entities, relations };
|
|
1206
|
+
}
|
|
1207
|
+
var GRAPHQL_GLOBS = [
|
|
1208
|
+
"**/*.graphql",
|
|
1209
|
+
"**/*.gql",
|
|
1210
|
+
"schema.{ts,js}",
|
|
1211
|
+
"src/schema.{ts,js}",
|
|
1212
|
+
"src/graphql/**/*.{ts,js}",
|
|
1213
|
+
"graphql/**/*.{ts,js}"
|
|
1214
|
+
];
|
|
1215
|
+
function extractEmbeddedSdl(jsContent) {
|
|
1216
|
+
const chunks = [];
|
|
1217
|
+
const pattern = /(?:gql|graphql)\s*`([^`]*)`/g;
|
|
1218
|
+
let match;
|
|
1219
|
+
while ((match = pattern.exec(jsContent)) !== null) {
|
|
1220
|
+
chunks.push(match[1]);
|
|
1221
|
+
}
|
|
1222
|
+
return chunks.length > 0 ? chunks.join("\n") : null;
|
|
1223
|
+
}
|
|
1224
|
+
async function parseGraphQlSchemas(rootDir) {
|
|
1225
|
+
const files = await fg5(GRAPHQL_GLOBS, {
|
|
1226
|
+
cwd: rootDir,
|
|
1227
|
+
absolute: true,
|
|
1228
|
+
ignore: ["node_modules/**", "dist/**", "build/**"]
|
|
1229
|
+
});
|
|
1230
|
+
const allEntities = [];
|
|
1231
|
+
const allRelations = [];
|
|
1232
|
+
const seenEntities = /* @__PURE__ */ new Set();
|
|
1233
|
+
for (const file of files) {
|
|
1234
|
+
let content;
|
|
1235
|
+
try {
|
|
1236
|
+
content = await readFile6(file, "utf-8");
|
|
1237
|
+
} catch {
|
|
1238
|
+
continue;
|
|
1239
|
+
}
|
|
1240
|
+
const sdl = file.endsWith(".graphql") || file.endsWith(".gql") ? content : extractEmbeddedSdl(content);
|
|
1241
|
+
if (!sdl) continue;
|
|
1242
|
+
const result = parseGraphQlContent(sdl, relative6(rootDir, file));
|
|
1243
|
+
for (const entity of result.entities) {
|
|
1244
|
+
if (!seenEntities.has(entity.name)) {
|
|
1245
|
+
seenEntities.add(entity.name);
|
|
1246
|
+
allEntities.push(entity);
|
|
1247
|
+
}
|
|
1248
|
+
}
|
|
1249
|
+
allRelations.push(...result.relations);
|
|
1250
|
+
}
|
|
1251
|
+
return { entities: allEntities, relations: allRelations };
|
|
1252
|
+
}
|
|
1253
|
+
|
|
1254
|
+
// src/discovery/domain/openapi.ts
|
|
1255
|
+
import { readFile as readFile7 } from "fs/promises";
|
|
1256
|
+
import { join as join2, relative as relative7 } from "path";
|
|
1257
|
+
var HTTP_METHODS = ["GET", "POST", "PUT", "PATCH", "DELETE"];
|
|
1258
|
+
var OPENAPI_FORMAT_MAP = {
|
|
1259
|
+
email: "email",
|
|
1260
|
+
uri: "url",
|
|
1261
|
+
url: "url",
|
|
1262
|
+
uuid: "uuid",
|
|
1263
|
+
"date-time": "iso-date",
|
|
1264
|
+
date: "iso-date",
|
|
1265
|
+
password: "password",
|
|
1266
|
+
int32: "integer",
|
|
1267
|
+
int64: "integer",
|
|
1268
|
+
float: "decimal",
|
|
1269
|
+
double: "decimal"
|
|
1270
|
+
};
|
|
1271
|
+
function refToEntityName(ref) {
|
|
1272
|
+
const match = /#\/components\/schemas\/([A-Z]\w+)/.exec(ref) ?? /#\/definitions\/([A-Z]\w+)/.exec(ref);
|
|
1273
|
+
return match ? match[1] : null;
|
|
1274
|
+
}
|
|
1275
|
+
function mapSchemaToField(fieldName, schema, required) {
|
|
1276
|
+
if (schema.$ref) {
|
|
1277
|
+
const refName = refToEntityName(schema.$ref);
|
|
1278
|
+
if (refName) {
|
|
1279
|
+
return { name: fieldName, type: "ref", refTo: refName, required };
|
|
1280
|
+
}
|
|
1281
|
+
}
|
|
1282
|
+
if (schema.enum) {
|
|
1283
|
+
const values = schema.enum.filter((v) => typeof v === "string");
|
|
1284
|
+
return { name: fieldName, type: "enum", required, enum: values };
|
|
1285
|
+
}
|
|
1286
|
+
if (schema.type === "array") {
|
|
1287
|
+
if (schema.items?.$ref) {
|
|
1288
|
+
const refName = refToEntityName(schema.items.$ref);
|
|
1289
|
+
if (refName) {
|
|
1290
|
+
return { name: fieldName, type: "ref", refTo: refName, required };
|
|
1291
|
+
}
|
|
1292
|
+
}
|
|
1293
|
+
return { name: fieldName, type: "list", required };
|
|
1294
|
+
}
|
|
1295
|
+
const baseType = schemaTypeToFieldType(schema.type);
|
|
1296
|
+
const format = schema.format ? OPENAPI_FORMAT_MAP[schema.format] : void 0;
|
|
1297
|
+
const field = { name: fieldName, type: baseType, required };
|
|
1298
|
+
if (format) field.format = format;
|
|
1299
|
+
return field;
|
|
1300
|
+
}
|
|
1301
|
+
function schemaTypeToFieldType(openApiType) {
|
|
1302
|
+
switch (openApiType) {
|
|
1303
|
+
case "string":
|
|
1304
|
+
return "string";
|
|
1305
|
+
case "integer":
|
|
1306
|
+
case "number":
|
|
1307
|
+
return "number";
|
|
1308
|
+
case "boolean":
|
|
1309
|
+
return "boolean";
|
|
1310
|
+
case "object":
|
|
1311
|
+
return "object";
|
|
1312
|
+
case "array":
|
|
1313
|
+
return "list";
|
|
1314
|
+
default:
|
|
1315
|
+
return "unknown";
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
function schemaToEntity(name, schema, filePath) {
|
|
1319
|
+
if (!schema.properties) return null;
|
|
1320
|
+
const requiredSet = new Set(schema.required ?? []);
|
|
1321
|
+
const fields = [];
|
|
1322
|
+
for (const [fieldName, fieldSchema] of Object.entries(schema.properties)) {
|
|
1323
|
+
fields.push(mapSchemaToField(fieldName, fieldSchema, requiredSet.has(fieldName)));
|
|
1324
|
+
}
|
|
1325
|
+
if (fields.length === 0) return null;
|
|
1326
|
+
const identifiers = fields.filter((f) => f.name === "id" || f.format === "uuid").map((f) => f.name).slice(0, 1);
|
|
1327
|
+
return {
|
|
1328
|
+
name,
|
|
1329
|
+
source: "openapi",
|
|
1330
|
+
filePath,
|
|
1331
|
+
fields,
|
|
1332
|
+
...identifiers.length > 0 ? { identifiers } : {}
|
|
1333
|
+
};
|
|
1334
|
+
}
|
|
1335
|
+
function extractRequestBodyEntityName(method) {
|
|
1336
|
+
const content = method.requestBody?.content;
|
|
1337
|
+
if (!content) return void 0;
|
|
1338
|
+
for (const mediaType of Object.values(content)) {
|
|
1339
|
+
const schema = mediaType.schema;
|
|
1340
|
+
if (!schema) continue;
|
|
1341
|
+
if (schema.$ref) {
|
|
1342
|
+
const name = refToEntityName(schema.$ref);
|
|
1343
|
+
if (name) return name;
|
|
1344
|
+
}
|
|
1345
|
+
}
|
|
1346
|
+
return void 0;
|
|
1347
|
+
}
|
|
1348
|
+
function hasAuthRequirement(method, global) {
|
|
1349
|
+
if (Array.isArray(method.security) && method.security.length > 0) return true;
|
|
1350
|
+
if (Array.isArray(global) && global.length > 0 && method.security === void 0) return true;
|
|
1351
|
+
return false;
|
|
1352
|
+
}
|
|
1353
|
+
function parseOpenApiDocument(doc, filePath) {
|
|
1354
|
+
const entities = [];
|
|
1355
|
+
const endpoints = [];
|
|
1356
|
+
const schemas = doc.components?.schemas ?? doc.definitions ?? {};
|
|
1357
|
+
for (const [name, schema] of Object.entries(schemas)) {
|
|
1358
|
+
const entity = schemaToEntity(name, schema, filePath);
|
|
1359
|
+
if (entity) entities.push(entity);
|
|
1360
|
+
}
|
|
1361
|
+
if (doc.paths) {
|
|
1362
|
+
for (const [path, methods] of Object.entries(doc.paths)) {
|
|
1363
|
+
for (const [methodLower, spec] of Object.entries(methods)) {
|
|
1364
|
+
const method = methodLower.toUpperCase();
|
|
1365
|
+
if (!HTTP_METHODS.includes(method)) continue;
|
|
1366
|
+
const bodySchema = extractRequestBodyEntityName(spec);
|
|
1367
|
+
endpoints.push({
|
|
1368
|
+
method,
|
|
1369
|
+
path,
|
|
1370
|
+
filePath,
|
|
1371
|
+
...bodySchema ? { bodySchema } : {},
|
|
1372
|
+
auth: hasAuthRequirement(spec, doc.security)
|
|
1373
|
+
});
|
|
1374
|
+
}
|
|
1375
|
+
}
|
|
1376
|
+
}
|
|
1377
|
+
return { entities, endpoints };
|
|
1378
|
+
}
|
|
1379
|
+
async function parseOpenApiSpec(rootDir) {
|
|
1380
|
+
const candidates = [
|
|
1381
|
+
"openapi.json",
|
|
1382
|
+
"swagger.json",
|
|
1383
|
+
"api/openapi.json",
|
|
1384
|
+
"api/swagger.json",
|
|
1385
|
+
"docs/openapi.json",
|
|
1386
|
+
"docs/swagger.json",
|
|
1387
|
+
"src/openapi.json"
|
|
1388
|
+
];
|
|
1389
|
+
for (const candidate of candidates) {
|
|
1390
|
+
const path = join2(rootDir, candidate);
|
|
1391
|
+
let content;
|
|
1392
|
+
try {
|
|
1393
|
+
content = await readFile7(path, "utf-8");
|
|
1394
|
+
} catch {
|
|
1395
|
+
continue;
|
|
1396
|
+
}
|
|
1397
|
+
let doc;
|
|
1398
|
+
try {
|
|
1399
|
+
doc = JSON.parse(content);
|
|
1400
|
+
} catch {
|
|
1401
|
+
continue;
|
|
1402
|
+
}
|
|
1403
|
+
return parseOpenApiDocument(doc, relative7(rootDir, path));
|
|
1404
|
+
}
|
|
1405
|
+
return { entities: [], endpoints: [] };
|
|
1406
|
+
}
|
|
1407
|
+
|
|
1408
|
+
// src/discovery/domain/forms.ts
|
|
1409
|
+
var INPUT_TAGS = /* @__PURE__ */ new Set(["input", "select", "textarea"]);
|
|
1410
|
+
var INTENT_KEYWORDS = {
|
|
1411
|
+
"sign-in": {
|
|
1412
|
+
fields: [],
|
|
1413
|
+
buttons: ["sign in", "log in", "login", "signin"],
|
|
1414
|
+
routes: ["/login", "/signin", "/sign-in", "/auth/login", "/auth/signin"]
|
|
1415
|
+
},
|
|
1416
|
+
"sign-up": {
|
|
1417
|
+
fields: [],
|
|
1418
|
+
buttons: ["sign up", "register", "create account", "get started", "join"],
|
|
1419
|
+
routes: ["/signup", "/sign-up", "/register", "/auth/register", "/auth/signup", "/join"]
|
|
1420
|
+
},
|
|
1421
|
+
"reset-password": {
|
|
1422
|
+
fields: [],
|
|
1423
|
+
buttons: ["reset password", "forgot password", "send reset link", "recover"],
|
|
1424
|
+
routes: ["/forgot-password", "/reset-password", "/auth/forgot", "/auth/reset", "/recover"]
|
|
1425
|
+
},
|
|
1426
|
+
search: {
|
|
1427
|
+
fields: ["search", "query", "q", "keywords", "term"],
|
|
1428
|
+
buttons: ["search", "find", "lookup"],
|
|
1429
|
+
routes: ["/search", "/results"]
|
|
1430
|
+
},
|
|
1431
|
+
checkout: {
|
|
1432
|
+
fields: ["card", "cardnumber", "card-number", "cvv", "cvc", "expiry", "exp-month", "expmonth", "shipping", "billing"],
|
|
1433
|
+
buttons: ["checkout", "pay", "place order", "buy now", "complete purchase", "confirm order"],
|
|
1434
|
+
routes: ["/checkout", "/cart/checkout", "/pay", "/payment"]
|
|
1435
|
+
},
|
|
1436
|
+
contact: {
|
|
1437
|
+
fields: ["message", "subject", "inquiry", "comments"],
|
|
1438
|
+
buttons: ["send message", "contact us", "send"],
|
|
1439
|
+
routes: ["/contact", "/support", "/help/contact"]
|
|
1440
|
+
},
|
|
1441
|
+
subscribe: {
|
|
1442
|
+
fields: ["newsletter", "subscribe"],
|
|
1443
|
+
buttons: ["subscribe", "sign me up", "join newsletter", "get updates"],
|
|
1444
|
+
routes: ["/subscribe", "/newsletter"]
|
|
1445
|
+
},
|
|
1446
|
+
create: {
|
|
1447
|
+
fields: [],
|
|
1448
|
+
buttons: ["create", "add", "new", "save"],
|
|
1449
|
+
routes: ["/new", "/create"]
|
|
1450
|
+
},
|
|
1451
|
+
update: {
|
|
1452
|
+
fields: [],
|
|
1453
|
+
buttons: ["update", "save changes", "apply"],
|
|
1454
|
+
routes: ["/edit", "/settings"]
|
|
1455
|
+
},
|
|
1456
|
+
unknown: { fields: [], buttons: [], routes: [] }
|
|
1457
|
+
};
|
|
1458
|
+
function tokenize(s) {
|
|
1459
|
+
return s.toLowerCase().split(/[^a-z0-9]+/).filter(Boolean);
|
|
1460
|
+
}
|
|
1461
|
+
function includesPhrase(haystack, needle) {
|
|
1462
|
+
return haystack.toLowerCase().includes(needle.toLowerCase());
|
|
1463
|
+
}
|
|
1464
|
+
function scoreIntent(intent, signals) {
|
|
1465
|
+
if (intent === "unknown") return 0;
|
|
1466
|
+
const keys = INTENT_KEYWORDS[intent];
|
|
1467
|
+
let score = 0;
|
|
1468
|
+
for (const field of signals.fieldNames) {
|
|
1469
|
+
const tokens = new Set(tokenize(field));
|
|
1470
|
+
for (const fieldKey of keys.fields) {
|
|
1471
|
+
const kTokens = tokenize(fieldKey);
|
|
1472
|
+
const matchesAll = kTokens.every((t) => tokens.has(t));
|
|
1473
|
+
if (matchesAll) score += 3;
|
|
1474
|
+
}
|
|
1475
|
+
}
|
|
1476
|
+
const allButtonText = signals.buttonTexts.join(" ");
|
|
1477
|
+
for (const phrase of keys.buttons) {
|
|
1478
|
+
if (includesPhrase(allButtonText, phrase)) score += 2;
|
|
1479
|
+
}
|
|
1480
|
+
if (signals.routePath) {
|
|
1481
|
+
for (const routePattern of keys.routes) {
|
|
1482
|
+
if (signals.routePath === routePattern || signals.routePath.startsWith(routePattern + "/")) {
|
|
1483
|
+
score += 4;
|
|
1484
|
+
}
|
|
1485
|
+
}
|
|
1486
|
+
}
|
|
1487
|
+
return score;
|
|
1488
|
+
}
|
|
1489
|
+
function hasField(fieldNames, token) {
|
|
1490
|
+
const needle = token.toLowerCase();
|
|
1491
|
+
return fieldNames.some((n) => tokenize(n).includes(needle));
|
|
1492
|
+
}
|
|
1493
|
+
function applyStructuralBoost(intent, signals, baseScore) {
|
|
1494
|
+
let score = baseScore;
|
|
1495
|
+
const hasPassword = hasField(signals.fieldNames, "password");
|
|
1496
|
+
const hasEmail = hasField(signals.fieldNames, "email");
|
|
1497
|
+
const hasName = hasField(signals.fieldNames, "name") || hasField(signals.fieldNames, "firstname") || hasField(signals.fieldNames, "lastname") || hasField(signals.fieldNames, "username");
|
|
1498
|
+
if (intent === "sign-in" && hasPassword && hasEmail) score += 3;
|
|
1499
|
+
if (intent === "sign-up" && hasPassword && hasEmail && hasName) score += 4;
|
|
1500
|
+
if (intent === "reset-password" && hasEmail && !hasPassword) score += 2;
|
|
1501
|
+
if (intent === "reset-password" && hasPassword && !hasEmail) score += 3;
|
|
1502
|
+
return score;
|
|
1503
|
+
}
|
|
1504
|
+
function classifyIntent(signals) {
|
|
1505
|
+
const intents = [
|
|
1506
|
+
"sign-in",
|
|
1507
|
+
"sign-up",
|
|
1508
|
+
"reset-password",
|
|
1509
|
+
"search",
|
|
1510
|
+
"checkout",
|
|
1511
|
+
"contact",
|
|
1512
|
+
"subscribe",
|
|
1513
|
+
"create",
|
|
1514
|
+
"update"
|
|
1515
|
+
];
|
|
1516
|
+
let best = "unknown";
|
|
1517
|
+
let bestScore = 0;
|
|
1518
|
+
for (const intent of intents) {
|
|
1519
|
+
let score = scoreIntent(intent, signals);
|
|
1520
|
+
score = applyStructuralBoost(intent, signals, score);
|
|
1521
|
+
if (score > bestScore) {
|
|
1522
|
+
best = intent;
|
|
1523
|
+
bestScore = score;
|
|
1524
|
+
}
|
|
1525
|
+
}
|
|
1526
|
+
if (bestScore === 0) return { intent: "unknown", confidence: 0 };
|
|
1527
|
+
const confidence = Math.min(bestScore / 10, 1);
|
|
1528
|
+
return { intent: best, confidence };
|
|
1529
|
+
}
|
|
1530
|
+
function matchRouteForFile(filePath, routes) {
|
|
1531
|
+
const direct = routes.find((r) => r.filePath === filePath);
|
|
1532
|
+
if (direct) return direct;
|
|
1533
|
+
const byDir = routes.find((r) => filePath.startsWith(r.filePath.replace(/\/[^/]+$/, "")));
|
|
1534
|
+
if (byDir) return byDir;
|
|
1535
|
+
return null;
|
|
1536
|
+
}
|
|
1537
|
+
function toFormField(element) {
|
|
1538
|
+
if (!INPUT_TAGS.has(element.tag)) return null;
|
|
1539
|
+
const name = element.name ?? element.testId ?? element.id ?? element.ariaLabel ?? "";
|
|
1540
|
+
if (!name) return null;
|
|
1541
|
+
return {
|
|
1542
|
+
name,
|
|
1543
|
+
type: element.type ?? element.tag,
|
|
1544
|
+
required: false,
|
|
1545
|
+
...element.testId ? { selector: `[data-testid="${element.testId}"]` } : {},
|
|
1546
|
+
...element.ariaLabel ? { ariaLabel: element.ariaLabel } : {}
|
|
1547
|
+
};
|
|
1548
|
+
}
|
|
1549
|
+
function extractButtonTexts(component) {
|
|
1550
|
+
return component.elements.filter((e) => e.tag === "button" || e.tag === "input" && e.type === "submit").map((e) => e.text ?? e.ariaLabel ?? e.name ?? "").filter(Boolean);
|
|
1551
|
+
}
|
|
1552
|
+
function extractForms(components, routes) {
|
|
1553
|
+
const forms = [];
|
|
1554
|
+
for (const component of components) {
|
|
1555
|
+
const inputFields = component.elements.map(toFormField).filter((f) => f !== null);
|
|
1556
|
+
if (inputFields.length === 0) continue;
|
|
1557
|
+
const matchedRoute = matchRouteForFile(component.filePath, routes);
|
|
1558
|
+
const fieldNames = inputFields.map((f) => f.name);
|
|
1559
|
+
const buttonTexts = extractButtonTexts(component);
|
|
1560
|
+
const signals = {
|
|
1561
|
+
fieldNames,
|
|
1562
|
+
buttonTexts,
|
|
1563
|
+
componentPath: component.filePath,
|
|
1564
|
+
routePath: matchedRoute?.path ?? null
|
|
1565
|
+
};
|
|
1566
|
+
const { intent, confidence } = classifyIntent(signals);
|
|
1567
|
+
forms.push({
|
|
1568
|
+
route: matchedRoute?.path ?? component.filePath,
|
|
1569
|
+
filePath: component.filePath,
|
|
1570
|
+
fields: inputFields,
|
|
1571
|
+
intent,
|
|
1572
|
+
intentConfidence: confidence
|
|
1573
|
+
});
|
|
1574
|
+
}
|
|
1575
|
+
return forms;
|
|
1576
|
+
}
|
|
1577
|
+
|
|
1578
|
+
// src/discovery/domain/vocab.ts
|
|
1579
|
+
import { readFile as readFile8 } from "fs/promises";
|
|
1580
|
+
import { join as join3 } from "path";
|
|
1581
|
+
var STOPWORDS = /* @__PURE__ */ new Set([
|
|
1582
|
+
"a",
|
|
1583
|
+
"an",
|
|
1584
|
+
"the",
|
|
1585
|
+
"and",
|
|
1586
|
+
"or",
|
|
1587
|
+
"but",
|
|
1588
|
+
"for",
|
|
1589
|
+
"with",
|
|
1590
|
+
"to",
|
|
1591
|
+
"in",
|
|
1592
|
+
"of",
|
|
1593
|
+
"on",
|
|
1594
|
+
"at",
|
|
1595
|
+
"by",
|
|
1596
|
+
"from",
|
|
1597
|
+
"as",
|
|
1598
|
+
"is",
|
|
1599
|
+
"are",
|
|
1600
|
+
"was",
|
|
1601
|
+
"were",
|
|
1602
|
+
"be",
|
|
1603
|
+
"been",
|
|
1604
|
+
"being",
|
|
1605
|
+
"have",
|
|
1606
|
+
"has",
|
|
1607
|
+
"had",
|
|
1608
|
+
"do",
|
|
1609
|
+
"does",
|
|
1610
|
+
"did",
|
|
1611
|
+
"will",
|
|
1612
|
+
"would",
|
|
1613
|
+
"should",
|
|
1614
|
+
"can",
|
|
1615
|
+
"could",
|
|
1616
|
+
"may",
|
|
1617
|
+
"might",
|
|
1618
|
+
"this",
|
|
1619
|
+
"that",
|
|
1620
|
+
"these",
|
|
1621
|
+
"those",
|
|
1622
|
+
"we",
|
|
1623
|
+
"you",
|
|
1624
|
+
"your",
|
|
1625
|
+
"our",
|
|
1626
|
+
"their",
|
|
1627
|
+
"its",
|
|
1628
|
+
"it",
|
|
1629
|
+
"they",
|
|
1630
|
+
"them",
|
|
1631
|
+
"us",
|
|
1632
|
+
"i",
|
|
1633
|
+
"my",
|
|
1634
|
+
"me",
|
|
1635
|
+
"src",
|
|
1636
|
+
"app",
|
|
1637
|
+
"page",
|
|
1638
|
+
"index",
|
|
1639
|
+
"pages",
|
|
1640
|
+
"components",
|
|
1641
|
+
"routes",
|
|
1642
|
+
"route",
|
|
1643
|
+
"api",
|
|
1644
|
+
"dist",
|
|
1645
|
+
"build",
|
|
1646
|
+
"public",
|
|
1647
|
+
"static"
|
|
1648
|
+
]);
|
|
1649
|
+
function tokenize2(source) {
|
|
1650
|
+
return source.toLowerCase().split(/[^a-z0-9]+/).filter((t) => t.length >= 2 && !STOPWORDS.has(t) && !/^\d+$/.test(t));
|
|
1651
|
+
}
|
|
1652
|
+
function dedupeAndRank(tokens) {
|
|
1653
|
+
const counts = /* @__PURE__ */ new Map();
|
|
1654
|
+
for (const t of tokens) counts.set(t, (counts.get(t) ?? 0) + 1);
|
|
1655
|
+
return Array.from(counts.entries()).sort((a, b) => b[1] - a[1]).map(([t]) => t);
|
|
1656
|
+
}
|
|
1657
|
+
function stripDynamicSegments(path) {
|
|
1658
|
+
return path.replace(/\[[^\]]+\]/g, "").replace(/:\w+/g, "");
|
|
1659
|
+
}
|
|
1660
|
+
function extractRouteVocabulary(routes) {
|
|
1661
|
+
const tokens = [];
|
|
1662
|
+
for (const route of routes) {
|
|
1663
|
+
tokens.push(...tokenize2(stripDynamicSegments(route.path)));
|
|
1664
|
+
}
|
|
1665
|
+
return dedupeAndRank(tokens);
|
|
1666
|
+
}
|
|
1667
|
+
function extractElementVocabulary(elements) {
|
|
1668
|
+
const tokens = [];
|
|
1669
|
+
for (const element of elements) {
|
|
1670
|
+
if (element.text) tokens.push(...tokenize2(element.text));
|
|
1671
|
+
if (element.ariaLabel) tokens.push(...tokenize2(element.ariaLabel));
|
|
1672
|
+
if (element.name) tokens.push(...tokenize2(element.name));
|
|
1673
|
+
}
|
|
1674
|
+
return dedupeAndRank(tokens);
|
|
1675
|
+
}
|
|
1676
|
+
async function extractDependencyVocabulary(rootDir) {
|
|
1677
|
+
try {
|
|
1678
|
+
const pkgRaw = await readFile8(join3(rootDir, "package.json"), "utf-8");
|
|
1679
|
+
const pkg = JSON.parse(pkgRaw);
|
|
1680
|
+
const deps = { ...pkg.dependencies ?? {}, ...pkg.devDependencies ?? {} };
|
|
1681
|
+
return Object.keys(deps).filter((name) => name.length > 0);
|
|
1682
|
+
} catch {
|
|
1683
|
+
return [];
|
|
1684
|
+
}
|
|
1685
|
+
}
|
|
1686
|
+
async function buildVocabulary(routes, elements, rootDir) {
|
|
1687
|
+
const [routeVocab, elementVocab, depVocab] = [
|
|
1688
|
+
extractRouteVocabulary(routes),
|
|
1689
|
+
extractElementVocabulary(elements),
|
|
1690
|
+
await extractDependencyVocabulary(rootDir)
|
|
1691
|
+
];
|
|
1692
|
+
return {
|
|
1693
|
+
routes: routeVocab,
|
|
1694
|
+
elements: elementVocab,
|
|
1695
|
+
deps: depVocab
|
|
1696
|
+
};
|
|
1697
|
+
}
|
|
1698
|
+
|
|
1699
|
+
// src/discovery/domain/index.ts
|
|
1700
|
+
var SOURCE_PRIORITY = [
|
|
1701
|
+
"prisma",
|
|
1702
|
+
"drizzle",
|
|
1703
|
+
"typeorm",
|
|
1704
|
+
"graphql",
|
|
1705
|
+
"openapi",
|
|
1706
|
+
"zod",
|
|
1707
|
+
"ts",
|
|
1708
|
+
"form"
|
|
1709
|
+
];
|
|
1710
|
+
function sourceRank(source) {
|
|
1711
|
+
const idx = SOURCE_PRIORITY.indexOf(source);
|
|
1712
|
+
return idx === -1 ? SOURCE_PRIORITY.length : idx;
|
|
1713
|
+
}
|
|
1714
|
+
function dedupeEntities(entities) {
|
|
1715
|
+
const byName = /* @__PURE__ */ new Map();
|
|
1716
|
+
for (const entity of entities) {
|
|
1717
|
+
const existing = byName.get(entity.name);
|
|
1718
|
+
if (!existing) {
|
|
1719
|
+
byName.set(entity.name, entity);
|
|
1720
|
+
continue;
|
|
1721
|
+
}
|
|
1722
|
+
if (sourceRank(entity.source) < sourceRank(existing.source)) {
|
|
1723
|
+
byName.set(entity.name, entity);
|
|
1724
|
+
}
|
|
1725
|
+
}
|
|
1726
|
+
return Array.from(byName.values());
|
|
1727
|
+
}
|
|
1728
|
+
function dedupeRelations(relations) {
|
|
1729
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1730
|
+
const out = [];
|
|
1731
|
+
for (const r of relations) {
|
|
1732
|
+
const key = `${r.from}::${r.to}::${r.field}::${r.cardinality}`;
|
|
1733
|
+
if (seen.has(key)) continue;
|
|
1734
|
+
seen.add(key);
|
|
1735
|
+
out.push(r);
|
|
1736
|
+
}
|
|
1737
|
+
return out;
|
|
1738
|
+
}
|
|
1739
|
+
async function computeAnalysisResult(rootDir) {
|
|
1740
|
+
const start = performance.now();
|
|
1741
|
+
const { detectFrameworks } = await import("./framework-detector-ISSM2Q2I.js");
|
|
1742
|
+
const { discoverRoutes } = await import("./route-discoverer-QBZCRQLJ.js");
|
|
1743
|
+
const { extractElements } = await import("./element-extractor-OZTFBVKZ.js");
|
|
1744
|
+
const frameworks = await detectFrameworks(rootDir);
|
|
1745
|
+
const routes = await discoverRoutes(rootDir, frameworks);
|
|
1746
|
+
const routeFilePaths = routes.map((r) => join4(rootDir, r.filePath)).filter(
|
|
1747
|
+
(f) => f.endsWith(".tsx") || f.endsWith(".jsx") || f.endsWith(".ts") || f.endsWith(".js") || f.endsWith(".vue")
|
|
1748
|
+
);
|
|
1749
|
+
const { elements, components } = await extractElements(routeFilePaths, rootDir);
|
|
1750
|
+
return {
|
|
1751
|
+
project: { name: basename(rootDir), frameworks, rootDir },
|
|
1752
|
+
routes,
|
|
1753
|
+
components,
|
|
1754
|
+
elements,
|
|
1755
|
+
metadata: {
|
|
1756
|
+
analyzedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1757
|
+
duration: performance.now() - start,
|
|
1758
|
+
fileCount: routeFilePaths.length,
|
|
1759
|
+
routeCount: routes.length,
|
|
1760
|
+
elementCount: elements.length
|
|
1761
|
+
}
|
|
1762
|
+
};
|
|
1763
|
+
}
|
|
1764
|
+
async function extractDomainSnapshot(rootDir, options = {}) {
|
|
1765
|
+
const start = performance.now();
|
|
1766
|
+
const analysis = options.analysis ?? await computeAnalysisResult(rootDir);
|
|
1767
|
+
const [prisma, drizzle, typeorm, zod, ts, graphql, openapi] = await Promise.all([
|
|
1768
|
+
parsePrismaSchema(rootDir),
|
|
1769
|
+
parseDrizzleSchemas(rootDir),
|
|
1770
|
+
parseTypeOrmEntities(rootDir),
|
|
1771
|
+
parseZodSchemas(rootDir),
|
|
1772
|
+
parseTsTypes(rootDir),
|
|
1773
|
+
parseGraphQlSchemas(rootDir),
|
|
1774
|
+
parseOpenApiSpec(rootDir)
|
|
1775
|
+
]);
|
|
1776
|
+
const allEntities = [
|
|
1777
|
+
...prisma.entities,
|
|
1778
|
+
...drizzle.entities,
|
|
1779
|
+
...typeorm.entities,
|
|
1780
|
+
...graphql.entities,
|
|
1781
|
+
...openapi.entities,
|
|
1782
|
+
...zod.entities,
|
|
1783
|
+
...ts.entities
|
|
1784
|
+
];
|
|
1785
|
+
const entities = dedupeEntities(allEntities);
|
|
1786
|
+
const allRelations = [
|
|
1787
|
+
...prisma.relations,
|
|
1788
|
+
...drizzle.relations,
|
|
1789
|
+
...typeorm.relations,
|
|
1790
|
+
...graphql.relations,
|
|
1791
|
+
...ts.relations
|
|
1792
|
+
];
|
|
1793
|
+
const entityNames = new Set(entities.map((e) => e.name));
|
|
1794
|
+
const relations = dedupeRelations(allRelations).filter(
|
|
1795
|
+
(r) => entityNames.has(r.from) && entityNames.has(r.to)
|
|
1796
|
+
);
|
|
1797
|
+
const apiEndpoints = [...openapi.endpoints];
|
|
1798
|
+
const forms = extractForms(analysis.components, analysis.routes);
|
|
1799
|
+
const vocabulary = await buildVocabulary(analysis.routes, analysis.elements, rootDir);
|
|
1800
|
+
return {
|
|
1801
|
+
project: analysis.project,
|
|
1802
|
+
routes: analysis.routes,
|
|
1803
|
+
forms,
|
|
1804
|
+
entities,
|
|
1805
|
+
relations,
|
|
1806
|
+
apiEndpoints,
|
|
1807
|
+
vocabulary,
|
|
1808
|
+
metadata: {
|
|
1809
|
+
analyzedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1810
|
+
duration: performance.now() - start
|
|
1811
|
+
}
|
|
1812
|
+
};
|
|
1813
|
+
}
|
|
1814
|
+
export {
|
|
1815
|
+
extractDomainSnapshot
|
|
1816
|
+
};
|
|
1817
|
+
//# sourceMappingURL=domain-UQ34YE4B.js.map
|