Skip to content

Commit

Permalink
fix: code review improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonrybczak committed Mar 28, 2023
1 parent e511375 commit ac8e1ed
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 24 deletions.
5 changes: 4 additions & 1 deletion __e2e__/__snapshots__/config.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ exports[`shows up current config without unnecessary output 1`] = `
"commands": [
{
"name": "log-ios",
"description": "starts iOS device syslog tail"
"description": "starts iOS device syslog tail",
"options": [
"<<REPLACED>>"
]
},
{
"name": "run-ios",
Expand Down
6 changes: 6 additions & 0 deletions packages/cli-platform-ios/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ react-native log-ios

Starts iOS device syslog tail.

#### Options

#### `--interactive`

Explicitly select simulator to tail logs from. By default it will tail logs from the first booted and available simulator.

## License

Everything inside this repository is [MIT licensed](./LICENSE).
34 changes: 31 additions & 3 deletions packages/cli-platform-ios/src/commands/logIOS/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@ import os from 'os';
import path from 'path';
import {logger} from '@react-native-community/cli-tools';
import listIOSDevices from '../../tools/listIOSDevices';
import {getSimulators} from '../runIOS';
import getSimulators from '../../tools/getSimulators';
import {Config} from '@react-native-community/cli-types';
import prompts from 'prompts';

/**
* Starts iOS device syslog tail
*/
async function logIOS() {

type Args = {
interactive: boolean;
};

async function logIOS(_argv: Array<string>, _ctx: Config, args: Args) {
// 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)
Expand All @@ -41,7 +48,21 @@ async function logIOS() {
return;
}

tailDeviceLogs(bootedAndAvailableSimulators[0].udid);
if (args.interactive && bootedAndAvailableSimulators.length > 1) {
const {udid} = await prompts({
type: 'select',
name: 'udid',
message: 'Select iOS simulators to tail logs from',
choices: bootedAndAvailableSimulators.map((simulator) => ({
title: simulator.name,
value: simulator.udid,
})),
});

tailDeviceLogs(udid);
} else {
tailDeviceLogs(bootedAndAvailableSimulators[0].udid);
}
}

function tailDeviceLogs(udid: string) {
Expand All @@ -67,4 +88,11 @@ export default {
name: 'log-ios',
description: 'starts iOS device syslog tail',
func: logIOS,
options: [
{
name: '--interactive',
description:
'Explicitly select simulator to tail logs from. By default it will tail logs from the first booted and available simulator.',
},
],
};
21 changes: 1 addition & 20 deletions packages/cli-platform-ios/src/commands/runIOS/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {getProjectInfo} from '../../tools/getProjectInfo';
import {getConfigurationScheme} from '../../tools/getConfigurationScheme';
import {selectFromInteractiveMode} from '../../tools/selectFromInteractiveMode';
import {promptForDeviceSelection} from '../../tools/prompts';
import getSimulators from '../../tools/getSimulators';

export interface FlagsT extends BuildFlags {
simulator?: string;
Expand Down Expand Up @@ -203,26 +204,6 @@ async function runIOS(_: Array<string>, ctx: Config, args: FlagsT) {
}
}

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

try {
simulators = JSON.parse(
child_process.execFileSync(
'xcrun',
['simctl', 'list', '--json', 'devices'],
{encoding: 'utf8'},
),
);
} catch (error) {
throw new CLIError(
'Could not get the simulator list from Xcode. Please open Xcode and try running project directly from there to resolve the remaining issues.',
error as Error,
);
}
return simulators;
};

async function runOnBootedDevicesSimulators(
scheme: string,
xcodeProject: IOSProjectInfo,
Expand Down
24 changes: 24 additions & 0 deletions packages/cli-platform-ios/src/tools/getSimulators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {CLIError} from '@react-native-community/cli-tools';
import child_process from 'child_process';
import {Device} from '../types';

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

try {
simulators = JSON.parse(
child_process.execFileSync(
'xcrun',
['simctl', 'list', '--json', 'devices'],
{encoding: 'utf8'},
),
);
} catch (error) {
throw new CLIError(
'Could not get the simulator list from Xcode. Please open Xcode and try running project directly from there to resolve the remaining issues.',
);
}
return simulators;
};

export default getSimulators;

0 comments on commit ac8e1ed

Please sign in to comment.