UNPKG

@remediator/core

Version:
150 lines (149 loc) 6.65 kB
import reMediator from "./reMediator.js"; const defaultOptions = { path: "src", includeFileNames: [ "*.mediator.ts", "*.command.mediator.ts", "*.query.mediator.ts", "*.middleware.mediator.ts", "*.command.ts", "*.query.ts", "*.middleware.ts", ], }; // Check if we're in a Node.js environment const isNodeEnv = typeof process !== "undefined" && typeof process.versions !== "undefined" && typeof process.versions.node !== "undefined"; export default async function autoRegisterFS(options) { if (!isNodeEnv) { console.warn("reMediator: Filesystem auto-registration only works in Node.js environment"); return; } const opts = { ...defaultOptions, ...options }; try { // Dynamic imports to avoid bundling fs/path in browser builds const fs = await import("fs"); const path = await import("path"); console.log("reMediator: Using filesystem auto-registration"); console.log("reMediator: Search path:", opts.path); console.log("reMediator: File patterns:", opts.includeFileNames); console.log("reMediator: Current working directory:", process.cwd()); const searchPath = opts.path || "src"; const filePatterns = opts.includeFileNames || []; console.log(`reMediator: Searching in '${searchPath}' and all subdirectories`); console.log(`reMediator: Looking for files matching:`, filePatterns); // Check if the search path exists const fullSearchPath = path.resolve(process.cwd(), searchPath); console.log(`reMediator: Full search path: ${fullSearchPath}`); console.log(`reMediator: Path exists: ${fs.existsSync(fullSearchPath)}`); if (!fs.existsSync(fullSearchPath)) { console.warn(`reMediator: Search path '${searchPath}' does not exist`); return; } if (!fs.statSync(fullSearchPath).isDirectory()) { console.warn(`reMediator: Search path '${searchPath}' is not a directory`); return; } console.log(`reMediator: Path is directory: ${fs .statSync(fullSearchPath) .isDirectory()}`); console.log(`reMediator: Contents of ${searchPath}:`, fs.readdirSync(fullSearchPath)); // Recursively find all matching files const files = []; for (const pattern of filePatterns) { console.log(`\nreMediator: === Searching for pattern: ${pattern} ===`); const patternFiles = await findFilesRecursively(fullSearchPath, pattern, fs, path); console.log(`reMediator: Found ${patternFiles.length} files matching '${pattern}':`, patternFiles); files.push(...patternFiles); } // Remove duplicates const uniqueFiles = [...new Set(files)]; console.log("\nreMediator: === SUMMARY ==="); console.log("reMediator: Total unique files found:", uniqueFiles.length); console.log("reMediator: Found files:", uniqueFiles); if (uniqueFiles.length === 0) { console.warn("reMediator: No files found matching patterns"); return; } // Process each file for (const filePath of uniqueFiles) { try { // Use the absolute file path for import const mod = await import(`file://${filePath}`); console.log(`reMediator: Processing module: ${filePath}`); // Iterate through each export in the module Object.entries(mod).forEach(([exportName, exportedValue]) => { if (typeof exportedValue !== "function") return; // Auto-register Commands & Queries by naming convention if (exportName.endsWith("Command") || exportName.endsWith("Query")) { const handlerName = `${exportName}Handler`; const HandlerCtor = mod[handlerName]; if (typeof HandlerCtor === "function") { reMediator.register(exportedValue, new HandlerCtor()); console.log(`reMediator: Registered ${exportName}${handlerName}`); } else { console.warn(`reMediator: No handler found for ${exportName}. Expected export '${handlerName}' in ${filePath}`); } } // Auto-register Middleware if (exportName.endsWith("Middleware")) { reMediator.use(exportedValue); console.log(`reMediator: Registered middleware → ${exportName}`); } }); } catch (error) { console.warn(`reMediator: Failed to process ${filePath}:`, error.message); } } } catch (error) { console.error("reMediator: Error during filesystem auto-registration:", error); } } // Simple recursive file search async function findFilesRecursively(dirPath, pattern, fs, path) { const files = []; try { const items = fs.readdirSync(dirPath); for (const item of items) { const fullPath = path.join(dirPath, item); const stat = fs.statSync(fullPath); if (stat.isDirectory()) { // Skip node_modules, dist, build, .git directories if (item === "node_modules" || item === "dist" || item === "build" || item === ".git") { continue; } // Recursively search subdirectories const subFiles = await findFilesRecursively(fullPath, pattern, fs, path); files.push(...subFiles); } else if (stat.isFile()) { // Check if file matches pattern const fileName = item; if (matchesPattern(fileName, pattern)) { files.push(fullPath); } } } } catch (error) { console.log(`reMediator: Error reading directory ${dirPath}:`, error.message); } return files; } // Simple pattern matching function matchesPattern(fileName, pattern) { // Convert glob pattern to simple matching const regexPattern = pattern .replace(/\./g, "\\.") // Escape dots .replace(/\*/g, ".*"); // Convert * to .* const regex = new RegExp(`^${regexPattern}$`); return regex.test(fileName); }