UNPKG

tusktsk

Version:

TuskTsk - The Freedom Configuration Language. Query databases, use any syntax, never bow to any king!

308 lines (263 loc) 8.65 kB
/** * Service Commands for TuskLang CLI * ================================== * Implements service management commands */ const { Command } = require('commander'); const fs = require('fs').promises; const path = require('path'); // Service status tracking let serviceStatus = { running: false, pid: null, startTime: null, services: {} }; // Service start command const start = new Command('start') .description('Start all TuskLang services') .action(async () => { try { console.log('🔄 Starting TuskLang services...'); // Start development server const devServer = await startDevServer(); // Start database services const dbServices = await startDatabaseServices(); // Start cache services const cacheServices = await startCacheServices(); serviceStatus.running = true; serviceStatus.startTime = new Date(); serviceStatus.services = { devServer, dbServices, cacheServices }; console.log('✅ All TuskLang services started successfully'); console.log(`📍 Services running since: ${serviceStatus.startTime.toISOString()}`); return { success: true, services: serviceStatus.services }; } catch (error) { console.error('❌ Failed to start services:', error.message); return { success: false, error: error.message }; } }); // Service stop command const stop = new Command('stop') .description('Stop all TuskLang services') .action(async () => { try { console.log('🔄 Stopping TuskLang services...'); // Stop all running services await stopAllServices(); serviceStatus.running = false; serviceStatus.pid = null; serviceStatus.startTime = null; serviceStatus.services = {}; console.log('✅ All TuskLang services stopped successfully'); return { success: true }; } catch (error) { console.error('❌ Failed to stop services:', error.message); return { success: false, error: error.message }; } }); // Service restart command const restart = new Command('restart') .description('Restart all TuskLang services') .action(async () => { try { console.log('🔄 Restarting TuskLang services...'); // Stop services await stopAllServices(); // Start services const devServer = await startDevServer(); const dbServices = await startDatabaseServices(); const cacheServices = await startCacheServices(); serviceStatus.running = true; serviceStatus.startTime = new Date(); serviceStatus.services = { devServer, dbServices, cacheServices }; console.log('✅ All TuskLang services restarted successfully'); return { success: true, services: serviceStatus.services }; } catch (error) { console.error('❌ Failed to restart services:', error.message); return { success: false, error: error.message }; } }); // Service status command const status = new Command('status') .description('Show status of all services') .action(async () => { try { console.log('📍 TuskLang Services Status'); console.log('============================'); if (!serviceStatus.running) { console.log('❌ No services are currently running'); return { running: false }; } console.log(`✅ Services running since: ${serviceStatus.startTime.toISOString()}`); console.log(`📍 Uptime: ${getUptime()}`); console.log(''); // Check individual service status const devServerStatus = await checkDevServerStatus(); const dbStatus = await checkDatabaseStatus(); const cacheStatus = await checkCacheStatus(); console.log('📊 Service Details:'); console.log(` Development Server: ${devServerStatus ? '✅ Running' : '❌ Stopped'}`); console.log(` Database Services: ${dbStatus ? '✅ Running' : '❌ Stopped'}`); console.log(` Cache Services: ${cacheStatus ? '✅ Running' : '❌ Stopped'}`); return { running: true, startTime: serviceStatus.startTime, uptime: getUptime(), services: { devServer: devServerStatus, database: dbStatus, cache: cacheStatus } }; } catch (error) { console.error('❌ Failed to get service status:', error.message); return { success: false, error: error.message }; } }); // Helper functions async function startDevServer() { try { // In a real implementation, this would start the development server // For now, we'll simulate it console.log(' 📍 Starting development server...'); // Simulate server startup await new Promise(resolve => setTimeout(resolve, 1000)); return { port: 8080, url: 'http://localhost:8080', status: 'running' }; } catch (error) { throw new Error(`Failed to start development server: ${error.message}`); } } async function startDatabaseServices() { try { console.log(' 📍 Starting database services...'); // Check for database configuration const peanutConfig = new (require('../../peanut-config.js'))(); const config = peanutConfig.load(process.cwd()); if (config.database) { console.log(` Database: ${config.database.type || 'unknown'}`); return { type: config.database.type || 'unknown', status: 'running' }; } else { console.log(' Database: No configuration found'); return { type: 'none', status: 'not configured' }; } } catch (error) { console.warn(' Database: Failed to start'); return { type: 'error', status: 'failed', error: error.message }; } } async function startCacheServices() { try { console.log(' 📍 Starting cache services...'); // Check for cache configuration const peanutConfig = new (require('../../peanut-config.js'))(); const config = peanutConfig.load(process.cwd()); if (config.cache) { console.log(` Cache: ${config.cache.driver || 'memory'}`); return { driver: config.cache.driver || 'memory', status: 'running' }; } else { console.log(' Cache: Using memory cache'); return { driver: 'memory', status: 'running' }; } } catch (error) { console.warn(' Cache: Failed to start'); return { driver: 'memory', status: 'failed', error: error.message }; } } async function stopAllServices() { try { console.log(' 📍 Stopping all services...'); // In a real implementation, this would stop all running services // For now, we'll simulate it await new Promise(resolve => setTimeout(resolve, 500)); console.log(' All services stopped'); } catch (error) { throw new Error(`Failed to stop services: ${error.message}`); } } async function checkDevServerStatus() { try { // In a real implementation, this would check if the dev server is responding // For now, we'll return the status from our tracking return serviceStatus.services.devServer?.status === 'running'; } catch (error) { return false; } } async function checkDatabaseStatus() { try { // Check database connection const adapters = require('../../adapters/sqlite.js'); const adapter = new adapters({ filename: ':memory:' }); const isConnected = await adapter.isConnected(); return isConnected; } catch (error) { return false; } } async function checkCacheStatus() { try { // In a real implementation, this would check cache connectivity // For now, we'll return true if we have cache configuration return serviceStatus.services.cacheServices?.status === 'running'; } catch (error) { return false; } } function getUptime() { if (!serviceStatus.startTime) { return '0s'; } const now = new Date(); const diff = now - serviceStatus.startTime; const seconds = Math.floor(diff / 1000); const minutes = Math.floor(seconds / 60); const hours = Math.floor(minutes / 60); const days = Math.floor(hours / 24); if (days > 0) { return `${days}d ${hours % 24}h ${minutes % 60}m`; } else if (hours > 0) { return `${hours}h ${minutes % 60}m`; } else if (minutes > 0) { return `${minutes}m ${seconds % 60}s`; } else { return `${seconds}s`; } } module.exports = { start, stop, restart, status };