File size: 3,830 Bytes
857cdcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
 *  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;