|
| 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 {Flags} from '@oclif/core'; |
| 7 | +import {MrtCommand, createTable, type ColumnDef} from '@salesforce/b2c-tooling-sdk/cli'; |
| 8 | +import { |
| 9 | + getB2CTargetInfo, |
| 10 | + setB2CTargetInfo, |
| 11 | + updateB2CTargetInfo, |
| 12 | + type B2CTargetInfo, |
| 13 | +} from '@salesforce/b2c-tooling-sdk/operations/mrt'; |
| 14 | +import {t} from '../../../i18n/index.js'; |
| 15 | + |
| 16 | +type InfoEntry = {field: string; value: string}; |
| 17 | + |
| 18 | +const COLUMNS: Record<string, ColumnDef<InfoEntry>> = { |
| 19 | + field: { |
| 20 | + header: 'Field', |
| 21 | + get: (e) => e.field, |
| 22 | + }, |
| 23 | + value: { |
| 24 | + header: 'Value', |
| 25 | + get: (e) => e.value, |
| 26 | + }, |
| 27 | +}; |
| 28 | + |
| 29 | +const DEFAULT_COLUMNS = ['field', 'value']; |
| 30 | + |
| 31 | +/** |
| 32 | + * Get or update B2C Commerce info for a target/environment. |
| 33 | + */ |
| 34 | +export default class MrtB2CTargetInfo extends MrtCommand<typeof MrtB2CTargetInfo> { |
| 35 | + static description = t( |
| 36 | + 'commands.mrt.b2c.target-info.description', |
| 37 | + 'Get or update B2C Commerce connection for a target/environment', |
| 38 | + ); |
| 39 | + |
| 40 | + static enableJsonFlag = true; |
| 41 | + |
| 42 | + static examples = [ |
| 43 | + '<%= config.bin %> <%= command.id %> -p my-storefront -e production', |
| 44 | + '<%= config.bin %> <%= command.id %> -p my-storefront -e production --instance-id aaaa_prd', |
| 45 | + '<%= config.bin %> <%= command.id %> -p my-storefront -e production --instance-id aaaa_prd --sites RefArch,SiteGenesis', |
| 46 | + '<%= config.bin %> <%= command.id %> -p my-storefront -e production --json', |
| 47 | + ]; |
| 48 | + |
| 49 | + static flags = { |
| 50 | + ...MrtCommand.baseFlags, |
| 51 | + 'instance-id': Flags.string({ |
| 52 | + description: 'B2C Commerce instance ID to connect', |
| 53 | + }), |
| 54 | + sites: Flags.string({ |
| 55 | + description: 'Comma-separated list of site IDs to connect', |
| 56 | + }), |
| 57 | + 'clear-sites': Flags.boolean({ |
| 58 | + description: 'Clear the sites list', |
| 59 | + default: false, |
| 60 | + }), |
| 61 | + }; |
| 62 | + |
| 63 | + async run(): Promise<B2CTargetInfo> { |
| 64 | + this.requireMrtCredentials(); |
| 65 | + |
| 66 | + const {mrtProject: project, mrtEnvironment: environment} = this.resolvedConfig.values; |
| 67 | + |
| 68 | + if (!project) { |
| 69 | + this.error( |
| 70 | + 'MRT project is required. Provide --project flag, set SFCC_MRT_PROJECT, or set mrtProject in dw.json.', |
| 71 | + ); |
| 72 | + } |
| 73 | + if (!environment) { |
| 74 | + this.error( |
| 75 | + 'MRT environment is required. Provide --environment flag, set SFCC_MRT_ENVIRONMENT, or set mrtEnvironment in dw.json.', |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + const instanceId = this.flags['instance-id']; |
| 80 | + const sitesStr = this.flags.sites; |
| 81 | + const clearSites = this.flags['clear-sites']; |
| 82 | + |
| 83 | + // If instance-id is provided, set or update the target info |
| 84 | + if (instanceId) { |
| 85 | + this.log( |
| 86 | + t('commands.mrt.b2c.target-info.setting', 'Setting B2C info for {{project}}/{{environment}}...', { |
| 87 | + project, |
| 88 | + environment, |
| 89 | + }), |
| 90 | + ); |
| 91 | + |
| 92 | + const sites = clearSites ? null : sitesStr ? sitesStr.split(',').map((s) => s.trim()) : undefined; |
| 93 | + |
| 94 | + const info = await setB2CTargetInfo( |
| 95 | + { |
| 96 | + projectSlug: project, |
| 97 | + targetSlug: environment, |
| 98 | + instanceId, |
| 99 | + sites, |
| 100 | + origin: this.resolvedConfig.values.mrtOrigin, |
| 101 | + }, |
| 102 | + this.getMrtAuth(), |
| 103 | + ); |
| 104 | + |
| 105 | + if (!this.jsonEnabled()) { |
| 106 | + this.log(t('commands.mrt.b2c.target-info.updated', 'B2C target info updated successfully.')); |
| 107 | + this.displayInfo(info); |
| 108 | + } |
| 109 | + |
| 110 | + return info; |
| 111 | + } |
| 112 | + |
| 113 | + // If only sites or clear-sites is provided, update |
| 114 | + if (sitesStr !== undefined || clearSites) { |
| 115 | + this.log( |
| 116 | + t('commands.mrt.b2c.target-info.updating', 'Updating B2C info for {{project}}/{{environment}}...', { |
| 117 | + project, |
| 118 | + environment, |
| 119 | + }), |
| 120 | + ); |
| 121 | + |
| 122 | + const sites = clearSites ? null : sitesStr ? sitesStr.split(',').map((s) => s.trim()) : undefined; |
| 123 | + |
| 124 | + const info = await updateB2CTargetInfo( |
| 125 | + { |
| 126 | + projectSlug: project, |
| 127 | + targetSlug: environment, |
| 128 | + sites, |
| 129 | + origin: this.resolvedConfig.values.mrtOrigin, |
| 130 | + }, |
| 131 | + this.getMrtAuth(), |
| 132 | + ); |
| 133 | + |
| 134 | + if (!this.jsonEnabled()) { |
| 135 | + this.log(t('commands.mrt.b2c.target-info.updated', 'B2C target info updated successfully.')); |
| 136 | + this.displayInfo(info); |
| 137 | + } |
| 138 | + |
| 139 | + return info; |
| 140 | + } |
| 141 | + |
| 142 | + // Otherwise, get the current info |
| 143 | + this.log( |
| 144 | + t('commands.mrt.b2c.target-info.fetching', 'Fetching B2C info for {{project}}/{{environment}}...', { |
| 145 | + project, |
| 146 | + environment, |
| 147 | + }), |
| 148 | + ); |
| 149 | + |
| 150 | + const info = await getB2CTargetInfo( |
| 151 | + { |
| 152 | + projectSlug: project, |
| 153 | + targetSlug: environment, |
| 154 | + origin: this.resolvedConfig.values.mrtOrigin, |
| 155 | + }, |
| 156 | + this.getMrtAuth(), |
| 157 | + ); |
| 158 | + |
| 159 | + if (!this.jsonEnabled()) { |
| 160 | + this.displayInfo(info); |
| 161 | + } |
| 162 | + |
| 163 | + return info; |
| 164 | + } |
| 165 | + |
| 166 | + private displayInfo(info: B2CTargetInfo): void { |
| 167 | + const entries: InfoEntry[] = [ |
| 168 | + {field: 'Instance ID', value: info.instance_id ?? '-'}, |
| 169 | + {field: 'Sites', value: info.sites && info.sites.length > 0 ? info.sites.join(', ') : 'None'}, |
| 170 | + ]; |
| 171 | + createTable(COLUMNS).render(entries, DEFAULT_COLUMNS); |
| 172 | + } |
| 173 | +} |
0 commit comments