@salesforce/plugin-packaging
Version:
SF plugin that support Salesforce Packaging Platform
150 lines • 6.43 kB
JavaScript
/*
* 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, SfCommand } from '@salesforce/sf-plugins-core';
import { BundleSObjects, PackageBundleVersion, PackageVersionEvents, } from '@salesforce/packaging';
import { Messages, Lifecycle } from '@salesforce/core';
import { camelCaseToTitleCase, Duration } from '@salesforce/kit';
import { requiredHubFlag } from '../../../../utils/hubFlag.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
// TODO: Update messages
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'bundle_version_create');
export class PackageBundlesCreate extends SfCommand {
static hidden = true;
static state = 'beta';
static summary = messages.getMessage('summary');
static description = messages.getMessage('description');
static examples = messages.getMessages('examples');
static requiresProject = true;
static flags = {
loglevel,
bundle: Flags.string({
char: 'b',
summary: messages.getMessage('flags.bundle.summary'),
required: true,
}),
description: Flags.string({
char: 'd',
summary: messages.getMessage('flags.description.summary'),
}),
'definition-file': Flags.string({
char: 'p',
summary: messages.getMessage('flags.definition-file.summary'),
required: true,
}),
'target-dev-hub': requiredHubFlag,
'api-version': orgApiVersionFlagWithDeprecations,
wait: Flags.integer({
char: 'w',
summary: messages.getMessage('flags.wait.summary'),
default: 0,
}),
verbose: Flags.boolean({
summary: messages.getMessage('flags.verbose.summary'),
}),
'version-number': Flags.string({
char: 'n',
summary: messages.getMessage('flags.version-number.summary'),
}),
};
async run() {
const { flags } = await this.parse(PackageBundlesCreate);
// Parse version number if provided
let majorVersion = '';
let minorVersion = '';
if (flags['version-number']) {
const versionParts = flags['version-number'].split('.');
majorVersion = versionParts[0] || '';
minorVersion = versionParts[1] || '';
}
const options = {
connection: flags['target-dev-hub'].getConnection(flags['api-version']),
project: this.project,
PackageBundle: flags.bundle,
BundleVersionComponentsPath: flags['definition-file'],
Description: flags.description,
MajorVersion: majorVersion,
MinorVersion: minorVersion,
Ancestor: '',
};
Lifecycle.getInstance().on(PackageVersionEvents.create.progress,
// no async methods
// eslint-disable-next-line @typescript-eslint/require-await
async (data) => {
if (data.RequestStatus !== BundleSObjects.PkgBundleVersionCreateReqStatus.success &&
data.RequestStatus !== BundleSObjects.PkgBundleVersionCreateReqStatus.error) {
const status = messages.getMessage('bundleVersionCreateWaitingStatus', [
data.remainingWaitTime.minutes,
data.RequestStatus,
]);
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('Creating bundle version...');
}
let result;
try {
result = await PackageBundleVersion.create({
...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.RequestStatus) {
case BundleSObjects.PkgBundleVersionCreateReqStatus.error: {
// Collect all error messages from both Error array and ValidationError
const errorMessages = [];
if (result.Error && result.Error.length > 0) {
errorMessages.push(...result.Error);
}
if (result.ValidationError) {
errorMessages.push(result.ValidationError);
}
const errorText = errorMessages.length > 0 ? errorMessages.join('\n') : 'Unknown error occurred during bundle version creation';
throw messages.createError('multipleErrors', [errorText]);
}
case BundleSObjects.PkgBundleVersionCreateReqStatus.success: {
// Show the PackageBundleVersionId (1Q8) if available, otherwise show the request ID
const displayId = result.PackageBundleVersionId || result.Id;
this.log(`Successfully created bundle version with ID ${displayId}`);
break;
}
default:
this.log(messages.getMessage('InProgress', [camelCaseToTitleCase(result.RequestStatus), result.Id]));
}
return result;
}
}
//# sourceMappingURL=create.js.map