import { randomUUID } from "node:crypto"; import { writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; /** * Shell argument limit is often around 128KB or lower depending on the OS. * We'll play it safe and use 32KB as our threshold to switch to @file syntax. */ const COMMAND_LINE_MAX_LENGTH = 32_768; /** * Automatically detects if a string is too long for shell argument limits. * If too long, writes it to a temp file and returns `@`. * Otherwise, returns the original string. * * NOTE: Callers MUST clean up the temp file (unlinkSync) after use. * Temp files are written to os.tmpdir() with no automatic cleanup. * * Works well with tools that support @file syntax (like curl or custom pi tools). */ export function wrapLongArg(text: string): string { if (text.length < COMMAND_LINE_MAX_LENGTH) { return text; } const tmpPath = join(tmpdir(), `pi-arg-${randomUUID()}.txt`); writeFileSync(tmpPath, text, "utf-8"); return `@${tmpPath}`; }