'use strict' ; import * as vscode from 'vscode' ; import { PackageRegistryEntry } from '../registries/PackageRegistryEntry' ; import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter' ; import { ExtensionCommands } from '../../ExtensionCommands' ; import { VSCodeBlockchainDockerOutputAdapter } from '../logging/VSCodeBlockchainDockerOutputAdapter' ; import { FabricEnvironmentRegistryEntry , IFabricEnvironmentConnection , LogType , EnvironmentType } from 'ibm-blockchain-platform-common' ; import { Reporter } from '../util/Reporter' ; import { FabricEnvironmentManager } from '../fabric/environments/FabricEnvironmentManager' ; import { SettingConfigurations } from '../configurations' ; export async function installSmartContract ( orgMap ? , chosenPackage ? ) { const outputAdapter = VSCodeBlockchainOutputAdapter . instance ( ) ; outputAdapter . log ( LogType . INFO , undefined , 'installSmartContract' ) ; let packageId ; let connection = FabricEnvironmentManager . instance ( ) . getConnection ( ) ; if ( ! connection ) { await vscode . commands . executeCommand ( ExtensionCommands . CONNECT_TO_ENVIRONMENT ) ; connection = FabricEnvironmentManager . instance ( ) . getConnection ( ) ; if ( ! connection ) { return [ ] ; } } const enum InstallError { NONE = 'none' , TIMEOUT = 'timeout' , OTHER = 'other' } let successfulInstall = InstallError . NONE ; let peerCount = 0 ; await vscode . window . withProgress ( { location : vscode . ProgressLocation . Notification , title : 'IBM Blockchain Platform Extension' , cancellable : false } , async ( progress ) => { const environmentRegistryEntry = FabricEnvironmentManager . instance ( ) . getEnvironmentRegistryEntry ( ) ; if ( environmentRegistryEntry . environmentType === EnvironmentType . LOCAL_MICROFAB_ENVIRONMENT ) { VSCodeBlockchainDockerOutputAdapter . instance ( environmentRegistryEntry . name ) . show ( ) ; } const peerNameArray = [ ] ; let peerNames ; for ( const org of orgMap . keys ( ) ) { peerNameArray . push ( ... orgMap . get ( org ) ) ; } peerNames = new Set ( peerNameArray ) ; for ( const peer of peerNames ) { peerCount ++ ; progress . report ( { message : `Installing Smart Contract on peer ${ peer }` } ) ; try { let timeout = vscode . workspace . getConfiguration ( ) . get ( SettingConfigurations . FABRIC_CLIENT_TIMEOUT ) ; if ( timeout ) { timeout = timeout * 1000 ; } packageId = await connection . installSmartContract ( chosenPackage . path , peer , `${ chosenPackage . name }_${ chosenPackage . version }` , timeout ) ; outputAdapter . log ( LogType . SUCCESS , `Successfully installed on peer ${ peer }` ) ; } catch ( error ) { outputAdapter . log ( LogType . ERROR , `Failed to install on peer ${ peer } with reason: ${ error . message }` , `Failed to install on peer ${ peer } with reason: ${ error . toString ( ) }` ) ; if ( error . message . includes ( 'REQUEST TIMEOUT' ) ) { successfulInstall = InstallError . TIMEOUT ; } else { successfulInstall = InstallError . OTHER ; } } } await vscode . commands . executeCommand ( ExtensionCommands . REFRESH_GATEWAYS ) ; await vscode . commands . executeCommand ( ExtensionCommands . REFRESH_ENVIRONMENTS ) ; } ) ; if ( successfulInstall === InstallError . NONE ) { if ( peerCount > 1 ) { outputAdapter . log ( LogType . SUCCESS , 'Successfully installed smart contract on all peers' ) ; } Reporter . instance ( ) . sendTelemetryEvent ( 'installCommand' ) ; return [ packageId , undefined ] ; } else { return [ undefined , successfulInstall ] ; } }