UNPKG

aws-cdk-lib

Version:

Version 2 of the AWS Cloud Development Kit library

2 lines (1 loc) 3.31 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.ConstructTree=void 0;var app_1=()=>{var tmp=require("../../app");return app_1=()=>tmp,tmp},cfn_resource_1=()=>{var tmp=require("../../cfn-resource");return cfn_resource_1=()=>tmp,tmp},stack_1=()=>{var tmp=require("../../stack");return stack_1=()=>tmp,tmp};class ConstructTree{constructor(root){this.root=root,this._traceCache=new Map,this._constructByPath=new Map,this._constructByTemplatePathAndLogicalId=new Map,app_1().App.isApp(this.root)?this.treeMetadata=this.root.node.tryFindChild("Tree"):this.treeMetadata=app_1().App.of(this.root)?.node.tryFindChild("Tree"),this._constructByPath.set(this.root.node.path,root),this.root.node.findAll().forEach(child=>{this._constructByPath.set(child.node.path,child);const defaultChild=child.node.defaultChild;if(defaultChild&&cfn_resource_1().CfnResource.isCfnResource(defaultChild)){const stack=stack_1().Stack.of(defaultChild),logicalId=stack.resolve(defaultChild.logicalId);this.setLogicalId(stack,logicalId,child)}}),this.root.node.findAll().forEach(child=>{if(cfn_resource_1().CfnResource.isCfnResource(child)){const stack=stack_1().Stack.of(child),logicalId=stack_1().Stack.of(child).resolve(child.logicalId);this.setLogicalId(stack,logicalId,child)}})}setLogicalId(stack,logicalId,child){this._constructByTemplatePathAndLogicalId.has(stack.templateFile)?this._constructByTemplatePathAndLogicalId.get(stack.templateFile)?.set(logicalId,child):this._constructByTemplatePathAndLogicalId.set(stack.templateFile,new Map([[logicalId,child]]))}getTraceMetadata(size,node){if(node){const construct=this.getConstructByPath(node.path);if(construct){let trace;return cfn_resource_1().CfnResource.isCfnResource(construct)?trace=construct.node.metadata.find(meta=>!!meta.trace)?.trace??[]:trace=construct.node.defaultChild?.node.metadata.find(meta=>!!meta.trace)?.trace??[],Object.create(trace.slice(0,size))}}return[]}getNodeWithTrace(node){return node.children?this.getNodeWithTrace(this.getChild(node.children)):node}lastChild(node){return node.children?this.lastChild(this.getChild(node.children)):node}getTrace(node,locations){const lastChild=this.lastChild(node),trace=this._traceCache.get(lastChild.path);if(trace)return trace;const size=this.nodeSize(node),nodeWithTrace=this.getNodeWithTrace(node),metadata=locations??this.getTraceMetadata(size,nodeWithTrace),thisLocation=metadata.pop(),constructTrace={id:node.id,path:node.path,child:node.children?this.getTrace(this.getChild(node.children),metadata):void 0,construct:node.constructInfo?.fqn,libraryVersion:node.constructInfo?.version,location:thisLocation??"Run with '--debug' to include location info"};return this._traceCache.set(lastChild.path,constructTrace),constructTrace}getChild(children){return Object.values(children)[0]}nodeSize(node){let size=1;if(!node.children)return size;let children=this.getChild(node.children);do size++,children=children.children?this.getChild(children.children):void 0;while(children);return size}getTreeNode(path){return this.treeMetadata._getNodeBranch(path)}getConstructByPath(path){return this._constructByPath.get(path)}getConstructByLogicalId(templateFile,logicalId){return this._constructByTemplatePathAndLogicalId.get(templateFile)?.get(logicalId)}}exports.ConstructTree=ConstructTree;