-
Notifications
You must be signed in to change notification settings - Fork 0
/
tagUtils.ts
53 lines (45 loc) · 1.84 KB
/
tagUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { Annotations, Aspects, IAspect, Stage, Tag } from "aws-cdk-lib";
import { IConstruct } from "constructs";
// Recursively Invoke Tag aspects
// This code is completely copied from the CDK source code except for filtering for Tag aspects
// Source:
// https://github.com/aws/aws-cdk/blob/main/packages/aws-cdk-lib/core/lib/private/synthesis.ts#L217
function invokeTags(root: IConstruct) {
const invokedByPath: { [nodePath: string]: IAspect[] } = {};
let nestedAspectWarning = false;
recurse(root, []);
function recurse(construct: IConstruct, inheritedAspects: IAspect[]) {
const node = construct.node;
const aspects = Aspects.of(construct);
const tags = aspects.all.filter((a) => a instanceof Tag);
const allAspectsHere = [...(inheritedAspects ?? []), ...tags];
const nodeAspectsCount = aspects.all.length;
for (const aspect of allAspectsHere) {
let invoked = invokedByPath[node.path];
if (!invoked) {
invoked = invokedByPath[node.path] = [];
}
if (invoked.includes(aspect)) {
continue;
}
aspect.visit(construct);
// if an aspect was added to the node while invoking another aspect it will not be invoked, emit a warning
// the `nestedAspectWarning` flag is used to prevent the warning from being emitted for every child
if (!nestedAspectWarning && nodeAspectsCount !== aspects.all.length) {
Annotations.of(construct).addWarningV2(
"@aws-cdk/core:ignoredAspect",
"We detected an Aspect was added via another Aspect, and will not be applied",
);
nestedAspectWarning = true;
}
// mark as invoked for this node
invoked.push(aspect);
}
for (const child of construct.node.children) {
if (!Stage.isStage(child)) {
recurse(child, allAspectsHere);
}
}
}
}
export { invokeTags };