microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
a83075130d152db06164830223cbc00712bd2c80

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/reactNativeDebugEntryPoint.ts

90lines · 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 };
58
59try {
60/* tslint:disable:no-var-requires */
61nodeDebug = require(path.join(nodeDebugFolder, "out", "node", "nodeDebug"));
62/* tslint:enable:no-var-requires */
63} catch (e) {
64// Unable to find nodeDebug, but we can make our own communication channel now
65const debugSession = new vscodeDebugAdapterPackage.DebugSession();
66// Note: this will not work in the context of debugging the debug adapter and communicating over a socket,
67// but in that case we have much better ways to investigate errors.
68debugSession.start(process.stdin, process.stdout);
69debugSession.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Unable to start debug adapter: " + e.toString(), "stderr"));
70debugSession.sendEvent(new vscodeDebugAdapterPackage.TerminatedEvent());
71
72bailOut("cannotFindNodeDebugAdapter");
73}
74
75vscodeDebugAdapterPackage.DebugSession.run = originalDebugSessionRun;
76
77// Customize node adapter requests
78try {
79let nodeDebugWrapper = new NodeDebugWrapper(appName, version, telemetryReporter, vscodeDebugAdapterPackage, nodeDebug.NodeDebugSession);
80nodeDebugWrapper.customizeNodeAdapterRequests();
81} catch (e) {
82const debugSession = new vscodeDebugAdapterPackage.DebugSession();
83debugSession.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Unable to start debug adapter: " + e.toString(), "stderr"));
84debugSession.sendEvent(new vscodeDebugAdapterPackage.TerminatedEvent());
85bailOut(e.toString());
86}
87
88// Run the debug session for the node debug adapter with our modified requests
89vscodeDebugAdapterPackage.DebugSession.run(nodeDebug.NodeDebugSession);
90});