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