66import { Args , Flags , ux } from '@oclif/core' ;
77import cliui from 'cliui' ;
88import { MrtCommand } from '@salesforce/b2c-tooling-sdk/cli' ;
9- import { createEnv , type MrtEnvironment } from '@salesforce/b2c-tooling-sdk/operations/mrt' ;
9+ import { createEnv , waitForEnv , type MrtEnvironment } from '@salesforce/b2c-tooling-sdk/operations/mrt' ;
1010import { t } from '../../../i18n/index.js' ;
1111
1212/**
@@ -140,18 +140,19 @@ export default class MrtEnvCreate extends MrtCommand<typeof MrtEnvCreate> {
140140 static enableJsonFlag = true ;
141141
142142 static examples = [
143+ '<%= config.bin %> <%= command.id %> staging --project my-storefront' ,
143144 '<%= config.bin %> <%= command.id %> staging --project my-storefront --name "Staging Environment"' ,
144- '<%= config.bin %> <%= command.id %> production --project my-storefront --name "Production" --production' ,
145- '<%= config.bin %> <%= command.id %> feature-test -p my-storefront -n "Feature Test" --region eu-west-1' ,
146- '<%= config.bin %> <%= command.id %> staging -p my-storefront -n "Staging" --proxy api=api.example.com --proxy ocapi=ocapi.example.com' ,
145+ '<%= config.bin %> <%= command.id %> production --project my-storefront --production' ,
146+ '<%= config.bin %> <%= command.id %> feature-test -p my-storefront --region eu-west-1' ,
147+ '<%= config.bin %> <%= command.id %> staging -p my-storefront --proxy api=api.example.com --proxy ocapi=ocapi.example.com' ,
148+ '<%= config.bin %> <%= command.id %> staging -p my-storefront --wait' ,
147149 ] ;
148150
149151 static flags = {
150152 ...MrtCommand . baseFlags ,
151153 name : Flags . string ( {
152154 char : 'n' ,
153- description : 'Display name for the environment' ,
154- required : true ,
155+ description : 'Display name for the environment (defaults to slug)' ,
155156 } ) ,
156157 region : Flags . string ( {
157158 char : 'r' ,
@@ -185,6 +186,11 @@ export default class MrtEnvCreate extends MrtCommand<typeof MrtEnvCreate> {
185186 description : 'Proxy configuration in format path=host (can be specified multiple times)' ,
186187 multiple : true ,
187188 } ) ,
189+ wait : Flags . boolean ( {
190+ char : 'w' ,
191+ description : 'Wait for the environment to be ready before returning' ,
192+ default : false ,
193+ } ) ,
188194 } ;
189195
190196 async run ( ) : Promise < MrtEnvironment > {
@@ -200,7 +206,7 @@ export default class MrtEnvCreate extends MrtCommand<typeof MrtEnvCreate> {
200206 }
201207
202208 const {
203- name,
209+ name : nameFlag ,
204210 region,
205211 production : isProduction ,
206212 hostname,
@@ -209,8 +215,12 @@ export default class MrtEnvCreate extends MrtCommand<typeof MrtEnvCreate> {
209215 'allow-cookies' : allowCookies ,
210216 'enable-source-maps' : enableSourceMaps ,
211217 proxy : proxyStrings ,
218+ wait,
212219 } = this . flags ;
213220
221+ // Default name to slug if not provided
222+ const name = nameFlag ?? slug ;
223+
214224 // Parse proxy configurations
215225 const proxyConfigs = proxyStrings ?. map ( ( p ) => parseProxyString ( p ) ) ;
216226
@@ -219,7 +229,7 @@ export default class MrtEnvCreate extends MrtCommand<typeof MrtEnvCreate> {
219229 ) ;
220230
221231 try {
222- const result = await createEnv (
232+ let result = await createEnv (
223233 {
224234 projectSlug : project ,
225235 slug,
@@ -237,6 +247,32 @@ export default class MrtEnvCreate extends MrtCommand<typeof MrtEnvCreate> {
237247 this . getMrtAuth ( ) ,
238248 ) ;
239249
250+ // Wait for environment to be ready if requested
251+ if ( wait ) {
252+ this . log ( t ( 'commands.mrt.env.create.waiting' , 'Waiting for environment "{{slug}}" to be ready...' , { slug} ) ) ;
253+
254+ const waitStartTime = Date . now ( ) ;
255+ result = await waitForEnv (
256+ {
257+ projectSlug : project ,
258+ slug,
259+ origin : this . resolvedConfig . mrtOrigin ,
260+ onPoll : ( env ) => {
261+ if ( ! this . jsonEnabled ( ) ) {
262+ const elapsed = Math . round ( ( Date . now ( ) - waitStartTime ) / 1000 ) ;
263+ this . log (
264+ t ( 'commands.mrt.env.create.state' , '[{{elapsed}}s] State: {{state}}' , {
265+ elapsed : String ( elapsed ) ,
266+ state : env . state ?? 'unknown' ,
267+ } ) ,
268+ ) ;
269+ }
270+ } ,
271+ } ,
272+ this . getMrtAuth ( ) ,
273+ ) ;
274+ }
275+
240276 if ( this . jsonEnabled ( ) ) {
241277 return result ;
242278 }
0 commit comments