microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/launcher.ts
69lines · 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 Q from "q"; |
| 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 {TelemetryHelper} from "../common/telemetryHelper"; |
| 11 | import {IRunOptions} from "./launchArgs"; |
| 12 | |
| 13 | export class Launcher { |
| 14 | private projectRootPath: string; |
| 15 | |
| 16 | constructor(projectRootPath: string) { |
| 17 | this.projectRootPath = projectRootPath; |
| 18 | } |
| 19 | |
| 20 | public launch() { |
| 21 | let resolver = new PlatformResolver(); |
| 22 | let runOptions = this.parseRunOptions(); |
| 23 | let mobilePlatform = resolver.resolveMobilePlatform(runOptions.platform); |
| 24 | if (!mobilePlatform) { |
| 25 | Log.logError("The target platform could not be read. Did you forget to add it to the launch.json configuration arguments?"); |
| 26 | } else { |
| 27 | let sourcesStoragePath = path.join(this.projectRootPath, ".vscode", ".react"); |
| 28 | let packager = new Packager(this.projectRootPath, sourcesStoragePath); |
| 29 | TelemetryHelper.generate("launch", (generator) => { |
| 30 | return packager.start() |
| 31 | // 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 |
| 32 | // and the user needs to Reload JS manually. We prewarm it to prevent that issue |
| 33 | .then(() => { |
| 34 | generator.step("prewarmBundleCache"); |
| 35 | packager.prewarmBundleCache(runOptions.platform); |
| 36 | }) |
| 37 | .then(() => { |
| 38 | generator.step("mobilePlatform.runApp"); |
| 39 | mobilePlatform.runApp(runOptions); |
| 40 | }) |
| 41 | .then(() => { |
| 42 | generator.step("Starting App Worker"); |
| 43 | new MultipleLifetimesAppWorker(sourcesStoragePath, runOptions.debugAdapterPort).start(); |
| 44 | }) // Start the app worker |
| 45 | .then(() => { |
| 46 | generator.step("mobilePlatform.enableJSDebuggingMode"); |
| 47 | mobilePlatform.enableJSDebuggingMode(runOptions); |
| 48 | }); |
| 49 | }).done(() => { }, reason => { |
| 50 | Log.logError("Cannot debug application.", reason); |
| 51 | }); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Parses the launch arguments set in the launch configuration. |
| 57 | */ |
| 58 | private parseRunOptions(): IRunOptions { |
| 59 | let result: IRunOptions = { projectRoot: this.projectRootPath }; |
| 60 | // We expect our debugAdapter to pass in arguments as [platform, debugAdapterPort, target?]; |
| 61 | |
| 62 | result.platform = process.argv[2].toLowerCase(); |
| 63 | result.debugAdapterPort = parseInt(process.argv[3], 10) || 9090; |
| 64 | result.target = process.argv[4]; |
| 65 | |
| 66 | return result; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | |