import path from "node:path"; import type {RunnerContext, RunnerDefinition, RunnerResult,} from "../types.js"; export class RunnerRegistry { #runners = new Map(); register(runner: RunnerDefinition): void { this.#runners.set(runner.id, runner); } get(id: string): RunnerDefinition | undefined { return this.#runners.get(id); } list(): RunnerDefinition[] { return [...this.#runners.values()].sort( (left, right) => left.priority - right.priority, ); } } export interface RunnerRoute { runner: RunnerDefinition; files: string[]; } /** Match runners to files by extension. Files are already inside the workspace root. */ export function selectRunnerRoutes( runners: RunnerDefinition[], files: string[], ): RunnerRoute[] { const routes: RunnerRoute[] = []; for (const runner of runners) { const extensionSet = new Set(runner.extensions); const matched = files.filter((file) => extensionSet.has(fileExtension(file)), ); if (matched.length > 0) routes.push({runner, files: matched}); } return routes; } /** Restrict runners by id; unknown names fail loudly with the available ids. */ export function filterRunners( runners: RunnerDefinition[], selected: string | string[] | undefined, ): RunnerDefinition[] { if (selected === undefined) return runners; const names = [ ...new Set( (Array.isArray(selected) ? selected : [selected]).map((name) => name.trim(), ), ), ].filter((name) => name.length > 0); if (names.length === 0) throw new Error("Runner parameter must not be blank."); // Runner ids match case-insensitively, mirroring filterAdapters. const missing = names.filter( (name) => !runners.some( (runner) => runner.id.toLowerCase() === name.toLowerCase(), ), ); if (missing.length) { const configured = runners.map((runner) => runner.id).join(", ") || "none"; throw new Error( `Unknown runner(s): ${missing.join(", ")}. Builtin runners: ${configured}.`, ); } const lowerNames = names.map((name) => name.toLowerCase()); return runners.filter((runner) => lowerNames.includes(runner.id.toLowerCase())); } function fileExtension(filePath: string) { // path.extname handles both separators and dotful directory names // (C:\my.project\README -> "") that a bare lastIndexOf(".") would // misread as an extension. return path.extname(filePath); } /** Run selected runners in parallel; an individual runner's throw becomes a failed result, never a lost lane. * * `reportStatus` gives every lane its own status-bar line: the runner spells the text * (`biome installing` / `biome check`) and this dispatcher owns the key plus the clear, * so a throwing or cancelled lane never leaves a status behind. */ export async function dispatchRunnerRoutes( routes: RunnerRoute[], context: { root: string; signal?: AbortSignal; defaultTimeoutMs: number; reportStatus?: (runnerId: string, text: string | undefined) => void; }, ): Promise { return Promise.all( routes.map(async ({runner, files}): Promise => { const report = context.reportStatus; const ctx: RunnerContext = { root: context.root, files, signal: context.signal, timeoutMs: runner.timeoutMs ?? context.defaultTimeoutMs, autoInstall: runner.autoInstall ?? false, reportStatus: report ? (text: string) => { report(runner.id, text); } : undefined, }; try { return await runner.run(ctx); } catch (error) { return { tool: runner.id, status: "failed", diagnostics: [], files, durationMs: 0, failureKind: "exception", failureMessage: error instanceof Error ? error.message : String(error), }; } finally { report?.(runner.id, undefined); } }), ); }