'use strict' ; import * as vscode from 'vscode' ; import * as fs from 'fs-extra' ; import * as path from 'path' ; import { Reporter } from '../util/Reporter' ; import { UserInputUtil } from './UserInputUtil' ; import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter' ; import { LogType , FileSystemUtil } from 'ibm-blockchain-platform-common' ; import { ExtensionCommands } from '../../ExtensionCommands' ; import { SettingConfigurations } from '../configurations' ; import { DeployView } from '../webview/DeployView' ; export async function importSmartContractPackageCommand ( ) { VSCodeBlockchainOutputAdapter . instance ( ) . log ( LogType . INFO , undefined , 'Import smart contract package' ) ; const openDialogOptions = { canSelectFiles : true , canSelectFolders : false , canSelectMany : false , openLabel : 'Select' , } ; const packagePath = await UserInputUtil . browse ( 'Browse for a package to import' , UserInputUtil . BROWSE_LABEL , openDialogOptions ) as string ; if ( ! packagePath ) { return ; } try { const extDir = SettingConfigurations . getExtensionDir ( ) ; const pkgDir = path . join ( extDir , 'packages' ) ; const resolvedPkgDir = FileSystemUtil . getDirPath ( pkgDir ) ; await fs . ensureDir ( resolvedPkgDir ) ; const packageName = path . basename ( packagePath ) ; const resolvedPkgPath = path . join ( resolvedPkgDir , packageName ) ; const nameRegex = new RegExp ( /^(.+?)(\.cds|\.tar\.gz|\.tgz)$/ ) ; const result = nameRegex . exec ( packageName ) ; if ( ! result ) { throw new Error ( 'Incorrect file type, file extension must be "cds", "tar.gz" or "tgz"' ) ; } const newName = result [ 1 ] ; const newExt = result [ 2 ] ; let existingPackages = await fs . readdir ( resolvedPkgDir ) ; existingPackages = existingPackages . filter ( ( name ) => { const existingName = nameRegex . exec ( name ) ; if ( ! existingName ) { return false ; } const existingIsCds = existingName [ 2 ] === '.cds' ; const newIsCds = newExt === '.cds' ; return newName === existingName [ 1 ] && existingIsCds === newIsCds ; } ) ; if ( existingPackages . length > 0 ) { const extText = newExt === nameRegex . exec ( existingPackages [ 0 ] ) [ 2 ] ? 'same' : 'equivalent' ; throw new Error ( `Package with name ${ newName } and ${ extText } file extension already exists` ) ; } await fs . copy ( packagePath , resolvedPkgPath ) ; await vscode . commands . executeCommand ( ExtensionCommands . REFRESH_PACKAGES ) ; const panel = DeployView . panel ; if ( panel ) { await DeployView . updatePackages ( ) ; } VSCodeBlockchainOutputAdapter . instance ( ) . log ( LogType . SUCCESS , 'Successfully imported smart contract package' , `Successfully imported smart contract package ${ packageName }` ) ; Reporter . instance ( ) . sendTelemetryEvent ( 'importSmartContractPackageCommand' ) ; } catch ( error ) { VSCodeBlockchainOutputAdapter . instance ( ) . log ( LogType . ERROR , `Failed to import smart contract package: ${ error . message }` , `Failed to import smart contract package: ${ error }` ) ; } }