import { ChildProcess , execSync , spawn } from 'child_process' ; import { ILogger } from '../common/logger' ; import * as utils from '../common/utilities' ; export class NativeScriptCli { private static CLI_OUTPUT_VERSION_REGEXP = /^(?:\d+\.){2}\d+.*?$/m ; private _path : string ; private _shellPath : string ; private _logger : ILogger ; constructor ( cliPath , logger ) { this . _path = cliPath ; this . _logger = logger ; this . _shellPath = process . env . SHELL ; if ( utils . getPlatform ( ) === utils . Platform . Windows ) { this . _shellPath = 'cmd.exe' ; } } public get path ( ) : string { return this . _path ; } public executeGetVersion ( ) { try { const versionOutput = this . executeSync ( [ '--version' ] , undefined ) ; return this . getVersionFromCLIOutput ( versionOutput ) ; } catch ( e ) { this . _logger . log ( e ) ; const errorMessage = `NativeScript CLI not found. Use 'nativescript.tnsPath' workspace setting
                                  to explicitly set the absolute path to the NativeScript CLI.` ; throw new Error ( errorMessage ) ; } } public executeSync ( args , cwd ) { args . unshift ( '--analyticsClient' , 'VSCode' ) ; const command = `${ this . _path } ${ args . join ( ' ' ) }` ; this . _logger . log ( `[NativeScriptCli] execute: ${ command }` ) ; return execSync ( command , { encoding : 'utf8' , cwd , shell : this . _shellPath } ) . toString ( ) . trim ( ) ; } public execute ( args , cwd ) { args . unshift ( '--analyticsClient' , 'VSCode' ) ; const command = `${ this . _path } ${ args . join ( ' ' ) }` ; this . _logger . log ( `[NativeScriptCli] execute: ${ command }` ) ; const options = { cwd , shell : this . _shellPath } ; const child = spawn ( this . _path , args , options ) ; child . stdout . setEncoding ( 'utf8' ) ; child . stderr . setEncoding ( 'utf8' ) ; return child ; } private getVersionFromCLIOutput ( commandOutput ) { const matches = commandOutput . match ( NativeScriptCli . CLI_OUTPUT_VERSION_REGEXP ) ; return matches && matches [ 0 ] ; } }