Skip to content

Commit 5ae6ae7

Browse files
committed
Use spawn instead of exec
1 parent 2a0ebe2 commit 5ae6ae7

File tree

2 files changed

+45
-29
lines changed

2 files changed

+45
-29
lines changed

src/.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"program": "${workspaceRoot}/../out/webkit/webKitDebug.js",
99
"runtimeArgs": ["--nolazy"],
1010
"args": [ "--server=4712" ],
11-
"stopOnEntry": false,
11+
"stopOnEntry": true,
1212
"sourceMaps": true,
1313
"outDir": "${workspaceRoot}/../out",
1414
"cwd": "${workspaceRoot}"

src/nativescript/nativescript.ts

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {exec, execSync, ChildProcess} from 'child_process';
1+
import {spawn, execSync, ChildProcess} from 'child_process';
22
import {EventEmitter} from 'events';
33
import * as path from 'path';
44
import * as https from 'https';
@@ -70,13 +70,16 @@ export class IosProject extends NSProject {
7070
}
7171

7272
// build command to execute
73-
let command: string = new CommandBuilder()
73+
let command = new CommandBuilder()
7474
.appendParam("run")
7575
.appendParam(this.platform())
76-
.tryAppendParam("--emulator", emulator)
76+
.appendParamIf("--emulator", emulator)
7777
.build();
7878

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+
8083
return Promise.resolve(child);
8184
}
8285

@@ -86,11 +89,11 @@ export class IosProject extends NSProject {
8689
}
8790

8891
// build command to execute
89-
let command: string = new CommandBuilder()
92+
let command = new CommandBuilder()
9093
.appendParam("debug")
9194
.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")
9497
.appendParam("--no-client")
9598
.appendParam(args.tnsArgs)
9699
.build();
@@ -101,7 +104,9 @@ export class IosProject extends NSProject {
101104

102105
return new Promise<string>((resolve, reject) => {
103106
// 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');
105110

106111
child.stdout.on('data', (data) => {
107112
let strData: string = data.toString();
@@ -119,7 +124,7 @@ export class IosProject extends NSProject {
119124
this.emit('TNS.outputMessage', data, 'error');
120125
});
121126

122-
child.on('close', (code) => {
127+
child.on('close', (code, signal) => {
123128
reject("The debug process exited unexpectedly code:" + code);
124129
});
125130
});
@@ -142,13 +147,16 @@ export class AndroidProject extends NSProject {
142147

143148
public run(emulator: boolean): Promise<ChildProcess> {
144149
// build command to execute
145-
let command: string = new CommandBuilder()
150+
let command = new CommandBuilder()
146151
.appendParam("run")
147152
.appendParam(this.platform())
148-
.tryAppendParam("--emulator", emulator)
153+
.appendParamIf("--emulator", emulator)
149154
.build();
150155

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+
152160
return Promise.resolve(child);
153161
}
154162

@@ -161,18 +169,20 @@ export class AndroidProject extends NSProject {
161169
let launched = false;
162170

163171
return new Promise<void>((resolve, reject) => {
164-
let command: string = new CommandBuilder()
172+
let command = new CommandBuilder()
165173
.appendParam("debug")
166174
.appendParam(this.platform())
167-
.tryAppendParam("--emulator", args.emulator)
175+
.appendParamIf("--emulator", args.emulator)
168176
.appendParam("--no-client")
169177
.appendParam(args.tnsArgs)
170178
.build();
171179

172180
Logger.log("tns debug command: " + command);
173181

174182
// 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');
176186
child.stdout.on('data', function(data) {
177187
let strData: string = data.toString();
178188
that.emit('TNS.outputMessage', data.toString(), 'log');
@@ -208,7 +218,7 @@ export class AndroidProject extends NSProject {
208218

209219
//return Promise.resolve(40001);
210220

211-
let command: string = new CommandBuilder()
221+
let command = new CommandBuilder()
212222
.appendParam("debug")
213223
.appendParam(this.platform())
214224
.appendParam("--get-port")
@@ -217,7 +227,10 @@ export class AndroidProject extends NSProject {
217227
let that = this;
218228
// run NativeScript CLI command
219229
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+
221234
child.stdout.on('data', function(data) {
222235
that.emit('TNS.outputMessage', data.toString(), 'log');
223236

@@ -256,26 +269,29 @@ export class AndroidProject extends NSProject {
256269
}
257270

258271
class CommandBuilder {
259-
private _command: string;
272+
public static tnsPath: string = 'tns';
260273

261-
constructor() {
262-
this._command = 'tns';
263-
}
274+
private _command: string[] = [];
264275

265-
public appendParam(parameter: string = ""): CommandBuilder {
266-
this._command += " " + parameter;
276+
public appendParam(parameter: string): CommandBuilder {
277+
this._command.push(parameter);
267278
return this;
268279
}
269280

270-
public tryAppendParam(parameter: string = "", condtion: boolean): CommandBuilder {
281+
public appendParamIf(parameter: string, condtion: boolean): CommandBuilder {
271282
if (condtion) {
272-
this._command += " " + parameter;
283+
this._command.push(parameter);
273284
}
274285
return this;
275286
}
276287

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(' ');
279295
}
280296
}
281297

@@ -416,7 +432,7 @@ export class CliVersionInfo {
416432
public static getInstalledCliVersion(): number[] {
417433
if (this.installedCliVersion === null) {
418434
// 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
420436
try {
421437
let versionStr: string = execSync(getVersionCommand).toString().trim(); // execute it
422438
this.installedCliVersion = versionStr ? Version.parse(versionStr) : null; // parse the version string

0 commit comments

Comments
 (0)