microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
6e7f90d839d33f208b4871f84b09b658c7607878

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/launcher.ts

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