Skip to content

Commit c70db3d

Browse files
authored
refactor: adopt getSimulators for OOT platforms (#2239)
1 parent 4d70c81 commit c70db3d

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

packages/cli-platform-apple/src/commands/logCommand/createLog.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import {spawnSync} from 'child_process';
44
import os from 'os';
55
import path from 'path';
66
import getSimulators from '../../tools/getSimulators';
7-
import listDevices from '../../tools/listDevices';
7+
import listDevices, {stripPlatform} from '../../tools/listDevices';
88
import {getPlatformInfo} from '../runCommand/getPlatformInfo';
9-
import {BuilderCommand} from '../../types';
9+
import {BuilderCommand, Device} from '../../types';
1010
import {supportedPlatforms} from '../../config/supportedPlatforms';
1111
import {promptForDeviceToTailLogs} from '../../tools/prompts';
1212

@@ -50,12 +50,14 @@ const createLog =
5050
return;
5151
}
5252

53-
const bootedAndAvailableSimulators = bootedSimulators.map((booted) => {
54-
const available = availableSimulators.find(
55-
({udid}) => udid === booted.udid,
56-
);
57-
return {...available, ...booted};
58-
});
53+
const bootedAndAvailableSimulators = bootedSimulators
54+
.map((booted) => {
55+
const available = availableSimulators.find(
56+
({udid}) => udid === booted.udid,
57+
);
58+
return {...available, ...booted};
59+
})
60+
.filter(({sdk}) => sdk && sdkNames.includes(stripPlatform(sdk)));
5961

6062
if (bootedAndAvailableSimulators.length === 0) {
6163
logger.error(
@@ -70,22 +72,34 @@ const createLog =
7072
bootedAndAvailableSimulators,
7173
);
7274

73-
tailDeviceLogs(udid);
75+
const simulator = bootedAndAvailableSimulators.find(
76+
({udid: deviceUDID}) => deviceUDID === udid,
77+
);
78+
79+
if (!simulator) {
80+
throw new CLIError(
81+
`Unable to find simulator with udid: ${udid} in booted simulators`,
82+
);
83+
}
84+
85+
tailDeviceLogs(simulator);
7486
} else {
75-
tailDeviceLogs(bootedAndAvailableSimulators[0].udid);
87+
tailDeviceLogs(bootedAndAvailableSimulators[0]);
7688
}
7789
};
7890

79-
function tailDeviceLogs(udid: string) {
91+
function tailDeviceLogs(device: Device) {
8092
const logDir = path.join(
8193
os.homedir(),
8294
'Library',
8395
'Logs',
8496
'CoreSimulator',
85-
udid,
97+
device.udid,
8698
'asl',
8799
);
88100

101+
logger.info(`Tailing logs for device ${device.name} (${device.udid})`);
102+
89103
const log = spawnSync('syslog', ['-w', '-F', 'std', '-d', logDir], {
90104
stdio: 'inherit',
91105
});

packages/cli-platform-apple/src/commands/runCommand/createRun.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
cacheManager,
2020
} from '@react-native-community/cli-tools';
2121
import getArchitecture from '../../tools/getArchitecture';
22-
import getSimulators from '../../tools/getSimulators';
2322
import listDevices from '../../tools/listDevices';
2423
import resolvePods, {getPackageJson} from '../../tools/pods';
2524
import {promptForDeviceSelection} from '../../tools/prompts';
@@ -208,12 +207,7 @@ const createRun =
208207
const bootedDevices = availableDevices.filter(
209208
({type}) => type === 'device',
210209
);
211-
212-
const simulators = getSimulators();
213-
const bootedSimulators = Object.keys(simulators.devices)
214-
.map((key) => simulators.devices[key])
215-
.reduce((acc, val) => acc.concat(val), [])
216-
.filter(({state}) => state === 'Booted');
210+
const bootedSimulators = devices.filter(({type}) => type === 'simulator');
217211

218212
const booted = [...bootedDevices, ...bootedSimulators];
219213
if (booted.length === 0) {

packages/cli-platform-apple/src/tools/listDevices.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ async function listDevices(sdkNames: string[]): Promise<Device[]> {
5454
return parseXcdeviceList(out, sdkNames);
5555
}
5656

57-
function stripPlatform(platform: string): string {
57+
export function stripPlatform(platform: string): string {
5858
return platform.replace('com.apple.platform.', '');
5959
}
6060

0 commit comments

Comments
 (0)