microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
4adeb45d4a2f292842d38deb2126dd90bb71bd20

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/exponent/exponentPlatform.ts

145lines · 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 { ErrorHelper } from "../../common/error/errorHelper";
5import { InternalErrorCode } from "../../common/error/internalErrorCode";
6import { IExponentRunOptions } from "../launchArgs";
7import { GeneralMobilePlatform, MobilePlatformDeps } from "../generalMobilePlatform";
8import { ExponentHelper } from "./exponentHelper";
9import { TelemetryHelper } from "../../common/telemetryHelper";
10import { QRCodeContentProvider } from "../qrCodeContentProvider";
11
12import * as vscode from "vscode";
13import * as Q from "q";
14import * as XDL from "./xdlInterface";
15import * as url from "url";
16import * as nls from "vscode-nls";
17const localize = nls.loadMessageBundle();
18
19
20export class ExponentPlatform extends GeneralMobilePlatform {
21 private exponentTunnelPath: string | null;
22 private exponentHelper: ExponentHelper;
23 private qrCodeContentProvider: QRCodeContentProvider = new QRCodeContentProvider();
24
25 constructor(runOptions: IExponentRunOptions, platformDeps: MobilePlatformDeps = {}) {
26 super(runOptions, platformDeps);
27 this.exponentHelper = new ExponentHelper(runOptions.workspaceRoot, runOptions.projectRoot);
28 this.exponentTunnelPath = null;
29 }
30
31 public runApp(): Q.Promise<void> {
32 let extProps = {
33 platform: {
34 value: "exponent",
35 isPii: false,
36 },
37 };
38
39 extProps = TelemetryHelper.addPropertyToTelemetryProperties(this.runOptions.reactNativeVersions.reactNativeVersion, "reactNativeVersion", extProps);
40
41 return TelemetryHelper.generate("ExponentPlatform.runApp", extProps, () => {
42 return this.loginToExponentOrSkip(this.runOptions.expoHostType)
43 .then(() =>
44 XDL.setOptions(this.projectPath, { packagerPort: this.packager.port })
45 )
46 .then(() =>
47 XDL.startExponentServer(this.projectPath)
48 )
49 .then(() => {
50 if (this.runOptions.expoHostType !== "tunnel") {
51 // the purpose of this is to save the same sequence of handling 'adb reverse' command execution as in Expo
52 // https://github.com/expo/expo-cli/blob/1d515d21200841e181518358fd9dc4c7b24c7cd6/packages/xdl/src/Project.ts#L2226-L2370
53 // we added this to be sure that our Expo launching logic doesn't have any negative side effects
54 return XDL.stopAdbReverse(this.projectPath);
55 }
56 return XDL.startTunnels(this.projectPath);
57 })
58 .then(() => {
59 if (this.runOptions.expoHostType !== "local") return false;
60 // we need to execute 'adb reverse' command to bind ports used by Expo and RN of local machine to ports of a connected Android device or a running emulator
61 return XDL.startAdbReverse(this.projectPath);
62 })
63 .then((isAdbReversed) => {
64 switch (this.runOptions.expoHostType) {
65 case "lan":
66 return XDL.getUrl(this.projectPath, { dev: true, minify: false, hostType: "lan" });
67 case "local":
68 if (isAdbReversed) {
69 this.logger.info(localize("ExpoStartAdbReverseSuccess", "A device or an emulator was found, 'adb reverse' command successfully executed."));
70 } else {
71 this.logger.warning(localize("ExpoStartAdbReverseFailure", "Adb reverse command failed. Couldn't find connected over usb device or running emulator. Also please make sure that there is only one currently connected device or running emulator."));
72 }
73
74 return XDL.getUrl(this.projectPath, { dev: true, minify: false, hostType: "localhost" });
75 case "tunnel":
76 default:
77 return XDL.getUrl(this.projectPath, { dev: true, minify: false });
78 }
79 })
80 .then(exponentUrl => {
81 return "exp://" + url.parse(exponentUrl).host;
82 })
83 .catch(reason => {
84 return Q.reject<string>(reason);
85 })
86 .then(exponentUrl => {
87 let exponentPage = vscode.window.createWebviewPanel("Expo QR Code", "Expo QR Code", vscode.ViewColumn.Two, { });
88 exponentPage.webview.html = this.qrCodeContentProvider.provideTextDocumentContent(vscode.Uri.parse(exponentUrl));
89 return exponentUrl;
90 })
91 .then(exponentUrl => {
92 if (!exponentUrl) {
93 return Q.reject<void>(ErrorHelper.getInternalError(InternalErrorCode.ExpectedExponentTunnelPath));
94 }
95 this.exponentTunnelPath = exponentUrl;
96 const outputMessage = localize("ApplicationIsRunningOnExponentOpenToSeeIt", "Application is running on Expo. Open your Expo app at {0} to see it.", this.exponentTunnelPath);
97 this.logger.info(outputMessage);
98
99 return Q.resolve(void 0);
100 });
101 });
102 }
103
104 public loginToExponentOrSkip(expoHostType?: "tunnel" | "lan" | "local") {
105 if (expoHostType !== "tunnel") {
106 return Q({});
107 }
108 return this.exponentHelper.loginToExponent(
109 (message, password) => {
110 return Q.Promise((resolve, reject) => {
111 vscode.window.showInputBox({ placeHolder: message, password: password })
112 .then(login => {
113 resolve(login || "");
114 }, reject);
115 });
116 },
117 (message) => {
118 return Q.Promise((resolve, reject) => {
119 const okButton = { title: "Ok" };
120 const cancelButton = { title: "Cancel", isCloseAffordance: true };
121 vscode.window.showInformationMessage(message, {modal: true}, okButton, cancelButton)
122 .then(answer => {
123 if (answer === cancelButton) {
124 reject(ErrorHelper.getInternalError(InternalErrorCode.UserCancelledExpoLogin));
125 }
126 resolve("");
127 }, reject);
128 });
129 }
130 );
131 }
132
133 public beforeStartPackager(): Q.Promise<void> {
134 return this.exponentHelper.configureExponentEnvironment();
135 }
136
137 public enableJSDebuggingMode(): Q.Promise<void> {
138 this.logger.info(localize("ApplicationIsRunningOnExponentShakeDeviceForRemoteDebugging", "Application is running on Expo. Please shake device and select 'Debug JS Remotely' to enable debugging."));
139 return Q.resolve<void>(void 0);
140 }
141
142 public getRunArguments(): string[] {
143 return [];
144 }
145}
146