microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
341dba36c6e7ac21203bd58ef861c4803cd85fc0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/exponent/exponentPlatform.ts

147lines · 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, PlatformType } 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 XDL from "./xdlInterface";
14import * as url from "url";
15import * as nls from "vscode-nls";
16nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })();
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 = this.packager.getExponentHelper();
28 this.exponentTunnelPath = null;
29 }
30
31 public runApp(): Promise<void> {
32 let extProps = {
33 platform: {
34 value: PlatformType.Exponent,
35 isPii: false,
36 },
37 };
38
39 extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties(this.runOptions, this.runOptions.reactNativeVersions, extProps);
40
41 return new Promise((resolve, reject) => {
42 TelemetryHelper.generate("ExponentPlatform.runApp", extProps, () => {
43 return this.loginToExponentOrSkip(this.runOptions.expoHostType)
44 .then(() =>
45 XDL.setOptions(this.projectPath, { packagerPort: this.packager.getPort() })
46 )
47 .then(() =>
48 XDL.startExponentServer(this.projectPath)
49 )
50 .then(() => {
51 if (this.runOptions.expoHostType !== "tunnel") {
52 // the purpose of this is to save the same sequence of handling 'adb reverse' command execution as in Expo
53 // https://github.com/expo/expo-cli/blob/1d515d21200841e181518358fd9dc4c7b24c7cd6/packages/xdl/src/Project.ts#L2226-L2370
54 // we added this to be sure that our Expo launching logic doesn't have any negative side effects
55 return XDL.stopAdbReverse(this.projectPath);
56 }
57 return XDL.startTunnels(this.projectPath);
58 })
59 .then(() => {
60 if (this.runOptions.expoHostType !== "local") return false;
61 // 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
62 return XDL.startAdbReverse(this.projectPath);
63 })
64 .then((isAdbReversed) => {
65 switch (this.runOptions.expoHostType) {
66 case "lan":
67 return XDL.getUrl(this.projectPath, { dev: true, minify: false, hostType: "lan" });
68 case "local":
69 if (isAdbReversed) {
70 this.logger.info(localize("ExpoStartAdbReverseSuccess", "A device or an emulator was found, 'adb reverse' command successfully executed."));
71 } else {
72 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."));
73 }
74
75 return XDL.getUrl(this.projectPath, { dev: true, minify: false, hostType: "localhost" });
76 case "tunnel":
77 default:
78 return XDL.getUrl(this.projectPath, { dev: true, minify: false });
79 }
80 })
81 .then(exponentUrl => {
82 return "exp://" + url.parse(exponentUrl).host;
83 })
84 .then(exponentUrl => {
85 let exponentPage = vscode.window.createWebviewPanel("Expo QR Code", "Expo QR Code", vscode.ViewColumn.Two, { });
86 exponentPage.webview.html = this.qrCodeContentProvider.provideTextDocumentContent(vscode.Uri.parse(exponentUrl));
87 return exponentUrl;
88 })
89 .then(exponentUrl => {
90 if (!exponentUrl) {
91 return reject(ErrorHelper.getInternalError(InternalErrorCode.ExpectedExponentTunnelPath));
92 }
93 this.exponentTunnelPath = exponentUrl;
94 const outputMessage = localize("ApplicationIsRunningOnExponentOpenToSeeIt", "Application is running on Expo. Open your Expo app at {0} to see it.", this.exponentTunnelPath);
95 this.logger.info(outputMessage);
96
97 return resolve();
98 })
99 .catch(reason => {
100 return reject(reason);
101 });
102 });
103 });
104 }
105
106 public loginToExponentOrSkip(expoHostType?: "tunnel" | "lan" | "local"): Promise<any> {
107 if (expoHostType !== "tunnel") {
108 return Promise.resolve();
109 }
110 return this.exponentHelper.loginToExponent(
111 (message, password) => {
112 return new Promise((resolve, reject) => {
113 vscode.window.showInputBox({ placeHolder: message, password: password })
114 .then(login => {
115 resolve(login || "");
116 }, reject);
117 });
118 },
119 (message) => {
120 return new Promise((resolve, reject) => {
121 const okButton = { title: "Ok" };
122 const cancelButton = { title: "Cancel", isCloseAffordance: true };
123 vscode.window.showInformationMessage(message, {modal: true}, okButton, cancelButton)
124 .then(answer => {
125 if (answer === cancelButton) {
126 reject(ErrorHelper.getInternalError(InternalErrorCode.UserCancelledExpoLogin));
127 }
128 resolve("");
129 }, reject);
130 });
131 }
132 );
133 }
134
135 public beforeStartPackager(): Promise<void> {
136 return this.exponentHelper.configureExponentEnvironment();
137 }
138
139 public enableJSDebuggingMode(): Promise<void> {
140 this.logger.info(localize("ApplicationIsRunningOnExponentShakeDeviceForRemoteDebugging", "Application is running on Expo. Please shake device and select 'Debug JS Remotely' to enable debugging."));
141 return Promise.resolve();
142 }
143
144 public getRunArguments(): string[] {
145 return [];
146 }
147}
148