11/* eslint-disable @typescript-eslint/no-shadow */ // yargs
22import * as cxapi from '@aws-cdk/cx-api' ;
3- import type { DeploymentMethod } from '@aws-cdk/toolkit-lib' ;
3+ import type { ChangeSetDeployment , DeploymentMethod , DirectDeployment } from '@aws-cdk/toolkit-lib' ;
44import { ToolkitError } from '@aws-cdk/toolkit-lib' ;
55import * as chalk from 'chalk' ;
66import { CdkToolkit , AssetBuildTime } from './cdk-toolkit' ;
@@ -322,43 +322,6 @@ export async function exec(args: string[], synthesizer?: Synthesizer): Promise<n
322322 throw new ToolkitError ( 'Can not supply both --[no-]execute and --method at the same time' ) ;
323323 }
324324
325- let deploymentMethod : DeploymentMethod | undefined ;
326- switch ( args . method ) {
327- case 'direct' :
328- if ( args . changeSetName ) {
329- throw new ToolkitError ( '--change-set-name cannot be used with method=direct' ) ;
330- }
331- if ( args . importExistingResources ) {
332- throw new ToolkitError ( '--import-existing-resources cannot be enabled with method=direct' ) ;
333- }
334- deploymentMethod = { method : 'direct' } ;
335- break ;
336- case 'change-set' :
337- deploymentMethod = {
338- method : 'change-set' ,
339- execute : true ,
340- changeSetName : args . changeSetName ,
341- importExistingResources : args . importExistingResources ,
342- } ;
343- break ;
344- case 'prepare-change-set' :
345- deploymentMethod = {
346- method : 'change-set' ,
347- execute : false ,
348- changeSetName : args . changeSetName ,
349- importExistingResources : args . importExistingResources ,
350- } ;
351- break ;
352- case undefined :
353- deploymentMethod = {
354- method : 'change-set' ,
355- execute : args . execute ?? true ,
356- changeSetName : args . changeSetName ,
357- importExistingResources : args . importExistingResources ,
358- } ;
359- break ;
360- }
361-
362325 return cli . deploy ( {
363326 selector,
364327 exclusively : args . exclusively ,
@@ -368,15 +331,14 @@ export async function exec(args: string[], synthesizer?: Synthesizer): Promise<n
368331 requireApproval : configuration . settings . get ( [ 'requireApproval' ] ) ,
369332 reuseAssets : args [ 'build-exclude' ] ,
370333 tags : configuration . settings . get ( [ 'tags' ] ) ,
371- deploymentMethod,
334+ deploymentMethod : determineDeploymentMethod ( args , configuration ) ,
372335 force : args . force ,
373336 parameters : parameterMap ,
374337 usePreviousParameters : args [ 'previous-parameters' ] ,
375338 outputsFile : configuration . settings . get ( [ 'outputsFile' ] ) ,
376339 progress : configuration . settings . get ( [ 'progress' ] ) ,
377340 ci : args . ci ,
378341 rollback : configuration . settings . get ( [ 'rollback' ] ) ,
379- hotswap : determineHotswapMode ( args . hotswap , args . hotswapFallback ) ,
380342 watch : args . watch ,
381343 traceLogs : args . logs ,
382344 concurrency : args . concurrency ,
@@ -424,14 +386,10 @@ export async function exec(args: string[], synthesizer?: Synthesizer): Promise<n
424386 toolkitStackName,
425387 roleArn : args . roleArn ,
426388 reuseAssets : args [ 'build-exclude' ] ,
427- deploymentMethod : {
428- method : 'change-set' ,
429- changeSetName : args . changeSetName ,
430- } ,
389+ deploymentMethod : determineDeploymentMethod ( args , configuration , true ) ,
431390 force : args . force ,
432391 progress : configuration . settings . get ( [ 'progress' ] ) ,
433392 rollback : configuration . settings . get ( [ 'rollback' ] ) ,
434- hotswap : determineHotswapMode ( args . hotswap , args . hotswapFallback , true ) ,
435393 traceLogs : args . logs ,
436394 concurrency : args . concurrency ,
437395 } ) ;
@@ -565,6 +523,65 @@ function arrayFromYargs(xs: string[]): string[] | undefined {
565523 return xs . filter ( ( x ) => x !== '' ) ;
566524}
567525
526+ function determineDeploymentMethod ( args : any , configuration : Configuration , watch ?: boolean ) : DeploymentMethod {
527+ let deploymentMethod : ChangeSetDeployment | DirectDeployment | undefined ;
528+ switch ( args . method ) {
529+ case 'direct' :
530+ if ( args . changeSetName ) {
531+ throw new ToolkitError ( '--change-set-name cannot be used with method=direct' ) ;
532+ }
533+ if ( args . importExistingResources ) {
534+ throw new ToolkitError ( '--import-existing-resources cannot be enabled with method=direct' ) ;
535+ }
536+ deploymentMethod = { method : 'direct' } ;
537+ break ;
538+ case 'change-set' :
539+ deploymentMethod = {
540+ method : 'change-set' ,
541+ execute : true ,
542+ changeSetName : args . changeSetName ,
543+ importExistingResources : args . importExistingResources ,
544+ } ;
545+ break ;
546+ case 'prepare-change-set' :
547+ deploymentMethod = {
548+ method : 'change-set' ,
549+ execute : false ,
550+ changeSetName : args . changeSetName ,
551+ importExistingResources : args . importExistingResources ,
552+ } ;
553+ break ;
554+ case undefined :
555+ default :
556+ deploymentMethod = {
557+ method : 'change-set' ,
558+ execute : watch ? true : args . execute ?? true ,
559+ changeSetName : args . changeSetName ,
560+ importExistingResources : args . importExistingResources ,
561+ } ;
562+ break ;
563+ }
564+
565+ const hotswapMode = determineHotswapMode ( args . hotswap , args . hotswapFallback , watch ) ;
566+ const hotswapProperties = configuration . settings . get ( [ 'hotswap' ] ) || { } ;
567+ switch ( hotswapMode ) {
568+ case HotswapMode . FALL_BACK :
569+ return {
570+ method : 'hotswap' ,
571+ properties : hotswapProperties ,
572+ fallback : deploymentMethod ,
573+ } ;
574+ case HotswapMode . HOTSWAP_ONLY :
575+ return {
576+ method : 'hotswap' ,
577+ properties : hotswapProperties ,
578+ } ;
579+ default :
580+ case HotswapMode . FULL_DEPLOYMENT :
581+ return deploymentMethod ;
582+ }
583+ }
584+
568585function determineHotswapMode ( hotswap ?: boolean , hotswapFallback ?: boolean , watch ?: boolean ) : HotswapMode {
569586 if ( hotswap && hotswapFallback ) {
570587 throw new ToolkitError ( 'Can not supply both --hotswap and --hotswap-fallback at the same time' ) ;
0 commit comments