@nerviq/cli 1.29.0 → 1.29.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/CHANGELOG.md +1527 -1493
- package/README.md +550 -538
- package/SECURITY.md +82 -82
- package/bin/cli.js +2562 -2558
- package/docs/api-reference.md +356 -356
- package/docs/audit-fix.md +109 -0
- package/docs/autofix.md +3 -62
- package/docs/getting-started.md +1 -1
- package/docs/index.html +592 -592
- package/docs/integration-contracts.md +287 -287
- package/docs/maintenance.md +128 -128
- package/docs/new-platform-guide.md +202 -202
- package/docs/release-process.md +63 -0
- package/docs/shallow-risk.md +244 -244
- package/docs/why-nerviq.md +82 -82
- package/package.json +67 -67
- package/src/aider/activity.js +226 -226
- package/src/aider/context.js +162 -162
- package/src/aider/freshness.js +123 -123
- package/src/aider/techniques.js +3465 -3465
- package/src/audit/layers.js +180 -180
- package/src/audit.js +1032 -1032
- package/src/benchmark.js +299 -299
- package/src/codex/activity.js +324 -324
- package/src/codex/freshness.js +142 -142
- package/src/codex/techniques.js +4895 -4895
- package/src/context.js +326 -326
- package/src/continuous-ops.js +11 -1
- package/src/convert.js +340 -340
- package/src/copilot/config-parser.js +280 -280
- package/src/copilot/context.js +218 -218
- package/src/copilot/freshness.js +177 -177
- package/src/copilot/patch.js +238 -238
- package/src/copilot/techniques.js +3578 -3578
- package/src/cursor/freshness.js +194 -194
- package/src/cursor/patch.js +243 -243
- package/src/cursor/techniques.js +3735 -3735
- package/src/doctor.js +201 -201
- package/src/fix-engine.js +511 -8
- package/src/formatters/csv.js +86 -86
- package/src/formatters/junit.js +123 -123
- package/src/formatters/markdown.js +164 -164
- package/src/formatters/otel.js +151 -151
- package/src/freshness.js +156 -156
- package/src/gemini/activity.js +402 -402
- package/src/gemini/context.js +290 -290
- package/src/gemini/freshness.js +183 -183
- package/src/gemini/patch.js +229 -229
- package/src/gemini/techniques.js +3811 -3811
- package/src/governance.js +533 -533
- package/src/harmony/audit.js +306 -306
- package/src/i18n.js +63 -63
- package/src/insights.js +119 -119
- package/src/integrations.js +134 -134
- package/src/locales/en.json +33 -33
- package/src/locales/es.json +33 -33
- package/src/migrate.js +354 -354
- package/src/opencode/activity.js +286 -286
- package/src/opencode/freshness.js +137 -137
- package/src/opencode/techniques.js +3450 -3450
- package/src/setup/analysis.js +12 -12
- package/src/setup.js +7 -6
- package/src/shallow-risk/index.js +56 -56
- package/src/shallow-risk/patterns/agent-config-cross-platform-drift.js +50 -50
- package/src/shallow-risk/patterns/agent-config-dangerous-autoapprove.js +46 -46
- package/src/shallow-risk/patterns/agent-config-deprecated-keys.js +46 -46
- package/src/shallow-risk/patterns/agent-config-missing-file.js +317 -317
- package/src/shallow-risk/patterns/agent-config-secret-literal.js +49 -49
- package/src/shallow-risk/patterns/agent-config-stack-contradiction.js +34 -34
- package/src/shallow-risk/patterns/hook-script-missing.js +70 -70
- package/src/shallow-risk/patterns/mcp-server-no-allowlist.js +52 -52
- package/src/shallow-risk/shared.js +648 -648
- package/src/source-urls.js +295 -295
- package/src/state-paths.js +85 -85
- package/src/supplemental-checks.js +805 -805
- package/src/telemetry.js +160 -160
- package/src/windsurf/context.js +359 -359
- package/src/windsurf/freshness.js +194 -194
- package/src/windsurf/patch.js +231 -231
- package/src/windsurf/techniques.js +3779 -3779
package/src/context.js
CHANGED
|
@@ -1,326 +1,326 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Project context scanner - reads project files to evaluate against techniques.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
const fs = require('fs');
|
|
6
|
-
const path = require('path');
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Scans and caches project files to provide fast lookups for technique checks.
|
|
10
|
-
* Reads the project directory on construction and exposes helpers for file content, JSON, and stack detection.
|
|
11
|
-
*/
|
|
12
|
-
class ProjectContext {
|
|
13
|
-
constructor(dir) {
|
|
14
|
-
this.dir = dir;
|
|
15
|
-
this.files = [];
|
|
16
|
-
this._cache = {};
|
|
17
|
-
this._dependencyCache = null;
|
|
18
|
-
this._scan();
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
_scan() {
|
|
22
|
-
try {
|
|
23
|
-
const entries = fs.readdirSync(this.dir, { withFileTypes: true });
|
|
24
|
-
for (const entry of entries) {
|
|
25
|
-
if (entry.isFile()) {
|
|
26
|
-
if (entry.name === '.DS_Store') continue;
|
|
27
|
-
this.files.push(entry.name);
|
|
28
|
-
} else if (entry.isDirectory()) {
|
|
29
|
-
if (entry.name.startsWith('.') && entry.name !== '.claude') continue;
|
|
30
|
-
if (entry.name === 'node_modules' || entry.name === '__pycache__') continue;
|
|
31
|
-
this.files.push(entry.name + '/');
|
|
32
|
-
// Scan .claude/ subdirectories
|
|
33
|
-
if (entry.name === '.claude') {
|
|
34
|
-
this._scanSubdir('.claude');
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
} catch (err) {
|
|
39
|
-
// Directory might not be readable
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
_scanSubdir(subdir) {
|
|
44
|
-
try {
|
|
45
|
-
const fullPath = path.join(this.dir, subdir);
|
|
46
|
-
const entries = fs.readdirSync(fullPath, { withFileTypes: true });
|
|
47
|
-
for (const entry of entries) {
|
|
48
|
-
if (entry.isDirectory()) {
|
|
49
|
-
this._scanSubdir(path.join(subdir, entry.name));
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
} catch (err) {
|
|
53
|
-
// Subdirectory might not exist
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
hasDir(dirPath) {
|
|
58
|
-
const fullPath = path.join(this.dir, dirPath);
|
|
59
|
-
try {
|
|
60
|
-
return fs.statSync(fullPath).isDirectory();
|
|
61
|
-
} catch {
|
|
62
|
-
return false;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
dirFiles(dirPath) {
|
|
67
|
-
const fullPath = path.join(this.dir, dirPath);
|
|
68
|
-
try {
|
|
69
|
-
return fs.readdirSync(fullPath).filter(f => !f.startsWith('.'));
|
|
70
|
-
} catch {
|
|
71
|
-
return [];
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Return the contents of the project's CLAUDE.md (root or .claude/ location).
|
|
77
|
-
* If CLAUDE.md contains only a reference to another file (e.g., "AGENTS.md"),
|
|
78
|
-
* follows that reference and returns the referenced file's content appended.
|
|
79
|
-
* @returns {string|null} File content or null if not found.
|
|
80
|
-
*/
|
|
81
|
-
claudeMdContent() {
|
|
82
|
-
const raw = this.fileContent('CLAUDE.md') || this.fileContent('.claude/CLAUDE.md');
|
|
83
|
-
if (!raw) return null;
|
|
84
|
-
|
|
85
|
-
// If the file is very short and looks like a file reference, follow it.
|
|
86
|
-
// Recognised pointer shapes on each line:
|
|
87
|
-
// AGENTS.md
|
|
88
|
-
// docs/CODING.md
|
|
89
|
-
// @AGENTS.md (Claude Code @import syntax)
|
|
90
|
-
// @./docs/CODING.md (Claude Code @import with relative prefix)
|
|
91
|
-
const trimmed = raw.trim();
|
|
92
|
-
const pointerLine = /^@?\.?\/?[a-zA-Z0-9_./-]+\.(md|txt|rst)$/;
|
|
93
|
-
if (trimmed.length < 200 && pointerLine.test(trimmed.split(/\r?\n/)[0].trim())) {
|
|
94
|
-
const lines = trimmed.split(/\r?\n/).map(l => l.trim()).filter(Boolean);
|
|
95
|
-
let combined = raw;
|
|
96
|
-
for (const line of lines) {
|
|
97
|
-
if (pointerLine.test(line)) {
|
|
98
|
-
const ref = line.replace(/^@/, '').replace(/^\.\//, '');
|
|
99
|
-
const referenced = this.fileContent(ref);
|
|
100
|
-
if (referenced) {
|
|
101
|
-
combined += '\n' + referenced;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
return combined;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
return raw;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Read and cache the content of a file relative to the project root.
|
|
113
|
-
* @param {string} filePath - Relative path from the project root.
|
|
114
|
-
* @returns {string|null} File content or null if not readable.
|
|
115
|
-
*/
|
|
116
|
-
fileContent(filePath) {
|
|
117
|
-
if (this._cache[filePath] !== undefined) return this._cache[filePath];
|
|
118
|
-
const fullPath = path.join(this.dir, filePath);
|
|
119
|
-
try {
|
|
120
|
-
const content = fs.readFileSync(fullPath, 'utf8');
|
|
121
|
-
this._cache[filePath] = content;
|
|
122
|
-
return content;
|
|
123
|
-
} catch {
|
|
124
|
-
this._cache[filePath] = null;
|
|
125
|
-
return null;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
fileSizeBytes(filePath) {
|
|
130
|
-
const fullPath = path.join(this.dir, filePath);
|
|
131
|
-
try {
|
|
132
|
-
return fs.statSync(fullPath).size;
|
|
133
|
-
} catch {
|
|
134
|
-
return null;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
lineNumber(filePath, matcher) {
|
|
139
|
-
const content = this.fileContent(filePath);
|
|
140
|
-
if (!content) return null;
|
|
141
|
-
|
|
142
|
-
const lines = content.split(/\r?\n/);
|
|
143
|
-
for (let index = 0; index < lines.length; index++) {
|
|
144
|
-
const line = lines[index];
|
|
145
|
-
if (typeof matcher === 'string' && line.includes(matcher)) {
|
|
146
|
-
return index + 1;
|
|
147
|
-
}
|
|
148
|
-
if (matcher instanceof RegExp && matcher.test(line)) {
|
|
149
|
-
matcher.lastIndex = 0;
|
|
150
|
-
return index + 1;
|
|
151
|
-
}
|
|
152
|
-
if (typeof matcher === 'function' && matcher(line, index + 1)) {
|
|
153
|
-
return index + 1;
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
return null;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
jsonFile(filePath) {
|
|
161
|
-
const content = this.fileContent(filePath);
|
|
162
|
-
if (!content) return null;
|
|
163
|
-
try {
|
|
164
|
-
return JSON.parse(content);
|
|
165
|
-
} catch {
|
|
166
|
-
return null;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
projectDependencies() {
|
|
171
|
-
if (this._dependencyCache) return this._dependencyCache;
|
|
172
|
-
|
|
173
|
-
const deps = {};
|
|
174
|
-
const addDependency = (name, source) => {
|
|
175
|
-
if (!name) return;
|
|
176
|
-
const normalized = `${name}`.trim().toLowerCase().replace(/\[.*\]$/, '');
|
|
177
|
-
if (!normalized || normalized === 'python') return;
|
|
178
|
-
if (!deps[normalized]) {
|
|
179
|
-
deps[normalized] = source || true;
|
|
180
|
-
}
|
|
181
|
-
};
|
|
182
|
-
|
|
183
|
-
const pkg = this.jsonFile('package.json') || {};
|
|
184
|
-
for (const source of ['dependencies', 'devDependencies', 'optionalDependencies', 'peerDependencies']) {
|
|
185
|
-
for (const name of Object.keys(pkg[source] || {})) {
|
|
186
|
-
addDependency(name, 'package.json');
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
const pyproject = this.fileContent('pyproject.toml') || '';
|
|
191
|
-
for (const name of extractPyprojectDependencies(pyproject)) {
|
|
192
|
-
addDependency(name, 'pyproject.toml');
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
const requirementFiles = [
|
|
196
|
-
'requirements.txt',
|
|
197
|
-
'requirements-dev.txt',
|
|
198
|
-
'requirements-dev.in',
|
|
199
|
-
'requirements-prod.txt',
|
|
200
|
-
'requirements/base.txt',
|
|
201
|
-
'requirements/dev.txt',
|
|
202
|
-
'requirements/test.txt',
|
|
203
|
-
];
|
|
204
|
-
for (const filePath of requirementFiles) {
|
|
205
|
-
const content = this.fileContent(filePath);
|
|
206
|
-
if (!content) continue;
|
|
207
|
-
for (const name of extractRequirementsDependencies(content)) {
|
|
208
|
-
addDependency(name, filePath);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
this._dependencyCache = deps;
|
|
213
|
-
return deps;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* Recursively check if a file or directory name exists anywhere under a given base directory.
|
|
218
|
-
* Searches up to maxDepth levels deep.
|
|
219
|
-
*/
|
|
220
|
-
_findInSubdirs(name, baseDir, maxDepth = 3) {
|
|
221
|
-
if (maxDepth <= 0) return false;
|
|
222
|
-
try {
|
|
223
|
-
const entries = fs.readdirSync(baseDir, { withFileTypes: true });
|
|
224
|
-
for (const entry of entries) {
|
|
225
|
-
if (entry.name === 'node_modules' || entry.name === '__pycache__' || entry.name === '.git') continue;
|
|
226
|
-
if (entry.name === name || entry.name.endsWith(name)) return true;
|
|
227
|
-
if (entry.isDirectory()) {
|
|
228
|
-
if (this._findInSubdirs(name, path.join(baseDir, entry.name), maxDepth - 1)) return true;
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
} catch {
|
|
232
|
-
// directory not readable
|
|
233
|
-
}
|
|
234
|
-
return false;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
detectStacks(STACKS) {
|
|
238
|
-
const detected = [];
|
|
239
|
-
for (const [key, stack] of Object.entries(STACKS)) {
|
|
240
|
-
// Check root-level files first (fast path)
|
|
241
|
-
let hasFile = stack.files.some(f => {
|
|
242
|
-
return this.files.some(pf => pf.startsWith(f));
|
|
243
|
-
});
|
|
244
|
-
// If not found at root, search subdirectories (up to 3 levels deep)
|
|
245
|
-
if (!hasFile) {
|
|
246
|
-
hasFile = stack.files.some(f => this._findInSubdirs(f, this.dir));
|
|
247
|
-
}
|
|
248
|
-
if (!hasFile) continue;
|
|
249
|
-
|
|
250
|
-
let contentMatch = true;
|
|
251
|
-
for (const [file, needle] of Object.entries(stack.content)) {
|
|
252
|
-
const content = this.fileContent(file);
|
|
253
|
-
if (!content || !content.includes(needle)) {
|
|
254
|
-
contentMatch = false;
|
|
255
|
-
break;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
if (hasFile && contentMatch) {
|
|
260
|
-
detected.push({ key, label: stack.label });
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
return detected;
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
function extractPyprojectDependencies(content) {
|
|
268
|
-
if (!content) return [];
|
|
269
|
-
|
|
270
|
-
const deps = new Set();
|
|
271
|
-
const add = (value) => {
|
|
272
|
-
if (!value) return;
|
|
273
|
-
deps.add(value.trim().toLowerCase().replace(/\[.*\]$/, ''));
|
|
274
|
-
};
|
|
275
|
-
|
|
276
|
-
const extractSection = (sectionName) => {
|
|
277
|
-
const escaped = sectionName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
278
|
-
const pattern = new RegExp(`\\[${escaped}\\]([\\s\\S]*?)(?:\\n\\s*\\[|$)`);
|
|
279
|
-
const match = content.match(pattern);
|
|
280
|
-
return match ? match[1] : '';
|
|
281
|
-
};
|
|
282
|
-
|
|
283
|
-
const poetryDeps = extractSection('tool.poetry.dependencies');
|
|
284
|
-
for (const match of poetryDeps.matchAll(/^\s*([A-Za-z0-9_.-]+)\s*=/gm)) {
|
|
285
|
-
add(match[1]);
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
const projectDeps = extractSection('project');
|
|
289
|
-
const projectDepsArrayMatch = projectDeps.match(/dependencies\s*=\s*\[([\s\S]*?)\]/m);
|
|
290
|
-
if (projectDepsArrayMatch) {
|
|
291
|
-
for (const item of projectDepsArrayMatch[1].matchAll(/["']([^"']+)["']/g)) {
|
|
292
|
-
const name = item[1].split(/[<>=!~ ]/)[0];
|
|
293
|
-
add(name);
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
const optionalDepsSection = extractSection('project.optional-dependencies');
|
|
298
|
-
for (const item of optionalDepsSection.matchAll(/["']([^"']+)["']/g)) {
|
|
299
|
-
const name = item[1].split(/[<>=!~ ]/)[0];
|
|
300
|
-
add(name);
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
const dependencyGroupsSection = extractSection('dependency-groups');
|
|
304
|
-
for (const item of dependencyGroupsSection.matchAll(/["']([^"']+)["']/g)) {
|
|
305
|
-
const name = item[1].split(/[<>=!~ ]/)[0];
|
|
306
|
-
add(name);
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
return [...deps].filter(Boolean);
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
function extractRequirementsDependencies(content) {
|
|
313
|
-
if (!content) return [];
|
|
314
|
-
|
|
315
|
-
const deps = new Set();
|
|
316
|
-
for (const rawLine of content.split(/\r?\n/)) {
|
|
317
|
-
const line = rawLine.replace(/#.*$/, '').trim();
|
|
318
|
-
if (!line || line.startsWith('-')) continue;
|
|
319
|
-
const match = line.match(/^([A-Za-z0-9_.-]+)/);
|
|
320
|
-
if (!match) continue;
|
|
321
|
-
deps.add(match[1].toLowerCase().replace(/\[.*\]$/, ''));
|
|
322
|
-
}
|
|
323
|
-
return [...deps];
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
module.exports = { ProjectContext };
|
|
1
|
+
/**
|
|
2
|
+
* Project context scanner - reads project files to evaluate against techniques.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
const path = require('path');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Scans and caches project files to provide fast lookups for technique checks.
|
|
10
|
+
* Reads the project directory on construction and exposes helpers for file content, JSON, and stack detection.
|
|
11
|
+
*/
|
|
12
|
+
class ProjectContext {
|
|
13
|
+
constructor(dir) {
|
|
14
|
+
this.dir = dir;
|
|
15
|
+
this.files = [];
|
|
16
|
+
this._cache = {};
|
|
17
|
+
this._dependencyCache = null;
|
|
18
|
+
this._scan();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
_scan() {
|
|
22
|
+
try {
|
|
23
|
+
const entries = fs.readdirSync(this.dir, { withFileTypes: true });
|
|
24
|
+
for (const entry of entries) {
|
|
25
|
+
if (entry.isFile()) {
|
|
26
|
+
if (entry.name === '.DS_Store') continue;
|
|
27
|
+
this.files.push(entry.name);
|
|
28
|
+
} else if (entry.isDirectory()) {
|
|
29
|
+
if (entry.name.startsWith('.') && entry.name !== '.claude') continue;
|
|
30
|
+
if (entry.name === 'node_modules' || entry.name === '__pycache__') continue;
|
|
31
|
+
this.files.push(entry.name + '/');
|
|
32
|
+
// Scan .claude/ subdirectories
|
|
33
|
+
if (entry.name === '.claude') {
|
|
34
|
+
this._scanSubdir('.claude');
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
} catch (err) {
|
|
39
|
+
// Directory might not be readable
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
_scanSubdir(subdir) {
|
|
44
|
+
try {
|
|
45
|
+
const fullPath = path.join(this.dir, subdir);
|
|
46
|
+
const entries = fs.readdirSync(fullPath, { withFileTypes: true });
|
|
47
|
+
for (const entry of entries) {
|
|
48
|
+
if (entry.isDirectory()) {
|
|
49
|
+
this._scanSubdir(path.join(subdir, entry.name));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
} catch (err) {
|
|
53
|
+
// Subdirectory might not exist
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
hasDir(dirPath) {
|
|
58
|
+
const fullPath = path.join(this.dir, dirPath);
|
|
59
|
+
try {
|
|
60
|
+
return fs.statSync(fullPath).isDirectory();
|
|
61
|
+
} catch {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
dirFiles(dirPath) {
|
|
67
|
+
const fullPath = path.join(this.dir, dirPath);
|
|
68
|
+
try {
|
|
69
|
+
return fs.readdirSync(fullPath).filter(f => !f.startsWith('.'));
|
|
70
|
+
} catch {
|
|
71
|
+
return [];
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Return the contents of the project's CLAUDE.md (root or .claude/ location).
|
|
77
|
+
* If CLAUDE.md contains only a reference to another file (e.g., "AGENTS.md"),
|
|
78
|
+
* follows that reference and returns the referenced file's content appended.
|
|
79
|
+
* @returns {string|null} File content or null if not found.
|
|
80
|
+
*/
|
|
81
|
+
claudeMdContent() {
|
|
82
|
+
const raw = this.fileContent('CLAUDE.md') || this.fileContent('.claude/CLAUDE.md');
|
|
83
|
+
if (!raw) return null;
|
|
84
|
+
|
|
85
|
+
// If the file is very short and looks like a file reference, follow it.
|
|
86
|
+
// Recognised pointer shapes on each line:
|
|
87
|
+
// AGENTS.md
|
|
88
|
+
// docs/CODING.md
|
|
89
|
+
// @AGENTS.md (Claude Code @import syntax)
|
|
90
|
+
// @./docs/CODING.md (Claude Code @import with relative prefix)
|
|
91
|
+
const trimmed = raw.trim();
|
|
92
|
+
const pointerLine = /^@?\.?\/?[a-zA-Z0-9_./-]+\.(md|txt|rst)$/;
|
|
93
|
+
if (trimmed.length < 200 && pointerLine.test(trimmed.split(/\r?\n/)[0].trim())) {
|
|
94
|
+
const lines = trimmed.split(/\r?\n/).map(l => l.trim()).filter(Boolean);
|
|
95
|
+
let combined = raw;
|
|
96
|
+
for (const line of lines) {
|
|
97
|
+
if (pointerLine.test(line)) {
|
|
98
|
+
const ref = line.replace(/^@/, '').replace(/^\.\//, '');
|
|
99
|
+
const referenced = this.fileContent(ref);
|
|
100
|
+
if (referenced) {
|
|
101
|
+
combined += '\n' + referenced;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return combined;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return raw;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Read and cache the content of a file relative to the project root.
|
|
113
|
+
* @param {string} filePath - Relative path from the project root.
|
|
114
|
+
* @returns {string|null} File content or null if not readable.
|
|
115
|
+
*/
|
|
116
|
+
fileContent(filePath) {
|
|
117
|
+
if (this._cache[filePath] !== undefined) return this._cache[filePath];
|
|
118
|
+
const fullPath = path.join(this.dir, filePath);
|
|
119
|
+
try {
|
|
120
|
+
const content = fs.readFileSync(fullPath, 'utf8');
|
|
121
|
+
this._cache[filePath] = content;
|
|
122
|
+
return content;
|
|
123
|
+
} catch {
|
|
124
|
+
this._cache[filePath] = null;
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
fileSizeBytes(filePath) {
|
|
130
|
+
const fullPath = path.join(this.dir, filePath);
|
|
131
|
+
try {
|
|
132
|
+
return fs.statSync(fullPath).size;
|
|
133
|
+
} catch {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
lineNumber(filePath, matcher) {
|
|
139
|
+
const content = this.fileContent(filePath);
|
|
140
|
+
if (!content) return null;
|
|
141
|
+
|
|
142
|
+
const lines = content.split(/\r?\n/);
|
|
143
|
+
for (let index = 0; index < lines.length; index++) {
|
|
144
|
+
const line = lines[index];
|
|
145
|
+
if (typeof matcher === 'string' && line.includes(matcher)) {
|
|
146
|
+
return index + 1;
|
|
147
|
+
}
|
|
148
|
+
if (matcher instanceof RegExp && matcher.test(line)) {
|
|
149
|
+
matcher.lastIndex = 0;
|
|
150
|
+
return index + 1;
|
|
151
|
+
}
|
|
152
|
+
if (typeof matcher === 'function' && matcher(line, index + 1)) {
|
|
153
|
+
return index + 1;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
jsonFile(filePath) {
|
|
161
|
+
const content = this.fileContent(filePath);
|
|
162
|
+
if (!content) return null;
|
|
163
|
+
try {
|
|
164
|
+
return JSON.parse(content);
|
|
165
|
+
} catch {
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
projectDependencies() {
|
|
171
|
+
if (this._dependencyCache) return this._dependencyCache;
|
|
172
|
+
|
|
173
|
+
const deps = {};
|
|
174
|
+
const addDependency = (name, source) => {
|
|
175
|
+
if (!name) return;
|
|
176
|
+
const normalized = `${name}`.trim().toLowerCase().replace(/\[.*\]$/, '');
|
|
177
|
+
if (!normalized || normalized === 'python') return;
|
|
178
|
+
if (!deps[normalized]) {
|
|
179
|
+
deps[normalized] = source || true;
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
const pkg = this.jsonFile('package.json') || {};
|
|
184
|
+
for (const source of ['dependencies', 'devDependencies', 'optionalDependencies', 'peerDependencies']) {
|
|
185
|
+
for (const name of Object.keys(pkg[source] || {})) {
|
|
186
|
+
addDependency(name, 'package.json');
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const pyproject = this.fileContent('pyproject.toml') || '';
|
|
191
|
+
for (const name of extractPyprojectDependencies(pyproject)) {
|
|
192
|
+
addDependency(name, 'pyproject.toml');
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const requirementFiles = [
|
|
196
|
+
'requirements.txt',
|
|
197
|
+
'requirements-dev.txt',
|
|
198
|
+
'requirements-dev.in',
|
|
199
|
+
'requirements-prod.txt',
|
|
200
|
+
'requirements/base.txt',
|
|
201
|
+
'requirements/dev.txt',
|
|
202
|
+
'requirements/test.txt',
|
|
203
|
+
];
|
|
204
|
+
for (const filePath of requirementFiles) {
|
|
205
|
+
const content = this.fileContent(filePath);
|
|
206
|
+
if (!content) continue;
|
|
207
|
+
for (const name of extractRequirementsDependencies(content)) {
|
|
208
|
+
addDependency(name, filePath);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
this._dependencyCache = deps;
|
|
213
|
+
return deps;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Recursively check if a file or directory name exists anywhere under a given base directory.
|
|
218
|
+
* Searches up to maxDepth levels deep.
|
|
219
|
+
*/
|
|
220
|
+
_findInSubdirs(name, baseDir, maxDepth = 3) {
|
|
221
|
+
if (maxDepth <= 0) return false;
|
|
222
|
+
try {
|
|
223
|
+
const entries = fs.readdirSync(baseDir, { withFileTypes: true });
|
|
224
|
+
for (const entry of entries) {
|
|
225
|
+
if (entry.name === 'node_modules' || entry.name === '__pycache__' || entry.name === '.git') continue;
|
|
226
|
+
if (entry.name === name || entry.name.endsWith(name)) return true;
|
|
227
|
+
if (entry.isDirectory()) {
|
|
228
|
+
if (this._findInSubdirs(name, path.join(baseDir, entry.name), maxDepth - 1)) return true;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
} catch {
|
|
232
|
+
// directory not readable
|
|
233
|
+
}
|
|
234
|
+
return false;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
detectStacks(STACKS) {
|
|
238
|
+
const detected = [];
|
|
239
|
+
for (const [key, stack] of Object.entries(STACKS)) {
|
|
240
|
+
// Check root-level files first (fast path)
|
|
241
|
+
let hasFile = stack.files.some(f => {
|
|
242
|
+
return this.files.some(pf => pf.startsWith(f));
|
|
243
|
+
});
|
|
244
|
+
// If not found at root, search subdirectories (up to 3 levels deep)
|
|
245
|
+
if (!hasFile) {
|
|
246
|
+
hasFile = stack.files.some(f => this._findInSubdirs(f, this.dir));
|
|
247
|
+
}
|
|
248
|
+
if (!hasFile) continue;
|
|
249
|
+
|
|
250
|
+
let contentMatch = true;
|
|
251
|
+
for (const [file, needle] of Object.entries(stack.content)) {
|
|
252
|
+
const content = this.fileContent(file);
|
|
253
|
+
if (!content || !content.includes(needle)) {
|
|
254
|
+
contentMatch = false;
|
|
255
|
+
break;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (hasFile && contentMatch) {
|
|
260
|
+
detected.push({ key, label: stack.label });
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
return detected;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function extractPyprojectDependencies(content) {
|
|
268
|
+
if (!content) return [];
|
|
269
|
+
|
|
270
|
+
const deps = new Set();
|
|
271
|
+
const add = (value) => {
|
|
272
|
+
if (!value) return;
|
|
273
|
+
deps.add(value.trim().toLowerCase().replace(/\[.*\]$/, ''));
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
const extractSection = (sectionName) => {
|
|
277
|
+
const escaped = sectionName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
278
|
+
const pattern = new RegExp(`\\[${escaped}\\]([\\s\\S]*?)(?:\\n\\s*\\[|$)`);
|
|
279
|
+
const match = content.match(pattern);
|
|
280
|
+
return match ? match[1] : '';
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
const poetryDeps = extractSection('tool.poetry.dependencies');
|
|
284
|
+
for (const match of poetryDeps.matchAll(/^\s*([A-Za-z0-9_.-]+)\s*=/gm)) {
|
|
285
|
+
add(match[1]);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const projectDeps = extractSection('project');
|
|
289
|
+
const projectDepsArrayMatch = projectDeps.match(/dependencies\s*=\s*\[([\s\S]*?)\]/m);
|
|
290
|
+
if (projectDepsArrayMatch) {
|
|
291
|
+
for (const item of projectDepsArrayMatch[1].matchAll(/["']([^"']+)["']/g)) {
|
|
292
|
+
const name = item[1].split(/[<>=!~ ]/)[0];
|
|
293
|
+
add(name);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
const optionalDepsSection = extractSection('project.optional-dependencies');
|
|
298
|
+
for (const item of optionalDepsSection.matchAll(/["']([^"']+)["']/g)) {
|
|
299
|
+
const name = item[1].split(/[<>=!~ ]/)[0];
|
|
300
|
+
add(name);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
const dependencyGroupsSection = extractSection('dependency-groups');
|
|
304
|
+
for (const item of dependencyGroupsSection.matchAll(/["']([^"']+)["']/g)) {
|
|
305
|
+
const name = item[1].split(/[<>=!~ ]/)[0];
|
|
306
|
+
add(name);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
return [...deps].filter(Boolean);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function extractRequirementsDependencies(content) {
|
|
313
|
+
if (!content) return [];
|
|
314
|
+
|
|
315
|
+
const deps = new Set();
|
|
316
|
+
for (const rawLine of content.split(/\r?\n/)) {
|
|
317
|
+
const line = rawLine.replace(/#.*$/, '').trim();
|
|
318
|
+
if (!line || line.startsWith('-')) continue;
|
|
319
|
+
const match = line.match(/^([A-Za-z0-9_.-]+)/);
|
|
320
|
+
if (!match) continue;
|
|
321
|
+
deps.add(match[1].toLowerCase().replace(/\[.*\]$/, ''));
|
|
322
|
+
}
|
|
323
|
+
return [...deps];
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
module.exports = { ProjectContext };
|