microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/reactNativeDebugEntryPoint.ts
85lines · modeblame
40e4b177Patricio Beltran10 years ago | 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 | | |
b8999098Dmitry Zinovyev9 years ago | 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"; | |
e45838cbVladimir Kotikov9 years ago | 12 | import { makeAdapter, makeSession } from "./nodeDebugWrapper"; |
40e4b177Patricio Beltran10 years ago | 13 | |
| 14 | const version = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "package.json"), "utf-8")).version; | |
| 15 | const telemetryReporter = new ReassignableTelemetryReporter(new NullTelemetryReporter()); | |
e45838cbVladimir Kotikov9 years ago | 16 | const extensionName = "react-native-debug-adapter"; |
40e4b177Patricio Beltran10 years ago | 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 | | |
b8999098Dmitry Zinovyev9 years ago | 24 | function codeToRun() { |
| 25 | | |
| 26 | /** | |
| 27 | * For debugging React Native we basically want to debug node plus some other stuff. | |
| 28 | * There is no need to create a new adapter for node because ther already exists one. | |
| 29 | * We look for node debug adapter on client's computer so we can jump of on top of that. | |
| 30 | */ | |
| 31 | let nodeDebugFolder: string; | |
| 32 | let VSCodeDebugAdapter: typeof VSCodeDebugAdapterPackage; | |
| 33 | let Node2DebugAdapter: typeof Node2DebugAdapterPackage.Node2DebugAdapter; | |
| 34 | let ChromeDebuggerPackage: typeof ChromeDebuggerCorePackage; | |
40e4b177Patricio Beltran10 years ago | 35 | |
b8999098Dmitry Zinovyev9 years ago | 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 | VSCodeDebugAdapter = require(path.join(nodeDebugFolder, "node_modules/vscode-debugadapter")); | |
| 42 | ChromeDebuggerPackage = require(path.join(nodeDebugFolder, "node_modules/vscode-chrome-debug-core")); | |
| 43 | Node2DebugAdapter = require(path.join(nodeDebugFolder, "out/src/nodeDebugAdapter")).NodeDebugAdapter; | |
| 44 | /* tslint:enable:no-var-requires */ | |
| 45 | } catch (e) { | |
| 46 | // Nothing we can do here: can't even communicate back because we don't know how to speak debug adapter | |
| 47 | bailOut("cannotFindDebugAdapter"); | |
| 48 | } | |
40e4b177Patricio Beltran10 years ago | 49 | |
b8999098Dmitry Zinovyev9 years ago | 50 | /** |
| 51 | * We did find chrome debugger package and node2 debug adapter. Lets create debug | |
| 52 | * session and adapter with our customizations. | |
| 53 | */ | |
| 54 | let session: typeof ChromeDebuggerCorePackage.ChromeDebugSession; | |
| 55 | let adapter: typeof Node2DebugAdapterPackage.Node2DebugAdapter; | |
40e4b177Patricio Beltran10 years ago | 56 | |
b8999098Dmitry Zinovyev9 years ago | 57 | try { |
| 58 | /* Create customised react-native debug adapter based on Node-debug2 adapter */ | |
| 59 | adapter = makeAdapter(Node2DebugAdapter); | |
| 60 | // Create a debug session class based on ChromeDebugSession | |
| 61 | session = makeSession(ChromeDebuggerPackage.ChromeDebugSession, | |
| 62 | { adapter, extensionName }, VSCodeDebugAdapter, telemetryReporter, extensionName, version); | |
| 63 | } catch (e) { | |
| 64 | const debugSession = new VSCodeDebugAdapter.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 VSCodeDebugAdapter.OutputEvent("Unable to start debug adapter: " + e.toString(), "stderr")); | |
| 68 | debugSession.sendEvent(new VSCodeDebugAdapter.TerminatedEvent()); | |
| 69 | bailOut(e.toString()); | |
| 70 | } | |
40e4b177Patricio Beltran10 years ago | 71 | |
b8999098Dmitry Zinovyev9 years ago | 72 | // Run the debug session for the node debug adapter with our modified requests |
| 73 | ChromeDebuggerPackage.ChromeDebugSession.run(session); | |
| 74 | } | |
40e4b177Patricio Beltran10 years ago | 75 | |
b8999098Dmitry Zinovyev9 years ago | 76 | // Enable telemetry |
| 77 | const entryPointHandler = new EntryPointHandler(ProcessType.Debugger); | |
| 78 | entryPointHandler.runApp( | |
| 79 | extensionName, | |
| 80 | () => version, | |
| 81 | ErrorHelper.getInternalError(InternalErrorCode.DebuggingFailed), | |
| 82 | telemetryReporter, | |
| 83 | codeToRun | |
| 84 | ); | |
e45838cbVladimir Kotikov9 years ago | 85 | |