|
| 1 | +import {Args, Flags, ux} from '@oclif/core'; |
| 2 | +import cliui from 'cliui'; |
| 3 | +import {MrtCommand} from '@salesforce/b2c-tooling/cli'; |
| 4 | +import {createEnv, type MrtEnvironment} from '@salesforce/b2c-tooling/operations/mrt'; |
| 5 | +import {t} from '../../../i18n/index.js'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Print environment details in a formatted table. |
| 9 | + */ |
| 10 | +function printEnvDetails(env: MrtEnvironment, project: string): void { |
| 11 | + const ui = cliui({width: process.stdout.columns || 80}); |
| 12 | + const labelWidth = 18; |
| 13 | + |
| 14 | + ui.div(''); |
| 15 | + ui.div({text: 'Slug:', width: labelWidth}, {text: env.slug ?? ''}); |
| 16 | + ui.div({text: 'Name:', width: labelWidth}, {text: env.name}); |
| 17 | + ui.div({text: 'Project:', width: labelWidth}, {text: project}); |
| 18 | + ui.div({text: 'State:', width: labelWidth}, {text: env.state ?? 'unknown'}); |
| 19 | + ui.div({text: 'Production:', width: labelWidth}, {text: env.is_production ? 'Yes' : 'No'}); |
| 20 | + |
| 21 | + if (env.ssr_region) { |
| 22 | + ui.div({text: 'Region:', width: labelWidth}, {text: env.ssr_region}); |
| 23 | + } |
| 24 | + |
| 25 | + if (env.hostname) { |
| 26 | + ui.div({text: 'Hostname:', width: labelWidth}, {text: env.hostname}); |
| 27 | + } |
| 28 | + |
| 29 | + if (env.ssr_external_hostname) { |
| 30 | + ui.div({text: 'External Host:', width: labelWidth}, {text: env.ssr_external_hostname}); |
| 31 | + } |
| 32 | + |
| 33 | + if (env.ssr_external_domain) { |
| 34 | + ui.div({text: 'External Domain:', width: labelWidth}, {text: env.ssr_external_domain}); |
| 35 | + } |
| 36 | + |
| 37 | + if (env.allow_cookies) { |
| 38 | + ui.div({text: 'Allow Cookies:', width: labelWidth}, {text: 'Yes'}); |
| 39 | + } |
| 40 | + |
| 41 | + if (env.enable_source_maps) { |
| 42 | + ui.div({text: 'Source Maps:', width: labelWidth}, {text: 'Yes'}); |
| 43 | + } |
| 44 | + |
| 45 | + if (env.log_level) { |
| 46 | + ui.div({text: 'Log Level:', width: labelWidth}, {text: env.log_level}); |
| 47 | + } |
| 48 | + |
| 49 | + ux.stdout(ui.toString()); |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Valid AWS regions for MRT environments. |
| 54 | + */ |
| 55 | +const SSR_REGIONS = [ |
| 56 | + 'us-east-1', |
| 57 | + 'us-east-2', |
| 58 | + 'us-west-1', |
| 59 | + 'us-west-2', |
| 60 | + 'ap-south-1', |
| 61 | + 'ap-south-2', |
| 62 | + 'ap-northeast-2', |
| 63 | + 'ap-southeast-1', |
| 64 | + 'ap-southeast-2', |
| 65 | + 'ap-southeast-3', |
| 66 | + 'ap-northeast-1', |
| 67 | + 'ap-northeast-3', |
| 68 | + 'ca-central-1', |
| 69 | + 'eu-central-1', |
| 70 | + 'eu-central-2', |
| 71 | + 'eu-west-1', |
| 72 | + 'eu-west-2', |
| 73 | + 'eu-west-3', |
| 74 | + 'eu-north-1', |
| 75 | + 'eu-south-1', |
| 76 | + 'il-central-1', |
| 77 | + 'me-central-1', |
| 78 | + 'sa-east-1', |
| 79 | +] as const; |
| 80 | + |
| 81 | +type SsrRegion = (typeof SSR_REGIONS)[number]; |
| 82 | + |
| 83 | +/** |
| 84 | + * Create a new environment (target) in a Managed Runtime project. |
| 85 | + */ |
| 86 | +export default class MrtEnvCreate extends MrtCommand<typeof MrtEnvCreate> { |
| 87 | + static args = { |
| 88 | + slug: Args.string({ |
| 89 | + description: 'Environment slug/identifier (e.g., staging, production)', |
| 90 | + required: true, |
| 91 | + }), |
| 92 | + }; |
| 93 | + |
| 94 | + static description = t('commands.mrt.env.create.description', 'Create a new Managed Runtime environment'); |
| 95 | + |
| 96 | + static enableJsonFlag = true; |
| 97 | + |
| 98 | + static examples = [ |
| 99 | + '<%= config.bin %> <%= command.id %> staging --project my-storefront --name "Staging Environment"', |
| 100 | + '<%= config.bin %> <%= command.id %> production --project my-storefront --name "Production" --production', |
| 101 | + '<%= config.bin %> <%= command.id %> feature-test -p my-storefront -n "Feature Test" --region eu-west-1', |
| 102 | + ]; |
| 103 | + |
| 104 | + static flags = { |
| 105 | + ...MrtCommand.baseFlags, |
| 106 | + name: Flags.string({ |
| 107 | + char: 'n', |
| 108 | + description: 'Display name for the environment', |
| 109 | + required: true, |
| 110 | + }), |
| 111 | + region: Flags.string({ |
| 112 | + char: 'r', |
| 113 | + description: 'AWS region for SSR deployment', |
| 114 | + options: SSR_REGIONS as unknown as string[], |
| 115 | + }), |
| 116 | + production: Flags.boolean({ |
| 117 | + description: 'Mark as a production environment', |
| 118 | + default: false, |
| 119 | + }), |
| 120 | + hostname: Flags.string({ |
| 121 | + description: 'Hostname pattern for V8 Tag loading', |
| 122 | + }), |
| 123 | + 'external-hostname': Flags.string({ |
| 124 | + description: 'Full external hostname (e.g., www.example.com)', |
| 125 | + }), |
| 126 | + 'external-domain': Flags.string({ |
| 127 | + description: 'External domain for Universal PWA SSR (e.g., example.com)', |
| 128 | + }), |
| 129 | + 'allow-cookies': Flags.boolean({ |
| 130 | + description: 'Forward HTTP cookies to origin', |
| 131 | + default: false, |
| 132 | + allowNo: true, |
| 133 | + }), |
| 134 | + 'enable-source-maps': Flags.boolean({ |
| 135 | + description: 'Enable source map support in the environment', |
| 136 | + default: false, |
| 137 | + allowNo: true, |
| 138 | + }), |
| 139 | + }; |
| 140 | + |
| 141 | + async run(): Promise<MrtEnvironment> { |
| 142 | + this.requireMrtCredentials(); |
| 143 | + |
| 144 | + const {slug} = this.args; |
| 145 | + const {mrtProject: project} = this.resolvedConfig; |
| 146 | + |
| 147 | + if (!project) { |
| 148 | + this.error( |
| 149 | + 'MRT project is required. Provide --project flag, set SFCC_MRT_PROJECT, or set mrtProject in dw.json.', |
| 150 | + ); |
| 151 | + } |
| 152 | + |
| 153 | + const { |
| 154 | + name, |
| 155 | + region, |
| 156 | + production: isProduction, |
| 157 | + hostname, |
| 158 | + 'external-hostname': externalHostname, |
| 159 | + 'external-domain': externalDomain, |
| 160 | + 'allow-cookies': allowCookies, |
| 161 | + 'enable-source-maps': enableSourceMaps, |
| 162 | + } = this.flags; |
| 163 | + |
| 164 | + this.log( |
| 165 | + t('commands.mrt.env.create.creating', 'Creating environment "{{slug}}" in {{project}}...', {slug, project}), |
| 166 | + ); |
| 167 | + |
| 168 | + try { |
| 169 | + const result = await createEnv( |
| 170 | + { |
| 171 | + projectSlug: project, |
| 172 | + slug, |
| 173 | + name, |
| 174 | + region: region as SsrRegion | undefined, |
| 175 | + isProduction, |
| 176 | + hostname, |
| 177 | + externalHostname, |
| 178 | + externalDomain, |
| 179 | + allowCookies: allowCookies || undefined, |
| 180 | + enableSourceMaps: enableSourceMaps || undefined, |
| 181 | + }, |
| 182 | + this.getMrtAuth(), |
| 183 | + ); |
| 184 | + |
| 185 | + if (this.jsonEnabled()) { |
| 186 | + return result; |
| 187 | + } |
| 188 | + |
| 189 | + // Human-readable output |
| 190 | + this.log(t('commands.mrt.env.create.success', 'Environment created successfully.')); |
| 191 | + |
| 192 | + printEnvDetails(result, project); |
| 193 | + |
| 194 | + return result; |
| 195 | + } catch (error) { |
| 196 | + if (error instanceof Error) { |
| 197 | + this.error( |
| 198 | + t('commands.mrt.env.create.failed', 'Failed to create environment: {{message}}', {message: error.message}), |
| 199 | + ); |
| 200 | + } |
| 201 | + throw error; |
| 202 | + } |
| 203 | + } |
| 204 | +} |
0 commit comments