11import { spawn , execSync , ChildProcess } from 'child_process' ;
2+ import * as fs from 'fs' ;
23import { EventEmitter } from 'events' ;
34import * as path from 'path' ;
45import * as https from 'https' ;
@@ -39,10 +40,12 @@ export interface INSDebugConnection {
3940
4041export abstract class NSProject extends EventEmitter {
4142 private _projectPath : string ;
43+ private _tnsOutputFileStream : fs . WriteStream ;
4244
43- constructor ( projectPath : string ) {
45+ constructor ( projectPath : string , tnsOutputFilePath ?: string ) {
4446 super ( ) ;
4547 this . _projectPath = projectPath ;
48+ this . _tnsOutputFileStream = tnsOutputFilePath ? fs . createWriteStream ( tnsOutputFilePath ) : null ;
4649 }
4750
4851 public projectPath ( ) : string {
@@ -52,12 +55,25 @@ export abstract class NSProject extends EventEmitter {
5255 public abstract platform ( ) : string ;
5356
5457 public abstract run ( emulator : boolean ) : Promise < ChildProcess > ;
58+
59+ protected spawnProcess ( commandPath : string , commandArgs : string [ ] , tnsOutput ?: string ) : ChildProcess {
60+ let child : ChildProcess = spawn ( commandPath , commandArgs , { cwd : this . projectPath ( ) } ) ;
61+ child . stdout . setEncoding ( 'utf8' ) ;
62+ child . stderr . setEncoding ( 'utf8' ) ;
63+ return child ;
64+ }
65+
66+ protected writeToTnsOutputFile ( message : string ) {
67+ if ( this . _tnsOutputFileStream ) {
68+ this . _tnsOutputFileStream . write ( message , 'utf8' ) ;
69+ }
70+ }
5571}
5672
5773export class IosProject extends NSProject {
5874
59- constructor ( projectPath : string ) {
60- super ( projectPath ) ;
75+ constructor ( projectPath : string , tnsOutputFilePath ?: string ) {
76+ super ( projectPath , tnsOutputFilePath ) ;
6177 }
6278
6379 public platform ( ) : string {
@@ -76,10 +92,7 @@ export class IosProject extends NSProject {
7692 . appendParamIf ( "--emulator" , emulator )
7793 . build ( ) ;
7894
79- let child : ChildProcess = spawn ( command . path , command . args , { cwd : this . projectPath ( ) } ) ;
80- child . stdout . setEncoding ( 'utf8' ) ;
81- child . stderr . setEncoding ( 'utf8' ) ;
82-
95+ let child : ChildProcess = this . spawnProcess ( command . path , command . args ) ;
8396 return Promise . resolve ( child ) ;
8497 }
8598
@@ -105,13 +118,12 @@ export class IosProject extends NSProject {
105118
106119 return new Promise < string > ( ( resolve , reject ) => {
107120 // run NativeScript CLI command
108- let child : ChildProcess = spawn ( command . path , command . args , { cwd : this . projectPath ( ) } ) ;
109- child . stdout . setEncoding ( 'utf8' ) ;
110- child . stderr . setEncoding ( 'utf8' ) ;
121+ let child : ChildProcess = this . spawnProcess ( command . path , command . args , args . tnsOutput ) ;
111122
112123 child . stdout . on ( 'data' , ( data ) => {
113124 let strData : string = data . toString ( ) ;
114125 this . emit ( 'TNS.outputMessage' , strData , 'log' ) ;
126+ this . writeToTnsOutputFile ( strData ) ;
115127 if ( ! readyToConnect ) {
116128 let matches : RegExpMatchArray = strData . match ( socketPathPattern ) ;
117129 if ( matches && matches . length > 0 ) {
@@ -123,6 +135,7 @@ export class IosProject extends NSProject {
123135
124136 child . stderr . on ( 'data' , ( data ) => {
125137 this . emit ( 'TNS.outputMessage' , data , 'error' ) ;
138+ this . writeToTnsOutputFile ( data ) ;
126139 } ) ;
127140
128141 child . on ( 'close' , ( code , signal ) => {
@@ -138,8 +151,8 @@ export class IosProject extends NSProject {
138151
139152export class AndroidProject extends NSProject {
140153
141- constructor ( projectPath : string ) {
142- super ( projectPath ) ;
154+ constructor ( projectPath : string , tnsOutputFilePath ?: string ) {
155+ super ( projectPath , tnsOutputFilePath ) ;
143156 }
144157
145158 public platform ( ) : string {
@@ -154,10 +167,7 @@ export class AndroidProject extends NSProject {
154167 . appendParamIf ( "--emulator" , emulator )
155168 . build ( ) ;
156169
157- let child : ChildProcess = spawn ( command . path , command . args , { cwd : this . projectPath ( ) } ) ;
158- child . stdout . setEncoding ( 'utf8' ) ;
159- child . stderr . setEncoding ( 'utf8' ) ;
160-
170+ let child : ChildProcess = this . spawnProcess ( command . path , command . args ) ;
161171 return Promise . resolve ( child ) ;
162172 }
163173
@@ -179,15 +189,14 @@ export class AndroidProject extends NSProject {
179189 . appendParams ( args . tnsArgs )
180190 . build ( ) ;
181191
182- Logger . log ( "tns debug command: " + command ) ;
192+ Logger . log ( "tns debug command: " + command ) ;
183193
184194 // run NativeScript CLI command
185- let child : ChildProcess = spawn ( command . path , command . args , { cwd : this . projectPath ( ) } ) ;
186- child . stdout . setEncoding ( 'utf8' ) ;
187- child . stderr . setEncoding ( 'utf8' ) ;
195+ let child : ChildProcess = this . spawnProcess ( command . path , command . args , args . tnsOutput ) ;
188196 child . stdout . on ( 'data' , function ( data ) {
189197 let strData : string = data . toString ( ) ;
190198 that . emit ( 'TNS.outputMessage' , data . toString ( ) , 'log' ) ;
199+ that . writeToTnsOutputFile ( strData ) ;
191200 if ( ! launched && args . request === "launch" && strData . indexOf ( '# NativeScript Debugger started #' ) > - 1 ) {
192201 launched = true ;
193202
@@ -200,7 +209,7 @@ export class AndroidProject extends NSProject {
200209
201210 child . stderr . on ( 'data' , function ( data ) {
202211 that . emit ( 'TNS.outputMessage' , data . toString ( ) , 'error' ) ;
203-
212+ that . writeToTnsOutputFile ( data ) ;
204213 } ) ;
205214
206215 child . on ( 'close' , function ( code ) {
@@ -229,12 +238,11 @@ export class AndroidProject extends NSProject {
229238 let that = this ;
230239 // run NativeScript CLI command
231240 return new Promise < number > ( ( resolve , reject ) => {
232- let child : ChildProcess = spawn ( command . path , command . args , { cwd : this . projectPath ( ) } ) ;
233- child . stdout . setEncoding ( 'utf8' ) ;
234- child . stderr . setEncoding ( 'utf8' ) ;
241+ let child : ChildProcess = this . spawnProcess ( command . path , command . args , args . tnsOutput ) ;
235242
236243 child . stdout . on ( 'data' , function ( data ) {
237244 that . emit ( 'TNS.outputMessage' , data . toString ( ) , 'log' ) ;
245+ that . writeToTnsOutputFile ( data ) ;
238246
239247 let regexp = new RegExp ( "(?:debug port: )([\\d]{5})" ) ;
240248
@@ -261,6 +269,7 @@ export class AndroidProject extends NSProject {
261269
262270 child . stderr . on ( 'data' , function ( data ) {
263271 that . emit ( 'TNS.outputMessage' , data . toString ( ) , 'error' ) ;
272+ that . writeToTnsOutputFile ( data ) ;
264273 } ) ;
265274
266275 child . on ( 'close' , function ( code ) {
@@ -280,7 +289,7 @@ class CommandBuilder {
280289 return this ;
281290 }
282291
283- public appendParams ( parameters : string [ ] ) : CommandBuilder {
292+ public appendParams ( parameters : string [ ] = [ ] ) : CommandBuilder {
284293 parameters . forEach ( param => this . appendParam ( param ) ) ;
285294 return this ;
286295 }
0 commit comments