import * as fs from 'fs-extra' ; import * as path from 'path' ; import { FabricGatewayRegistryEntry } from './FabricGatewayRegistryEntry' ; import { FileConfigurations } from '../registries/FileConfigurations' ; import { FileRegistry } from '../registries/FileRegistry' ; import { FabricEnvironmentRegistryEntry , EnvironmentType , EnvironmentFlags } from '../registries/FabricEnvironmentRegistryEntry' ; import { FabricEnvironmentRegistry } from '../registries/FabricEnvironmentRegistry' ; import { MicrofabEnvironment } from '../environments/MicrofabEnvironment' ; export class FabricGatewayRegistry extends FileRegistry < FabricGatewayRegistryEntry > { public static instance ( ) { return FabricGatewayRegistry . _instance ; } private static _instance : FabricGatewayRegistry = new FabricGatewayRegistry ( ) ; private constructor ( ) { super ( FileConfigurations . FABRIC_GATEWAYS ) ; } public async getAll ( showLocalFabric = true ) { const entries = await super . getAll ( ) ; let gateways = [ ] ; const localGateways = [ ] ; for ( const entry of entries ) { const envName = entry . fromEnvironment ; let environmentType ; if ( envName ) { const environment = await FabricEnvironmentRegistry . instance ( ) . get ( envName ) ; environmentType = environment . environmentType ; } if ( environmentType === EnvironmentType . LOCAL_MICROFAB_ENVIRONMENT ) { localGateways . push ( entry ) ; continue ; } gateways . push ( entry ) ; } if ( showLocalFabric && localGateways . length > 0 ) { gateways = [ ... localGateways , ... gateways ] ; } return gateways ; } public async getEntries ( ) { const normalEntries = await super . getEntries ( ) ; const otherEntries = [ ] ; const microfabEnvironmentEntries = await FabricEnvironmentRegistry . instance ( ) . getAll ( [ EnvironmentFlags . MICROFAB ] ) ; for ( const microfabEnvironmentEntry of microfabEnvironmentEntries ) { const environment = this . newMicrofabEnvironment ( microfabEnvironmentEntry . name , microfabEnvironmentEntry . environmentDirectory , microfabEnvironmentEntry . url ) ; const isAlive = await environment . isAlive ( ) ; if ( ! isAlive ) { continue ; } const gatewayEntries = await environment . getGateways ( ) ; otherEntries . push ( ... gatewayEntries ) ; } return [ ... normalEntries , ... otherEntries ] . sort ( ( a , b ) : number => { return a . name . localeCompare ( b . name ) ; } ) ; } public async update ( newEntry ) { if ( newEntry . fromEnvironment ) { const connectionProfilePath = newEntry . connectionProfilePath ; const gatewayFolderPath = connectionProfilePath . substr ( 0 , connectionProfilePath . lastIndexOf ( '/' ) ) ; await fs . writeJson ( path . join ( gatewayFolderPath , '.config.json' ) , newEntry ) ; this . emit ( FileRegistry . EVENT_NAME , FileConfigurations . FABRIC_GATEWAYS ) ; } else { await super . update ( newEntry ) ; } } public newMicrofabEnvironment ( name , directory , url ) { return new MicrofabEnvironment ( name , directory , url ) ; } }