UNPKG

arky-js

Version:

**Arky.js** is a powerful, annotation-based framework for building serverless applications on **AWS Lambda and API Gateway**. Inspired by Angular and NestJS, Arky.js simplifies serverless development by providing decorators for defining modules, controlle

80 lines (79 loc) 3.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DependencyTreeWalker = void 0; const ts_morph_1 = require("ts-morph"); const class_config_util_1 = require("../../utils/class-config-util"); class DependencyTreeWalker { constructor() { this.project = new ts_morph_1.Project(); } /** * Recursively parses the dependencies of a class and builds a dependency map. * @param cls The class declaration to parse. * @returns A Map<string, string[]> where the key is the fully qualified class name and the value is an array of dependency names. */ buildDependencyMap(cls) { const dependencyMap = new Map(); this.parseDependencies(cls, dependencyMap); return dependencyMap; } /** * Recursively parses the dependencies of a class. * @param cls The class declaration to parse. */ parseDependencies(cls, dependencyMap) { const fullyQualifiedName = this.getFullyQualifiedName(cls); if (!fullyQualifiedName || dependencyMap.has(fullyQualifiedName)) { return; // Skip if the class is already processed } const dependencies = []; // Get the constructor parameters const constructor = cls.getConstructors()[0]; if (constructor) { const parameters = constructor.getParameters(); for (const param of parameters) { const type = param.getType(); const dependencyName = this.getTypeName(type); if (dependencyName) { const dependencyClass = (0, class_config_util_1.findClassByImport)(cls, dependencyName, this.project); if (dependencyClass) { const dependencyFullyQualifiedName = this.getFullyQualifiedName(dependencyClass); dependencies.push(dependencyFullyQualifiedName); // Recursively parse dependencies of the dependency this.parseDependencies(dependencyClass, dependencyMap); } } } } // Add the class and its dependencies to the map dependencyMap.set(fullyQualifiedName, dependencies); } /** * Gets the fully qualified name of a class. * @param cls The class declaration. * @returns The fully qualified name (e.g., "module/path:ClassName"). */ getFullyQualifiedName(cls) { const sourceFile = cls.getSourceFile(); const filePath = sourceFile.getFilePath(); // Full path to the file const className = cls.getName(); return `${filePath}:::${className}`; // Combine file path and class name } /** * Gets the name of a type. * @param type The type to get the name for. * @returns The name of the type. */ getTypeName(type) { const symbol = type.getSymbol(); return symbol === null || symbol === void 0 ? void 0 : symbol.getName(); } /** * Adds a source file to the project. * @param filePath The path to the source file. */ addSourceFile(filePath) { this.project.addSourceFileAtPath(filePath); } } exports.DependencyTreeWalker = DependencyTreeWalker;