microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/debugSessionBase.ts
122lines · 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 | |
| 4 | import * as vscode from "vscode"; |
| 5 | import * as Q from "q"; |
| 6 | import * as path from "path"; |
| 7 | import * as fs from "fs"; |
| 8 | import stripJsonComments = require("strip-json-comments"); |
| 9 | import { LoggingDebugSession, Logger, logger } from "vscode-debugadapter"; |
| 10 | import { DebugProtocol } from "vscode-debugprotocol"; |
| 11 | import { getLoggingDirectory, LogHelper } from "../extension/log/LogHelper"; |
| 12 | import { ReactNativeProjectHelper } from "../common/reactNativeProjectHelper"; |
| 13 | import { ErrorHelper } from "../common/error/errorHelper"; |
| 14 | import { InternalErrorCode } from "../common/error/internalErrorCode"; |
| 15 | import { ILaunchArgs } from "../extension/launchArgs"; |
| 16 | import { AppLauncher } from "../extension/appLauncher"; |
| 17 | import { MultipleLifetimesAppWorker } from "./appWorker"; |
| 18 | import { LogLevel } from "../extension/log/LogHelper"; |
| 19 | |
| 20 | /** |
| 21 | * Enum of possible statuses of debug session |
| 22 | */ |
| 23 | export 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 | |
| 38 | export 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 | |
| 46 | export interface ILaunchRequestArgs extends DebugProtocol.LaunchRequestArguments, IAttachRequestArgs { } |
| 47 | |
| 48 | export 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 initializeSettings(args: any): Q.Promise<any> { |
| 69 | if (!this.isSettingsInitialized) { |
| 70 | let chromeDebugCoreLogs = getLoggingDirectory(); |
| 71 | if (chromeDebugCoreLogs) { |
| 72 | chromeDebugCoreLogs = path.join(chromeDebugCoreLogs, "DebugSessionLogs.txt"); |
| 73 | } |
| 74 | let logLevel: string = args.trace; |
| 75 | if (logLevel) { |
| 76 | logLevel = logLevel.replace(logLevel[0], logLevel[0].toUpperCase()); |
| 77 | logger.setup(Logger.LogLevel[logLevel], chromeDebugCoreLogs || false); |
| 78 | this.cdpProxyLogLevel = LogLevel[logLevel] === LogLevel.Verbose ? LogLevel.Custom : LogLevel.None; |
| 79 | } else { |
| 80 | logger.setup(Logger.LogLevel.Log, chromeDebugCoreLogs || false); |
| 81 | this.cdpProxyLogLevel = LogHelper.LOG_LEVEL === LogLevel.Trace ? LogLevel.Custom : LogLevel.None; |
| 82 | } |
| 83 | |
| 84 | if (!args.sourceMaps) { |
| 85 | args.sourceMaps = true; |
| 86 | } |
| 87 | |
| 88 | const projectRootPath = getProjectRoot(args); |
| 89 | return ReactNativeProjectHelper.isReactNativeProject(projectRootPath) |
| 90 | .then((result) => { |
| 91 | if (!result) { |
| 92 | throw ErrorHelper.getInternalError(InternalErrorCode.NotInReactNativeFolderError); |
| 93 | } |
| 94 | this.projectRootPath = projectRootPath; |
| 95 | this.appLauncher = AppLauncher.getAppLauncherByProjectRootPath(projectRootPath); |
| 96 | this.isSettingsInitialized = true; |
| 97 | |
| 98 | return void 0; |
| 99 | }); |
| 100 | } else { |
| 101 | return Q.resolve<void>(void 0); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Parses settings.json file for workspace root property |
| 108 | */ |
| 109 | export function getProjectRoot(args: any): string { |
| 110 | const vsCodeRoot = args.cwd ? path.resolve(args.cwd) : path.resolve(args.program, "../.."); |
| 111 | const settingsPath = path.resolve(vsCodeRoot, ".vscode/settings.json"); |
| 112 | try { |
| 113 | let settingsContent = fs.readFileSync(settingsPath, "utf8"); |
| 114 | settingsContent = stripJsonComments(settingsContent); |
| 115 | let parsedSettings = JSON.parse(settingsContent); |
| 116 | let projectRootPath = parsedSettings["react-native-tools.projectRoot"] || parsedSettings["react-native-tools"].projectRoot; |
| 117 | return path.resolve(vsCodeRoot, projectRootPath); |
| 118 | } catch (e) { |
| 119 | logger.verbose(`${settingsPath} file doesn't exist or its content is incorrect. This file will be ignored.`); |
| 120 | return args.cwd ? path.resolve(args.cwd) : path.resolve(args.program, "../.."); |
| 121 | } |
| 122 | } |
| 123 | |