Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/plugins/default-browser-emulator/lib/utils/getLocalOperatingSystemMeta.ts
1030 views
1
import * as Os from 'os';
2
import {
3
convertMacOsVersionString,
4
convertVersionsToTree,
5
getClosestNumberMatch,
6
} from '../VersionUtils';
7
import * as darwinToMacOsVersionMap from '../../data/darwinToMacOsVersionMap.json';
8
import * as windowsToWindowsVersionMap from '../../data/windowsToWindowsVersionMap.json';
9
10
export default function getLocalOperatingSystemMeta(
11
platform = Os.platform(),
12
release = Os.release(),
13
) {
14
const name = extractOsName(platform);
15
const version = extractOsVersion(platform, release);
16
return { name, version };
17
}
18
19
function extractOsName(platform: string) {
20
return platformToOsName[platform.toLowerCase()];
21
}
22
23
function extractOsVersion(platform: string, release: string) {
24
let versionStr = '';
25
26
if (platform === 'darwin') {
27
versionStr = extractMacOsVersion(release);
28
} else if (platform === 'win32') {
29
versionStr = extractWindowsVersion(release);
30
} // else if linux then no version
31
32
return versionStr.split('.').slice(0, 2).join('-');
33
}
34
35
function extractWindowsVersion(release: string) {
36
// there is no guarantee we have an exact match, so let's get the closest
37
const [major, minor] = release.split('.');
38
const majors = Object.keys(windowsVersionTree).map(x => Number(x));
39
const majorMatch = getClosestNumberMatch(Number(major), majors);
40
let versionMatch = `${majorMatch}`;
41
42
const minors = Object.keys(windowsVersionTree[majorMatch]).map(x => Number(x));
43
const minorMatch = getClosestNumberMatch(Number(minor), minors);
44
versionMatch += `.${minorMatch}`;
45
46
return windowsToWindowsVersionMap[versionMatch];
47
}
48
49
function extractMacOsVersion(release: string) {
50
// there is no guarantee we have an exact match, so let's get the closest
51
const [major, minor, build] = release.split('.');
52
const majors = Object.keys(darwinVersionTree).map(x => Number(x));
53
const majorMatch = getClosestNumberMatch(Number(major), majors);
54
let versionMatch = `${majorMatch}`;
55
56
const minors = Object.keys(darwinVersionTree[majorMatch]).map(x => Number(x));
57
const minorMatch = getClosestNumberMatch(Number(minor), minors);
58
versionMatch += `.${minorMatch}`;
59
60
if (build) {
61
const builds = darwinVersionTree[majorMatch][minorMatch];
62
const buildMatch = getClosestNumberMatch(Number(build), builds);
63
versionMatch += `.${buildMatch}`;
64
}
65
66
const versionString = darwinToMacOsVersionMap[versionMatch];
67
return convertMacOsVersionString(versionString);
68
}
69
70
const platformToOsName = {
71
darwin: 'mac-os',
72
win32: 'windows',
73
linux: 'linux',
74
aix: 'linux',
75
freebsd: 'linux',
76
openbsd: 'linux',
77
sunos: 'linux',
78
};
79
80
const darwinVersionTree = convertVersionsToTree(Object.keys(darwinToMacOsVersionMap));
81
const windowsVersionTree = convertVersionsToTree(Object.keys(windowsToWindowsVersionMap));
82
83