Files
sprites/tools/build/exec.ts
Christopher Monsanto f6e0b189f3 Cut over to content-addressed deploys
pnpm build/deploy now run the umbrella CLI (build/deploy/run/inspect); the
fixed-name build/ tree, the old planner (rename detection, adopt, tamper
checks), and the vm deploy sandbox are gone. Also lands tools/deploy/api.ts
and the CLI, which earlier commits referenced but an overbroad local
gitignore pattern had silently excluded.

Migration: the store migrates the v1 db in place; build/ is dead and can be
deleted. A full rebuild repopulated .build/cas, and the old and new deploy
trees were diffed: byte-identical except the two multi-file hash pointers
(algorithm change), fb/twitter PNG re-encodes (pixel-identical; old outputs
predated current tool versions), and a stale 2023 orphan sprite the old
system never cleaned up.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-21 01:13:12 -04:00

82 lines
2.6 KiB
TypeScript

import {spawn} from 'child_process';
export interface ExecResult {
code : number | null;
signal : NodeJS.Signals | null;
output : string;
durationMs : number;
}
const livePids = new Set<number>();
// Emergency stop (e.g. second Ctrl-C): SIGKILL every live process group.
export function killAllProcessGroups() : void {
for (const pid of livePids) {
try {
process.kill(-pid, 'SIGKILL');
} catch {}
}
}
// The command script is fed to sh via stdin rather than -c: a single argv
// entry is capped by the kernel (MAX_ARG_STRLEN, ~128KB) and the largest %f
// expansion is already 80KB+.
export function runShell(command : string, opts : {cwd : string, signal : AbortSignal}) : Promise<ExecResult> {
return new Promise((resolve, reject) => {
const start = performance.now();
// detached: own process group, so an abort kills grandchildren
// (magick, optipng, ...) with one signal
const child = spawn('sh', [], {cwd: opts.cwd, detached: true, stdio: ['pipe', 'pipe', 'pipe']});
if (child.pid !== undefined) {
livePids.add(child.pid);
}
const chunks : Buffer[] = [];
child.stdout.on('data', c => chunks.push(c));
child.stderr.on('data', c => chunks.push(c));
child.stdin.on('error', () => {}); // EPIPE if the shell exits early
child.stdin.end(command + '\n');
let killTimer : NodeJS.Timeout | undefined;
const kill = (sig : NodeJS.Signals) => {
try {
process.kill(-child.pid!, sig);
} catch {}
};
const onAbort = () => {
kill('SIGTERM');
killTimer = setTimeout(() => kill('SIGKILL'), 5000);
killTimer.unref();
};
if (opts.signal.aborted) {
onAbort();
} else {
opts.signal.addEventListener('abort', onAbort, {once: true});
}
const cleanup = () => {
if (child.pid !== undefined) {
livePids.delete(child.pid);
}
opts.signal.removeEventListener('abort', onAbort);
if (killTimer !== undefined) {
clearTimeout(killTimer);
}
};
child.on('error', err => {
cleanup();
reject(err);
});
child.on('close', (code, signal) => {
cleanup();
resolve({
code,
signal,
output: Buffer.concat(chunks).toString(),
durationMs: performance.now() - start,
});
});
});
}