microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
18d7d68f72d1291e3cdd73f28f16abbd119339d1

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/debugger/reactNativeDebugEntryPoint.ts

93lines · modeblame

40e4b177Patricio Beltran10 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import * as fs from "fs";
5import * as path from "path";
6
7import {TelemetryHelper} from "../common/telemetryHelper";
8import {EntryPointHandler, ProcessType} from "../common/entryPointHandler";
9import {ErrorHelper} from "../common/error/errorHelper";
10import {InternalErrorCode} from "../common/error/internalErrorCode";
11import {NullTelemetryReporter, ReassignableTelemetryReporter} from "../common/telemetryReporters";
12import {NodeDebugWrapper} from "./nodeDebugWrapper";
13
14const version = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "package.json"), "utf-8")).version;
15const telemetryReporter = new ReassignableTelemetryReporter(new NullTelemetryReporter());
16const appName = "react-native-debug-adapter";
17
18function bailOut(reason: string): void {
19// Things have gone wrong in initialization: Report the error to telemetry and exit
20TelemetryHelper.sendSimpleEvent(reason);
21process.exit(1);
22}
23
24// Enable telemetry
25new EntryPointHandler(ProcessType.Debugger).runApp(appName, () => version,
26ErrorHelper.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*/
33let nodeDebugFolder: string;
34let 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
38try {
39/* tslint:disable:no-var-requires */
40nodeDebugFolder = require("./nodeDebugLocation.json").nodeDebugPath;
41vscodeDebugAdapterPackage = 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
45bailOut("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
54const originalDebugSessionRun = vscodeDebugAdapterPackage.DebugSession.run;
55vscodeDebugAdapterPackage.DebugSession.run = () => { };
56
57let nodeDebug: { NodeDebugSession: typeof NodeDebugSession };
774cffd7Vladimir Kotikov9 years ago58let sourceMaps: { SourceMaps: typeof SourceMaps };
40e4b177Patricio Beltran10 years ago59
60try {
61/* tslint:disable:no-var-requires */
62nodeDebug = require(path.join(nodeDebugFolder, "out", "node", "nodeDebug"));
774cffd7Vladimir Kotikov9 years ago63sourceMaps = require(path.join(nodeDebugFolder, "out", "node", "sourceMaps"));
40e4b177Patricio Beltran10 years ago64/* tslint:enable:no-var-requires */
65} catch (e) {
66// Unable to find nodeDebug, but we can make our own communication channel now
67const 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.
70debugSession.start(process.stdin, process.stdout);
71debugSession.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Unable to start debug adapter: " + e.toString(), "stderr"));
72debugSession.sendEvent(new vscodeDebugAdapterPackage.TerminatedEvent());
73
74bailOut("cannotFindNodeDebugAdapter");
75}
76
77vscodeDebugAdapterPackage.DebugSession.run = originalDebugSessionRun;
78
79// Customize node adapter requests
80try {
774cffd7Vladimir Kotikov9 years ago81let nodeDebugWrapper = new NodeDebugWrapper(appName, version, telemetryReporter,
82vscodeDebugAdapterPackage, nodeDebug.NodeDebugSession, sourceMaps.SourceMaps);
40e4b177Patricio Beltran10 years ago83nodeDebugWrapper.customizeNodeAdapterRequests();
84} catch (e) {
85const debugSession = new vscodeDebugAdapterPackage.DebugSession();
86debugSession.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Unable to start debug adapter: " + e.toString(), "stderr"));
87debugSession.sendEvent(new vscodeDebugAdapterPackage.TerminatedEvent());
88bailOut(e.toString());
89}
90
91// Run the debug session for the node debug adapter with our modified requests
92vscodeDebugAdapterPackage.DebugSession.run(nodeDebug.NodeDebugSession);
774cffd7Vladimir Kotikov9 years ago93});