fix(cli): resolve process hang and improve output consistency
- Add process.stdin.destroy() after rl.close() in all interactive commands to properly release stdin and allow process to exit cleanly - Replace raw console.log with logger methods throughout CLI handlers - Convert manual box drawing to logger.logBox() in daemon.ts - Standardize menu formatting with logger.info() and logger.dim() - Improve migration output to only show when migrations actually run Fixes issue where process would not exit after "Setup complete!" message due to stdin keeping the event loop alive.
This commit is contained in:
@@ -50,11 +50,11 @@ WantedBy=multi-user.target
|
||||
try {
|
||||
await fs.access(configPath);
|
||||
} catch (error) {
|
||||
console.log('');
|
||||
console.log(`${symbols.error} ${theme.error('No configuration found')}`);
|
||||
console.log(` ${theme.dim('Config file:')} ${configPath}`);
|
||||
console.log(` ${theme.dim('Run')} ${theme.command('nupst ups add')} ${theme.dim('to create a configuration')}`);
|
||||
console.log('');
|
||||
logger.log('');
|
||||
logger.error('No configuration found');
|
||||
logger.log(` ${theme.dim('Config file:')} ${configPath}`);
|
||||
logger.log(` ${theme.dim('Run')} ${theme.command('nupst ups add')} ${theme.dim('to create a configuration')}`);
|
||||
logger.log('');
|
||||
throw new Error('Configuration not found');
|
||||
}
|
||||
}
|
||||
@@ -192,11 +192,11 @@ WantedBy=multi-user.target
|
||||
}
|
||||
|
||||
// Display beautiful status
|
||||
console.log('');
|
||||
logger.log('');
|
||||
if (isActive) {
|
||||
console.log(`${symbols.running} ${theme.success('Service:')} ${theme.statusActive('active (running)')}`);
|
||||
logger.log(`${symbols.running} ${theme.success('Service:')} ${theme.statusActive('active (running)')}`);
|
||||
} else {
|
||||
console.log(`${symbols.stopped} ${theme.dim('Service:')} ${theme.statusInactive('inactive')}`);
|
||||
logger.log(`${symbols.stopped} ${theme.dim('Service:')} ${theme.statusInactive('inactive')}`);
|
||||
}
|
||||
|
||||
if (pid || memory || cpu) {
|
||||
@@ -204,14 +204,14 @@ WantedBy=multi-user.target
|
||||
if (pid) details.push(`PID: ${theme.dim(pid)}`);
|
||||
if (memory) details.push(`Memory: ${theme.dim(memory)}`);
|
||||
if (cpu) details.push(`CPU: ${theme.dim(cpu)}`);
|
||||
console.log(` ${details.join(' ')}`);
|
||||
logger.log(` ${details.join(' ')}`);
|
||||
}
|
||||
console.log('');
|
||||
logger.log('');
|
||||
|
||||
} catch (error) {
|
||||
console.log('');
|
||||
console.log(`${symbols.stopped} ${theme.dim('Service:')} ${theme.statusInactive('not installed')}`);
|
||||
console.log('');
|
||||
logger.log('');
|
||||
logger.log(`${symbols.stopped} ${theme.dim('Service:')} ${theme.statusInactive('not installed')}`);
|
||||
logger.log('');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -228,7 +228,7 @@ WantedBy=multi-user.target
|
||||
|
||||
// Check if we have the new multi-UPS config format
|
||||
if (config.upsDevices && Array.isArray(config.upsDevices) && config.upsDevices.length > 0) {
|
||||
console.log(theme.info(`UPS Devices (${config.upsDevices.length}):`));
|
||||
logger.info(`UPS Devices (${config.upsDevices.length}):`);
|
||||
|
||||
// Show status for each UPS
|
||||
for (const ups of config.upsDevices) {
|
||||
@@ -236,7 +236,7 @@ WantedBy=multi-user.target
|
||||
}
|
||||
} else if (config.snmp) {
|
||||
// Legacy single UPS configuration
|
||||
console.log(theme.info('UPS Devices (1):'));
|
||||
logger.info('UPS Devices (1):');
|
||||
const legacyUps = {
|
||||
id: 'default',
|
||||
name: 'Default UPS',
|
||||
@@ -247,16 +247,16 @@ WantedBy=multi-user.target
|
||||
|
||||
await this.displaySingleUpsStatus(legacyUps, snmp);
|
||||
} else {
|
||||
console.log('');
|
||||
console.log(`${symbols.warning} ${theme.warning('No UPS devices configured')}`);
|
||||
console.log(` ${theme.dim('Run')} ${theme.command('nupst ups add')} ${theme.dim('to add a device')}`);
|
||||
console.log('');
|
||||
logger.log('');
|
||||
logger.warn('No UPS devices configured');
|
||||
logger.log(` ${theme.dim('Run')} ${theme.command('nupst ups add')} ${theme.dim('to add a device')}`);
|
||||
logger.log('');
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('');
|
||||
console.log(`${symbols.error} ${theme.error('Failed to retrieve UPS status')}`);
|
||||
console.log(` ${theme.dim(error instanceof Error ? error.message : String(error))}`);
|
||||
console.log('');
|
||||
logger.log('');
|
||||
logger.error('Failed to retrieve UPS status');
|
||||
logger.log(` ${theme.dim(error instanceof Error ? error.message : String(error))}`);
|
||||
logger.log('');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -284,15 +284,15 @@ WantedBy=multi-user.target
|
||||
}
|
||||
|
||||
// Display UPS name and power status
|
||||
console.log(` ${statusSymbol} ${theme.highlight(ups.name)} - ${formatPowerStatus(status.powerStatus)}`);
|
||||
logger.log(` ${statusSymbol} ${theme.highlight(ups.name)} - ${formatPowerStatus(status.powerStatus)}`);
|
||||
|
||||
// Display battery with color coding
|
||||
const batteryColor = getBatteryColor(status.batteryCapacity);
|
||||
const batterySymbol = status.batteryCapacity >= ups.thresholds.battery ? symbols.success : symbols.warning;
|
||||
console.log(` Battery: ${batteryColor(status.batteryCapacity + '%')} ${batterySymbol} Runtime: ${getRuntimeColor(status.batteryRuntime)(status.batteryRuntime + ' min')}`);
|
||||
logger.log(` Battery: ${batteryColor(status.batteryCapacity + '%')} ${batterySymbol} Runtime: ${getRuntimeColor(status.batteryRuntime)(status.batteryRuntime + ' min')}`);
|
||||
|
||||
// Display host info
|
||||
console.log(` ${theme.dim(`Host: ${ups.snmp.host}:${ups.snmp.port}`)}`);
|
||||
logger.log(` ${theme.dim(`Host: ${ups.snmp.host}:${ups.snmp.port}`)}`);
|
||||
|
||||
// Display groups if any
|
||||
if (ups.groups && ups.groups.length > 0) {
|
||||
@@ -301,17 +301,17 @@ WantedBy=multi-user.target
|
||||
const group = config.groups?.find((g: { id: string }) => g.id === groupId);
|
||||
return group ? group.name : groupId;
|
||||
});
|
||||
console.log(` ${theme.dim(`Groups: ${groupNames.join(', ')}`)}`);
|
||||
logger.log(` ${theme.dim(`Groups: ${groupNames.join(', ')}`)}`);
|
||||
}
|
||||
|
||||
console.log('');
|
||||
logger.log('');
|
||||
|
||||
} catch (error) {
|
||||
// Display error for this UPS
|
||||
console.log(` ${symbols.error} ${theme.highlight(ups.name)} - ${theme.error('Connection failed')}`);
|
||||
console.log(` ${theme.dim(error instanceof Error ? error.message : String(error))}`);
|
||||
console.log(` ${theme.dim(`Host: ${ups.snmp.host}:${ups.snmp.port}`)}`);
|
||||
console.log('');
|
||||
logger.log(` ${symbols.error} ${theme.highlight(ups.name)} - ${theme.error('Connection failed')}`);
|
||||
logger.log(` ${theme.dim(error instanceof Error ? error.message : String(error))}`);
|
||||
logger.log(` ${theme.dim(`Host: ${ups.snmp.host}:${ups.snmp.port}`)}`);
|
||||
logger.log('');
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user