microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/exponent/exponentPlatform.ts
85lines · 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 | |
| 10 | import * as vscode from "vscode"; |
| 11 | import * as Q from "q"; |
| 12 | import {PackagerRunAs} from "../../common/packager"; |
| 13 | import {PackagerStatus} from "../packagerStatusIndicator"; |
| 14 | import {SettingsHelper} from "../settingsHelper"; |
| 15 | |
| 16 | const projectRootPath = SettingsHelper.getReactNativeProjectRoot(); |
| 17 | const workspaceRootPath = vscode.workspace.rootPath; |
| 18 | |
| 19 | export class ExponentPlatform extends GeneralMobilePlatform { |
| 20 | private exponentTunnelPath: string | null; |
| 21 | private exponentHelper = new ExponentHelper(workspaceRootPath, projectRootPath); |
| 22 | |
| 23 | constructor(runOptions: IRunOptions, platformDeps: MobilePlatformDeps = {}) { |
| 24 | super(runOptions, platformDeps); |
| 25 | this.exponentTunnelPath = null; |
| 26 | } |
| 27 | |
| 28 | public runApp(): Q.Promise<void> { |
| 29 | const outputMessage = `Application is running on Exponent. Open your exponent app at ${this.exponentTunnelPath} to see it.`; |
| 30 | this.logger.info(outputMessage); |
| 31 | return Q.resolve<void>(void 0); |
| 32 | } |
| 33 | |
| 34 | public enableJSDebuggingMode(): Q.Promise<void> { |
| 35 | this.logger.info("Application is running on Exponent. Please shake device and select 'Debug JS Remotely' to enable debugging."); |
| 36 | return Q.resolve<void>(void 0); |
| 37 | } |
| 38 | |
| 39 | public startPackager(): Q.Promise<void> { |
| 40 | this.logger.info("Starting Exponent Packager."); |
| 41 | return this.packager.isRunning().then((running) => { |
| 42 | if (running) { |
| 43 | if (this.packager.getRunningAs() !== PackagerRunAs.EXPONENT) { |
| 44 | return this.packager.stop().then(() => |
| 45 | this.packageStatusIndicator.updatePackagerStatus(PackagerStatus.PACKAGER_STOPPED)); |
| 46 | } |
| 47 | |
| 48 | this.logger.info("Attaching to running Exponent packager"); |
| 49 | } |
| 50 | return void 0; |
| 51 | }).then(() => |
| 52 | this.exponentHelper.configureExponentEnvironment() |
| 53 | ).then(() => |
| 54 | this.exponentHelper.loginToExponent( |
| 55 | (message, password) => { |
| 56 | return Q.Promise((resolve, reject) => { |
| 57 | vscode.window.showInputBox({ placeHolder: message, password: password }) |
| 58 | .then(resolve, reject); |
| 59 | }); |
| 60 | }, |
| 61 | (message) => { |
| 62 | return Q.Promise((resolve, reject) => { |
| 63 | vscode.window.showInformationMessage(message) |
| 64 | .then(resolve, reject); |
| 65 | }); |
| 66 | } |
| 67 | )) |
| 68 | .then(() => { |
| 69 | return this.packager.startAsExponent(); |
| 70 | }) |
| 71 | .then(exponentUrl => { |
| 72 | vscode.commands.executeCommand("vscode.previewHtml", vscode.Uri.parse(exponentUrl), 1, "Expo QR code"); |
| 73 | this.packageStatusIndicator.updatePackagerStatus(PackagerStatus.EXPONENT_PACKAGER_STARTED); |
| 74 | return exponentUrl; |
| 75 | }) |
| 76 | .then(exponentUrl => { |
| 77 | if (!exponentUrl) { |
| 78 | return Q.reject<void>(ErrorHelper.getInternalError(InternalErrorCode.ExpectedExponentTunnelPath, |
| 79 | "No link provided by exponent. Is your project correctly setup?")); |
| 80 | } |
| 81 | this.exponentTunnelPath = exponentUrl; |
| 82 | return Q.resolve(void 0); |
| 83 | }); |
| 84 | } |
| 85 | } |
| 86 | |