microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
190e393cdfd678505e325972de68688a94051a36

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/log";
10import {ErrorHelper} from "../common/error/errorHelper";
11import {InternalErrorCode} from "../common/error/internalErrorCode";
12import {PlatformResolver} from "./platformResolver";
13import {TelemetryHelper} from "../common/telemetryHelper";
14import {EntryPointHandler} from "../common/entryPointHandler";
15import {IRunOptions} from "./launchArgs";
16
17export class Launcher {
18 private projectRootPath: string;
19
20 constructor(projectRootPath: string) {
21 this.projectRootPath = projectRootPath;
22 }
23
24 public launch(): void {
25 // Enable telemetry
26 new EntryPointHandler().runApp("react-native-debug-process", () => this.getAppVersion(),
27 ErrorHelper.getInternalError(InternalErrorCode.DebuggingFailed), () => {
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 throw new RangeError("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 }).then(() =>
60 Log.logToConsole("Debugging session started succesfuly."));
61 }
62 });
63 });
64 }
65
66 private getAppVersion() {
67 return JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "package.json"), "utf-8")).version;
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