UNPKG

weaver-frontend-cli

Version:

🕷️ Weaver CLI - Generador completo de arquitectura Clean Architecture con parser OpenAPI avanzado para entidades CRUD y flujos de negocio complejos

155 lines 5.85 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DirectoryDetector = void 0; const path = __importStar(require("path")); const fs = __importStar(require("fs-extra")); const chalk_1 = __importDefault(require("chalk")); class DirectoryDetector { /** * Detecta el API name del directorio actual */ static async detectCurrentApi() { const currentDir = process.cwd(); const baseName = path.basename(currentDir); // Verificar si estamos dentro de un directorio que parece ser un API const isLikelyApiDirectory = await this.isApiDirectory(currentDir); // Buscar APIs hermanas (directorios al mismo nivel) const parentDir = path.dirname(currentDir); const siblingApis = await this.findSiblingApis(parentDir, baseName); return { currentApiName: isLikelyApiDirectory ? baseName : null, detectedFromPath: isLikelyApiDirectory, possibleApiNames: siblingApis, baseDirectory: currentDir }; } /** * Verifica si un directorio parece ser un API (tiene estructura típica) */ static async isApiDirectory(dirPath) { const indicatorPaths = [ 'domain', 'infrastructure', 'facade', 'core', 'domain/models', 'domain/services' ]; let foundIndicators = 0; for (const indicator of indicatorPaths) { const fullPath = path.join(dirPath, indicator); if (await fs.pathExists(fullPath)) { foundIndicators++; } } // Si encuentra al menos 2 indicadores, probablemente es un API return foundIndicators >= 2; } /** * Encuentra APIs hermanas en el directorio padre */ static async findSiblingApis(parentDir, currentDirName) { try { const siblings = []; const entries = await fs.readdir(parentDir, { withFileTypes: true }); for (const entry of entries) { if (entry.isDirectory()) { const siblingPath = path.join(parentDir, entry.name); const isApi = await this.isApiDirectory(siblingPath); if (isApi) { siblings.push(entry.name); } } } return siblings.sort(); } catch (error) { console.log(chalk_1.default.yellow('⚠️ No se pudo analizar el directorio padre')); return [currentDirName].filter(Boolean); } } /** * Calcula la ruta de generación según el API target */ static calculateTargetPath(baseDir, currentApiName, targetApiName) { // SIEMPRE usar el directorio actual como base // El targetApiName se usará para crear la estructura apis/{targetApiName}/ return baseDir; } /** * Valida que el directorio target sea accesible */ static async validateTargetPath(targetPath) { try { // Verificar si el directorio existe if (await fs.pathExists(targetPath)) { // Verificar permisos de escritura await fs.access(targetPath, fs.constants.W_OK); return { isValid: true, message: `Directorio target válido: ${targetPath}` }; } else { // El directorio no existe, pero podemos crearlo const parentDir = path.dirname(targetPath); if (await fs.pathExists(parentDir)) { return { isValid: true, message: `Se creará el directorio: ${targetPath}` }; } else { return { isValid: false, message: `El directorio padre no existe: ${parentDir}` }; } } } catch (error) { return { isValid: false, message: `Sin permisos de escritura en: ${targetPath}` }; } } } exports.DirectoryDetector = DirectoryDetector; //# sourceMappingURL=directory-detector.js.map