File size: 9,881 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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | #!/usr/bin/env node
/**
* Chahuadev Framework - CLI Interface
* Chahua Development Thailand
* CEO: Saharath C.
*
* Purpose: Command Line Interface สำหรับ automation และ integration
*/
const { program } = require('commander');
const path = require('path');
const fs = require('fs');
// Import core modules (same as app.js)
const ValidationGateway = require('./validation_gateway');
const ContextManager = require('./modules/context-manager');
const ErrorHandler = require('./modules/error-handler');
const Executor = require('./modules/executor');
const SystemDetector = require('./modules/system-detector');
const PluginManager = require('./modules/plugin-manager');
class ChahuadevCLI {
constructor() {
this.validationGateway = new ValidationGateway();
this.contextManager = new ContextManager();
this.errorHandler = new ErrorHandler();
this.executor = new Executor();
this.systemDetector = new SystemDetector();
this.pluginManager = new PluginManager();
console.log(' Chahuadev CLI initialized');
}
async initialize() {
try {
// Load plugins
await this.pluginManager.loadPlugins();
console.log(' CLI ready with all modules loaded');
} catch (error) {
console.error(' CLI initialization failed:', error.message);
process.exit(1);
}
}
async executeCommand(command, options = {}) {
const context = this.contextManager.create('cli-execution', {
command,
options,
timestamp: new Date().toISOString(),
cwd: process.cwd()
});
try {
// Validate request
const validation = await this.validationGateway.validateRequest({
command,
...options
});
if (!validation.success) {
throw new Error(`Security validation failed: ${validation.error}`);
}
// Execute through executor
const result = await this.executor.execute({
command,
project: options.project || process.cwd(),
strategy: options.strategy
}, context);
return result;
} catch (error) {
this.errorHandler.handle(error, context.id);
throw error;
}
}
async scanProject(projectPath) {
try {
const fullPath = path.resolve(projectPath);
if (!fs.existsSync(fullPath)) {
throw new Error(`Project path does not exist: ${fullPath}`);
}
const detection = await this.systemDetector.detectProject(fullPath);
console.log('\n Project Scan Results:');
console.log('========================');
console.log('Project Path:', fullPath);
console.log('Detected Type:', detection.type);
console.log('Confidence:', `${(detection.confidence * 100).toFixed(1)}%`);
console.log('Strategy:', detection.strategy);
if (detection.details) {
console.log('\n Details:');
Object.entries(detection.details).forEach(([key, value]) => {
console.log(` ${key}: ${value}`);
});
}
return detection;
} catch (error) {
console.error(' Project scan failed:', error.message);
throw error;
}
}
async listPlugins() {
try {
const plugins = this.pluginManager.getAvailablePlugins();
console.log('\n Available Plugins:');
console.log('====================');
if (plugins.length === 0) {
console.log('No plugins loaded');
return;
}
plugins.forEach(plugin => {
console.log(` ${plugin.name}`);
console.log(` File: ${plugin.filename}`);
console.log(` Status: ${plugin.status}`);
console.log(` Loaded: ${plugin.loadedAt.toISOString()}`);
if (plugin.metadata.description) {
console.log(` Description: ${plugin.metadata.description}`);
}
console.log('');
});
return plugins;
} catch (error) {
console.error(' Failed to list plugins:', error.message);
throw error;
}
}
async executePlugin(pluginName, data) {
try {
const result = await this.pluginManager.executePlugin(pluginName, data);
if (result.success) {
console.log(` Plugin ${pluginName} executed successfully`);
console.log('Result:', JSON.stringify(result.result, null, 2));
} else {
console.error(` Plugin ${pluginName} failed: ${result.error}`);
}
return result;
} catch (error) {
console.error(' Plugin execution failed:', error.message);
throw error;
}
}
async getStatus() {
const status = {
framework: 'Chahuadev Framework',
version: '1.0.0',
interface: 'CLI',
timestamp: new Date().toISOString(),
modules: {
validationGateway: !!this.validationGateway,
contextManager: !!this.contextManager,
errorHandler: !!this.errorHandler,
executor: !!this.executor,
systemDetector: !!this.systemDetector,
pluginManager: !!this.pluginManager
},
plugins: this.pluginManager.getPluginStats()
};
console.log('\n System Status:');
console.log('================');
console.log(JSON.stringify(status, null, 2));
return status;
}
}
// CLI Commands Setup
async function setupCommands() {
const cli = new ChahuadevCLI();
await cli.initialize();
program
.name('chahuadev')
.description('Chahuadev Framework CLI')
.version('1.0.0');
// Execute command
program
.command('execute <command>')
.description('Execute a command through the framework')
.option('-p, --project <path>', 'Project path', process.cwd())
.option('-s, --strategy <strategy>', 'Force specific strategy')
.action(async (command, options) => {
try {
console.log(` Executing: ${command}`);
const result = await cli.executeCommand(command, options);
console.log(' Execution completed');
console.log(JSON.stringify(result, null, 2));
} catch (error) {
console.error(' Execution failed:', error.message);
process.exit(1);
}
});
// Scan project
program
.command('scan <path>')
.description('Scan project and detect type')
.action(async (projectPath) => {
try {
await cli.scanProject(projectPath);
} catch (error) {
console.error(' Scan failed:', error.message);
process.exit(1);
}
});
// Plugin management
program
.command('plugins')
.description('List available plugins')
.action(async () => {
try {
await cli.listPlugins();
} catch (error) {
console.error(' Failed to list plugins:', error.message);
process.exit(1);
}
});
program
.command('plugin <name>')
.description('Execute a plugin')
.option('-d, --data <data>', 'JSON data to pass to plugin', '{}')
.action(async (name, options) => {
try {
const data = JSON.parse(options.data);
await cli.executePlugin(name, data);
} catch (error) {
console.error(' Plugin execution failed:', error.message);
process.exit(1);
}
});
// Status
program
.command('status')
.description('Show system status')
.action(async () => {
try {
await cli.getStatus();
} catch (error) {
console.error(' Failed to get status:', error.message);
process.exit(1);
}
});
// Help command
program
.command('help')
.description('Show help information')
.action(() => {
console.log('\n Chahuadev Framework CLI');
console.log('===========================');
console.log('Usage examples:');
console.log('');
console.log(' chahuadev execute "npm test" # Execute npm test');
console.log(' chahuadev execute "node app.js" -p ./src # Execute in specific project');
console.log(' chahuadev scan ./my-project # Scan project type');
console.log(' chahuadev plugins # List available plugins');
console.log(' chahuadev plugin zip-compressor # Execute plugin');
console.log(' chahuadev status # Show system status');
console.log('');
program.help();
});
return { cli, program };
}
// Main execution
if (require.main === module) {
setupCommands().then(({ program }) => {
// Parse command line arguments
program.parse();
}).catch(error => {
console.error(' CLI setup failed:', error.message);
process.exit(1);
});
}
module.exports = { ChahuadevCLI, setupCommands }; |