generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 8
/
list.ts
133 lines (119 loc) · 5.08 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
/*
* 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 { Connection, Messages } from '@salesforce/core';
import { PackageVersion, PackageVersionCreateRequestResult, getPackageVersionNumber } from '@salesforce/packaging';
import chalk from 'chalk';
import { requiredHubFlag } from '../../../../utils/hubFlag.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_version_create_list');
const packaging = Messages.loadMessages('@salesforce/plugin-packaging', 'packaging');
export type CreateListCommandResult = Array<
PackageVersionCreateRequestResult & {
VersionName?: string;
VersionNumber?: string;
}
>;
type Status = 'Queued' | 'InProgress' | 'Success' | 'Error';
export class PackageVersionCreateListCommand extends SfCommand<CreateListCommandResult> {
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:create: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'),
}),
status: Flags.custom<Status>({
options: ['Queued', 'InProgress', 'Success', 'Error'],
})({
char: 's',
summary: messages.getMessage('flags.status.summary'),
}),
'show-conversions-only': Flags.boolean({
summary: messages.getMessage('flags.show-conversions-only.summary'),
}),
verbose: Flags.boolean({
summary: messages.getMessage('flags.verbose.summary'),
}),
};
private connection!: Connection;
public async run(): Promise<CreateListCommandResult> {
const { flags } = await this.parse(PackageVersionCreateListCommand);
this.connection = flags['target-dev-hub'].getConnection(flags['api-version']);
let results = (await PackageVersion.getPackageVersionCreateRequests(this.connection, {
createdlastdays: flags['created-last-days'],
status: flags.status,
showConversionsOnly: flags['show-conversions-only'],
})) as CreateListCommandResult;
if (results.length === 0) {
this.warn('No results found');
} else {
if (flags.verbose) {
try {
results = await this.fetchVerboseData(results);
} catch (err) {
const errMsg = typeof err === 'string' ? err : err instanceof Error ? err.message : 'unknown error';
this.warn(`error when retrieving verbose data (package name and version) due to: ${errMsg}`);
}
}
const data = results.map((r) => ({
Id: r.Id,
Status: r.Status,
'Package Id': r.Package2Id,
'Package Version Id': r.Package2VersionId,
'Subscriber Package Version Id': r.SubscriberPackageVersionId,
Tag: r.Tag,
Branch: r.Branch,
'Created Date': r.CreatedDate,
'Created By': r.CreatedBy,
...(flags['show-conversions-only'] ? { 'Converted From Version Id': r.ConvertedFromVersionId } : {}),
...(flags.verbose ? { 'Version Name': r.VersionName, 'Version Number': r.VersionNumber } : {}),
}));
this.table({ data, overflow: 'wrap', title: chalk.blue(`Package Version Create Requests [${results.length}]`) });
}
return results;
}
// Queries Package2Version for the name and version number of the packages and adds that data
// to the results.
private async fetchVerboseData(results: CreateListCommandResult): Promise<CreateListCommandResult> {
type VersionDataMap = {
[id: string]: { name: string; version: string };
};
// Query for the version name and number data
const versionData = await PackageVersion.queryPackage2Version(this.connection, {
fields: ['Id', 'Name', 'MajorVersion', 'MinorVersion', 'PatchVersion', 'BuildNumber'],
whereClause: "WHERE Id IN ('%IDS%')",
whereClauseItems: results.map((pvcrr) => pvcrr.Package2VersionId).filter(Boolean),
});
const vDataMap: VersionDataMap = {};
versionData.map((vData) => {
if (vData) {
const version = getPackageVersionNumber(vData, true);
vDataMap[vData.Id] = { name: vData.Name, version };
}
});
return results.map((pvcrr) => {
if (vDataMap[pvcrr.Package2VersionId]) {
return {
...pvcrr,
...{
VersionName: vDataMap[pvcrr.Package2VersionId].name,
VersionNumber: vDataMap[pvcrr.Package2VersionId].version,
},
};
} else return pvcrr;
});
}
}