#!/usr/bin/env node /** * Project Manager Script - จัดการโปรเจกต์ผ่าน command line * Chahua Development Thailand * CEO: Saharath C. */ const fs = require('fs'); const path = require('path'); const { exec } = require('child_process'); class ProjectManager { constructor() { this.projectsDir = path.join(__dirname, '..', 'projects'); this.command = process.argv[2]; this.projectName = process.argv[3]; } async run() { switch (this.command) { case 'list': this.listProjects(); break; case 'install': case 'start': case 'stop': case 'status': await this.runProjectAction(this.command, this.projectName); break; default: this.showUsage(); break; } } listProjects() { try { if (!fs.existsSync(this.projectsDir)) { console.log(' Projects directory not found:', this.projectsDir); return; } const projects = fs.readdirSync(this.projectsDir) .filter(item => { const itemPath = path.join(this.projectsDir, item); return fs.statSync(itemPath).isDirectory() && fs.existsSync(path.join(itemPath, 'package.json')); }); console.log('\n Available NPM Projects:'); console.log('='.repeat(40)); if (projects.length === 0) { console.log(' No projects found with package.json'); return; } projects.forEach((project, index) => { try { const packagePath = path.join(this.projectsDir, project, 'package.json'); const packageData = JSON.parse(fs.readFileSync(packagePath, 'utf8')); console.log(`\n${index + 1}. ${project}`); console.log(` Name: ${packageData.name || project}`); console.log(` Description: ${packageData.description || 'No description'}`); console.log(` Version: ${packageData.version || '1.0.0'}`); console.log(` Path: ./projects/${project}`); if (packageData.scripts) { const scripts = Object.keys(packageData.scripts); console.log(` Scripts: ${scripts.join(', ')}`); } } catch (error) { console.log(` Error reading package.json: ${error.message}`); } }); console.log('\n Usage Examples:'); console.log(' npm run project:install '); console.log(' npm run project:start '); console.log(' npm run project:stop '); console.log(' npm run project:status '); } catch (error) { console.error(' Error listing projects:', error.message); } } async runProjectAction(action, projectName) { if (!projectName) { console.error(' Project name required'); console.log('Usage: node scripts/project-manager.js '); return; } const projectPath = path.join(this.projectsDir, projectName); if (!fs.existsSync(projectPath) || !fs.existsSync(path.join(projectPath, 'package.json'))) { console.error(` Project not found: ${projectName}`); this.listProjects(); return; } console.log(` ${action.toUpperCase()}ing project: ${projectName}`); console.log('='.repeat(50)); try { if (action === 'install') { await this.runNpmCommand(projectPath, 'install'); } else if (action === 'start') { await this.runNpmCommand(projectPath, 'start'); } else { console.log(`ℹ ${action} action would use framework plugin`); console.log('For now, use the web UI for stop/status operations'); } } catch (error) { console.error(` ${action} failed:`, error.message); } } async runNpmCommand(projectPath, command) { return new Promise((resolve, reject) => { console.log(` Running: npm ${command} in ${path.basename(projectPath)}`); const child = exec(`npm ${command}`, { cwd: projectPath, maxBuffer: 1024 * 1024 * 10 // 10MB }); child.stdout.on('data', (data) => { process.stdout.write(data); }); child.stderr.on('data', (data) => { process.stderr.write(data); }); child.on('close', (code) => { if (code === 0) { console.log(`\n ${command} completed successfully!`); resolve(); } else { reject(new Error(`npm ${command} failed with exit code ${code}`)); } }); child.on('error', reject); }); } showUsage() { console.log('\n Chahuadev Project Manager'); console.log('='.repeat(40)); console.log('Usage: node scripts/project-manager.js [project-name]'); console.log('\nCommands:'); console.log(' list - List all available projects'); console.log(' install - Install project dependencies'); console.log(' start - Start project server'); console.log(' stop - Stop project server'); console.log(' status - Check project status'); console.log('\nExamples:'); console.log(' node scripts/project-manager.js list'); console.log(' node scripts/project-manager.js install sample-app'); console.log(' node scripts/project-manager.js start sample-app'); console.log('\nNPM Scripts:'); console.log(' npm run project:list'); console.log(' npm run project:install sample-app'); console.log(' npm run project:start sample-app'); } } // Run if called directly if (require.main === module) { const manager = new ProjectManager(); manager.run().catch(console.error); } module.exports = ProjectManager;