microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
771dc596c273a8374ef21888eebdcb2fa3b4e887

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/launcher.ts

84lines · 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 {Packager} from "../common/packager";
9import {Log} from "../common/log";
10import {PlatformResolver} from "./platformResolver";
11import {Telemetry} from "../common/telemetry";
12import {TelemetryHelper} from "../common/telemetryHelper";
13import {IRunOptions} from "./launchArgs";
14
15export class Launcher {
16 private projectRootPath: string;
17
18 constructor(projectRootPath: string) {
19 this.projectRootPath = projectRootPath;
20 }
21
22 public launch() {
23 let version = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "package.json"), "utf-8")).version;
24
25 // Enable telemetry
26 Telemetry.init("react-native-debug-process", version, true).then(() => {
27 return TelemetryHelper.generate("launch", (generator) => {
28 const resolver = new PlatformResolver();
29 const runOptions = this.parseRunOptions();
30 const mobilePlatform = resolver.resolveMobilePlatform(runOptions.platform);
31 if (!mobilePlatform) {
32 Log.logError("The target platform could not be read. Did you forget to add it to the launch.json configuration arguments?");
33 } else {
34 const sourcesStoragePath = path.join(this.projectRootPath, ".vscode", ".react");
35 const packager = new Packager(this.projectRootPath, sourcesStoragePath);
36 return Q({})
37 .then(() => {
38 generator.step("startPackager");
39 return packager.start();
40 })
41 // 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
42 // and the user needs to Reload JS manually. We prewarm it to prevent that issue
43 .then(() => {
44 generator.step("prewarmBundleCache");
45 return packager.prewarmBundleCache(runOptions.platform);
46 })
47 .then(() => {
48 generator.step("mobilePlatform.runApp");
49 return mobilePlatform.runApp(runOptions);
50 })
51 .then(() => {
52 generator.step("Starting App Worker");
53 return new MultipleLifetimesAppWorker(sourcesStoragePath, runOptions.debugAdapterPort).start();
54 }) // Start the app worker
55 .then(() => {
56 generator.step("mobilePlatform.enableJSDebuggingMode");
57 return mobilePlatform.enableJSDebuggingMode(runOptions);
58 });
59 }
60 });
61 }).done(
62 () => {
63 Log.logMessage("Debugging session started succesfuly.");
64 },
65 reason => {
66 Log.logError("Cannot debug application.", reason);
67 });
68 }
69
70 /**
71 * Parses the launch arguments set in the launch configuration.
72 */
73 private parseRunOptions(): IRunOptions {
74 const result: IRunOptions = { projectRoot: this.projectRootPath };
75 // We expect our debugAdapter to pass in arguments as [platform, debugAdapterPort, target?];
76
77 result.platform = process.argv[2].toLowerCase();
78 result.debugAdapterPort = parseInt(process.argv[3], 10) || 9090;
79 result.target = process.argv[4];
80
81 return result;
82 }
83}
84
85