microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/exponent/exponentPlatform.ts
195lines · 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 | | |
0d77292aJiglioNero4 years ago | 33 | public async 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 | |
0d77292aJiglioNero4 years ago | 47 | await TelemetryHelper.generate("ExponentPlatform.runApp", extProps, async () => { |
| 48 | await this.loginToExponentOrSkip(this.runOptions.expoHostType); | |
| 49 | await XDL.setOptions(this.projectPath, { packagerPort: this.packager.getPort() }); | |
| 50 | await XDL.startExponentServer(this.projectPath); | |
| 51 | | |
| 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 | | |
| 56 | if (this.runOptions.expoHostType === "tunnel") { | |
| 57 | await this.prepareExpoTunnels(); | |
| 58 | } else { | |
| 59 | await XDL.stopAdbReverse(this.projectPath); | |
| 60 | } | |
| 61 | | |
| 62 | const isAdbReversed = | |
| 63 | this.runOptions.expoHostType !== "local" | |
| 64 | ? 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 | await XDL.startAdbReverse(this.projectPath); | |
| 67 | let exponentUrl = ""; | |
| 68 | switch (this.runOptions.expoHostType) { | |
| 69 | case "lan": | |
| 70 | exponentUrl = await XDL.getUrl(this.projectPath, { | |
| 71 | dev: true, | |
| 72 | minify: false, | |
| 73 | hostType: "lan", | |
| 74 | }); | |
| 75 | break; | |
| 76 | case "local": | |
| 77 | if (isAdbReversed) { | |
| 78 | this.logger.info( | |
| 79 | localize( | |
| 80 | "ExpoStartAdbReverseSuccess", | |
| 81 | "A device or an emulator was found, 'adb reverse' command successfully executed.", | |
| 82 | ), | |
| 83 | ); | |
| 84 | } else { | |
| 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 | ), | |
34472878RedMickey5 years ago | 90 | ); |
0d77292aJiglioNero4 years ago | 91 | } |
| 92 | | |
| 93 | exponentUrl = await XDL.getUrl(this.projectPath, { | |
| 94 | dev: true, | |
| 95 | minify: false, | |
| 96 | hostType: "localhost", | |
ce5e88eeYuri Skorokhodov5 years ago | 97 | }); |
0d77292aJiglioNero4 years ago | 98 | break; |
| 99 | case "tunnel": | |
| 100 | default: | |
| 101 | exponentUrl = await XDL.getUrl(this.projectPath, { dev: true, minify: false }); | |
| 102 | } | |
| 103 | exponentUrl = "exp://" + url.parse(exponentUrl).host; | |
| 104 | | |
| 105 | if (!exponentUrl) { | |
| 106 | throw ErrorHelper.getInternalError(InternalErrorCode.ExpectedExponentTunnelPath); | |
| 107 | } | |
| 108 | | |
| 109 | if (this.runOptions.openExpoQR) { | |
| 110 | let exponentPage = vscode.window.createWebviewPanel( | |
| 111 | "Expo QR Code", | |
| 112 | "Expo QR Code", | |
| 113 | vscode.ViewColumn.Two, | |
| 114 | {}, | |
| 115 | ); | |
| 116 | exponentPage.webview.html = this.qrCodeContentProvider.provideTextDocumentContent( | |
| 117 | vscode.Uri.parse(exponentUrl), | |
| 118 | ); | |
| 119 | } | |
| 120 | | |
| 121 | this.exponentTunnelPath = exponentUrl; | |
| 122 | const outputMessage = localize( | |
| 123 | "ExponentServerIsRunningOpenToSeeIt", | |
| 124 | "Expo server is running. Open your Expo app at {0} to see it.", | |
| 125 | this.exponentTunnelPath, | |
| 126 | ); | |
| 127 | this.logger.info(outputMessage); | |
| 128 | | |
| 129 | const copyButton = localize("CopyToClipboard", "Copy to clipboard"); | |
| 130 | | |
| 131 | vscode.window.showInformationMessage(outputMessage, copyButton).then(selection => { | |
| 132 | if (selection === copyButton) { | |
| 133 | vscode.env.clipboard.writeText(exponentUrl); | |
| 134 | } | |
ce5e88eeYuri Skorokhodov5 years ago | 135 | }); |
4787ec09Artem Egorov8 years ago | 136 | }); |
| 137 | } | |
| 138 | | |
0d77292aJiglioNero4 years ago | 139 | public async loginToExponentOrSkip(expoHostType?: ExpoHostType): Promise<any> { |
62c4de22RedMickey6 years ago | 140 | if (expoHostType !== "tunnel") { |
0d77292aJiglioNero4 years ago | 141 | return; |
62c4de22RedMickey6 years ago | 142 | } |
0d77292aJiglioNero4 years ago | 143 | |
| 144 | return await this.exponentHelper.loginToExponent( | |
| 145 | async (message, password) => { | |
| 146 | return ( | |
| 147 | (await vscode.window.showInputBox({ | |
| 148 | placeHolder: message, | |
| 149 | password: password, | |
| 150 | })) || "" | |
| 151 | ); | |
62c4de22RedMickey6 years ago | 152 | }, |
0d77292aJiglioNero4 years ago | 153 | async message => { |
| 154 | const okButton = { title: "Ok" }; | |
| 155 | const cancelButton = { title: "Cancel", isCloseAffordance: true }; | |
| 156 | const answer = await vscode.window.showInformationMessage( | |
| 157 | message, | |
| 158 | { modal: true }, | |
| 159 | okButton, | |
| 160 | cancelButton, | |
| 161 | ); | |
| 162 | if (answer === cancelButton) { | |
| 163 | throw ErrorHelper.getInternalError(InternalErrorCode.UserCancelledExpoLogin); | |
| 164 | } | |
| 165 | return ""; | |
34472878RedMickey5 years ago | 166 | }, |
62c4de22RedMickey6 years ago | 167 | ); |
| 168 | } | |
| 169 | | |
0d77292aJiglioNero4 years ago | 170 | public async beforeStartPackager(): Promise<void> { |
4787ec09Artem Egorov8 years ago | 171 | return this.exponentHelper.configureExponentEnvironment(); |
0a68f8dbArtem Egorov8 years ago | 172 | } |
| 173 | | |
0d77292aJiglioNero4 years ago | 174 | public async enableJSDebuggingMode(): Promise<void> { |
34472878RedMickey5 years ago | 175 | this.logger.info( |
| 176 | localize( | |
| 177 | "ApplicationIsRunningOnExponentShakeDeviceForRemoteDebugging", | |
| 178 | "Application is running on Expo. Please shake device and select 'Debug JS Remotely' to enable debugging.", | |
| 179 | ), | |
| 180 | ); | |
0a68f8dbArtem Egorov8 years ago | 181 | } |
db6fd42aRuslan Bikkinin7 years ago | 182 | |
cbc7ac5bArtem Egorov7 years ago | 183 | public getRunArguments(): string[] { |
db6fd42aRuslan Bikkinin7 years ago | 184 | return []; |
| 185 | } | |
efb436fcRedMickey5 years ago | 186 | |
0d77292aJiglioNero4 years ago | 187 | private async prepareExpoTunnels(): Promise<void> { |
| 188 | try { | |
| 189 | await this.exponentHelper.findOrInstallNgrokGlobally(); | |
| 190 | await XDL.startTunnels(this.projectPath); | |
| 191 | } finally { | |
| 192 | this.exponentHelper.removeNodeModulesPathFromEnvIfWasSet(); | |
| 193 | } | |
efb436fcRedMickey5 years ago | 194 | } |
0a68f8dbArtem Egorov8 years ago | 195 | } |