microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/exponent/exponentPlatform.ts
100lines · 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 | |
| 11 | import * as vscode from "vscode"; |
| 12 | import * as Q from "q"; |
| 13 | import * as XDL from "./xdlInterface"; |
| 14 | import * as url from "url"; |
| 15 | |
| 16 | export class ExponentPlatform extends GeneralMobilePlatform { |
| 17 | private exponentTunnelPath: string | null; |
| 18 | private exponentHelper: ExponentHelper; |
| 19 | |
| 20 | constructor(runOptions: IRunOptions, platformDeps: MobilePlatformDeps = {}) { |
| 21 | super(runOptions, platformDeps); |
| 22 | this.exponentHelper = new ExponentHelper(runOptions.workspaceRoot, runOptions.projectRoot); |
| 23 | this.exponentTunnelPath = null; |
| 24 | } |
| 25 | |
| 26 | public runApp(): Q.Promise<void> { |
| 27 | const extProps = { |
| 28 | platform: { |
| 29 | value: "exponent", |
| 30 | isPii: false, |
| 31 | }, |
| 32 | }; |
| 33 | |
| 34 | return TelemetryHelper.generate("ExponentPlatform.runApp", extProps, () => { |
| 35 | return this.exponentHelper.loginToExponent( |
| 36 | (message, password) => { |
| 37 | return Q.Promise((resolve, reject) => { |
| 38 | vscode.window.showInputBox({ placeHolder: message, password: password }) |
| 39 | .then(login => { |
| 40 | resolve(login || ""); |
| 41 | }, reject); |
| 42 | }); |
| 43 | }, |
| 44 | (message) => { |
| 45 | return Q.Promise((resolve, reject) => { |
| 46 | vscode.window.showInformationMessage(message) |
| 47 | .then(password => { |
| 48 | resolve(password || ""); |
| 49 | }, reject); |
| 50 | }); |
| 51 | } |
| 52 | ) |
| 53 | .then(() => |
| 54 | XDL.setOptions(this.projectPath, { packagerPort: this.packager.port }) |
| 55 | ) |
| 56 | .then(() => |
| 57 | XDL.startExponentServer(this.projectPath) |
| 58 | ) |
| 59 | .then(() => |
| 60 | XDL.startTunnels(this.projectPath) |
| 61 | ) |
| 62 | .then(() => |
| 63 | XDL.getUrl(this.projectPath, { dev: true, minify: false }) |
| 64 | ).then(exponentUrl => { |
| 65 | return "exp://" + url.parse(exponentUrl).host; |
| 66 | }) |
| 67 | .catch(reason => { |
| 68 | return Q.reject<string>(reason); |
| 69 | }) |
| 70 | .then(exponentUrl => { |
| 71 | vscode.commands.executeCommand("vscode.previewHtml", vscode.Uri.parse(exponentUrl), 1, "Expo QR code"); |
| 72 | return exponentUrl; |
| 73 | }) |
| 74 | .then(exponentUrl => { |
| 75 | if (!exponentUrl) { |
| 76 | return Q.reject<void>(ErrorHelper.getInternalError(InternalErrorCode.ExpectedExponentTunnelPath, |
| 77 | "No link provided by exponent. Is your project correctly setup?")); |
| 78 | } |
| 79 | this.exponentTunnelPath = exponentUrl; |
| 80 | const outputMessage = `Application is running on Exponent. Open your exponent app at ${this.exponentTunnelPath} to see it.`; |
| 81 | this.logger.info(outputMessage); |
| 82 | |
| 83 | return Q.resolve(void 0); |
| 84 | }); |
| 85 | }); |
| 86 | } |
| 87 | |
| 88 | public beforeStartPackager(): Q.Promise<void> { |
| 89 | return this.exponentHelper.configureExponentEnvironment(); |
| 90 | } |
| 91 | |
| 92 | public enableJSDebuggingMode(): Q.Promise<void> { |
| 93 | this.logger.info("Application is running on Exponent. Please shake device and select 'Debug JS Remotely' to enable debugging."); |
| 94 | return Q.resolve<void>(void 0); |
| 95 | } |
| 96 | |
| 97 | protected getRunArguments(): string[] { |
| 98 | return []; |
| 99 | } |
| 100 | } |
| 101 | |