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