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