// Run the app on an iOS simulator. Run outside `nix develop`. import { execFileSync, spawn } from 'node:child_process'; const XCODE = '/Applications/Xcode.app/Contents/Developer'; const SIM_NAME = 'Synapse Sim'; const env = { ...process.env, DEVELOPER_DIR: XCODE }; for (const k of [ 'SDKROOT', 'MACOSX_DEPLOYMENT_TARGET', 'NIX_CFLAGS_COMPILE', 'NIX_CFLAGS_COMPILE_FOR_BUILD', ]) delete env[k]; const xcrun = (args) => execFileSync('xcrun', args, { encoding: 'utf8', env }); const simctl = (args) => xcrun(['simctl', ...args]); function sdkMajor() { const m = /iPhoneSimulator(\d+)\./.exec(xcrun(['--sdk', 'iphonesimulator', '--show-sdk-path'])); return m ? m[1] : null; } const data = JSON.parse(simctl(['list', 'devices', '--json'])); const runtimes = JSON.parse(simctl(['list', 'runtimes', '--json'])).runtimes; const major = sdkMajor(); const runtime = runtimes.find( (r) => r.isAvailable && new RegExp(`iOS-${major}-`).test(r.identifier), ); if (!runtime) { console.error(`No iOS ${major}.x simulator runtime installed.`); process.exit(1); } let udid; for (const list of Object.values(data.devices)) for (const d of list) if (d.name === SIM_NAME && d.isAvailable) udid = d.udid; if (!udid) { console.log(`Creating simulator "${SIM_NAME}" (iOS ${major}.x)...`); udid = simctl(['create', SIM_NAME, 'iPhone 16', runtime.identifier]).trim(); } const booted = Object.values(data.devices) .flat() .some((d) => d.udid === udid && d.state === 'Booted'); if (!booted) { try { simctl(['boot', udid]); } catch { // already booting } spawn('open', ['-a', 'Simulator'], { stdio: 'ignore', detached: true }).unref(); } console.log(`Running on "${SIM_NAME}" (iOS ${major}.x)`); const dev = spawn('pnpm', ['tauri', 'ios', 'dev', SIM_NAME], { stdio: 'inherit', env }); dev.on('exit', (code) => process.exit(code ?? 0));