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