import { IApplicationContext } from '@/application/Context/IApplicationContext' ; import { OperatingSystem } from '@/domain/OperatingSystem' ; import { IApplication } from '@/domain/IApplication' ; import { Environment } from '../Environment/Environment' ; import { IEnvironment } from '../Environment/IEnvironment' ; import { IApplicationFactory } from '../IApplicationFactory' ; import { ApplicationFactory } from '../ApplicationFactory' ; import { ApplicationContext } from './ApplicationContext' ; export async function buildContext ( factory = ApplicationFactory . Current , environment = Environment . CurrentEnvironment ) { if ( ! factory ) { throw new Error ( 'undefined factory' ) ; } if ( ! environment ) { throw new Error ( 'undefined environment' ) ; } const app = await factory . getApp ( ) ; const os = getInitialOs ( app , environment ) ; return new ApplicationContext ( app , os ) ; } function getInitialOs ( app , environment ) { const currentOs = environment . os ; const supportedOsList = app . getSupportedOsList ( ) ; if ( supportedOsList . includes ( currentOs ) ) { return currentOs ; } supportedOsList . sort ( ( os1 , os2 ) => { const getPriority = ( os ) => app . getCollection ( os ) . totalScripts ; return getPriority ( os2 ) - getPriority ( os1 ) ; } ) ; return supportedOsList [ 0 ] ; }