microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/appcenter/appCenterCommandPalleteHandler.ts
106lines · 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 { AppCenterClient } from "./api/index"; |
| 9 | import { Profile } from "./auth/profile/profile"; |
| 10 | import { AppCenterClientFactory, createAppCenterClient } from "./api/createClient"; |
| 11 | import { SettingsHelper } from "../settingsHelper"; |
| 12 | import { AppCenterCommandType } from "./appCenterConstants"; |
| 13 | import { AppCenterExtensionManager } from "./appCenterExtensionManager"; |
| 14 | import { ACStrings } from "./appCenterStrings"; |
| 15 | import { ACUtils } from "./helpers/utils"; |
| 16 | import { VsCodeUtils } from "./helpers/vscodeUtils"; |
| 17 | |
| 18 | export class AppCenterCommandPalleteHandler { |
| 19 | private commandExecutor: AppCenterCommandExecutor; |
| 20 | private client: AppCenterClient; |
| 21 | private logger: ILogger; |
| 22 | private clientFactory: AppCenterClientFactory; |
| 23 | private appCenterManager: AppCenterExtensionManager; |
| 24 | |
| 25 | constructor(logger: ILogger) { |
| 26 | this.commandExecutor = new AppCenterCommandExecutor(logger); |
| 27 | this.clientFactory = createAppCenterClient(); |
| 28 | this.logger = logger; |
| 29 | } |
| 30 | |
| 31 | public set AppCenterManager(manager: AppCenterExtensionManager) { |
| 32 | this.appCenterManager = manager; |
| 33 | } |
| 34 | |
| 35 | public run(command: AppCenterCommandType): Q.Promise<void> { |
| 36 | return ACUtils.isCodePushProject(this.appCenterManager.projectRootPath).then((isCodePush: boolean) => { |
| 37 | if (!isCodePush) { |
| 38 | VsCodeUtils.ShowWarningMessage(ACStrings.NoCodePushDetectedMsg); |
| 39 | return Q.resolve(void 0); |
| 40 | } else { |
| 41 | // Login is special case |
| 42 | if (command === AppCenterCommandType.Login) { |
| 43 | return this.commandExecutor.login(this.appCenterManager); |
| 44 | } |
| 45 | |
| 46 | return Auth.getProfile(this.appCenterManager.projectRootPath).then((profile: Profile) => { |
| 47 | if (!profile) { |
| 48 | VsCodeUtils.ShowWarningMessage(ACStrings.UserIsNotLoggedInMsg); |
| 49 | return Q.resolve(void 0); |
| 50 | } else { |
| 51 | const clientOrNull: AppCenterClient | null = this.resolveAppCenterClient(profile); |
| 52 | if (clientOrNull) { |
| 53 | this.client = clientOrNull; |
| 54 | |
| 55 | switch (command) { |
| 56 | case (AppCenterCommandType.Logout): |
| 57 | return this.commandExecutor.logout(this.appCenterManager); |
| 58 | |
| 59 | case (AppCenterCommandType.Whoami): |
| 60 | return this.commandExecutor.whoAmI(this.appCenterManager); |
| 61 | |
| 62 | case (AppCenterCommandType.SetCurrentApp): |
| 63 | return this.commandExecutor.setCurrentApp(this.client, this.appCenterManager); |
| 64 | |
| 65 | case (AppCenterCommandType.GetCurrentApp): |
| 66 | return this.commandExecutor.getCurrentApp(this.appCenterManager); |
| 67 | |
| 68 | case (AppCenterCommandType.SetCurrentDeployment): |
| 69 | return this.commandExecutor.setCurrentDeployment(this.appCenterManager); |
| 70 | |
| 71 | case (AppCenterCommandType.CodePushReleaseReact): |
| 72 | return this.commandExecutor.releaseReact(this.client, this.appCenterManager); |
| 73 | |
| 74 | case (AppCenterCommandType.ShowMenu): |
| 75 | return this.commandExecutor.showMenu(this.client, this.appCenterManager); |
| 76 | |
| 77 | case (AppCenterCommandType.SwitchMandatoryPropForRelease): |
| 78 | return this.commandExecutor.switchIsMandatoryForRelease(this.appCenterManager); |
| 79 | |
| 80 | case (AppCenterCommandType.SetTargetBinaryVersionForRelease): |
| 81 | return this.commandExecutor.setTargetBinaryVersionForRelease(this.appCenterManager); |
| 82 | |
| 83 | default: |
| 84 | throw new Error("Unknown App Center command!"); |
| 85 | } |
| 86 | } else { |
| 87 | this.logger.log("Failed to get App Center client", LogLevel.Error); |
| 88 | throw new Error("Failed to get App Center client!"); |
| 89 | } |
| 90 | } |
| 91 | }); |
| 92 | } |
| 93 | }); |
| 94 | } |
| 95 | |
| 96 | private resolveAppCenterClient(profile: Profile): AppCenterClient | null { |
| 97 | if (!this.client) { |
| 98 | if (profile) { |
| 99 | return this.clientFactory.fromProfile(profile, SettingsHelper.getAppCenterAPIEndpoint()); |
| 100 | } else { |
| 101 | throw new Error("No App Center user specified!"); |
| 102 | } |
| 103 | } |
| 104 | return this.client; |
| 105 | } |
| 106 | } |