import { ChildProcess, type ChildProcessByStdio, type ChildProcessWithoutNullStreams, type ExecFileOptions, type ExecFileSyncOptionsWithStringEncoding, execFile, execFileSync, type SpawnOptions, type SpawnOptionsWithoutStdio, type SpawnOptionsWithStdioTuple, type SpawnSyncOptions, type SpawnSyncOptionsWithStringEncoding, type SpawnSyncReturns, type StdioNull, type StdioPipe, spawn, spawnSync, } from 'node:child_process'; import type { Readable, Writable } from 'node:stream'; import { mendBeforeStart, programArgumentsOf } from './uid-cache.js'; /** * The one way this package and its tests start a process, so every one starts with no window. * A process on a desktop of its own is started by a helper, which `spawnOnDesktop` in `desktop.ts` * starts here: Node cannot name a desktop. * * A process with no console, which this server is under a harness and a gate run is when detached, * gives every console program it starts a console window of its own on the desktop in use: a gate * run detached after a push opened about twenty Godot console windows on the screen of the person * at the machine. `windowsHide` is what keeps them off it, and it is set here rather than left to * each call to remember. A regression refuses `child_process` and `Bun.spawn` anywhere else. */ export type { ChildProcess, ChildProcessWithoutNullStreams, SpawnOptions, SpawnSyncReturns, } from 'node:child_process'; /** * A process some other process started, which its starter reports on: a start through the desktop * helper, whose events and streams `desktop-process.ts` fills in. */ export class RelayedChild extends ChildProcess {} /** A child that was never started: no pid and no streams, for the answer to a start that could not be made. */ export function unstartedChild(): ChildProcess { return new ChildProcess(); } /** * Sets the uid cache and the class list of the project a start names right. Only the program's * arguments before a bare `--` are its own: what follows belongs to the game, whose `--path` is no * project of ours. */ function mendProjectOf(command: string, args: readonly string[], cwd: string | URL | undefined): void { const own = programArgumentsOf(command, args); const end = own.indexOf('--'); mendBeforeStart(end < 0 ? own : own.slice(0, end), cwd); } type Stdio = StdioNull | StdioPipe; type Piped = S extends StdioPipe ? Stream : null; /** A child with no window, typed by which of its streams are piped, as Node's own `spawn` is. */ export function spawnHidden( command: string, args: readonly string[], options: SpawnOptionsWithStdioTuple, ): ChildProcessByStdio, Piped, Piped>; export function spawnHidden( command: string, args: readonly string[], options?: SpawnOptionsWithoutStdio, ): ChildProcessWithoutNullStreams; export function spawnHidden(command: string, args: readonly string[], options: SpawnOptions): ChildProcess; export function spawnHidden( command: string, args: readonly string[], options: SpawnOptions = {}, ): ChildProcess { mendProjectOf(command, args, options.cwd); return spawn(command, [...args], { ...options, windowsHide: true }); } /** * A game, whose window is what it is run for: Windows applies the hiding flag to the first window a * program shows as well as to its console, so hiding the start would hide the game. The keeper * starts it detached, which gives a console program no console at all. */ export function spawnShown( command: string, args: readonly string[], options: SpawnOptions = {}, ): ChildProcess { mendProjectOf(command, args, options.cwd); return spawn(command, [...args], { ...options, windowsHide: false }); } /** What a hidden child printed, as text. A failure rejects with what it printed attached. */ export function runHidden( command: string, args: readonly string[], options: ExecFileOptions = {}, ): Promise<{ stdout: string; stderr: string }> { mendProjectOf(command, args, options.cwd ?? undefined); return new Promise((resolve, reject) => { execFile( command, [...args], { ...options, encoding: 'utf8', windowsHide: true }, (error: Error | null, stdout: string, stderr: string) => { if (error !== null) { Object.assign(error, { stdout, stderr }); reject(error); return; } resolve({ stdout, stderr }); }, ); }); } /** What a hidden child printed, waited for. It throws as `execFileSync` does when the child fails. */ export function runSyncHidden( command: string, args: readonly string[], options: ExecFileSyncOptionsWithStringEncoding, ): string { mendProjectOf(command, args, options.cwd ?? undefined); return execFileSync(command, [...args], { ...options, windowsHide: true }); } /** A hidden child run to its end before this returns, typed by its encoding as `spawnSync` is. */ export function spawnSyncHidden( command: string, args: readonly string[], options: SpawnSyncOptionsWithStringEncoding, ): SpawnSyncReturns; export function spawnSyncHidden( command: string, args: readonly string[], options?: SpawnSyncOptions, ): SpawnSyncReturns; export function spawnSyncHidden( command: string, args: readonly string[], options: SpawnSyncOptions = {}, ): SpawnSyncReturns { mendProjectOf(command, args, options.cwd); return spawnSync(command, [...args], { ...options, windowsHide: true }); }