import { OperatingSystem } from '@/domain/OperatingSystem' ; import { BrowserOsDetector } from './BrowserOs/BrowserOsDetector' ; import { IBrowserOsDetector } from './BrowserOs/IBrowserOsDetector' ; import { IEnvironment } from './IEnvironment' ; export interface IEnvironmentVariables { readonly window : Window & typeof globalThis ; readonly process : NodeJS . Process ; readonly navigator : Navigator ; } export class Environment implements IEnvironment { public static readonly CurrentEnvironment : IEnvironment = new Environment ( { window , process : typeof process !== 'undefined' ? process : undefined , navigator , } ) ; public readonly isDesktop : boolean ; public readonly os : OperatingSystem ; protected constructor ( variables , browserOsDetector = new BrowserOsDetector ( ) ) { if ( ! variables ) { throw new Error ( 'variables is null or empty' ) ; } this . isDesktop = isDesktop ( variables ) ; if ( this . isDesktop ) { this . os = getDesktopOsType ( getProcessPlatform ( variables ) ) ; } else { const userAgent = getUserAgent ( variables ) ; this . os = ! userAgent ? undefined : browserOsDetector . detect ( userAgent ) ; } } } function getUserAgent ( variables ) { if ( ! variables . window || ! variables . window . navigator ) { return undefined ; } return variables . window . navigator . userAgent ; } function getProcessPlatform ( variables ) { if ( ! variables . process || ! variables . process . platform ) { return undefined ; } return variables . process . platform ; } function getDesktopOsType ( processPlatform ) { switch ( processPlatform ) { case 'darwin' : return OperatingSystem . macOS ; case 'win32' : return OperatingSystem . Windows ; case 'linux' : return OperatingSystem . Linux ; default : return undefined ; } } function isDesktop ( variables ) { if ( variables . window && variables . window . process && variables . window . process . type === 'renderer' ) { return true ; } if ( variables . process && variables . process . versions && Boolean ( variables . process . versions . electron ) ) { return true ; } if ( variables . navigator && variables . navigator . userAgent && variables . navigator . userAgent . includes ( 'Electron' ) ) { return true ; } return false ; }