|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Salesforce, Inc. |
| 3 | + * SPDX-License-Identifier: Apache-2 |
| 4 | + * For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | + */ |
| 6 | +import {Args, Flags} from '@oclif/core'; |
| 7 | +import {MrtCommand} from '@salesforce/b2c-tooling-sdk/cli'; |
| 8 | +import { |
| 9 | + bulkDeleteBundles, |
| 10 | + deleteBundle, |
| 11 | + type BulkDeleteBundlesResult, |
| 12 | +} from '@salesforce/b2c-tooling-sdk/operations/mrt'; |
| 13 | +import {t, withDocs} from '../../../i18n/index.js'; |
| 14 | +import {confirm} from '../../../prompts.js'; |
| 15 | + |
| 16 | +interface DeleteBundleResult { |
| 17 | + queued: number[]; |
| 18 | + rejected: BulkDeleteBundlesResult['rejected']; |
| 19 | +} |
| 20 | + |
| 21 | +export default class MrtBundleDelete extends MrtCommand<typeof MrtBundleDelete> { |
| 22 | + static args = { |
| 23 | + bundleId: Args.integer({ |
| 24 | + description: 'Bundle ID to delete (additional IDs may follow)', |
| 25 | + required: true, |
| 26 | + }), |
| 27 | + }; |
| 28 | + |
| 29 | + static description = withDocs( |
| 30 | + t('commands.mrt.bundle.delete.description', 'Delete one or more bundles from a Managed Runtime project'), |
| 31 | + '/cli/mrt.html#b2c-mrt-bundle-delete', |
| 32 | + ); |
| 33 | + |
| 34 | + static enableJsonFlag = true; |
| 35 | + |
| 36 | + static examples = [ |
| 37 | + '<%= config.bin %> <%= command.id %> 12345 --project my-storefront', |
| 38 | + '<%= config.bin %> <%= command.id %> 12345 12346 12347 -p my-storefront', |
| 39 | + '<%= config.bin %> <%= command.id %> 12345 -p my-storefront --force', |
| 40 | + ]; |
| 41 | + |
| 42 | + static flags = { |
| 43 | + ...MrtCommand.baseFlags, |
| 44 | + force: Flags.boolean({ |
| 45 | + char: 'f', |
| 46 | + description: 'Skip confirmation prompt', |
| 47 | + default: false, |
| 48 | + }), |
| 49 | + }; |
| 50 | + |
| 51 | + // Allow multiple positional bundle IDs |
| 52 | + static strict = false; |
| 53 | + |
| 54 | + protected operations = { |
| 55 | + deleteBundle, |
| 56 | + bulkDeleteBundles, |
| 57 | + }; |
| 58 | + |
| 59 | + async run(): Promise<DeleteBundleResult> { |
| 60 | + this.requireMrtCredentials(); |
| 61 | + |
| 62 | + const {argv, flags} = await this.parse(MrtBundleDelete); |
| 63 | + const {mrtProject: project} = this.resolvedConfig.values; |
| 64 | + |
| 65 | + if (!project) { |
| 66 | + this.error('MRT project is required. Provide --project flag, set MRT_PROJECT, or set mrtProject in dw.json.'); |
| 67 | + } |
| 68 | + |
| 69 | + const bundleIds: number[] = []; |
| 70 | + for (const arg of argv as Array<number | string>) { |
| 71 | + const n = typeof arg === 'number' ? arg : Number.parseInt(String(arg), 10); |
| 72 | + if (!Number.isInteger(n) || n <= 0) { |
| 73 | + this.error(t('commands.mrt.bundle.delete.invalidId', 'Invalid bundle ID: {{arg}}', {arg: String(arg)})); |
| 74 | + } |
| 75 | + bundleIds.push(n); |
| 76 | + } |
| 77 | + |
| 78 | + if (!flags.force && !this.jsonEnabled()) { |
| 79 | + const message = |
| 80 | + bundleIds.length === 1 |
| 81 | + ? t('commands.mrt.bundle.delete.confirmOne', 'Delete bundle {{id}} from {{project}}?', { |
| 82 | + id: String(bundleIds[0]), |
| 83 | + project, |
| 84 | + }) |
| 85 | + : t('commands.mrt.bundle.delete.confirmMany', 'Delete {{n}} bundles from {{project}}?', { |
| 86 | + n: String(bundleIds.length), |
| 87 | + project, |
| 88 | + }); |
| 89 | + const confirmed = await confirm(message); |
| 90 | + if (!confirmed) { |
| 91 | + this.log(t('commands.mrt.bundle.delete.cancelled', 'Delete cancelled.')); |
| 92 | + return {queued: [], rejected: []}; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + try { |
| 97 | + if (bundleIds.length === 1) { |
| 98 | + const [bundleId] = bundleIds; |
| 99 | + await this.operations.deleteBundle( |
| 100 | + { |
| 101 | + projectSlug: project, |
| 102 | + bundleId, |
| 103 | + origin: this.resolvedConfig.values.mrtOrigin, |
| 104 | + }, |
| 105 | + this.getMrtAuth(), |
| 106 | + ); |
| 107 | + |
| 108 | + if (!this.jsonEnabled()) { |
| 109 | + this.log( |
| 110 | + t('commands.mrt.bundle.delete.queuedOne', 'Bundle {{id}} queued for deletion.', {id: String(bundleId)}), |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + return {queued: [bundleId], rejected: []}; |
| 115 | + } |
| 116 | + |
| 117 | + const result = await this.operations.bulkDeleteBundles( |
| 118 | + { |
| 119 | + projectSlug: project, |
| 120 | + bundleIds, |
| 121 | + origin: this.resolvedConfig.values.mrtOrigin, |
| 122 | + }, |
| 123 | + this.getMrtAuth(), |
| 124 | + ); |
| 125 | + |
| 126 | + if (!this.jsonEnabled()) { |
| 127 | + this.log( |
| 128 | + t('commands.mrt.bundle.delete.queuedMany', '{{n}} bundle(s) queued for deletion.', { |
| 129 | + n: String(result.queued.length), |
| 130 | + }), |
| 131 | + ); |
| 132 | + if (result.rejected.length > 0) { |
| 133 | + this.log(t('commands.mrt.bundle.delete.rejectedHeader', 'Rejected bundles:')); |
| 134 | + for (const r of result.rejected) { |
| 135 | + this.log(` - ${r.bundleId ?? '?'}: ${r.reason}`); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return result; |
| 141 | + } catch (error) { |
| 142 | + if (error instanceof Error) { |
| 143 | + this.error( |
| 144 | + t('commands.mrt.bundle.delete.failed', 'Failed to delete bundle(s): {{message}}', {message: error.message}), |
| 145 | + ); |
| 146 | + } |
| 147 | + throw error; |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments