microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/appcenter/appCenterCommandPalleteHandler.ts
82lines · 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 { ILogger, LogLevel } from "../log/LogHelper"; |
| 5 | import * as Q from "q"; |
| 6 | import { AppCenterCommandExecutor } from "./command/commandExecutor"; |
| 7 | import Auth from "../appcenter/auth/auth"; |
| 8 | import * as vscode from "vscode"; |
| 9 | import { AppCenterClient } from "./api/index"; |
| 10 | import { getUser } from "./auth/profile/profile"; |
| 11 | import { AppCenterClientFactory, createAppCenterClient } from "./api/createClient"; |
| 12 | import { SettingsHelper } from "../settingsHelper"; |
| 13 | import { AppCenterCommandType } from "./appCenterConstants"; |
| 14 | import { AppCenterExtensionManager } from "./appCenterExtensionManager"; |
| 15 | import { ACStrings } from "./appCenterStrings"; |
| 16 | |
| 17 | export class AppCenterCommandPalleteHandler { |
| 18 | private commandExecutor: AppCenterCommandExecutor; |
| 19 | private client: AppCenterClient; |
| 20 | private logger: ILogger; |
| 21 | private clientFactory: AppCenterClientFactory; |
| 22 | |
| 23 | constructor(logger: ILogger) { |
| 24 | this.commandExecutor = new AppCenterCommandExecutor(logger); |
| 25 | this.clientFactory = createAppCenterClient(); |
| 26 | this.logger = logger; |
| 27 | } |
| 28 | |
| 29 | public run(command: AppCenterCommandType, appCenterManager: AppCenterExtensionManager): Q.Promise<void> { |
| 30 | // Login is special case |
| 31 | if (command === AppCenterCommandType.Login) { |
| 32 | return this.commandExecutor.login(appCenterManager); |
| 33 | } |
| 34 | |
| 35 | return Auth.isAuthenticated().then((isAuthenticated: boolean) => { |
| 36 | if (!isAuthenticated) { |
| 37 | vscode.window.showInformationMessage(ACStrings.UserIsNotLoggedInMsg); |
| 38 | return Q.resolve(void 0); |
| 39 | } else { |
| 40 | const clientOrNull: AppCenterClient | null = this.resolveAppCenterClient(); |
| 41 | if (clientOrNull) { |
| 42 | this.client = clientOrNull; |
| 43 | |
| 44 | switch (command) { |
| 45 | case (AppCenterCommandType.Logout): |
| 46 | return this.commandExecutor.logout(appCenterManager); |
| 47 | |
| 48 | case (AppCenterCommandType.Whoami): |
| 49 | return this.commandExecutor.whoAmI(); |
| 50 | |
| 51 | case (AppCenterCommandType.SetCurrentApp): |
| 52 | return this.commandExecutor.setCurrentApp(); |
| 53 | |
| 54 | case (AppCenterCommandType.GetCurrentApp): |
| 55 | return this.commandExecutor.getCurrentApp(); |
| 56 | |
| 57 | case (AppCenterCommandType.CodePushReleaseReact): |
| 58 | return this.commandExecutor.releaseReact(this.client, appCenterManager); |
| 59 | |
| 60 | default: |
| 61 | throw new Error("Unknown App Center command!"); |
| 62 | } |
| 63 | } else { |
| 64 | this.logger.log("Failed to get App Center client", LogLevel.Error); |
| 65 | throw new Error("Failed to get App Center client!"); |
| 66 | } |
| 67 | } |
| 68 | }); |
| 69 | } |
| 70 | |
| 71 | private resolveAppCenterClient(): AppCenterClient | null { |
| 72 | if (!this.client) { |
| 73 | const user = getUser(); |
| 74 | if (user) { |
| 75 | return this.clientFactory.fromProfile(user, SettingsHelper.getAppCenterAPIEndpoint()); |
| 76 | } else { |
| 77 | throw new Error("No App Center user specified"); |
| 78 | } |
| 79 | } |
| 80 | return this.client; |
| 81 | } |
| 82 | } |