#!/usr/bin/env node
/**
* Clean restart script for Robotics webapp.
* Ensures no file watchers or lingering processes interfere with restart.
*/
const { execSync, spawn } = require('child_process');
const path = require('path');
function killProcessesOnPorts(...ports) {
try {
// Find processes on ports
const netstat = execSync('netstat -ano', { encoding: 'utf8' });
for (const port of ports) {
const lines = netstat.split('\n');
for (const line of lines) {
if (line.includes(`:${port} `) && line.includes('LISTENING')) {
const parts = line.trim().split(/\s+/);
if (parts.length >= 5) {
const pid = parts[parts.length - 1];
try {
execSync(`taskkill /PID ${pid} /F`, { stdio: 'ignore' });
console.log(`Killed process ${pid} on port ${port}`);
} catch (e) {
console.log(`Failed to kill process ${pid}: ${e.message}`);
}
}
}
}
}
} catch (e) {
console.log(`Error checking processes: ${e.message}`);
}
}
function killViteProcesses() {
try {
// Kill any Vite processes
execSync('taskkill /F /IM vite.exe', { stdio: 'ignore' });
execSync('taskkill /F /FI "WINDOWTITLE eq vite*"', { stdio: 'ignore' });
execSync('taskkill /F /FI "IMAGENAME eq node.exe" /FI "WINDOWTITLE eq *vite*"', { stdio: 'ignore' });
console.log('Killed any running Vite processes');
} catch (e) {
// Ignore errors if no processes found
}
}
function startWebapp() {
console.log('Starting Robotics webapp...');
const projectRoot = path.resolve(__dirname, '..');
const command = 'npm';
const args = ['run', 'dev'];
const child = spawn(command, args, {
cwd: projectRoot,
stdio: 'inherit',
shell: true,
env: {
...process.env,
// Ensure no hot reload
VITE_HMR: 'false',
CHOKIDAR_USEPOLLING: 'false'
}
});
// Handle process termination
process.on('SIGINT', () => {
console.log('Stopping webapp...');
child.kill('SIGINT');
process.exit(0);
});
process.on('SIGTERM', () => {
console.log('Stopping webapp...');
child.kill('SIGTERM');
process.exit(0);
});
child.on('error', (error) => {
console.error(`Failed to start webapp: ${error.message}`);
process.exit(1);
});
}
function main() {
console.log('🧹 Performing clean restart of Robotics webapp...');
// Kill processes on webapp ports
console.log('🔪 Killing processes on webapp ports...');
killProcessesOnPorts(5173);
// Kill Vite processes
console.log('🔪 Killing Vite processes...');
killViteProcesses();
// Wait a moment for cleanup
console.log('⏳ Waiting for cleanup...');
setTimeout(() => {
// Start the webapp
startWebapp();
}, 2000);
}
main();