UNPKG

dipend-graph

Version:

A library for generating a DAG (Directed Acyclic Graph) of dependencies registered in Dipend's dependency container.

69 lines (67 loc) 2.72 kB
/* * Copyright 2025 Saulo V. Alvarenga. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GraphDataHandler = void 0; class GraphDataHandler { dependencyContainer; constructor(dependencyContainer) { this.dependencyContainer = dependencyContainer; } getNodeName(dependencyId) { const tokens = this.dependencyContainer.dependencyTokenStore.getTokens(dependencyId); const tokenNames = tokens.map((token) => { const tokenType = this.dependencyContainer.dependencyTokenType.getTokenType(token); return this.dependencyContainer.dependencyTokenName.getTokenName(token, tokenType); }); return tokenNames.join(":"); } getNodeType(dependencyId) { const dependencyRegistry = this.dependencyContainer.dependencyStore.dependencies.get(dependencyId); return dependencyRegistry.lifecycle.charAt(0).toUpperCase() + dependencyRegistry.lifecycle.slice(1).toLowerCase(); } getNode(dependencyId) { return { node: this.getNodeName(dependencyId), type: this.getNodeType(dependencyId), }; } getTypes() { return [ { type: "Singleton", color: "#03C800" }, { type: "Transient", color: "#FF5733" }, ]; } getDependencyGraphData() { const dependencyIds = this.dependencyContainer.dependencyStore.getSortedDependenciesIds(); const graphAndDegrees = this.dependencyContainer.dependencyStore.initializeGraphAndDegrees(); const nodes = dependencyIds.map((dependencyId) => this.getNode(dependencyId)); const links = []; for (const [source, targets] of graphAndDegrees.graph.entries()) { targets.forEach((target) => { links.push({ source: this.getNodeName(source), target: this.getNodeName(target), }); }); } return { nodes, links, types: this.getTypes() }; } handle() { return this.getDependencyGraphData(); } } exports.GraphDataHandler = GraphDataHandler;