-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathinstall.ts
More file actions
167 lines (154 loc) · 5.95 KB
/
Copy pathinstall.ts
File metadata and controls
167 lines (154 loc) · 5.95 KB
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
/*
* Copyright 2026, Salesforce, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
Flags,
loglevel,
orgApiVersionFlagWithDeprecations,
requiredOrgFlagWithDeprecations,
SfCommand,
} from '@salesforce/sf-plugins-core';
import { BundleSObjects, BundleInstallOptions, PackageBundleInstall } from '@salesforce/packaging';
import { Messages, Lifecycle, Org } from '@salesforce/core';
import { camelCaseToTitleCase, Duration } from '@salesforce/kit';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'bundle_install');
export type BundleInstall = BundleSObjects.PkgBundleVersionInstallReqResult;
export class PackageBundlesInstall extends SfCommand<BundleSObjects.PkgBundleVersionInstallReqResult> {
public static readonly hidden = true;
public static state = 'beta';
public static readonly summary = messages.getMessage('summary');
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessages('examples');
public static readonly requiresProject = true;
public static readonly flags = {
loglevel,
bundle: Flags.string({
char: 'b',
summary: messages.getMessage('flags.bundle.summary'),
required: true,
}),
'target-org': requiredOrgFlagWithDeprecations,
'api-version': orgApiVersionFlagWithDeprecations,
'dev-hub-org': Flags.string({
char: 'd',
summary: messages.getMessage('flags.dev-hub-org.summary'),
description: messages.getMessage('flags.dev-hub-org.description'),
required: true,
}),
wait: Flags.integer({
char: 'w',
summary: messages.getMessage('flags.wait.summary'),
default: 0,
}),
verbose: Flags.boolean({
summary: messages.getMessage('flags.verbose.summary'),
}),
};
public async run(): Promise<BundleSObjects.PkgBundleVersionInstallReqResult> {
const { flags } = await this.parse(PackageBundlesInstall);
// Get the target org connection
const targetOrg = flags['target-org'];
const connection = targetOrg.getConnection(flags['api-version']);
const devHubInput = flags['dev-hub-org'];
let devHubOrgId: string;
// If the input already looks like a 15/18-char org ID starting with 00D, use it directly.
// Otherwise resolve the alias or username through the auth store.
if (/^00D[a-zA-Z0-9]{12}([a-zA-Z0-9]{3})?$/.test(devHubInput)) {
devHubOrgId = devHubInput;
} else {
const devHubOrg = await Org.create({ aliasOrUsername: devHubInput });
devHubOrgId = devHubOrg.getOrgId();
}
const options: BundleInstallOptions = {
connection,
project: this.project!,
PackageBundleVersion: flags.bundle,
DevelopmentOrganization: devHubOrgId,
};
// Set up lifecycle events for progress tracking
Lifecycle.getInstance().on(
'bundle-install-progress',
// no async methods
// eslint-disable-next-line @typescript-eslint/require-await
async (data: BundleSObjects.PkgBundleVersionInstallReqResult & { remainingWaitTime: Duration }) => {
if (
data.InstallStatus !== BundleSObjects.PkgBundleVersionInstallReqStatus.success &&
data.InstallStatus !== BundleSObjects.PkgBundleVersionInstallReqStatus.error
) {
const status = messages.getMessage('bundleInstallWaitingStatus', [
data.remainingWaitTime.minutes,
data.InstallStatus,
]);
if (flags.verbose) {
this.log(status);
} else {
this.spinner.status = status;
}
}
}
);
// Start spinner if polling is enabled and not in verbose mode
const isSpinnerRunning = flags.wait && flags.wait > 0 && !flags.verbose;
if (isSpinnerRunning) {
this.spinner.start('Installing bundle...');
}
let result: BundleSObjects.PkgBundleVersionInstallReqResult;
try {
result = await PackageBundleInstall.installBundle(connection, this.project!, {
...options,
...(flags.wait && flags.wait > 0
? { polling: { timeout: Duration.minutes(flags.wait), frequency: Duration.seconds(5) } }
: undefined),
});
} catch (error) {
// Stop spinner on error
if (isSpinnerRunning) {
this.spinner.stop();
}
throw error;
}
// Stop spinner only if it was started - stop it cleanly without a message
if (isSpinnerRunning) {
this.spinner.stop();
}
switch (result.InstallStatus) {
case BundleSObjects.PkgBundleVersionInstallReqStatus.error: {
const errorText =
result.ValidationError ||
`Bundle installation failed. Run 'sf package bundle install report -i ${result.Id} -o ${
targetOrg.getUsername() ?? 'targetOrg'
}' for more details.`;
throw messages.createError('bundleInstallError', [errorText]);
}
case BundleSObjects.PkgBundleVersionInstallReqStatus.success: {
const bundleVersionId = result.PackageBundleVersionId || flags.bundle;
this.log(
`Successfully installed bundle version ${bundleVersionId} to ${targetOrg.getUsername() ?? 'target org'}`
);
break;
}
default:
this.log(
messages.getMessage('bundleInstallInProgress', [
camelCaseToTitleCase(result.InstallStatus as string),
result.Id,
targetOrg.getUsername() ?? '',
])
);
}
return result;
}
}