From 13f520f4ac8fb1d4d405857c8449b1ba1702d7e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Kwas=CC=81niewski?= Date: Tue, 2 Jan 2024 16:36:40 +0100 Subject: [PATCH] refactor: adopt getSimulators for OOT platforms --- .../src/commands/logCommand/createLog.ts | 38 +++++++++++++------ .../src/commands/runCommand/createRun.ts | 8 +--- .../src/tools/listDevices.ts | 2 +- 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/packages/cli-platform-apple/src/commands/logCommand/createLog.ts b/packages/cli-platform-apple/src/commands/logCommand/createLog.ts index 1ee6456c1..af3224260 100644 --- a/packages/cli-platform-apple/src/commands/logCommand/createLog.ts +++ b/packages/cli-platform-apple/src/commands/logCommand/createLog.ts @@ -4,9 +4,9 @@ import {spawnSync} from 'child_process'; import os from 'os'; import path from 'path'; import getSimulators from '../../tools/getSimulators'; -import listDevices from '../../tools/listDevices'; +import listDevices, {stripPlatform} from '../../tools/listDevices'; import {getPlatformInfo} from '../runCommand/getPlatformInfo'; -import {BuilderCommand} from '../../types'; +import {BuilderCommand, Device} from '../../types'; import {supportedPlatforms} from '../../config/supportedPlatforms'; import {promptForDeviceToTailLogs} from '../../tools/prompts'; @@ -50,12 +50,14 @@ const createLog = return; } - const bootedAndAvailableSimulators = bootedSimulators.map((booted) => { - const available = availableSimulators.find( - ({udid}) => udid === booted.udid, - ); - return {...available, ...booted}; - }); + const bootedAndAvailableSimulators = bootedSimulators + .map((booted) => { + const available = availableSimulators.find( + ({udid}) => udid === booted.udid, + ); + return {...available, ...booted}; + }) + .filter(({sdk}) => sdk && sdkNames.includes(stripPlatform(sdk))); if (bootedAndAvailableSimulators.length === 0) { logger.error( @@ -70,22 +72,34 @@ const createLog = bootedAndAvailableSimulators, ); - tailDeviceLogs(udid); + const simulator = bootedAndAvailableSimulators.find( + ({udid: deviceUDID}) => deviceUDID === udid, + ); + + if (!simulator) { + throw new CLIError( + `Unable to find simulator with udid: ${udid} in booted simulators`, + ); + } + + tailDeviceLogs(simulator); } else { - tailDeviceLogs(bootedAndAvailableSimulators[0].udid); + tailDeviceLogs(bootedAndAvailableSimulators[0]); } }; -function tailDeviceLogs(udid: string) { +function tailDeviceLogs(device: Device) { const logDir = path.join( os.homedir(), 'Library', 'Logs', 'CoreSimulator', - udid, + device.udid, 'asl', ); + logger.info(`Tailing logs for device ${device.name} (${device.udid})`); + const log = spawnSync('syslog', ['-w', '-F', 'std', '-d', logDir], { stdio: 'inherit', }); diff --git a/packages/cli-platform-apple/src/commands/runCommand/createRun.ts b/packages/cli-platform-apple/src/commands/runCommand/createRun.ts index 3381e722a..f6fa230a7 100644 --- a/packages/cli-platform-apple/src/commands/runCommand/createRun.ts +++ b/packages/cli-platform-apple/src/commands/runCommand/createRun.ts @@ -19,7 +19,6 @@ import { cacheManager, } from '@react-native-community/cli-tools'; import getArchitecture from '../../tools/getArchitecture'; -import getSimulators from '../../tools/getSimulators'; import listDevices from '../../tools/listDevices'; import resolvePods, {getPackageJson} from '../../tools/pods'; import {promptForDeviceSelection} from '../../tools/prompts'; @@ -208,12 +207,7 @@ const createRun = const bootedDevices = availableDevices.filter( ({type}) => type === 'device', ); - - const simulators = getSimulators(); - const bootedSimulators = Object.keys(simulators.devices) - .map((key) => simulators.devices[key]) - .reduce((acc, val) => acc.concat(val), []) - .filter(({state}) => state === 'Booted'); + const bootedSimulators = devices.filter(({type}) => type === 'simulator'); const booted = [...bootedDevices, ...bootedSimulators]; if (booted.length === 0) { diff --git a/packages/cli-platform-apple/src/tools/listDevices.ts b/packages/cli-platform-apple/src/tools/listDevices.ts index f039aa7a0..a96bee029 100644 --- a/packages/cli-platform-apple/src/tools/listDevices.ts +++ b/packages/cli-platform-apple/src/tools/listDevices.ts @@ -54,7 +54,7 @@ async function listDevices(sdkNames: string[]): Promise { return parseXcdeviceList(out, sdkNames); } -function stripPlatform(platform: string): string { +export function stripPlatform(platform: string): string { return platform.replace('com.apple.platform.', ''); }