import { findProcessesByCommandNameAndProjectDir, findRunningProcessesByProjectDir, type ProcessEntry } from './database/processTable.ts'; import { UsageError } from './errors.ts'; import { handleKillCommand } from './kill-command.ts'; import { startOneService } from './start/startOneService.ts'; interface RestartOptions { projectDir: string; commandNames: string[]; consoleOutputFormat: 'pretty' | 'json'; } export async function handleRestart(options: RestartOptions) { const { projectDir, consoleOutputFormat } = options; let { commandNames } = options; if (!projectDir) { throw new Error('handleRestart: projectDir is required'); } // If no command names provided, restart all running processes in the project if (commandNames.length === 0) { const runningProcesses = findRunningProcessesByProjectDir(projectDir); if (runningProcesses.length === 0) { throw new UsageError('No running processes found in this project to restart'); } // Deduplicate command names to avoid killing the same service multiple times commandNames = [...new Set(runningProcesses.map(p => p.command_name))]; } try { // First, fetch process info for all command names before killing const processInfoMap = new Map(); for (const commandName of commandNames) { const processes = findProcessesByCommandNameAndProjectDir(commandName, projectDir); processInfoMap.set(commandName, processes[0]); } // Kill all existing processes await handleKillCommand({ projectDir, commandNames }); // Then restart each service using stored shell/root if available for (const commandName of commandNames) { const runningProcess = processInfoMap.get(commandName); await startOneService({ projectDir, commandName, consoleOutputFormat, shell: runningProcess?.shell, root: runningProcess?.root, }); } } catch (error) { console.error(`Failed to restart: ${error.message}`); } }