'use strict' ; import * as vscode from 'vscode' ; import * as child_process from 'child_process' ; import stripAnsi = require ( 'strip-ansi' ) ; import * as childProcessPromise from 'child-process-promise' ; import { ConsoleOutputAdapter , LogType , OutputAdapter } from 'ibm-blockchain-platform-common' ; import * as request from 'request' ; const exec = childProcessPromise . exec ; export class CommandUtil { public static async sendCommand ( command , cwd ? ) { const result = await exec ( command , { cwd : cwd } ) ; return result . stdout . trim ( ) ; } public static async sendCommandWithProgress ( command , cwd , message ) { return vscode . window . withProgress ( { location : vscode . ProgressLocation . Notification , title : 'Blockchain Extension' , cancellable : false } , async ( progress ) : Promise < string > => { progress . report ( { message } ) ; return this . sendCommand ( command , cwd ) ; } ) ; } public static async sendCommandWithOutput ( command , args , cwd ? , env ? , outputAdapter ? , shell = false ) { if ( ! outputAdapter ) { outputAdapter = ConsoleOutputAdapter . instance ( ) ; } const options = { cwd , env , shell } ; const child = child_process . spawn ( command , args , options ) ; child . stdout . on ( 'data' , ( data ) => { const str = stripAnsi ( data . toString ( ) ) ; str . replace ( /[\r\n]+$/ , '' ) . split ( /[\r\n]+/ ) . forEach ( ( line ) => outputAdapter . log ( LogType . INFO , undefined , line ) ) ; } ) ; child . stderr . on ( 'data' , ( data ) => { const str = stripAnsi ( data . toString ( ) ) ; str . replace ( /[\r\n]+$/ , '' ) . split ( /[\r\n]+/ ) . forEach ( ( line ) => outputAdapter . log ( LogType . INFO , undefined , line ) ) ; } ) ; return new Promise < void > ( ( resolve , reject ) : any => { child . on ( 'error' , reject ) ; child . on ( 'exit' , ( code ) => { if ( code ) { return reject ( new Error ( `Failed to execute command "${ command }" with  arguments "${ args . join ( ', ' ) }" return code ${ code }` ) ) ; } resolve ( ) ; } ) ; } ) ; } public static async sendCommandWithOutputAndProgress ( command , args , message , cwd ? , env ? , outputAdapter ? , shell = false ) { await vscode . window . withProgress ( { location : vscode . ProgressLocation . Notification , title : 'Blockchain Extension' , cancellable : false } , async ( progress ) : Promise < void > => { progress . report ( { message } ) ; await this . sendCommandWithOutput ( command , args , cwd , env , outputAdapter , shell ) ; } ) ; } public static sendRequestWithOutput ( url , outputAdapter ? ) { if ( ! outputAdapter ) { outputAdapter = ConsoleOutputAdapter . instance ( ) ; } return request . get ( url ) . on ( 'data' , ( response ) => { const str = stripAnsi ( response . toString ( ) ) ; str . replace ( /[\r\n]+$/ , '' ) . split ( /[\r\n]+/ ) . forEach ( ( line ) => { line = line . replace ( /^[\s]+/ , '' ) ; outputAdapter . log ( LogType . INFO , undefined , line ) ; } ) ; } ) . on ( 'error' , ( error ) => { const str = stripAnsi ( error . toString ( ) ) ; str . replace ( /[\r\n]+$/ , '' ) . split ( /[\r\n]+/ ) . forEach ( ( line ) => { line = line . replace ( /^[\s]+/ , '' ) ; outputAdapter . log ( LogType . ERROR , undefined , line ) ; } ) ; } ) ; } public static abortRequest ( requestToAbort ) { requestToAbort . abort ( ) ; } }