Skip to content

Commit 9565b9b

Browse files
authored
feat(core): allow multiple transforms on ITemplateOptions (#3395)
This deprecates the `templateOptions.transform` field and introduces a new `templateOptions.transforms` field. When both are specified, the deprecated field's value is prepended to the new field value, preserving original behavior. Fixes #3366
1 parent cd0e9bd commit 9565b9b

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

packages/@aws-cdk/core/lib/stack.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,9 +545,19 @@ export class Stack extends Construct implements ITaggable {
545545
* @internal
546546
*/
547547
protected _toCloudFormation() {
548+
if (this.templateOptions.transform) {
549+
// tslint:disable-next-line: max-line-length
550+
this.node.addWarning('This stack is using the deprecated `templateOptions.transform` property. Consider switching to `templateOptions.transforms`.');
551+
if (!this.templateOptions.transforms) {
552+
this.templateOptions.transforms = [];
553+
}
554+
if (this.templateOptions.transforms.indexOf(this.templateOptions.transform) === -1) {
555+
this.templateOptions.transforms.unshift(this.templateOptions.transform);
556+
}
557+
}
548558
const template: any = {
549559
Description: this.templateOptions.description,
550-
Transform: this.templateOptions.transform,
560+
Transform: extractSingleValue(this.templateOptions.transforms),
551561
AWSTemplateFormatVersion: this.templateOptions.templateFormatVersion,
552562
Metadata: this.templateOptions.metadata
553563
};
@@ -690,9 +700,16 @@ export interface ITemplateOptions {
690700

691701
/**
692702
* Gets or sets the top-level template transform for this stack (e.g. "AWS::Serverless-2016-10-31").
703+
*
704+
* @deprecated use `transforms` instead.
693705
*/
694706
transform?: string;
695707

708+
/**
709+
* Gets or sets the top-level template transform(s) for this stack (e.g. `["AWS::Serverless-2016-10-31"]`).
710+
*/
711+
transforms?: string[];
712+
696713
/**
697714
* Metadata associated with the CloudFormation template.
698715
*/
@@ -746,3 +763,10 @@ interface StackDependency {
746763
stack: Stack;
747764
reason: string;
748765
}
766+
767+
function extractSingleValue<T>(array: T[] | undefined): T[] | T | undefined {
768+
if (array && array.length === 1) {
769+
return array[0];
770+
}
771+
return array;
772+
}

packages/@aws-cdk/core/test/test.stack.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,15 @@ export = {
4444

4545
stack.templateOptions.description = 'StackDescription';
4646
stack.templateOptions.templateFormatVersion = 'TemplateVersion';
47-
stack.templateOptions.transform = 'Transform';
47+
stack.templateOptions.transform = 'DeprecatedField';
48+
stack.templateOptions.transforms = ['Transform'];
4849
stack.templateOptions.metadata = {
4950
MetadataKey: 'MetadataValue'
5051
};
5152

5253
test.deepEqual(toCloudFormation(stack), {
5354
Description: 'StackDescription',
54-
Transform: 'Transform',
55+
Transform: ['DeprecatedField', 'Transform'],
5556
AWSTemplateFormatVersion: 'TemplateVersion',
5657
Metadata: { MetadataKey: 'MetadataValue' }
5758
});

0 commit comments

Comments
 (0)