microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
da86e6fffddfaf7f6ca5a97cf9f1c345394f3bdc

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/launcher.ts

90lines · 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";
14import {ReactNativeProjectHelper} from "../common/reactNativeProjectHelper";
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
26 // Enable telemetry
27 Telemetry.init("react-native-debug-process", version, true).then(() => {
28 return TelemetryHelper.generate("launch", (generator) => {
29 const resolver = new PlatformResolver();
30 const runOptions = this.parseRunOptions();
31 const mobilePlatform = resolver.resolveMobilePlatform(runOptions.platform);
32 if (!mobilePlatform) {
33 Log.logError("The target platform could not be read. Did you forget to add it to the launch.json configuration arguments?");
34 } else {
35 const sourcesStoragePath = path.join(this.projectRootPath, ".vscode", ".react");
36 const packager = new Packager(this.projectRootPath, sourcesStoragePath);
37 return Q({})
38 .then(() => {
39 generator.step("startPackager");
40 return packager.start();
41 })
42 // 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
43 // and the user needs to Reload JS manually. We prewarm it to prevent that issue
44 .then(() => {
45 generator.step("prewarmBundleCache");
46 return packager.prewarmBundleCache(runOptions.platform);
47 })
48 .then(() => {
49 generator.step("mobilePlatform.runApp");
50 return mobilePlatform.runApp(runOptions);
51 })
52 .then(() => {
53 generator.step("Starting App Worker");
54 return new MultipleLifetimesAppWorker(sourcesStoragePath, runOptions.debugAdapterPort).start();
55 }) // Start the app worker
56 .then(() => {
57 generator.step("mobilePlatform.enableJSDebuggingMode");
58 return mobilePlatform.enableJSDebuggingMode(runOptions);
59 });
60 }
61 });
62 }).done(
63 () => {
64 Log.logMessage("Debugging session started succesfuly.");
65 },
66 reason => {
67 Log.logError("Cannot debug application.", reason);
68 // This is the top level handler of the app, so when we get an error we exit
69 TelemetryHelper.sendSimpleEvent("launchDebuggerError");
70 Telemetry.sendPendingData().finally(() => {
71 process.exit(1);
72 });
73 });
74 }
75
76 /**
77 * Parses the launch arguments set in the launch configuration.
78 */
79 private parseRunOptions(): IRunOptions {
80 const result: IRunOptions = { projectRoot: this.projectRootPath };
81 // We expect our debugAdapter to pass in arguments as [platform, debugAdapterPort, target?];
82
83 result.platform = process.argv[2].toLowerCase();
84 result.debugAdapterPort = parseInt(process.argv[3], 10) || 9090;
85 result.target = process.argv[4];
86
87 return result;
88 }
89}
90
91