@@ -4,7 +4,7 @@ import * as chalk from 'chalk';
4
4
import * as chokidar from 'chokidar' ;
5
5
import * as fs from 'fs-extra' ;
6
6
import { ToolkitServices } from './private' ;
7
- import { AssetBuildTime , DeployOptions , RequireApproval } from '../actions/deploy' ;
7
+ import { AssetBuildTime , DeployOptions , ExtendedDeployOptions , RequireApproval } from '../actions/deploy' ;
8
8
import { buildParameterMap , removePublishedAssets } from '../actions/deploy/private' ;
9
9
import { DestroyOptions } from '../actions/destroy' ;
10
10
import { DiffOptions } from '../actions/diff' ;
@@ -200,9 +200,16 @@ export class Toolkit extends CloudAssemblySourceBuilder implements AsyncDisposab
200
200
* Deploys the selected stacks into an AWS account
201
201
*/
202
202
public async deploy ( cx : ICloudAssemblySource , options : DeployOptions = { } ) : Promise < void > {
203
- const ioHost = withAction ( this . ioHost , 'deploy' ) ;
204
- const timer = Timer . start ( ) ;
205
203
const assembly = await this . assemblyFromSource ( cx ) ;
204
+ return this . _deploy ( assembly , 'deploy' , options ) ;
205
+ }
206
+
207
+ /**
208
+ * Helper to allow deploy being called as part of the watch action.
209
+ */
210
+ private async _deploy ( assembly : StackAssembly , action : 'deploy' | 'watch' , options : ExtendedDeployOptions = { } ) {
211
+ const ioHost = withAction ( this . ioHost , action ) ;
212
+ const timer = Timer . start ( ) ;
206
213
const stackCollection = assembly . selectStacksV2 ( options . stacks ?? ALL_STACKS ) ;
207
214
await this . validateStacksMetadata ( stackCollection , ioHost ) ;
208
215
@@ -361,8 +368,8 @@ export class Toolkit extends CloudAssemblySourceBuilder implements AsyncDisposab
361
368
ci : options . ci ,
362
369
rollback,
363
370
hotswap : options . hotswap ,
371
+ extraUserAgent : options . extraUserAgent ,
364
372
// hotswapPropertyOverrides: hotswapPropertyOverrides,
365
-
366
373
assetParallelism : options . assetParallelism ,
367
374
} ) ;
368
375
@@ -386,7 +393,7 @@ export class Toolkit extends CloudAssemblySourceBuilder implements AsyncDisposab
386
393
}
387
394
388
395
// Perform a rollback
389
- await this . rollback ( cx , {
396
+ await this . _rollback ( assembly , action , {
390
397
stacks : { patterns : [ stack . hierarchicalId ] , strategy : StackSelectionStrategy . PATTERN_MUST_MATCH_SINGLE } ,
391
398
orphanFailedResources : options . force ,
392
399
} ) ;
@@ -511,6 +518,7 @@ export class Toolkit extends CloudAssemblySourceBuilder implements AsyncDisposab
511
518
* Implies hotswap deployments.
512
519
*/
513
520
public async watch ( cx : ICloudAssemblySource , options : WatchOptions ) : Promise < void > {
521
+ const assembly = await this . assemblyFromSource ( cx , false ) ;
514
522
const ioHost = withAction ( this . ioHost , 'watch' ) ;
515
523
const rootDir = options . watchDir ?? process . cwd ( ) ;
516
524
await ioHost . notify ( debug ( `root directory used for 'watch' is: ${ rootDir } ` ) ) ;
@@ -531,19 +539,20 @@ export class Toolkit extends CloudAssemblySourceBuilder implements AsyncDisposab
531
539
rootDir,
532
540
returnRootDirIfEmpty : true ,
533
541
} ) ;
534
- await ioHost . notify ( debug ( `'include' patterns for 'watch': ${ watchIncludes } ` ) ) ;
542
+ await ioHost . notify ( debug ( `'include' patterns for 'watch': ${ JSON . stringify ( watchIncludes ) } ` ) ) ;
535
543
536
544
// For the "exclude" subkey under the "watch" key,
537
545
// the behavior is to add some default excludes in addition to the ones specified by the user:
538
546
// 1. The CDK output directory.
539
547
// 2. Any file whose name starts with a dot.
540
548
// 3. Any directory's content whose name starts with a dot.
541
549
// 4. Any node_modules and its content (even if it's not a JS/TS project, you might be using a local aws-cli package)
550
+ const outdir = options . outdir ?? 'cdk.out' ;
542
551
const watchExcludes = patternsArrayForWatch ( options . exclude , {
543
552
rootDir,
544
553
returnRootDirIfEmpty : false ,
545
- } ) . concat ( `${ options . outdir } /**` , '**/.*' , '**/.*/**' , '**/node_modules/**' ) ;
546
- await ioHost . notify ( debug ( `'exclude' patterns for 'watch': ${ watchExcludes } ` ) ) ;
554
+ } ) . concat ( `${ outdir } /**` , '**/.*' , '**/.*/**' , '**/node_modules/**' ) ;
555
+ await ioHost . notify ( debug ( `'exclude' patterns for 'watch': ${ JSON . stringify ( watchExcludes ) } ` ) ) ;
547
556
548
557
// Since 'cdk deploy' is a relatively slow operation for a 'watch' process,
549
558
// introduce a concurrency latch that tracks the state.
@@ -564,7 +573,7 @@ export class Toolkit extends CloudAssemblySourceBuilder implements AsyncDisposab
564
573
latch = 'deploying' ;
565
574
// cloudWatchLogMonitor?.deactivate();
566
575
567
- await this . invokeDeployFromWatch ( cx , options ) ;
576
+ await this . invokeDeployFromWatch ( assembly , options ) ;
568
577
569
578
// If latch is still 'deploying' after the 'await', that's fine,
570
579
// but if it's 'queued', that means we need to deploy again
@@ -573,7 +582,7 @@ export class Toolkit extends CloudAssemblySourceBuilder implements AsyncDisposab
573
582
// and thinks the above 'while' condition is always 'false' without the cast
574
583
latch = 'deploying' ;
575
584
await ioHost . notify ( info ( "Detected file changes during deployment. Invoking 'cdk deploy' again" ) ) ;
576
- await this . invokeDeployFromWatch ( cx , options ) ;
585
+ await this . invokeDeployFromWatch ( assembly , options ) ;
577
586
}
578
587
latch = 'open' ;
579
588
// cloudWatchLogMonitor?.activate();
@@ -583,7 +592,6 @@ export class Toolkit extends CloudAssemblySourceBuilder implements AsyncDisposab
583
592
. watch ( watchIncludes , {
584
593
ignored : watchExcludes ,
585
594
cwd : rootDir ,
586
- // ignoreInitial: true,
587
595
} )
588
596
. on ( 'ready' , async ( ) => {
589
597
latch = 'open' ;
@@ -613,9 +621,16 @@ export class Toolkit extends CloudAssemblySourceBuilder implements AsyncDisposab
613
621
* Rolls back the selected stacks.
614
622
*/
615
623
public async rollback ( cx : ICloudAssemblySource , options : RollbackOptions ) : Promise < void > {
616
- const ioHost = withAction ( this . ioHost , 'rollback' ) ;
617
- const timer = Timer . start ( ) ;
618
624
const assembly = await this . assemblyFromSource ( cx ) ;
625
+ return this . _rollback ( assembly , 'rollback' , options ) ;
626
+ }
627
+
628
+ /**
629
+ * Helper to allow rollback being called as part of the deploy or watch action.
630
+ */
631
+ private async _rollback ( assembly : StackAssembly , action : 'rollback' | 'deploy' | 'watch' , options : RollbackOptions ) : Promise < void > {
632
+ const ioHost = withAction ( this . ioHost , action ) ;
633
+ const timer = Timer . start ( ) ;
619
634
const stacks = assembly . selectStacksV2 ( options . stacks ) ;
620
635
await this . validateStacksMetadata ( stacks , ioHost ) ;
621
636
const synthTime = timer . end ( ) ;
@@ -751,25 +766,24 @@ export class Toolkit extends CloudAssemblySourceBuilder implements AsyncDisposab
751
766
}
752
767
753
768
private async invokeDeployFromWatch (
754
- cx : ICloudAssemblySource ,
769
+ assembly : StackAssembly ,
755
770
options : WatchOptions ,
756
771
) : Promise < void > {
757
- const deployOptions : DeployOptions = {
772
+ const deployOptions : ExtendedDeployOptions = {
758
773
...options ,
759
774
requireApproval : RequireApproval . NEVER ,
760
- // if 'watch' is called by invoking 'cdk deploy --watch',
761
- // we need to make sure to not call 'deploy' with 'watch' again,
762
- // as that would lead to a cycle
763
- // watch: false,
764
775
// cloudWatchLogMonitor,
765
- // cacheCloudAssembly: false,
766
776
hotswap : options . hotswap ,
767
- // extraUserAgent: `cdk-watch/hotswap-${options.hotswap !== HotswapMode.FALL_BACK ? 'on' : 'off'}`,
777
+ extraUserAgent : `cdk-watch/hotswap-${ options . hotswap !== HotswapMode . FALL_BACK ? 'on' : 'off' } ` ,
768
778
concurrency : options . concurrency ,
769
779
} ;
770
780
771
781
try {
772
- await this . deploy ( cx , deployOptions ) ;
782
+ await this . _deploy (
783
+ assembly ,
784
+ 'watch' ,
785
+ deployOptions ,
786
+ ) ;
773
787
} catch {
774
788
// just continue - deploy will show the error
775
789
}
0 commit comments