File size: 6,641 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/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 <project-name>');
            console.log('   npm run project:start <project-name>');
            console.log('   npm run project:stop <project-name>');
            console.log('   npm run project:status <project-name>');
            
        } 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 <action> <project-name>');
            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 <command> [project-name]');
        console.log('\nCommands:');
        console.log('  list                     - List all available projects');
        console.log('  install <project-name>   - Install project dependencies');
        console.log('  start <project-name>     - Start project server');
        console.log('  stop <project-name>      - Stop project server');
        console.log('  status <project-name>    - 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;