generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlist.ts
289 lines (267 loc) · 11.9 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* 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';
import type { TableOptions } from '@oclif/table';
import {
getContainerOptions,
getPackageVersionStrings,
INSTALL_URL_BASE,
Package,
PackageVersionListResult,
} from '@salesforce/packaging';
import { requiredHubFlag } from '../../../utils/hubFlag.js';
import { maybeGetProject } from '../../../utils/getProject.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_version_list');
const packaging = Messages.loadMessages('@salesforce/plugin-packaging', 'packaging');
export type PackageVersionListDetails = Omit<
PackageVersionListResult,
| 'HasMetadataRemoved'
| 'IsReleased'
| 'IsPasswordProtected'
| 'HasPassedCodeCoverageCheck'
| 'CreatedById'
| 'BuildDurationInSeconds'
| 'CodeCoverage'
| 'Package2'
> & {
HasMetadataRemoved: string;
IsPasswordProtected: string | boolean;
IsReleased: string | boolean;
HasPassedCodeCoverageCheck: string | boolean;
BuildDurationInSeconds: string | number;
CodeCoverage: string;
NamespacePrefix: string;
Package2Name: string;
Version: string;
InstallUrl: string;
AncestorVersion: string;
Alias: string;
IsOrgDependent: 'N/A' | 'Yes' | 'No';
CreatedBy: string;
ValidatedAsync?: boolean;
};
export type PackageVersionListCommandResult = PackageVersionListDetails[];
export class PackageVersionListCommand extends SfCommand<PackageVersionListCommandResult> {
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:version:list'];
public static readonly flags = {
loglevel,
'target-dev-hub': requiredHubFlag,
'api-version': orgApiVersionFlagWithDeprecations,
'created-last-days': Flags.integer({
char: 'c',
deprecateAliases: true,
aliases: ['createdlastdays'],
summary: packaging.getMessage('flags.created-last-days.summary'),
}),
concise: Flags.boolean({
summary: messages.getMessage('flags.concise.summary'),
}),
'show-conversions-only': Flags.boolean({
summary: messages.getMessage('flags.show-conversions-only.summary'),
}),
'modified-last-days': Flags.integer({
char: 'm',
deprecateAliases: true,
aliases: ['modifiedlastdays'],
summary: packaging.getMessage('flags.modified-last-days.summary'),
}),
packages: Flags.string({
char: 'p',
summary: messages.getMessage('flags.packages.summary'),
}),
released: Flags.boolean({
char: 'r',
summary: messages.getMessage('flags.released.summary'),
}),
branch: Flags.string({
char: 'b',
summary: messages.getMessage('flags.branch.summary'),
}),
'order-by': Flags.string({
// eslint-disable-next-line sf-plugin/dash-o
char: 'o',
deprecateAliases: true,
aliases: ['orderby'],
summary: messages.getMessage('flags.order-by.summary'),
}),
verbose: Flags.boolean({
summary: messages.getMessage('flags.verbose.summary'),
}),
};
public async run(): Promise<PackageVersionListCommandResult> {
const { flags } = await this.parse(PackageVersionListCommand);
const connection = flags['target-dev-hub'].getConnection(flags['api-version']);
const project = await maybeGetProject();
const records = await Package.listVersions(connection, project, {
createdLastDays: flags['created-last-days'],
concise: flags.concise,
modifiedLastDays: flags['modified-last-days'],
packages: flags.packages?.split(',') ?? [],
isReleased: flags.released,
orderBy: flags['order-by'],
verbose: flags.verbose,
branch: flags.branch,
showConversionsOnly: flags['show-conversions-only'],
});
const results: PackageVersionListCommandResult = [];
if (records?.length > 0) {
let ancestorVersionsMap: Map<string, string> | undefined;
// lookup ancestorVersions if ancestorIds are present
const ancestorIds = records.filter((record) => record.AncestorId).map((record) => record.AncestorId);
if (ancestorIds?.length > 0) {
ancestorVersionsMap = await getPackageVersionStrings(ancestorIds, connection);
}
// Get the container options for each package version. We need this for determining if the version is OrgDependent
const recordIds = [...new Set(records.map((record) => record.Package2Id))];
const containerOptionsMap = await getContainerOptions(recordIds, connection);
records.forEach((record) => {
const ids = [record.Id, record.SubscriberPackageVersionId];
const aliases = ids.map((id) => (project ? project.getAliasesFromPackageId(id) : id)).flat();
const AliasStr = project ? (aliases.length > 0 ? aliases.join() : '') : '';
// set Ancestor display values
let ancestorVersion: string | undefined;
if (record.AncestorId) {
ancestorVersion = ancestorVersionsMap?.get(record.AncestorId);
} else if (containerOptionsMap.get(record.Package2Id) !== 'Managed') {
// display N/A if package is unlocked
ancestorVersion = 'N/A';
record.AncestorId = 'N/A';
}
function getCodeCoverage(): string {
if (!flags.verbose) {
return 'use --verbose for code coverage';
}
return record.CodeCoverage?.apexCodeCoveragePercentage != null
? `${record.CodeCoverage.apexCodeCoveragePercentage.toString()}%`
: Boolean(record.Package2.IsOrgDependent) || record.ValidationSkipped
? 'N/A'
: '';
}
const hasPassedCodeCoverageCheck =
record.Package2.IsOrgDependent === true || record.ValidationSkipped
? 'N/A'
: record.HasPassedCodeCoverageCheck;
const isOrgDependent =
containerOptionsMap.get(record.Package2Id) === 'Managed'
? 'N/A'
: record.Package2.IsOrgDependent
? 'Yes'
: 'No';
const hasMetadataRemoved =
containerOptionsMap.get(record.Package2Id) !== 'Managed' ? 'N/A' : record.HasMetadataRemoved ? 'Yes' : 'No';
results.push({
Package2Id: record.Package2Id,
Branch: record.Branch,
Tag: record.Tag,
MajorVersion: record.MajorVersion,
MinorVersion: record.MinorVersion,
PatchVersion: record.PatchVersion,
BuildNumber: record.BuildNumber,
Id: record.Id,
SubscriberPackageVersionId: record.SubscriberPackageVersionId,
ConvertedFromVersionId: record.ConvertedFromVersionId,
Name: record.Name,
NamespacePrefix: record.Package2.NamespacePrefix,
Package2Name: record.Package2.Name,
Description: record.Description,
Version: [record.MajorVersion, record.MinorVersion, record.PatchVersion, record.BuildNumber].join('.'),
// Table output needs string false to display 'false'
IsPasswordProtected: flags.json ? record.IsPasswordProtected : record.IsPasswordProtected.toString(),
IsReleased: flags.json ? record.IsReleased : record.IsReleased.toString(),
CreatedDate: new Date(record.CreatedDate).toISOString().replace('T', ' ').substring(0, 16),
LastModifiedDate: new Date(record.LastModifiedDate).toISOString().replace('T', ' ').substring(0, 16),
InstallUrl: INSTALL_URL_BASE.toString() + record.SubscriberPackageVersionId,
CodeCoverage: getCodeCoverage(),
HasPassedCodeCoverageCheck: hasPassedCodeCoverageCheck as string | boolean,
ValidationSkipped: record.ValidationSkipped,
ValidatedAsync: record.ValidatedAsync,
AncestorId: record.AncestorId,
AncestorVersion: ancestorVersion as string,
Alias: AliasStr,
IsOrgDependent: isOrgDependent,
ReleaseVersion: record.ReleaseVersion == null ? '' : Number.parseFloat(record.ReleaseVersion).toFixed(1),
BuildDurationInSeconds: record.BuildDurationInSeconds ?? '',
HasMetadataRemoved: hasMetadataRemoved,
CreatedBy: record.CreatedById,
Language: record.Language,
});
});
this.table({
title: `Package Versions [${results.length}]`,
data: results,
overflow: 'wrap',
columns: getColumnData(flags.concise, flags.verbose, flags['show-conversions-only']),
});
} else {
this.warn('No results found');
}
return results;
}
}
const getColumnData = (
concise: boolean,
verbose: boolean,
conversions: boolean
): TableOptions<PackageVersionListDetails>['columns'] => {
if (concise) {
return [
{ key: 'Package2Id', name: messages.getMessage('package-id') },
{ key: 'Version', name: messages.getMessage('version') },
{ key: 'SubscriberPackageVersionId', name: messages.getMessage('subscriberPackageVersionId') },
{ key: 'IsReleased', name: 'Released' },
];
}
const defaultCols = [
{ key: 'Package2Name', name: 'Package Name' },
{ key: 'NamespacePrefix', name: 'Namespace' },
{ key: 'Name', name: 'Version Name' },
{ key: 'Version', name: messages.getMessage('version') },
{ key: 'SubscriberPackageVersionId', name: messages.getMessage('subscriberPackageVersionId') },
{ key: 'Alias', name: messages.getMessage('alias') },
{ key: 'IsPasswordProtected', name: messages.getMessage('installKey') },
{ key: 'IsReleased', name: 'Released' },
{ key: 'ValidationSkipped', name: messages.getMessage('validationSkipped') },
{ key: 'ValidatedAsync', name: messages.getMessage('validatedAsync') },
{ key: 'AncestorId', name: 'Ancestor' },
{ key: 'AncestorVersion', name: 'Ancestor Version' },
{ key: 'Branch', name: messages.getMessage('packageBranch') },
];
if (conversions && !verbose) {
defaultCols.push({ key: 'ConvertedFromVersionId', name: messages.getMessage('convertedFromVersionId') });
}
if (!verbose) {
// @ts-expect-error the default cols don't match 1:1 to the data in the table, but that's on purpose
return defaultCols;
} else {
// add additional columns for verbose output
// @ts-expect-error the verbose match 1:1 to the data in the table, but that's on purpose, but the OCLIF types can't determine tha
return defaultCols.concat([
{ key: 'Package2Id', name: messages.getMessage('package-id') },
{ key: 'InstallUrl', name: messages.getMessage('installUrl') },
{ key: 'Id', name: messages.getMessage('id') },
{ key: 'CreatedDate', name: 'Created Date' },
{ key: 'LastModifiedDate', name: 'Last Modified Date' },
{ key: 'Tag', name: messages.getMessage('packageTag') },
{ key: 'Description', name: messages.getMessage('description') },
{ key: 'CodeCoverage', name: messages.getMessage('codeCoverage') },
{ key: 'HasPassedCodeCoverageCheck', name: messages.getMessage('hasPassedCodeCoverageCheck') },
{ key: 'ConvertedFromVersionId', name: messages.getMessage('convertedFromVersionId') },
{ key: 'IsOrgDependent', name: messages.getMessage('isOrgDependent') },
{ key: 'ReleaseVersion', name: messages.getMessage('releaseVersion') },
{ key: 'BuildDurationInSeconds', name: messages.getMessage('buildDurationInSeconds') },
{ key: 'HasMetadataRemoved', name: messages.getMessage('hasMetadataRemoved') },
{ key: 'CreatedBy', name: messages.getMessage('createdBy') },
{ key: 'Language', name: messages.getMessage('language') },
]);
}
};