microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/nodeDebugWrapper.ts
272lines · 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 | |
| 4 | import * as Q from "q"; |
| 5 | import * as path from "path"; |
| 6 | import * as http from "http"; |
| 7 | import * as fs from "fs"; |
| 8 | import stripJsonComments = require("strip-json-comments"); |
| 9 | |
| 10 | import {Telemetry} from "../common/telemetry"; |
| 11 | import {TelemetryHelper} from "../common/telemetryHelper"; |
| 12 | import {RemoteExtension} from "../common/remoteExtension"; |
| 13 | import {IOSPlatform} from "./ios/iOSPlatform"; |
| 14 | import {PlatformResolver} from "./platformResolver"; |
| 15 | import {IRunOptions} from "../common/launchArgs"; |
| 16 | import {TargetPlatformHelper} from "../common/targetPlatformHelper"; |
| 17 | import {ExtensionTelemetryReporter, ReassignableTelemetryReporter} from "../common/telemetryReporters"; |
| 18 | import {NodeDebugAdapterLogger} from "../common/log/loggers"; |
| 19 | import {Log} from "../common/log/log"; |
| 20 | import {GeneralMobilePlatform} from "../common/generalMobilePlatform"; |
| 21 | |
| 22 | export class NodeDebugWrapper { |
| 23 | private projectRootPath: string; |
| 24 | private remoteExtension: RemoteExtension; |
| 25 | private telemetryReporter: ReassignableTelemetryReporter; |
| 26 | private appName: string; |
| 27 | private version: string; |
| 28 | private mobilePlatformOptions: IRunOptions; |
| 29 | |
| 30 | private vscodeDebugAdapterPackage: typeof VSCodeDebugAdapter; |
| 31 | private nodeDebugSession: typeof NodeDebugSession; |
| 32 | private sourceMapsConstructor: typeof SourceMaps; |
| 33 | private originalLaunchRequest: (response: any, args: any) => void; |
| 34 | |
| 35 | public constructor(appName: string, version: string, telemetryReporter: ReassignableTelemetryReporter, |
| 36 | debugAdapter: typeof VSCodeDebugAdapter, debugSession: typeof NodeDebugSession, sourceMaps: typeof SourceMaps) { |
| 37 | |
| 38 | this.appName = appName; |
| 39 | this.version = version; |
| 40 | this.telemetryReporter = telemetryReporter; |
| 41 | this.vscodeDebugAdapterPackage = debugAdapter; |
| 42 | this.nodeDebugSession = debugSession; |
| 43 | this.sourceMapsConstructor = sourceMaps; |
| 44 | this.originalLaunchRequest = this.nodeDebugSession.prototype.launchRequest; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Calls customize methods for all requests needed |
| 49 | */ |
| 50 | public customizeNodeAdapterRequests(): void { |
| 51 | this.customizeLaunchRequest(); |
| 52 | this.customizeAttachRequest(); |
| 53 | this.customizeDisconnectRequest(); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Intecept the "launchRequest" instance method of NodeDebugSession to interpret arguments. |
| 58 | * Launch should: |
| 59 | * - Run the packager if needed |
| 60 | * - Compile and run application |
| 61 | * - Prewarm bundle |
| 62 | */ |
| 63 | private customizeLaunchRequest(): void { |
| 64 | const nodeDebugWrapper = this; |
| 65 | this.nodeDebugSession.prototype.launchRequest = function (request: any, args: ILaunchRequestArgs) { |
| 66 | nodeDebugWrapper.requestSetup(this, args); |
| 67 | nodeDebugWrapper.mobilePlatformOptions.target = args.target || "simulator"; |
| 68 | nodeDebugWrapper.mobilePlatformOptions.iosRelativeProjectPath = !nodeDebugWrapper.isNullOrUndefined(args.iosRelativeProjectPath) ? |
| 69 | args.iosRelativeProjectPath : |
| 70 | IOSPlatform.DEFAULT_IOS_PROJECT_RELATIVE_PATH; |
| 71 | |
| 72 | // We add the parameter if it's defined (adapter crashes otherwise) |
| 73 | if (!nodeDebugWrapper.isNullOrUndefined(args.logCatArguments)) { |
| 74 | nodeDebugWrapper.mobilePlatformOptions.logCatArguments = [nodeDebugWrapper.parseLogCatArguments(args.logCatArguments)]; |
| 75 | } |
| 76 | |
| 77 | return TelemetryHelper.generate("launch", (generator) => { |
| 78 | const resolver = new PlatformResolver(); |
| 79 | return nodeDebugWrapper.remoteExtension.getPackagerPort() |
| 80 | .then(packagerPort => { |
| 81 | nodeDebugWrapper.mobilePlatformOptions.packagerPort = packagerPort; |
| 82 | const mobilePlatform = resolver.resolveMobilePlatform(args.platform, nodeDebugWrapper.mobilePlatformOptions); |
| 83 | return Q({}) |
| 84 | .then(() => { |
| 85 | generator.step("checkPlatformCompatibility"); |
| 86 | TargetPlatformHelper.checkTargetPlatformSupport(nodeDebugWrapper.mobilePlatformOptions.platform); |
| 87 | generator.step("startPackager"); |
| 88 | return mobilePlatform.startPackager(); |
| 89 | }) |
| 90 | .then(() => { |
| 91 | // 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 |
| 92 | // and the user needs to Reload JS manually. We prewarm it to prevent that issue |
| 93 | generator.step("prewarmBundleCache"); |
| 94 | Log.logMessage("Prewarming bundle cache. This may take a while ..."); |
| 95 | return mobilePlatform.prewarmBundleCache(); |
| 96 | }) |
| 97 | .then(() => { |
| 98 | generator.step("mobilePlatform.runApp"); |
| 99 | Log.logMessage("Building and running application."); |
| 100 | return mobilePlatform.runApp(); |
| 101 | }) |
| 102 | .then(() => |
| 103 | nodeDebugWrapper.attachRequest(this, request, args, mobilePlatform)); |
| 104 | }).catch(error => |
| 105 | nodeDebugWrapper.bailOut(this, error.message)); |
| 106 | }); |
| 107 | }; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Intecept the "attachRequest" instance method of NodeDebugSession to interpret arguments |
| 112 | */ |
| 113 | private customizeAttachRequest(): void { |
| 114 | const nodeDebugWrapper = this; |
| 115 | this.nodeDebugSession.prototype.attachRequest = function (request: any, args: IAttachRequestArgs) { |
| 116 | nodeDebugWrapper.requestSetup(this, args); |
| 117 | nodeDebugWrapper.attachRequest(this, request, args, new GeneralMobilePlatform(nodeDebugWrapper.mobilePlatformOptions)); |
| 118 | }; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Intecept the "disconnectRequest" instance method of NodeDebugSession to interpret arguments |
| 123 | */ |
| 124 | private customizeDisconnectRequest(): void { |
| 125 | const originalRequest = this.nodeDebugSession.prototype.disconnectRequest; |
| 126 | const nodeDebugWrapper = this; |
| 127 | |
| 128 | this.nodeDebugSession.prototype.disconnectRequest = function (response: any, args: any): void { |
| 129 | // First we tell the extension to stop monitoring the logcat, and then we disconnect the debugging session |
| 130 | |
| 131 | if (nodeDebugWrapper.mobilePlatformOptions.platform === "android") { |
| 132 | nodeDebugWrapper.remoteExtension.stopMonitoringLogcat() |
| 133 | .catch(reason => |
| 134 | Log.logError(`WARNING: Couldn't stop monitoring logcat: ${reason.message || reason}\n`)) |
| 135 | .finally(() => |
| 136 | originalRequest.call(this, response, args)); |
| 137 | } else { |
| 138 | originalRequest.call(this, response, args); |
| 139 | } |
| 140 | }; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Makes the required setup for request customization |
| 145 | * - Enables telemetry |
| 146 | * - Sets up mobilePlatformOptions, remote extension and projectRootPath |
| 147 | * - Starts debug server |
| 148 | * - Create global logger |
| 149 | */ |
| 150 | private requestSetup(debugSession: NodeDebugSession, args: any) { |
| 151 | this.projectRootPath = this.getProjectRoot(args); |
| 152 | this.remoteExtension = RemoteExtension.atProjectRootPath(this.projectRootPath); |
| 153 | this.mobilePlatformOptions = { |
| 154 | projectRoot: this.projectRootPath, |
| 155 | platform: args.platform, |
| 156 | }; |
| 157 | |
| 158 | // Start to send telemetry |
| 159 | this.telemetryReporter.reassignTo(new ExtensionTelemetryReporter( |
| 160 | this.appName, this.version, Telemetry.APPINSIGHTS_INSTRUMENTATIONKEY, this.projectRootPath)); |
| 161 | |
| 162 | // Create a server waiting for messages to re-initialize the debug session; |
| 163 | const debugServerListeningPort = this.createReinitializeServer(debugSession, args.internalDebuggerPort, args.outDir); |
| 164 | args.args = [debugServerListeningPort.toString()]; |
| 165 | |
| 166 | Log.SetGlobalLogger(new NodeDebugAdapterLogger(this.vscodeDebugAdapterPackage, debugSession)); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Runs logic needed to attach. |
| 171 | * Attach should: |
| 172 | * - Enable js debugging |
| 173 | */ |
| 174 | private attachRequest(debugSession: NodeDebugSession, request: any, args: any, mobilePlatform: any): Q.Promise<void> { |
| 175 | return TelemetryHelper.generate("attach", (generator) => { |
| 176 | return Q({}) |
| 177 | .then(() => { |
| 178 | generator.step("mobilePlatform.enableJSDebuggingMode"); |
| 179 | if (mobilePlatform) { |
| 180 | return mobilePlatform.enableJSDebuggingMode(); |
| 181 | } else { |
| 182 | Log.logMessage("Debugger ready. Enable remote debugging in app."); |
| 183 | } |
| 184 | }).then(() => |
| 185 | this.originalLaunchRequest.call(debugSession, request, args)) |
| 186 | .catch(error => |
| 187 | this.bailOut(debugSession, error.message)); |
| 188 | }); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Creates internal debug server and returns the port that the server is hook up into. |
| 193 | */ |
| 194 | private createReinitializeServer(debugSession: NodeDebugSession, internalDebuggerPort: string, sourcesDir: string): number { |
| 195 | // Create the server |
| 196 | const server = http.createServer((req, res) => { |
| 197 | res.statusCode = 404; |
| 198 | if (req.url === "/refreshBreakpoints") { |
| 199 | res.statusCode = 200; |
| 200 | if (debugSession) { |
| 201 | const sourceMaps = debugSession._sourceMaps; |
| 202 | if (sourceMaps) { |
| 203 | // Flush any cached source maps |
| 204 | // Rather than cleaning internal caches we recreate |
| 205 | // SourceMaps to add downloaded bundle map to cache |
| 206 | const bundlePattern = path.join(sourcesDir, "*.bundle"); |
| 207 | const sourceMaps = new this.sourceMapsConstructor(debugSession, sourcesDir, [bundlePattern]); |
| 208 | debugSession._sourceMaps = sourceMaps; |
| 209 | } |
| 210 | // Send an "initialized" event to trigger breakpoints to be re-sent |
| 211 | debugSession.sendEvent(new this.vscodeDebugAdapterPackage.InitializedEvent()); |
| 212 | } |
| 213 | } |
| 214 | res.end(); |
| 215 | }); |
| 216 | |
| 217 | // Setup listen port and on error response |
| 218 | const port = parseInt(internalDebuggerPort, 10) || 9090; |
| 219 | |
| 220 | server.listen(port); |
| 221 | server.on("error", (err: Error) => { |
| 222 | TelemetryHelper.sendSimpleEvent("reinitializeServerError"); |
| 223 | Log.logError("Error in debug adapter server: " + err.toString()); |
| 224 | Log.logMessage("Breakpoints may not update. Consider restarting and specifying a different 'internalDebuggerPort' in launch.json"); |
| 225 | }); |
| 226 | |
| 227 | // Return listen port |
| 228 | return port; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Logs error to user and finishes the debugging process. |
| 233 | */ |
| 234 | private bailOut(debugSession: NodeDebugSession, message: string): void { |
| 235 | Log.logError(`Could not debug. ${message}`); |
| 236 | debugSession.sendEvent(new this.vscodeDebugAdapterPackage.TerminatedEvent()); |
| 237 | process.exit(1); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Parses log cat arguments to a string |
| 242 | */ |
| 243 | private parseLogCatArguments(userProvidedLogCatArguments: any): string { |
| 244 | return Array.isArray(userProvidedLogCatArguments) |
| 245 | ? userProvidedLogCatArguments.join(" ") // If it's an array, we join the arguments |
| 246 | : userProvidedLogCatArguments; // If not, we leave it as-is |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Helper method to know if a value is either null or undefined |
| 251 | */ |
| 252 | private isNullOrUndefined(value: any): boolean { |
| 253 | return typeof value === "undefined" || value === null; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Parses settings.json file for workspace root property |
| 258 | */ |
| 259 | private getProjectRoot(args: any): string { |
| 260 | try { |
| 261 | let vsCodeRoot = path.resolve(args.program, "../.."); |
| 262 | let settingsPath = path.resolve(vsCodeRoot, ".vscode/settings.json"); |
| 263 | let settingsContent = fs.readFileSync(settingsPath, "utf8"); |
| 264 | settingsContent = stripJsonComments(settingsContent); |
| 265 | let parsedSettings = JSON.parse(settingsContent); |
| 266 | let projectRootPath = parsedSettings["react-native-tools"].projectRoot; |
| 267 | return path.resolve(vsCodeRoot, projectRootPath); |
| 268 | } catch (e) { |
| 269 | return path.resolve(args.program, "../.."); |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |