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

47 lines (46 loc) 2.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.findClassByImport = findClassByImport; exports.hasDecorator = hasDecorator; const ts_morph_1 = require("ts-morph"); const file_util_1 = require("./file-util"); /** * Finds a class by its import path in the original source file. * @param cls The class declaration where the dependency is used. * @param className The name of the dependency class. * @returns The class declaration or undefined if not found. */ function findClassByImport(cls, className, project) { const sourceFile = cls.getSourceFile(); // Get all import declarations in the source file const importDeclarations = sourceFile.getImportDeclarations(); for (const importDecl of importDeclarations) { const namedImports = importDecl.getNamedImports(); for (const namedImport of namedImports) { if (namedImport.getName() === className) { // Resolve the module specifier (import path) const moduleSpecifier = importDecl.getModuleSpecifierValue(); const moduleSpecifierAbsolute = (0, file_util_1.resolveAbsolutePath)(sourceFile.getFilePath(), moduleSpecifier); const resolvedSourceFile = project.addSourceFileAtPath(moduleSpecifierAbsolute); if (resolvedSourceFile) { // Find the class in the resolved source file return resolvedSourceFile.getClass(className); } } } } return undefined; } function hasDecorator(filePath, className, decoratorName, project) { if (!project) { project = new ts_morph_1.Project({ tsConfigFilePath: "tsconfig.json" }); } const sourceFile = project.addSourceFileAtPath(filePath); const classNode = sourceFile.getClass(className); if (!classNode) { throw new Error(`Class "${className}" not found in ${filePath}`); } return classNode .getDecorators() .some((decorator) => decorator.getName() === decoratorName); }