'use strict' ; import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter' ; import { UserInputUtil , IBlockchainQuickPickItem } from './UserInputUtil' ; import { FabricWalletRegistryEntry , LogType } from 'ibm-blockchain-platform-common' ; import * as os from 'os' ; import * as path from 'path' ; import * as vscode from 'vscode' ; import * as fs from 'fs-extra' ; import { Reporter } from '../util/Reporter' ; import { WalletTreeItem } from '../explorer/wallets/WalletTreeItem' ; export async function exportWallet ( walletTreeItem ? ) { const outputAdapter = VSCodeBlockchainOutputAdapter . instance ( ) ; outputAdapter . log ( LogType . INFO , undefined , 'exportWalletCommand' ) ; let exportedWalletRegistryEntry ; if ( ! walletTreeItem ) { const chosenWallet = await UserInputUtil . showWalletsQuickPickBox ( 'Choose a wallet to export' , false , true ) as IBlockchainQuickPickItem < FabricWalletRegistryEntry > ; if ( ! chosenWallet ) { return ; } exportedWalletRegistryEntry = chosenWallet . data ; } else { exportedWalletRegistryEntry = walletTreeItem . registryEntry ; } let defaultPath ; const workspaceFolders = UserInputUtil . getWorkspaceFolders ( ) ; if ( workspaceFolders . length > 0 ) { defaultPath = path . join ( workspaceFolders [ 0 ] . uri . fsPath , exportedWalletRegistryEntry . name ) ; } else { defaultPath = path . join ( os . homedir ( ) , exportedWalletRegistryEntry . name ) ; } const walletUri = await vscode . window . showSaveDialog ( { defaultUri : vscode . Uri . file ( defaultPath ) , saveLabel : 'Export' } ) ; if ( ! walletUri ) { return ; } const walletName = exportedWalletRegistryEntry . displayName ? exportedWalletRegistryEntry . displayName : exportedWalletRegistryEntry . name ; try { await fs . ensureDir ( walletUri . fsPath ) ; const walletContents = await fs . readdir ( exportedWalletRegistryEntry . walletPath ) ; if ( walletContents . length ) { for ( const walletItem of walletContents ) { const walletItemPath = path . join ( exportedWalletRegistryEntry . walletPath , walletItem ) ; if ( walletItemPath . endsWith ( '.id' ) ) { await fs . copy ( walletItemPath , path . join ( walletUri . fsPath , walletItem ) ) ; } } } } catch ( error ) { outputAdapter . log ( LogType . ERROR , `Issue exporting wallet: ${ error . message }` , `Issue exporting wallet: ${ error . toString ( ) }` ) ; if ( ( await fs . pathExists ( walletUri . fsPath ) ) ) { await fs . remove ( walletUri . fsPath ) ; } return ; } outputAdapter . log ( LogType . SUCCESS , `Successfully exported wallet ${ walletName } to ${ walletUri . fsPath }` ) ; Reporter . instance ( ) . sendTelemetryEvent ( 'exportWallet' ) ; }