microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/launcher.ts
56lines · modeblame
a31b007cunknown10 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
3fb37ad5unknown10 years ago | 4 | import * as Q from "q"; |
4677921cdigeff10 years ago | 5 | import * as path from "path"; |
| 6 | import {MultipleLifetimesAppWorker} from "./appWorker"; | |
3fb37ad5unknown10 years ago | 7 | import {Packager} from "./packager"; |
bedf110funknown10 years ago | 8 | import {Log} from "../utils/commands/log"; |
f5ea0577unknown10 years ago | 9 | import {PlatformResolver} from "./platformResolver"; |
fb2bae06Daniel10 years ago | 10 | import {IRunOptions} from "./launchArgs"; |
3fb37ad5unknown10 years ago | 11 | |
| 12 | export class Launcher { | |
| 13 | private projectRootPath: string; | |
| 14 | | |
| 15 | constructor(projectRootPath: string) { | |
| 16 | this.projectRootPath = projectRootPath; | |
| 17 | } | |
| 18 | | |
b3a793eeNisheet Jain10 years ago | 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?"); | |
3af9a124unknown10 years ago | 25 | } else { |
4e7a6f0edlebu10 years ago | 26 | let sourcesStoragePath = path.join(this.projectRootPath, ".vscode", ".react"); |
418d8ac5digeff10 years ago | 27 | let packager = new Packager(this.projectRootPath, sourcesStoragePath); |
3af9a124unknown10 years ago | 28 | Q({}) |
418d8ac5digeff10 years ago | 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 | |
e639e7a4Daniel10 years ago | 35 | .then(() => mobilePlatform.enableJSDebuggingMode(runOptions)) |
3af9a124unknown10 years ago | 36 | .done(() => { }, reason => { |
| 37 | Log.logError("Cannot debug application.", reason); | |
| 38 | }); | |
b3a793eeNisheet Jain10 years ago | 39 | } |
| 40 | } | |
| 41 | | |
3af9a124unknown10 years ago | 42 | /** |
fb2bae06Daniel10 years ago | 43 | * Parses the launch arguments set in the launch configuration. |
3af9a124unknown10 years ago | 44 | */ |
fb2bae06Daniel10 years ago | 45 | private parseRunOptions(): IRunOptions { |
| 46 | let result: IRunOptions = { projectRoot: this.projectRootPath }; | |
3af9a124unknown10 years ago | 47 | |
| 48 | if (process.argv.length > 2) { | |
fb2bae06Daniel10 years ago | 49 | result.platform = process.argv[2].toLowerCase(); |
3af9a124unknown10 years ago | 50 | } |
| 51 | | |
fb2bae06Daniel10 years ago | 52 | result.target = process.argv[3]; |
3af9a124unknown10 years ago | 53 | return result; |
3fb37ad5unknown10 years ago | 54 | } |
| 55 | } | |
fb2bae06Daniel10 years ago | 56 | |