microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/reactNativeDebugEntryPoint.ts
93lines · 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 fs from "fs"; |
| 5 | import * as path from "path"; |
| 6 | |
| 7 | import {TelemetryHelper} from "../common/telemetryHelper"; |
| 8 | import {EntryPointHandler, ProcessType} from "../common/entryPointHandler"; |
| 9 | import {ErrorHelper} from "../common/error/errorHelper"; |
| 10 | import {InternalErrorCode} from "../common/error/internalErrorCode"; |
| 11 | import {NullTelemetryReporter, ReassignableTelemetryReporter} from "../common/telemetryReporters"; |
| 12 | import {NodeDebugWrapper} from "./nodeDebugWrapper"; |
| 13 | |
| 14 | const version = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "package.json"), "utf-8")).version; |
| 15 | const telemetryReporter = new ReassignableTelemetryReporter(new NullTelemetryReporter()); |
| 16 | const appName = "react-native-debug-adapter"; |
| 17 | |
| 18 | function bailOut(reason: string): void { |
| 19 | // Things have gone wrong in initialization: Report the error to telemetry and exit |
| 20 | TelemetryHelper.sendSimpleEvent(reason); |
| 21 | process.exit(1); |
| 22 | } |
| 23 | |
| 24 | // Enable telemetry |
| 25 | new EntryPointHandler(ProcessType.Debugger).runApp(appName, () => version, |
| 26 | ErrorHelper.getInternalError(InternalErrorCode.DebuggingFailed), telemetryReporter, () => { |
| 27 | |
| 28 | /** |
| 29 | * For debugging React Native we basically want to debug node plus some other stuff. |
| 30 | * There is no need to create a new adapter for node because ther already exists one. |
| 31 | * We look for node debug adapter on client's computer so we can jump of on top of that. |
| 32 | */ |
| 33 | let nodeDebugFolder: string; |
| 34 | let vscodeDebugAdapterPackage: typeof VSCodeDebugAdapter; |
| 35 | |
| 36 | // nodeDebugLocation.json is dynamically generated on extension activation. |
| 37 | // If it fails, we must not have been in a react native project |
| 38 | try { |
| 39 | /* tslint:disable:no-var-requires */ |
| 40 | nodeDebugFolder = require("./nodeDebugLocation.json").nodeDebugPath; |
| 41 | vscodeDebugAdapterPackage = require(path.join(nodeDebugFolder, "node_modules", "vscode-debugadapter")); |
| 42 | /* tslint:enable:no-var-requires */ |
| 43 | } catch (e) { |
| 44 | // Nothing we can do here: can't even communicate back because we don't know how to speak debug adapter |
| 45 | bailOut("cannotFindDebugAdapter"); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * We did find node debug adapter. Lets get the debugSession from it. |
| 50 | * And add our customizations to the requests. |
| 51 | */ |
| 52 | |
| 53 | // Temporarily dummy out the DebugSession.run function so we do not start the debug adapter until we are ready |
| 54 | const originalDebugSessionRun = vscodeDebugAdapterPackage.DebugSession.run; |
| 55 | vscodeDebugAdapterPackage.DebugSession.run = () => { }; |
| 56 | |
| 57 | let nodeDebug: { NodeDebugSession: typeof NodeDebugSession }; |
| 58 | let sourceMaps: { SourceMaps: typeof SourceMaps }; |
| 59 | |
| 60 | try { |
| 61 | /* tslint:disable:no-var-requires */ |
| 62 | nodeDebug = require(path.join(nodeDebugFolder, "out", "node", "nodeDebug")); |
| 63 | sourceMaps = require(path.join(nodeDebugFolder, "out", "node", "sourceMaps")); |
| 64 | /* tslint:enable:no-var-requires */ |
| 65 | } catch (e) { |
| 66 | // Unable to find nodeDebug, but we can make our own communication channel now |
| 67 | const debugSession = new vscodeDebugAdapterPackage.DebugSession(); |
| 68 | // Note: this will not work in the context of debugging the debug adapter and communicating over a socket, |
| 69 | // but in that case we have much better ways to investigate errors. |
| 70 | debugSession.start(process.stdin, process.stdout); |
| 71 | debugSession.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Unable to start debug adapter: " + e.toString(), "stderr")); |
| 72 | debugSession.sendEvent(new vscodeDebugAdapterPackage.TerminatedEvent()); |
| 73 | |
| 74 | bailOut("cannotFindNodeDebugAdapter"); |
| 75 | } |
| 76 | |
| 77 | vscodeDebugAdapterPackage.DebugSession.run = originalDebugSessionRun; |
| 78 | |
| 79 | // Customize node adapter requests |
| 80 | try { |
| 81 | let nodeDebugWrapper = new NodeDebugWrapper(appName, version, telemetryReporter, |
| 82 | vscodeDebugAdapterPackage, nodeDebug.NodeDebugSession, sourceMaps.SourceMaps); |
| 83 | nodeDebugWrapper.customizeNodeAdapterRequests(); |
| 84 | } catch (e) { |
| 85 | const debugSession = new vscodeDebugAdapterPackage.DebugSession(); |
| 86 | debugSession.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Unable to start debug adapter: " + e.toString(), "stderr")); |
| 87 | debugSession.sendEvent(new vscodeDebugAdapterPackage.TerminatedEvent()); |
| 88 | bailOut(e.toString()); |
| 89 | } |
| 90 | |
| 91 | // Run the debug session for the node debug adapter with our modified requests |
| 92 | vscodeDebugAdapterPackage.DebugSession.run(nodeDebug.NodeDebugSession); |
| 93 | }); |
| 94 | |