microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/launcher.ts
87lines · 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 {ScriptImporter} from "./scriptImporter"; |
| 9 | import {Log} from "../common/log"; |
| 10 | import {PlatformResolver} from "./platformResolver"; |
| 11 | import {TelemetryHelper} from "../common/telemetryHelper"; |
| 12 | import {IRunOptions} from "../common/launchArgs"; |
| 13 | import * as em from "../common/extensionMessaging"; |
| 14 | import {EntryPointHandler} from "../common/entryPointHandler"; |
| 15 | |
| 16 | export class Launcher { |
| 17 | private projectRootPath: string; |
| 18 | |
| 19 | constructor(projectRootPath: string) { |
| 20 | this.projectRootPath = projectRootPath; |
| 21 | } |
| 22 | |
| 23 | public launch(): void { |
| 24 | // Enable telemetry |
| 25 | new EntryPointHandler().runApp("react-native-debug-process", () => this.getAppVersion(), "Cannot debug application", () => { |
| 26 | return TelemetryHelper.generate("launch", (generator) => { |
| 27 | const resolver = new PlatformResolver(); |
| 28 | const runOptions = this.parseRunOptions(); |
| 29 | const mobilePlatform = resolver.resolveMobilePlatform(runOptions.platform); |
| 30 | if (!mobilePlatform) { |
| 31 | throw new RangeError("The target platform could not be read. Did you forget to add it to the launch.json configuration arguments?"); |
| 32 | } else { |
| 33 | const sourcesStoragePath = path.join(this.projectRootPath, ".vscode", ".react"); |
| 34 | let extensionMessageSender = new em.ExtensionMessageSender(); |
| 35 | return Q({}) |
| 36 | .then(() => { |
| 37 | generator.step("startPackager"); |
| 38 | return extensionMessageSender.sendMessage(em.ExtensionMessage.START_PACKAGER); |
| 39 | }) |
| 40 | .then(() => { |
| 41 | let scriptImporter = new ScriptImporter(sourcesStoragePath); |
| 42 | return scriptImporter.downloadDebuggerWorker(sourcesStoragePath).then(() => { |
| 43 | Log.logMessage("Downloaded debuggerWorker.js (Logic to run the React Native app) from the Packager."); |
| 44 | }); |
| 45 | }) |
| 46 | // 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 |
| 47 | // and the user needs to Reload JS manually. We prewarm it to prevent that issue |
| 48 | .then(() => { |
| 49 | generator.step("prewarmBundleCache"); |
| 50 | return extensionMessageSender.sendMessage(em.ExtensionMessage.PREWARM_BUNDLE_CACHE, [runOptions.platform]); |
| 51 | }) |
| 52 | .then(() => { |
| 53 | generator.step("mobilePlatform.runApp"); |
| 54 | return mobilePlatform.runApp(runOptions); |
| 55 | }) |
| 56 | .then(() => { |
| 57 | generator.step("Starting App Worker"); |
| 58 | return new MultipleLifetimesAppWorker(sourcesStoragePath, runOptions.debugAdapterPort).start(); |
| 59 | }) // Start the app worker |
| 60 | .then(() => { |
| 61 | generator.step("mobilePlatform.enableJSDebuggingMode"); |
| 62 | return mobilePlatform.enableJSDebuggingMode(runOptions); |
| 63 | }).then(() => |
| 64 | Log.logMessage("Debugging session started succesfuly.")); |
| 65 | } |
| 66 | }); |
| 67 | }); |
| 68 | } |
| 69 | |
| 70 | private getAppVersion() { |
| 71 | return JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "package.json"), "utf-8")).version; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Parses the launch arguments set in the launch configuration. |
| 76 | */ |
| 77 | private parseRunOptions(): IRunOptions { |
| 78 | const result: IRunOptions = { projectRoot: this.projectRootPath }; |
| 79 | // We expect our debugAdapter to pass in arguments as [platform, debugAdapterPort, target?]; |
| 80 | |
| 81 | result.platform = process.argv[2].toLowerCase(); |
| 82 | result.debugAdapterPort = parseInt(process.argv[3], 10) || 9090; |
| 83 | result.target = process.argv[4]; |
| 84 | |
| 85 | return result; |
| 86 | } |
| 87 | } |