import { access, readFile } from "node:fs/promises"; import { join } from "node:path"; const COMMAND_START = /^(?:npm|pnpm|yarn|bun|node|npx|git|go|cargo|pytest|python3?|make|cmake|gradle|mvn|dotnet|ruby|bundle|composer)\b/i; export function explicitAcceptanceCommands(objective: string): string[] { return [...objective.matchAll(/`([^`\r\n]+)`/g)].map((match) => match[1]?.trim() ?? "").filter((command) => COMMAND_START.test(command)); } export function explicitAcceptanceCommand(objective: string): string | undefined { const commands = [...objective.matchAll(/(?:\bacceptance(?:\s+command)?)\s*:\s*`([^`\r\n]+)`/gi)].map((match) => match[1]?.trim()).filter((command): command is string => Boolean(command)); if (commands.length > 1) throw new Error("Task must declare exactly one acceptance command"); return commands[0]; } async function exists(path: string): Promise { return access(path).then(() => true, () => false); } export async function inferAcceptanceCommand(cwd: string, trusted = false): Promise { if (!trusted) return undefined; try { const pkg = JSON.parse(await readFile(join(cwd, "package.json"), "utf8")) as { scripts?: Record }; const scripts = pkg.scripts ?? {}; const runner = await exists(join(cwd, "bun.lock")) || await exists(join(cwd, "bun.lockb")) ? "bun" : await exists(join(cwd, "pnpm-lock.yaml")) ? "pnpm" : await exists(join(cwd, "yarn.lock")) ? "yarn" : "npm"; if (typeof scripts.verify === "string") return `${runner} run verify`; if (typeof scripts.test === "string" && !scripts.test.includes("Error: no test specified")) return `${runner} test`; } catch {} if (await exists(join(cwd, "go.mod"))) return "go test ./..."; if (await exists(join(cwd, "Cargo.toml"))) return "cargo test"; if (await Promise.any(["pytest.ini", "pyproject.toml", "setup.cfg", "tox.ini"].map((file) => access(join(cwd, file)))).then(() => true, () => false)) return "pytest"; return undefined; }