import { spawn } from 'node:child_process'; import * as fs from 'node:fs'; import * as readline from 'node:readline/promises'; import * as tty from 'node:tty'; import { homedir } from 'node:os'; import { fileURLToPath } from 'node:url'; import { homeFrom } from './serve-protocol.js'; import { spawnRefresh } from './update-check.js'; export type Env = Record; /** A hook's stdin is read for at most this long; Claude Code writes it at once and closes it. */ export const HOOK_STDIN_CAP_MS = 1000; /** * Everything a command touches outside the store and adapters. The bin passes * `processIo()`; tests pass a recording double. */ export interface Io { cwd: string; env: Env; /** The script the CLI was started as (`process.argv[1]`); undefined under `node -e`. */ entry: string | undefined; stdinIsTTY: boolean; /** Whether stdout is a terminal (colour is off when it is not); absent means no. */ stdoutIsTTY?: boolean; /** Whether stderr is a terminal: the update notice is printed there only then. Absent means no. */ stderrIsTTY?: boolean; /** The terminal's width on stderr, when it is one (a progress line must not wrap). */ stderrColumns?(): number | undefined; out(text: string): void; err(text: string): void; /** All of stdin, or what arrived within `capMs`. */ readStdin(capMs?: number): Promise; /** Prints `question` on stderr (never stdout, which may be piped data) and reads one line from stdin. */ ask(question: string): Promise; /** * Asks on the controlling terminal itself (`/dev/tty`), never stdin: an answer piped in is not * read. Undefined when the process has no controlling terminal. This narrows what counts as a * confirmation at a terminal; a program that drives a pseudo-terminal can still answer. */ askTerminal?(question: string): Promise; /** Like `ask` (the question on stderr), with the typed input hidden on a terminal; a pipe supplies one line. Never echoed. */ askSecret(question: string): Promise; /** Starts a detached `oort hook drain` in `cwd` and does not wait for it. */ spawnDrain(cwd: string): void; /** * Starts the detached background update check (`update-refresh.js`) and does not wait for it. * Absent: no background check (tests that do not record one). */ spawnUpdateCheck?(): void; /** Writes the hook's stdout, then exits 0 as soon as the write is flushed. */ hookExit(stdout: string): void; } /** * Runs a child with the terminal attached for input (npm, the macOS `security` prompt), but its * stdout on our stderr: stdout stays for the command's own output (one `oort.v1` envelope with * `--json`), and a child's progress or prompt still reaches the person. Resolves the exit code. */ export function runToStderr( file: string, args: string[], opts: { env?: NodeJS.ProcessEnv } = {}, spawnFn: typeof spawn = spawn, ): Promise { return new Promise((resolve, reject) => { spawnFn(file, args, { stdio: ['inherit', 2, 2], ...(opts.env !== undefined && { env: opts.env }), }) .once('error', reject) .once('close', (code) => resolve(code ?? 1)); }); } /** * Runs an installer (npm, pnpm) with no shell, its stdout and stderr forwarded to our stderr as * they come (stdout stays for `--json`'s envelope) and the last 64 KB of both kept, so a caller * can tell why it failed (npm's ETARGET right after a release). Resolves the exit code and that * tail; a child that cannot start resolves code 127. */ export function runForwarding( file: string, args: readonly string[], opts: { env?: NodeJS.ProcessEnv; cwd?: string } = {}, spawnFn: typeof spawn = spawn, ): Promise<{ code: number; output: string }> { return new Promise((resolve) => { let tail = ''; const keep = (c: Buffer): void => { process.stderr.write(c); tail = (tail + c.toString('utf8')).slice(-65_536); }; const child = spawnFn(file, [...args], { stdio: ['inherit', 'pipe', 'pipe'], ...(opts.env !== undefined && { env: opts.env }), ...(opts.cwd !== undefined && { cwd: opts.cwd }), }); child.stdout?.on('data', keep); child.stderr?.on('data', keep); child.once('error', (e) => resolve({ code: 127, output: `${tail}${e.message}` })); child.once('close', (code) => resolve({ code: code ?? 1, output: tail })); }); } /** * Runs `node