microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/launcher.ts
97lines · 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 fs from "fs"; |
| 5 | import * as path from "path"; |
| 6 | import * as Q from "q"; |
| 7 | import {MultipleLifetimesAppWorker} from "./appWorker"; |
| 8 | import {Log} from "../common/log/log"; |
| 9 | import {ErrorHelper} from "../common/error/errorHelper"; |
| 10 | import {InternalErrorCode} from "../common/error/internalErrorCode"; |
| 11 | import {ScriptImporter} from "./scriptImporter"; |
| 12 | import {PlatformResolver} from "./platformResolver"; |
| 13 | import {TelemetryHelper} from "../common/telemetryHelper"; |
| 14 | import {TargetPlatformHelper} from "../common/targetPlatformHelper"; |
| 15 | import {IRunOptions} from "../common/launchArgs"; |
| 16 | import {RemoteExtension} from "../common/remoteExtension"; |
| 17 | import {EntryPointHandler, ProcessType} from "../common/entryPointHandler"; |
| 18 | |
| 19 | export class Launcher { |
| 20 | private projectRootPath: string; |
| 21 | |
| 22 | constructor(projectRootPath: string) { |
| 23 | this.projectRootPath = projectRootPath; |
| 24 | } |
| 25 | |
| 26 | public launch(): void { |
| 27 | // Enable telemetry |
| 28 | new EntryPointHandler(ProcessType.Debugee).runApp("react-native-debug-process", () => this.getAppVersion(), |
| 29 | ErrorHelper.getInternalError(InternalErrorCode.DebuggingFailed), this.projectRootPath, () => { |
| 30 | return TelemetryHelper.generate("launch", (generator) => { |
| 31 | const resolver = new PlatformResolver(); |
| 32 | const runOptions = this.parseRunOptions(); |
| 33 | const mobilePlatform = resolver.resolveMobilePlatform(runOptions.platform, runOptions); |
| 34 | if (!mobilePlatform) { |
| 35 | throw new RangeError("The target platform could not be read. Did you forget to add it to the launch.json configuration arguments?"); |
| 36 | } else { |
| 37 | const sourcesStoragePath = path.join(this.projectRootPath, ".vscode", ".react"); |
| 38 | let remoteExtension = new RemoteExtension(this.projectRootPath); |
| 39 | return Q({}) |
| 40 | .then(() => { |
| 41 | generator.step("checkPlatformCompatibility"); |
| 42 | TargetPlatformHelper.checkTargetPlatformSupport(runOptions.platform); |
| 43 | generator.step("startPackager"); |
| 44 | return remoteExtension.startPackager(); |
| 45 | }) |
| 46 | .then(() => { |
| 47 | let scriptImporter = new ScriptImporter(sourcesStoragePath); |
| 48 | return scriptImporter.downloadDebuggerWorker(sourcesStoragePath).then(() => { |
| 49 | Log.logMessage("Downloaded debuggerWorker.js (Logic to run the React Native app) from the Packager."); |
| 50 | }); |
| 51 | }) |
| 52 | // We've seen that if we don't prewarm the bundle cache, the app fails on the first attempt to connect to the debugger logic |
| 53 | // and the user needs to Reload JS manually. We prewarm it to prevent that issue |
| 54 | .then(() => { |
| 55 | generator.step("prewarmBundleCache"); |
| 56 | Log.logMessage("Prewarming bundle cache. This may take a while ..."); |
| 57 | return remoteExtension.prewarmBundleCache(runOptions.platform); |
| 58 | }) |
| 59 | .then(() => { |
| 60 | generator.step("mobilePlatform.runApp"); |
| 61 | Log.logMessage("Building and running application."); |
| 62 | return mobilePlatform.runApp(); |
| 63 | }) |
| 64 | .then(() => { |
| 65 | generator.step("Starting App Worker"); |
| 66 | Log.logMessage("Starting debugger app worker."); |
| 67 | return new MultipleLifetimesAppWorker(sourcesStoragePath, runOptions.debugAdapterPort).start(); |
| 68 | }) // Start the app worker |
| 69 | .then(() => { |
| 70 | generator.step("mobilePlatform.enableJSDebuggingMode"); |
| 71 | return mobilePlatform.enableJSDebuggingMode(); |
| 72 | }).then(() => |
| 73 | Log.logMessage("Debugging session started successfully.")); |
| 74 | } |
| 75 | }); |
| 76 | }); |
| 77 | } |
| 78 | |
| 79 | private getAppVersion() { |
| 80 | return JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "package.json"), "utf-8")).version; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Parses the launch arguments set in the launch configuration. |
| 85 | */ |
| 86 | private parseRunOptions(): IRunOptions { |
| 87 | const result: IRunOptions = { projectRoot: this.projectRootPath }; |
| 88 | // We expect our debugAdapter to pass in arguments as [platform, debugAdapterPort, target?]; |
| 89 | |
| 90 | result.platform = process.argv[2].toLowerCase(); |
| 91 | result.debugAdapterPort = parseInt(process.argv[3], 10) || 9090; |
| 92 | result.target = process.argv[4]; |
| 93 | result.logCatArguments = process.argv[5]; |
| 94 | |
| 95 | return result; |
| 96 | } |
| 97 | } |
| 98 | |