|
| 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 {InstanceCommand} from '@salesforce/b2c-tooling-sdk/cli'; |
| 8 | +import { |
| 9 | + type CartridgePathResult, |
| 10 | + type CartridgePosition, |
| 11 | + BM_SITE_ID, |
| 12 | + addCartridge, |
| 13 | +} from '@salesforce/b2c-tooling-sdk/operations/sites'; |
| 14 | +import {t, withDocs} from '../../../i18n/index.js'; |
| 15 | + |
| 16 | +export default class SitesCartridgesAdd extends InstanceCommand<typeof SitesCartridgesAdd> { |
| 17 | + static description = withDocs( |
| 18 | + t('commands.sites.cartridges.add.description', "Add a cartridge to a site's cartridge path"), |
| 19 | + '/cli/sites.html#b2c-sites-cartridges-add', |
| 20 | + ); |
| 21 | + |
| 22 | + static enableJsonFlag = true; |
| 23 | + |
| 24 | + static examples = [ |
| 25 | + '<%= config.bin %> sites cartridges add my_cartridge --site-id RefArch', |
| 26 | + '<%= config.bin %> sites cartridges add my_cartridge --site-id RefArch --position first', |
| 27 | + '<%= config.bin %> sites cartridges add my_cartridge --site-id RefArch --position after --target app_storefront_base', |
| 28 | + '<%= config.bin %> sites cartridges add bm_extension --bm', |
| 29 | + ]; |
| 30 | + |
| 31 | + static hiddenAliases = ['sites:cartridge:add']; |
| 32 | + |
| 33 | + static args = { |
| 34 | + cartridge: Args.string({ |
| 35 | + description: t('args.cartridge.description', 'Cartridge name to add'), |
| 36 | + required: true, |
| 37 | + }), |
| 38 | + }; |
| 39 | + |
| 40 | + static flags = { |
| 41 | + 'site-id': Flags.string({ |
| 42 | + description: t('flags.siteId.description', 'Site ID (e.g. RefArch)'), |
| 43 | + exclusive: ['bm'], |
| 44 | + }), |
| 45 | + bm: Flags.boolean({ |
| 46 | + description: t('flags.bm.description', 'Use Business Manager site (Sites-Site)'), |
| 47 | + exclusive: ['site-id'], |
| 48 | + }), |
| 49 | + position: Flags.string({ |
| 50 | + description: t('flags.position.description', 'Position to add the cartridge'), |
| 51 | + options: ['first', 'last', 'before', 'after'], |
| 52 | + default: 'first', |
| 53 | + }), |
| 54 | + target: Flags.string({ |
| 55 | + description: t('flags.target.description', 'Target cartridge (required when position is before/after)'), |
| 56 | + }), |
| 57 | + }; |
| 58 | + |
| 59 | + async run(): Promise<CartridgePathResult> { |
| 60 | + this.requireOAuthCredentials(); |
| 61 | + |
| 62 | + const siteId = this.resolveSiteId(); |
| 63 | + const {cartridge} = this.args; |
| 64 | + const position = this.flags.position as CartridgePosition; |
| 65 | + const {target} = this.flags; |
| 66 | + |
| 67 | + // Validate target is provided for relative positions |
| 68 | + if ((position === 'before' || position === 'after') && !target) { |
| 69 | + this.error( |
| 70 | + t('commands.sites.cartridges.add.targetRequired', '--target is required when --position is "{{position}}"', { |
| 71 | + position, |
| 72 | + }), |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + const result = await addCartridge( |
| 77 | + this.instance, |
| 78 | + siteId, |
| 79 | + {name: cartridge, position, target}, |
| 80 | + { |
| 81 | + log: (msg) => { |
| 82 | + if (!this.jsonEnabled()) this.log(msg); |
| 83 | + }, |
| 84 | + waitOptions: { |
| 85 | + onProgress: (exec, elapsed) => { |
| 86 | + if (!this.jsonEnabled()) { |
| 87 | + const elapsedSec = Math.floor(elapsed / 1000); |
| 88 | + this.log( |
| 89 | + t('commands.sites.cartridges.jobProgress', ' Status: {{status}} ({{elapsed}}s elapsed)', { |
| 90 | + status: exec.execution_status, |
| 91 | + elapsed: elapsedSec.toString(), |
| 92 | + }), |
| 93 | + ); |
| 94 | + } |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + ); |
| 99 | + |
| 100 | + if (this.jsonEnabled()) { |
| 101 | + return result; |
| 102 | + } |
| 103 | + |
| 104 | + this.log( |
| 105 | + t('commands.sites.cartridges.add.success', 'Added "{{cartridge}}" to site "{{siteId}}" cartridge path.', { |
| 106 | + cartridge, |
| 107 | + siteId, |
| 108 | + }), |
| 109 | + ); |
| 110 | + this.log( |
| 111 | + t('commands.sites.cartridges.add.updatedPath', 'Updated path: {{cartridges}}', { |
| 112 | + cartridges: result.cartridges, |
| 113 | + }), |
| 114 | + ); |
| 115 | + |
| 116 | + return result; |
| 117 | + } |
| 118 | + |
| 119 | + private resolveSiteId(): string { |
| 120 | + const siteId = this.flags['site-id']; |
| 121 | + const bm = this.flags.bm; |
| 122 | + |
| 123 | + if (!siteId && !bm) { |
| 124 | + this.error(t('commands.sites.cartridges.siteIdRequired', 'Provide --site-id <id> or --bm to specify a site.')); |
| 125 | + } |
| 126 | + |
| 127 | + return bm ? BM_SITE_ID : siteId!; |
| 128 | + } |
| 129 | +} |
0 commit comments