microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
4b124a08cf4726fc4b6b1f843dcd24e31f33db5e

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/reactNativeDebugEntryPoint.ts

90lines · modecode

1// 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
20 TelemetryHelper.sendSimpleEvent(reason);
21 process.exit(1);
22}
23
24// Enable telemetry
25new EntryPointHandler(ProcessType.Debugger).runApp(appName, () => version,
26 ErrorHelper.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 */
33 let nodeDebugFolder: string;
34 let 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
38 try {
39 /* tslint:disable:no-var-requires */
40 nodeDebugFolder = require("./nodeDebugLocation.json").nodeDebugPath;
41 vscodeDebugAdapterPackage = 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
45 bailOut("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
54 const originalDebugSessionRun = vscodeDebugAdapterPackage.DebugSession.run;
55 vscodeDebugAdapterPackage.DebugSession.run = () => { };
56
57 let nodeDebug: { NodeDebugSession: typeof NodeDebugSession };
58
59 try {
60 /* tslint:disable:no-var-requires */
61 nodeDebug = 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
65 const 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.
68 debugSession.start(process.stdin, process.stdout);
69 debugSession.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Unable to start debug adapter: " + e.toString(), "stderr"));
70 debugSession.sendEvent(new vscodeDebugAdapterPackage.TerminatedEvent());
71
72 bailOut("cannotFindNodeDebugAdapter");
73 }
74
75 vscodeDebugAdapterPackage.DebugSession.run = originalDebugSessionRun;
76
77 // Customize node adapter requests
78 try {
79 let nodeDebugWrapper = new NodeDebugWrapper(appName, version, telemetryReporter, vscodeDebugAdapterPackage, nodeDebug.NodeDebugSession);
80 nodeDebugWrapper.customizeNodeAdapterRequests();
81 } catch (e) {
82 const debugSession = new vscodeDebugAdapterPackage.DebugSession();
83 debugSession.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Unable to start debug adapter: " + e.toString(), "stderr"));
84 debugSession.sendEvent(new vscodeDebugAdapterPackage.TerminatedEvent());
85 bailOut(e.toString());
86 }
87
88 // Run the debug session for the node debug adapter with our modified requests
89 vscodeDebugAdapterPackage.DebugSession.run(nodeDebug.NodeDebugSession);
90 });