'use strict' ; import { FabricEnvironmentRegistryEntry , EnvironmentType , FabricEnvironment , FileSystemUtil , FileConfigurations , MicrofabEnvironment } from 'ibm-blockchain-platform-common' ; import * as path from 'path' ; import { SettingConfigurations } from '../../configurations' ; import { LocalMicroEnvironment } from './LocalMicroEnvironment' ; import { LocalMicroEnvironmentManager } from './LocalMicroEnvironmentManager' ; import { LocalEnvironment } from './LocalEnvironment' ; export class EnvironmentFactory { public static getEnvironment ( environmentRegistryEntry ) { const name = environmentRegistryEntry . name ; if ( ! name ) { throw new Error ( 'Unable to get environment, a name must be provided' ) ; } let managedRuntime = environmentRegistryEntry . managedRuntime ; if ( managedRuntime === undefined ) { managedRuntime = false ; } const type = environmentRegistryEntry . environmentType ; const v2Path = path . join ( 'v2' , 'environments' ) ; const isV1LocalAnsible = type === EnvironmentType . MANAGED && environmentRegistryEntry . environmentDirectory && environmentRegistryEntry . environmentDirectory . indexOf ( v2Path ) === - 1 ; if ( managedRuntime && ( type === EnvironmentType . LOCAL_ENVIRONMENT || isV1LocalAnsible ) ) { const extDir = isV1LocalAnsible ? SettingConfigurations . getExtensionDir ( ) . replace ( `${ path . sep }v2` , '' ) : SettingConfigurations . getExtensionDir ( ) ; const resolvedExtDir = FileSystemUtil . getDirPath ( extDir ) ; const envPath = path . join ( resolvedExtDir , FileConfigurations . FABRIC_ENVIRONMENTS , name ) ; const runtime = new LocalEnvironment ( name , envPath ) ; return runtime ; } else if ( type === EnvironmentType . LOCAL_MICROFAB_ENVIRONMENT ) { const runtime = LocalMicroEnvironmentManager . instance ( ) . getRuntime ( name ) ; return runtime ; } else if ( type === EnvironmentType . MICROFAB_ENVIRONMENT ) { return new MicrofabEnvironment ( name , environmentRegistryEntry . environmentDirectory , environmentRegistryEntry . url ) ; } else { const extDir = SettingConfigurations . getExtensionDir ( ) ; const resolvedExtDir = FileSystemUtil . getDirPath ( extDir ) ; const envDir = path . join ( resolvedExtDir , FileConfigurations . FABRIC_ENVIRONMENTS , name ) ; return new FabricEnvironment ( name , envDir ) ; } } }