microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/exponent/exponentPlatform.ts
148lines · modeblame
0a68f8dbArtem Egorov8 years ago | 1 | // 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 ago | 4 | import { ErrorHelper } from "../../common/error/errorHelper"; |
| 5 | import { InternalErrorCode } from "../../common/error/internalErrorCode"; | |
259c018fYuri Skorokhodov5 years ago | 6 | import { IExponentRunOptions, PlatformType } from "../launchArgs"; |
031832ffArtem Egorov8 years ago | 7 | import { GeneralMobilePlatform, MobilePlatformDeps } from "../generalMobilePlatform"; |
| 8 | import { ExponentHelper } from "./exponentHelper"; | |
| 9 | import { TelemetryHelper } from "../../common/telemetryHelper"; | |
3c871141Yuri Skorokhodov7 years ago | 10 | import { QRCodeContentProvider } from "../qrCodeContentProvider"; |
0a68f8dbArtem Egorov8 years ago | 11 | |
| 12 | import * as vscode from "vscode"; | |
4787ec09Artem Egorov8 years ago | 13 | import * as XDL from "./xdlInterface"; |
| 14 | import * as url from "url"; | |
d7d405aeYuri Skorokhodov7 years ago | 15 | import * as nls from "vscode-nls"; |
2d8af448Yuri Skorokhodov6 years ago | 16 | nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })(); |
d7d405aeYuri Skorokhodov7 years ago | 17 | const localize = nls.loadMessageBundle(); |
0a68f8dbArtem Egorov8 years ago | 18 | |
3c871141Yuri Skorokhodov7 years ago | 19 | |
0a68f8dbArtem Egorov8 years ago | 20 | export class ExponentPlatform extends GeneralMobilePlatform { |
| 21 | private exponentTunnelPath: string | null; | |
2e432a9eArtem Egorov8 years ago | 22 | private exponentHelper: ExponentHelper; |
3c871141Yuri Skorokhodov7 years ago | 23 | private qrCodeContentProvider: QRCodeContentProvider = new QRCodeContentProvider(); |
0a68f8dbArtem Egorov8 years ago | 24 | |
62c4de22RedMickey6 years ago | 25 | constructor(runOptions: IExponentRunOptions, platformDeps: MobilePlatformDeps = {}) { |
0a68f8dbArtem Egorov8 years ago | 26 | super(runOptions, platformDeps); |
c036b0d3JiglioNero6 years ago | 27 | this.exponentHelper = this.packager.getExponentHelper(); |
0a68f8dbArtem Egorov8 years ago | 28 | this.exponentTunnelPath = null; |
| 29 | } | |
| 30 | | |
ce5e88eeYuri Skorokhodov5 years ago | 31 | public runApp(): Promise<void> { |
ba953e9fRedMickey6 years ago | 32 | let extProps = { |
031832ffArtem Egorov8 years ago | 33 | platform: { |
259c018fYuri Skorokhodov5 years ago | 34 | value: PlatformType.Exponent, |
031832ffArtem Egorov8 years ago | 35 | isPii: false, |
4787ec09Artem Egorov8 years ago | 36 | }, |
031832ffArtem Egorov8 years ago | 37 | }; |
4787ec09Artem Egorov8 years ago | 38 | |
7fa90b3bRedMickey6 years ago | 39 | extProps = TelemetryHelper.addPropertyToTelemetryProperties(this.runOptions.reactNativeVersions.reactNativeVersion, "reactNativeVersion", extProps); |
de838bbfJiglioNero6 years ago | 40 | extProps = TelemetryHelper.addPropertyToTelemetryProperties(this.runOptions.expoHostType, "expoHostType", extProps); |
ba953e9fRedMickey6 years ago | 41 | |
ce5e88eeYuri Skorokhodov5 years ago | 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 | } | |
62c4de22RedMickey6 years ago | 75 | |
ce5e88eeYuri Skorokhodov5 years ago | 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); | |
031832ffArtem Egorov8 years ago | 97 | |
ce5e88eeYuri Skorokhodov5 years ago | 98 | return resolve(); |
| 99 | }) | |
| 100 | .catch(reason => { | |
| 101 | return reject(reason); | |
| 102 | }); | |
| 103 | }); | |
4787ec09Artem Egorov8 years ago | 104 | }); |
| 105 | } | |
| 106 | | |
ce5e88eeYuri Skorokhodov5 years ago | 107 | public loginToExponentOrSkip(expoHostType?: "tunnel" | "lan" | "local"): Promise<any> { |
62c4de22RedMickey6 years ago | 108 | if (expoHostType !== "tunnel") { |
ce5e88eeYuri Skorokhodov5 years ago | 109 | return Promise.resolve(); |
62c4de22RedMickey6 years ago | 110 | } |
| 111 | return this.exponentHelper.loginToExponent( | |
| 112 | (message, password) => { | |
ce5e88eeYuri Skorokhodov5 years ago | 113 | return new Promise((resolve, reject) => { |
62c4de22RedMickey6 years ago | 114 | vscode.window.showInputBox({ placeHolder: message, password: password }) |
| 115 | .then(login => { | |
| 116 | resolve(login || ""); | |
| 117 | }, reject); | |
| 118 | }); | |
| 119 | }, | |
| 120 | (message) => { | |
ce5e88eeYuri Skorokhodov5 years ago | 121 | return new Promise((resolve, reject) => { |
62c4de22RedMickey6 years ago | 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 | | |
ce5e88eeYuri Skorokhodov5 years ago | 136 | public beforeStartPackager(): Promise<void> { |
4787ec09Artem Egorov8 years ago | 137 | return this.exponentHelper.configureExponentEnvironment(); |
0a68f8dbArtem Egorov8 years ago | 138 | } |
| 139 | | |
ce5e88eeYuri Skorokhodov5 years ago | 140 | public enableJSDebuggingMode(): Promise<void> { |
d7d405aeYuri Skorokhodov7 years ago | 141 | this.logger.info(localize("ApplicationIsRunningOnExponentShakeDeviceForRemoteDebugging", "Application is running on Expo. Please shake device and select 'Debug JS Remotely' to enable debugging.")); |
ce5e88eeYuri Skorokhodov5 years ago | 142 | return Promise.resolve(); |
0a68f8dbArtem Egorov8 years ago | 143 | } |
db6fd42aRuslan Bikkinin7 years ago | 144 | |
cbc7ac5bArtem Egorov7 years ago | 145 | public getRunArguments(): string[] { |
db6fd42aRuslan Bikkinin7 years ago | 146 | return []; |
| 147 | } | |
0a68f8dbArtem Egorov8 years ago | 148 | } |