// rnx desktop — launch the installed desktop companion via the // electron driver. the driver wraps findDesktopCompanion + // launchDesktopCompanion, so this command is a thin CLI surface that // parses options, prompts for install, and delegates everything else. import { ensureProfile } from '../../src/profiles' import { findDesktopCompanion } from '../desktop-companion' import { electronDriver } from '../drivers' import { confirm } from '../prompt' import { rnxExit } from '../run-rnx' import { buildShellUrl } from './control' import { printMissingCompanionHint } from './no-bridge-hint' interface ElectronOptions { port?: number device?: string } function parseInlinePort(args: string[]): number | undefined { const portIdx = args.indexOf('--port') if (portIdx < 0) return undefined const raw = args[portIdx + 1] const port = raw ? Number(raw) : Number.NaN if (!Number.isInteger(port) || port <= 0) { console.error(` invalid --port value: ${raw || '(missing)'}`) rnxExit(1) } return port } export async function resolveElectronLaunchUrl( args: string[], opts: ElectronOptions, ): Promise { const targetPort = parseInlinePort(args) ?? opts.port return targetPort ? buildShellUrl(String(targetPort)) : undefined } export async function runElectron(args: string[], opts: ElectronOptions) { if (args.includes('--help') || args.includes('-h')) { console.log(` rnx desktop — launch the desktop companion usage: rnx desktop [options] options: --port connect to running dev server on this port --profile launch with an isolated persistent storage profile --ephemeral launch with a temporary isolated storage profile examples: rnx desktop rnx desktop --port 5173 rnx desktop --profile qa `) rnxExit(0) } const profileArg = args.find((_, i) => args[i - 1] === '--profile') const ephemeralProfile = args.includes('--ephemeral') if (profileArg && ephemeralProfile) { console.error(' rnx desktop: --profile cannot be combined with --ephemeral') rnxExit(1) } const profileId = profileArg ? ensureProfile(profileArg).id : undefined let install = findDesktopCompanion() if (!install) { printMissingCompanionHint() const canPrompt = process.stdin.isTTY && process.env.CI !== '1' && process.env.RNX_NO_PROMPT !== '1' if (canPrompt && (await confirm('run rnx desktop install now?', true))) { console.log() const { runInstallDesktop } = await import('./install-desktop') await runInstallDesktop(['--yes']) install = findDesktopCompanion() } if (!install) rnxExit(1) } console.log(` launching ${install.path}`) const url = await resolveElectronLaunchUrl(args, opts) const result = await electronDriver.launch({ url, device: opts.device, profileId, ephemeralProfile, }) if (!result.launched) { console.error(` ${result.message}`) rnxExit(1) } console.log(` ${result.message}`) }