import { loadConfig } from './config.mjs'; import { openStore } from './store.mjs'; import { parseInstant, validateMaintenance, maintenancePhase } from './maintenance.mjs'; const config = loadConfig(); const [command, ...args] = process.argv.slice(2); const store = openStore(config.dbPath); try { if (command === 'list') console.log(JSON.stringify(store.maintenances().map(item => ({ ...item, phase: maintenancePhase(item) })), null, 2)); else if (['cancel', 'finish'].includes(command) && /^\d+$/.test(args[0] || '')) { if (!store.endMaintenance(args[0], command)) throw new Error('Maintenance window not found or already closed'); console.log(`Maintenance ${args[0]}: ${command}`); } else if (command === 'schedule') { const options = Object.fromEntries(args.map(arg => { const at = arg.indexOf('='); if (at < 1) throw new Error('Use key=value arguments'); return [arg.slice(0, at), arg.slice(at + 1)]; })); const input = validateMaintenance({ titleRu: options['title-ru'], titleEn: options['title-en'], messageRu: options['message-ru'], messageEn: options['message-en'], serviceIds: options.services ? options.services.split(',') : [], startsAt: parseInstant(options.starts), endsAt: parseInstant(options.ends), }, config.services.map(service => service.id)); console.log(`Scheduled maintenance ${store.createMaintenance(input)}`); } else throw new Error('Usage: npm run maintenance -- list | cancel ID | finish ID | schedule services=api,pwa starts=ISO ends=ISO "title-ru=..." "title-en=..." "message-ru=..." "message-en=..."'); } catch (error) { console.error(error.message); process.exitCode = 1; } finally { store.close(); }