microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/exponent/exponentPlatform.ts
146lines · 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 Egorov7 years ago | 4 | import { ErrorHelper } from "../../common/error/errorHelper"; |
| 5 | import { InternalErrorCode } from "../../common/error/internalErrorCode"; | |
62c4de22RedMickey6 years ago | 6 | import { IExponentRunOptions } from "../launchArgs"; |
031832ffArtem Egorov7 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"; | |
| 13 | import * as Q from "q"; | |
4787ec09Artem Egorov7 years ago | 14 | import * as XDL from "./xdlInterface"; |
| 15 | import * as url from "url"; | |
d7d405aeYuri Skorokhodov7 years ago | 16 | import * as nls from "vscode-nls"; |
| 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); |
2e432a9eArtem Egorov8 years ago | 27 | this.exponentHelper = new ExponentHelper(runOptions.workspaceRoot, runOptions.projectRoot); |
0a68f8dbArtem Egorov8 years ago | 28 | this.exponentTunnelPath = null; |
| 29 | } | |
| 30 | | |
| 31 | public runApp(): Q.Promise<void> { | |
ba953e9fRedMickey6 years ago | 32 | let extProps = { |
031832ffArtem Egorov7 years ago | 33 | platform: { |
| 34 | value: "exponent", | |
| 35 | isPii: false, | |
4787ec09Artem Egorov7 years ago | 36 | }, |
031832ffArtem Egorov7 years ago | 37 | }; |
4787ec09Artem Egorov7 years ago | 38 | |
7fa90b3bRedMickey6 years ago | 39 | extProps = TelemetryHelper.addPropertyToTelemetryProperties(this.runOptions.reactNativeVersions.reactNativeVersion, "reactNativeVersion", extProps); |
97855c3cYuri Skorokhodov6 years ago | 40 | extProps = TelemetryHelper.addPropertyToTelemetryProperties(this.runOptions.expoHostType, "expoHostType", extProps); |
ba953e9fRedMickey6 years ago | 41 | |
031832ffArtem Egorov7 years ago | 42 | return TelemetryHelper.generate("ExponentPlatform.runApp", extProps, () => { |
62c4de22RedMickey6 years ago | 43 | return this.loginToExponentOrSkip(this.runOptions.expoHostType) |
031832ffArtem Egorov7 years ago | 44 | .then(() => |
| 45 | XDL.setOptions(this.projectPath, { packagerPort: this.packager.port }) | |
| 46 | ) | |
| 47 | .then(() => | |
| 48 | XDL.startExponentServer(this.projectPath) | |
| 49 | ) | |
62c4de22RedMickey6 years ago | 50 | .then(() => { |
| 51 | if (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 | |
| 55 | return XDL.stopAdbReverse(this.projectPath); | |
| 56 | } | |
| 57 | return XDL.startTunnels(this.projectPath); | |
| 58 | }) | |
| 59 | .then(() => { | |
| 60 | if (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 | |
| 62 | return XDL.startAdbReverse(this.projectPath); | |
| 63 | }) | |
| 64 | .then((isAdbReversed) => { | |
| 65 | switch (this.runOptions.expoHostType) { | |
| 66 | case "lan": | |
| 67 | return XDL.getUrl(this.projectPath, { dev: true, minify: false, hostType: "lan" }); | |
| 68 | case "local": | |
| 69 | if (isAdbReversed) { | |
| 70 | this.logger.info(localize("ExpoStartAdbReverseSuccess", "A device or an emulator was found, 'adb reverse' command successfully executed.")); | |
| 71 | } else { | |
| 72 | 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.")); | |
| 73 | } | |
| 74 | | |
| 75 | return XDL.getUrl(this.projectPath, { dev: true, minify: false, hostType: "localhost" }); | |
| 76 | case "tunnel": | |
| 77 | default: | |
| 78 | return XDL.getUrl(this.projectPath, { dev: true, minify: false }); | |
| 79 | } | |
| 80 | }) | |
| 81 | .then(exponentUrl => { | |
031832ffArtem Egorov7 years ago | 82 | return "exp://" + url.parse(exponentUrl).host; |
| 83 | }) | |
| 84 | .catch(reason => { | |
| 85 | return Q.reject<string>(reason); | |
| 86 | }) | |
| 87 | .then(exponentUrl => { | |
3c871141Yuri Skorokhodov7 years ago | 88 | let exponentPage = vscode.window.createWebviewPanel("Expo QR Code", "Expo QR Code", vscode.ViewColumn.Two, { }); |
| 89 | exponentPage.webview.html = this.qrCodeContentProvider.provideTextDocumentContent(vscode.Uri.parse(exponentUrl)); | |
031832ffArtem Egorov7 years ago | 90 | return exponentUrl; |
| 91 | }) | |
| 92 | .then(exponentUrl => { | |
| 93 | if (!exponentUrl) { | |
10f9b914Yuri Skorokhodov7 years ago | 94 | return Q.reject<void>(ErrorHelper.getInternalError(InternalErrorCode.ExpectedExponentTunnelPath)); |
031832ffArtem Egorov7 years ago | 95 | } |
| 96 | this.exponentTunnelPath = exponentUrl; | |
d7d405aeYuri Skorokhodov7 years ago | 97 | const outputMessage = localize("ApplicationIsRunningOnExponentOpenToSeeIt", "Application is running on Expo. Open your Expo app at {0} to see it.", this.exponentTunnelPath); |
031832ffArtem Egorov7 years ago | 98 | this.logger.info(outputMessage); |
| 99 | | |
| 100 | return Q.resolve(void 0); | |
| 101 | }); | |
4787ec09Artem Egorov7 years ago | 102 | }); |
| 103 | } | |
| 104 | | |
62c4de22RedMickey6 years ago | 105 | public loginToExponentOrSkip(expoHostType?: "tunnel" | "lan" | "local") { |
| 106 | if (expoHostType !== "tunnel") { | |
| 107 | return Q({}); | |
| 108 | } | |
| 109 | return this.exponentHelper.loginToExponent( | |
| 110 | (message, password) => { | |
| 111 | return Q.Promise((resolve, reject) => { | |
| 112 | vscode.window.showInputBox({ placeHolder: message, password: password }) | |
| 113 | .then(login => { | |
| 114 | resolve(login || ""); | |
| 115 | }, reject); | |
| 116 | }); | |
| 117 | }, | |
| 118 | (message) => { | |
| 119 | return Q.Promise((resolve, reject) => { | |
| 120 | const okButton = { title: "Ok" }; | |
| 121 | const cancelButton = { title: "Cancel", isCloseAffordance: true }; | |
| 122 | vscode.window.showInformationMessage(message, {modal: true}, okButton, cancelButton) | |
| 123 | .then(answer => { | |
| 124 | if (answer === cancelButton) { | |
| 125 | reject(ErrorHelper.getInternalError(InternalErrorCode.UserCancelledExpoLogin)); | |
| 126 | } | |
| 127 | resolve(""); | |
| 128 | }, reject); | |
| 129 | }); | |
| 130 | } | |
| 131 | ); | |
| 132 | } | |
| 133 | | |
4787ec09Artem Egorov7 years ago | 134 | public beforeStartPackager(): Q.Promise<void> { |
| 135 | return this.exponentHelper.configureExponentEnvironment(); | |
0a68f8dbArtem Egorov8 years ago | 136 | } |
| 137 | | |
| 138 | public enableJSDebuggingMode(): Q.Promise<void> { | |
d7d405aeYuri Skorokhodov7 years ago | 139 | this.logger.info(localize("ApplicationIsRunningOnExponentShakeDeviceForRemoteDebugging", "Application is running on Expo. Please shake device and select 'Debug JS Remotely' to enable debugging.")); |
0a68f8dbArtem Egorov8 years ago | 140 | return Q.resolve<void>(void 0); |
| 141 | } | |
db6fd42aRuslan Bikkinin7 years ago | 142 | |
cbc7ac5bArtem Egorov7 years ago | 143 | public getRunArguments(): string[] { |
db6fd42aRuslan Bikkinin7 years ago | 144 | return []; |
| 145 | } | |
0a68f8dbArtem Egorov8 years ago | 146 | } |