Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/node/osReleaseInfo.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { constants as FSConstants, promises as FSPromises } from 'fs';
7
import { createInterface as readLines } from 'readline';
8
import * as Platform from '../common/platform.js';
9
10
type ReleaseInfo = {
11
id: string;
12
id_like?: string;
13
version_id?: string;
14
};
15
16
export async function getOSReleaseInfo(errorLogger: (error: any) => void): Promise<ReleaseInfo | undefined> {
17
if (Platform.isMacintosh || Platform.isWindows) {
18
return;
19
}
20
21
// Extract release information on linux based systems
22
// using the identifiers specified in
23
// https://www.freedesktop.org/software/systemd/man/os-release.html
24
let handle: FSPromises.FileHandle | undefined;
25
for (const filePath of ['/etc/os-release', '/usr/lib/os-release', '/etc/lsb-release']) {
26
try {
27
handle = await FSPromises.open(filePath, FSConstants.R_OK);
28
break;
29
} catch (err) { }
30
}
31
32
if (!handle) {
33
errorLogger('Unable to retrieve release information from known identifier paths.');
34
return;
35
}
36
37
try {
38
const osReleaseKeys = new Set([
39
'ID',
40
'DISTRIB_ID',
41
'ID_LIKE',
42
'VERSION_ID',
43
'DISTRIB_RELEASE',
44
]);
45
const releaseInfo: ReleaseInfo = {
46
id: 'unknown'
47
};
48
49
for await (const line of readLines({ input: handle.createReadStream(), crlfDelay: Infinity })) {
50
if (!line.includes('=')) {
51
continue;
52
}
53
const key = line.split('=')[0].toUpperCase().trim();
54
if (osReleaseKeys.has(key)) {
55
const value = line.split('=')[1].replace(/"/g, '').toLowerCase().trim();
56
if (key === 'ID' || key === 'DISTRIB_ID') {
57
releaseInfo.id = value;
58
} else if (key === 'ID_LIKE') {
59
releaseInfo.id_like = value;
60
} else if (key === 'VERSION_ID' || key === 'DISTRIB_RELEASE') {
61
releaseInfo.version_id = value;
62
}
63
}
64
}
65
66
return releaseInfo;
67
} catch (err) {
68
errorLogger(err);
69
}
70
71
return;
72
}
73
74