-
Notifications
You must be signed in to change notification settings - Fork 11
/
auth.ts
58 lines (53 loc) · 2.16 KB
/
auth.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
* 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 { loglevel, SfCommand } from '@salesforce/sf-plugins-core';
import { AuthInfo, Messages, OrgAuthorization } from '@salesforce/core';
type AuthListResult = Omit<OrgAuthorization, 'aliases'> & { alias: string };
export type AuthListResults = AuthListResult[];
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-auth', 'list');
export default class ListAuth extends SfCommand<AuthListResults> {
public static readonly summary = messages.getMessage('summary');
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessages('examples');
public static readonly deprecateAliases = true;
public static readonly aliases = ['force:auth:list', 'auth:list'];
public static readonly flags = {
loglevel,
};
public async run(): Promise<AuthListResults> {
await this.parse(ListAuth);
try {
const auths = await AuthInfo.listAllAuthorizations();
if (auths.length === 0) {
this.log(messages.getMessage('noResultsFound'));
return [];
}
const mappedAuths: AuthListResults = auths.map((auth: OrgAuthorization) => {
const { aliases, ...rest } = auth;
// core3 moved to aliases as a string[], revert to alias as a string
return { ...rest, alias: aliases ? aliases.join(',') : '' };
});
const hasErrors = auths.filter((auth) => !!auth.error).length > 0;
this.table({
data: mappedAuths.map((auth) => ({
ALIAS: auth.alias,
USERNAME: auth.username,
'ORG ID': auth.orgId,
'INSTANCE URL': auth.instanceUrl,
'AUTH METHOD': auth.oauthMethod,
...(hasErrors ? { error: { header: 'ERROR' } } : {}),
})),
title: 'authenticated orgs',
});
return mappedAuths;
} catch (err) {
this.log(messages.getMessage('noResultsFound'));
return [];
}
}
}