-
Notifications
You must be signed in to change notification settings - Fork 5
/
findMatchingSimulator.js
112 lines (91 loc) · 3.31 KB
/
findMatchingSimulator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
/**
* Takes in a parsed simulator list and a desired name, and returns an object with the matching simulator. The desired
* name can optionally include the iOS version in between parenthesis after the device name. Ex: "iPhone 6 (9.2)" in
* which case it'll attempt to find a simulator with the exact version specified.
*
* If the simulatorString 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 simulatorString 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, simulatorString) {
if (!simulators.devices) {
return null;
}
const devices = simulators.devices;
const parsedSimulatorName = simulatorString ? simulatorString.match(/(.*)? (?:\((.*)?\))?/) : [];
if (parsedSimulatorName && parsedSimulatorName[2] !== undefined) {
var simulatorVersion = parsedSimulatorName[2];
var simulatorName = parsedSimulatorName[1];
} else {
simulatorName = simulatorString;
}
let match;
for (const versionDescriptor in devices) {
const device = devices[versionDescriptor];
let version = versionDescriptor;
if (/^com\.apple\.CoreSimulator\.SimRuntime\./g.test(version)) {
// Transform "com.apple.CoreSimulator.SimRuntime.iOS-12-2" into "iOS 12.2"
version = version.replace(/^com\.apple\.CoreSimulator\.SimRuntime\.([^-]+)-([^-]+)-([^-]+)$/g, '$1 $2.$3');
} // Making sure the version of the simulator is an iOS or tvOS (Removes Apple Watch, etc)
if (!version.startsWith('iOS') && !version.startsWith('tvOS')) {
continue;
}
if (simulatorVersion && !version.endsWith(simulatorVersion)) {
continue;
}
for (const i in device) {
const simulator = device[i]; // Skipping non-available simulator
if (simulator.availability !== '(available)' && simulator.isAvailable !== 'YES' && simulator.isAvailable !== true) {
continue;
}
const booted = simulator.state === 'Booted';
if (booted && simulatorName === null) {
return {
udid: simulator.udid,
name: simulator.name,
booted,
version
};
}
if (simulator.name === simulatorName && !match) {
match = {
udid: simulator.udid,
name: simulator.name,
booted,
version
};
} // 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,
booted,
version
};
}
}
}
if (match) {
return match;
}
return null;
}
var _default = findMatchingSimulator;
exports.default = _default;