|
11 | 11 | import child_process from 'child_process'; |
12 | 12 | import fs from 'fs'; |
13 | 13 | import path from 'path'; |
14 | | - |
| 14 | +import chalk from 'chalk'; |
15 | 15 | import type {ConfigT} from 'types'; |
16 | | - |
17 | 16 | import findXcodeProject from './findXcodeProject'; |
18 | 17 | import parseIOSDevicesList from './parseIOSDevicesList'; |
19 | 18 | import findMatchingSimulator from './findMatchingSimulator'; |
@@ -64,40 +63,44 @@ function runIOS(_: Array<string>, ctx: ConfigT, args: FlagsT) { |
64 | 63 | }`, |
65 | 64 | ); |
66 | 65 |
|
| 66 | + const {device, udid} = args; |
| 67 | + |
| 68 | + if (!device && !udid) { |
| 69 | + return runOnSimulator(xcodeProject, scheme, args); |
| 70 | + } |
| 71 | + |
67 | 72 | const devices = parseIOSDevicesList( |
68 | 73 | // $FlowExpectedError https://github.com/facebook/flow/issues/5675 |
69 | 74 | child_process.execFileSync('xcrun', ['instruments', '-s'], { |
70 | 75 | encoding: 'utf8', |
71 | 76 | }), |
72 | 77 | ); |
73 | 78 |
|
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 | + } |
84 | 82 |
|
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); |
93 | 84 |
|
94 | | - return logger.error(message); |
95 | | - } |
| 85 | + if (selectedDevice) { |
| 86 | + return runOnDevice(selectedDevice, scheme, xcodeProject, args); |
| 87 | + } |
96 | 88 |
|
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 | + ); |
98 | 95 | } |
99 | 96 |
|
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 | + } |
101 | 104 | } |
102 | 105 |
|
103 | 106 | async function runOnSimulator(xcodeProject, scheme, args: FlagsT) { |
@@ -331,45 +334,37 @@ function xcprettyAvailable() { |
331 | 334 | return true; |
332 | 335 | } |
333 | 336 |
|
334 | | -function matchingDevice(devices, deviceName) { |
| 337 | +function matchingDevice(devices, deviceName, udid) { |
| 338 | + if (udid) { |
| 339 | + return matchingDeviceByUdid(devices, udid); |
| 340 | + } |
335 | 341 | if (deviceName === true && devices.length === 1) { |
336 | 342 | 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.`, |
340 | 346 | ); |
341 | 347 | return devices[0]; |
342 | 348 | } |
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 | + ); |
352 | 353 | } |
353 | 354 |
|
354 | 355 | 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); |
361 | 357 | } |
362 | 358 |
|
363 | 359 | function formattedDeviceName(simulator) { |
364 | 360 | return `${simulator.name} (${simulator.version})`; |
365 | 361 | } |
366 | 362 |
|
367 | 363 | 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'); |
373 | 368 | } |
374 | 369 |
|
375 | 370 | function getProcessOptions({packager, terminal, port}) { |
|
0 commit comments