microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
eb4cb70c8f0f4fcc4d72b116ca1b0cff5e89c5a0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/debugger/launcher.ts

91lines · 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
4import * as fs from "fs";
5import * as path from "path";
6import * as Q from "q";
7import {MultipleLifetimesAppWorker} from "./appWorker";
8import {Log} from "../common/log/log";
9import {ErrorHelper} from "../common/error/errorHelper";
10import {InternalErrorCode} from "../common/error/internalErrorCode";
11import {ScriptImporter} from "./scriptImporter";
12import {PlatformResolver} from "./platformResolver";
13import {TelemetryHelper} from "../common/telemetryHelper";
14import {IRunOptions} from "../common/launchArgs";
15import * as em from "../common/extensionMessaging";
16import {EntryPointHandler} from "../common/entryPointHandler";
17
18export class Launcher {
19 private projectRootPath: string;
20
21 constructor(projectRootPath: string) {
22 this.projectRootPath = projectRootPath;
23 }
24
25 public launch(): void {
26 // Enable telemetry
27 new EntryPointHandler(true).runApp("react-native-debug-process", () => this.getAppVersion(),
28 ErrorHelper.getInternalError(InternalErrorCode.DebuggingFailed), () => {
29 return TelemetryHelper.generate("launch", (generator) => {
30 const resolver = new PlatformResolver();
31 const runOptions = this.parseRunOptions();
32 const mobilePlatform = resolver.resolveMobilePlatform(runOptions.platform);
33 if (!mobilePlatform) {
34 throw new RangeError("The target platform could not be read. Did you forget to add it to the launch.json configuration arguments?");
35 } else {
36 const sourcesStoragePath = path.join(this.projectRootPath, ".vscode", ".react");
37 let extensionMessageSender = new em.ExtensionMessageSender();
38 return Q({})
39 .then(() => {
40 generator.step("startPackager");
41 return extensionMessageSender.sendMessage(em.ExtensionMessage.START_PACKAGER);
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 });
48 })
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");
53 return extensionMessageSender.sendMessage(em.ExtensionMessage.PREWARM_BUNDLE_CACHE, [runOptions.platform]);
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);
66 }).then(() =>
67 Log.logMessage("Debugging session started successfully."));
68 }
69 });
70 });
71 }
72
73 private getAppVersion() {
74 return JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "package.json"), "utf-8")).version;
75 }
76
77 /**
78 * Parses the launch arguments set in the launch configuration.
79 */
80 private parseRunOptions(): IRunOptions {
81 const result: IRunOptions = { projectRoot: this.projectRootPath };
82 // We expect our debugAdapter to pass in arguments as [platform, debugAdapterPort, target?];
83
84 result.platform = process.argv[2].toLowerCase();
85 result.debugAdapterPort = parseInt(process.argv[3], 10) || 9090;
86 result.target = process.argv[4];
87 result.logCatArguments = process.argv[5];
88
89 return result;
90 }
91}