microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/exponent/exponentPlatform.ts
207lines · 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"; | |
5471436aRedMickey5 years ago | 6 | import { ExpoHostType, 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"; |
34472878RedMickey5 years ago | 16 | nls.config({ |
| 17 | messageFormat: nls.MessageFormat.bundle, | |
| 18 | bundleFormat: nls.BundleFormat.standalone, | |
| 19 | })(); | |
d7d405aeYuri Skorokhodov7 years ago | 20 | const localize = nls.loadMessageBundle(); |
0a68f8dbArtem Egorov8 years ago | 21 | |
| 22 | export class ExponentPlatform extends GeneralMobilePlatform { | |
| 23 | private exponentTunnelPath: string | null; | |
2e432a9eArtem Egorov8 years ago | 24 | private exponentHelper: ExponentHelper; |
3c871141Yuri Skorokhodov7 years ago | 25 | private qrCodeContentProvider: QRCodeContentProvider = new QRCodeContentProvider(); |
0a68f8dbArtem Egorov8 years ago | 26 | |
62c4de22RedMickey6 years ago | 27 | constructor(runOptions: IExponentRunOptions, platformDeps: MobilePlatformDeps = {}) { |
0a68f8dbArtem Egorov8 years ago | 28 | super(runOptions, platformDeps); |
c036b0d3JiglioNero6 years ago | 29 | this.exponentHelper = this.packager.getExponentHelper(); |
0a68f8dbArtem Egorov8 years ago | 30 | this.exponentTunnelPath = null; |
| 31 | } | |
| 32 | | |
ce5e88eeYuri Skorokhodov5 years ago | 33 | public runApp(): Promise<void> { |
ba953e9fRedMickey6 years ago | 34 | let extProps = { |
031832ffArtem Egorov8 years ago | 35 | platform: { |
259c018fYuri Skorokhodov5 years ago | 36 | value: PlatformType.Exponent, |
031832ffArtem Egorov8 years ago | 37 | isPii: false, |
4787ec09Artem Egorov8 years ago | 38 | }, |
031832ffArtem Egorov8 years ago | 39 | }; |
4787ec09Artem Egorov8 years ago | 40 | |
34472878RedMickey5 years ago | 41 | extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties( |
| 42 | this.runOptions, | |
| 43 | this.runOptions.reactNativeVersions, | |
| 44 | extProps, | |
| 45 | ); | |
ba953e9fRedMickey6 years ago | 46 | |
ce5e88eeYuri Skorokhodov5 years ago | 47 | return new Promise((resolve, reject) => { |
34472878RedMickey5 years ago | 48 | TelemetryHelper.generate("ExponentPlatform.runApp", extProps, () => { |
ce5e88eeYuri Skorokhodov5 years ago | 49 | return this.loginToExponentOrSkip(this.runOptions.expoHostType) |
| 50 | .then(() => | |
34472878RedMickey5 years ago | 51 | XDL.setOptions(this.projectPath, { packagerPort: this.packager.getPort() }), |
ce5e88eeYuri Skorokhodov5 years ago | 52 | ) |
34472878RedMickey5 years ago | 53 | .then(() => XDL.startExponentServer(this.projectPath)) |
ce5e88eeYuri Skorokhodov5 years ago | 54 | .then(() => { |
| 55 | if (this.runOptions.expoHostType !== "tunnel") { | |
| 56 | // the purpose of this is to save the same sequence of handling 'adb reverse' command execution as in Expo | |
| 57 | // https://github.com/expo/expo-cli/blob/1d515d21200841e181518358fd9dc4c7b24c7cd6/packages/xdl/src/Project.ts#L2226-L2370 | |
| 58 | // we added this to be sure that our Expo launching logic doesn't have any negative side effects | |
| 59 | return XDL.stopAdbReverse(this.projectPath); | |
| 60 | } | |
| 61 | return XDL.startTunnels(this.projectPath); | |
| 62 | }) | |
| 63 | .then(() => { | |
| 64 | if (this.runOptions.expoHostType !== "local") return false; | |
| 65 | // 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 | |
| 66 | return XDL.startAdbReverse(this.projectPath); | |
| 67 | }) | |
34472878RedMickey5 years ago | 68 | .then(isAdbReversed => { |
ce5e88eeYuri Skorokhodov5 years ago | 69 | switch (this.runOptions.expoHostType) { |
| 70 | case "lan": | |
34472878RedMickey5 years ago | 71 | return XDL.getUrl(this.projectPath, { |
| 72 | dev: true, | |
| 73 | minify: false, | |
| 74 | hostType: "lan", | |
| 75 | }); | |
ce5e88eeYuri Skorokhodov5 years ago | 76 | case "local": |
| 77 | if (isAdbReversed) { | |
34472878RedMickey5 years ago | 78 | this.logger.info( |
| 79 | localize( | |
| 80 | "ExpoStartAdbReverseSuccess", | |
| 81 | "A device or an emulator was found, 'adb reverse' command successfully executed.", | |
| 82 | ), | |
| 83 | ); | |
ce5e88eeYuri Skorokhodov5 years ago | 84 | } else { |
34472878RedMickey5 years ago | 85 | this.logger.warning( |
| 86 | localize( | |
| 87 | "ExpoStartAdbReverseFailure", | |
| 88 | "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.", | |
| 89 | ), | |
| 90 | ); | |
ce5e88eeYuri Skorokhodov5 years ago | 91 | } |
62c4de22RedMickey6 years ago | 92 | |
34472878RedMickey5 years ago | 93 | return XDL.getUrl(this.projectPath, { |
| 94 | dev: true, | |
| 95 | minify: false, | |
| 96 | hostType: "localhost", | |
| 97 | }); | |
ce5e88eeYuri Skorokhodov5 years ago | 98 | case "tunnel": |
| 99 | default: | |
| 100 | return XDL.getUrl(this.projectPath, { dev: true, minify: false }); | |
| 101 | } | |
| 102 | }) | |
| 103 | .then(exponentUrl => { | |
| 104 | return "exp://" + url.parse(exponentUrl).host; | |
| 105 | }) | |
| 106 | .then(exponentUrl => { | |
58882bb6JiglioNero5 years ago | 107 | if (this.runOptions.openExpoQR) { |
34472878RedMickey5 years ago | 108 | let exponentPage = vscode.window.createWebviewPanel( |
| 109 | "Expo QR Code", | |
| 110 | "Expo QR Code", | |
| 111 | vscode.ViewColumn.Two, | |
| 112 | {}, | |
| 113 | ); | |
| 114 | exponentPage.webview.html = this.qrCodeContentProvider.provideTextDocumentContent( | |
| 115 | vscode.Uri.parse(exponentUrl), | |
| 116 | ); | |
58882bb6JiglioNero5 years ago | 117 | } |
ce5e88eeYuri Skorokhodov5 years ago | 118 | return exponentUrl; |
| 119 | }) | |
| 120 | .then(exponentUrl => { | |
| 121 | if (!exponentUrl) { | |
34472878RedMickey5 years ago | 122 | return reject( |
| 123 | ErrorHelper.getInternalError( | |
| 124 | InternalErrorCode.ExpectedExponentTunnelPath, | |
| 125 | ), | |
| 126 | ); | |
ce5e88eeYuri Skorokhodov5 years ago | 127 | } |
| 128 | this.exponentTunnelPath = exponentUrl; | |
34472878RedMickey5 years ago | 129 | const outputMessage = localize( |
| 130 | "ExponentServerIsRunningOpenToSeeIt", | |
| 131 | "Expo server is running. Open your Expo app at {0} to see it.", | |
| 132 | this.exponentTunnelPath, | |
| 133 | ); | |
ce5e88eeYuri Skorokhodov5 years ago | 134 | this.logger.info(outputMessage); |
031832ffArtem Egorov8 years ago | 135 | |
58882bb6JiglioNero5 years ago | 136 | const copyButton = localize("CopyToClipboard", "Copy to clipboard"); |
| 137 | | |
34472878RedMickey5 years ago | 138 | vscode.window |
| 139 | .showInformationMessage(outputMessage, copyButton) | |
| 140 | .then(selection => { | |
| 141 | if (selection === copyButton) { | |
| 142 | vscode.env.clipboard.writeText(exponentUrl); | |
| 143 | } | |
| 144 | }); | |
58882bb6JiglioNero5 years ago | 145 | |
ce5e88eeYuri Skorokhodov5 years ago | 146 | return resolve(); |
| 147 | }) | |
| 148 | .catch(reason => { | |
| 149 | return reject(reason); | |
| 150 | }); | |
| 151 | }); | |
4787ec09Artem Egorov8 years ago | 152 | }); |
| 153 | } | |
| 154 | | |
5471436aRedMickey5 years ago | 155 | public loginToExponentOrSkip(expoHostType?: ExpoHostType): Promise<any> { |
62c4de22RedMickey6 years ago | 156 | if (expoHostType !== "tunnel") { |
34472878RedMickey5 years ago | 157 | return Promise.resolve(); |
62c4de22RedMickey6 years ago | 158 | } |
| 159 | return this.exponentHelper.loginToExponent( | |
| 160 | (message, password) => { | |
ce5e88eeYuri Skorokhodov5 years ago | 161 | return new Promise((resolve, reject) => { |
34472878RedMickey5 years ago | 162 | vscode.window |
| 163 | .showInputBox({ placeHolder: message, password: password }) | |
62c4de22RedMickey6 years ago | 164 | .then(login => { |
| 165 | resolve(login || ""); | |
| 166 | }, reject); | |
| 167 | }); | |
| 168 | }, | |
34472878RedMickey5 years ago | 169 | message => { |
ce5e88eeYuri Skorokhodov5 years ago | 170 | return new Promise((resolve, reject) => { |
34472878RedMickey5 years ago | 171 | const okButton = { title: "Ok" }; |
| 172 | const cancelButton = { title: "Cancel", isCloseAffordance: true }; | |
| 173 | vscode.window | |
| 174 | .showInformationMessage(message, { modal: true }, okButton, cancelButton) | |
62c4de22RedMickey6 years ago | 175 | .then(answer => { |
| 176 | if (answer === cancelButton) { | |
34472878RedMickey5 years ago | 177 | reject( |
| 178 | ErrorHelper.getInternalError( | |
| 179 | InternalErrorCode.UserCancelledExpoLogin, | |
| 180 | ), | |
| 181 | ); | |
62c4de22RedMickey6 years ago | 182 | } |
| 183 | resolve(""); | |
| 184 | }, reject); | |
| 185 | }); | |
34472878RedMickey5 years ago | 186 | }, |
62c4de22RedMickey6 years ago | 187 | ); |
| 188 | } | |
| 189 | | |
ce5e88eeYuri Skorokhodov5 years ago | 190 | public beforeStartPackager(): Promise<void> { |
4787ec09Artem Egorov8 years ago | 191 | return this.exponentHelper.configureExponentEnvironment(); |
0a68f8dbArtem Egorov8 years ago | 192 | } |
| 193 | | |
ce5e88eeYuri Skorokhodov5 years ago | 194 | public enableJSDebuggingMode(): Promise<void> { |
34472878RedMickey5 years ago | 195 | this.logger.info( |
| 196 | localize( | |
| 197 | "ApplicationIsRunningOnExponentShakeDeviceForRemoteDebugging", | |
| 198 | "Application is running on Expo. Please shake device and select 'Debug JS Remotely' to enable debugging.", | |
| 199 | ), | |
| 200 | ); | |
ce5e88eeYuri Skorokhodov5 years ago | 201 | return Promise.resolve(); |
0a68f8dbArtem Egorov8 years ago | 202 | } |
db6fd42aRuslan Bikkinin7 years ago | 203 | |
cbc7ac5bArtem Egorov7 years ago | 204 | public getRunArguments(): string[] { |
db6fd42aRuslan Bikkinin7 years ago | 205 | return []; |
| 206 | } | |
0a68f8dbArtem Egorov8 years ago | 207 | } |