2026-02-07 17:26:33 +09:00
|
|
|
import type { RunResult } from "./types"
|
|
|
|
|
|
|
|
|
|
export interface JsonOutputManager {
|
|
|
|
|
redirectToStderr: () => void
|
|
|
|
|
restore: () => void
|
|
|
|
|
emitResult: (result: RunResult) => void
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface JsonOutputManagerOptions {
|
|
|
|
|
stdout?: NodeJS.WriteStream
|
|
|
|
|
stderr?: NodeJS.WriteStream
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function createJsonOutputManager(
|
|
|
|
|
options: JsonOutputManagerOptions = {}
|
|
|
|
|
): JsonOutputManager {
|
|
|
|
|
const stdout = options.stdout ?? process.stdout
|
|
|
|
|
const stderr = options.stderr ?? process.stderr
|
|
|
|
|
|
|
|
|
|
const originalWrite = stdout.write.bind(stdout)
|
|
|
|
|
|
|
|
|
|
function redirectToStderr(): void {
|
2026-02-07 17:39:16 +09:00
|
|
|
stdout.write = function (...args: Parameters<NodeJS.WriteStream["write"]>): boolean {
|
|
|
|
|
return (stderr.write as Function).apply(stderr, args)
|
|
|
|
|
} as NodeJS.WriteStream["write"]
|
2026-02-07 17:26:33 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function restore(): void {
|
|
|
|
|
stdout.write = originalWrite
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function emitResult(result: RunResult): void {
|
|
|
|
|
restore()
|
|
|
|
|
originalWrite(JSON.stringify(result) + "\n")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
redirectToStderr,
|
|
|
|
|
restore,
|
|
|
|
|
emitResult,
|
|
|
|
|
}
|
|
|
|
|
}
|