Path: blob/main/plugins/default-browser-emulator/lib/utils/getLocalOperatingSystemMeta.ts
1030 views
import * as Os from 'os';1import {2convertMacOsVersionString,3convertVersionsToTree,4getClosestNumberMatch,5} from '../VersionUtils';6import * as darwinToMacOsVersionMap from '../../data/darwinToMacOsVersionMap.json';7import * as windowsToWindowsVersionMap from '../../data/windowsToWindowsVersionMap.json';89export default function getLocalOperatingSystemMeta(10platform = Os.platform(),11release = Os.release(),12) {13const name = extractOsName(platform);14const version = extractOsVersion(platform, release);15return { name, version };16}1718function extractOsName(platform: string) {19return platformToOsName[platform.toLowerCase()];20}2122function extractOsVersion(platform: string, release: string) {23let versionStr = '';2425if (platform === 'darwin') {26versionStr = extractMacOsVersion(release);27} else if (platform === 'win32') {28versionStr = extractWindowsVersion(release);29} // else if linux then no version3031return versionStr.split('.').slice(0, 2).join('-');32}3334function extractWindowsVersion(release: string) {35// there is no guarantee we have an exact match, so let's get the closest36const [major, minor] = release.split('.');37const majors = Object.keys(windowsVersionTree).map(x => Number(x));38const majorMatch = getClosestNumberMatch(Number(major), majors);39let versionMatch = `${majorMatch}`;4041const minors = Object.keys(windowsVersionTree[majorMatch]).map(x => Number(x));42const minorMatch = getClosestNumberMatch(Number(minor), minors);43versionMatch += `.${minorMatch}`;4445return windowsToWindowsVersionMap[versionMatch];46}4748function extractMacOsVersion(release: string) {49// there is no guarantee we have an exact match, so let's get the closest50const [major, minor, build] = release.split('.');51const majors = Object.keys(darwinVersionTree).map(x => Number(x));52const majorMatch = getClosestNumberMatch(Number(major), majors);53let versionMatch = `${majorMatch}`;5455const minors = Object.keys(darwinVersionTree[majorMatch]).map(x => Number(x));56const minorMatch = getClosestNumberMatch(Number(minor), minors);57versionMatch += `.${minorMatch}`;5859if (build) {60const builds = darwinVersionTree[majorMatch][minorMatch];61const buildMatch = getClosestNumberMatch(Number(build), builds);62versionMatch += `.${buildMatch}`;63}6465const versionString = darwinToMacOsVersionMap[versionMatch];66return convertMacOsVersionString(versionString);67}6869const platformToOsName = {70darwin: 'mac-os',71win32: 'windows',72linux: 'linux',73aix: 'linux',74freebsd: 'linux',75openbsd: 'linux',76sunos: 'linux',77};7879const darwinVersionTree = convertVersionsToTree(Object.keys(darwinToMacOsVersionMap));80const windowsVersionTree = convertVersionsToTree(Object.keys(windowsToWindowsVersionMap));818283