microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
14ebf4e6fdee5f69a41d9a7deea4dc164dd28b7e

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/extension/extensionServer.ts

274lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import * as Q from "q";
5import * as vscode from "vscode";
6
7import {MessagingHelper}from "../common/extensionMessaging";
8import {OutputChannelLogger} from "./log/OutputChannelLogger";
9import {Packager} from "../common/packager";
10import {LogCatMonitor} from "./android/logCatMonitor";
11import {FileSystem} from "../common/node/fileSystem";
12import {SettingsHelper} from "./settingsHelper";
13import {Telemetry} from "../common/telemetry";
14import {PlatformResolver} from "./platformResolver";
15import {TelemetryHelper} from "../common/telemetryHelper";
16import {TargetPlatformHelper} from "../common/targetPlatformHelper";
17import {MobilePlatformDeps} from "./generalMobilePlatform";
18import {IRemoteExtension} from "../common/remoteExtension";
19import * as rpc from "noice-json-rpc";
20import * as WebSocket from "ws";
21import WebSocketServer = WebSocket.Server;
22
23export class ExtensionServer implements vscode.Disposable {
24 public api: IRemoteExtension;
25 public isDisposed: boolean = false;
26 private serverInstance: WebSocketServer | null;
27 private reactNativePackager: Packager;
28 private pipePath: string;
29 private logCatMonitor: LogCatMonitor | null = null;
30 private logger: OutputChannelLogger = OutputChannelLogger.getMainChannel();
31
32 public constructor(projectRootPath: string, reactNativePackager: Packager) {
33 this.pipePath = MessagingHelper.getPath(projectRootPath);
34 this.reactNativePackager = reactNativePackager;
35 }
36
37 /**
38 * Starts the server.
39 */
40 public setup(): Q.Promise<void> {
41 this.isDisposed = false;
42
43 return Q.Promise((resolve, reject) => {
44 this._setup(resolve, reject);
45 });
46 }
47
48 /**
49 * Stops the server.
50 */
51 public dispose(): void {
52 this.isDisposed = true;
53 if (this.serverInstance) {
54 this.serverInstance.close();
55 this.serverInstance = null;
56 }
57
58 this.reactNativePackager.statusIndicator.dispose();
59 this.reactNativePackager.stop(true);
60 this.stopMonitoringLogCat();
61 }
62
63 private _setup(resolve: (val: void | Q.IPromise<void>) => void, reject: (reason: any) => void): void {
64 const errorCallback = this.recoverServer.bind(this, resolve, reject);
65 let launchCallback = (done: (val: void | Q.IPromise<void>) => void) => {
66 this.logger.debug(`Extension messaging server started at ${this.pipePath}.`);
67
68 if (this.serverInstance) {
69 this.serverInstance.removeListener("error", errorCallback);
70 this.serverInstance.on("error", this.recoverServer.bind(this, null, null));
71 }
72 done(void 0);
73 };
74
75 this.serverInstance = new WebSocketServer({port: <any>this.pipePath});
76 this.api = new rpc.Server(this.serverInstance).api();
77 this.serverInstance.on("listening", launchCallback.bind(this, resolve));
78 this.serverInstance.on("error", errorCallback);
79
80 this.setupApiHandlers();
81 }
82
83 private setupApiHandlers(): void {
84 let methods: any = {};
85 methods.stopMonitoringLogCat = this.stopMonitoringLogCat.bind(this);
86 methods.getPackagerPort = this.getPackagerPort.bind(this);
87 methods.sendTelemetry = this.sendTelemetry.bind(this);
88 methods.openFileAtLocation = this.openFileAtLocation.bind(this);
89 methods.showInformationMessage = this.showInformationMessage.bind(this);
90 methods.launch = this.launch.bind(this);
91 methods.showDevMenu = this.showDevMenu.bind(this);
92 methods.reloadApp = this.reloadApp.bind(this);
93
94 this.api.Extension.expose(methods);
95 }
96
97 private showDevMenu(deviceId?: string) {
98 this.api.Debugger.emitShowDevMenu(deviceId);
99 }
100
101 private reloadApp(deviceId?: string) {
102 this.api.Debugger.emitReloadApp(deviceId);
103 }
104
105 /**
106 * Recovers the server in case the named socket we use already exists, but no other instance of VSCode is active.
107 */
108 private recoverServer(resolve: (value: void) => {} , reject: (reason: any) => {}, error: any): void {
109 let errorHandler = (e: any) => {
110 /* The named socket is not used. */
111 if (e.code === "ECONNREFUSED") {
112 new FileSystem().removePathRecursivelyAsync(this.pipePath)
113 .then(() => {
114 if (resolve && reject) {
115 return this._setup(resolve, reject);
116 } else {
117 return this.setup();
118 }
119 })
120 .done();
121 }
122 };
123
124 /* The named socket already exists. */
125 if (error.code === "EADDRINUSE") {
126 let clientSocket = new WebSocket(`ws+unix://${this.pipePath}`);
127 clientSocket.on("error", errorHandler);
128 clientSocket.on("open", function() {
129 clientSocket.close();
130 });
131 }
132 }
133
134 /**
135 * Message handler for GET_PACKAGER_PORT.
136 */
137 private getPackagerPort(program: string): number {
138 return SettingsHelper.getPackagerPort(program);
139 }
140
141 /**
142 * Message handler for OPEN_FILE_AT_LOCATION
143 */
144 private openFileAtLocation(filename: string, lineNumber: number): Promise<void> {
145 return new Promise((resolve) => {
146 vscode.workspace.openTextDocument(vscode.Uri.file(filename))
147 .then((document: vscode.TextDocument) => {
148 vscode.window.showTextDocument(document)
149 .then((editor: vscode.TextEditor) => {
150 let range = editor.document.lineAt(lineNumber - 1).range;
151 editor.selection = new vscode.Selection(range.start, range.end);
152 editor.revealRange(range, vscode.TextEditorRevealType.InCenter);
153 resolve();
154 });
155 });
156 });
157 }
158
159 private stopMonitoringLogCat(): void {
160 if (this.logCatMonitor) {
161 this.logCatMonitor.dispose();
162 this.logCatMonitor = null;
163 }
164 }
165
166 /**
167 * Sends telemetry
168 */
169 private sendTelemetry(extensionId: string, extensionVersion: string, appInsightsKey: string, eventName: string, properties: {[key: string]: string}, measures: {[key: string]: number}): void {
170 Telemetry.sendExtensionTelemetry(extensionId, extensionVersion, appInsightsKey, eventName, properties, measures);
171 }
172
173 /**
174 * Message handler for SHOW_INFORMATION_MESSAGE
175 */
176 private showInformationMessage(message: string): void {
177 vscode.window.showInformationMessage(message);
178 }
179
180 private launch(request: any): Promise<any> {
181 let mobilePlatformOptions = requestSetup(request.arguments);
182
183 // We add the parameter if it's defined (adapter crashes otherwise)
184 if (!isNullOrUndefined(request.arguments.logCatArguments)) {
185 mobilePlatformOptions.logCatArguments = [parseLogCatArguments(request.arguments.logCatArguments)];
186 }
187
188 if (!isNullOrUndefined(request.arguments.variant)) {
189 mobilePlatformOptions.variant = request.arguments.variant;
190 }
191
192 if (!isNullOrUndefined(request.arguments.scheme)) {
193 mobilePlatformOptions.scheme = request.arguments.scheme;
194 }
195
196 mobilePlatformOptions.packagerPort = SettingsHelper.getPackagerPort(request.arguments.program);
197 const platformDeps: MobilePlatformDeps = {
198 packager: this.reactNativePackager,
199 };
200 const mobilePlatform = new PlatformResolver()
201 .resolveMobilePlatform(request.arguments.platform, mobilePlatformOptions, platformDeps);
202 return new Promise((resolve, reject) => {
203 TelemetryHelper.generate("launch", (generator) => {
204 generator.step("checkPlatformCompatibility");
205 TargetPlatformHelper.checkTargetPlatformSupport(mobilePlatformOptions.platform);
206 generator.step("startPackager");
207 return mobilePlatform.startPackager()
208 .then(() => {
209 // We've seen that if we don't prewarm the bundle cache, the app fails on the first attempt to connect to the debugger logic
210 // and the user needs to Reload JS manually. We prewarm it to prevent that issue
211 generator.step("prewarmBundleCache");
212 this.logger.info("Prewarming bundle cache. This may take a while ...");
213 return mobilePlatform.prewarmBundleCache();
214 })
215 .then(() => {
216 generator.step("mobilePlatform.runApp").add("target", mobilePlatformOptions.target, false);
217 this.logger.info("Building and running application.");
218 return mobilePlatform.runApp();
219 })
220 .then(() => {
221 generator.step("mobilePlatform.enableJSDebuggingMode");
222 this.logger.info("Enable JS Debugging");
223 return mobilePlatform.enableJSDebuggingMode();
224 })
225 .then(() => {
226 resolve();
227 })
228 .catch(error => {
229 this.logger.error(error);
230 reject(error);
231 });
232 });
233 });
234 }
235}
236
237/**
238 * Parses log cat arguments to a string
239 */
240function parseLogCatArguments(userProvidedLogCatArguments: any): string {
241 return Array.isArray(userProvidedLogCatArguments)
242 ? userProvidedLogCatArguments.join(" ") // If it's an array, we join the arguments
243 : userProvidedLogCatArguments; // If not, we leave it as-is
244}
245
246function isNullOrUndefined(value: any): boolean {
247 return typeof value === "undefined" || value === null;
248}
249
250function requestSetup(args: any): any {
251 const workspaceFolder: vscode.WorkspaceFolder = <vscode.WorkspaceFolder>vscode.workspace.getWorkspaceFolder(vscode.Uri.file(args.program));
252 const projectRootPath = getProjectRoot(args);
253 let mobilePlatformOptions: any = {
254 workspaceRoot: workspaceFolder.uri.fsPath,
255 projectRoot: projectRootPath,
256 platform: args.platform,
257 env: args.env,
258 envFile: args.envFile,
259 target: args.target || "simulator",
260 };
261
262 if (!args.runArguments) {
263 let runArgs = SettingsHelper.getRunArgs(args.platform, args.target || "simulator", workspaceFolder.uri);
264 mobilePlatformOptions.runArguments = runArgs;
265 } else {
266 mobilePlatformOptions.runArguments = args.runArguments;
267 }
268
269 return mobilePlatformOptions;
270}
271
272function getProjectRoot(args: any): string {
273 return SettingsHelper.getReactNativeProjectRoot(args.program);
274}
275