@@ -2,7 +2,8 @@ import { ConfigIO, SecureCredentials } from '../../../lib';
22import type { AgentCoreMcpSpec , DeployedState } from '../../../schema' ;
33import { applyTargetRegionToEnv } from '../../aws' ;
44import { validateAwsCredentials } from '../../aws/account' ;
5- import { createSwitchableIoHost } from '../../cdk/toolkit-lib' ;
5+ import { CdkToolkitWrapper , createSwitchableIoHost } from '../../cdk/toolkit-lib' ;
6+ import type { SwitchableIoHost } from '../../cdk/toolkit-lib' ;
67import {
78 buildDeployedState ,
89 getStackOutputs ,
@@ -41,6 +42,7 @@ import {
4142import { setupHttpGateways } from '../../operations/deploy/post-deploy-http-gateways' ;
4243import { enableOnlineEvalConfigs } from '../../operations/deploy/post-deploy-online-evals' ;
4344import type { DeployResult } from './types' ;
45+ import { StackSelectionStrategy } from '@aws-cdk/toolkit-lib' ;
4446
4547export interface ValidatedDeployOptions {
4648 target : string ;
@@ -55,6 +57,30 @@ export interface ValidatedDeployOptions {
5557const AGENT_NEXT_STEPS = [ 'agentcore invoke' , 'agentcore status' ] ;
5658const MEMORY_ONLY_NEXT_STEPS = [ 'agentcore add agent' , 'agentcore status' ] ;
5759
60+ export async function runDiff (
61+ toolkitWrapper : CdkToolkitWrapper ,
62+ stackName : string ,
63+ switchableIoHost ?: SwitchableIoHost
64+ ) : Promise < void > {
65+ const diffIoHost = switchableIoHost ?? createSwitchableIoHost ( ) ;
66+ let hasDiffContent = false ;
67+ diffIoHost . setOnRawMessage ( ( code , _level , message ) => {
68+ if ( ! message ) return ;
69+ // I4002: formatted diff per stack, I4001: overall diff summary
70+ if ( code === 'CDK_TOOLKIT_I4002' || code === 'CDK_TOOLKIT_I4001' ) {
71+ hasDiffContent = true ;
72+ console . log ( message ) ;
73+ }
74+ } ) ;
75+ diffIoHost . setVerbose ( true ) ;
76+ await toolkitWrapper . diff ( { stacks : { strategy : StackSelectionStrategy . PATTERN_MUST_MATCH , patterns : [ stackName ] } } ) ;
77+ if ( ! hasDiffContent ) {
78+ console . log ( 'No stack differences detected.' ) ;
79+ }
80+ diffIoHost . setVerbose ( false ) ;
81+ diffIoHost . setOnRawMessage ( null ) ;
82+ }
83+
5884export async function handleDeploy ( options : ValidatedDeployOptions ) : Promise < DeployResult > {
5985 let toolkitWrapper = null ;
6086 let restoreEnv : ( ( ) => void ) | null = null ;
@@ -296,23 +322,7 @@ export async function handleDeploy(options: ValidatedDeployOptions): Promise<Dep
296322 // Diff mode: run cdk diff and exit without deploying
297323 if ( options . diff ) {
298324 startStep ( 'Run CDK diff' ) ;
299- const diffIoHost = switchableIoHost ?? createSwitchableIoHost ( ) ;
300- let hasDiffContent = false ;
301- diffIoHost . setOnRawMessage ( ( code , _level , message ) => {
302- if ( ! message ) return ;
303- // I4002: formatted diff per stack, I4001: overall diff summary
304- if ( code === 'CDK_TOOLKIT_I4002' || code === 'CDK_TOOLKIT_I4001' ) {
305- hasDiffContent = true ;
306- console . log ( message ) ;
307- }
308- } ) ;
309- diffIoHost . setVerbose ( true ) ;
310- await toolkitWrapper . diff ( ) ;
311- if ( ! hasDiffContent ) {
312- console . log ( 'No stack differences detected.' ) ;
313- }
314- diffIoHost . setVerbose ( false ) ;
315- diffIoHost . setOnRawMessage ( null ) ;
325+ await runDiff ( toolkitWrapper , stackName , switchableIoHost ) ;
316326 endStep ( 'success' ) ;
317327
318328 logger . finalize ( true ) ;
0 commit comments