2016-09-06 08:00:00 -07:00
|
|
|
/**
|
2018-09-11 15:27:47 -07:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2016-09-06 08:00:00 -07:00
|
|
|
*
|
2018-02-16 18:25:02 -08:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2018-05-11 12:43:49 -07:00
|
|
|
*
|
|
|
|
* @format
|
2016-09-06 08:00:00 -07:00
|
|
|
*/
|
2018-05-11 12:43:49 -07:00
|
|
|
|
2016-09-06 08:00:00 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes in a parsed simulator list and a desired name, and returns an object with the matching simulator.
|
|
|
|
*
|
|
|
|
* If the simulatorName argument is null, we'll go into default mode and return the currently booted simulator, or if
|
|
|
|
* none is booted, it will be the first in the list.
|
|
|
|
*
|
|
|
|
* @param Object simulators a parsed list from `xcrun simctl list --json devices` command
|
|
|
|
* @param String|null simulatorName the string with the name of desired simulator. If null, it will use the currently
|
|
|
|
* booted simulator, or if none are booted, the first in the list.
|
|
|
|
* @returns {Object} {udid, name, version}
|
|
|
|
*/
|
|
|
|
function findMatchingSimulator(simulators, simulatorName) {
|
|
|
|
if (!simulators.devices) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const devices = simulators.devices;
|
|
|
|
var match;
|
|
|
|
for (let version in devices) {
|
2018-01-29 13:20:48 -08:00
|
|
|
// Making sure the version of the simulator is an iOS or tvOS (Removes Apple Watch, etc)
|
|
|
|
if (!version.startsWith('iOS') && !version.startsWith('tvOS')) {
|
2016-09-06 08:00:00 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (let i in devices[version]) {
|
|
|
|
let simulator = devices[version][i];
|
|
|
|
// Skipping non-available simulator
|
check isAvailable key on simulator object (#21557)
Summary:
With the new xcode command line tools (10.1 beta2), running `xcrun simctl list --json devices` seems to gives us a slightly different format than what we were expecting. More specifically, the `availability` key is changed to `isAvailable` and returns `'YES'` if the simulator is available:
Before:
```js
devices: {
'iOS 9.2': [
{
state: 'Shutdown',
availability: '(unavailable, runtime profile not found)',
name: 'iPhone 4s',
udid: 'B9B5E161-416B-43C4-A78F-729CB96CC8C6',
},
{
state: 'Shutdown',
availability: '(unavailable, runtime profile not found)',
name: 'iPhone 5',
udid: '1CCBBF8B-5773-4EA6-BD6F-C308C87A1ADB',
},
...
]
}
```
After:
```js
devices: {
'iOS 12.1': [
{
state: 'Shutdown',
isAvailable: 'YES',
name: 'iPhone 6s',
udid: 'D0F29BE7-CC3C-4976-888D-C739B4F50508',
},
{
state: 'Shutdown',
isAvailable: 'YES',
name: 'iPhone 6',
udid: 'BA0D93BD-07E6-4182-9B0A-F60A2474139C',
},
...
]
}
```
Without this fix, `npm run ios` is not able to launch the simulator correctly and returns the following error:
```
Could not find iPhone 6 simulator
Error: Could not find iPhone 6 simulator
at resolve (/Users/antonyc/Projects/AwesomeProject/node_modules/react-native/local-cli/runIOS/runIOS.js:148:13)
at new Promise (<anonymous>)
at runOnSimulator (/Users/antonyc/Projects/AwesomeProject/node_modules/react-native/local-cli/runIOS/runIOS.js:134:10)
at Object.runIOS [as func] (/Users/antonyc/Projects/AwesomeProject/node_modules/react-native/local-cli/runIOS/runIOS.js:106:12)
at Promise.resolve.then (/Users/antonyc/Projects/AwesomeProject/node_modules/react-native/local-cli/cliEntry.js:117:22)
```
Pull Request resolved: https://github.com/facebook/react-native/pull/21557
Differential Revision: D10248115
Pulled By: TheSavior
fbshipit-source-id: 37197bbf828e4a6e254270d46411a6716a91afe3
2018-10-08 23:14:32 -07:00
|
|
|
if (
|
|
|
|
simulator.availability !== '(available)' &&
|
|
|
|
simulator.isAvailable !== 'YES'
|
|
|
|
) {
|
2016-09-06 08:00:00 -07:00
|
|
|
continue;
|
|
|
|
}
|
2018-02-27 14:03:50 -08:00
|
|
|
let booted = simulator.state === 'Booted';
|
|
|
|
if (booted && simulatorName === null) {
|
2016-09-06 08:00:00 -07:00
|
|
|
return {
|
|
|
|
udid: simulator.udid,
|
|
|
|
name: simulator.name,
|
2018-02-27 14:03:50 -08:00
|
|
|
booted,
|
2018-05-11 12:43:49 -07:00
|
|
|
version,
|
2016-09-06 08:00:00 -07:00
|
|
|
};
|
|
|
|
}
|
2016-12-01 09:50:07 -08:00
|
|
|
if (simulator.name === simulatorName && !match) {
|
|
|
|
match = {
|
2016-09-06 08:00:00 -07:00
|
|
|
udid: simulator.udid,
|
|
|
|
name: simulator.name,
|
2018-02-27 14:03:50 -08:00
|
|
|
booted,
|
2018-05-11 12:43:49 -07:00
|
|
|
version,
|
2016-09-06 08:00:00 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
// Keeps track of the first available simulator for use if we can't find one above.
|
|
|
|
if (simulatorName === null && !match) {
|
|
|
|
match = {
|
|
|
|
udid: simulator.udid,
|
|
|
|
name: simulator.name,
|
2018-02-27 14:03:50 -08:00
|
|
|
booted,
|
2018-05-11 12:43:49 -07:00
|
|
|
version,
|
2016-09-06 08:00:00 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (match) {
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = findMatchingSimulator;
|