aws-cdk-lib
Version:
Version 2 of the AWS Cloud Development Kit library
2 lines (1 loc) • 2.86 kB
JavaScript
;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},helpers_internal_1=()=>{var tmp=require("../../helpers-internal");return helpers_internal_1=()=>tmp,tmp},stack_1=()=>{var tmp=require("../../stack");return stack_1=()=>tmp,tmp};class ConstructTree{_constructByPath=new Map;_constructByTemplatePathAndLogicalId=new Map;root;constructor(root){this.root=app_1().App.of(root)??root,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]]))}traceFromPath(constructPath){const components=constructPath.replace(/^\//,"").split("/"),rootPath=[],stackTraces=[];let node=this.root;for(;node;){rootPath.push(this.constructTraceLevelFromTreeNode(node)),stackTraces.push(this.stackTrace(node));const component=components.shift();node=component!==void 0?node.node.tryFindChild(component):void 0}rootPath.shift(),stackTraces.shift();let ret={...rootPath[rootPath.length-1],location:stackLocationAt(rootPath.length-1)};for(let i=rootPath.length-2;i>=0;i--)ret={...rootPath[i],child:ret,location:stackLocationAt(i)};return ret;function stackLocationAt(i){let j=i;for(;j<rootPath.length&&!stackTraces[j];)j++;return j===rootPath.length?STACK_TRACE_HINT:stackTraces[j]?.[0+j-i]??STACK_TRACE_HINT}}constructTraceLevelFromTreeNode(node){const constructInfo=(0,helpers_internal_1().constructInfoFromConstruct)(node);return{id:node.node.id,path:node.node.path,construct:constructInfo?.fqn,libraryVersion:constructInfo?.version}}stackTrace(construct){return construct?.node.metadata.find(meta=>!!meta.trace)?.trace}getConstructByPath(path){return this._constructByPath.get(path)}getConstructByLogicalId(templateFile,logicalId){return this._constructByTemplatePathAndLogicalId.get(templateFile)?.get(logicalId)}}exports.ConstructTree=ConstructTree;const STACK_TRACE_HINT="Run with '--debug' to include location info";