microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/appcenter/command/commandExecutor.ts
104lines · 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 * as vscode from "vscode"; |
| 5 | import * as Q from "q"; |
| 6 | import * as qs from "qs"; |
| 7 | import * as os from "os"; |
| 8 | |
| 9 | import { ILogger, LogLevel } from "../../log/LogHelper"; |
| 10 | // tslint:disable-next-line:no-var-requires |
| 11 | const opener = require("opener"); |
| 12 | import Auth from "../../appcenter/auth/auth"; |
| 13 | import { AppCenterLoginType } from "../../appcenter/auth/appCenterLoginType"; |
| 14 | import { Profile } from "../../appcenter/auth/profile/profile"; |
| 15 | import { SettingsHelper } from "../../settingsHelper"; |
| 16 | import { AppCenterClient } from "../api/index"; |
| 17 | import { CodePushDeploymentList } from "../codepush/index"; |
| 18 | import { IDefaultCommandParams } from "./commandParams"; |
| 19 | |
| 20 | interface IAppCenterAuth { |
| 21 | login(): Q.Promise<void>; |
| 22 | logout(client: AppCenterClient): Q.Promise<void>; |
| 23 | whoAmI(client: AppCenterClient): Q.Promise<void>; |
| 24 | } |
| 25 | |
| 26 | interface IAppCenterCodePush { |
| 27 | codePushDeploymentList(client: AppCenterClient): Q.Promise<void>; |
| 28 | } |
| 29 | |
| 30 | export class AppCenterCommandExecutor implements IAppCenterAuth, IAppCenterCodePush { |
| 31 | private logger: ILogger; |
| 32 | |
| 33 | constructor(logger: ILogger) { |
| 34 | this.logger = logger; |
| 35 | } |
| 36 | |
| 37 | public login(): Q.Promise<void> { |
| 38 | const appCenterLoginOptions: string[] = Object.keys(AppCenterLoginType).filter(k => typeof AppCenterLoginType[k as any] === "number"); |
| 39 | vscode.window.showQuickPick(appCenterLoginOptions, { placeHolder: "Please select the way you would like to login to AppCenter" }) |
| 40 | .then((loginType) => { |
| 41 | switch (loginType) { |
| 42 | case (AppCenterLoginType[AppCenterLoginType.Interactive]): |
| 43 | const loginUrl = SettingsHelper.getAppCenterLoginEndpoint() + "?" + qs.stringify({ hostname: os.hostname()}); |
| 44 | vscode.window.showInformationMessage("Please login to AppCenter in the browser window we will open, then enter your token from the browser to vscode", ...["OK"]) |
| 45 | .then(() => { |
| 46 | opener(loginUrl); |
| 47 | vscode.window.showInputBox({ prompt: "Please provide token to authenticate", ignoreFocusOut: true }).then(token => { |
| 48 | if (token) { |
| 49 | return Auth.doTokenLogin(token).then((profile: Profile) => { |
| 50 | vscode.window.showInformationMessage(`Successfully logged in as ${profile.displayName}`); |
| 51 | }); |
| 52 | } else { return Q.resolve(void 0); } |
| 53 | }); |
| 54 | }); |
| 55 | break; |
| 56 | case (AppCenterLoginType[AppCenterLoginType.Token]): |
| 57 | vscode.window.showInputBox({ prompt: "Please provide token to authenticate" , ignoreFocusOut: true}).then(token => { |
| 58 | if (token) { |
| 59 | return Auth.doTokenLogin(token).then((profile: Profile) => { |
| 60 | vscode.window.showInformationMessage(`Successfully logged in as ${profile.displayName}`); |
| 61 | }); |
| 62 | } else { return Q.resolve(void 0); } |
| 63 | }); |
| 64 | break; |
| 65 | default: |
| 66 | throw new Error("Unsupported login parameter!"); |
| 67 | } |
| 68 | }); |
| 69 | return Q.resolve(void 0); |
| 70 | } |
| 71 | |
| 72 | public logout(client: AppCenterClient): Q.Promise<void> { |
| 73 | return Auth.doLogout().then(() => { |
| 74 | vscode.window.showInformationMessage("Successfully logged out from AppCenter"); |
| 75 | }).catch(() => { |
| 76 | this.logger.log("An errro occured on logout", LogLevel.Error); |
| 77 | }); |
| 78 | } |
| 79 | |
| 80 | public whoAmI(client: AppCenterClient): Q.Promise<void> { |
| 81 | return Auth.whoAmI().then((displayName: string) => { |
| 82 | if (displayName) { |
| 83 | vscode.window.showInformationMessage(`You are logged in as ${displayName}`); |
| 84 | } else { |
| 85 | vscode.window.showInformationMessage(`You are not logged in to AppCenter`); |
| 86 | } |
| 87 | }); |
| 88 | } |
| 89 | |
| 90 | public codePushDeploymentList(client: AppCenterClient): Q.Promise<void> { |
| 91 | let params: any = {}; |
| 92 | params.app = {}; |
| 93 | // TODO: get it for real! |
| 94 | params.app.appName = "UpdatedViaCLI"; |
| 95 | params.app.ownerName = "max-mironov"; |
| 96 | params.app.identifier = "max-mironov/UpdatedViaClI"; |
| 97 | |
| 98 | return CodePushDeploymentList.exec(client, <IDefaultCommandParams>params, this.logger).then((result) => { |
| 99 | if (result.succeeded) { |
| 100 | vscode.window.showInformationMessage(`Got deployments!`); |
| 101 | } |
| 102 | }); |
| 103 | } |
| 104 | } |