generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 8
/
create.ts
151 lines (141 loc) · 5.39 KB
/
create.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
/*
* 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,
requiredOrgFlagWithDeprecations,
SfCommand,
} from '@salesforce/sf-plugins-core';
import { Lifecycle, Messages } from '@salesforce/core';
import { Duration } from '@salesforce/kit';
import { Package1Version, PackageVersionEvents, PackagingSObjects } from '@salesforce/packaging';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package1_version_create');
export type PackageUploadRequest = PackagingSObjects.PackageUploadRequest;
export class Package1VersionCreateCommand extends SfCommand<PackageUploadRequest> {
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:package1:version:create'];
public static readonly flags = {
loglevel,
'target-org': requiredOrgFlagWithDeprecations,
'api-version': orgApiVersionFlagWithDeprecations,
'package-id': Flags.salesforceId({
length: 'both',
deprecateAliases: true,
aliases: ['packageid'],
char: 'i',
summary: messages.getMessage('flags.package-id.summary'),
required: true,
startsWith: '033',
}),
name: Flags.string({
char: 'n',
summary: messages.getMessage('flags.name.summary'),
required: true,
}),
description: Flags.string({
char: 'd',
summary: messages.getMessage('flags.description.summary'),
}),
version: Flags.string({
char: 'v',
summary: messages.getMessage('flags.version.summary'),
}),
'managed-released': Flags.boolean({
char: 'm',
deprecateAliases: true,
aliases: ['managedrelease'],
summary: messages.getMessage('flags.managed-released.summary'),
description: messages.getMessage('flags.managed-released.description'),
}),
'release-notes-url': Flags.string({
char: 'r',
deprecateAliases: true,
aliases: ['releasenotesurl'],
summary: messages.getMessage('flags.release-notes-url.summary'),
description: messages.getMessage('flags.release-notes-url.description'),
}),
'post-install-url': Flags.string({
char: 'p',
deprecateAliases: true,
aliases: ['postinstallurl'],
summary: messages.getMessage('flags.post-install-url.summary'),
description: messages.getMessage('flags.post-install-url.description'),
}),
'installation-key': Flags.string({
char: 'k',
deprecateAliases: true,
aliases: ['installationkey'],
summary: messages.getMessage('flags.installation-key.summary'),
}),
wait: Flags.duration({
unit: 'minutes',
char: 'w',
summary: messages.getMessage('flags.wait.summary'),
}),
};
public async run(): Promise<PackageUploadRequest> {
const { flags } = await this.parse(Package1VersionCreateCommand);
const version = this.parseVersion(flags.version as string);
if (flags.wait) {
// if we're waiting for the request, set up the listener
Lifecycle.getInstance().on(
PackageVersionEvents.create.progress,
// the 'on' method requires an async method, but we don't have any async calls
// eslint-disable-next-line @typescript-eslint/require-await
async (data: { timeout: number; pollingResult: PackageUploadRequest }) => {
this.log(
`Package upload is ${data.pollingResult.Status === 'QUEUED' ? 'enqueued' : 'in progress'}. Waiting ${
data.timeout
} more seconds`
);
}
);
}
const result = await Package1Version.create(
flags['target-org'].getConnection(flags['api-version']),
{
MetadataPackageId: flags['package-id'],
VersionName: flags.name,
Description: flags.description,
MajorVersion: version.major,
MinorVersion: version.minor,
IsReleaseVersion: flags['managed-released'],
ReleaseNotesUrl: flags['release-notes-url'],
PostInstallUrl: flags['post-install-url'],
Password: flags['installation-key'],
},
{ frequency: Duration.seconds(5), timeout: flags.wait ?? Duration.seconds(0) }
);
const arg =
result.Status === 'SUCCESS'
? [result.MetadataPackageVersionId]
: [this.config.bin, result.Id, flags['target-org'].getUsername()];
this.log(messages.getMessage(result.Status, arg));
return result;
}
// eslint-disable-next-line class-methods-use-this
private parseVersion(versionString: string): { major: number | undefined; minor: number | undefined } {
const versions = versionString?.split('.');
if (!versions) {
// return nulls so when no version flag is provided, the server can infer the correct version
return { major: undefined, minor: undefined };
}
if (versions.length === 2) {
return {
major: Number(versions[0]),
minor: Number(versions[1]),
};
} else {
throw messages.createError('package1VersionCreateCommandInvalidVersion', [versionString]);
}
}
}