Skip to content

Commit 7d9f8cd

Browse files
authored
feat: refactor run-ios for readability and better error messages (#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 #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`
1 parent d4c7104 commit 7d9f8cd

File tree

1 file changed

+43
-48
lines changed
  • packages/platform-ios/src/commands/runIOS

1 file changed

+43
-48
lines changed

packages/platform-ios/src/commands/runIOS/index.js

Lines changed: 43 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
import child_process from 'child_process';
1212
import fs from 'fs';
1313
import path from 'path';
14-
14+
import chalk from 'chalk';
1515
import type {ConfigT} from 'types';
16-
1716
import findXcodeProject from './findXcodeProject';
1817
import parseIOSDevicesList from './parseIOSDevicesList';
1918
import findMatchingSimulator from './findMatchingSimulator';
@@ -64,40 +63,44 @@ function runIOS(_: Array<string>, ctx: ConfigT, args: FlagsT) {
6463
}`,
6564
);
6665

66+
const {device, udid} = args;
67+
68+
if (!device && !udid) {
69+
return runOnSimulator(xcodeProject, scheme, args);
70+
}
71+
6772
const devices = parseIOSDevicesList(
6873
// $FlowExpectedError https://github.com/facebook/flow/issues/5675
6974
child_process.execFileSync('xcrun', ['instruments', '-s'], {
7075
encoding: 'utf8',
7176
}),
7277
);
7378

74-
const device = ((args.device: any): string);
75-
const udid = ((args.udid: any): string);
76-
if (device || udid) {
77-
const selectedDevice = device
78-
? matchingDevice(devices, device)
79-
: matchingDeviceByUdid(devices, udid);
80-
81-
if (selectedDevice) {
82-
return runOnDevice(selectedDevice, scheme, xcodeProject, args);
83-
}
79+
if (devices.length === 0) {
80+
return logger.error('No iOS devices connected.');
81+
}
8482

85-
if (devices && devices.length > 0) {
86-
const message = device
87-
? `Could not find device with the name: "${device}". Choose one of the following:\n${printFoundDevices(
88-
devices,
89-
)}`
90-
: `Could not find device with the udid: "${udid}". Choose one of the following:\n${printFoundDevices(
91-
devices,
92-
)}`;
83+
const selectedDevice = matchingDevice(devices, device, udid);
9384

94-
return logger.error(message);
95-
}
85+
if (selectedDevice) {
86+
return runOnDevice(selectedDevice, scheme, xcodeProject, args);
87+
}
9688

97-
return logger.error('No iOS devices connected.');
89+
if (device) {
90+
return logger.error(
91+
`Could not find a device named: "${chalk.bold(
92+
device,
93+
)}". ${printFoundDevices(devices)}`,
94+
);
9895
}
9996

100-
return runOnSimulator(xcodeProject, scheme, args);
97+
if (udid) {
98+
return logger.error(
99+
`Could not find a device with udid: "${chalk.bold(
100+
udid,
101+
)}". ${printFoundDevices(devices)}`,
102+
);
103+
}
101104
}
102105

103106
async function runOnSimulator(xcodeProject, scheme, args: FlagsT) {
@@ -331,45 +334,37 @@ function xcprettyAvailable() {
331334
return true;
332335
}
333336

334-
function matchingDevice(devices, deviceName) {
337+
function matchingDevice(devices, deviceName, udid) {
338+
if (udid) {
339+
return matchingDeviceByUdid(devices, udid);
340+
}
335341
if (deviceName === true && devices.length === 1) {
336342
logger.info(
337-
`Using first available device ${
338-
devices[0].name
339-
} due to lack of name supplied.`,
343+
`Using first available device named "${chalk.bold(
344+
devices[0].name,
345+
)}" due to lack of name supplied.`,
340346
);
341347
return devices[0];
342348
}
343-
for (let i = devices.length - 1; i >= 0; i--) {
344-
if (
345-
devices[i].name === deviceName ||
346-
formattedDeviceName(devices[i]) === deviceName
347-
) {
348-
return devices[i];
349-
}
350-
}
351-
return null;
349+
return devices.find(
350+
device =>
351+
device.name === deviceName || formattedDeviceName(device) === deviceName,
352+
);
352353
}
353354

354355
function matchingDeviceByUdid(devices, udid) {
355-
for (let i = devices.length - 1; i >= 0; i--) {
356-
if (devices[i].udid === udid) {
357-
return devices[i];
358-
}
359-
}
360-
return null;
356+
return devices.find(device => device.udid === udid);
361357
}
362358

363359
function formattedDeviceName(simulator) {
364360
return `${simulator.name} (${simulator.version})`;
365361
}
366362

367363
function printFoundDevices(devices) {
368-
let output = '';
369-
for (let i = devices.length - 1; i >= 0; i--) {
370-
output += `${devices[i].name} Udid: ${devices[i].udid}\n`;
371-
}
372-
return output;
364+
return [
365+
'Available devices:',
366+
...devices.map(device => ` - ${device.name} (${device.udid})`),
367+
].join('\n');
373368
}
374369

375370
function getProcessOptions({packager, terminal, port}) {

0 commit comments

Comments
 (0)