microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/exponent/exponentPlatform.ts
110lines · 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"; | |
| 6 | import { IRunOptions } from "../launchArgs"; | |
| 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 | |
| 25 | constructor(runOptions: IRunOptions, platformDeps: MobilePlatformDeps = {}) { | |
| 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> { | |
031832ffArtem Egorov7 years ago | 32 | const extProps = { |
| 33 | platform: { | |
| 34 | value: "exponent", | |
| 35 | isPii: false, | |
4787ec09Artem Egorov7 years ago | 36 | }, |
031832ffArtem Egorov7 years ago | 37 | }; |
4787ec09Artem Egorov7 years ago | 38 | |
031832ffArtem Egorov7 years ago | 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) => { | |
79069a37Ruslan Bikkinin7 years ago | 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) { | |
d7d405aeYuri Skorokhodov7 years ago | 56 | reject(ErrorHelper.getInternalError(InternalErrorCode.UserCancelledExpoLogin)); |
79069a37Ruslan Bikkinin7 years ago | 57 | } |
| 58 | resolve(""); | |
031832ffArtem Egorov7 years ago | 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 => { | |
3c871141Yuri Skorokhodov7 years ago | 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)); | |
031832ffArtem Egorov7 years ago | 83 | return exponentUrl; |
| 84 | }) | |
| 85 | .then(exponentUrl => { | |
| 86 | if (!exponentUrl) { | |
10f9b914Yuri Skorokhodov7 years ago | 87 | return Q.reject<void>(ErrorHelper.getInternalError(InternalErrorCode.ExpectedExponentTunnelPath)); |
031832ffArtem Egorov7 years ago | 88 | } |
| 89 | this.exponentTunnelPath = exponentUrl; | |
d7d405aeYuri Skorokhodov7 years ago | 90 | const outputMessage = localize("ApplicationIsRunningOnExponentOpenToSeeIt", "Application is running on Expo. Open your Expo app at {0} to see it.", this.exponentTunnelPath); |
031832ffArtem Egorov7 years ago | 91 | this.logger.info(outputMessage); |
| 92 | | |
| 93 | return Q.resolve(void 0); | |
| 94 | }); | |
4787ec09Artem Egorov7 years ago | 95 | }); |
| 96 | } | |
| 97 | | |
| 98 | public beforeStartPackager(): Q.Promise<void> { | |
| 99 | return this.exponentHelper.configureExponentEnvironment(); | |
0a68f8dbArtem Egorov8 years ago | 100 | } |
| 101 | | |
| 102 | public enableJSDebuggingMode(): Q.Promise<void> { | |
d7d405aeYuri Skorokhodov7 years ago | 103 | 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 | 104 | return Q.resolve<void>(void 0); |
| 105 | } | |
db6fd42aRuslan Bikkinin7 years ago | 106 | |
cbc7ac5bArtem Egorov7 years ago | 107 | public getRunArguments(): string[] { |
db6fd42aRuslan Bikkinin7 years ago | 108 | return []; |
| 109 | } | |
0a68f8dbArtem Egorov8 years ago | 110 | } |