|
| 1 | +import { Args, Command, Flags } from '@oclif/core'; |
| 2 | +import { B2CInstance, uploadCartridges } from '@salesforce/b2c-tooling'; |
| 3 | +import { loadConfig } from '../../config/loader.js'; |
| 4 | +import { AuthResolver } from '../../config/auth-resolver.js'; |
| 5 | + |
| 6 | +export default class Deploy extends Command { |
| 7 | + static args = { |
| 8 | + cartridgePath: Args.string({ |
| 9 | + description: 'Path to cartridges directory', |
| 10 | + default: './cartridges', |
| 11 | + }), |
| 12 | + }; |
| 13 | + |
| 14 | + static description = 'Deploy cartridges to a B2C Commerce instance'; |
| 15 | + |
| 16 | + static examples = [ |
| 17 | + '<%= config.bin %> <%= command.id %>', |
| 18 | + '<%= config.bin %> <%= command.id %> ./my-cartridges', |
| 19 | + '<%= config.bin %> <%= command.id %> --hostname my-sandbox.demandware.net --code-version v1', |
| 20 | + ]; |
| 21 | + |
| 22 | + static flags = { |
| 23 | + hostname: Flags.string({ |
| 24 | + char: 'h', |
| 25 | + description: 'Instance hostname', |
| 26 | + }), |
| 27 | + 'code-version': Flags.string({ |
| 28 | + char: 'v', |
| 29 | + description: 'Code version to deploy to', |
| 30 | + }), |
| 31 | + username: Flags.string({ |
| 32 | + char: 'u', |
| 33 | + description: 'Username for Basic Auth', |
| 34 | + }), |
| 35 | + password: Flags.string({ |
| 36 | + char: 'p', |
| 37 | + description: 'Password for Basic Auth', |
| 38 | + }), |
| 39 | + 'client-id': Flags.string({ |
| 40 | + description: 'Client ID for OAuth', |
| 41 | + }), |
| 42 | + 'client-secret': Flags.string({ |
| 43 | + description: 'Client Secret for OAuth', |
| 44 | + }), |
| 45 | + }; |
| 46 | + |
| 47 | + async run(): Promise<void> { |
| 48 | + const { args, flags } = await this.parse(Deploy); |
| 49 | + |
| 50 | + // 1. Load Config with precedence (CLI > Env > dw.json) |
| 51 | + const config = await loadConfig({ |
| 52 | + hostname: flags.hostname, |
| 53 | + codeVersion: flags['code-version'], |
| 54 | + username: flags.username, |
| 55 | + password: flags.password, |
| 56 | + clientId: flags['client-id'], |
| 57 | + clientSecret: flags['client-secret'], |
| 58 | + }); |
| 59 | + |
| 60 | + // Validate required config |
| 61 | + if (!config.hostname) { |
| 62 | + this.error('Hostname is required. Set via --hostname, DW_HOSTNAME env var, or dw.json'); |
| 63 | + } |
| 64 | + |
| 65 | + if (!config.codeVersion) { |
| 66 | + this.error( |
| 67 | + 'Code version is required. Set via --code-version, DW_CODE_VERSION env var, or dw.json' |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + // 2. Create Auth Resolver |
| 72 | + const resolver = new AuthResolver(config); |
| 73 | + |
| 74 | + if (!resolver.hasWebDavCredentials()) { |
| 75 | + this.error( |
| 76 | + 'No valid credentials found. Provide username/password or clientId/clientSecret.' |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + // 3. Create Instance with WebDAV Auth Strategy |
| 81 | + const instance = new B2CInstance( |
| 82 | + { |
| 83 | + hostname: config.hostname, |
| 84 | + codeVersion: config.codeVersion, |
| 85 | + }, |
| 86 | + resolver.getForWebDav() |
| 87 | + ); |
| 88 | + |
| 89 | + // 4. Execute the operation |
| 90 | + this.log(`Deploying cartridges from ${args.cartridgePath}...`); |
| 91 | + this.log(`Target: ${config.hostname}`); |
| 92 | + this.log(`Code Version: ${config.codeVersion}`); |
| 93 | + |
| 94 | + try { |
| 95 | + await uploadCartridges(instance, args.cartridgePath); |
| 96 | + this.log('✓ Deployment complete'); |
| 97 | + } catch (error) { |
| 98 | + if (error instanceof Error) { |
| 99 | + this.error(`Deployment failed: ${error.message}`); |
| 100 | + } |
| 101 | + throw error; |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments