microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
8b2be31e754e9f0cdcb9f30d07d7b34dc181dcc7

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/launcher.ts

101lines · 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 {Log} from "../common/log/log";
9import {ErrorHelper} from "../common/error/errorHelper";
10import {InternalErrorCode} from "../common/error/internalErrorCode";
11import {ScriptImporter} from "./scriptImporter";
12import {PlatformResolver} from "./platformResolver";
13import {TelemetryHelper} from "../common/telemetryHelper";
14import {TargetPlatformHelper} from "../common/targetPlatformHelper";
15import {IRunOptions} from "../common/launchArgs";
16import {RemoteExtension} from "../common/remoteExtension";
17import {EntryPointHandler, ProcessType} from "../common/entryPointHandler";
18
19export class Launcher {
20 private projectRootPath: string;
21 private remoteExtension: RemoteExtension;
22
23 constructor(projectRootPath: string) {
24 this.projectRootPath = projectRootPath;
25 this.remoteExtension = new RemoteExtension(this.projectRootPath);
26 }
27
28 public launch(): void {
29 // Enable telemetry
30 new EntryPointHandler(ProcessType.Debugee).runApp("react-native-debug-process", () => this.getAppVersion(),
31 ErrorHelper.getInternalError(InternalErrorCode.DebuggingFailed), this.projectRootPath, () => {
32 return TelemetryHelper.generate("launch", (generator) => {
33 const resolver = new PlatformResolver();
34 return this.parseRunOptions().then(runOptions => {
35 const mobilePlatform = resolver.resolveMobilePlatform(runOptions.platform, runOptions);
36 if (!mobilePlatform) {
37 throw new RangeError("The target platform could not be read. Did you forget to add it to the launch.json configuration arguments?");
38 } else {
39 const sourcesStoragePath = path.join(this.projectRootPath, ".vscode", ".react");
40 return Q({})
41 .then(() => {
42 generator.step("checkPlatformCompatibility");
43 TargetPlatformHelper.checkTargetPlatformSupport(runOptions.platform);
44 generator.step("startPackager");
45 return this.remoteExtension.startPackager();
46 })
47 .then(() => {
48 let scriptImporter = new ScriptImporter(runOptions.packagerPort, sourcesStoragePath);
49 return scriptImporter.downloadDebuggerWorker(sourcesStoragePath).then(() => {
50 Log.logMessage("Downloaded debuggerWorker.js (Logic to run the React Native app) from the Packager.");
51 });
52 })
53 // 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
54 // and the user needs to Reload JS manually. We prewarm it to prevent that issue
55 .then(() => {
56 generator.step("prewarmBundleCache");
57 Log.logMessage("Prewarming bundle cache. This may take a while ...");
58 return this.remoteExtension.prewarmBundleCache(runOptions.platform);
59 })
60 .then(() => {
61 generator.step("mobilePlatform.runApp");
62 Log.logMessage("Building and running application.");
63 return mobilePlatform.runApp();
64 })
65 .then(() => {
66 generator.step("Starting App Worker");
67 Log.logMessage("Starting debugger app worker.");
68 return new MultipleLifetimesAppWorker(runOptions.packagerPort, sourcesStoragePath, runOptions.debugAdapterPort).start();
69 }) // Start the app worker
70 .then(() => {
71 generator.step("mobilePlatform.enableJSDebuggingMode");
72 return mobilePlatform.enableJSDebuggingMode();
73 }).then(() =>
74 Log.logMessage("Debugging session started successfully."));
75 }
76 });
77 });
78 });
79 }
80
81 private getAppVersion() {
82 return JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "package.json"), "utf-8")).version;
83 }
84
85 /**
86 * Parses the launch arguments set in the launch configuration.
87 */
88 private parseRunOptions(): Q.Promise<IRunOptions> {
89 // We expect our debugAdapter to pass in arguments as [platform, debugAdapterPort, target?, logCatArguments?];
90 return this.remoteExtension.getPackagerPort().then(packagerPort => {
91 return {
92 projectRoot: this.projectRootPath,
93 platform: process.argv[2].toLowerCase(),
94 debugAdapterPort: parseInt(process.argv[3], 10) || 9090,
95 target: process.argv[4],
96 packagerPort: packagerPort,
97 logCatArguments: process.argv[5],
98 };
99 });
100 }
101}
102