claude-crap 0.1.2 → 0.3.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/CHANGELOG.md +68 -0
- package/README.md +44 -23
- package/dist/index.js +142 -1
- package/dist/index.js.map +1 -1
- package/dist/scanner/auto-scan.d.ts +57 -0
- package/dist/scanner/auto-scan.d.ts.map +1 -0
- package/dist/scanner/auto-scan.js +138 -0
- package/dist/scanner/auto-scan.js.map +1 -0
- package/dist/scanner/bootstrap.d.ts +89 -0
- package/dist/scanner/bootstrap.d.ts.map +1 -0
- package/dist/scanner/bootstrap.js +278 -0
- package/dist/scanner/bootstrap.js.map +1 -0
- package/dist/scanner/detector.d.ts +53 -0
- package/dist/scanner/detector.d.ts.map +1 -0
- package/dist/scanner/detector.js +173 -0
- package/dist/scanner/detector.js.map +1 -0
- package/dist/scanner/index.d.ts +23 -0
- package/dist/scanner/index.d.ts.map +1 -0
- package/dist/scanner/index.js +23 -0
- package/dist/scanner/index.js.map +1 -0
- package/dist/scanner/runner.d.ts +59 -0
- package/dist/scanner/runner.d.ts.map +1 -0
- package/dist/scanner/runner.js +159 -0
- package/dist/scanner/runner.js.map +1 -0
- package/dist/schemas/tool-schemas.d.ts +23 -0
- package/dist/schemas/tool-schemas.d.ts.map +1 -1
- package/dist/schemas/tool-schemas.js +23 -0
- package/dist/schemas/tool-schemas.js.map +1 -1
- package/package.json +5 -1
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/plugin/bundle/mcp-server.mjs +732 -0
- package/plugin/bundle/mcp-server.mjs.map +4 -4
- package/plugin/package.json +1 -1
- package/src/index.ts +176 -0
- package/src/scanner/auto-scan.ts +212 -0
- package/src/scanner/bootstrap.ts +383 -0
- package/src/scanner/detector.ts +224 -0
- package/src/scanner/index.ts +30 -0
- package/src/scanner/runner.ts +212 -0
- package/src/schemas/tool-schemas.ts +27 -0
- package/src/tests/auto-scan.test.ts +137 -0
- package/src/tests/integration/mcp-server.integration.test.ts +3 -1
- package/src/tests/scanner-bootstrap.test.ts +186 -0
- package/src/tests/scanner-detector.test.ts +181 -0
- package/src/tests/scanner-runner.test.ts +63 -0
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bootstrap a scanner for projects that don't have one configured.
|
|
3
|
+
*
|
|
4
|
+
* Detects the project type from workspace signals (package.json,
|
|
5
|
+
* tsconfig.json, pyproject.toml, pom.xml, *.csproj, etc.), installs
|
|
6
|
+
* the appropriate scanner, creates a minimal config file, and runs
|
|
7
|
+
* `autoScan()` to verify and ingest findings immediately.
|
|
8
|
+
*
|
|
9
|
+
* Coverage maps to the five languages the tree-sitter engine supports:
|
|
10
|
+
*
|
|
11
|
+
* - JavaScript / TypeScript → ESLint (npm install + flat config)
|
|
12
|
+
* - Python → Bandit (install instructions only — virtualenv boundary)
|
|
13
|
+
* - Java → Semgrep (install instructions)
|
|
14
|
+
* - C# → Semgrep (install instructions)
|
|
15
|
+
* - Unknown → Semgrep (polyglot fallback)
|
|
16
|
+
*
|
|
17
|
+
* For JS/TS projects the tool runs `npm install --save-dev` and writes
|
|
18
|
+
* an `eslint.config.mjs`. For all other languages it returns manual
|
|
19
|
+
* install instructions rather than executing package managers whose
|
|
20
|
+
* environment assumptions may not hold.
|
|
21
|
+
*
|
|
22
|
+
* @module scanner/bootstrap
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { existsSync, writeFileSync, readdirSync } from "node:fs";
|
|
26
|
+
import { join } from "node:path";
|
|
27
|
+
import { execFile } from "node:child_process";
|
|
28
|
+
import type { Logger } from "pino";
|
|
29
|
+
import type { KnownScanner } from "../adapters/common.js";
|
|
30
|
+
import { detectScanners } from "./detector.js";
|
|
31
|
+
import { autoScan, type AutoScanResult } from "./auto-scan.js";
|
|
32
|
+
import type { SarifStore } from "../sarif/sarif-store.js";
|
|
33
|
+
|
|
34
|
+
// ── Types ──────────────────────────────────────────────────────────
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Detected project type, aligned with tree-sitter supported languages.
|
|
38
|
+
*/
|
|
39
|
+
export type ProjectType =
|
|
40
|
+
| "javascript"
|
|
41
|
+
| "typescript"
|
|
42
|
+
| "python"
|
|
43
|
+
| "java"
|
|
44
|
+
| "csharp"
|
|
45
|
+
| "unknown";
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* A single step in the bootstrap process.
|
|
49
|
+
*/
|
|
50
|
+
export interface BootstrapStep {
|
|
51
|
+
/** What was attempted (e.g. "install eslint", "create eslint.config.mjs"). */
|
|
52
|
+
action: string;
|
|
53
|
+
/** Whether the step completed successfully. */
|
|
54
|
+
success: boolean;
|
|
55
|
+
/** Human-readable detail (command output, error message, or instruction). */
|
|
56
|
+
detail: string;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Complete result of a bootstrap_scanner invocation.
|
|
61
|
+
*/
|
|
62
|
+
export interface BootstrapResult {
|
|
63
|
+
/** Detected project type based on workspace signals. */
|
|
64
|
+
projectType: ProjectType;
|
|
65
|
+
/** Whether a scanner was already configured (detected by detector.ts). */
|
|
66
|
+
alreadyConfigured: boolean;
|
|
67
|
+
/** Which scanners were already available, if any. */
|
|
68
|
+
existingScanners: string[];
|
|
69
|
+
/** Steps executed (or instructions returned) during bootstrap. */
|
|
70
|
+
steps: BootstrapStep[];
|
|
71
|
+
/** The auto-scan result after installation (null if skipped). */
|
|
72
|
+
autoScanResult: AutoScanResult | null;
|
|
73
|
+
/** Whether the overall bootstrap succeeded. */
|
|
74
|
+
success: boolean;
|
|
75
|
+
/** Summary message suitable for display. */
|
|
76
|
+
summary: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// ── Project type detection ─────────────────────────────────────────
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Detect the project type from workspace signals.
|
|
83
|
+
*
|
|
84
|
+
* Checks in priority order: TypeScript, JavaScript, Python, Java,
|
|
85
|
+
* C#, then unknown. TypeScript wins over plain JavaScript because
|
|
86
|
+
* `tsconfig.json` implies a superset.
|
|
87
|
+
*/
|
|
88
|
+
export function detectProjectType(workspaceRoot: string): ProjectType {
|
|
89
|
+
const has = (file: string) => existsSync(join(workspaceRoot, file));
|
|
90
|
+
|
|
91
|
+
// JS/TS detection — package.json is the anchor
|
|
92
|
+
if (has("package.json")) {
|
|
93
|
+
if (has("tsconfig.json")) return "typescript";
|
|
94
|
+
return "javascript";
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Python detection
|
|
98
|
+
if (has("pyproject.toml") || has("setup.py") || has("requirements.txt")) {
|
|
99
|
+
return "python";
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Java detection
|
|
103
|
+
if (has("pom.xml") || has("build.gradle") || has("build.gradle.kts")) {
|
|
104
|
+
return "java";
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// C# detection
|
|
108
|
+
if (has("Directory.Build.props")) return "csharp";
|
|
109
|
+
try {
|
|
110
|
+
const entries = readdirSync(workspaceRoot);
|
|
111
|
+
if (entries.some((e) => e.endsWith(".csproj") || e.endsWith(".sln"))) {
|
|
112
|
+
return "csharp";
|
|
113
|
+
}
|
|
114
|
+
} catch {
|
|
115
|
+
// readdirSync can fail on permissions — fall through
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return "unknown";
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// ── ESLint config generation ───────────────────────────────────────
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Generate a minimal ESLint flat config (ESLint 9+).
|
|
125
|
+
*
|
|
126
|
+
* @param isTypeScript Include typescript-eslint when true.
|
|
127
|
+
* @returns The config file content as a string.
|
|
128
|
+
*/
|
|
129
|
+
export function generateEslintConfig(isTypeScript: boolean): string {
|
|
130
|
+
if (isTypeScript) {
|
|
131
|
+
return `import js from "@eslint/js";
|
|
132
|
+
import tseslint from "typescript-eslint";
|
|
133
|
+
|
|
134
|
+
export default tseslint.config(
|
|
135
|
+
js.configs.recommended,
|
|
136
|
+
...tseslint.configs.recommended,
|
|
137
|
+
{
|
|
138
|
+
ignores: ["dist/", "node_modules/", "coverage/"],
|
|
139
|
+
},
|
|
140
|
+
);
|
|
141
|
+
`;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return `import js from "@eslint/js";
|
|
145
|
+
|
|
146
|
+
export default [
|
|
147
|
+
js.configs.recommended,
|
|
148
|
+
{
|
|
149
|
+
ignores: ["dist/", "node_modules/", "coverage/"],
|
|
150
|
+
},
|
|
151
|
+
];
|
|
152
|
+
`;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// ── Installation helpers ───────────────────────────────────────────
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Run `npm install --save-dev` for the given packages.
|
|
159
|
+
*/
|
|
160
|
+
function npmInstall(
|
|
161
|
+
workspaceRoot: string,
|
|
162
|
+
packages: string[],
|
|
163
|
+
): Promise<BootstrapStep> {
|
|
164
|
+
return new Promise((resolve) => {
|
|
165
|
+
execFile(
|
|
166
|
+
"npm",
|
|
167
|
+
["install", "--save-dev", ...packages],
|
|
168
|
+
{
|
|
169
|
+
cwd: workspaceRoot,
|
|
170
|
+
timeout: 120_000,
|
|
171
|
+
env: { ...process.env, FORCE_COLOR: "0" },
|
|
172
|
+
},
|
|
173
|
+
(err, stdout, stderr) => {
|
|
174
|
+
if (err) {
|
|
175
|
+
resolve({
|
|
176
|
+
action: `npm install --save-dev ${packages.join(" ")}`,
|
|
177
|
+
success: false,
|
|
178
|
+
detail: stderr || (err as Error).message,
|
|
179
|
+
});
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
resolve({
|
|
183
|
+
action: `npm install --save-dev ${packages.join(" ")}`,
|
|
184
|
+
success: true,
|
|
185
|
+
detail: `installed ${packages.join(", ")}`,
|
|
186
|
+
});
|
|
187
|
+
},
|
|
188
|
+
);
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Write the ESLint config file to the workspace root.
|
|
194
|
+
* Returns failure if the file already exists.
|
|
195
|
+
*/
|
|
196
|
+
function writeEslintConfigFile(
|
|
197
|
+
workspaceRoot: string,
|
|
198
|
+
isTypeScript: boolean,
|
|
199
|
+
): BootstrapStep {
|
|
200
|
+
const configPath = join(workspaceRoot, "eslint.config.mjs");
|
|
201
|
+
if (existsSync(configPath)) {
|
|
202
|
+
return {
|
|
203
|
+
action: "create eslint.config.mjs",
|
|
204
|
+
success: true,
|
|
205
|
+
detail: "eslint.config.mjs already exists — skipped",
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
try {
|
|
210
|
+
writeFileSync(configPath, generateEslintConfig(isTypeScript), "utf-8");
|
|
211
|
+
return {
|
|
212
|
+
action: "create eslint.config.mjs",
|
|
213
|
+
success: true,
|
|
214
|
+
detail: `created eslint.config.mjs (${isTypeScript ? "TypeScript" : "JavaScript"} template)`,
|
|
215
|
+
};
|
|
216
|
+
} catch (err) {
|
|
217
|
+
return {
|
|
218
|
+
action: "create eslint.config.mjs",
|
|
219
|
+
success: false,
|
|
220
|
+
detail: (err as Error).message,
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// ── Scanner-to-language mapping ────────────────────────────────────
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Map project type → recommended scanner and install instructions.
|
|
229
|
+
*/
|
|
230
|
+
interface ScannerRecommendation {
|
|
231
|
+
scanner: KnownScanner;
|
|
232
|
+
canAutoInstall: boolean;
|
|
233
|
+
installInstructions: string;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function getRecommendation(projectType: ProjectType): ScannerRecommendation {
|
|
237
|
+
switch (projectType) {
|
|
238
|
+
case "javascript":
|
|
239
|
+
case "typescript":
|
|
240
|
+
return {
|
|
241
|
+
scanner: "eslint",
|
|
242
|
+
canAutoInstall: true,
|
|
243
|
+
installInstructions: "npm install --save-dev eslint @eslint/js",
|
|
244
|
+
};
|
|
245
|
+
case "python":
|
|
246
|
+
return {
|
|
247
|
+
scanner: "bandit",
|
|
248
|
+
canAutoInstall: false,
|
|
249
|
+
installInstructions:
|
|
250
|
+
"pip install bandit (or: pipx install bandit, poetry add --group dev bandit)",
|
|
251
|
+
};
|
|
252
|
+
case "java":
|
|
253
|
+
case "csharp":
|
|
254
|
+
return {
|
|
255
|
+
scanner: "semgrep",
|
|
256
|
+
canAutoInstall: false,
|
|
257
|
+
installInstructions:
|
|
258
|
+
"brew install semgrep (or: pip install semgrep, pipx install semgrep)",
|
|
259
|
+
};
|
|
260
|
+
case "unknown":
|
|
261
|
+
return {
|
|
262
|
+
scanner: "semgrep",
|
|
263
|
+
canAutoInstall: false,
|
|
264
|
+
installInstructions:
|
|
265
|
+
"brew install semgrep (or: pip install semgrep, pipx install semgrep)",
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// ── Main orchestrator ──────────────────────────────────────────────
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Bootstrap a scanner for the current workspace.
|
|
274
|
+
*
|
|
275
|
+
* 1. Check if a scanner is already configured (short-circuit if so)
|
|
276
|
+
* 2. Detect the project type
|
|
277
|
+
* 3. Install the recommended scanner (or return instructions)
|
|
278
|
+
* 4. Run auto_scan to verify and ingest findings
|
|
279
|
+
*
|
|
280
|
+
* @param workspaceRoot Absolute path to the project root.
|
|
281
|
+
* @param sarifStore Live SARIF store for auto-scan ingestion.
|
|
282
|
+
* @param logger Pino logger for progress reporting.
|
|
283
|
+
*/
|
|
284
|
+
export async function bootstrapScanner(
|
|
285
|
+
workspaceRoot: string,
|
|
286
|
+
sarifStore: SarifStore,
|
|
287
|
+
logger: Logger,
|
|
288
|
+
): Promise<BootstrapResult> {
|
|
289
|
+
// 1. Check existing scanners
|
|
290
|
+
const detections = await detectScanners(workspaceRoot);
|
|
291
|
+
const available = detections.filter((d) => d.available);
|
|
292
|
+
|
|
293
|
+
if (available.length > 0) {
|
|
294
|
+
const existingScanners = available.map((d) => d.scanner);
|
|
295
|
+
logger.info(
|
|
296
|
+
{ existingScanners },
|
|
297
|
+
"bootstrap: scanner(s) already configured — skipping",
|
|
298
|
+
);
|
|
299
|
+
return {
|
|
300
|
+
projectType: detectProjectType(workspaceRoot),
|
|
301
|
+
alreadyConfigured: true,
|
|
302
|
+
existingScanners,
|
|
303
|
+
steps: [],
|
|
304
|
+
autoScanResult: null,
|
|
305
|
+
success: true,
|
|
306
|
+
summary: `Scanner(s) already configured: ${existingScanners.join(", ")}. Run auto_scan to ingest findings.`,
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// 2. Detect project type
|
|
311
|
+
const projectType = detectProjectType(workspaceRoot);
|
|
312
|
+
const recommendation = getRecommendation(projectType);
|
|
313
|
+
const steps: BootstrapStep[] = [];
|
|
314
|
+
|
|
315
|
+
logger.info(
|
|
316
|
+
{ projectType, scanner: recommendation.scanner },
|
|
317
|
+
"bootstrap: detected project type",
|
|
318
|
+
);
|
|
319
|
+
|
|
320
|
+
// 3. Install scanner
|
|
321
|
+
if (recommendation.canAutoInstall) {
|
|
322
|
+
// JS/TS: auto-install ESLint
|
|
323
|
+
const isTypeScript = projectType === "typescript";
|
|
324
|
+
const packages = isTypeScript
|
|
325
|
+
? ["eslint", "@eslint/js", "typescript-eslint"]
|
|
326
|
+
: ["eslint", "@eslint/js"];
|
|
327
|
+
|
|
328
|
+
const installStep = await npmInstall(workspaceRoot, packages);
|
|
329
|
+
steps.push(installStep);
|
|
330
|
+
|
|
331
|
+
if (installStep.success) {
|
|
332
|
+
const configStep = writeEslintConfigFile(workspaceRoot, isTypeScript);
|
|
333
|
+
steps.push(configStep);
|
|
334
|
+
}
|
|
335
|
+
} else {
|
|
336
|
+
// Python / Java / C# / Unknown: return instructions
|
|
337
|
+
steps.push({
|
|
338
|
+
action: `suggest ${recommendation.scanner} install`,
|
|
339
|
+
success: true,
|
|
340
|
+
detail: recommendation.installInstructions,
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// 4. Run auto_scan if installation succeeded
|
|
345
|
+
const installSucceeded = steps.every((s) => s.success);
|
|
346
|
+
let autoScanResult: AutoScanResult | null = null;
|
|
347
|
+
|
|
348
|
+
if (installSucceeded && recommendation.canAutoInstall) {
|
|
349
|
+
try {
|
|
350
|
+
autoScanResult = await autoScan(workspaceRoot, sarifStore, logger);
|
|
351
|
+
} catch (err) {
|
|
352
|
+
logger.warn(
|
|
353
|
+
{ err: (err as Error).message },
|
|
354
|
+
"bootstrap: auto_scan after install failed",
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// 5. Build result
|
|
360
|
+
const findings = autoScanResult?.totalFindings ?? 0;
|
|
361
|
+
const scannerInstalled = recommendation.canAutoInstall && installSucceeded;
|
|
362
|
+
|
|
363
|
+
let summary: string;
|
|
364
|
+
if (scannerInstalled && autoScanResult) {
|
|
365
|
+
summary = `Installed ${recommendation.scanner} for ${projectType} project. Auto-scan found ${findings} finding(s).`;
|
|
366
|
+
} else if (scannerInstalled) {
|
|
367
|
+
summary = `Installed ${recommendation.scanner} for ${projectType} project. Auto-scan did not run.`;
|
|
368
|
+
} else if (!recommendation.canAutoInstall) {
|
|
369
|
+
summary = `Detected ${projectType} project. Install ${recommendation.scanner} manually: ${recommendation.installInstructions}`;
|
|
370
|
+
} else {
|
|
371
|
+
summary = `Failed to install ${recommendation.scanner}. Check the error details in the steps.`;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
return {
|
|
375
|
+
projectType,
|
|
376
|
+
alreadyConfigured: false,
|
|
377
|
+
existingScanners: [],
|
|
378
|
+
steps,
|
|
379
|
+
autoScanResult,
|
|
380
|
+
success: installSucceeded,
|
|
381
|
+
summary,
|
|
382
|
+
};
|
|
383
|
+
}
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auto-detect which scanners are available in the current workspace.
|
|
3
|
+
*
|
|
4
|
+
* For each of the four supported scanners (ESLint, Semgrep, Bandit,
|
|
5
|
+
* Stryker) the detector probes three signal layers in order:
|
|
6
|
+
*
|
|
7
|
+
* 1. Config file existence (fastest — a single `fs.stat`)
|
|
8
|
+
* 2. Package.json dependency (for JS-ecosystem scanners)
|
|
9
|
+
* 3. Binary availability via `which` (slowest — spawns a child process)
|
|
10
|
+
*
|
|
11
|
+
* Detection short-circuits on the first hit, so a project that has an
|
|
12
|
+
* `eslint.config.mjs` will never shell out to `which eslint`.
|
|
13
|
+
*
|
|
14
|
+
* The module is side-effect-free beyond filesystem reads and one
|
|
15
|
+
* `child_process.execFile` per binary probe.
|
|
16
|
+
*
|
|
17
|
+
* @module scanner/detector
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
21
|
+
import { join } from "node:path";
|
|
22
|
+
import { execFile } from "node:child_process";
|
|
23
|
+
import type { KnownScanner } from "../adapters/common.js";
|
|
24
|
+
|
|
25
|
+
// ── Types ──────────────────────────────────────────────────────────
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Result of probing a single scanner's availability.
|
|
29
|
+
*/
|
|
30
|
+
export interface ScannerDetection {
|
|
31
|
+
/** Which scanner was probed. */
|
|
32
|
+
scanner: KnownScanner;
|
|
33
|
+
/** Whether the scanner is available and can be executed. */
|
|
34
|
+
available: boolean;
|
|
35
|
+
/** Human-readable reason for the verdict. */
|
|
36
|
+
reason: string;
|
|
37
|
+
/** Path to the config file that triggered detection, if any. */
|
|
38
|
+
configPath?: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// ── Detection signals ──────────────────────────────────────────────
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Config file globs and package.json keys per scanner. Order matters:
|
|
45
|
+
* the first matching config file short-circuits further probes.
|
|
46
|
+
*/
|
|
47
|
+
interface ScannerSignals {
|
|
48
|
+
configFiles: string[];
|
|
49
|
+
packageJsonKeys: string[];
|
|
50
|
+
binaryNames: string[];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const SCANNER_SIGNALS: Record<KnownScanner, ScannerSignals> = {
|
|
54
|
+
eslint: {
|
|
55
|
+
configFiles: [
|
|
56
|
+
"eslint.config.js",
|
|
57
|
+
"eslint.config.mjs",
|
|
58
|
+
"eslint.config.cjs",
|
|
59
|
+
"eslint.config.ts",
|
|
60
|
+
"eslint.config.mts",
|
|
61
|
+
"eslint.config.cts",
|
|
62
|
+
".eslintrc.js",
|
|
63
|
+
".eslintrc.cjs",
|
|
64
|
+
".eslintrc.yaml",
|
|
65
|
+
".eslintrc.yml",
|
|
66
|
+
".eslintrc.json",
|
|
67
|
+
],
|
|
68
|
+
packageJsonKeys: ["eslint"],
|
|
69
|
+
binaryNames: ["eslint"],
|
|
70
|
+
},
|
|
71
|
+
semgrep: {
|
|
72
|
+
configFiles: [
|
|
73
|
+
".semgrep.yml",
|
|
74
|
+
".semgrep.yaml",
|
|
75
|
+
".semgrep.json",
|
|
76
|
+
],
|
|
77
|
+
packageJsonKeys: [],
|
|
78
|
+
binaryNames: ["semgrep"],
|
|
79
|
+
},
|
|
80
|
+
bandit: {
|
|
81
|
+
configFiles: [
|
|
82
|
+
".bandit",
|
|
83
|
+
"bandit.yaml",
|
|
84
|
+
"bandit.yml",
|
|
85
|
+
],
|
|
86
|
+
packageJsonKeys: [],
|
|
87
|
+
binaryNames: ["bandit"],
|
|
88
|
+
},
|
|
89
|
+
stryker: {
|
|
90
|
+
configFiles: [
|
|
91
|
+
"stryker.conf.js",
|
|
92
|
+
"stryker.conf.mjs",
|
|
93
|
+
"stryker.conf.cjs",
|
|
94
|
+
"stryker.conf.json",
|
|
95
|
+
".strykerrc",
|
|
96
|
+
".strykerrc.json",
|
|
97
|
+
],
|
|
98
|
+
packageJsonKeys: ["@stryker-mutator/core"],
|
|
99
|
+
binaryNames: ["stryker"],
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
// ── Probes ──────────────────────────────────────────────────────────
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Check if any of the scanner's config files exist in the workspace.
|
|
107
|
+
*/
|
|
108
|
+
function probeConfigFiles(
|
|
109
|
+
workspaceRoot: string,
|
|
110
|
+
scanner: KnownScanner,
|
|
111
|
+
): { found: boolean; path?: string } {
|
|
112
|
+
const signals = SCANNER_SIGNALS[scanner];
|
|
113
|
+
for (const file of signals.configFiles) {
|
|
114
|
+
const fullPath = join(workspaceRoot, file);
|
|
115
|
+
if (existsSync(fullPath)) {
|
|
116
|
+
return { found: true, path: fullPath };
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return { found: false };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Check if the scanner appears in package.json deps or devDeps.
|
|
124
|
+
*/
|
|
125
|
+
function probePackageJson(
|
|
126
|
+
workspaceRoot: string,
|
|
127
|
+
scanner: KnownScanner,
|
|
128
|
+
): boolean {
|
|
129
|
+
const signals = SCANNER_SIGNALS[scanner];
|
|
130
|
+
if (signals.packageJsonKeys.length === 0) return false;
|
|
131
|
+
|
|
132
|
+
const pkgPath = join(workspaceRoot, "package.json");
|
|
133
|
+
if (!existsSync(pkgPath)) return false;
|
|
134
|
+
|
|
135
|
+
try {
|
|
136
|
+
const raw = readFileSync(pkgPath, "utf-8");
|
|
137
|
+
const pkg = JSON.parse(raw) as Record<string, unknown>;
|
|
138
|
+
const deps = {
|
|
139
|
+
...(typeof pkg.dependencies === "object" && pkg.dependencies !== null
|
|
140
|
+
? (pkg.dependencies as Record<string, string>)
|
|
141
|
+
: {}),
|
|
142
|
+
...(typeof pkg.devDependencies === "object" && pkg.devDependencies !== null
|
|
143
|
+
? (pkg.devDependencies as Record<string, string>)
|
|
144
|
+
: {}),
|
|
145
|
+
};
|
|
146
|
+
return signals.packageJsonKeys.some((key) => key in deps);
|
|
147
|
+
} catch {
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Check if a binary is available on PATH via `which`.
|
|
154
|
+
*/
|
|
155
|
+
function probeBinary(binaryName: string): Promise<boolean> {
|
|
156
|
+
return new Promise((resolve) => {
|
|
157
|
+
execFile("which", [binaryName], { timeout: 5_000 }, (err) => {
|
|
158
|
+
resolve(err === null);
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// ── Public API ──────────────────────────────────────────────────────
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Detect which of the four supported scanners are available in the
|
|
167
|
+
* given workspace. Probes config files, package.json, and binary
|
|
168
|
+
* availability in order, short-circuiting on first match.
|
|
169
|
+
*
|
|
170
|
+
* @param workspaceRoot Absolute path to the project root.
|
|
171
|
+
* @returns One {@link ScannerDetection} per known scanner.
|
|
172
|
+
*/
|
|
173
|
+
export async function detectScanners(
|
|
174
|
+
workspaceRoot: string,
|
|
175
|
+
): Promise<ScannerDetection[]> {
|
|
176
|
+
const scanners: KnownScanner[] = ["eslint", "semgrep", "bandit", "stryker"];
|
|
177
|
+
|
|
178
|
+
const results = await Promise.all(
|
|
179
|
+
scanners.map(async (scanner): Promise<ScannerDetection> => {
|
|
180
|
+
// 1. Config file probe (fastest)
|
|
181
|
+
const configProbe = probeConfigFiles(workspaceRoot, scanner);
|
|
182
|
+
if (configProbe.found && configProbe.path) {
|
|
183
|
+
return {
|
|
184
|
+
scanner,
|
|
185
|
+
available: true,
|
|
186
|
+
reason: `config file found: ${configProbe.path.replace(workspaceRoot + "/", "")}`,
|
|
187
|
+
configPath: configProbe.path,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// 2. Package.json probe
|
|
192
|
+
if (probePackageJson(workspaceRoot, scanner)) {
|
|
193
|
+
return {
|
|
194
|
+
scanner,
|
|
195
|
+
available: true,
|
|
196
|
+
reason: `found in package.json dependencies`,
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// 3. Binary probe (slowest)
|
|
201
|
+
const signals = SCANNER_SIGNALS[scanner];
|
|
202
|
+
for (const bin of signals.binaryNames) {
|
|
203
|
+
if (await probeBinary(bin)) {
|
|
204
|
+
return {
|
|
205
|
+
scanner,
|
|
206
|
+
available: true,
|
|
207
|
+
reason: `binary "${bin}" found on PATH`,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return {
|
|
213
|
+
scanner,
|
|
214
|
+
available: false,
|
|
215
|
+
reason: "no config file, package.json entry, or binary found",
|
|
216
|
+
};
|
|
217
|
+
}),
|
|
218
|
+
);
|
|
219
|
+
|
|
220
|
+
return results;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Exported for testing
|
|
224
|
+
export { SCANNER_SIGNALS };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public SDK entry point for the scanner auto-detection and execution
|
|
3
|
+
* pipeline.
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { autoScan, detectScanners } from "claude-crap/scanner";
|
|
9
|
+
*
|
|
10
|
+
* // Full pipeline: detect → run → ingest
|
|
11
|
+
* const result = await autoScan(workspaceRoot, sarifStore, logger);
|
|
12
|
+
*
|
|
13
|
+
* // Detection only (no execution)
|
|
14
|
+
* const detections = await detectScanners(workspaceRoot);
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @module scanner
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
export { detectScanners, type ScannerDetection } from "./detector.js";
|
|
21
|
+
export { runScanner, type ScannerRunResult } from "./runner.js";
|
|
22
|
+
export { autoScan, type AutoScanResult, type ScannerResult } from "./auto-scan.js";
|
|
23
|
+
export {
|
|
24
|
+
bootstrapScanner,
|
|
25
|
+
detectProjectType,
|
|
26
|
+
generateEslintConfig,
|
|
27
|
+
type BootstrapResult,
|
|
28
|
+
type BootstrapStep,
|
|
29
|
+
type ProjectType,
|
|
30
|
+
} from "./bootstrap.js";
|