Something went wrong. Try again.
Background process management for Pi. Start long-running commands without blocking the conversation.
Something went wrong. Try again.
1.6 kB · 59 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960// Uses node:child_process directly instead of pi.exec() because process// management requires long-lived streaming processes with stdin/stdout piping// and detached process groups, which pi.exec() does not support.import { type ChildProcess, spawn } from "node:child_process";import { existsSync } from "node:fs";import { isAbsolute } from "node:path";
interface ResolveShellExecutableOptions { configuredShell?: string; knownPaths: string[];}
const DEFAULT_KNOWN_SHELL_PATHS = [ "/run/current-system/sw/bin/bash", "/bin/bash", "/usr/bin/bash", "/usr/local/bin/bash",];
function isExistingAbsolutePath(shell: string | undefined): shell is string { return typeof shell === "string" && isAbsolute(shell) && existsSync(shell);}
export function resolveShellExecutable({ configuredShell, knownPaths,}: ResolveShellExecutableOptions): string { if (isExistingAbsolutePath(configuredShell)) { return configuredShell; }
for (const path of knownPaths) { if (isExistingAbsolutePath(path)) { return path; } }
throw new Error( "Unable to resolve shell executable. Checked configured shell and known shell paths.", );}
export function spawnCommand( command: string, cwd: string, configuredShell?: string,): ChildProcess { const shellExecutable = resolveShellExecutable({ configuredShell, knownPaths: DEFAULT_KNOWN_SHELL_PATHS, });
return spawn(shellExecutable, ["-lc", command], { cwd, env: process.env, stdio: ["pipe", "pipe", "pipe"], detached: true, });}