-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathos.ts
86 lines (77 loc) · 1.9 KB
/
os.ts
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
import getos from "getos";
import getArch from "./arch";
export enum OS {
MacOS,
Ubuntu,
Windows,
}
export namespace OS {
export function all(): OS[] {
return [OS.MacOS, OS.Ubuntu, OS.Windows];
}
}
const AVAILABLE_OS: { [platform: string]: string[] } = {
macOS: ["latest", "14", "13", "12", "11"],
Ubuntu: ["latest", "24.04", "22.04", "20.04"],
Windows: ["latest", "10"],
};
export interface System {
os: OS;
version: string;
name: string;
arch: string;
}
export async function getSystem(): Promise<System> {
let detectedSystem = await new Promise<getos.Os>((resolve, reject) => {
getos((error, os) => {
os ? resolve(os) : reject(error || "No OS detected");
});
});
const arch = getArch();
let system: System;
switch (detectedSystem.os) {
case "darwin":
system = {
os: OS.MacOS,
version: "latest",
name: "macOS",
arch,
};
break;
case "linux":
if (detectedSystem.dist !== "Ubuntu") {
throw new Error(
`"${detectedSystem.dist}" is not a supported Linux distribution`
);
}
if (arch !== "x64" && arch !== "arm64") {
throw new Error(`${arch} is not a supported architecture for Linux`);
}
system = {
os: OS.Ubuntu,
version: detectedSystem.release,
name: "Ubuntu",
arch,
};
break;
case "win32":
if (arch !== "x64") {
throw new Error(`${arch} is not a supported architecture for Windows`);
}
system = {
os: OS.Windows,
version: "latest",
name: "Windows",
arch,
};
break;
default:
throw new Error(`"${detectedSystem.os}" is not a supported platform`);
}
if (!AVAILABLE_OS[system.name].includes(system.version)) {
throw new Error(
`Version "${system.version}" of ${system.name} is not supported`
);
}
return system;
}