microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/launcher.ts
77lines · 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 {MultipleLifetimesAppWorker} from "./appWorker"; |
| 7 | import {Packager} from "../common/packager"; |
| 8 | import {Log} from "../common/log"; |
| 9 | import {PlatformResolver} from "./platformResolver"; |
| 10 | import {Telemetry} from "../common/telemetry"; |
| 11 | import {TelemetryHelper} from "../common/telemetryHelper"; |
| 12 | import {IRunOptions} from "./launchArgs"; |
| 13 | |
| 14 | export class Launcher { |
| 15 | private projectRootPath: string; |
| 16 | |
| 17 | constructor(projectRootPath: string) { |
| 18 | this.projectRootPath = projectRootPath; |
| 19 | } |
| 20 | |
| 21 | public launch() { |
| 22 | let version = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "package.json"), "utf-8")).version; |
| 23 | |
| 24 | // Enable telemetry |
| 25 | Telemetry.init("react-native-debug-process", version, true).then(() => { |
| 26 | 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 | Log.logError("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 | const packager = new Packager(this.projectRootPath, sourcesStoragePath); |
| 35 | generator.step("startPackager"); |
| 36 | return packager.start() |
| 37 | // 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 |
| 38 | // and the user needs to Reload JS manually. We prewarm it to prevent that issue |
| 39 | .then(() => { |
| 40 | generator.step("prewarmBundleCache"); |
| 41 | return packager.prewarmBundleCache(runOptions.platform); |
| 42 | }) |
| 43 | .then(() => { |
| 44 | generator.step("mobilePlatform.runApp"); |
| 45 | return mobilePlatform.runApp(runOptions); |
| 46 | }) |
| 47 | .then(() => { |
| 48 | generator.step("Starting App Worker"); |
| 49 | return new MultipleLifetimesAppWorker(sourcesStoragePath, runOptions.debugAdapterPort).start(); |
| 50 | }) // Start the app worker |
| 51 | .then(() => { |
| 52 | generator.step("mobilePlatform.enableJSDebuggingMode"); |
| 53 | return mobilePlatform.enableJSDebuggingMode(runOptions); |
| 54 | }); |
| 55 | } |
| 56 | }).done(() => { }, |
| 57 | reason => { |
| 58 | Log.logError("Cannot debug application.", reason); |
| 59 | }); |
| 60 | }); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Parses the launch arguments set in the launch configuration. |
| 65 | */ |
| 66 | private parseRunOptions(): IRunOptions { |
| 67 | const result: IRunOptions = { projectRoot: this.projectRootPath }; |
| 68 | // We expect our debugAdapter to pass in arguments as [platform, debugAdapterPort, target?]; |
| 69 | |
| 70 | result.platform = process.argv[2].toLowerCase(); |
| 71 | result.debugAdapterPort = parseInt(process.argv[3], 10) || 9090; |
| 72 | result.target = process.argv[4]; |
| 73 | |
| 74 | return result; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | |