34 lines
978 B
TypeScript
34 lines
978 B
TypeScript
import type { FormatterConfig } from "../schema.ts";
|
|
|
|
export function createCommandFormatterRunner(deps: {
|
|
execCommand: (
|
|
command: string,
|
|
args: string[],
|
|
options: { cwd: string; timeout?: number },
|
|
) => Promise<{ code: number; stdout: string; stderr: string }>;
|
|
}) {
|
|
return {
|
|
async formatFile(input: {
|
|
absolutePath: string;
|
|
workspaceRoot: string;
|
|
formatter: FormatterConfig;
|
|
timeoutMs?: number;
|
|
}) {
|
|
const [command, ...args] = input.formatter.command.map((part) => part.replaceAll("{file}", input.absolutePath));
|
|
const result = await deps.execCommand(command, args, {
|
|
cwd: input.workspaceRoot,
|
|
timeout: input.timeoutMs,
|
|
});
|
|
|
|
if (result.code !== 0) {
|
|
return {
|
|
status: "failed" as const,
|
|
message: (result.stderr || result.stdout || `formatter exited with ${result.code}`).trim(),
|
|
};
|
|
}
|
|
|
|
return { status: "formatted" as const };
|
|
},
|
|
};
|
|
}
|