microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
b5dd05b96ebb0f85736935514bd3fef31f7af2d5

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/launcher.ts

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