microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
3c013cde4fe612455943c1ec04edd32268a040b1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/launcher.ts

92lines · 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 {ScriptImporter} from "./scriptImporter";
9import {Log} from "../common/log";
10import {PlatformResolver} from "./platformResolver";
11import {Telemetry} from "../common/telemetry";
12import {TelemetryHelper} from "../common/telemetryHelper";
13import {IRunOptions} from "../common/launchArgs";
14import * as em from "../common/extensionMessaging";
15
16export class Launcher {
17 private projectRootPath: string;
18
19 constructor(projectRootPath: string) {
20 this.projectRootPath = projectRootPath;
21 }
22
23 public launch() {
24 let version = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "package.json"), "utf-8")).version;
25 let extensionMessageSender = new em.ExtensionMessageSender();
26
27 // Enable telemetry
28 Telemetry.init("react-native-debug-process", version, true).then(() => {
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 Log.logError("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 return Q({})
38 .then(() => {
39 generator.step("startPackager");
40 return extensionMessageSender.sendMessage(em.ExtensionMessage.START_PACKAGER);
41 })
42 .then(() => {
43 let scriptImporter = new ScriptImporter(sourcesStoragePath);
44 return scriptImporter.downloadDebuggerWorker(sourcesStoragePath).then(() => {
45 Log.logMessage("Downloaded debuggerWorker.js (Logic to run the React Native app) from the Packager.");
46 });
47 })
48 // 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
49 // and the user needs to Reload JS manually. We prewarm it to prevent that issue
50 .then(() => {
51 generator.step("prewarmBundleCache");
52 return extensionMessageSender.sendMessage(em.ExtensionMessage.PREWARM_BUNDLE_CACHE, [runOptions.platform]);
53 })
54 .then(() => {
55 generator.step("mobilePlatform.runApp");
56 return mobilePlatform.runApp(runOptions);
57 })
58 .then(() => {
59 generator.step("Starting App Worker");
60 return new MultipleLifetimesAppWorker(sourcesStoragePath, runOptions.debugAdapterPort).start();
61 }) // Start the app worker
62 .then(() => {
63 generator.step("mobilePlatform.enableJSDebuggingMode");
64 return mobilePlatform.enableJSDebuggingMode(runOptions);
65 });
66 }
67 });
68 }).done(
69 () => {
70 Log.logMessage("Debugging session started succesfuly.");
71 },
72 reason => {
73 Log.logError("Cannot debug application.", reason);
74 });
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.internalExtensionPort = parseInt(process.argv[4], 10) || em.ServerDefaultParams.PORT;
87 result.target = process.argv[5];
88
89 return result;
90 }
91}
92
93