-
Notifications
You must be signed in to change notification settings - Fork 945
Expand file tree
/
Copy pathtryLaunchEmulator.ts
More file actions
99 lines (88 loc) · 2.79 KB
/
Copy pathtryLaunchEmulator.ts
File metadata and controls
99 lines (88 loc) · 2.79 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
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
import os from 'os';
import execa from 'execa';
import adb from './adb';
const emulatorCommand = process.env.ANDROID_HOME
? `${process.env.ANDROID_HOME}/emulator/emulator`
: 'emulator';
export const getEmulators = () => {
try {
const emulatorsOutput = execa.sync(emulatorCommand, ['-list-avds']).stdout;
return emulatorsOutput
.split(os.EOL)
.filter((name) => name !== '' && !name.includes(' '));
// The `name` is AVD ID which is expected to not contain whitespace.
// The `emulator` command, however, can occasionally return verbose
// information about crashes or similar. Hence filtering out anything
// that has basic whitespace.
} catch {
return [];
}
};
const launchEmulator = async (
emulatorName: string,
adbPath: string,
port?: number,
): Promise<boolean> => {
const manualCommand = `${emulatorCommand} @${emulatorName}`;
const cp = execa(
emulatorCommand,
port ? [`@${emulatorName}`, '-port', `${port}`] : [`@${emulatorName}`],
{
detached: true,
stdio: 'ignore',
},
);
cp.unref();
const timeout = 30;
return new Promise<boolean>((resolve, reject) => {
const bootCheckInterval = setInterval(async () => {
const devices = adb.getDevices(adbPath);
const connected = port
? devices.find((d) => d.includes(`${port}`))
: devices.length > 0;
if (connected) {
cleanup();
resolve(true);
}
}, 1000);
// Reject command after timeout
const rejectTimeout = setTimeout(() => {
stopWaitingAndReject(
`It took too long to start and connect with Android emulator: ${emulatorName}. You can try starting the emulator manually from the terminal with: ${manualCommand}`,
);
}, timeout * 1000);
const cleanup = () => {
clearTimeout(rejectTimeout);
clearInterval(bootCheckInterval);
};
const stopWaitingAndReject = (message: string) => {
cleanup();
reject(new Error(message));
};
cp.on('error', ({message}) => stopWaitingAndReject(message));
cp.on('exit', () => {
stopWaitingAndReject(
`The emulator (${emulatorName}) quit before it finished opening. You can try starting the emulator manually from the terminal with: ${manualCommand}`,
);
});
});
};
export default async function tryLaunchEmulator(
adbPath: string,
emulatorName?: string,
port?: number,
): Promise<{success: boolean; error?: string}> {
const emulators = getEmulators();
if (emulators.length > 0) {
try {
await launchEmulator(emulatorName ?? emulators[0], adbPath, port);
return {success: true};
} catch (error) {
return {success: false, error: (error as any)?.message};
}
}
return {
success: false,
error: 'No emulators found as an output of `emulator -list-avds`',
};
}