microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/reactNativeDebugEntryPoint.ts
87lines · 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 | |
0a68f8dbArtem Egorov8 years ago | 14 | import { ChromeDebugSession, ChromeDebugAdapter } from "vscode-chrome-debug-core"; |
| 15 | import { DebugSession, OutputEvent, TerminatedEvent } from "vscode-debugadapter"; | |
| 16 | | |
40e4b177Patricio Beltran10 years ago | 17 | const version = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "package.json"), "utf-8")).version; |
| 18 | const telemetryReporter = new ReassignableTelemetryReporter(new NullTelemetryReporter()); | |
51a4641cArtem Egorov7 years ago | 19 | const extensionName = "react-native-tools"; |
d124bf0eYuri Skorokhodov7 years ago | 20 | import * as nls from "vscode-nls"; |
| 21 | const localize = nls.loadMessageBundle(); | |
40e4b177Patricio Beltran10 years ago | 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 | | |
b8999098Dmitry Zinovyev9 years ago | 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; | |
0a68f8dbArtem Egorov8 years ago | 37 | let Node2DebugAdapter: typeof ChromeDebugAdapter; |
40e4b177Patricio Beltran10 years ago | 38 | |
b8999098Dmitry Zinovyev9 years ago | 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 */ | |
40e4b177Patricio Beltran10 years ago | 46 | |
5c8365a6Artem Egorov8 years ago | 47 | /** |
| 48 | * We did find chrome debugger package and node2 debug adapter. Lets create debug | |
| 49 | * session and adapter with our customizations. | |
| 50 | */ | |
0a68f8dbArtem Egorov8 years ago | 51 | let session: typeof ChromeDebugSession; |
| 52 | let adapter: typeof ChromeDebugAdapter; | |
40e4b177Patricio Beltran10 years ago | 53 | |
5c8365a6Artem Egorov8 years ago | 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 | |
0a68f8dbArtem Egorov8 years ago | 58 | session = makeSession(ChromeDebugSession, |
| 59 | { adapter, extensionName }, telemetryReporter, extensionName, version); | |
5c8365a6Artem Egorov8 years ago | 60 | |
| 61 | // Run the debug session for the node debug adapter with our modified requests | |
0a68f8dbArtem Egorov8 years ago | 62 | ChromeDebugSession.run(session); |
5c8365a6Artem Egorov8 years ago | 63 | } catch (e) { |
0a68f8dbArtem Egorov8 years ago | 64 | const debugSession = new DebugSession(); |
5c8365a6Artem Egorov8 years ago | 65 | // Start session before sending any events otherwise the client wouldn't receive them |
| 66 | debugSession.start(process.stdin, process.stdout); | |
d124bf0eYuri Skorokhodov7 years ago | 67 | debugSession.sendEvent(new OutputEvent(localize("UnableToStartDebugAdapter", "Unable to start debug adapter: {0}", e.toString()), "stderr")); |
0a68f8dbArtem Egorov8 years ago | 68 | debugSession.sendEvent(new TerminatedEvent()); |
5c8365a6Artem Egorov8 years ago | 69 | bailOut(e.toString()); |
| 70 | } | |
b8999098Dmitry Zinovyev9 years ago | 71 | } catch (e) { |
5c8365a6Artem Egorov8 years ago | 72 | // Nothing we can do here: can't even communicate back because we don't know how to speak debug adapter |
| 73 | bailOut("cannotFindDebugAdapter"); | |
b8999098Dmitry Zinovyev9 years ago | 74 | } |
40e4b177Patricio Beltran10 years ago | 75 | |
b8999098Dmitry Zinovyev9 years ago | 76 | } |
40e4b177Patricio Beltran10 years ago | 77 | |
b8999098Dmitry Zinovyev9 years ago | 78 | // Enable telemetry |
| 79 | const entryPointHandler = new EntryPointHandler(ProcessType.Debugger); | |
| 80 | entryPointHandler.runApp( | |
c9c3d133Artem Egorov8 years ago | 81 | extensionName, |
2e432a9eArtem Egorov8 years ago | 82 | version, |
b8999098Dmitry Zinovyev9 years ago | 83 | ErrorHelper.getInternalError(InternalErrorCode.DebuggingFailed), |
| 84 | telemetryReporter, | |
| 85 | codeToRun | |
| 86 | ); | |
e45838cbVladimir Kotikov9 years ago | 87 | |