import type { ChildProcess } from "node:child_process"; /** * Ensures that all data from a child process's stdout and stderr pipes * has been fully drained before resolving. This prevents a common race * condition where the process exits but the OS hasn't finished flushing * the last few buffers to the parent. * * NOTE: This is forward-looking scaffolding for when the codebase spawns * async child processes. Currently all child process use is synchronous * (execFileSync), which has no stdio pipes to drain. */ export async function waitForProcessDrain(child: ChildProcess): Promise { const pipes: Promise[] = []; if (child.stdout) { pipes.push(new Promise((resolve) => { child.stdout?.on("end", resolve); // Ensure we don't hang if stdout was already closed if (child.stdout?.destroyed) resolve(); })); } if (child.stderr) { pipes.push(new Promise((resolve) => { child.stderr?.on("end", resolve); // Ensure we don't hang if stderr was already closed if (child.stderr?.destroyed) resolve(); })); } await Promise.all(pipes); }