@dimassetoid/septum-mcp 0.0.0-stage → 0.2.1
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/LICENSE +21 -0
- package/README.md +124 -2
- package/bin/septum.ts +4 -0
- package/package.json +70 -4
- package/scripts/postinstall.ts +25 -0
- package/src/cli/commands/check.ts +122 -0
- package/src/cli/commands/hook.ts +268 -0
- package/src/cli/commands/init.ts +553 -0
- package/src/cli/commands/serve.ts +5 -0
- package/src/cli/commands/sync.ts +41 -0
- package/src/cli/commands/topology.ts +161 -0
- package/src/cli/index.ts +167 -0
- package/src/cli/scaffold/harness-config.ts +621 -0
- package/src/cli/templates/hooks.ts +75 -0
- package/src/cli/templates/rules.ts +50 -0
- package/src/cli/templates/skill.ts +51 -0
- package/src/cli/ui/prompts.ts +122 -0
- package/src/core/boundary/evaluator.ts +442 -0
- package/src/core/boundary/staged-auditor.ts +58 -0
- package/src/core/config/loader.ts +122 -0
- package/src/core/config/schema.ts +69 -0
- package/src/core/database/client.ts +60 -0
- package/src/core/database/repositories/dependency.repository.ts +712 -0
- package/src/core/database/repositories/domain.repository.ts +330 -0
- package/src/core/database/repositories/environment.repository.ts +207 -0
- package/src/core/database/repositories/file.repository.ts +117 -0
- package/src/core/database/repositories/meta.repository.ts +33 -0
- package/src/core/database/repositories/symbol.repository.ts +508 -0
- package/src/core/database/repository.ts +303 -0
- package/src/core/database/schema.ts +154 -0
- package/src/core/discovery/topology-detector.ts +805 -0
- package/src/core/ingestion/hasher.ts +5 -0
- package/src/core/ingestion/pipeline.ts +623 -0
- package/src/core/parser/behavioral-analyzer.ts +119 -0
- package/src/core/parser/boundary-tracker.ts +443 -0
- package/src/core/parser/extractors/base.ts +67 -0
- package/src/core/parser/extractors/blade.ts +159 -0
- package/src/core/parser/extractors/csharp.ts +244 -0
- package/src/core/parser/extractors/django-semantic.ts +687 -0
- package/src/core/parser/extractors/elysia-semantic.ts +538 -0
- package/src/core/parser/extractors/express-semantic.ts +444 -0
- package/src/core/parser/extractors/fastapi-semantic.ts +736 -0
- package/src/core/parser/extractors/fastify-semantic.ts +774 -0
- package/src/core/parser/extractors/flask-semantic.ts +515 -0
- package/src/core/parser/extractors/frontend.ts +149 -0
- package/src/core/parser/extractors/go-semantic.ts +611 -0
- package/src/core/parser/extractors/go.ts +491 -0
- package/src/core/parser/extractors/hono-semantic.ts +567 -0
- package/src/core/parser/extractors/java.ts +225 -0
- package/src/core/parser/extractors/laravel-semantic.ts +1122 -0
- package/src/core/parser/extractors/mcp-cli-semantic.ts +368 -0
- package/src/core/parser/extractors/nestjs-semantic.ts +543 -0
- package/src/core/parser/extractors/php.ts +569 -0
- package/src/core/parser/extractors/python.ts +498 -0
- package/src/core/parser/extractors/registry.ts +78 -0
- package/src/core/parser/extractors/ruby.ts +234 -0
- package/src/core/parser/extractors/rust.ts +286 -0
- package/src/core/parser/extractors/semantic-extractor.interface.ts +22 -0
- package/src/core/parser/extractors/symfony-semantic.ts +354 -0
- package/src/core/parser/extractors/typescript.ts +826 -0
- package/src/core/parser/language-capabilities.ts +71 -0
- package/src/core/parser/schema/providers/laravel-migration-provider.ts +216 -0
- package/src/core/parser/schema/providers/prisma-schema-provider.ts +90 -0
- package/src/core/parser/schema/schema-provider-registry.ts +43 -0
- package/src/core/parser/schema/schema-provider.interface.ts +33 -0
- package/src/core/parser/tree-sitter.ts +63 -0
- package/src/core/parser/wasm-assets.ts +25 -0
- package/src/core/parser/wasm-loader.ts +274 -0
- package/src/core/resolver/call-graph-tracer.ts +724 -0
- package/src/core/resolver/import-resolver.ts +437 -0
- package/src/core/resolver/module-resolver.ts +482 -0
- package/src/core/resolver/path-resolver.ts +200 -0
- package/src/core/resolver/symbol-locator.ts +276 -0
- package/src/core/resolver/symbol-reference.ts +53 -0
- package/src/core/resolver/vertical-slice-tracer.ts +366 -0
- package/src/core/resources/ignore.ts +123 -0
- package/src/core/resources/limits.ts +14 -0
- package/src/core/resources/source-files.ts +62 -0
- package/src/core/resources/work-queue.ts +74 -0
- package/src/core/session/session-manager.ts +107 -0
- package/src/core/telemetry/telemetry.ts +97 -0
- package/src/core/topology/environment-detector.ts +973 -0
- package/src/core/topology/types.ts +83 -0
- package/src/index.ts +21 -0
- package/src/mcp/schemas.ts +1105 -0
- package/src/mcp/server.ts +774 -0
- package/src/mcp/tools/boundary.ts +17 -0
- package/src/mcp/tools/check-boundary.ts +387 -0
- package/src/mcp/tools/domain.ts +40 -0
- package/src/mcp/tools/feature.ts +67 -0
- package/src/mcp/tools/get-domain-catalog.ts +226 -0
- package/src/mcp/tools/get-environment-topology.ts +138 -0
- package/src/mcp/tools/get-feature-context.ts +224 -0
- package/src/mcp/tools/get-symbol-hotspots.ts +54 -0
- package/src/mcp/tools/get-symbol-impact.ts +64 -0
- package/src/mcp/tools/get-symbol.ts +175 -0
- package/src/mcp/tools/locate-symbol.ts +196 -0
- package/src/mcp/tools/register-domain.ts +75 -0
- package/src/mcp/tools/register-feature.ts +110 -0
- package/src/mcp/tools/slice.ts +16 -0
- package/src/mcp/tools/symbol.ts +93 -0
- package/src/mcp/tools/trace-vertical-slice.ts +80 -0
- package/src/mcp/workspace-manager.ts +159 -0
- package/src/mcp/workspace-watcher.ts +202 -0
- package/src/types/index.ts +456 -0
- package/src/types/wasm.d.ts +4 -0
- package/src/utils/version-check.ts +209 -0
- package/src/version.ts +1 -0
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
import { readOptionalSourceFile } from "../../resources/source-files.ts";
|
|
2
|
+
import { parseWithTreeSitter } from "../wasm-loader.ts";
|
|
3
|
+
import { ImportResolver } from "../../resolver/import-resolver.ts";
|
|
4
|
+
import type Parser from "web-tree-sitter";
|
|
5
|
+
import * as fs from "node:fs";
|
|
6
|
+
import * as path from "node:path";
|
|
7
|
+
import type { SemanticSliceExtractor, VerticalSliceCandidate } from "./semantic-extractor.interface.ts";
|
|
8
|
+
|
|
9
|
+
const EXPRESS_VERBS = new Set([
|
|
10
|
+
"get",
|
|
11
|
+
"post",
|
|
12
|
+
"put",
|
|
13
|
+
"patch",
|
|
14
|
+
"delete",
|
|
15
|
+
"all",
|
|
16
|
+
"head",
|
|
17
|
+
"options",
|
|
18
|
+
]);
|
|
19
|
+
|
|
20
|
+
interface RawRoute {
|
|
21
|
+
file: string;
|
|
22
|
+
relPath: string;
|
|
23
|
+
routerVar: string;
|
|
24
|
+
httpMethod: string;
|
|
25
|
+
rawPath: string;
|
|
26
|
+
middlewares: Array<{ symbol: string; line: number }>;
|
|
27
|
+
handler: { symbol: string; line: number };
|
|
28
|
+
routeLine: number;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface RawMount {
|
|
32
|
+
file: string;
|
|
33
|
+
relPath: string;
|
|
34
|
+
callerVar: string;
|
|
35
|
+
prefix: string;
|
|
36
|
+
targetVar: string;
|
|
37
|
+
line: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface ImportBinding {
|
|
41
|
+
importedName: string;
|
|
42
|
+
sourceSpecifier: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function cleanLiteral(node: Parser.SyntaxNode | null | undefined): string | undefined {
|
|
46
|
+
if (!node) return undefined;
|
|
47
|
+
if (node.type === "string" || node.type === "template_string") {
|
|
48
|
+
const raw = node.text.trim();
|
|
49
|
+
if (raw.includes("${")) return undefined;
|
|
50
|
+
return raw.replace(/^['"`]|['"`]$/g, "");
|
|
51
|
+
}
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function normalizeRouteUri(prefix: string, rawPath: string): string {
|
|
56
|
+
const p = (prefix ?? "").trim().replace(/\/+$/, "");
|
|
57
|
+
let r = (rawPath ?? "").trim();
|
|
58
|
+
if (!r.startsWith("/")) r = "/" + r;
|
|
59
|
+
if (r === "/" && p.length > 0) return p;
|
|
60
|
+
if (r === "/" && p.length === 0) return "/";
|
|
61
|
+
const joined = `${p}${r}`;
|
|
62
|
+
return joined.startsWith("/") ? joined : `/${joined}`;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export class ExpressSemanticExtractor implements SemanticSliceExtractor {
|
|
66
|
+
public readonly id = "express";
|
|
67
|
+
public readonly name = "Express Router Composition Extractor";
|
|
68
|
+
|
|
69
|
+
public canHandle(projectRoot: string, framework?: string): boolean {
|
|
70
|
+
if (framework === "express") return true;
|
|
71
|
+
|
|
72
|
+
const pkgPath = path.join(projectRoot, "package.json");
|
|
73
|
+
if (fs.existsSync(pkgPath)) {
|
|
74
|
+
try {
|
|
75
|
+
const pkg = JSON.parse(readOptionalSourceFile(pkgPath, "utf-8"));
|
|
76
|
+
const deps = { ...pkg.dependencies, ...pkg.devDependencies };
|
|
77
|
+
if (deps["express"]) return true;
|
|
78
|
+
} catch {}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
public async extractSlices(
|
|
85
|
+
projectRoot: string,
|
|
86
|
+
domainId?: number,
|
|
87
|
+
signal?: AbortSignal
|
|
88
|
+
): Promise<VerticalSliceCandidate[]> {
|
|
89
|
+
const slices: VerticalSliceCandidate[] = [];
|
|
90
|
+
const sourceFiles = this.findSourceFiles(projectRoot);
|
|
91
|
+
const resolver = new ImportResolver(projectRoot);
|
|
92
|
+
|
|
93
|
+
const allRoutes: RawRoute[] = [];
|
|
94
|
+
const allMounts: RawMount[] = [];
|
|
95
|
+
const fileImports = new Map<string, Map<string, string>>(); // relPath -> (importedIdent -> targetRelPath)
|
|
96
|
+
|
|
97
|
+
for (const file of sourceFiles) {
|
|
98
|
+
if (signal?.aborted) throw new Error("semantic_extraction_cancelled");
|
|
99
|
+
const relPath = path.relative(projectRoot, file).replace(/\\/g, "/");
|
|
100
|
+
const content = readOptionalSourceFile(file, "utf-8");
|
|
101
|
+
if (!content || (!content.includes(".get") && !content.includes(".post") && !content.includes(".use") && !content.includes(".route") && !content.includes("Router") && !content.includes("express"))) {
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const fileData = await parseWithTreeSitter(
|
|
106
|
+
"typescript",
|
|
107
|
+
content,
|
|
108
|
+
(rootNode) => this.extractFileAst(file, relPath, rootNode, resolver)
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
if (fileData) {
|
|
112
|
+
allRoutes.push(...fileData.routes);
|
|
113
|
+
allMounts.push(...fileData.mounts);
|
|
114
|
+
if (fileData.resolvedImports.size > 0) {
|
|
115
|
+
fileImports.set(relPath, fileData.resolvedImports);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Resolve Router Prefix Hierarchy
|
|
121
|
+
const routerPrefixes = this.resolvePrefixHierarchy(allMounts, fileImports);
|
|
122
|
+
|
|
123
|
+
// Build Vertical Slices
|
|
124
|
+
for (const route of allRoutes) {
|
|
125
|
+
const prefixes = routerPrefixes.get(`${route.relPath}#${route.routerVar}`) || [""];
|
|
126
|
+
|
|
127
|
+
for (const prefix of prefixes) {
|
|
128
|
+
const fullUri = normalizeRouteUri(prefix, route.rawPath);
|
|
129
|
+
const actionSymbol = route.handler.symbol;
|
|
130
|
+
const rootIdent = actionSymbol.includes(".") ? actionSymbol.split(".")[0] : actionSymbol;
|
|
131
|
+
const imports = fileImports.get(route.relPath);
|
|
132
|
+
const resolvedControllerFile = imports?.get(rootIdent) || route.relPath;
|
|
133
|
+
const fileBase = path.basename(route.relPath).replace(/\.[^.]+$/, "");
|
|
134
|
+
const controllerClass = actionSymbol.includes(".")
|
|
135
|
+
? actionSymbol.split(".")[0]
|
|
136
|
+
: fileBase.charAt(0).toUpperCase() + fileBase.slice(1);
|
|
137
|
+
const actionName = actionSymbol.includes(".") ? actionSymbol.split(".").pop()! : actionSymbol;
|
|
138
|
+
|
|
139
|
+
const chain: Array<{ stage: string; symbol: string; file?: string; line?: number; description?: string }> = [
|
|
140
|
+
{
|
|
141
|
+
stage: "ingress",
|
|
142
|
+
symbol: `${route.httpMethod} ${fullUri}`,
|
|
143
|
+
file: route.relPath,
|
|
144
|
+
line: route.routeLine,
|
|
145
|
+
description: `Express Route: ${route.httpMethod} ${fullUri}`,
|
|
146
|
+
},
|
|
147
|
+
];
|
|
148
|
+
|
|
149
|
+
for (const mw of route.middlewares) {
|
|
150
|
+
chain.push({
|
|
151
|
+
stage: "middleware",
|
|
152
|
+
symbol: mw.symbol,
|
|
153
|
+
file: route.relPath,
|
|
154
|
+
line: mw.line,
|
|
155
|
+
description: `Express Middleware: ${mw.symbol}`,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
chain.push({
|
|
160
|
+
stage: "controller",
|
|
161
|
+
symbol: actionSymbol,
|
|
162
|
+
file: resolvedControllerFile,
|
|
163
|
+
line: route.handler.line,
|
|
164
|
+
description: `Express Action Handler`,
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
slices.push({
|
|
168
|
+
domain_id: domainId ?? null,
|
|
169
|
+
feature_key: `express.${controllerClass.toLowerCase()}.${actionName}`,
|
|
170
|
+
http_method: route.httpMethod,
|
|
171
|
+
route_uri: fullUri,
|
|
172
|
+
route_name: `${controllerClass}.${actionName}`,
|
|
173
|
+
controller_class: controllerClass,
|
|
174
|
+
action_name: actionName,
|
|
175
|
+
controller_file: resolvedControllerFile,
|
|
176
|
+
controller_line: route.handler.line,
|
|
177
|
+
architecture_style: "mvc",
|
|
178
|
+
entry_kind: "http_route",
|
|
179
|
+
execution_chain_json: JSON.stringify(chain),
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return slices;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
private extractFileAst(
|
|
188
|
+
_filePath: string,
|
|
189
|
+
relPath: string,
|
|
190
|
+
rootNode: Parser.SyntaxNode,
|
|
191
|
+
resolver: ImportResolver
|
|
192
|
+
): { routes: RawRoute[]; mounts: RawMount[]; resolvedImports: Map<string, string> } {
|
|
193
|
+
const routes: RawRoute[] = [];
|
|
194
|
+
const mounts: RawMount[] = [];
|
|
195
|
+
const resolvedImports = new Map<string, string>();
|
|
196
|
+
|
|
197
|
+
// 1. Collect static imports
|
|
198
|
+
for (const child of rootNode.namedChildren) {
|
|
199
|
+
if (child.type === "import_statement") {
|
|
200
|
+
const sourceNode =
|
|
201
|
+
child.childForFieldName("source") ??
|
|
202
|
+
child.namedChildren.find((c) => c.type === "string");
|
|
203
|
+
const specifier = cleanLiteral(sourceNode);
|
|
204
|
+
if (!specifier) continue;
|
|
205
|
+
|
|
206
|
+
const res = resolver.resolve(specifier, relPath);
|
|
207
|
+
const targetFile = res.status === "resolved" && res.targets[0]?.files[0] ? res.targets[0].files[0] : undefined;
|
|
208
|
+
if (!targetFile) continue;
|
|
209
|
+
|
|
210
|
+
// Default import
|
|
211
|
+
const importClause = child.namedChildren.find((c) => c.type === "import_clause");
|
|
212
|
+
if (importClause) {
|
|
213
|
+
for (const ic of importClause.namedChildren) {
|
|
214
|
+
if (ic.type === "identifier") {
|
|
215
|
+
resolvedImports.set(ic.text.trim(), targetFile);
|
|
216
|
+
} else if (ic.type === "namespace_import") {
|
|
217
|
+
const identNode = ic.namedChildren.find((n) => n.type === "identifier");
|
|
218
|
+
if (identNode) resolvedImports.set(identNode.text.trim(), targetFile);
|
|
219
|
+
} else if (ic.type === "named_imports") {
|
|
220
|
+
for (const spec of ic.namedChildren) {
|
|
221
|
+
if (spec.type === "import_specifier") {
|
|
222
|
+
const alias = spec.childForFieldName("alias");
|
|
223
|
+
const name = spec.childForFieldName("name") ?? spec.namedChildren[0];
|
|
224
|
+
const ident = alias ? alias.text.trim() : name ? name.text.trim() : "";
|
|
225
|
+
if (ident) resolvedImports.set(ident, targetFile);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// 2. Walk call expressions for routes and mounts
|
|
235
|
+
const walkCalls = (node: Parser.SyntaxNode): void => {
|
|
236
|
+
if (node.type === "call_expression") {
|
|
237
|
+
const fnNode = node.childForFieldName("function");
|
|
238
|
+
if (fnNode && fnNode.type === "member_expression") {
|
|
239
|
+
const objNode = fnNode.childForFieldName("object");
|
|
240
|
+
const propNode = fnNode.childForFieldName("property");
|
|
241
|
+
const argsNode = node.childForFieldName("arguments");
|
|
242
|
+
|
|
243
|
+
if (objNode && propNode && argsNode) {
|
|
244
|
+
const callerVar = objNode.text.trim();
|
|
245
|
+
const methodName = propNode.text.trim().toLowerCase();
|
|
246
|
+
const args = argsNode.namedChildren;
|
|
247
|
+
|
|
248
|
+
// Route Mount: app.use('/api', apiRouter) or router.use('/sub', subRouter)
|
|
249
|
+
if (methodName === "use") {
|
|
250
|
+
if (args.length >= 1) {
|
|
251
|
+
const firstLit = cleanLiteral(args[0]);
|
|
252
|
+
const hasPath = firstLit !== undefined;
|
|
253
|
+
const prefix = hasPath ? firstLit : "";
|
|
254
|
+
const targetArg = hasPath ? args[1] : args[0];
|
|
255
|
+
|
|
256
|
+
if (targetArg && targetArg.type === "identifier") {
|
|
257
|
+
mounts.push({
|
|
258
|
+
file: relPath,
|
|
259
|
+
relPath,
|
|
260
|
+
callerVar,
|
|
261
|
+
prefix,
|
|
262
|
+
targetVar: targetArg.text.trim(),
|
|
263
|
+
line: node.startPosition.row + 1,
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
} else if (EXPRESS_VERBS.has(methodName)) {
|
|
268
|
+
// Direct HTTP Route: app.get('/path', ...middlewares, handler)
|
|
269
|
+
if (args.length >= 2) {
|
|
270
|
+
const rawPath = cleanLiteral(args[0]);
|
|
271
|
+
if (rawPath !== undefined) {
|
|
272
|
+
const middlewareNodes = args.slice(1, args.length - 1);
|
|
273
|
+
const handlerNode = args[args.length - 1];
|
|
274
|
+
|
|
275
|
+
const middlewares = this.extractHandlers(middlewareNodes);
|
|
276
|
+
const handler = this.extractSingleHandler(handlerNode);
|
|
277
|
+
|
|
278
|
+
routes.push({
|
|
279
|
+
file: relPath,
|
|
280
|
+
relPath,
|
|
281
|
+
routerVar: callerVar,
|
|
282
|
+
httpMethod: methodName.toUpperCase(),
|
|
283
|
+
rawPath,
|
|
284
|
+
middlewares,
|
|
285
|
+
handler,
|
|
286
|
+
routeLine: node.startPosition.row + 1,
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
for (const child of node.namedChildren) {
|
|
296
|
+
walkCalls(child);
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
walkCalls(rootNode);
|
|
301
|
+
return { routes, mounts, resolvedImports };
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
private extractSingleHandler(node: Parser.SyntaxNode): { symbol: string; line: number } {
|
|
305
|
+
const line = node.startPosition.row + 1;
|
|
306
|
+
if (node.type === "identifier" || node.type === "member_expression") {
|
|
307
|
+
return { symbol: node.text.trim(), line };
|
|
308
|
+
}
|
|
309
|
+
if (node.type === "arrow_function" || node.type === "function_expression" || node.type === "function") {
|
|
310
|
+
const nameNode = node.childForFieldName("name");
|
|
311
|
+
const name = nameNode ? nameNode.text.trim() : "closure";
|
|
312
|
+
return { symbol: name, line };
|
|
313
|
+
}
|
|
314
|
+
return { symbol: "closure", line };
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
private extractHandlers(nodes: Parser.SyntaxNode[]): Array<{ symbol: string; line: number }> {
|
|
318
|
+
const list: Array<{ symbol: string; line: number }> = [];
|
|
319
|
+
for (const node of nodes) {
|
|
320
|
+
if (node.type === "array") {
|
|
321
|
+
for (const el of node.namedChildren) {
|
|
322
|
+
list.push(this.extractSingleHandler(el));
|
|
323
|
+
}
|
|
324
|
+
} else {
|
|
325
|
+
list.push(this.extractSingleHandler(node));
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return list;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
private resolvePrefixHierarchy(
|
|
332
|
+
mounts: RawMount[],
|
|
333
|
+
fileImports: Map<string, Map<string, string>>
|
|
334
|
+
): Map<string, string[]> {
|
|
335
|
+
// Map: (relPath#varName) -> array of prefixes
|
|
336
|
+
const prefixes = new Map<string, Set<string>>();
|
|
337
|
+
|
|
338
|
+
const getPrefixes = (key: string): Set<string> => {
|
|
339
|
+
let set = prefixes.get(key);
|
|
340
|
+
if (!set) {
|
|
341
|
+
set = new Set<string>();
|
|
342
|
+
prefixes.set(key, set);
|
|
343
|
+
}
|
|
344
|
+
return set;
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
// 1. Identify root app variables (e.g. app in app.ts) which start with root prefix ""
|
|
348
|
+
// Also identify top-level callers that are not targets of any mount
|
|
349
|
+
const targetedCallers = new Set<string>();
|
|
350
|
+
for (const mount of mounts) {
|
|
351
|
+
const fileMap = fileImports.get(mount.relPath);
|
|
352
|
+
const importedFile = fileMap?.get(mount.targetVar);
|
|
353
|
+
if (importedFile) {
|
|
354
|
+
targetedCallers.add(`${importedFile}#router`);
|
|
355
|
+
targetedCallers.add(`${importedFile}#default`);
|
|
356
|
+
targetedCallers.add(`${importedFile}#${mount.targetVar}`);
|
|
357
|
+
for (const m of mounts) {
|
|
358
|
+
if (m.relPath === importedFile) {
|
|
359
|
+
targetedCallers.add(`${importedFile}#${m.callerVar}`);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
} else {
|
|
363
|
+
targetedCallers.add(`${mount.relPath}#${mount.targetVar}`);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
for (const mount of mounts) {
|
|
368
|
+
const callerKey = `${mount.relPath}#${mount.callerVar}`;
|
|
369
|
+
if (mount.callerVar === "app" || mount.callerVar === "server" || !targetedCallers.has(callerKey)) {
|
|
370
|
+
getPrefixes(callerKey).add("");
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// 2. Propagate mounts
|
|
375
|
+
let changed = true;
|
|
376
|
+
let iterations = 0;
|
|
377
|
+
|
|
378
|
+
while (changed && iterations < 10) {
|
|
379
|
+
changed = false;
|
|
380
|
+
iterations++;
|
|
381
|
+
|
|
382
|
+
for (const mount of mounts) {
|
|
383
|
+
const callerKey = `${mount.relPath}#${mount.callerVar}`;
|
|
384
|
+
const callerPrefixes = getPrefixes(callerKey);
|
|
385
|
+
if (callerPrefixes.size === 0) continue;
|
|
386
|
+
const basePrefixes = Array.from(callerPrefixes);
|
|
387
|
+
|
|
388
|
+
// Check if targetVar is imported from another file
|
|
389
|
+
const fileMap = fileImports.get(mount.relPath);
|
|
390
|
+
const importedFile = fileMap?.get(mount.targetVar);
|
|
391
|
+
|
|
392
|
+
const targetKeys: string[] = [];
|
|
393
|
+
if (importedFile) {
|
|
394
|
+
// Cross-file mount: target router in importedFile
|
|
395
|
+
targetKeys.push(`${importedFile}#router`, `${importedFile}#default`, `${importedFile}#${mount.targetVar}`);
|
|
396
|
+
// Also any router variable in importedFile
|
|
397
|
+
for (const m of mounts) {
|
|
398
|
+
if (m.relPath === importedFile) {
|
|
399
|
+
targetKeys.push(`${importedFile}#${m.callerVar}`);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
} else {
|
|
403
|
+
// Local mount in same file
|
|
404
|
+
targetKeys.push(`${mount.relPath}#${mount.targetVar}`);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
for (const targetKey of targetKeys) {
|
|
408
|
+
const targetSet = getPrefixes(targetKey);
|
|
409
|
+
for (const baseP of basePrefixes) {
|
|
410
|
+
const combined = normalizeRouteUri(baseP, mount.prefix);
|
|
411
|
+
if (!targetSet.has(combined)) {
|
|
412
|
+
targetSet.add(combined);
|
|
413
|
+
changed = true;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// Convert Set to Array
|
|
421
|
+
const result = new Map<string, string[]>();
|
|
422
|
+
for (const [k, set] of prefixes.entries()) {
|
|
423
|
+
result.set(k, Array.from(set));
|
|
424
|
+
}
|
|
425
|
+
return result;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
private findSourceFiles(dir: string): string[] {
|
|
429
|
+
const results: string[] = [];
|
|
430
|
+
try {
|
|
431
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
432
|
+
for (const entry of entries) {
|
|
433
|
+
const full = path.join(dir, entry.name);
|
|
434
|
+
if (entry.isDirectory() && !entry.name.startsWith(".") && entry.name !== "node_modules" && entry.name !== "dist") {
|
|
435
|
+
results.push(...this.findSourceFiles(full));
|
|
436
|
+
} else if (entry.isFile() && /\.(?:[cm]?[jt]sx?)$/.test(entry.name) && !entry.name.endsWith(".d.ts")) {
|
|
437
|
+
results.push(full);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
} catch {}
|
|
441
|
+
return results;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|