Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
64 lines (48 loc) · 1.74 KB
/
Copy pathindex.js
File metadata and controls
64 lines (48 loc) · 1.74 KB
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
const os = require('os')
const LocalDevice = require('./lib/local')
const SimulatorDevice = require('./lib/simulator')
const AndroidDevice = require('./lib/android')
exports.LocalDevice = LocalDevice
exports.SimulatorDevice = SimulatorDevice
exports.AndroidDevice = AndroidDevice
exports.devices = async function devices() {
return [new LocalDevice(), ...(await simulators()), ...(await androids())]
}
exports.find = async function find(opts = {}) {
const { platform = os.platform(), name = null } = opts
const candidates = (await list(platform)).filter((device) => device.platform === platform)
if (candidates.length === 0) throw new Error(`No devices for platform '${platform}'`)
if (name !== null) {
const query = name.toLowerCase()
const device =
candidates.find((device) => device.name.toLowerCase() === query) ||
candidates.find((device) => device.name.toLowerCase().includes(query))
if (device === undefined) throw new Error(`No device matching '${name}'`)
return device
}
// A simulator is booted when it is first used, so one can be picked even if none is running.
const device =
candidates.find((device) => device.running) ||
candidates.find((device) => device.kind === 'simulator')
if (device === undefined) {
throw new Error(`No running device for platform '${platform}', so one must be named`)
}
return device
}
function list(platform) {
switch (platform) {
case 'ios':
return simulators()
case 'android':
return androids()
default:
return [new LocalDevice()]
}
}
function simulators() {
if (os.platform() !== 'darwin') return []
return SimulatorDevice.list().catch(() => [])
}
function androids() {
return AndroidDevice.list().catch(() => [])
}