1
- import { exec , execSync , ChildProcess } from 'child_process' ;
1
+ import { spawn , execSync , ChildProcess } from 'child_process' ;
2
2
import { EventEmitter } from 'events' ;
3
3
import * as path from 'path' ;
4
4
import * as https from 'https' ;
@@ -70,13 +70,16 @@ export class IosProject extends NSProject {
70
70
}
71
71
72
72
// build command to execute
73
- let command : string = new CommandBuilder ( )
73
+ let command = new CommandBuilder ( )
74
74
. appendParam ( "run" )
75
75
. appendParam ( this . platform ( ) )
76
- . tryAppendParam ( "--emulator" , emulator )
76
+ . appendParamIf ( "--emulator" , emulator )
77
77
. build ( ) ;
78
78
79
- let child : ChildProcess = exec ( command , { cwd : this . projectPath ( ) } ) ;
79
+ let child : ChildProcess = spawn ( command . path , command . args , { cwd : this . projectPath ( ) } ) ;
80
+ child . stdout . setEncoding ( 'utf8' ) ;
81
+ child . stderr . setEncoding ( 'utf8' ) ;
82
+
80
83
return Promise . resolve ( child ) ;
81
84
}
82
85
@@ -86,11 +89,11 @@ export class IosProject extends NSProject {
86
89
}
87
90
88
91
// build command to execute
89
- let command : string = new CommandBuilder ( )
92
+ let command = new CommandBuilder ( )
90
93
. appendParam ( "debug" )
91
94
. appendParam ( this . platform ( ) )
92
- . tryAppendParam ( "--emulator" , args . emulator )
93
- . tryAppendParam ( "--start" , args . request === "attach" )
95
+ . appendParamIf ( "--emulator" , args . emulator )
96
+ . appendParamIf ( "--start" , args . request === "attach" )
94
97
. appendParam ( "--no-client" )
95
98
. appendParam ( args . tnsArgs )
96
99
. build ( ) ;
@@ -101,7 +104,9 @@ export class IosProject extends NSProject {
101
104
102
105
return new Promise < string > ( ( resolve , reject ) => {
103
106
// run NativeScript CLI command
104
- let child : ChildProcess = exec ( command , { cwd : this . projectPath ( ) } ) ;
107
+ let child : ChildProcess = spawn ( command . path , command . args , { cwd : this . projectPath ( ) } ) ;
108
+ child . stdout . setEncoding ( 'utf8' ) ;
109
+ child . stderr . setEncoding ( 'utf8' ) ;
105
110
106
111
child . stdout . on ( 'data' , ( data ) => {
107
112
let strData : string = data . toString ( ) ;
@@ -119,7 +124,7 @@ export class IosProject extends NSProject {
119
124
this . emit ( 'TNS.outputMessage' , data , 'error' ) ;
120
125
} ) ;
121
126
122
- child . on ( 'close' , ( code ) => {
127
+ child . on ( 'close' , ( code , signal ) => {
123
128
reject ( "The debug process exited unexpectedly code:" + code ) ;
124
129
} ) ;
125
130
} ) ;
@@ -142,13 +147,16 @@ export class AndroidProject extends NSProject {
142
147
143
148
public run ( emulator : boolean ) : Promise < ChildProcess > {
144
149
// build command to execute
145
- let command : string = new CommandBuilder ( )
150
+ let command = new CommandBuilder ( )
146
151
. appendParam ( "run" )
147
152
. appendParam ( this . platform ( ) )
148
- . tryAppendParam ( "--emulator" , emulator )
153
+ . appendParamIf ( "--emulator" , emulator )
149
154
. build ( ) ;
150
155
151
- let child : ChildProcess = exec ( command , { cwd : this . projectPath ( ) } ) ;
156
+ let child : ChildProcess = spawn ( command . path , command . args , { cwd : this . projectPath ( ) } ) ;
157
+ child . stdout . setEncoding ( 'utf8' ) ;
158
+ child . stderr . setEncoding ( 'utf8' ) ;
159
+
152
160
return Promise . resolve ( child ) ;
153
161
}
154
162
@@ -161,18 +169,20 @@ export class AndroidProject extends NSProject {
161
169
let launched = false ;
162
170
163
171
return new Promise < void > ( ( resolve , reject ) => {
164
- let command : string = new CommandBuilder ( )
172
+ let command = new CommandBuilder ( )
165
173
. appendParam ( "debug" )
166
174
. appendParam ( this . platform ( ) )
167
- . tryAppendParam ( "--emulator" , args . emulator )
175
+ . appendParamIf ( "--emulator" , args . emulator )
168
176
. appendParam ( "--no-client" )
169
177
. appendParam ( args . tnsArgs )
170
178
. build ( ) ;
171
179
172
180
Logger . log ( "tns debug command: " + command ) ;
173
181
174
182
// run NativeScript CLI command
175
- let child : ChildProcess = exec ( command , { cwd : this . projectPath ( ) } ) ;
183
+ let child : ChildProcess = spawn ( command . path , command . args , { cwd : this . projectPath ( ) } ) ;
184
+ child . stdout . setEncoding ( 'utf8' ) ;
185
+ child . stderr . setEncoding ( 'utf8' ) ;
176
186
child . stdout . on ( 'data' , function ( data ) {
177
187
let strData : string = data . toString ( ) ;
178
188
that . emit ( 'TNS.outputMessage' , data . toString ( ) , 'log' ) ;
@@ -208,7 +218,7 @@ export class AndroidProject extends NSProject {
208
218
209
219
//return Promise.resolve(40001);
210
220
211
- let command : string = new CommandBuilder ( )
221
+ let command = new CommandBuilder ( )
212
222
. appendParam ( "debug" )
213
223
. appendParam ( this . platform ( ) )
214
224
. appendParam ( "--get-port" )
@@ -217,7 +227,10 @@ export class AndroidProject extends NSProject {
217
227
let that = this ;
218
228
// run NativeScript CLI command
219
229
return new Promise < number > ( ( resolve , reject ) => {
220
- let child : ChildProcess = exec ( command , { cwd : this . projectPath ( ) } ) ;
230
+ let child : ChildProcess = spawn ( command . path , command . args , { cwd : this . projectPath ( ) } ) ;
231
+ child . stdout . setEncoding ( 'utf8' ) ;
232
+ child . stderr . setEncoding ( 'utf8' ) ;
233
+
221
234
child . stdout . on ( 'data' , function ( data ) {
222
235
that . emit ( 'TNS.outputMessage' , data . toString ( ) , 'log' ) ;
223
236
@@ -256,26 +269,29 @@ export class AndroidProject extends NSProject {
256
269
}
257
270
258
271
class CommandBuilder {
259
- private _command : string ;
272
+ public static tnsPath : string = 'tns' ;
260
273
261
- constructor ( ) {
262
- this . _command = 'tns' ;
263
- }
274
+ private _command : string [ ] = [ ] ;
264
275
265
- public appendParam ( parameter : string = "" ) : CommandBuilder {
266
- this . _command += " " + parameter ;
276
+ public appendParam ( parameter : string ) : CommandBuilder {
277
+ this . _command . push ( parameter ) ;
267
278
return this ;
268
279
}
269
280
270
- public tryAppendParam ( parameter : string = "" , condtion : boolean ) : CommandBuilder {
281
+ public appendParamIf ( parameter : string , condtion : boolean ) : CommandBuilder {
271
282
if ( condtion ) {
272
- this . _command += " " + parameter ;
283
+ this . _command . push ( parameter ) ;
273
284
}
274
285
return this ;
275
286
}
276
287
277
- public build ( ) : string {
278
- return this . _command ;
288
+ public build ( ) : { path : string , args : string [ ] } {
289
+ return { path : CommandBuilder . tnsPath , args : this . _command } ;
290
+ }
291
+
292
+ public buildAsString ( ) : string {
293
+ let result = this . build ( ) ;
294
+ return `${ result . path } ` + result . args . join ( ' ' ) ;
279
295
}
280
296
}
281
297
@@ -416,7 +432,7 @@ export class CliVersionInfo {
416
432
public static getInstalledCliVersion ( ) : number [ ] {
417
433
if ( this . installedCliVersion === null ) {
418
434
// get the currently installed CLI version
419
- let getVersionCommand : string = new CommandBuilder ( ) . appendParam ( '--version' ) . build ( ) ; // tns --version
435
+ let getVersionCommand : string = new CommandBuilder ( ) . appendParam ( '--version' ) . buildAsString ( ) ; // tns --version
420
436
try {
421
437
let versionStr : string = execSync ( getVersionCommand ) . toString ( ) . trim ( ) ; // execute it
422
438
this . installedCliVersion = versionStr ? Version . parse ( versionStr ) : null ; // parse the version string
0 commit comments