|
| 1 | +import type { IConstruct } from 'constructs'; |
| 2 | +import { CfnResource } from 'aws-cdk-lib/core'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Selects constructs from a construct tree based on various criteria. |
| 6 | + */ |
| 7 | +export abstract class ConstructSelector { |
| 8 | + /** |
| 9 | + * Selects all constructs in the tree. |
| 10 | + */ |
| 11 | + static all(): ConstructSelector { |
| 12 | + return new AllConstructsSelector(); |
| 13 | + } |
| 14 | + |
| 15 | + /** |
| 16 | + * Selects CfnResource constructs or the default CfnResource child. |
| 17 | + */ |
| 18 | + static cfnResource(): ConstructSelector { |
| 19 | + return new CfnResourceSelector(); |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * Selects constructs of a specific type. |
| 24 | + */ |
| 25 | + static resourcesOfType(type: string | any): ConstructSelector { |
| 26 | + return new ResourceTypeSelector(type); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Selects constructs whose IDs match a pattern. |
| 31 | + */ |
| 32 | + static byId(pattern: any): ConstructSelector { |
| 33 | + return new IdPatternSelector(pattern); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Selects constructs from the given scope based on the selector's criteria. |
| 38 | + */ |
| 39 | + abstract select(scope: IConstruct): IConstruct[]; |
| 40 | +} |
| 41 | + |
| 42 | +class AllConstructsSelector extends ConstructSelector { |
| 43 | + select(scope: IConstruct): IConstruct[] { |
| 44 | + const result: IConstruct[] = []; |
| 45 | + const visit = (node: IConstruct) => { |
| 46 | + result.push(node); |
| 47 | + for (const child of node.node.children) { |
| 48 | + visit(child); |
| 49 | + } |
| 50 | + }; |
| 51 | + visit(scope); |
| 52 | + return result; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +class CfnResourceSelector extends ConstructSelector { |
| 57 | + select(scope: IConstruct): IConstruct[] { |
| 58 | + if (CfnResource.isCfnResource(scope)) { |
| 59 | + return [scope]; |
| 60 | + } |
| 61 | + const defaultChild = scope.node.defaultChild; |
| 62 | + if (CfnResource.isCfnResource(defaultChild)) { |
| 63 | + return [defaultChild]; |
| 64 | + } |
| 65 | + return []; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +class ResourceTypeSelector extends ConstructSelector { |
| 70 | + constructor(private readonly type: string | any) { |
| 71 | + super(); |
| 72 | + } |
| 73 | + |
| 74 | + select(scope: IConstruct): IConstruct[] { |
| 75 | + const result: IConstruct[] = []; |
| 76 | + const visit = (node: IConstruct) => { |
| 77 | + if (typeof this.type === 'string') { |
| 78 | + if (CfnResource.isCfnResource(node) && node.cfnResourceType === this.type) { |
| 79 | + result.push(node); |
| 80 | + } |
| 81 | + } else if ('isCfnResource' in this.type && 'CFN_RESOURCE_TYPE_NAME' in this.type) { |
| 82 | + if (CfnResource.isCfnResource(node) && node.cfnResourceType === this.type.CFN_RESOURCE_TYPE_NAME) { |
| 83 | + result.push(node); |
| 84 | + } |
| 85 | + } else { |
| 86 | + if (node instanceof this.type) { |
| 87 | + result.push(node); |
| 88 | + } |
| 89 | + } |
| 90 | + for (const child of node.node.children) { |
| 91 | + visit(child); |
| 92 | + } |
| 93 | + }; |
| 94 | + visit(scope); |
| 95 | + return result; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +class IdPatternSelector extends ConstructSelector { |
| 100 | + constructor(private readonly pattern: any) { |
| 101 | + super(); |
| 102 | + } |
| 103 | + |
| 104 | + select(scope: IConstruct): IConstruct[] { |
| 105 | + const result: IConstruct[] = []; |
| 106 | + const visit = (node: IConstruct) => { |
| 107 | + if (this.pattern && typeof this.pattern.test === 'function' && this.pattern.test(node.node.id)) { |
| 108 | + result.push(node); |
| 109 | + } |
| 110 | + for (const child of node.node.children) { |
| 111 | + visit(child); |
| 112 | + } |
| 113 | + }; |
| 114 | + visit(scope); |
| 115 | + return result; |
| 116 | + } |
| 117 | +} |
0 commit comments