File size: 5,494 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// generate-manifests.js (v2 - Final)
const fs = require('fs');
const path = require('path');
const SystemDetector = require('./modules/system-detector.js');

//  รับ pluginsDir เป็นพารามิเตอร์
async function generateManifests(pluginsDir) {
    console.log(' Starting Intelligent Manifest Generator...');
    console.log(` Scanning target directory: ${pluginsDir}`); // Log path ที่ได้รับมา
    const systemDetector = new SystemDetector();

    if (!fs.existsSync(pluginsDir)) {
        console.error(' Plugins directory not found.');
        return;
    }

    const items = fs.readdirSync(pluginsDir, { withFileTypes: true });
    let generated = 0, skipped = 0;

    for (const item of items) {
        if (!item.isDirectory() || item.name.startsWith('.')) continue;

        const projectPath = path.join(pluginsDir, item.name);
        const manifestPath = path.join(projectPath, 'chahua.json');

        if (fs.existsSync(manifestPath)) {
            console.log(`-  Skipping ${item.name}: Manifest already exists.`);
            skipped++;
            continue;
        }

        console.log(`+  Analyzing ${item.name}...`);
        try {
            const detectionResult = await systemDetector.detect(projectPath);
            const { type, confidence } = detectionResult;

            const manifestData = {
                name: item.name.replace(/_/g, ' ').replace(/[.-]/g, ' '),
                description: `Auto-generated manifest for ${type} project.`,
                type: type,
                icon: getIconForType(type),
                buttons: generateButtonsForType(type, projectPath, item.name),
                publisher: 'Chahua Development Thailand'  //  เพิ่มบรรทัดนี้
            };

            fs.writeFileSync(manifestPath, JSON.stringify(manifestData, null, 2));
            console.log(`  ->  Successfully generated chahua.json for [${type}] project: ${item.name}`);
            generated++;
        } catch (error) {
            console.error(`  ->  Failed to analyze ${item.name}: ${error.message}`);
        }
    }
    console.log(`\n Manifest generation complete! (${generated} generated, ${skipped} skipped)`);
}

function getIconForType(projectType) {
    const icons = {
        'node': '', 'python': '', 'batch_project': '',
        'executable_project': '', 'standalone_js': '', 'html': ''
    };
    return icons[projectType] || '';
}

function generateButtonsForType(projectType, projectPath, folderName) {
    const buttons = [];
    const rootFiles = fs.readdirSync(projectPath).map(f => f.toLowerCase());

    switch (projectType) {
        case 'node':
            const pkgPath = path.join(projectPath, 'package.json');
            try {
                const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
                buttons.push({ id: 'install', name: 'Install', command: 'npm install', icon: '' });
                if (pkg.scripts?.start) buttons.push({ id: 'start', name: 'Start', command: 'npm start', icon: '' });
                // ลบปุ่ม Dev ออก: if (pkg.scripts?.dev) buttons.push({ id: 'dev', name: 'Dev', command: 'npm run dev', icon: '' });
            } catch { console.warn(`Could not parse package.json for ${folderName}`); }
            break;

        case 'batch_project':
            const batFile = rootFiles.find(f => f === `${folderName.toLowerCase()}.bat`) || rootFiles.find(f => f.endsWith('.bat'));
            if (batFile) {
                buttons.push({ id: 'run-batch', name: 'Run Script', command: 'run-batch', icon: '' });
                buttons.push({ id: 'edit-batch', name: 'Edit Script', command: 'edit-batch', icon: '' });
            }
            break;

        case 'executable_project':
            const exeFile = rootFiles.find(f => f === `${folderName.toLowerCase()}.exe`) || rootFiles.find(f => f.endsWith('.exe'));
            if (exeFile) {
                buttons.push({ id: 'launch-exe', name: 'Launch App', command: 'launch-exe', icon: '' });
                buttons.push({ id: 'run-as-admin', name: 'Run as Admin', command: 'run-as-admin', icon: '' });
            }
            break;
            
        case 'python':
            if (rootFiles.includes('requirements.txt')) {
                buttons.push({ id: 'pip-install', name: 'Install', command: 'pip install -r requirements.txt', icon: '' });
            }
            const pyFile = ['app.py', 'main.py'].find(f => rootFiles.includes(f)) || rootFiles.find(f => f.endsWith('.py'));
            if (pyFile) {
                buttons.push({ id: 'run-python', name: 'Run', command: `python ${pyFile}`, icon: '' });
            }
            break;

        case 'standalone_js':
            const jsFile = rootFiles.find(f => f.endsWith('.js'));
            if (jsFile) {
                buttons.push({ id: 'run-js', name: 'Run Script', command: `node ${jsFile}`, icon: '' });
            }
            break;
    }

    buttons.push({ id: 'open-folder', name: 'Open Folder', command: 'open_explorer', icon: '' });
    return buttons;
}

//  Export the function for use in other modules (e.g., main.js)
module.exports = { generateManifests };

if (require.main === module) {
    //  เมื่อรันโดยตรง ให้ใช้ path แบบเดิม (สำหรับ development)
    const pluginsDir = path.join(__dirname, 'plugins');
    generateManifests(pluginsDir).catch(console.error);
}