microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
b7451aef6bd5846d7333cd23872aaadd1d57136c

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/debugSessionBase.ts

124lines · 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 vscode from "vscode";
5import * as Q from "q";
6import * as path from "path";
7import * as fs from "fs";
8import stripJsonComments = require("strip-json-comments");
9import { LoggingDebugSession, Logger, logger } from "vscode-debugadapter";
10import { DebugProtocol } from "vscode-debugprotocol";
11import { getLoggingDirectory, LogHelper } from "../extension/log/LogHelper";
12import { ReactNativeProjectHelper } from "../common/reactNativeProjectHelper";
13import { ErrorHelper } from "../common/error/errorHelper";
14import { InternalErrorCode } from "../common/error/internalErrorCode";
15import { ILaunchArgs } from "../extension/launchArgs";
16import { AppLauncher } from "../extension/appLauncher";
17import { MultipleLifetimesAppWorker } from "./appWorker";
18import { LogLevel } from "../extension/log/LogHelper";
19
20/**
21 * Enum of possible statuses of debug session
22 */
23export enum DebugSessionStatus {
24 /** A session has been just created */
25 FirstConnection,
26 /** This status is required in order to exclude the possible creation of several debug sessions at the first start */
27 FirstConnectionPending,
28 /** This status means that an application can be reloaded */
29 ConnectionAllowed,
30 /** This status means that an application is reloading now, and we shouldn't terminate the current debug session */
31 ConnectionPending,
32 /** A debuggee connected successfully */
33 ConnectionDone,
34 /** A debuggee failed to connect */
35 ConnectionFailed,
36}
37
38export interface IAttachRequestArgs extends DebugProtocol.AttachRequestArguments, ILaunchArgs {
39 cwd: string; /* Automatically set by VS Code to the currently opened folder */
40 port: number;
41 url?: string;
42 address?: string;
43 trace?: string;
44}
45
46export interface ILaunchRequestArgs extends DebugProtocol.LaunchRequestArguments, IAttachRequestArgs { }
47
48export abstract class DebugSessionBase extends LoggingDebugSession {
49
50 protected appLauncher: AppLauncher;
51 protected appWorker: MultipleLifetimesAppWorker | null;
52 protected projectRootPath: string;
53 protected isSettingsInitialized: boolean; // used to prevent parameters reinitialization when attach is called from launch function
54 protected previousAttachArgs: IAttachRequestArgs;
55 protected cdpProxyLogLevel: LogLevel;
56 protected debugSessionStatus: DebugSessionStatus;
57 protected session: vscode.DebugSession;
58
59 constructor(session: vscode.DebugSession) {
60 super();
61
62 this.session = session;
63 this.isSettingsInitialized = false;
64 this.appWorker = null;
65 this.debugSessionStatus = DebugSessionStatus.FirstConnection;
66 }
67
68 protected abstract establishDebugSession(resolve?: (value?: void | PromiseLike<void> | undefined) => void): void;
69
70 protected initializeSettings(args: any): Q.Promise<any> {
71 if (!this.isSettingsInitialized) {
72 let chromeDebugCoreLogs = getLoggingDirectory();
73 if (chromeDebugCoreLogs) {
74 chromeDebugCoreLogs = path.join(chromeDebugCoreLogs, "DebugSessionLogs.txt");
75 }
76 let logLevel: string = args.trace;
77 if (logLevel) {
78 logLevel = logLevel.replace(logLevel[0], logLevel[0].toUpperCase());
79 logger.setup(Logger.LogLevel[logLevel], chromeDebugCoreLogs || false);
80 this.cdpProxyLogLevel = LogLevel[logLevel] === LogLevel.Verbose ? LogLevel.Custom : LogLevel.None;
81 } else {
82 logger.setup(Logger.LogLevel.Log, chromeDebugCoreLogs || false);
83 this.cdpProxyLogLevel = LogHelper.LOG_LEVEL === LogLevel.Trace ? LogLevel.Custom : LogLevel.None;
84 }
85
86 if (!args.sourceMaps) {
87 args.sourceMaps = true;
88 }
89
90 const projectRootPath = getProjectRoot(args);
91 return ReactNativeProjectHelper.isReactNativeProject(projectRootPath)
92 .then((result) => {
93 if (!result) {
94 throw ErrorHelper.getInternalError(InternalErrorCode.NotInReactNativeFolderError);
95 }
96 this.projectRootPath = projectRootPath;
97 this.appLauncher = AppLauncher.getAppLauncherByProjectRootPath(projectRootPath);
98 this.isSettingsInitialized = true;
99
100 return void 0;
101 });
102 } else {
103 return Q.resolve<void>(void 0);
104 }
105 }
106}
107
108/**
109 * Parses settings.json file for workspace root property
110 */
111export function getProjectRoot(args: any): string {
112 const vsCodeRoot = args.cwd ? path.resolve(args.cwd) : path.resolve(args.program, "../..");
113 const settingsPath = path.resolve(vsCodeRoot, ".vscode/settings.json");
114 try {
115 let settingsContent = fs.readFileSync(settingsPath, "utf8");
116 settingsContent = stripJsonComments(settingsContent);
117 let parsedSettings = JSON.parse(settingsContent);
118 let projectRootPath = parsedSettings["react-native-tools.projectRoot"] || parsedSettings["react-native-tools"].projectRoot;
119 return path.resolve(vsCodeRoot, projectRootPath);
120 } catch (e) {
121 logger.verbose(`${settingsPath} file doesn't exist or its content is incorrect. This file will be ignored.`);
122 return args.cwd ? path.resolve(args.cwd) : path.resolve(args.program, "../..");
123 }
124}
125