Skip to content

Commit

Permalink
fix: log-ios command
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonrybczak committed Mar 27, 2023
1 parent ae566b4 commit e511375
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
44 changes: 21 additions & 23 deletions packages/cli-platform-ios/src/commands/logIOS/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,42 @@
*
*/

import {execFileSync, spawnSync} from 'child_process';
import {spawnSync} from 'child_process';
import os from 'os';
import path from 'path';
import {logger} from '@react-native-community/cli-tools';
import {Device} from '../../types';

function findAvailableDevice(devices: {[index: string]: Array<Device>}) {
for (const key of Object.keys(devices)) {
for (const device of devices[key]) {
if (device.availability === '(available)' && device.state === 'Booted') {
return device;
}
}
}
return null;
}
import listIOSDevices from '../../tools/listIOSDevices';
import {getSimulators} from '../runIOS';

/**
* Starts iOS device syslog tail
*/
async function logIOS() {
const rawDevices = execFileSync(
'xcrun',
['simctl', 'list', 'devices', '--json'],
{encoding: 'utf8'},
// Here we're using two command because first command `xcrun simctl list --json devices` outputs `state` but doesn't return `available`. But second command `xcrun xcdevice list` outputs `available` but doesn't output `state`. So we need to connect outputs of both commands.
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 devices = await listIOSDevices();
const availableSimulators = devices.filter(
({type, isAvailable}) => type === 'simulator' && isAvailable,
);

const {devices} = JSON.parse(rawDevices) as {
devices: {[index: string]: Array<Device>};
};
const bootedAndAvailableSimulators = bootedSimulators.map((booted) => {
const available = availableSimulators.find(
({udid}) => udid === booted.udid,
);
return {...available, ...booted};
});

const device = findAvailableDevice(devices);
if (device === null) {
if (bootedAndAvailableSimulators.length === 0) {
logger.error('No active iOS device found');
return;
}

tailDeviceLogs(device.udid);
tailDeviceLogs(bootedAndAvailableSimulators[0].udid);
}

function tailDeviceLogs(udid: string) {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-platform-ios/src/commands/runIOS/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ async function runIOS(_: Array<string>, ctx: Config, args: FlagsT) {
}
}

const getSimulators = () => {
export const getSimulators = () => {
let simulators: {devices: {[index: string]: Array<Device>}};

try {
Expand Down

0 comments on commit e511375

Please sign in to comment.