microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/reactNativeDebugEntryPoint.ts
87lines · 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 { makeAdapter, makeSession } from "./nodeDebugWrapper"; |
| 13 | |
| 14 | import { ChromeDebugSession, ChromeDebugAdapter } from "vscode-chrome-debug-core"; |
| 15 | import { DebugSession, OutputEvent, TerminatedEvent } from "vscode-debugadapter"; |
| 16 | |
| 17 | const version = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "package.json"), "utf-8")).version; |
| 18 | const telemetryReporter = new ReassignableTelemetryReporter(new NullTelemetryReporter()); |
| 19 | const extensionName = "react-native-tools"; |
| 20 | import * as nls from "vscode-nls"; |
| 21 | const localize = nls.loadMessageBundle(); |
| 22 | |
| 23 | function bailOut(reason: string): void { |
| 24 | // Things have gone wrong in initialization: Report the error to telemetry and exit |
| 25 | TelemetryHelper.sendSimpleEvent(reason); |
| 26 | process.exit(1); |
| 27 | } |
| 28 | |
| 29 | function codeToRun() { |
| 30 | |
| 31 | /** |
| 32 | * For debugging React Native we basically want to debug node plus some other stuff. |
| 33 | * There is no need to create a new adapter for node because ther already exists one. |
| 34 | * We look for node debug adapter on client's computer so we can jump of on top of that. |
| 35 | */ |
| 36 | let nodeDebugFolder: string; |
| 37 | let Node2DebugAdapter: typeof ChromeDebugAdapter; |
| 38 | |
| 39 | // nodeDebugLocation.json is dynamically generated on extension activation. |
| 40 | // If it fails, we must not have been in a react native project |
| 41 | try { |
| 42 | /* tslint:disable:no-var-requires */ |
| 43 | nodeDebugFolder = require("./nodeDebugLocation.json").nodeDebugPath; |
| 44 | Node2DebugAdapter = require(path.join(nodeDebugFolder, "out/src/nodeDebugAdapter")).NodeDebugAdapter; |
| 45 | /* tslint:enable:no-var-requires */ |
| 46 | |
| 47 | /** |
| 48 | * We did find chrome debugger package and node2 debug adapter. Lets create debug |
| 49 | * session and adapter with our customizations. |
| 50 | */ |
| 51 | let session: typeof ChromeDebugSession; |
| 52 | let adapter: typeof ChromeDebugAdapter; |
| 53 | |
| 54 | try { |
| 55 | /* Create customised react-native debug adapter based on Node-debug2 adapter */ |
| 56 | adapter = makeAdapter(Node2DebugAdapter); |
| 57 | // Create a debug session class based on ChromeDebugSession |
| 58 | session = makeSession(ChromeDebugSession, |
| 59 | { adapter, extensionName }, telemetryReporter, extensionName, version); |
| 60 | |
| 61 | // Run the debug session for the node debug adapter with our modified requests |
| 62 | ChromeDebugSession.run(session); |
| 63 | } catch (e) { |
| 64 | const debugSession = new DebugSession(); |
| 65 | // Start session before sending any events otherwise the client wouldn't receive them |
| 66 | debugSession.start(process.stdin, process.stdout); |
| 67 | debugSession.sendEvent(new OutputEvent(localize("UnableToStartDebugAdapter", "Unable to start debug adapter: {0}", e.toString()), "stderr")); |
| 68 | debugSession.sendEvent(new TerminatedEvent()); |
| 69 | bailOut(e.toString()); |
| 70 | } |
| 71 | } catch (e) { |
| 72 | // Nothing we can do here: can't even communicate back because we don't know how to speak debug adapter |
| 73 | bailOut("cannotFindDebugAdapter"); |
| 74 | } |
| 75 | |
| 76 | } |
| 77 | |
| 78 | // Enable telemetry |
| 79 | const entryPointHandler = new EntryPointHandler(ProcessType.Debugger); |
| 80 | entryPointHandler.runApp( |
| 81 | extensionName, |
| 82 | version, |
| 83 | ErrorHelper.getInternalError(InternalErrorCode.DebuggingFailed), |
| 84 | telemetryReporter, |
| 85 | codeToRun |
| 86 | ); |
| 87 | |
| 88 | |