microsoft/vscode-react-native

Public

mirrored from https://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 · modeblame

0a68f8dbArtem Egorov8 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
031832ffArtem Egorov7 years ago4import { ErrorHelper } from "../../common/error/errorHelper";
5import { InternalErrorCode } from "../../common/error/internalErrorCode";
62c4de22RedMickey6 years ago6import { IExponentRunOptions } from "../launchArgs";
031832ffArtem Egorov7 years ago7import { GeneralMobilePlatform, MobilePlatformDeps } from "../generalMobilePlatform";
8import { ExponentHelper } from "./exponentHelper";
9import { TelemetryHelper } from "../../common/telemetryHelper";
3c871141Yuri Skorokhodov7 years ago10import { QRCodeContentProvider } from "../qrCodeContentProvider";
0a68f8dbArtem Egorov8 years ago11
12import * as vscode from "vscode";
13import * as Q from "q";
4787ec09Artem Egorov7 years ago14import * as XDL from "./xdlInterface";
15import * as url from "url";
d7d405aeYuri Skorokhodov7 years ago16import * as nls from "vscode-nls";
17const localize = nls.loadMessageBundle();
0a68f8dbArtem Egorov8 years ago18
3c871141Yuri Skorokhodov7 years ago19
0a68f8dbArtem Egorov8 years ago20export class ExponentPlatform extends GeneralMobilePlatform {
21private exponentTunnelPath: string | null;
2e432a9eArtem Egorov8 years ago22private exponentHelper: ExponentHelper;
3c871141Yuri Skorokhodov7 years ago23private qrCodeContentProvider: QRCodeContentProvider = new QRCodeContentProvider();
0a68f8dbArtem Egorov8 years ago24
62c4de22RedMickey6 years ago25constructor(runOptions: IExponentRunOptions, platformDeps: MobilePlatformDeps = {}) {
0a68f8dbArtem Egorov8 years ago26super(runOptions, platformDeps);
2e432a9eArtem Egorov8 years ago27this.exponentHelper = new ExponentHelper(runOptions.workspaceRoot, runOptions.projectRoot);
0a68f8dbArtem Egorov8 years ago28this.exponentTunnelPath = null;
29}
30
31public runApp(): Q.Promise<void> {
ba953e9fRedMickey6 years ago32let extProps = {
031832ffArtem Egorov7 years ago33platform: {
34value: "exponent",
35isPii: false,
4787ec09Artem Egorov7 years ago36},
031832ffArtem Egorov7 years ago37};
4787ec09Artem Egorov7 years ago38
7fa90b3bRedMickey6 years ago39extProps = TelemetryHelper.addPropertyToTelemetryProperties(this.runOptions.reactNativeVersions.reactNativeVersion, "reactNativeVersion", extProps);
97855c3cYuri Skorokhodov6 years ago40extProps = TelemetryHelper.addPropertyToTelemetryProperties(this.runOptions.expoHostType, "expoHostType", extProps);
ba953e9fRedMickey6 years ago41
031832ffArtem Egorov7 years ago42return TelemetryHelper.generate("ExponentPlatform.runApp", extProps, () => {
62c4de22RedMickey6 years ago43return this.loginToExponentOrSkip(this.runOptions.expoHostType)
031832ffArtem Egorov7 years ago44.then(() =>
45XDL.setOptions(this.projectPath, { packagerPort: this.packager.port })
46)
47.then(() =>
48XDL.startExponentServer(this.projectPath)
49)
62c4de22RedMickey6 years ago50.then(() => {
51if (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
55return XDL.stopAdbReverse(this.projectPath);
56}
57return XDL.startTunnels(this.projectPath);
58})
59.then(() => {
60if (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
62return XDL.startAdbReverse(this.projectPath);
63})
64.then((isAdbReversed) => {
65switch (this.runOptions.expoHostType) {
66case "lan":
67return XDL.getUrl(this.projectPath, { dev: true, minify: false, hostType: "lan" });
68case "local":
69if (isAdbReversed) {
70this.logger.info(localize("ExpoStartAdbReverseSuccess", "A device or an emulator was found, 'adb reverse' command successfully executed."));
71} else {
72this.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
75return XDL.getUrl(this.projectPath, { dev: true, minify: false, hostType: "localhost" });
76case "tunnel":
77default:
78return XDL.getUrl(this.projectPath, { dev: true, minify: false });
79}
80})
81.then(exponentUrl => {
031832ffArtem Egorov7 years ago82return "exp://" + url.parse(exponentUrl).host;
83})
84.catch(reason => {
85return Q.reject<string>(reason);
86})
87.then(exponentUrl => {
3c871141Yuri Skorokhodov7 years ago88let exponentPage = vscode.window.createWebviewPanel("Expo QR Code", "Expo QR Code", vscode.ViewColumn.Two, { });
89exponentPage.webview.html = this.qrCodeContentProvider.provideTextDocumentContent(vscode.Uri.parse(exponentUrl));
031832ffArtem Egorov7 years ago90return exponentUrl;
91})
92.then(exponentUrl => {
93if (!exponentUrl) {
10f9b914Yuri Skorokhodov7 years ago94return Q.reject<void>(ErrorHelper.getInternalError(InternalErrorCode.ExpectedExponentTunnelPath));
031832ffArtem Egorov7 years ago95}
96this.exponentTunnelPath = exponentUrl;
d7d405aeYuri Skorokhodov7 years ago97const outputMessage = localize("ApplicationIsRunningOnExponentOpenToSeeIt", "Application is running on Expo. Open your Expo app at {0} to see it.", this.exponentTunnelPath);
031832ffArtem Egorov7 years ago98this.logger.info(outputMessage);
99
100return Q.resolve(void 0);
101});
4787ec09Artem Egorov7 years ago102});
103}
104
62c4de22RedMickey6 years ago105public loginToExponentOrSkip(expoHostType?: "tunnel" | "lan" | "local") {
106if (expoHostType !== "tunnel") {
107return Q({});
108}
109return this.exponentHelper.loginToExponent(
110(message, password) => {
111return Q.Promise((resolve, reject) => {
112vscode.window.showInputBox({ placeHolder: message, password: password })
113.then(login => {
114resolve(login || "");
115}, reject);
116});
117},
118(message) => {
119return Q.Promise((resolve, reject) => {
120const okButton = { title: "Ok" };
121const cancelButton = { title: "Cancel", isCloseAffordance: true };
122vscode.window.showInformationMessage(message, {modal: true}, okButton, cancelButton)
123.then(answer => {
124if (answer === cancelButton) {
125reject(ErrorHelper.getInternalError(InternalErrorCode.UserCancelledExpoLogin));
126}
127resolve("");
128}, reject);
129});
130}
131);
132}
133
4787ec09Artem Egorov7 years ago134public beforeStartPackager(): Q.Promise<void> {
135return this.exponentHelper.configureExponentEnvironment();
0a68f8dbArtem Egorov8 years ago136}
137
138public enableJSDebuggingMode(): Q.Promise<void> {
d7d405aeYuri Skorokhodov7 years ago139this.logger.info(localize("ApplicationIsRunningOnExponentShakeDeviceForRemoteDebugging", "Application is running on Expo. Please shake device and select 'Debug JS Remotely' to enable debugging."));
0a68f8dbArtem Egorov8 years ago140return Q.resolve<void>(void 0);
141}
db6fd42aRuslan Bikkinin7 years ago142
cbc7ac5bArtem Egorov7 years ago143public getRunArguments(): string[] {
db6fd42aRuslan Bikkinin7 years ago144return [];
145}
0a68f8dbArtem Egorov8 years ago146}