UNPKG

woaru

Version:

Universal Project Setup Autopilot - Analyze and automatically configure development tools for ANY programming language

99 lines 3.59 kB
import axios from 'axios'; import fs from 'fs-extra'; import * as path from 'path'; import { fileURLToPath } from 'url'; import { dirname } from 'path'; // ES module compatibility const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); export class DatabaseManager { localDbPath; remoteDbUrl; cachedDb = null; constructor() { this.localDbPath = path.join(__dirname, 'tools-db.json'); this.remoteDbUrl = 'https://raw.githubusercontent.com/iamthamanic/WOARU-WorkaroundUltra/main/src/database/tools-db.json'; } async getDatabase() { if (this.cachedDb) { return this.cachedDb; } try { const dbContent = await fs.readFile(this.localDbPath, 'utf-8'); const db = JSON.parse(dbContent); this.cachedDb = db; return db; } catch (error) { throw new Error(`Failed to load tools database: ${error}`); } } async updateDatabase() { try { const response = await axios.get(this.remoteDbUrl, { timeout: 10000, }); const remoteDb = response.data; // Validate database structure if (!this.validateDatabase(remoteDb)) { throw new Error('Invalid database structure received from remote'); } // Backup current database const backupPath = `${this.localDbPath}.backup`; if (await fs.pathExists(this.localDbPath)) { await fs.copy(this.localDbPath, backupPath); } // Save new database await fs.writeFile(this.localDbPath, JSON.stringify(remoteDb, null, 2)); this.cachedDb = remoteDb; return true; } catch (error) { console.error('Failed to update database:', error); return false; } } validateDatabase(db) { return (typeof db === 'object' && db !== null && typeof db.version === 'string' && typeof db.lastUpdated === 'string' && typeof db.categories === 'object' && typeof db.frameworks === 'object'); } async getToolsForFramework(framework) { const db = await this.getDatabase(); const frameworkConfig = db.frameworks[framework]; if (!frameworkConfig) { return []; } const tools = []; Object.entries(frameworkConfig.recommendedTools).forEach(([_category, categoryTools]) => { tools.push(...categoryTools); }); return [...new Set(tools)]; // Remove duplicates } async getToolConfig(toolName, framework) { const db = await this.getDatabase(); // Search through all categories for the tool for (const [, categoryTools] of Object.entries(db.categories)) { if (categoryTools[toolName]) { const toolConfig = categoryTools[toolName]; // Return framework-specific config if available if (framework && toolConfig.configs && toolConfig.configs[framework]) { return { ...toolConfig, packages: [ ...toolConfig.packages, ...toolConfig.configs[framework], ], }; } return toolConfig; } } return null; } } //# sourceMappingURL=DatabaseManager.js.map