Skip to content

Commit

Permalink
feat: refactor run-ios for readability and better error messages (r…
Browse files Browse the repository at this point in the history
…eact-native-community#328)

Summary:
---------

Refactor `run-ios` for readability and better error messages:

<img width="737" alt="Screenshot 2019-04-17 at 22 04 38" src="https://user-images.githubusercontent.com/5106466/56317371-e0540900-615c-11e9-82e2-e8ef4e706721.png">
<img width="535" alt="Screenshot 2019-04-17 at 22 05 04" src="https://user-images.githubusercontent.com/5106466/56317372-e0540900-615c-11e9-9c82-774889ee2b05.png">
<img width="621" alt="Screenshot 2019-04-17 at 22 08 00" src="https://user-images.githubusercontent.com/5106466/56317551-4e003500-615d-11e9-92dc-c06f0e092e90.png">

Followup to react-native-community#310 cc @lucasbento 

Test Plan:
----------

Verified that following commands work properly:
- `run-ios`
- `run-ios --device`
- `run-ios --device phone-name`
- `run-ios --uuid asd123`
  • Loading branch information
thymikee authored and dratwas committed Jul 12, 2019
1 parent 000caff commit c6298da
Showing 1 changed file with 43 additions and 48 deletions.
91 changes: 43 additions & 48 deletions packages/platform-ios/src/commands/runIOS/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
import child_process from 'child_process';
import fs from 'fs';
import path from 'path';

import chalk from 'chalk';
import type {ConfigT} from 'types';

import findXcodeProject from './findXcodeProject';
import parseIOSDevicesList from './parseIOSDevicesList';
import findMatchingSimulator from './findMatchingSimulator';
Expand Down Expand Up @@ -64,40 +63,44 @@ function runIOS(_: Array<string>, ctx: ConfigT, args: FlagsT) {
}`,
);

const {device, udid} = args;

if (!device && !udid) {
return runOnSimulator(xcodeProject, scheme, args);
}

const devices = parseIOSDevicesList(
// $FlowExpectedError https://github.com/facebook/flow/issues/5675
child_process.execFileSync('xcrun', ['instruments', '-s'], {
encoding: 'utf8',
}),
);

const device = ((args.device: any): string);
const udid = ((args.udid: any): string);
if (device || udid) {
const selectedDevice = device
? matchingDevice(devices, device)
: matchingDeviceByUdid(devices, udid);

if (selectedDevice) {
return runOnDevice(selectedDevice, scheme, xcodeProject, args);
}
if (devices.length === 0) {
return logger.error('No iOS devices connected.');
}

if (devices && devices.length > 0) {
const message = device
? `Could not find device with the name: "${device}". Choose one of the following:\n${printFoundDevices(
devices,
)}`
: `Could not find device with the udid: "${udid}". Choose one of the following:\n${printFoundDevices(
devices,
)}`;
const selectedDevice = matchingDevice(devices, device, udid);

return logger.error(message);
}
if (selectedDevice) {
return runOnDevice(selectedDevice, scheme, xcodeProject, args);
}

return logger.error('No iOS devices connected.');
if (device) {
return logger.error(
`Could not find a device named: "${chalk.bold(
device,
)}". ${printFoundDevices(devices)}`,
);
}

return runOnSimulator(xcodeProject, scheme, args);
if (udid) {
return logger.error(
`Could not find a device with udid: "${chalk.bold(
udid,
)}". ${printFoundDevices(devices)}`,
);
}
}

async function runOnSimulator(xcodeProject, scheme, args: FlagsT) {
Expand Down Expand Up @@ -331,45 +334,37 @@ function xcprettyAvailable() {
return true;
}

function matchingDevice(devices, deviceName) {
function matchingDevice(devices, deviceName, udid) {
if (udid) {
return matchingDeviceByUdid(devices, udid);
}
if (deviceName === true && devices.length === 1) {
logger.info(
`Using first available device ${
devices[0].name
} due to lack of name supplied.`,
`Using first available device named "${chalk.bold(
devices[0].name,
)}" due to lack of name supplied.`,
);
return devices[0];
}
for (let i = devices.length - 1; i >= 0; i--) {
if (
devices[i].name === deviceName ||
formattedDeviceName(devices[i]) === deviceName
) {
return devices[i];
}
}
return null;
return devices.find(
device =>
device.name === deviceName || formattedDeviceName(device) === deviceName,
);
}

function matchingDeviceByUdid(devices, udid) {
for (let i = devices.length - 1; i >= 0; i--) {
if (devices[i].udid === udid) {
return devices[i];
}
}
return null;
return devices.find(device => device.udid === udid);
}

function formattedDeviceName(simulator) {
return `${simulator.name} (${simulator.version})`;
}

function printFoundDevices(devices) {
let output = '';
for (let i = devices.length - 1; i >= 0; i--) {
output += `${devices[i].name} Udid: ${devices[i].udid}\n`;
}
return output;
return [
'Available devices:',
...devices.map(device => ` - ${device.name} (${device.udid})`),
].join('\n');
}

function getProcessOptions({packager, terminal, port}) {
Expand Down

0 comments on commit c6298da

Please sign in to comment.