-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·109 lines (89 loc) · 2.46 KB
/
index.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
#!/usr/bin/env node
import { execSync } from "node:child_process";
import { select } from "@inquirer/prompts";
const runCommand = (command) => {
try {
return execSync(command, { encoding: "utf-8" }).trim();
} catch (error) {
throw new Error(`Error executing command: ${command}`);
}
};
const getIOSRuntimes = () => {
const outputJson = runCommand("xcrun simctl list runtimes -j");
const output = JSON.parse(outputJson);
return output.runtimes;
};
const getIOSDevices = (runtime) => {
const outputJson = runCommand("xcrun simctl list devices -j");
const output = JSON.parse(outputJson);
if (!(runtime in output.devices)) {
throw new Error("Error not found runtime in output");
}
return output.devices[runtime];
};
const execIOSLaunch = async () => {
const runtimes = getIOSRuntimes();
const selectedRuntime = await select({
message: "Select a runtime",
loop: true,
choices: runtimes.map((v) => ({
name: v.name,
value: v.identifier,
})),
});
const devices = getIOSDevices(selectedRuntime);
const selectedDeviceId = await select({
message: "Select a device",
loop: true,
choices: devices.map((v) => ({
name: v.name,
value: v.udid,
})),
});
console.log(`Booting device: ${selectedDeviceId} with ${selectedRuntime}...`);
runCommand(`xcrun simctl boot ${selectedDeviceId}`);
console.log("Opening Simulator app...");
runCommand("open -a Simulator");
};
const getAndroidVirtualDevices = () => {
const output = runCommand("emulator -list-avds");
return output.split("\n").filter((v) => v);
};
const execAndroidLaunch = async () => {
const devices = getAndroidVirtualDevices();
const selectedAvd = await select({
message: "Select a device",
loop: true,
choices: devices.map((v) => ({
value: v,
})),
});
console.log(`Booting device: ${selectedAvd} ...`);
runCommand(`emulator -avd ${selectedAvd}`);
};
const main = async () => {
const osNames = ["iOS", "Android"];
try {
const selectedOSName = await select({
message: "Select a OS",
loop: true,
choices: osNames.map((v) => ({
value: v,
})),
});
switch (selectedOSName) {
case "iOS":
await execIOSLaunch();
break;
case "Android":
await execAndroidLaunch();
break;
default:
throw new Error("Invalid OS name");
}
} catch (error) {
console.error(error.message);
process.exit(1);
}
};
main();