microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/exponent/exponentPlatform.ts
110lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import { ErrorHelper } from "../../common/error/errorHelper"; |
| 5 | import { InternalErrorCode } from "../../common/error/internalErrorCode"; |
| 6 | import { IRunOptions } from "../launchArgs"; |
| 7 | import { GeneralMobilePlatform, MobilePlatformDeps } from "../generalMobilePlatform"; |
| 8 | import { ExponentHelper } from "./exponentHelper"; |
| 9 | import { TelemetryHelper } from "../../common/telemetryHelper"; |
| 10 | import { QRCodeContentProvider } from "../qrCodeContentProvider"; |
| 11 | |
| 12 | import * as vscode from "vscode"; |
| 13 | import * as Q from "q"; |
| 14 | import * as XDL from "./xdlInterface"; |
| 15 | import * as url from "url"; |
| 16 | import * as nls from "vscode-nls"; |
| 17 | const localize = nls.loadMessageBundle(); |
| 18 | |
| 19 | |
| 20 | export class ExponentPlatform extends GeneralMobilePlatform { |
| 21 | private exponentTunnelPath: string | null; |
| 22 | private exponentHelper: ExponentHelper; |
| 23 | private qrCodeContentProvider: QRCodeContentProvider = new QRCodeContentProvider(); |
| 24 | |
| 25 | constructor(runOptions: IRunOptions, platformDeps: MobilePlatformDeps = {}) { |
| 26 | super(runOptions, platformDeps); |
| 27 | this.exponentHelper = new ExponentHelper(runOptions.workspaceRoot, runOptions.projectRoot); |
| 28 | this.exponentTunnelPath = null; |
| 29 | } |
| 30 | |
| 31 | public runApp(): Q.Promise<void> { |
| 32 | const extProps = { |
| 33 | platform: { |
| 34 | value: "exponent", |
| 35 | isPii: false, |
| 36 | }, |
| 37 | }; |
| 38 | |
| 39 | return TelemetryHelper.generate("ExponentPlatform.runApp", extProps, () => { |
| 40 | return this.exponentHelper.loginToExponent( |
| 41 | (message, password) => { |
| 42 | return Q.Promise((resolve, reject) => { |
| 43 | vscode.window.showInputBox({ placeHolder: message, password: password }) |
| 44 | .then(login => { |
| 45 | resolve(login || ""); |
| 46 | }, reject); |
| 47 | }); |
| 48 | }, |
| 49 | (message) => { |
| 50 | return Q.Promise((resolve, reject) => { |
| 51 | const okButton = { title: "Ok" }; |
| 52 | const cancelButton = { title: "Cancel", isCloseAffordance: true }; |
| 53 | vscode.window.showInformationMessage(message, {modal: true}, okButton, cancelButton) |
| 54 | .then(answer => { |
| 55 | if (answer === cancelButton) { |
| 56 | reject(ErrorHelper.getInternalError(InternalErrorCode.UserCancelledExpoLogin)); |
| 57 | } |
| 58 | resolve(""); |
| 59 | }, reject); |
| 60 | }); |
| 61 | } |
| 62 | ) |
| 63 | .then(() => |
| 64 | XDL.setOptions(this.projectPath, { packagerPort: this.packager.port }) |
| 65 | ) |
| 66 | .then(() => |
| 67 | XDL.startExponentServer(this.projectPath) |
| 68 | ) |
| 69 | .then(() => |
| 70 | XDL.startTunnels(this.projectPath) |
| 71 | ) |
| 72 | .then(() => |
| 73 | XDL.getUrl(this.projectPath, { dev: true, minify: false }) |
| 74 | ).then(exponentUrl => { |
| 75 | return "exp://" + url.parse(exponentUrl).host; |
| 76 | }) |
| 77 | .catch(reason => { |
| 78 | return Q.reject<string>(reason); |
| 79 | }) |
| 80 | .then(exponentUrl => { |
| 81 | let exponentPage = vscode.window.createWebviewPanel("Expo QR Code", "Expo QR Code", vscode.ViewColumn.Two, { }); |
| 82 | exponentPage.webview.html = this.qrCodeContentProvider.provideTextDocumentContent(vscode.Uri.parse(exponentUrl)); |
| 83 | return exponentUrl; |
| 84 | }) |
| 85 | .then(exponentUrl => { |
| 86 | if (!exponentUrl) { |
| 87 | return Q.reject<void>(ErrorHelper.getInternalError(InternalErrorCode.ExpectedExponentTunnelPath)); |
| 88 | } |
| 89 | this.exponentTunnelPath = exponentUrl; |
| 90 | const outputMessage = localize("ApplicationIsRunningOnExponentOpenToSeeIt", "Application is running on Expo. Open your Expo app at {0} to see it.", this.exponentTunnelPath); |
| 91 | this.logger.info(outputMessage); |
| 92 | |
| 93 | return Q.resolve(void 0); |
| 94 | }); |
| 95 | }); |
| 96 | } |
| 97 | |
| 98 | public beforeStartPackager(): Q.Promise<void> { |
| 99 | return this.exponentHelper.configureExponentEnvironment(); |
| 100 | } |
| 101 | |
| 102 | public enableJSDebuggingMode(): Q.Promise<void> { |
| 103 | this.logger.info(localize("ApplicationIsRunningOnExponentShakeDeviceForRemoteDebugging", "Application is running on Expo. Please shake device and select 'Debug JS Remotely' to enable debugging.")); |
| 104 | return Q.resolve<void>(void 0); |
| 105 | } |
| 106 | |
| 107 | public getRunArguments(): string[] { |
| 108 | return []; |
| 109 | } |
| 110 | } |
| 111 | |