Skip to content

Commit fde971a

Browse files
committed
Chrome dev tools
Open a web socket proxy when debugging is started with chrome option
1 parent 4d6c0d4 commit fde971a

File tree

4 files changed

+71
-76
lines changed

4 files changed

+71
-76
lines changed

lib/declarations.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ interface IOptions extends ICommonOptions {
103103
rebuild: boolean;
104104
syncAllFiles: boolean;
105105
liveEdit: boolean;
106+
chrome: boolean;
106107
}
107108

108109
interface IInitService {
@@ -233,7 +234,8 @@ interface IAndroidToolsInfoData {
233234
}
234235

235236
interface ISocketProxyFactory {
236-
createSocketProxy(factory: () => any): IFuture<any>;
237+
createTCPSocketProxy(factory: () => any): any;
238+
createWebSocketProxy(factory: () => any): any;
237239
}
238240

239241
interface IiOSNotification {
+54-68
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { PacketStream } from "./packet-stream";
22
import * as net from "net";
3-
import * as semver from "semver";
43
import * as ws from "ws";
54
import temp = require("temp");
65
import * as helpers from "../../common/helpers";
@@ -12,25 +11,65 @@ export class SocketProxyFactory implements ISocketProxyFactory {
1211
private $projectDataService: IProjectDataService,
1312
private $options: IOptions) { }
1413

15-
public createSocketProxy(factory: () => net.Socket): IFuture<any> {
16-
return (() => {
17-
let socketFactory = (callback: (_socket: net.Socket) => void) => helpers.connectEventually(factory, callback);
14+
public createTCPSocketProxy(factory: () => net.Socket): any {
15+
let socketFactory = (callback: (_socket: net.Socket) => void) => helpers.connectEventually(factory, callback);
1816

19-
this.$projectDataService.initialize(this.$projectData.projectDir);
20-
let frameworkVersion = this.$projectDataService.getValue("tns-ios").wait().version;
21-
let result: any;
17+
this.$logger.info("\nSetting up proxy...\nPress Ctrl + C to terminate, or disconnect.\n");
2218

23-
if(semver.gte(frameworkVersion, "1.4.0")) {
24-
result = this.createTcpSocketProxy(socketFactory);
25-
} else {
26-
result = this.createWebSocketProxy(socketFactory);
27-
}
19+
let server = net.createServer({
20+
allowHalfOpen: true
21+
});
22+
23+
server.on("connection", (frontendSocket: net.Socket) => {
24+
this.$logger.info("Frontend client connected.");
25+
26+
frontendSocket.on("end", () => {
27+
this.$logger.info('Frontend socket closed!');
28+
if (!(this.$config.debugLivesync && this.$options.watch)) {
29+
process.exit(0);
30+
}
31+
});
2832

29-
return result;
30-
}).future<any>()();
33+
socketFactory((backendSocket: net.Socket) => {
34+
this.$logger.info("Backend socket created.");
35+
36+
backendSocket.on("end", () => {
37+
this.$logger.info("Backend socket closed!");
38+
if (!(this.$config.debugLivesync && this.$options.watch)) {
39+
process.exit(0);
40+
}
41+
});
42+
43+
frontendSocket.on("close", () => {
44+
console.log("frontend socket closed");
45+
if (!(<any>backendSocket).destroyed) {
46+
backendSocket.destroy();
47+
}
48+
});
49+
backendSocket.on("close", () => {
50+
console.log("backend socket closed");
51+
if (!(<any>frontendSocket).destroyed) {
52+
frontendSocket.destroy();
53+
}
54+
});
55+
56+
backendSocket.pipe(frontendSocket);
57+
frontendSocket.pipe(backendSocket);
58+
frontendSocket.resume();
59+
});
60+
});
61+
62+
let socketFileLocation = temp.path({ suffix: ".sock" });
63+
server.listen(socketFileLocation);
64+
if(!this.$options.client) {
65+
this.$logger.info("socket-file-location: " + socketFileLocation);
66+
}
67+
68+
return server;
3169
}
3270

33-
private createWebSocketProxy(socketFactory: (handler: (socket: net.Socket) => void) => void): ws.Server {
71+
public createWebSocketProxy(factory: () => net.Socket): ws.Server {
72+
let socketFactory = (callback: (_socket: net.Socket) => void) => helpers.connectEventually(factory, callback);
3473
// NOTE: We will try to provide command line options to select ports, at least on the localhost.
3574
let localPort = 8080;
3675

@@ -86,58 +125,5 @@ export class SocketProxyFactory implements ISocketProxyFactory {
86125
this.$logger.info("Opened localhost " + localPort);
87126
return server;
88127
}
89-
90-
private createTcpSocketProxy(socketFactory: (handler: (socket: net.Socket) => void) => void): net.Server {
91-
this.$logger.info("\nSetting up proxy...\nPress Ctrl + C to terminate, or disconnect.\n");
92-
93-
let server = net.createServer({
94-
allowHalfOpen: true
95-
});
96-
97-
server.on("connection", (frontendSocket: net.Socket) => {
98-
this.$logger.info("Frontend client connected.");
99-
100-
frontendSocket.on("end", () => {
101-
this.$logger.info('Frontend socket closed!');
102-
if (!(this.$config.debugLivesync && this.$options.watch)) {
103-
process.exit(0);
104-
}
105-
});
106-
107-
socketFactory((backendSocket: net.Socket) => {
108-
this.$logger.info("Backend socket created.");
109-
110-
backendSocket.on("end", () => {
111-
this.$logger.info("Backend socket closed!");
112-
if (!(this.$config.debugLivesync && this.$options.watch)) {
113-
process.exit(0);
114-
}
115-
});
116-
117-
frontendSocket.on("close", () => {
118-
if (!(<any>backendSocket).destroyed) {
119-
backendSocket.destroy();
120-
}
121-
});
122-
backendSocket.on("close", () => {
123-
if (!(<any>frontendSocket).destroyed) {
124-
frontendSocket.destroy();
125-
}
126-
});
127-
128-
backendSocket.pipe(frontendSocket);
129-
frontendSocket.pipe(backendSocket);
130-
frontendSocket.resume();
131-
});
132-
});
133-
134-
let socketFileLocation = temp.path({ suffix: ".sock" });
135-
server.listen(socketFileLocation);
136-
if(!this.$options.client) {
137-
this.$logger.info("socket-file-location: " + socketFileLocation);
138-
}
139-
140-
return server;
141-
}
142128
}
143129
$injector.register("socketProxyFactory", SocketProxyFactory);

lib/options.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ export class Options extends commonOptionsLibPath.OptionsBase {
4141
teamId: { type: OptionType.String },
4242
rebuild: { type: OptionType.Boolean, default: true },
4343
syncAllFiles: { type: OptionType.Boolean },
44-
liveEdit: { type: OptionType.Boolean }
44+
liveEdit: { type: OptionType.Boolean },
45+
chrome: { type: OptionType.Boolean }
4546
},
4647
path.join($hostInfo.isWindows ? process.env.AppData : path.join(osenv.home(), ".local/share"), ".nativescript-cli"),
4748
$errors, $staticConfig);

lib/services/ios-debug-service.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class IOSDebugService implements IDebugService {
1414
private _lldbProcess: ChildProcess;
1515
private _sockets: net.Socket[] = [];
1616
private _childProcess: ChildProcess;
17-
private _socketProxy: net.Server;
17+
private _socketProxy: any;
1818

1919
constructor(
2020
private $config: IConfiguration,
@@ -30,7 +30,6 @@ class IOSDebugService implements IDebugService {
3030
private $injector: IInjector,
3131
private $npmInstallationManager: INpmInstallationManager,
3232
private $options: IOptions,
33-
private $projectDataService: IProjectDataService,
3433
private $utils: IUtils,
3534
private $iOSNotification: IiOSNotification,
3635
private $iOSSocketRequestExecutor: IiOSSocketRequestExecutor,
@@ -190,17 +189,24 @@ class IOSDebugService implements IDebugService {
190189

191190
private wireDebuggerClient(device?: Mobile.IiOSDevice): IFuture<void> {
192191
return (() => {
193-
this._socketProxy = this.$socketProxyFactory.createSocketProxy(() => {
192+
let factory = () => {
194193
let socket = device ? device.connectToPort(inspectorBackendPort) : net.connect(inspectorBackendPort);
195194
this._sockets.push(socket);
196195
return socket;
197-
}).wait();
196+
};
197+
198+
if (this.$options.chrome) {
199+
this._socketProxy = this.$socketProxyFactory.createWebSocketProxy(factory);
198200

199-
this.openDebuggerClient(<any>this._socketProxy.address()).wait();
201+
this.$logger.info(`To start debugging, open the following URL in Chrome:\nchrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=localhost:${this._socketProxy.options.port}\n`);
202+
} else {
203+
this._socketProxy = this.$socketProxyFactory.createTCPSocketProxy(factory);
204+
this.openAppInspector(this._socketProxy.address()).wait();
205+
}
200206
}).future<void>()();
201207
}
202208

203-
private openDebuggerClient(fileDescriptor: string): IFuture<void> {
209+
private openAppInspector(fileDescriptor: string): IFuture<void> {
204210
if (this.$options.client) {
205211
return (() => {
206212
let inspectorPath = this.$npmInstallationManager.install(inspectorNpmPackageName).wait();

0 commit comments

Comments
 (0)