aws-cdk-lib
Version:
Version 2 of the AWS Cloud Development Kit library
2 lines (1 loc) • 3.34 kB
JavaScript
Object.defineProperty(exports,"__esModule",{value:!0}),exports.TreeMetadata=void 0;var fs=()=>{var tmp=require("fs");return fs=()=>tmp,tmp},path=()=>{var tmp=require("path");return path=()=>tmp,tmp},constructs_1=()=>{var tmp=require("constructs");return constructs_1=()=>tmp,tmp},runtime_info_1=()=>{var tmp=require("./runtime-info");return runtime_info_1=()=>tmp,tmp},cloud_assembly_schema_1=()=>{var tmp=require("../../../cloud-assembly-schema");return cloud_assembly_schema_1=()=>tmp,tmp},annotations_1=()=>{var tmp=require("../annotations");return annotations_1=()=>tmp,tmp},stack_1=()=>{var tmp=require("../stack");return stack_1=()=>tmp,tmp},tree_1=()=>{var tmp=require("../tree");return tree_1=()=>tmp,tmp};const FILE_PATH="tree.json";class TreeMetadata extends constructs_1().Construct{constructor(scope){super(scope,"Tree")}_synthesizeTree(session){const lookup={},visit=construct=>{const childrenMap=construct.node.children.map(c=>{try{return visit(c)}catch(e){annotations_1().Annotations.of(this).addWarningV2(`@aws-cdk/core:failedToRenderTreeMetadata-${c.node.id}`,`Failed to render tree metadata for node [${c.node.id}]. Reason: ${e}`);return}}).filter(child=>child!==void 0).reduce((map,child)=>Object.assign(map,{[child.id]:child}),{}),parent=construct.node.scope,node={id:construct.node.id||"App",path:construct.node.path,parent:parent&&parent.node.path?{id:parent.node.id,path:parent.node.path,constructInfo:(0,runtime_info_1().constructInfoFromConstruct)(parent)}:void 0,children:Object.keys(childrenMap).length===0?void 0:childrenMap,attributes:this.synthAttributes(construct),constructInfo:(0,runtime_info_1().constructInfoFromConstruct)(construct)};return lookup[node.path]=node,node},tree={version:"tree-0.1",tree:visit(this.node.root)};this._tree=lookup;const builder=session.assembly;fs().writeFileSync(path().join(builder.outdir,FILE_PATH),JSON.stringify(tree,(key,value)=>{if(key!=="parent")return value}),{encoding:"utf-8"}),builder.addArtifact("Tree",{type:cloud_assembly_schema_1().ArtifactType.CDK_TREE,properties:{file:FILE_PATH}})}getNodeWithParents(node){if(!this._tree)throw new Error(`attempting to get node branch for ${node.path}, but the tree has not been created yet!`);let tree=node;return node.parent&&(tree={...node,parent:this.getNodeWithParents(this._tree[node.parent.path])}),tree}renderTreeWithChildren(node){function renderTreeWithSingleChild(currentNode2,currentNodeChild){return currentNode2.children={[currentNodeChild.id]:currentNodeChild},currentNode2.parent&&(currentNode2.parent=renderTreeWithSingleChild(currentNode2.parent,currentNode2)),currentNode2}let root=node.parent?renderTreeWithSingleChild(node.parent,node):node;do root.parent&&(root=root.parent);while(root.parent);return root}_getNodeBranch(constructPath){if(!this._tree)throw new Error(`attempting to get node branch for ${constructPath}, but the tree has not been created yet!`);const tree=this._tree[constructPath],treeWithParents=this.getNodeWithParents(tree);return this.renderTreeWithChildren(treeWithParents)}synthAttributes(construct){function canInspect(inspectable){return inspectable.inspect!==void 0}const inspector=new(tree_1()).TreeInspector;if(canInspect(construct))return construct.inspect(inspector),stack_1().Stack.of(construct).resolve(inspector.attributes)}}exports.TreeMetadata=TreeMetadata;
;