'use strict' ; import * as vscode from 'vscode' ; import { Reporter } from '../util/Reporter' ; import { UserInputUtil , IBlockchainQuickPickItem } from './UserInputUtil' ; import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter' ; import * as path from 'path' ; import * as os from 'os' ; import * as fs from 'fs-extra' ; import * as yaml from 'js-yaml' ; import { GatewayTreeItem } from '../explorer/model/GatewayTreeItem' ; import { FabricGatewayHelper } from '../fabric/FabricGatewayHelper' ; import { FabricGatewayConnectionManager } from '../fabric/FabricGatewayConnectionManager' ; import { ConnectionProfileUtil , LogType , FabricGatewayRegistryEntry } from 'ibm-blockchain-platform-common' ; import * as lodash from 'lodash' ; export async function exportConnectionProfile ( gatewayTreeItem , isConnected ? ) { const outputAdapter = VSCodeBlockchainOutputAdapter . instance ( ) ; outputAdapter . log ( LogType . INFO , undefined , 'exportConnectionProfileCommand' ) ; let gatewayEntry ; if ( isConnected ) { const connectedGateway = await FabricGatewayConnectionManager . instance ( ) . getGatewayRegistryEntry ( ) ; gatewayEntry = connectedGateway ; } else if ( ! gatewayTreeItem ) { const chosenGateway = await UserInputUtil . showGatewayQuickPickBox ( 'Choose a gateway to export a connection profile from' , false , true ) as IBlockchainQuickPickItem < FabricGatewayRegistryEntry > ; if ( ! chosenGateway ) { return ; } gatewayEntry = chosenGateway . data ; } else { gatewayEntry = gatewayTreeItem . gateway ; } let defaultPath ; let fileName = gatewayEntry . name ; const firstAlphaIndex = fileName . search ( /[a-zA-Z]/ ) ; const beforeFirstAlpha = lodash . camelCase ( fileName . substr ( 0 , firstAlphaIndex ) ) + fileName . charAt ( firstAlphaIndex ) . toUpperCase ( ) ; fileName = beforeFirstAlpha + lodash . camelCase ( fileName . slice ( firstAlphaIndex + 1 ) ) + `Connection.json` ; const workspaceFolders = UserInputUtil . getWorkspaceFolders ( ) ; if ( workspaceFolders . length > 0 ) { defaultPath = path . join ( workspaceFolders [ 0 ] . uri . fsPath , fileName ) ; } else { defaultPath = path . join ( os . homedir ( ) , fileName ) ; } const chosenPathUri = await vscode . window . showSaveDialog ( { defaultUri : vscode . Uri . file ( defaultPath ) , saveLabel : 'Export' } ) ; if ( ! chosenPathUri ) { return ; } try { const connectionProfilePath = await FabricGatewayHelper . getConnectionProfilePath ( gatewayEntry ) ; const connectionProfile = await ConnectionProfileUtil . readConnectionProfile ( connectionProfilePath ) ; delete connectionProfile . wallet ; let connectionProfileData ; if ( connectionProfilePath . endsWith ( '.json' ) ) { connectionProfileData = JSON . stringify ( connectionProfile , null , 4 ) ; } else { connectionProfileData = yaml . dump ( connectionProfile ) ; } await fs . writeFile ( chosenPathUri . fsPath , connectionProfileData ) ; } catch ( error ) { outputAdapter . log ( LogType . ERROR , `Issue exporting connection profile: ${ error . message }` , `Issue exporting connection profile: ${ error . toString ( ) }` ) ; return ; } outputAdapter . log ( LogType . SUCCESS , `Successfully exported connection profile to ${ chosenPathUri . fsPath }` ) ; Reporter . instance ( ) . sendTelemetryEvent ( 'exportConnectionProfileCommand' ) ; }