generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 8
/
list.ts
115 lines (107 loc) · 3.98 KB
/
list.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* Copyright (c) 2022, 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 { Flags, loglevel, orgApiVersionFlagWithDeprecations, SfCommand } from '@salesforce/sf-plugins-core';
import { Messages } from '@salesforce/core/messages';
import { Package, PackagingSObjects } from '@salesforce/packaging';
import chalk from 'chalk';
import { SfProject } from '@salesforce/core';
import { requiredHubFlag } from '../../utils/hubFlag.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_list');
export type Package2Result = Required<Pick<PackagingSObjects.Package2, 'Id' | 'Name'>> &
Partial<
Pick<
PackagingSObjects.Package2,
| 'SubscriberPackageId'
| 'Description'
| 'NamespacePrefix'
| 'ContainerOptions'
| 'ConvertedFromPackageId'
| 'PackageErrorUsername'
| 'AppAnalyticsEnabled'
> & {
Alias: string;
CreatedBy: string;
IsOrgDependent: string;
}
>;
export type PackageListCommandResult = Package2Result[];
export class PackageListCommand extends SfCommand<PackageListCommandResult> {
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:package:list'];
public static readonly flags = {
loglevel,
'target-dev-hub': requiredHubFlag,
'api-version': orgApiVersionFlagWithDeprecations,
verbose: Flags.boolean({
summary: messages.getMessage('flags.verbose.summary'),
}),
};
public async run(): Promise<PackageListCommandResult> {
const { flags } = await this.parse(PackageListCommand);
const connection = flags['target-dev-hub'].getConnection(flags['api-version']);
const queryResult = await Package.list(connection);
const results = mapRecordsToResults(queryResult);
this.displayResults(results, flags.verbose, connection.getApiVersion());
return results;
}
private displayResults(results: Package2Result[], verbose = false, apiVersion: string): void {
const data = results.map((r) => ({
'Namespace Prefix': r.NamespacePrefix,
Name: r.Name,
Id: r.Id,
Alias: r.Alias,
Description: r.Description,
ContainerOptions: r.ContainerOptions,
...(verbose
? {
'Package Id': r.SubscriberPackageId,
'Converted From Package Id': r.ConvertedFromPackageId,
'Org-Dependent Unlocked Package': r.IsOrgDependent,
'Error Notification Username': r.PackageErrorUsername,
'Created By': r.CreatedBy,
...(parseInt(apiVersion, 10) >= 59 ? { 'App Analytics Enabled': r.AppAnalyticsEnabled } : {}),
}
: {}),
}));
this.table({ data, title: chalk.blue(`Packages [${results.length}]`) });
}
}
const mapRecordsToResults = (records: PackagingSObjects.Package2[], project?: SfProject): Package2Result[] =>
records
.filter((record) => record.IsDeprecated === false)
.map(
({
Id,
SubscriberPackageId,
Name,
Description,
NamespacePrefix,
ContainerOptions,
ConvertedFromPackageId,
IsOrgDependent,
PackageErrorUsername,
AppAnalyticsEnabled,
CreatedById,
}) => ({
Id,
SubscriberPackageId,
Name,
Description,
NamespacePrefix,
ContainerOptions,
ConvertedFromPackageId,
Alias: project ? project.getAliasesFromPackageId(Id).join() : '',
IsOrgDependent: ContainerOptions === 'Managed' ? 'N/A' : IsOrgDependent ? 'Yes' : 'No',
PackageErrorUsername,
AppAnalyticsEnabled,
CreatedBy: CreatedById,
})
);