/** * Plugin Manager (V4 - Correct Pathing) * Chahua Development Thailand * Purpose: โหลดและจัดการ plugins ผ่าน chahua.json manifest เท่านั้น * แก้ไข: กำหนด Path ของ plugins ให้ถูกต้องทั้งในโหมด Dev และ Production */ const fs = require('fs'); const path = require('path'); const os = require('os'); class PluginManager { constructor() { this.projectFeatures = new Map(); // --- ส่วน Path Detection ที่แก้ไขใหม่ให้ถูกต้อง 100% --- const { app } = require('electron'); if (app.isPackaged) { // Production Mode: ใช้โฟลเดอร์ 'plugins' ที่อยู่ข้างไฟล์ .exe this.pluginsDir = path.join(path.dirname(app.getPath('exe')), 'plugins'); } else { // Development Mode: ใช้โฟลเดอร์ 'plugins' ใน AppData this.pluginsDir = path.join(app.getPath('userData'), 'plugins'); } // สร้างโฟลเดอร์ plugins ให้ผู้ใช้เลย ถ้ายังไม่มี if (!fs.existsSync(this.pluginsDir)) { try { fs.mkdirSync(this.pluginsDir, { recursive: true }); console.log(` Created user plugins directory at: ${this.pluginsDir}`); } catch (error) { console.error(` Failed to create user plugins directory:`, error); } } console.log(' Plugin Manager initialized. User plugins path is now:', this.pluginsDir); } /** * ฟังก์ชันหลัก: สแกนหาและอ่าน chahua.json จาก Path ที่ถูกต้อง */ async loadPlugins() { console.log(`[PluginManager] Scanning for manifests in '${this.pluginsDir}'...`); this.projectFeatures.clear(); if (!fs.existsSync(this.pluginsDir)) { console.warn(`[PluginManager] Plugins directory not found, skipping scan.`); return; } try { const items = fs.readdirSync(this.pluginsDir, { withFileTypes: true }); for (const item of items) { if (!item.isDirectory()) continue; const projectPath = path.join(this.pluginsDir, item.name); const manifestPath = path.join(projectPath, 'chahua.json'); if (fs.existsSync(manifestPath)) { try { const manifestContent = fs.readFileSync(manifestPath, 'utf8'); const manifestData = JSON.parse(manifestContent); this.projectFeatures.set(item.name, { name: item.name, displayName: manifestData.name || item.name, path: projectPath, description: manifestData.description || 'No description.', type: manifestData.type || 'unknown', icon: manifestData.icon || '', buttons: manifestData.buttons || [] }); console.log(` -> Registered plugin: ${manifestData.name || item.name}`); } catch (e) { console.error(` -> Error parsing manifest for ${item.name}: ${e.message}`); } } } } catch (error) { console.error(`[PluginManager] Error during manifest scan: ${error.message}`); } } getProjectFeatures() { return Array.from(this.projectFeatures.values()); } getPluginStats() { return { projects: this.getProjectFeatures() }; } } module.exports = PluginManager;