From 4a4d79bcd9f2017e70f75967bb0661ae3eb4aa4a Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Mon, 13 Sep 2021 14:14:01 -0600 Subject: [PATCH] feat: add logout hook --- src/deauthorizer.ts | 38 ++++++++++++++++++++++++++++++++++++++ src/exported.ts | 1 + src/hooks.ts | 5 ++++- src/types/index.ts | 8 ++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/deauthorizer.ts diff --git a/src/deauthorizer.ts b/src/deauthorizer.ts new file mode 100644 index 000000000..c1ccf566b --- /dev/null +++ b/src/deauthorizer.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2020, salesforce.com, inc. + * All rights reserved. + * Licensed under the BSD 3-Clause license. + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause + */ + +import { JsonMap } from '@salesforce/ts-types'; + +export abstract class Deauthorizer { + public async removeAll(): Promise { + const result = { + successes: [], + failures: [], + } as Deauthorizer.Result; + + const environments = await this.find(); + for (const id of Object.keys(environments)) { + try { + await this.remove(id); + result.successes.push(id); + } catch { + result.failures.push(id); + } + } + return result; + } + + public abstract find(): Promise>; + public abstract remove(id: string): Promise; +} + +export namespace Deauthorizer { + export type Result = { + successes: string[]; + failures: string[]; + }; +} diff --git a/src/exported.ts b/src/exported.ts index 499b3767b..13fc355ed 100644 --- a/src/exported.ts +++ b/src/exported.ts @@ -7,6 +7,7 @@ export { generateTableChoices } from './util'; export { Deployable, Deployer } from './deployer'; +export { Deauthorizer } from './deauthorizer'; export { Prompter } from './prompter'; export { SfHook } from './hooks'; export * from './types'; diff --git a/src/hooks.ts b/src/hooks.ts index 637533d98..2f498d613 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -10,13 +10,15 @@ import { Hook, Hooks } from '@oclif/core/lib/interfaces/hooks'; import { cli } from 'cli-ux'; import { Duration, env } from '@salesforce/kit'; import { Deployer } from './deployer'; -import { EnvList, EnvDisplay, JsonObject, Deploy, Login } from './types'; +import { EnvList, EnvDisplay, JsonObject, Deploy, Login, Logout } from './types'; +import { Deauthorizer } from './deauthorizer'; interface SfHooks extends Hooks { 'sf:env:list': EnvList.HookMeta; 'sf:env:display': EnvDisplay.HookMeta; 'sf:deploy': Deploy.HookMeta; 'sf:login': Login.HookMeta; + 'sf:logout': Logout.HookMeta; } type GenericHook = Hook>; @@ -42,4 +44,5 @@ export namespace SfHook { export type EnvDisplay = GenericHook<'sf:env:display', T>; export type Deploy = GenericHook<'sf:deploy', T>; export type Login = Hook<'sf:login', SfHooks>; + export type Logout = Hook<'sf:logout', SfHooks>; } diff --git a/src/types/index.ts b/src/types/index.ts index 22558544c..970f18137 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -5,6 +5,7 @@ * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ +import { Deauthorizer } from '../deauthorizer'; import { Deployer } from '../deployer'; export type JsonObject = { @@ -101,3 +102,10 @@ export namespace Login { return: void; }; } + +export namespace Logout { + export type HookMeta = { + options: {}; + return: T; + }; +}