@@ -8,9 +8,11 @@ import {MrtCommand} from '@salesforce/b2c-tooling-sdk/cli';
88import {
99 pushBundle ,
1010 createDeployment ,
11+ waitForEnv ,
1112 DEFAULT_SSR_PARAMETERS ,
1213 type PushResult ,
1314 type CreateDeploymentResult ,
15+ type MrtEnvironment ,
1416} from '@salesforce/b2c-tooling-sdk/operations/mrt' ;
1517import { t , withDocs } from '../../../i18n/index.js' ;
1618
@@ -53,7 +55,7 @@ function parseSsrParams(params: string[]): Record<string, string> {
5355 return result ;
5456}
5557
56- type DeployResult = CreateDeploymentResult | PushResult ;
58+ type DeployResult = CreateDeploymentResult | MrtEnvironment | PushResult ;
5759
5860/**
5961 * Deploy a bundle to Managed Runtime.
@@ -86,6 +88,7 @@ export default class MrtBundleDeploy extends MrtCommand<typeof MrtBundleDeploy>
8688 '<%= config.bin %> <%= command.id %> --project my-storefront --node-version 20.x' ,
8789 '<%= config.bin %> <%= command.id %> --project my-storefront --ssr-param SSRProxyPath=/api' ,
8890 '<%= config.bin %> <%= command.id %> 12345 --project my-storefront --environment staging' ,
91+ '<%= config.bin %> <%= command.id %> 12345 --project my-storefront --environment staging --wait' ,
8992 ] ;
9093
9194 static flags = {
@@ -116,6 +119,27 @@ export default class MrtBundleDeploy extends MrtCommand<typeof MrtBundleDeploy>
116119 multiple : true ,
117120 default : [ ] ,
118121 } ) ,
122+ wait : Flags . boolean ( {
123+ char : 'w' ,
124+ description : 'Wait for the deployment to complete before returning' ,
125+ default : false ,
126+ } ) ,
127+ 'poll-interval' : Flags . integer ( {
128+ description : 'Polling interval in seconds when using --wait' ,
129+ default : 30 ,
130+ dependsOn : [ 'wait' ] ,
131+ } ) ,
132+ timeout : Flags . integer ( {
133+ description : 'Maximum time to wait in seconds when using --wait (0 for no timeout)' ,
134+ default : 600 ,
135+ dependsOn : [ 'wait' ] ,
136+ } ) ,
137+ } ;
138+
139+ protected operations = {
140+ pushBundle,
141+ createDeployment,
142+ waitForEnv,
119143 } ;
120144
121145 async run ( ) : Promise < DeployResult > {
@@ -132,7 +156,7 @@ export default class MrtBundleDeploy extends MrtCommand<typeof MrtBundleDeploy>
132156 /**
133157 * Deploy an existing bundle to an environment.
134158 */
135- private async deployExistingBundle ( bundleId : number ) : Promise < CreateDeploymentResult > {
159+ private async deployExistingBundle ( bundleId : number ) : Promise < CreateDeploymentResult | MrtEnvironment > {
136160 const { mrtProject : project , mrtEnvironment : environment } = this . resolvedConfig . values ;
137161
138162 if ( ! project ) {
@@ -153,7 +177,7 @@ export default class MrtBundleDeploy extends MrtCommand<typeof MrtBundleDeploy>
153177 ) ;
154178
155179 try {
156- const result = await createDeployment (
180+ const result = await this . operations . createDeployment (
157181 {
158182 projectSlug : project ,
159183 targetSlug : environment ,
@@ -174,12 +198,18 @@ export default class MrtBundleDeploy extends MrtCommand<typeof MrtBundleDeploy>
174198 } ,
175199 ) ,
176200 ) ;
177- this . log (
178- t (
179- 'commands.mrt.bundle.deploy.note' ,
180- 'Note: Deployments are asynchronous. Use "b2c mrt env get" or the Runtime Admin dashboard to check status.' ,
181- ) ,
182- ) ;
201+ if ( ! this . flags . wait ) {
202+ this . log (
203+ t (
204+ 'commands.mrt.bundle.deploy.note' ,
205+ 'Note: Deployments are asynchronous. Use "b2c mrt env get" or the Runtime Admin dashboard to check status.' ,
206+ ) ,
207+ ) ;
208+ }
209+ }
210+
211+ if ( this . flags . wait ) {
212+ return this . waitForDeployment ( project , environment ) ;
183213 }
184214
185215 return result ;
@@ -198,7 +228,7 @@ export default class MrtBundleDeploy extends MrtCommand<typeof MrtBundleDeploy>
198228 /**
199229 * Push a local build to create a new bundle.
200230 */
201- private async pushLocalBuild ( ) : Promise < PushResult > {
231+ private async pushLocalBuild ( ) : Promise < MrtEnvironment | PushResult > {
202232 const { mrtProject : project , mrtEnvironment : target } = this . resolvedConfig . values ;
203233 const { message} = this . flags ;
204234
@@ -227,7 +257,7 @@ export default class MrtBundleDeploy extends MrtCommand<typeof MrtBundleDeploy>
227257 }
228258
229259 try {
230- const result = await pushBundle (
260+ const result = await this . operations . pushBundle (
231261 {
232262 projectSlug : project ,
233263 target,
@@ -256,6 +286,14 @@ export default class MrtBundleDeploy extends MrtCommand<typeof MrtBundleDeploy>
256286 ) ,
257287 ) ;
258288
289+ if ( this . flags . wait ) {
290+ if ( ! target ) {
291+ this . warn ( '--wait was specified but no environment target was provided. Skipping wait.' ) ;
292+ return result ;
293+ }
294+ return this . waitForDeployment ( project , target ) ;
295+ }
296+
259297 return result ;
260298 } catch ( error ) {
261299 if ( error instanceof Error ) {
@@ -264,4 +302,46 @@ export default class MrtBundleDeploy extends MrtCommand<typeof MrtBundleDeploy>
264302 throw error ;
265303 }
266304 }
305+
306+ /**
307+ * Wait for a deployment to complete by polling the environment state.
308+ */
309+ private async waitForDeployment ( project : string , environment : string ) : Promise < MrtEnvironment > {
310+ this . log (
311+ t ( 'commands.mrt.bundle.deploy.waiting' , 'Waiting for deployment to complete on {{environment}}...' , {
312+ environment,
313+ } ) ,
314+ ) ;
315+
316+ const envResult = await this . operations . waitForEnv (
317+ {
318+ projectSlug : project ,
319+ slug : environment ,
320+ origin : this . resolvedConfig . values . mrtOrigin ,
321+ pollIntervalSeconds : this . flags [ 'poll-interval' ] ,
322+ timeoutSeconds : this . flags . timeout ,
323+ onPoll : ( info ) => {
324+ if ( ! this . jsonEnabled ( ) ) {
325+ this . log (
326+ t ( 'commands.mrt.bundle.deploy.state' , '[{{elapsed}}s] State: {{state}}' , {
327+ elapsed : String ( info . elapsedSeconds ) ,
328+ state : info . state ,
329+ } ) ,
330+ ) ;
331+ }
332+ } ,
333+ } ,
334+ this . getMrtAuth ( ) ,
335+ ) ;
336+
337+ if ( ! this . jsonEnabled ( ) ) {
338+ this . log (
339+ t ( 'commands.mrt.bundle.deploy.deployComplete' , 'Deployment complete. Environment is {{state}}.' , {
340+ state : envResult . state ?? 'unknown' ,
341+ } ) ,
342+ ) ;
343+ }
344+
345+ return envResult ;
346+ }
267347}
0 commit comments