forked from microsoft/vscode-cpptools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinuxDistribution.ts
More file actions
60 lines (53 loc) · 2.56 KB
/
linuxDistribution.ts
File metadata and controls
60 lines (53 loc) · 2.56 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
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved.
* See 'LICENSE' in the project root for license information.
* ------------------------------------------------------------------------------------------ */
import * as fs from 'fs';
import * as os from 'os';
export class LinuxDistribution {
constructor(public name: string, public version: string) { }
/**
* There is no standard way on Linux to find the distribution name and version.
* Recently, systemd has pushed to standardize the os-release file. This has
* seen adoption in "recent" versions of all major distributions.
* https://www.freedesktop.org/software/systemd/man/os-release.html
*/
public static GetDistroInformation(): Promise<LinuxDistribution> {
// First check /etc/os-release and only fallback to /usr/lib/os-release
// as per the os-release documentation.
const linuxDistro: Promise<LinuxDistribution> = LinuxDistribution.getDistroInformationFromFile('/etc/os-release')
.catch(() => LinuxDistribution.getDistroInformationFromFile('/usr/lib/os-release'))
.catch(() => Promise.resolve(new LinuxDistribution('unknown', 'unknown'))); // couldn't get distro information;
return linuxDistro;
}
private static getDistroInformationFromFile(path: string): Promise<LinuxDistribution> {
return new Promise<LinuxDistribution>((resolve, reject) => {
fs.readFile(path, 'utf8', (error, data) => {
if (error) {
reject(error);
return;
}
resolve(LinuxDistribution.getDistroInformation(data));
});
});
}
// Only public for tests.
public static getDistroInformation(data: string): LinuxDistribution {
const idKey: string = 'ID';
const versionKey: string = 'VERSION_ID';
let distroName: string = 'unknown';
let distroVersion: string = 'unknown';
const keyValues: string[] = data.split(os.EOL);
for (let i: number = 0; i < keyValues.length; i++) {
const keyValue: string[] = keyValues[i].split('=');
if (keyValue.length === 2) {
if (keyValue[0] === idKey) {
distroName = keyValue[1];
} else if (keyValue[0] === versionKey) {
distroVersion = keyValue[1];
}
}
}
return new LinuxDistribution(distroName, distroVersion);
}
}