|
| 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 { |
| 8 | + downloadCartridges, |
| 9 | + getActiveCodeVersion, |
| 10 | + type DownloadResult, |
| 11 | +} from '@salesforce/b2c-tooling-sdk/operations/code'; |
| 12 | +import {CartridgeCommand} from '@salesforce/b2c-tooling-sdk/cli'; |
| 13 | +import {t, withDocs} from '../../i18n/index.js'; |
| 14 | + |
| 15 | +export default class CodeDownload extends CartridgeCommand<typeof CodeDownload> { |
| 16 | + static hiddenAliases = ['code:download']; |
| 17 | + |
| 18 | + static args = { |
| 19 | + ...CartridgeCommand.baseArgs, |
| 20 | + }; |
| 21 | + |
| 22 | + static description = withDocs( |
| 23 | + t('commands.code.download.description', 'Download cartridge code from a B2C Commerce instance'), |
| 24 | + '/cli/code.html#b2c-code-download', |
| 25 | + ); |
| 26 | + |
| 27 | + static enableJsonFlag = true; |
| 28 | + |
| 29 | + static examples = [ |
| 30 | + '<%= config.bin %> <%= command.id %>', |
| 31 | + '<%= config.bin %> <%= command.id %> -o ./downloaded', |
| 32 | + '<%= config.bin %> <%= command.id %> --server my-sandbox.demandware.net --code-version v1', |
| 33 | + '<%= config.bin %> <%= command.id %> --mirror', |
| 34 | + '<%= config.bin %> <%= command.id %> -c app_storefront_base -c plugin_applepay', |
| 35 | + '<%= config.bin %> <%= command.id %> -x test_cartridge', |
| 36 | + ]; |
| 37 | + |
| 38 | + static flags = { |
| 39 | + ...CartridgeCommand.baseFlags, |
| 40 | + ...CartridgeCommand.cartridgeFlags, |
| 41 | + output: Flags.string({ |
| 42 | + char: 'o', |
| 43 | + description: 'Output directory for downloaded cartridges', |
| 44 | + default: 'cartridges', |
| 45 | + exclusive: ['mirror'], |
| 46 | + }), |
| 47 | + mirror: Flags.boolean({ |
| 48 | + char: 'm', |
| 49 | + description: 'Extract cartridges to their local project locations', |
| 50 | + default: false, |
| 51 | + exclusive: ['output'], |
| 52 | + }), |
| 53 | + }; |
| 54 | + |
| 55 | + protected operations = { |
| 56 | + downloadCartridges, |
| 57 | + getActiveCodeVersion, |
| 58 | + }; |
| 59 | + |
| 60 | + async run(): Promise<DownloadResult> { |
| 61 | + this.requireWebDavCredentials(); |
| 62 | + |
| 63 | + const hostname = this.resolvedConfig.values.hostname!; |
| 64 | + let version = this.resolvedConfig.values.codeVersion; |
| 65 | + |
| 66 | + const needsOAuth = !version; |
| 67 | + if (needsOAuth && !this.hasOAuthCredentials()) { |
| 68 | + this.error( |
| 69 | + t( |
| 70 | + 'commands.code.download.oauthRequired', |
| 71 | + 'No code version specified. OAuth credentials are required to auto-discover the active code version.\n\nProvide --code-version to use basic auth only, or configure OAuth credentials.\nSee: https://salesforcecommercecloud.github.io/b2c-developer-tooling/guide/configuration.html', |
| 72 | + ), |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + // If no code version specified, discover the active one |
| 77 | + if (!version) { |
| 78 | + this.warn( |
| 79 | + t('commands.code.download.noCodeVersion', 'No code version specified, discovering active code version...'), |
| 80 | + ); |
| 81 | + const activeVersion = await this.operations.getActiveCodeVersion(this.instance); |
| 82 | + if (!activeVersion?.id) { |
| 83 | + this.error( |
| 84 | + t('commands.code.download.noActiveVersion', 'No active code version found. Specify one with --code-version.'), |
| 85 | + ); |
| 86 | + } |
| 87 | + version = activeVersion.id; |
| 88 | + this.instance.config.codeVersion = version; |
| 89 | + } |
| 90 | + |
| 91 | + // Build mirror map if --mirror flag is set |
| 92 | + let mirror: Map<string, string> | undefined; |
| 93 | + if (this.flags.mirror) { |
| 94 | + const cartridges = await this.findCartridgesWithProviders(); |
| 95 | + if (cartridges.length === 0) { |
| 96 | + this.error( |
| 97 | + t('commands.code.download.noLocalCartridges', 'No local cartridges found in {{path}} for mirror mode', { |
| 98 | + path: this.cartridgePath, |
| 99 | + }), |
| 100 | + ); |
| 101 | + } |
| 102 | + mirror = new Map(cartridges.map((c) => [c.name, c.src])); |
| 103 | + } |
| 104 | + |
| 105 | + // Create lifecycle context |
| 106 | + const context = this.createContext('code:download', { |
| 107 | + cartridgePath: this.cartridgePath, |
| 108 | + hostname, |
| 109 | + codeVersion: version, |
| 110 | + mirror: this.flags.mirror, |
| 111 | + output: this.flags.output, |
| 112 | + ...this.cartridgeOptions, |
| 113 | + }); |
| 114 | + |
| 115 | + // Run beforeOperation hooks |
| 116 | + const beforeResult = await this.runBeforeHooks(context); |
| 117 | + if (beforeResult.skip) { |
| 118 | + this.log( |
| 119 | + t('commands.code.download.skipped', 'Download skipped: {{reason}}', { |
| 120 | + reason: beforeResult.skipReason || 'skipped by plugin', |
| 121 | + }), |
| 122 | + ); |
| 123 | + return { |
| 124 | + cartridges: [], |
| 125 | + codeVersion: version, |
| 126 | + outputDirectory: this.flags.output ?? 'cartridges', |
| 127 | + }; |
| 128 | + } |
| 129 | + |
| 130 | + this.log( |
| 131 | + t('commands.code.download.downloading', 'Downloading code version "{{version}}" from {{hostname}}...', { |
| 132 | + version, |
| 133 | + hostname, |
| 134 | + }), |
| 135 | + ); |
| 136 | + |
| 137 | + // Temporarily allow DELETE for zip cleanup |
| 138 | + const cleanupSafetyRule = this.safetyGuard.temporarilyAddRule({ |
| 139 | + method: 'DELETE', |
| 140 | + path: '**/Cartridges/*.zip', |
| 141 | + action: 'allow', |
| 142 | + }); |
| 143 | + |
| 144 | + try { |
| 145 | + const phaseLabels = { |
| 146 | + zipping: t('commands.code.download.zipping', 'Archiving code version'), |
| 147 | + downloading: t('commands.code.download.downloadingZip', 'Downloading cartridges'), |
| 148 | + cleanup: t('commands.code.download.cleanup', 'Cleaning up'), |
| 149 | + extracting: t('commands.code.download.extracting', 'Extracting cartridges'), |
| 150 | + }; |
| 151 | + |
| 152 | + const result = await this.operations.downloadCartridges(this.instance, this.flags.output ?? 'cartridges', { |
| 153 | + include: this.cartridgeOptions.include, |
| 154 | + exclude: this.cartridgeOptions.exclude, |
| 155 | + mirror, |
| 156 | + onProgress: (info) => { |
| 157 | + if (this.jsonEnabled()) return; |
| 158 | + const label = phaseLabels[info.phase]; |
| 159 | + if (info.elapsedSeconds === 0) { |
| 160 | + this.log(` ${label}...`); |
| 161 | + } else { |
| 162 | + this.log( |
| 163 | + t('commands.code.download.elapsed', ' {{label}}... ({{elapsed}}s elapsed)', { |
| 164 | + label, |
| 165 | + elapsed: String(info.elapsedSeconds), |
| 166 | + }), |
| 167 | + ); |
| 168 | + } |
| 169 | + }, |
| 170 | + }); |
| 171 | + |
| 172 | + this.log( |
| 173 | + t('commands.code.download.summary', 'Downloaded {{count}} cartridge(s) from version "{{codeVersion}}"', { |
| 174 | + count: result.cartridges.length, |
| 175 | + codeVersion: result.codeVersion, |
| 176 | + }), |
| 177 | + ); |
| 178 | + |
| 179 | + for (const name of result.cartridges) { |
| 180 | + this.log(` ${name}`); |
| 181 | + } |
| 182 | + |
| 183 | + // Run afterOperation hooks with success |
| 184 | + await this.runAfterHooks(context, { |
| 185 | + success: true, |
| 186 | + duration: Date.now() - context.startTime, |
| 187 | + data: result, |
| 188 | + }); |
| 189 | + |
| 190 | + return result; |
| 191 | + } catch (error) { |
| 192 | + // Run afterOperation hooks with failure |
| 193 | + await this.runAfterHooks(context, { |
| 194 | + success: false, |
| 195 | + error: error instanceof Error ? error : new Error(String(error)), |
| 196 | + duration: Date.now() - context.startTime, |
| 197 | + }); |
| 198 | + |
| 199 | + if (error instanceof Error) { |
| 200 | + this.error(t('commands.code.download.failed', 'Download failed: {{message}}', {message: error.message})); |
| 201 | + } |
| 202 | + throw error; |
| 203 | + } finally { |
| 204 | + cleanupSafetyRule(); |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments