Skip to content

Commit 08ed1c8

Browse files
committed
Introduce tnsOutput option in launch.json
1 parent 5c72e88 commit 08ed1c8

File tree

4 files changed

+48
-27
lines changed

4 files changed

+48
-27
lines changed

package.json

+10
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@
191191
},
192192
"default": []
193193
},
194+
"tnsOutput": {
195+
"type": "string",
196+
"description": "Path to file where the output of the CLI to redirected.",
197+
"default": null
198+
},
194199
"appRoot": {
195200
"type": "string",
196201
"description": "The path to the root folder of the application relative to the current working directory.",
@@ -237,6 +242,11 @@
237242
},
238243
"default": []
239244
},
245+
"tnsOutput": {
246+
"type": "string",
247+
"description": "Path to file where the output of the CLI to redirected.",
248+
"default": null
249+
},
240250
"appRoot": {
241251
"type": "string",
242252
"description": "The path to the root folder of the application relative to the current working directory.",

src/nativescript/nativescript.ts

+34-25
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {spawn, execSync, ChildProcess} from 'child_process';
2+
import * as fs from 'fs';
23
import {EventEmitter} from 'events';
34
import * as path from 'path';
45
import * as https from 'https';
@@ -39,10 +40,12 @@ export interface INSDebugConnection {
3940

4041
export 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

5773
export 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

139152
export 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
}

src/webkit/webKitAdapterInterfaces.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface ILaunchRequestArgs extends DebugProtocol.LaunchRequestArguments
1111
emulator?:boolean;
1212
request: string;
1313
tnsArgs?: string[];
14+
tnsOutput?: string;
1415
}
1516

1617
export interface IAttachRequestArgs extends DebugProtocol.AttachRequestArguments {
@@ -21,6 +22,7 @@ export interface IAttachRequestArgs extends DebugProtocol.AttachRequestArguments
2122
emulator?:boolean;
2223
request: string;
2324
tnsArgs?: string[];
25+
tnsOutput?: string;
2426
}
2527

2628
export interface ISetBreakpointsArgs extends DebugProtocol.SetBreakpointsArguments {

src/webkit/webKitDebugAdapter.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export class WebKitDebugAdapter implements IDebugAdapter {
131131
}
132132

133133
private _attachIos(args: IAttachRequestArgs | ILaunchRequestArgs): Promise<void> {
134-
let iosProject : ns.IosProject = new ns.IosProject(this.appRoot);
134+
let iosProject : ns.IosProject = new ns.IosProject(this.appRoot, args.tnsOutput);
135135
iosProject.on('TNS.outputMessage', (message, level) => this.onTnsOutputMessage.apply(this, [message, level]));
136136

137137
return iosProject.debug(args)
@@ -143,7 +143,7 @@ export class WebKitDebugAdapter implements IDebugAdapter {
143143
}
144144

145145
private _attachAndroid(args: IAttachRequestArgs | ILaunchRequestArgs): Promise<void> {
146-
let androidProject: ns.AndroidProject = new ns.AndroidProject(this.appRoot);
146+
let androidProject: ns.AndroidProject = new ns.AndroidProject(this.appRoot, args.tnsOutput);
147147
let thisAdapter: WebKitDebugAdapter = this;
148148

149149
androidProject.on('TNS.outputMessage', (message, level) => thisAdapter.onTnsOutputMessage.apply(thisAdapter, [message, level]));

0 commit comments

Comments
 (0)