septum 0.1.3 → 0.1.5
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 -21
- package/README.md +105 -102
- package/bin/septum.ts +4 -4
- package/package.json +63 -63
- package/src/cli/commands/check.ts +161 -161
- package/src/cli/commands/hook.ts +188 -188
- package/src/cli/commands/ingest.ts +29 -29
- package/src/cli/commands/init.ts +211 -211
- package/src/cli/commands/locate.ts +61 -61
- package/src/cli/commands/query.ts +64 -64
- package/src/cli/commands/serve.ts +5 -5
- package/src/cli/commands/slice.ts +48 -90
- package/src/cli/commands/sync.ts +31 -31
- package/src/cli/commands/topology.ts +119 -0
- package/src/cli/index.ts +210 -199
- package/src/cli/templates/hooks.ts +75 -75
- package/src/cli/templates/rules.ts +38 -38
- package/src/cli/templates/skill.ts +69 -69
- package/src/core/boundary/evaluator.ts +232 -210
- package/src/core/config/loader.ts +97 -97
- package/src/core/config/schema.ts +39 -39
- package/src/core/database/client.ts +44 -44
- package/src/core/database/repositories/dependency.repository.ts +366 -349
- package/src/core/database/repositories/domain.repository.ts +218 -198
- package/src/core/database/repositories/environment.repository.ts +207 -0
- package/src/core/database/repositories/file.repository.ts +116 -106
- package/src/core/database/repositories/meta.repository.ts +33 -33
- package/src/core/database/repositories/symbol.repository.ts +443 -424
- package/src/core/database/repository.ts +264 -255
- package/src/core/database/schema.ts +34 -1
- package/src/core/discovery/topology-detector.ts +486 -486
- package/src/core/ingestion/hasher.ts +5 -5
- package/src/core/ingestion/pipeline.ts +584 -535
- package/src/core/parser/behavioral-analyzer.ts +119 -0
- package/src/core/parser/boundary-tracker.ts +288 -128
- package/src/core/parser/extractors/base.ts +62 -61
- package/src/core/parser/extractors/frontend.ts +194 -194
- package/src/core/parser/extractors/go.ts +229 -176
- package/src/core/parser/extractors/laravel-semantic.ts +1039 -585
- package/src/core/parser/extractors/mcp-cli-semantic.ts +199 -199
- package/src/core/parser/extractors/nestjs-semantic.ts +119 -119
- package/src/core/parser/extractors/php.ts +153 -149
- package/src/core/parser/extractors/python.ts +228 -226
- package/src/core/parser/extractors/registry.ts +70 -70
- package/src/core/parser/extractors/semantic-extractor.interface.ts +21 -21
- package/src/core/parser/extractors/typescript.ts +181 -177
- package/src/core/parser/schema/providers/laravel-migration-provider.ts +215 -0
- package/src/core/parser/schema/providers/prisma-schema-provider.ts +89 -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 +41 -41
- package/src/core/resolver/call-graph-tracer.ts +666 -668
- package/src/core/resolver/module-resolver.ts +408 -342
- package/src/core/resolver/symbol-locator.ts +543 -543
- package/src/core/resolver/vertical-slice-tracer.ts +301 -252
- package/src/core/session/session-manager.ts +56 -56
- package/src/core/telemetry/telemetry.ts +86 -86
- package/src/core/topology/environment-detector.ts +860 -0
- package/src/core/topology/types.ts +83 -0
- package/src/index.ts +21 -18
- package/src/mcp/schemas.ts +578 -238
- package/src/mcp/server.ts +875 -656
- package/src/mcp/tools/check-boundary.ts +171 -152
- package/src/mcp/tools/get-domain-catalog.ts +145 -121
- package/src/mcp/tools/get-environment-topology.ts +141 -0
- package/src/mcp/tools/get-feature-context.ts +212 -116
- package/src/mcp/tools/get-symbol-hotspots.ts +60 -49
- package/src/mcp/tools/get-symbol-impact.ts +28 -26
- package/src/mcp/tools/get-symbol.ts +65 -63
- package/src/mcp/tools/locate-symbol.ts +171 -190
- package/src/mcp/tools/register-domain.ts +68 -64
- package/src/mcp/tools/register-feature.ts +100 -0
- package/src/mcp/tools/trace-vertical-slice.ts +74 -103
- package/src/types/index.ts +335 -299
|
@@ -1,177 +1,181 @@
|
|
|
1
|
-
import type { ExtractedDependency, ExtractedSymbol, ParsedFileAST, Visibility } from "../../../types/index.ts";
|
|
2
|
-
import { findBraceBlockEnd } from "../boundary-tracker.ts";
|
|
3
|
-
import type { CodeExtractor } from "./base.ts";
|
|
4
|
-
|
|
5
|
-
export class TypeScriptExtractor implements CodeExtractor {
|
|
6
|
-
public canHandle(filePath: string): boolean {
|
|
7
|
-
return /\.(ts|tsx|js|jsx|mjs|cjs)$/i.test(filePath);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
public async extract(filePath: string, content: string): Promise<ParsedFileAST> {
|
|
11
|
-
const symbols: ExtractedSymbol[] = [];
|
|
12
|
-
const dependencies: ExtractedDependency[] = [];
|
|
13
|
-
|
|
14
|
-
const lines = content.split("\n");
|
|
15
|
-
|
|
16
|
-
// 1. Extract Imports / Dependencies
|
|
17
|
-
const importRegex = /(?:import\s+(?:(?:[\w*\s{},]+)\s+from\s+)?['"]([^'"]+)['"]|require\s*\(\s*['"]([^'"]+)['"]\s*\))/g;
|
|
18
|
-
for (let i = 0; i < lines.length; i++) {
|
|
19
|
-
const line = lines[i];
|
|
20
|
-
let match: RegExpExecArray | null;
|
|
21
|
-
while ((match = importRegex.exec(line)) !== null) {
|
|
22
|
-
const target = match[1] || match[2];
|
|
23
|
-
if (target) {
|
|
24
|
-
const isExternal = !target.startsWith(".") && !target.startsWith("/");
|
|
25
|
-
dependencies.push({
|
|
26
|
-
target,
|
|
27
|
-
statement: line.trim(),
|
|
28
|
-
line_number: i + 1,
|
|
29
|
-
is_external: isExternal,
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// 2. Extract Symbols (Classes, Interfaces, Types, Functions, Methods)
|
|
36
|
-
let currentClass: string | null = null;
|
|
37
|
-
let currentClassEndLine = 0;
|
|
38
|
-
|
|
39
|
-
for (let i = 0; i < lines.length; i++) {
|
|
40
|
-
const line = lines[i];
|
|
41
|
-
const trimmed = line.trim();
|
|
42
|
-
const lineNum = i + 1;
|
|
43
|
-
|
|
44
|
-
if (currentClass && lineNum > currentClassEndLine) {
|
|
45
|
-
currentClass = null;
|
|
46
|
-
currentClassEndLine = 0;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// Class / Interface / Type definition
|
|
50
|
-
const classMatch = /(?:export\s+)?(?:default\s+)?(?:abstract\s+)?(class|interface)\s+([A-Za-z0-9_]+)/.exec(trimmed);
|
|
51
|
-
if (classMatch) {
|
|
52
|
-
currentClass = classMatch[2];
|
|
53
|
-
const endLine = findBraceBlockEnd(lines, i);
|
|
54
|
-
currentClassEndLine = endLine;
|
|
55
|
-
symbols.push({
|
|
56
|
-
name: classMatch[2],
|
|
57
|
-
kind: classMatch[1] as "class" | "interface",
|
|
58
|
-
signature: trimmed.replace(/\{$/, "").trim(),
|
|
59
|
-
visibility: "public",
|
|
60
|
-
line_start: lineNum,
|
|
61
|
-
line_end: endLine,
|
|
62
|
-
line_count: Math.max(1, endLine - lineNum + 1),
|
|
63
|
-
});
|
|
64
|
-
continue;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// Type Alias
|
|
68
|
-
const typeMatch = /(?:export\s+)?type\s+([A-Za-z0-9_]+)\s*=/.exec(trimmed);
|
|
69
|
-
if (typeMatch) {
|
|
70
|
-
const endLine = trimmed.endsWith(";") ? lineNum : findBraceBlockEnd(lines, i);
|
|
71
|
-
symbols.push({
|
|
72
|
-
name: typeMatch[1],
|
|
73
|
-
kind: "type",
|
|
74
|
-
signature: trimmed.replace(/;$/, "").trim(),
|
|
75
|
-
visibility: "public",
|
|
76
|
-
line_start: lineNum,
|
|
77
|
-
line_end: endLine,
|
|
78
|
-
line_count: Math.max(1, endLine - lineNum + 1),
|
|
79
|
-
});
|
|
80
|
-
continue;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// Enum Declaration
|
|
84
|
-
const enumMatch = /(?:export\s+)?(?:const\s+)?enum\s+([A-Za-z0-9_]+)/.exec(trimmed);
|
|
85
|
-
if (enumMatch) {
|
|
86
|
-
const endLine = findBraceBlockEnd(lines, i);
|
|
87
|
-
symbols.push({
|
|
88
|
-
name: enumMatch[1],
|
|
89
|
-
kind: "enum",
|
|
90
|
-
signature: trimmed.replace(/\{$/, "").trim(),
|
|
91
|
-
visibility: "public",
|
|
92
|
-
line_start: lineNum,
|
|
93
|
-
line_end: endLine,
|
|
94
|
-
line_count: Math.max(1, endLine - lineNum + 1),
|
|
95
|
-
});
|
|
96
|
-
continue;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// Function Declaration
|
|
100
|
-
const fnMatch = /(?:export\s+)?(?:async\s+)?function\s+([A-Za-z0-9_]+)\s*\(([^)]*)\)(?:\s*:\s*([^;{]+))?/.exec(trimmed);
|
|
101
|
-
if (fnMatch) {
|
|
102
|
-
const name = fnMatch[1];
|
|
103
|
-
const params = fnMatch[2];
|
|
104
|
-
const retType = fnMatch[3] ? `: ${fnMatch[3].trim()}` : "";
|
|
105
|
-
const endLine = findBraceBlockEnd(lines, i);
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
const
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
const
|
|
160
|
-
const
|
|
161
|
-
const
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
}
|
|
1
|
+
import type { ExtractedDependency, ExtractedSymbol, ParsedFileAST, Visibility } from "../../../types/index.ts";
|
|
2
|
+
import { findBraceBlockEnd, calculateControlFlowNesting } from "../boundary-tracker.ts";
|
|
3
|
+
import type { CodeExtractor } from "./base.ts";
|
|
4
|
+
|
|
5
|
+
export class TypeScriptExtractor implements CodeExtractor {
|
|
6
|
+
public canHandle(filePath: string): boolean {
|
|
7
|
+
return /\.(ts|tsx|js|jsx|mjs|cjs)$/i.test(filePath);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
public async extract(filePath: string, content: string): Promise<ParsedFileAST> {
|
|
11
|
+
const symbols: ExtractedSymbol[] = [];
|
|
12
|
+
const dependencies: ExtractedDependency[] = [];
|
|
13
|
+
|
|
14
|
+
const lines = content.split("\n");
|
|
15
|
+
|
|
16
|
+
// 1. Extract Imports / Dependencies
|
|
17
|
+
const importRegex = /(?:import\s+(?:(?:[\w*\s{},]+)\s+from\s+)?['"]([^'"]+)['"]|require\s*\(\s*['"]([^'"]+)['"]\s*\))/g;
|
|
18
|
+
for (let i = 0; i < lines.length; i++) {
|
|
19
|
+
const line = lines[i];
|
|
20
|
+
let match: RegExpExecArray | null;
|
|
21
|
+
while ((match = importRegex.exec(line)) !== null) {
|
|
22
|
+
const target = match[1] || match[2];
|
|
23
|
+
if (target) {
|
|
24
|
+
const isExternal = !target.startsWith(".") && !target.startsWith("/");
|
|
25
|
+
dependencies.push({
|
|
26
|
+
target,
|
|
27
|
+
statement: line.trim(),
|
|
28
|
+
line_number: i + 1,
|
|
29
|
+
is_external: isExternal,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// 2. Extract Symbols (Classes, Interfaces, Types, Functions, Methods)
|
|
36
|
+
let currentClass: string | null = null;
|
|
37
|
+
let currentClassEndLine = 0;
|
|
38
|
+
|
|
39
|
+
for (let i = 0; i < lines.length; i++) {
|
|
40
|
+
const line = lines[i];
|
|
41
|
+
const trimmed = line.trim();
|
|
42
|
+
const lineNum = i + 1;
|
|
43
|
+
|
|
44
|
+
if (currentClass && lineNum > currentClassEndLine) {
|
|
45
|
+
currentClass = null;
|
|
46
|
+
currentClassEndLine = 0;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Class / Interface / Type definition
|
|
50
|
+
const classMatch = /(?:export\s+)?(?:default\s+)?(?:abstract\s+)?(class|interface)\s+([A-Za-z0-9_]+)/.exec(trimmed);
|
|
51
|
+
if (classMatch) {
|
|
52
|
+
currentClass = classMatch[2];
|
|
53
|
+
const endLine = findBraceBlockEnd(lines, i);
|
|
54
|
+
currentClassEndLine = endLine;
|
|
55
|
+
symbols.push({
|
|
56
|
+
name: classMatch[2],
|
|
57
|
+
kind: classMatch[1] as "class" | "interface",
|
|
58
|
+
signature: trimmed.replace(/\{$/, "").trim(),
|
|
59
|
+
visibility: "public",
|
|
60
|
+
line_start: lineNum,
|
|
61
|
+
line_end: endLine,
|
|
62
|
+
line_count: Math.max(1, endLine - lineNum + 1),
|
|
63
|
+
});
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Type Alias
|
|
68
|
+
const typeMatch = /(?:export\s+)?type\s+([A-Za-z0-9_]+)\s*=/.exec(trimmed);
|
|
69
|
+
if (typeMatch) {
|
|
70
|
+
const endLine = trimmed.endsWith(";") ? lineNum : findBraceBlockEnd(lines, i);
|
|
71
|
+
symbols.push({
|
|
72
|
+
name: typeMatch[1],
|
|
73
|
+
kind: "type",
|
|
74
|
+
signature: trimmed.replace(/;$/, "").trim(),
|
|
75
|
+
visibility: "public",
|
|
76
|
+
line_start: lineNum,
|
|
77
|
+
line_end: endLine,
|
|
78
|
+
line_count: Math.max(1, endLine - lineNum + 1),
|
|
79
|
+
});
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Enum Declaration
|
|
84
|
+
const enumMatch = /(?:export\s+)?(?:const\s+)?enum\s+([A-Za-z0-9_]+)/.exec(trimmed);
|
|
85
|
+
if (enumMatch) {
|
|
86
|
+
const endLine = findBraceBlockEnd(lines, i);
|
|
87
|
+
symbols.push({
|
|
88
|
+
name: enumMatch[1],
|
|
89
|
+
kind: "enum",
|
|
90
|
+
signature: trimmed.replace(/\{$/, "").trim(),
|
|
91
|
+
visibility: "public",
|
|
92
|
+
line_start: lineNum,
|
|
93
|
+
line_end: endLine,
|
|
94
|
+
line_count: Math.max(1, endLine - lineNum + 1),
|
|
95
|
+
});
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Function Declaration
|
|
100
|
+
const fnMatch = /(?:export\s+)?(?:async\s+)?function\s+([A-Za-z0-9_]+)\s*\(([^)]*)\)(?:\s*:\s*([^;{]+))?/.exec(trimmed);
|
|
101
|
+
if (fnMatch) {
|
|
102
|
+
const name = fnMatch[1];
|
|
103
|
+
const params = fnMatch[2];
|
|
104
|
+
const retType = fnMatch[3] ? `: ${fnMatch[3].trim()}` : "";
|
|
105
|
+
const endLine = findBraceBlockEnd(lines, i);
|
|
106
|
+
const nestingDepth = calculateControlFlowNesting(lines, i, endLine - 1);
|
|
107
|
+
symbols.push({
|
|
108
|
+
name,
|
|
109
|
+
kind: "function",
|
|
110
|
+
signature: `${name}(${params})${retType}`,
|
|
111
|
+
visibility: "public",
|
|
112
|
+
line_start: lineNum,
|
|
113
|
+
line_end: endLine,
|
|
114
|
+
line_count: Math.max(1, endLine - lineNum + 1),
|
|
115
|
+
nesting_depth: nestingDepth,
|
|
116
|
+
});
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Exported Constants / Variables at module level
|
|
121
|
+
const constMatch = /^export\s+(?:const|let|var)\s+([A-Za-z0-9_]+)\s*(?::\s*([^=;]+))?\s*=/.exec(trimmed);
|
|
122
|
+
if (constMatch) {
|
|
123
|
+
const varName = constMatch[1];
|
|
124
|
+
const typeStr = constMatch[2] ? `: ${constMatch[2].trim()}` : "";
|
|
125
|
+
symbols.push({
|
|
126
|
+
name: varName,
|
|
127
|
+
kind: "property",
|
|
128
|
+
signature: `const ${varName}${typeStr}`,
|
|
129
|
+
visibility: "public",
|
|
130
|
+
line_start: lineNum,
|
|
131
|
+
line_end: lineNum,
|
|
132
|
+
line_count: 1,
|
|
133
|
+
});
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Method or Property in class
|
|
138
|
+
if (currentClass) {
|
|
139
|
+
// Class property: public foo: string; or private bar = 1;
|
|
140
|
+
const propMatch = /^(?:(public|protected|private)\s+)?(?:readonly\s+)?([A-Za-z0-9_]+)\s*(?:\?|\!)?\s*(?::\s*([^;={]+))?(?:\s*=[^;]+)?;\s*$/.exec(trimmed);
|
|
141
|
+
if (propMatch && !["return", "const", "let", "var", "throw", "import"].includes(propMatch[2])) {
|
|
142
|
+
const vis = (propMatch[1] as Visibility) || "public";
|
|
143
|
+
const propName = propMatch[2];
|
|
144
|
+
const typeStr = propMatch[3] ? `: ${propMatch[3].trim()}` : "";
|
|
145
|
+
symbols.push({
|
|
146
|
+
name: `${currentClass}::${propName}`,
|
|
147
|
+
kind: "property",
|
|
148
|
+
signature: `${vis} ${propName}${typeStr}`,
|
|
149
|
+
visibility: vis,
|
|
150
|
+
line_start: lineNum,
|
|
151
|
+
line_end: lineNum,
|
|
152
|
+
line_count: 1,
|
|
153
|
+
});
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const methodMatch = /^(?:(public|protected|private)\s+)?(?:static\s+)?(?:async\s+)?([A-Za-z0-9_]+)\s*\(([^)]*)\)(?:\s*:\s*([^;{]+))?/.exec(trimmed);
|
|
158
|
+
if (methodMatch && !["if", "for", "while", "switch", "catch"].includes(methodMatch[2])) {
|
|
159
|
+
const vis = (methodMatch[1] as Visibility) || "public";
|
|
160
|
+
const name = methodMatch[2];
|
|
161
|
+
const params = methodMatch[3];
|
|
162
|
+
const retType = methodMatch[4] ? `: ${methodMatch[4].trim()}` : "";
|
|
163
|
+
const endLine = findBraceBlockEnd(lines, i);
|
|
164
|
+
const nestingDepth = calculateControlFlowNesting(lines, i, endLine - 1);
|
|
165
|
+
symbols.push({
|
|
166
|
+
name: `${currentClass}::${name}`,
|
|
167
|
+
kind: "method",
|
|
168
|
+
signature: `${vis} ${name}(${params})${retType}`,
|
|
169
|
+
visibility: vis,
|
|
170
|
+
line_start: lineNum,
|
|
171
|
+
line_end: endLine,
|
|
172
|
+
line_count: Math.max(1, endLine - lineNum + 1),
|
|
173
|
+
nesting_depth: nestingDepth,
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return { symbols, dependencies };
|
|
180
|
+
}
|
|
181
|
+
}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import type {
|
|
4
|
+
SchemaFieldSummary,
|
|
5
|
+
SchemaMetadata,
|
|
6
|
+
StackSchemaProvider,
|
|
7
|
+
} from "../schema-provider.interface.ts";
|
|
8
|
+
|
|
9
|
+
export class LaravelMigrationSchemaProvider implements StackSchemaProvider {
|
|
10
|
+
public readonly id = "laravel-migration";
|
|
11
|
+
public readonly name = "Laravel Migration Schema Provider";
|
|
12
|
+
|
|
13
|
+
public canHandle(projectRoot: string): boolean {
|
|
14
|
+
const migrationsDir = path.join(projectRoot, "database", "migrations");
|
|
15
|
+
return fs.existsSync(migrationsDir) || fs.existsSync(path.join(projectRoot, "artisan"));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public resolveEntitySchema(
|
|
19
|
+
projectRoot: string,
|
|
20
|
+
entitySymbol: string,
|
|
21
|
+
entityFilePath?: string
|
|
22
|
+
): SchemaMetadata | null {
|
|
23
|
+
const migrationsDir = path.join(projectRoot, "database", "migrations");
|
|
24
|
+
if (!fs.existsSync(migrationsDir)) return null;
|
|
25
|
+
|
|
26
|
+
const tableName = this.resolveTableName(projectRoot, entitySymbol, entityFilePath);
|
|
27
|
+
if (!tableName) return null;
|
|
28
|
+
|
|
29
|
+
const migrationFiles = this.findMigrationFilesForTable(migrationsDir, tableName);
|
|
30
|
+
if (migrationFiles.length === 0) return null;
|
|
31
|
+
|
|
32
|
+
const fieldsMap = new Map<string, SchemaFieldSummary>();
|
|
33
|
+
let primarySourceFile = migrationFiles[0];
|
|
34
|
+
|
|
35
|
+
for (const migFile of migrationFiles) {
|
|
36
|
+
const fullPath = path.join(migrationsDir, migFile);
|
|
37
|
+
try {
|
|
38
|
+
const content = fs.readFileSync(fullPath, "utf-8");
|
|
39
|
+
this.extractColumnsFromMigration(content, tableName, fieldsMap);
|
|
40
|
+
} catch {
|
|
41
|
+
// Skip unreadable files
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (fieldsMap.size === 0) return null;
|
|
46
|
+
|
|
47
|
+
const relSourceFile = path.relative(projectRoot, path.join(migrationsDir, primarySourceFile));
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
entityName: entitySymbol,
|
|
51
|
+
tableName,
|
|
52
|
+
schemaSourceFile: relSourceFile,
|
|
53
|
+
fields: Array.from(fieldsMap.values()),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
private resolveTableName(
|
|
58
|
+
projectRoot: string,
|
|
59
|
+
entitySymbol: string,
|
|
60
|
+
entityFilePath?: string
|
|
61
|
+
): string {
|
|
62
|
+
// 1. Check if model file defines explicit protected $table = '...';
|
|
63
|
+
if (entityFilePath) {
|
|
64
|
+
const fullEntityPath = path.isAbsolute(entityFilePath)
|
|
65
|
+
? entityFilePath
|
|
66
|
+
: path.join(projectRoot, entityFilePath);
|
|
67
|
+
if (fs.existsSync(fullEntityPath)) {
|
|
68
|
+
try {
|
|
69
|
+
const content = fs.readFileSync(fullEntityPath, "utf-8");
|
|
70
|
+
const tableMatch = /protected\s+\$table\s*=\s*['"]([^'"]+)['"]/.exec(content);
|
|
71
|
+
if (tableMatch) {
|
|
72
|
+
return tableMatch[1];
|
|
73
|
+
}
|
|
74
|
+
} catch {}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// 2. Derive snake_case plural from entity name (e.g. OrderItem -> order_items)
|
|
79
|
+
return this.pluralize(this.toSnakeCase(entitySymbol));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
private toSnakeCase(str: string): string {
|
|
83
|
+
return str
|
|
84
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1_$2")
|
|
85
|
+
.replace(/([A-Z]+)([A-Z][a-z])/g, "$1_$2")
|
|
86
|
+
.toLowerCase();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
private pluralize(snake: string): string {
|
|
90
|
+
if (snake.endsWith("y") && !/[aeiou]y$/i.test(snake)) {
|
|
91
|
+
return `${snake.slice(0, -1)}ies`;
|
|
92
|
+
}
|
|
93
|
+
if (/(?:s|x|z|ch|sh)$/i.test(snake)) {
|
|
94
|
+
return `${snake}es`;
|
|
95
|
+
}
|
|
96
|
+
return `${snake}s`;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
private findMigrationFilesForTable(migrationsDir: string, tableName: string): string[] {
|
|
100
|
+
const files = fs.readdirSync(migrationsDir).filter((f) => f.endsWith(".php")).sort();
|
|
101
|
+
const matched: string[] = [];
|
|
102
|
+
|
|
103
|
+
// Priority 1: *_create_${tableName}_table.php
|
|
104
|
+
const createTarget = `_create_${tableName}_table.php`;
|
|
105
|
+
for (const f of files) {
|
|
106
|
+
if (f.endsWith(createTarget)) {
|
|
107
|
+
matched.push(f);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Priority 2: Other migration files altering this table
|
|
112
|
+
for (const f of files) {
|
|
113
|
+
if (matched.includes(f)) continue;
|
|
114
|
+
if (f.includes(`_${tableName}_`)) {
|
|
115
|
+
matched.push(f);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// If still not found by filename, do a lightweight content search
|
|
120
|
+
if (matched.length === 0) {
|
|
121
|
+
for (const f of files) {
|
|
122
|
+
try {
|
|
123
|
+
const content = fs.readFileSync(path.join(migrationsDir, f), "utf-8");
|
|
124
|
+
if (
|
|
125
|
+
content.includes(`Schema::create('${tableName}'`) ||
|
|
126
|
+
content.includes(`Schema::create("${tableName}"`) ||
|
|
127
|
+
content.includes(`Schema::table('${tableName}'`) ||
|
|
128
|
+
content.includes(`Schema::table("${tableName}"`)
|
|
129
|
+
) {
|
|
130
|
+
matched.push(f);
|
|
131
|
+
}
|
|
132
|
+
} catch {}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return matched;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
private extractColumnsFromMigration(
|
|
140
|
+
content: string,
|
|
141
|
+
tableName: string,
|
|
142
|
+
fieldsMap: Map<string, SchemaFieldSummary>
|
|
143
|
+
): void {
|
|
144
|
+
const lines = content.split("\n");
|
|
145
|
+
|
|
146
|
+
for (const rawLine of lines) {
|
|
147
|
+
const line = rawLine.trim();
|
|
148
|
+
if (!line.includes("$table->")) continue;
|
|
149
|
+
|
|
150
|
+
// Special shorthand: $table->id();
|
|
151
|
+
if (/\$table->id\s*\(\s*\)/.test(line)) {
|
|
152
|
+
fieldsMap.set("id", { name: "id", type: "id", isPrimary: true });
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Special shorthand: $table->id('custom_id');
|
|
157
|
+
const customIdMatch = /\$table->id\s*\(\s*['"]([^'"]+)['"]\s*\)/.exec(line);
|
|
158
|
+
if (customIdMatch) {
|
|
159
|
+
fieldsMap.set(customIdMatch[1], { name: customIdMatch[1], type: "id", isPrimary: true });
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Special shorthand: $table->timestamps();
|
|
164
|
+
if (/\$table->timestamps\s*\(/.test(line)) {
|
|
165
|
+
if (!fieldsMap.has("created_at")) {
|
|
166
|
+
fieldsMap.set("created_at", { name: "created_at", type: "timestamp", nullable: true });
|
|
167
|
+
}
|
|
168
|
+
if (!fieldsMap.has("updated_at")) {
|
|
169
|
+
fieldsMap.set("updated_at", { name: "updated_at", type: "timestamp", nullable: true });
|
|
170
|
+
}
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Special shorthand: $table->softDeletes();
|
|
175
|
+
if (/\$table->softDeletes\s*\(/.test(line)) {
|
|
176
|
+
if (!fieldsMap.has("deleted_at")) {
|
|
177
|
+
fieldsMap.set("deleted_at", { name: "deleted_at", type: "timestamp", nullable: true });
|
|
178
|
+
}
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// Special shorthand: $table->rememberToken();
|
|
183
|
+
if (/\$table->rememberToken\s*\(/.test(line)) {
|
|
184
|
+
if (!fieldsMap.has("remember_token")) {
|
|
185
|
+
fieldsMap.set("remember_token", { name: "remember_token", type: "string", nullable: true });
|
|
186
|
+
}
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// Standard column definitions: $table->type('column_name', ...)
|
|
191
|
+
const colMatch = /\$table->([a-zA-Z0-9_]+)\s*\(\s*['"]([^'"]+)['"]/.exec(line);
|
|
192
|
+
if (colMatch) {
|
|
193
|
+
const type = colMatch[1];
|
|
194
|
+
const name = colMatch[2];
|
|
195
|
+
|
|
196
|
+
// Skip non-column fluent methods like foreign, dropColumn, index, primary
|
|
197
|
+
if (["index", "unique", "primary", "foreign", "dropColumn", "dropForeign"].includes(type)) {
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const nullable = line.includes("->nullable()");
|
|
202
|
+
const isPrimary = type === "increments" || type === "bigIncrements" || line.includes("->primary()");
|
|
203
|
+
const isForeign = type.startsWith("foreign") || name.endsWith("_id");
|
|
204
|
+
|
|
205
|
+
fieldsMap.set(name, {
|
|
206
|
+
name,
|
|
207
|
+
type,
|
|
208
|
+
nullable: nullable || undefined,
|
|
209
|
+
isPrimary: isPrimary || undefined,
|
|
210
|
+
isForeign: isForeign || undefined,
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import type {
|
|
4
|
+
SchemaFieldSummary,
|
|
5
|
+
SchemaMetadata,
|
|
6
|
+
StackSchemaProvider,
|
|
7
|
+
} from "../schema-provider.interface.ts";
|
|
8
|
+
|
|
9
|
+
export class PrismaSchemaProvider implements StackSchemaProvider {
|
|
10
|
+
public readonly id = "prisma";
|
|
11
|
+
public readonly name = "Prisma Schema Provider";
|
|
12
|
+
|
|
13
|
+
public canHandle(projectRoot: string): boolean {
|
|
14
|
+
return (
|
|
15
|
+
fs.existsSync(path.join(projectRoot, "prisma", "schema.prisma")) ||
|
|
16
|
+
fs.existsSync(path.join(projectRoot, "schema.prisma"))
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public resolveEntitySchema(
|
|
21
|
+
projectRoot: string,
|
|
22
|
+
entitySymbol: string,
|
|
23
|
+
_entityFilePath?: string
|
|
24
|
+
): SchemaMetadata | null {
|
|
25
|
+
const candidatePaths = [
|
|
26
|
+
path.join(projectRoot, "prisma", "schema.prisma"),
|
|
27
|
+
path.join(projectRoot, "schema.prisma"),
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
let schemaPath: string | null = null;
|
|
31
|
+
for (const p of candidatePaths) {
|
|
32
|
+
if (fs.existsSync(p)) {
|
|
33
|
+
schemaPath = p;
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (!schemaPath) return null;
|
|
39
|
+
|
|
40
|
+
try {
|
|
41
|
+
const content = fs.readFileSync(schemaPath, "utf-8");
|
|
42
|
+
const modelRegex = new RegExp(`model\\s+${entitySymbol}\\s*\\{([^}]+)\\}`, "m");
|
|
43
|
+
const match = modelRegex.exec(content);
|
|
44
|
+
if (!match) return null;
|
|
45
|
+
|
|
46
|
+
const body = match[1];
|
|
47
|
+
const lines = body.split("\n");
|
|
48
|
+
const fields: SchemaFieldSummary[] = [];
|
|
49
|
+
|
|
50
|
+
for (const rawLine of lines) {
|
|
51
|
+
const line = rawLine.trim();
|
|
52
|
+
if (!line || line.startsWith("//") || line.startsWith("@@")) continue;
|
|
53
|
+
|
|
54
|
+
// Pattern: fieldName FieldType attributes...
|
|
55
|
+
const parts = line.split(/\s+/);
|
|
56
|
+
if (parts.length >= 2) {
|
|
57
|
+
const name = parts[0];
|
|
58
|
+
let type = parts[1];
|
|
59
|
+
const nullable = type.endsWith("?");
|
|
60
|
+
if (nullable) {
|
|
61
|
+
type = type.slice(0, -1);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const isPrimary = line.includes("@id");
|
|
65
|
+
const isForeign = line.includes("@relation");
|
|
66
|
+
|
|
67
|
+
fields.push({
|
|
68
|
+
name,
|
|
69
|
+
type,
|
|
70
|
+
nullable: nullable || undefined,
|
|
71
|
+
isPrimary: isPrimary || undefined,
|
|
72
|
+
isForeign: isForeign || undefined,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (fields.length === 0) return null;
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
entityName: entitySymbol,
|
|
81
|
+
tableName: entitySymbol.toLowerCase(),
|
|
82
|
+
schemaSourceFile: path.relative(projectRoot, schemaPath),
|
|
83
|
+
fields,
|
|
84
|
+
};
|
|
85
|
+
} catch {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|