microsoft/vscode-react-native

Public

mirrored from https://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 · 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 Egorov8 years ago4import { ErrorHelper } from "../../common/error/errorHelper";
5import { InternalErrorCode } from "../../common/error/internalErrorCode";
259c018fYuri Skorokhodov5 years ago6import { IExponentRunOptions, PlatformType } from "../launchArgs";
031832ffArtem Egorov8 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";
4787ec09Artem Egorov8 years ago13import * as XDL from "./xdlInterface";
14import * as url from "url";
d7d405aeYuri Skorokhodov7 years ago15import * as nls from "vscode-nls";
2d8af448Yuri Skorokhodov6 years ago16nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })();
d7d405aeYuri Skorokhodov7 years ago17const 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);
c036b0d3JiglioNero6 years ago27this.exponentHelper = this.packager.getExponentHelper();
0a68f8dbArtem Egorov8 years ago28this.exponentTunnelPath = null;
29}
30
ce5e88eeYuri Skorokhodov5 years ago31public runApp(): Promise<void> {
ba953e9fRedMickey6 years ago32let extProps = {
031832ffArtem Egorov8 years ago33platform: {
259c018fYuri Skorokhodov5 years ago34value: PlatformType.Exponent,
031832ffArtem Egorov8 years ago35isPii: false,
4787ec09Artem Egorov8 years ago36},
031832ffArtem Egorov8 years ago37};
4787ec09Artem Egorov8 years ago38
7fa90b3bRedMickey6 years ago39extProps = TelemetryHelper.addPropertyToTelemetryProperties(this.runOptions.reactNativeVersions.reactNativeVersion, "reactNativeVersion", extProps);
de838bbfJiglioNero6 years ago40extProps = TelemetryHelper.addPropertyToTelemetryProperties(this.runOptions.expoHostType, "expoHostType", extProps);
ba953e9fRedMickey6 years ago41
ce5e88eeYuri Skorokhodov5 years ago42return new Promise((resolve, reject) => {
43TelemetryHelper.generate("ExponentPlatform.runApp", extProps, () => {
44return this.loginToExponentOrSkip(this.runOptions.expoHostType)
45.then(() =>
46XDL.setOptions(this.projectPath, { packagerPort: this.packager.getPort() })
47)
48.then(() =>
49XDL.startExponentServer(this.projectPath)
50)
51.then(() => {
52if (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
56return XDL.stopAdbReverse(this.projectPath);
57}
58return XDL.startTunnels(this.projectPath);
59})
60.then(() => {
61if (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
63return XDL.startAdbReverse(this.projectPath);
64})
65.then((isAdbReversed) => {
66switch (this.runOptions.expoHostType) {
67case "lan":
68return XDL.getUrl(this.projectPath, { dev: true, minify: false, hostType: "lan" });
69case "local":
70if (isAdbReversed) {
71this.logger.info(localize("ExpoStartAdbReverseSuccess", "A device or an emulator was found, 'adb reverse' command successfully executed."));
72} else {
73this.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}
62c4de22RedMickey6 years ago75
ce5e88eeYuri Skorokhodov5 years ago76return XDL.getUrl(this.projectPath, { dev: true, minify: false, hostType: "localhost" });
77case "tunnel":
78default:
79return XDL.getUrl(this.projectPath, { dev: true, minify: false });
80}
81})
82.then(exponentUrl => {
83return "exp://" + url.parse(exponentUrl).host;
84})
85.then(exponentUrl => {
86let exponentPage = vscode.window.createWebviewPanel("Expo QR Code", "Expo QR Code", vscode.ViewColumn.Two, { });
87exponentPage.webview.html = this.qrCodeContentProvider.provideTextDocumentContent(vscode.Uri.parse(exponentUrl));
88return exponentUrl;
89})
90.then(exponentUrl => {
91if (!exponentUrl) {
92return reject(ErrorHelper.getInternalError(InternalErrorCode.ExpectedExponentTunnelPath));
93}
94this.exponentTunnelPath = exponentUrl;
95const outputMessage = localize("ApplicationIsRunningOnExponentOpenToSeeIt", "Application is running on Expo. Open your Expo app at {0} to see it.", this.exponentTunnelPath);
96this.logger.info(outputMessage);
031832ffArtem Egorov8 years ago97
ce5e88eeYuri Skorokhodov5 years ago98return resolve();
99})
100.catch(reason => {
101return reject(reason);
102});
103});
4787ec09Artem Egorov8 years ago104});
105}
106
ce5e88eeYuri Skorokhodov5 years ago107public loginToExponentOrSkip(expoHostType?: "tunnel" | "lan" | "local"): Promise<any> {
62c4de22RedMickey6 years ago108if (expoHostType !== "tunnel") {
ce5e88eeYuri Skorokhodov5 years ago109return Promise.resolve();
62c4de22RedMickey6 years ago110}
111return this.exponentHelper.loginToExponent(
112(message, password) => {
ce5e88eeYuri Skorokhodov5 years ago113return new Promise((resolve, reject) => {
62c4de22RedMickey6 years ago114vscode.window.showInputBox({ placeHolder: message, password: password })
115.then(login => {
116resolve(login || "");
117}, reject);
118});
119},
120(message) => {
ce5e88eeYuri Skorokhodov5 years ago121return new Promise((resolve, reject) => {
62c4de22RedMickey6 years ago122const okButton = { title: "Ok" };
123const cancelButton = { title: "Cancel", isCloseAffordance: true };
124vscode.window.showInformationMessage(message, {modal: true}, okButton, cancelButton)
125.then(answer => {
126if (answer === cancelButton) {
127reject(ErrorHelper.getInternalError(InternalErrorCode.UserCancelledExpoLogin));
128}
129resolve("");
130}, reject);
131});
132}
133);
134}
135
ce5e88eeYuri Skorokhodov5 years ago136public beforeStartPackager(): Promise<void> {
4787ec09Artem Egorov8 years ago137return this.exponentHelper.configureExponentEnvironment();
0a68f8dbArtem Egorov8 years ago138}
139
ce5e88eeYuri Skorokhodov5 years ago140public enableJSDebuggingMode(): Promise<void> {
d7d405aeYuri Skorokhodov7 years ago141this.logger.info(localize("ApplicationIsRunningOnExponentShakeDeviceForRemoteDebugging", "Application is running on Expo. Please shake device and select 'Debug JS Remotely' to enable debugging."));
ce5e88eeYuri Skorokhodov5 years ago142return Promise.resolve();
0a68f8dbArtem Egorov8 years ago143}
db6fd42aRuslan Bikkinin7 years ago144
cbc7ac5bArtem Egorov7 years ago145public getRunArguments(): string[] {
db6fd42aRuslan Bikkinin7 years ago146return [];
147}
0a68f8dbArtem Egorov8 years ago148}