// 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); }