project-context-map-mcp 0.1.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/.claude/skills/project-context-map/SKILL.md +43 -0
- package/README.md +500 -0
- package/package.json +39 -0
- package/scripts/hooks/post-commit +2 -0
- package/scripts/hooks/post-merge +2 -0
- package/scripts/hooks/post-push +2 -0
- package/scripts/install-git-hooks.js +32 -0
- package/scripts/refresh-map.js +26 -0
- package/src/claude.js +86 -0
- package/src/cli.js +110 -0
- package/src/git.js +96 -0
- package/src/indexer.js +780 -0
- package/src/ranker.js +107 -0
- package/src/server.js +363 -0
- package/src/utils.js +155 -0
package/src/ranker.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { tokenize } from "./utils.js";
|
|
2
|
+
|
|
3
|
+
const STOP_WORDS = new Set([
|
|
4
|
+
"the",
|
|
5
|
+
"and",
|
|
6
|
+
"for",
|
|
7
|
+
"with",
|
|
8
|
+
"that",
|
|
9
|
+
"this",
|
|
10
|
+
"from",
|
|
11
|
+
"into",
|
|
12
|
+
"your",
|
|
13
|
+
"have",
|
|
14
|
+
"will",
|
|
15
|
+
"need",
|
|
16
|
+
"make",
|
|
17
|
+
"change",
|
|
18
|
+
"changes",
|
|
19
|
+
"file",
|
|
20
|
+
"files",
|
|
21
|
+
"bug",
|
|
22
|
+
"fix"
|
|
23
|
+
]);
|
|
24
|
+
|
|
25
|
+
function scoreTokenMatch(haystack, token, points) {
|
|
26
|
+
return haystack.includes(token) ? points : 0;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function findRelevantFiles(task, projectMap, limit = 8) {
|
|
30
|
+
const rawTokens = tokenize(task).filter((token) => !STOP_WORDS.has(token));
|
|
31
|
+
const tokenSet = [...new Set(rawTokens)];
|
|
32
|
+
const hotspotMap = new Map((projectMap.hotspots || []).map((item) => [item.path, item.count]));
|
|
33
|
+
const recentChangeMap = new Map();
|
|
34
|
+
|
|
35
|
+
for (const commit of projectMap.recent_changes || []) {
|
|
36
|
+
for (const filePath of commit.files || []) {
|
|
37
|
+
recentChangeMap.set(filePath, (recentChangeMap.get(filePath) || 0) + 1);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const ranked = (projectMap.files || [])
|
|
42
|
+
.map((file) => {
|
|
43
|
+
let score = 0;
|
|
44
|
+
const reasons = [];
|
|
45
|
+
const pathLower = (file.path || "").toLowerCase();
|
|
46
|
+
const roleLower = (file.role || "").toLowerCase();
|
|
47
|
+
const summaryLower = (file.summary || "").toLowerCase();
|
|
48
|
+
const tagsLower = (file.tags || []).join(" ").toLowerCase();
|
|
49
|
+
const moduleLower = (file.module || "").toLowerCase();
|
|
50
|
+
const relatedLower = (file.related_to || []).join(" ").toLowerCase();
|
|
51
|
+
|
|
52
|
+
for (const token of tokenSet) {
|
|
53
|
+
const tokenScore =
|
|
54
|
+
scoreTokenMatch(pathLower, token, 12) +
|
|
55
|
+
scoreTokenMatch(roleLower, token, 7) +
|
|
56
|
+
scoreTokenMatch(summaryLower, token, 6) +
|
|
57
|
+
scoreTokenMatch(tagsLower, token, 5) +
|
|
58
|
+
scoreTokenMatch(moduleLower, token, 5) +
|
|
59
|
+
scoreTokenMatch(relatedLower, token, 3);
|
|
60
|
+
|
|
61
|
+
if (tokenScore > 0) {
|
|
62
|
+
score += tokenScore;
|
|
63
|
+
reasons.push(`matched "${token}"`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const hotspotScore = hotspotMap.get(file.path) || 0;
|
|
68
|
+
if (hotspotScore > 0) {
|
|
69
|
+
score += Math.min(hotspotScore * 2, 10);
|
|
70
|
+
reasons.push(`recent hotspot x${hotspotScore}`);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const recentScore = recentChangeMap.get(file.path) || 0;
|
|
74
|
+
if (recentScore > 0) {
|
|
75
|
+
score += Math.min(recentScore * 2, 8);
|
|
76
|
+
reasons.push(`changed recently x${recentScore}`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
path: file.path,
|
|
81
|
+
module: file.module,
|
|
82
|
+
role: file.role,
|
|
83
|
+
score,
|
|
84
|
+
lastChangedReason: file.last_changed_reason || "",
|
|
85
|
+
reasons: [...new Set(reasons)]
|
|
86
|
+
};
|
|
87
|
+
})
|
|
88
|
+
.filter((item) => item.score > 0)
|
|
89
|
+
.sort((left, right) => right.score - left.score || left.path.localeCompare(right.path))
|
|
90
|
+
.slice(0, limit);
|
|
91
|
+
|
|
92
|
+
const maxScore = ranked[0]?.score || 0;
|
|
93
|
+
const confidence = maxScore === 0 ? 0 : Math.min(100, Math.round((maxScore / 35) * 100));
|
|
94
|
+
const matchedModules = new Set(ranked.map((item) => item.module).filter(Boolean));
|
|
95
|
+
const avoidReading = (projectMap.modules || [])
|
|
96
|
+
.filter((module) => !matchedModules.has(module.name))
|
|
97
|
+
.slice(0, 5)
|
|
98
|
+
.map((module) => module.name);
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
task,
|
|
102
|
+
tokens: tokenSet,
|
|
103
|
+
confidence,
|
|
104
|
+
matches: ranked,
|
|
105
|
+
avoidReading
|
|
106
|
+
};
|
|
107
|
+
}
|
package/src/server.js
ADDED
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
4
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
5
|
+
import {
|
|
6
|
+
CallToolRequestSchema,
|
|
7
|
+
ListToolsRequestSchema
|
|
8
|
+
} from "@modelcontextprotocol/sdk/types.js";
|
|
9
|
+
import {
|
|
10
|
+
explainFile,
|
|
11
|
+
getModuleContext,
|
|
12
|
+
loadProjectMap,
|
|
13
|
+
readFilesSparse,
|
|
14
|
+
readProjectContext,
|
|
15
|
+
refreshProjectMap,
|
|
16
|
+
writeAndLog
|
|
17
|
+
} from "./indexer.js";
|
|
18
|
+
import { getRecentGitChanges } from "./git.js";
|
|
19
|
+
import { findRelevantFiles } from "./ranker.js";
|
|
20
|
+
import { resolveProjectRoot } from "./utils.js";
|
|
21
|
+
|
|
22
|
+
function asTextResult(payload) {
|
|
23
|
+
return {
|
|
24
|
+
content: [
|
|
25
|
+
{
|
|
26
|
+
type: "text",
|
|
27
|
+
text: JSON.stringify(payload, null, 2)
|
|
28
|
+
}
|
|
29
|
+
]
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function getRoot(args) {
|
|
34
|
+
return resolveProjectRoot(args?.projectRoot);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function createServer() {
|
|
38
|
+
return new Server(
|
|
39
|
+
{
|
|
40
|
+
name: "project-context-map",
|
|
41
|
+
version: "0.1.0"
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
capabilities: {
|
|
45
|
+
tools: {}
|
|
46
|
+
},
|
|
47
|
+
instructions:
|
|
48
|
+
"Use project map tools before reading many files. Prefer get_project_summary and find_relevant_files first, then inspect only the returned files."
|
|
49
|
+
}
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const server = createServer();
|
|
54
|
+
|
|
55
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
56
|
+
return {
|
|
57
|
+
tools: [
|
|
58
|
+
{
|
|
59
|
+
name: "read_project_context",
|
|
60
|
+
description: "Returns the generated .project.md content for the repository.",
|
|
61
|
+
inputSchema: {
|
|
62
|
+
type: "object",
|
|
63
|
+
properties: {
|
|
64
|
+
projectRoot: {
|
|
65
|
+
type: "string",
|
|
66
|
+
description: "Optional project root. Defaults to PROJECT_ROOT or current working directory."
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
name: "query_topology",
|
|
73
|
+
description:
|
|
74
|
+
"Takes a natural language query and returns matched file paths from the query index, along with import/dependency topology context.",
|
|
75
|
+
inputSchema: {
|
|
76
|
+
type: "object",
|
|
77
|
+
required: ["query"],
|
|
78
|
+
properties: {
|
|
79
|
+
projectRoot: {
|
|
80
|
+
type: "string",
|
|
81
|
+
description: "Optional project root. Defaults to PROJECT_ROOT or current working directory."
|
|
82
|
+
},
|
|
83
|
+
query: {
|
|
84
|
+
type: "string",
|
|
85
|
+
description: "Natural-language query."
|
|
86
|
+
},
|
|
87
|
+
limit: {
|
|
88
|
+
type: "number",
|
|
89
|
+
description: "Maximum number of file paths to return.",
|
|
90
|
+
default: 8
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
name: "read_files_sparse",
|
|
97
|
+
description:
|
|
98
|
+
"Read only a small set of indexed files. Rejects paths that are outside the project root or missing from the project topology.",
|
|
99
|
+
inputSchema: {
|
|
100
|
+
type: "object",
|
|
101
|
+
required: ["paths"],
|
|
102
|
+
properties: {
|
|
103
|
+
projectRoot: {
|
|
104
|
+
type: "string",
|
|
105
|
+
description: "Optional project root. Defaults to PROJECT_ROOT or current working directory."
|
|
106
|
+
},
|
|
107
|
+
paths: {
|
|
108
|
+
type: "array",
|
|
109
|
+
items: { type: "string" },
|
|
110
|
+
description: "Repository-relative file paths."
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
name: "write_and_log",
|
|
117
|
+
description: "Writes file content and appends an entry to the .project.md Change Log.",
|
|
118
|
+
inputSchema: {
|
|
119
|
+
type: "object",
|
|
120
|
+
required: ["path", "content"],
|
|
121
|
+
properties: {
|
|
122
|
+
projectRoot: {
|
|
123
|
+
type: "string",
|
|
124
|
+
description: "Optional project root. Defaults to PROJECT_ROOT or current working directory."
|
|
125
|
+
},
|
|
126
|
+
path: {
|
|
127
|
+
type: "string",
|
|
128
|
+
description: "Repository-relative path to write."
|
|
129
|
+
},
|
|
130
|
+
content: {
|
|
131
|
+
type: "string",
|
|
132
|
+
description: "Full file content to write."
|
|
133
|
+
},
|
|
134
|
+
summary: {
|
|
135
|
+
type: "string",
|
|
136
|
+
description: "Short Change Log summary."
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
name: "get_project_summary",
|
|
143
|
+
description:
|
|
144
|
+
"Load the current project context map and return the project summary, modules, stack, and hotspots.",
|
|
145
|
+
inputSchema: {
|
|
146
|
+
type: "object",
|
|
147
|
+
properties: {
|
|
148
|
+
projectRoot: {
|
|
149
|
+
type: "string",
|
|
150
|
+
description: "Optional project root. Defaults to PROJECT_ROOT or current working directory."
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
name: "refresh_project_map",
|
|
157
|
+
description:
|
|
158
|
+
"Regenerate .project-map.json and .project-map.md for the current repository.",
|
|
159
|
+
inputSchema: {
|
|
160
|
+
type: "object",
|
|
161
|
+
properties: {
|
|
162
|
+
projectRoot: {
|
|
163
|
+
type: "string",
|
|
164
|
+
description: "Optional project root. Defaults to PROJECT_ROOT or current working directory."
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
name: "get_module_context",
|
|
171
|
+
description:
|
|
172
|
+
"Return context for a module or feature area, including module summary, files, recent commits, and likely impact paths.",
|
|
173
|
+
inputSchema: {
|
|
174
|
+
type: "object",
|
|
175
|
+
required: ["module"],
|
|
176
|
+
properties: {
|
|
177
|
+
projectRoot: {
|
|
178
|
+
type: "string",
|
|
179
|
+
description: "Optional project root. Defaults to PROJECT_ROOT or current working directory."
|
|
180
|
+
},
|
|
181
|
+
module: {
|
|
182
|
+
type: "string",
|
|
183
|
+
description: "Module or feature name."
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
{
|
|
189
|
+
name: "find_relevant_files",
|
|
190
|
+
description:
|
|
191
|
+
"Rank the most relevant files for a natural-language task so the coding agent can open only a small subset.",
|
|
192
|
+
inputSchema: {
|
|
193
|
+
type: "object",
|
|
194
|
+
required: ["task"],
|
|
195
|
+
properties: {
|
|
196
|
+
projectRoot: {
|
|
197
|
+
type: "string",
|
|
198
|
+
description: "Optional project root. Defaults to PROJECT_ROOT or current working directory."
|
|
199
|
+
},
|
|
200
|
+
task: {
|
|
201
|
+
type: "string",
|
|
202
|
+
description: "Natural-language change request, bug description, or feature request."
|
|
203
|
+
},
|
|
204
|
+
limit: {
|
|
205
|
+
type: "number",
|
|
206
|
+
description: "Maximum number of files to return.",
|
|
207
|
+
default: 8
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
name: "get_recent_git_changes",
|
|
214
|
+
description:
|
|
215
|
+
"Summarize git commits and hotspots from the last N days. Default is 5 days.",
|
|
216
|
+
inputSchema: {
|
|
217
|
+
type: "object",
|
|
218
|
+
properties: {
|
|
219
|
+
projectRoot: {
|
|
220
|
+
type: "string",
|
|
221
|
+
description: "Optional project root. Defaults to PROJECT_ROOT or current working directory."
|
|
222
|
+
},
|
|
223
|
+
days: {
|
|
224
|
+
type: "number",
|
|
225
|
+
description: "How many days of git history to inspect.",
|
|
226
|
+
default: 5
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
},
|
|
231
|
+
{
|
|
232
|
+
name: "explain_file_role",
|
|
233
|
+
description:
|
|
234
|
+
"Explain why a file exists, which module it belongs to, and whether it changed recently.",
|
|
235
|
+
inputSchema: {
|
|
236
|
+
type: "object",
|
|
237
|
+
required: ["path"],
|
|
238
|
+
properties: {
|
|
239
|
+
projectRoot: {
|
|
240
|
+
type: "string",
|
|
241
|
+
description: "Optional project root. Defaults to PROJECT_ROOT or current working directory."
|
|
242
|
+
},
|
|
243
|
+
path: {
|
|
244
|
+
type: "string",
|
|
245
|
+
description: "Repository-relative file path."
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
]
|
|
251
|
+
};
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
255
|
+
const { name, arguments: args = {} } = request.params;
|
|
256
|
+
|
|
257
|
+
if (name === "read_project_context") {
|
|
258
|
+
return asTextResult(readProjectContext(getRoot(args)));
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if (name === "query_topology") {
|
|
262
|
+
const projectMap = loadProjectMap(getRoot(args));
|
|
263
|
+
const result = findRelevantFiles(args.query, projectMap, Number(args.limit || 8));
|
|
264
|
+
const matchedPaths = result.matches.map((item) => item.path);
|
|
265
|
+
const topology = matchedPaths.map((filePath) => {
|
|
266
|
+
const file = projectMap.files.find((item) => item.path === filePath);
|
|
267
|
+
return {
|
|
268
|
+
path: filePath,
|
|
269
|
+
module: file?.module || "",
|
|
270
|
+
dependencies: file?.dependencies || [],
|
|
271
|
+
usedBy: file?.used_by || []
|
|
272
|
+
};
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
return asTextResult({
|
|
276
|
+
query: args.query,
|
|
277
|
+
matchedPaths,
|
|
278
|
+
confidence: result.confidence,
|
|
279
|
+
matches: result.matches,
|
|
280
|
+
topology
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
if (name === "read_files_sparse") {
|
|
285
|
+
return asTextResult(readFilesSparse(getRoot(args), args.paths || []));
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
if (name === "write_and_log") {
|
|
289
|
+
return asTextResult(writeAndLog(getRoot(args), args.path, args.content, args.summary || ""));
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (name === "get_project_summary") {
|
|
293
|
+
const projectMap = loadProjectMap(getRoot(args));
|
|
294
|
+
return asTextResult({
|
|
295
|
+
projectRoot: projectMap.projectRoot,
|
|
296
|
+
generatedAt: projectMap.generatedAt,
|
|
297
|
+
projectSummary: projectMap.project_summary,
|
|
298
|
+
techStack: projectMap.tech_stack,
|
|
299
|
+
stats: projectMap.stats,
|
|
300
|
+
entrypoints: projectMap.entrypoints,
|
|
301
|
+
topModules: projectMap.modules.slice(0, 12),
|
|
302
|
+
hotspots: (projectMap.hotspots || []).slice(0, 10)
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (name === "refresh_project_map") {
|
|
307
|
+
const result = refreshProjectMap(getRoot(args));
|
|
308
|
+
return asTextResult({
|
|
309
|
+
message: "Project map refreshed.",
|
|
310
|
+
jsonPath: result.jsonPath,
|
|
311
|
+
markdownPath: result.markdownPath,
|
|
312
|
+
projectMarkdownPath: result.projectMarkdownPath,
|
|
313
|
+
generatedAt: result.projectMap.generatedAt,
|
|
314
|
+
totalFiles: result.projectMap.stats.totalFiles
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
if (name === "get_module_context") {
|
|
319
|
+
const result = getModuleContext(getRoot(args), args.module);
|
|
320
|
+
|
|
321
|
+
if (!result) {
|
|
322
|
+
return asTextResult({
|
|
323
|
+
error: `Module not found in project map: ${args.module}`
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
return asTextResult(result);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
if (name === "find_relevant_files") {
|
|
331
|
+
const projectMap = loadProjectMap(getRoot(args));
|
|
332
|
+
const result = findRelevantFiles(args.task, projectMap, Number(args.limit || 8));
|
|
333
|
+
return asTextResult(result);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
if (name === "get_recent_git_changes") {
|
|
337
|
+
const result = getRecentGitChanges(getRoot(args), Number(args.days || 5));
|
|
338
|
+
return asTextResult(result);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
if (name === "explain_file_role") {
|
|
342
|
+
const result = explainFile(getRoot(args), args.path);
|
|
343
|
+
|
|
344
|
+
if (!result) {
|
|
345
|
+
return asTextResult({
|
|
346
|
+
error: `File not found in project map: ${args.path}`
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
return asTextResult(result);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
throw new Error(`Unknown tool: ${name}`);
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
export async function startServer() {
|
|
357
|
+
const transport = new StdioServerTransport();
|
|
358
|
+
await server.connect(transport);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
362
|
+
await startServer();
|
|
363
|
+
}
|
package/src/utils.js
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
export const MAP_JSON_FILE = ".project-map.json";
|
|
5
|
+
export const MAP_MD_FILE = ".project-map.md";
|
|
6
|
+
export const PROJECT_MD_FILE = ".project.md";
|
|
7
|
+
|
|
8
|
+
const IGNORED_DIRS = new Set([
|
|
9
|
+
".git",
|
|
10
|
+
".idea",
|
|
11
|
+
".next",
|
|
12
|
+
".turbo",
|
|
13
|
+
".vscode",
|
|
14
|
+
"coverage",
|
|
15
|
+
"dist",
|
|
16
|
+
"build",
|
|
17
|
+
"node_modules",
|
|
18
|
+
"target",
|
|
19
|
+
"__pycache__",
|
|
20
|
+
".venv",
|
|
21
|
+
"venv"
|
|
22
|
+
]);
|
|
23
|
+
|
|
24
|
+
const TEXT_EXTENSIONS = new Set([
|
|
25
|
+
".cjs",
|
|
26
|
+
".conf",
|
|
27
|
+
".css",
|
|
28
|
+
".env",
|
|
29
|
+
".html",
|
|
30
|
+
".java",
|
|
31
|
+
".js",
|
|
32
|
+
".json",
|
|
33
|
+
".jsx",
|
|
34
|
+
".kt",
|
|
35
|
+
".md",
|
|
36
|
+
".mjs",
|
|
37
|
+
".properties",
|
|
38
|
+
".py",
|
|
39
|
+
".rb",
|
|
40
|
+
".sql",
|
|
41
|
+
".sh",
|
|
42
|
+
".ts",
|
|
43
|
+
".tsx",
|
|
44
|
+
".txt",
|
|
45
|
+
".xml",
|
|
46
|
+
".yaml",
|
|
47
|
+
".yml"
|
|
48
|
+
]);
|
|
49
|
+
|
|
50
|
+
export function resolveProjectRoot(projectRoot) {
|
|
51
|
+
return path.resolve(projectRoot || process.env.PROJECT_ROOT || process.cwd());
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function pathExists(targetPath) {
|
|
55
|
+
return fs.existsSync(targetPath);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function readJsonIfExists(filePath) {
|
|
59
|
+
if (!pathExists(filePath)) {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return JSON.parse(fs.readFileSync(filePath, "utf8"));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function writeJson(filePath, value) {
|
|
67
|
+
fs.writeFileSync(filePath, `${JSON.stringify(value, null, 2)}\n`, "utf8");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function writeText(filePath, value) {
|
|
71
|
+
fs.writeFileSync(filePath, `${value}\n`, "utf8");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function readTextIfExists(filePath) {
|
|
75
|
+
if (!pathExists(filePath)) {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return fs.readFileSync(filePath, "utf8");
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function toPosixPath(value) {
|
|
83
|
+
return value.split(path.sep).join("/");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function relativeProjectPath(projectRoot, targetPath) {
|
|
87
|
+
return toPosixPath(path.relative(projectRoot, targetPath));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function isPathInsideProject(projectRoot, targetPath) {
|
|
91
|
+
const relativePath = path.relative(projectRoot, targetPath);
|
|
92
|
+
return relativePath === "" || (!relativePath.startsWith("..") && !path.isAbsolute(relativePath));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function shouldIgnoreDirectory(dirname) {
|
|
96
|
+
return IGNORED_DIRS.has(dirname);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function isTextLikeFile(filePath) {
|
|
100
|
+
return TEXT_EXTENSIONS.has(path.extname(filePath).toLowerCase());
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function walkProjectFiles(projectRoot) {
|
|
104
|
+
const files = [];
|
|
105
|
+
const queue = [projectRoot];
|
|
106
|
+
|
|
107
|
+
while (queue.length > 0) {
|
|
108
|
+
const current = queue.pop();
|
|
109
|
+
const entries = fs.readdirSync(current, { withFileTypes: true });
|
|
110
|
+
|
|
111
|
+
for (const entry of entries) {
|
|
112
|
+
const absolutePath = path.join(current, entry.name);
|
|
113
|
+
|
|
114
|
+
if (entry.isDirectory()) {
|
|
115
|
+
if (!shouldIgnoreDirectory(entry.name)) {
|
|
116
|
+
queue.push(absolutePath);
|
|
117
|
+
}
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (!entry.isFile()) {
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
files.push(absolutePath);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return files.sort((left, right) => left.localeCompare(right));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export function firstNonEmptyLine(filePath) {
|
|
133
|
+
if (!isTextLikeFile(filePath)) {
|
|
134
|
+
return "";
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
try {
|
|
138
|
+
const content = fs.readFileSync(filePath, "utf8");
|
|
139
|
+
const line = content
|
|
140
|
+
.split(/\r?\n/u)
|
|
141
|
+
.map((value) => value.trim())
|
|
142
|
+
.find(Boolean);
|
|
143
|
+
|
|
144
|
+
return line || "";
|
|
145
|
+
} catch {
|
|
146
|
+
return "";
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export function tokenize(text) {
|
|
151
|
+
return (text || "")
|
|
152
|
+
.toLowerCase()
|
|
153
|
+
.split(/[^a-z0-9]+/u)
|
|
154
|
+
.filter((token) => token.length > 2);
|
|
155
|
+
}
|