#!/usr/bin/env node /** * Internal TTY-preserving runner for restart-desktop-remote.mjs. * Electron writes the ready status; this runner records an early pnpm/Forge exit. */ import { spawn } from 'node:child_process'; import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { resolvePnpmInvocation, usablePnpmExecPath } from './shared/pnpm-invocation.mjs'; const rootDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); const mode = process.argv[2]; if (mode !== 'remote' && mode !== 'local') { console.error(`desktop-dev-runner: mode must be remote or local, received ${mode ?? '(empty)'}`); process.exit(2); } const devScript = mode === 'local' ? 'dev:desktop' : 'dev:desktop:remote'; // The restart pipeline opens a fresh Windows cmd.exe. That environment does // not always carry npm_execpath, and may carry a stale one, so the path is // validated before use; resolvePnpmInvocation then decides how to execute it // (JS entry via node, native binary directly, command wrapper through PATH), // and falls back to PATH/PATHEXT resolution when nothing usable is left. const invocation = resolvePnpmInvocation([devScript], { npmExecPath: usablePnpmExecPath(process.env.npm_execpath, fs.existsSync), }); const { command, args } = invocation; const relaunchSignalPath = path.join( os.tmpdir(), `cindy-desktop-dev-relaunch-${process.pid}-${Date.now()}-${Math.random().toString(16).slice(2)}.json`, ); const restartedRequestIds = new Set(); startChild(); function startChild() { const child = spawn(command, args, { cwd: rootDir, env: { ...process.env, COREPACK_ENABLE_AUTO_PIN: '0', XDT_DESKTOP_DEV_RELAUNCH_SIGNAL_FILE: relaunchSignalPath, ...(invocation.env ?? {}), }, stdio: 'inherit', windowsHide: false, shell: invocation.shell, windowsVerbatimArguments: invocation.windowsVerbatimArguments, }); child.once('error', (error) => { fs.rmSync(relaunchSignalPath, { force: true }); writeFailedStatus({ exitCode: null, error: error.message }); console.error(`desktop-dev-runner: ${error.message}`); process.exit(1); }); child.once('exit', (code, signal) => { const relaunchRequest = consumeRelaunchRequest(); if (relaunchRequest && !restartedRequestIds.has(relaunchRequest.requestId)) { restartedRequestIds.add(relaunchRequest.requestId); console.log( `desktop-dev-runner: restarting Forge/Vite for database cleanup ${relaunchRequest.requestId}`, ); startChild(); return; } if (relaunchRequest) { console.error( `desktop-dev-runner: refused repeated database cleanup relaunch ${relaunchRequest.requestId}`, ); } const statusPath = process.env.XDT_DESKTOP_DEV_STARTUP_STATUS_FILE; const existing = statusPath ? readStatus(statusPath) : null; if (statusPath) { if (existing?.state === 'ready' || existing?.state === 'abandoned' || !existing) { fs.rmSync(statusPath, { force: true }); } else if (existing.state !== 'failed') { writeFailedStatus({ exitCode: code, signal }); } } if (signal && process.platform !== 'win32') { process.kill(process.pid, signal); return; } process.exit(code ?? 1); }); } function consumeRelaunchRequest() { try { const value = JSON.parse(fs.readFileSync(relaunchSignalPath, 'utf8')); if ( value?.version !== 1 || typeof value.requestId !== 'string' || value.requestId.length === 0 || value.requestId.length > 128 ) { return null; } return { requestId: value.requestId }; } catch { return null; } finally { fs.rmSync(relaunchSignalPath, { force: true }); } } function readStatus(statusPath) { try { return JSON.parse(fs.readFileSync(statusPath, 'utf8')); } catch { return null; } } function writeFailedStatus(detail) { const statusPath = process.env.XDT_DESKTOP_DEV_STARTUP_STATUS_FILE; const state = statusPath ? readStatus(statusPath)?.state : null; if (!statusPath || state === 'ready' || state === 'abandoned') return; const errorMessage = detail.error ? ` Desktop dev command failed: ${detail.error}` : ''; writeStatus(statusPath, { state: 'failed', code: 'DEV_PROCESS_EXITED', message: `The desktop dev process exited before the main window became ready.${errorMessage}`, detail: { rootDir, command, devScript, ...detail, }, ...detail, pid: process.pid, at: Date.now(), }); } function writeStatus(statusPath, status) { const tempPath = `${statusPath}.${process.pid}.tmp`; try { fs.writeFileSync(tempPath, `${JSON.stringify(status)}\n`, { mode: 0o600 }); fs.renameSync(tempPath, statusPath); } catch (error) { fs.rmSync(tempPath, { force: true }); console.error(`desktop-dev-runner: failed to write startup status: ${error.message}`); } }