microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.0.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/exponent/exponentPlatform.ts

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