microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.14.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/exponent/exponentPlatform.ts

146lines · 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 extProps = TelemetryHelper.addPropertyToTelemetryProperties(this.runOptions.expoHostType, "expoHostType", extProps);
41
42 return TelemetryHelper.generate("ExponentPlatform.runApp", extProps, () => {
43 return this.loginToExponentOrSkip(this.runOptions.expoHostType)
44 .then(() =>
45 XDL.setOptions(this.projectPath, { packagerPort: this.packager.port })
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 .catch(reason => {
85 return Q.reject<string>(reason);
86 })
87 .then(exponentUrl => {
88 let exponentPage = vscode.window.createWebviewPanel("Expo QR Code", "Expo QR Code", vscode.ViewColumn.Two, { });
89 exponentPage.webview.html = this.qrCodeContentProvider.provideTextDocumentContent(vscode.Uri.parse(exponentUrl));
90 return exponentUrl;
91 })
92 .then(exponentUrl => {
93 if (!exponentUrl) {
94 return Q.reject<void>(ErrorHelper.getInternalError(InternalErrorCode.ExpectedExponentTunnelPath));
95 }
96 this.exponentTunnelPath = exponentUrl;
97 const outputMessage = localize("ApplicationIsRunningOnExponentOpenToSeeIt", "Application is running on Expo. Open your Expo app at {0} to see it.", this.exponentTunnelPath);
98 this.logger.info(outputMessage);
99
100 return Q.resolve(void 0);
101 });
102 });
103 }
104
105 public loginToExponentOrSkip(expoHostType?: "tunnel" | "lan" | "local") {
106 if (expoHostType !== "tunnel") {
107 return Q({});
108 }
109 return this.exponentHelper.loginToExponent(
110 (message, password) => {
111 return Q.Promise((resolve, reject) => {
112 vscode.window.showInputBox({ placeHolder: message, password: password })
113 .then(login => {
114 resolve(login || "");
115 }, reject);
116 });
117 },
118 (message) => {
119 return Q.Promise((resolve, reject) => {
120 const okButton = { title: "Ok" };
121 const cancelButton = { title: "Cancel", isCloseAffordance: true };
122 vscode.window.showInformationMessage(message, {modal: true}, okButton, cancelButton)
123 .then(answer => {
124 if (answer === cancelButton) {
125 reject(ErrorHelper.getInternalError(InternalErrorCode.UserCancelledExpoLogin));
126 }
127 resolve("");
128 }, reject);
129 });
130 }
131 );
132 }
133
134 public beforeStartPackager(): Q.Promise<void> {
135 return this.exponentHelper.configureExponentEnvironment();
136 }
137
138 public enableJSDebuggingMode(): Q.Promise<void> {
139 this.logger.info(localize("ApplicationIsRunningOnExponentShakeDeviceForRemoteDebugging", "Application is running on Expo. Please shake device and select 'Debug JS Remotely' to enable debugging."));
140 return Q.resolve<void>(void 0);
141 }
142
143 public getRunArguments(): string[] {
144 return [];
145 }
146}
147