/** * @license * Copyright 2025 Vybestack LLC * SPDX-License-Identifier: Apache-2.0 */ /** * Relaunch utilities for the CLI. * Handles spawning child processes with modified arguments/environment. */ import { spawn } from 'node:child_process'; /** * Relaunch the current application in a child process with additional Node.js arguments. * This is used to restart with higher memory limits or enter sandboxed environments. * * The child process inherits stdio for seamless I/O, and receives an environment * variable to prevent infinite relaunch loops. * * @param additionalArgs - Additional Node.js arguments (e.g., --max-old-space-size=4096) * @param argvTail - The argv tail to launch with (defaults to `process.argv.slice(1)` * for compatibility). Callers that need to augment argv (e.g. transporting a * bootstrap path across a memory relaunch) pass a custom tail. * @returns Promise that resolves to the child process exit code */ export async function relaunchAppInChildProcess( additionalArgs: string[], argvTail: string[] = process.argv.slice(1), ): Promise { const nodeArgs = [...additionalArgs, ...argvTail]; const newEnv = { ...process.env, LLXPRT_CODE_NO_RELAUNCH: 'true' }; const child = spawn(process.execPath, nodeArgs, { stdio: 'inherit', env: newEnv, }); return new Promise((resolve) => { child.on('close', (code) => { resolve(code ?? 0); }); }); }