|
| 1 | +import { normalizeError } from '../../utils/errors.ts'; |
| 2 | +import { runCmd } from '../../utils/exec.ts'; |
| 3 | +import type { DeviceInfo } from '../../utils/device.ts'; |
| 4 | +import { contextFromFlags } from '../context.ts'; |
| 5 | +import type { DaemonRequest, DaemonResponse, SessionState } from '../types.ts'; |
| 6 | +import { SessionStore } from '../session-store.ts'; |
| 7 | +import { stopIosRunnerSession } from '../../platforms/ios/runner-client.ts'; |
| 8 | +import { shutdownSimulator } from '../../platforms/ios/simulator.ts'; |
| 9 | +import { |
| 10 | + clearRuntimeHintsFromApp, |
| 11 | + hasRuntimeTransportHints, |
| 12 | +} from '../runtime-hints.ts'; |
| 13 | +import { cleanupRetainedMaterializedPathsForSession } from '../materialized-path-registry.ts'; |
| 14 | +import { |
| 15 | + IOS_SIMULATOR_POST_CLOSE_SETTLE_MS, |
| 16 | + isAndroidEmulator, |
| 17 | + isIosSimulator, |
| 18 | + settleIosSimulator, |
| 19 | +} from './session-device-utils.ts'; |
| 20 | + |
| 21 | +type AppLogStream = NonNullable<SessionState['appLog']>; |
| 22 | + |
| 23 | +async function shutdownAndroidEmulator(device: DeviceInfo): Promise<{ |
| 24 | + success: boolean; |
| 25 | + exitCode: number; |
| 26 | + stdout: string; |
| 27 | + stderr: string; |
| 28 | +}> { |
| 29 | + const result = await runCmd('adb', ['-s', device.id, 'emu', 'kill'], { allowFailure: true, timeoutMs: 15_000 }); |
| 30 | + return { |
| 31 | + success: result.exitCode === 0, |
| 32 | + exitCode: result.exitCode, |
| 33 | + stdout: String(result.stdout ?? ''), |
| 34 | + stderr: String(result.stderr ?? ''), |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +export type ShutdownAndroidEmulatorFn = typeof shutdownAndroidEmulator; |
| 39 | + |
| 40 | +type SessionShutdownResult = { |
| 41 | + success: boolean; |
| 42 | + exitCode: number; |
| 43 | + stdout: string; |
| 44 | + stderr: string; |
| 45 | + error?: ReturnType<typeof normalizeError>; |
| 46 | +}; |
| 47 | + |
| 48 | +async function maybeShutdownSessionTarget(params: { |
| 49 | + device: DeviceInfo; |
| 50 | + shutdownRequested: boolean | undefined; |
| 51 | + shutdownSimulator: typeof shutdownSimulator; |
| 52 | + shutdownAndroidEmulator: typeof shutdownAndroidEmulator; |
| 53 | +}): Promise<SessionShutdownResult | undefined> { |
| 54 | + const { device, shutdownRequested, shutdownSimulator, shutdownAndroidEmulator } = params; |
| 55 | + if (!shutdownRequested) return undefined; |
| 56 | + if (!isIosSimulator(device) && !isAndroidEmulator(device)) return undefined; |
| 57 | + try { |
| 58 | + return isIosSimulator(device) |
| 59 | + ? await shutdownSimulator(device) |
| 60 | + : await shutdownAndroidEmulator(device); |
| 61 | + } catch (error) { |
| 62 | + const normalized = normalizeError(error); |
| 63 | + return { |
| 64 | + success: false, |
| 65 | + exitCode: -1, |
| 66 | + stdout: '', |
| 67 | + stderr: normalized.message, |
| 68 | + error: normalized, |
| 69 | + }; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +export async function handleCloseCommand(params: { |
| 74 | + req: DaemonRequest; |
| 75 | + sessionName: string; |
| 76 | + logPath: string; |
| 77 | + sessionStore: SessionStore; |
| 78 | + dispatch: (device: DeviceInfo, command: string, positionals: string[], out?: string, context?: Record<string, unknown>) => Promise<Record<string, unknown> | void>; |
| 79 | + stopIosRunner?: typeof stopIosRunnerSession; |
| 80 | + clearRuntimeHints?: typeof clearRuntimeHintsFromApp; |
| 81 | + settleSimulator?: typeof settleIosSimulator; |
| 82 | + shutdownSimulator?: typeof shutdownSimulator; |
| 83 | + shutdownAndroidEmulator?: ShutdownAndroidEmulatorFn; |
| 84 | + appLogOps: { |
| 85 | + stop: (stream: AppLogStream) => Promise<void>; |
| 86 | + }; |
| 87 | +}): Promise<DaemonResponse> { |
| 88 | + const { |
| 89 | + req, |
| 90 | + sessionName, |
| 91 | + logPath, |
| 92 | + sessionStore, |
| 93 | + dispatch, |
| 94 | + stopIosRunner = stopIosRunnerSession, |
| 95 | + clearRuntimeHints = clearRuntimeHintsFromApp, |
| 96 | + settleSimulator = settleIosSimulator, |
| 97 | + shutdownSimulator: shutdownSimulatorFn = shutdownSimulator, |
| 98 | + shutdownAndroidEmulator: shutdownAndroidEmulatorFn = shutdownAndroidEmulator, |
| 99 | + appLogOps, |
| 100 | + } = params; |
| 101 | + const session = sessionStore.get(sessionName); |
| 102 | + if (!session) { |
| 103 | + return { ok: false, error: { code: 'SESSION_NOT_FOUND', message: 'No active session' } }; |
| 104 | + } |
| 105 | + if (session.appLog) { |
| 106 | + await appLogOps.stop(session.appLog); |
| 107 | + } |
| 108 | + if (req.positionals && req.positionals.length > 0) { |
| 109 | + if (session.device.platform === 'ios') { |
| 110 | + await stopIosRunner(session.device.id); |
| 111 | + } |
| 112 | + await dispatch(session.device, 'close', req.positionals, req.flags?.out, { |
| 113 | + ...contextFromFlags(logPath, req.flags, session.appBundleId, session.trace?.outPath), |
| 114 | + }); |
| 115 | + await settleSimulator(session.device, IOS_SIMULATOR_POST_CLOSE_SETTLE_MS); |
| 116 | + } |
| 117 | + if (session.device.platform === 'ios') { |
| 118 | + // The targeted close path stops before dispatch to avoid runner/app races. |
| 119 | + // Stop again here so both plain and targeted closes end with the runner down. |
| 120 | + await stopIosRunner(session.device.id); |
| 121 | + } |
| 122 | + const runtime = sessionStore.getRuntimeHints(sessionName); |
| 123 | + if (hasRuntimeTransportHints(runtime) && session.appBundleId) { |
| 124 | + await clearRuntimeHints({ |
| 125 | + device: session.device, |
| 126 | + appId: session.appBundleId, |
| 127 | + }).catch(() => {}); |
| 128 | + } |
| 129 | + sessionStore.recordAction(session, { |
| 130 | + command: 'close', |
| 131 | + positionals: req.positionals ?? [], |
| 132 | + flags: req.flags ?? {}, |
| 133 | + result: { session: sessionName }, |
| 134 | + }); |
| 135 | + if (req.flags?.saveScript) { |
| 136 | + session.recordSession = true; |
| 137 | + } |
| 138 | + sessionStore.writeSessionLog(session); |
| 139 | + await cleanupRetainedMaterializedPathsForSession(sessionName).catch(() => {}); |
| 140 | + sessionStore.delete(sessionName); |
| 141 | + const shutdownResult = await maybeShutdownSessionTarget({ |
| 142 | + device: session.device, |
| 143 | + shutdownRequested: req.flags?.shutdown, |
| 144 | + shutdownSimulator: shutdownSimulatorFn, |
| 145 | + shutdownAndroidEmulator: shutdownAndroidEmulatorFn, |
| 146 | + }); |
| 147 | + if (shutdownResult) { |
| 148 | + return { |
| 149 | + ok: true, |
| 150 | + data: { session: sessionName, shutdown: shutdownResult }, |
| 151 | + }; |
| 152 | + } |
| 153 | + return { ok: true, data: { session: sessionName } }; |
| 154 | +} |
0 commit comments