1
1
import { spawn , execSync , ChildProcess } from 'child_process' ;
2
+ import * as fs from 'fs' ;
2
3
import { EventEmitter } from 'events' ;
3
4
import * as path from 'path' ;
4
5
import * as https from 'https' ;
@@ -39,10 +40,12 @@ export interface INSDebugConnection {
39
40
40
41
export abstract class NSProject extends EventEmitter {
41
42
private _projectPath : string ;
43
+ private _tnsOutputFileStream : fs . WriteStream ;
42
44
43
- constructor ( projectPath : string ) {
45
+ constructor ( projectPath : string , tnsOutputFilePath ?: string ) {
44
46
super ( ) ;
45
47
this . _projectPath = projectPath ;
48
+ this . _tnsOutputFileStream = tnsOutputFilePath ? fs . createWriteStream ( tnsOutputFilePath ) : null ;
46
49
}
47
50
48
51
public projectPath ( ) : string {
@@ -52,12 +55,25 @@ export abstract class NSProject extends EventEmitter {
52
55
public abstract platform ( ) : string ;
53
56
54
57
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
+ }
55
71
}
56
72
57
73
export class IosProject extends NSProject {
58
74
59
- constructor ( projectPath : string ) {
60
- super ( projectPath ) ;
75
+ constructor ( projectPath : string , tnsOutputFilePath ?: string ) {
76
+ super ( projectPath , tnsOutputFilePath ) ;
61
77
}
62
78
63
79
public platform ( ) : string {
@@ -76,10 +92,7 @@ export class IosProject extends NSProject {
76
92
. appendParamIf ( "--emulator" , emulator )
77
93
. build ( ) ;
78
94
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 ) ;
83
96
return Promise . resolve ( child ) ;
84
97
}
85
98
@@ -105,13 +118,12 @@ export class IosProject extends NSProject {
105
118
106
119
return new Promise < string > ( ( resolve , reject ) => {
107
120
// 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 ) ;
111
122
112
123
child . stdout . on ( 'data' , ( data ) => {
113
124
let strData : string = data . toString ( ) ;
114
125
this . emit ( 'TNS.outputMessage' , strData , 'log' ) ;
126
+ this . writeToTnsOutputFile ( strData ) ;
115
127
if ( ! readyToConnect ) {
116
128
let matches : RegExpMatchArray = strData . match ( socketPathPattern ) ;
117
129
if ( matches && matches . length > 0 ) {
@@ -123,6 +135,7 @@ export class IosProject extends NSProject {
123
135
124
136
child . stderr . on ( 'data' , ( data ) => {
125
137
this . emit ( 'TNS.outputMessage' , data , 'error' ) ;
138
+ this . writeToTnsOutputFile ( data ) ;
126
139
} ) ;
127
140
128
141
child . on ( 'close' , ( code , signal ) => {
@@ -138,8 +151,8 @@ export class IosProject extends NSProject {
138
151
139
152
export class AndroidProject extends NSProject {
140
153
141
- constructor ( projectPath : string ) {
142
- super ( projectPath ) ;
154
+ constructor ( projectPath : string , tnsOutputFilePath ?: string ) {
155
+ super ( projectPath , tnsOutputFilePath ) ;
143
156
}
144
157
145
158
public platform ( ) : string {
@@ -154,10 +167,7 @@ export class AndroidProject extends NSProject {
154
167
. appendParamIf ( "--emulator" , emulator )
155
168
. build ( ) ;
156
169
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 ) ;
161
171
return Promise . resolve ( child ) ;
162
172
}
163
173
@@ -179,15 +189,14 @@ export class AndroidProject extends NSProject {
179
189
. appendParams ( args . tnsArgs )
180
190
. build ( ) ;
181
191
182
- Logger . log ( "tns debug command: " + command ) ;
192
+ Logger . log ( "tns debug command: " + command ) ;
183
193
184
194
// 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 ) ;
188
196
child . stdout . on ( 'data' , function ( data ) {
189
197
let strData : string = data . toString ( ) ;
190
198
that . emit ( 'TNS.outputMessage' , data . toString ( ) , 'log' ) ;
199
+ that . writeToTnsOutputFile ( strData ) ;
191
200
if ( ! launched && args . request === "launch" && strData . indexOf ( '# NativeScript Debugger started #' ) > - 1 ) {
192
201
launched = true ;
193
202
@@ -200,7 +209,7 @@ export class AndroidProject extends NSProject {
200
209
201
210
child . stderr . on ( 'data' , function ( data ) {
202
211
that . emit ( 'TNS.outputMessage' , data . toString ( ) , 'error' ) ;
203
-
212
+ that . writeToTnsOutputFile ( data ) ;
204
213
} ) ;
205
214
206
215
child . on ( 'close' , function ( code ) {
@@ -229,12 +238,11 @@ export class AndroidProject extends NSProject {
229
238
let that = this ;
230
239
// run NativeScript CLI command
231
240
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 ) ;
235
242
236
243
child . stdout . on ( 'data' , function ( data ) {
237
244
that . emit ( 'TNS.outputMessage' , data . toString ( ) , 'log' ) ;
245
+ that . writeToTnsOutputFile ( data ) ;
238
246
239
247
let regexp = new RegExp ( "(?:debug port: )([\\d]{5})" ) ;
240
248
@@ -261,6 +269,7 @@ export class AndroidProject extends NSProject {
261
269
262
270
child . stderr . on ( 'data' , function ( data ) {
263
271
that . emit ( 'TNS.outputMessage' , data . toString ( ) , 'error' ) ;
272
+ that . writeToTnsOutputFile ( data ) ;
264
273
} ) ;
265
274
266
275
child . on ( 'close' , function ( code ) {
@@ -280,7 +289,7 @@ class CommandBuilder {
280
289
return this ;
281
290
}
282
291
283
- public appendParams ( parameters : string [ ] ) : CommandBuilder {
292
+ public appendParams ( parameters : string [ ] = [ ] ) : CommandBuilder {
284
293
parameters . forEach ( param => this . appendParam ( param ) ) ;
285
294
return this ;
286
295
}
0 commit comments