111 lines
3.1 KiB
JavaScript
111 lines
3.1 KiB
JavaScript
const { app, BrowserWindow } = require('electron');
|
||
const path = require('path');
|
||
const { spawn, exec } = require('child_process');
|
||
|
||
let mainWindow;
|
||
let serverProcess;
|
||
|
||
function createWindow() {
|
||
mainWindow = new BrowserWindow({
|
||
width: 1200,
|
||
height: 800,
|
||
autoHideMenuBar: true,
|
||
webPreferences: {
|
||
nodeIntegration: true,
|
||
contextIsolation: false
|
||
}
|
||
});
|
||
|
||
mainWindow.setMenu(null);
|
||
|
||
// --- ОБРАБОТЧИК ЗАКРЫТИЯ ОКНА (ВНУТРИ ФУНКЦИИ) ---
|
||
mainWindow.on('close', () => {
|
||
console.log("⚠️ Окно закрывается.");
|
||
|
||
// 1. Пытаемся убить запущенный процесс
|
||
if (serverProcess) {
|
||
serverProcess.kill('SIGTERM');
|
||
}
|
||
|
||
// 2. ЗАПУСКАЕМ ЧИСТКУ ПОРТА
|
||
setTimeout(() => {
|
||
killPort3000();
|
||
}, 200);
|
||
});
|
||
|
||
startServer();
|
||
|
||
setTimeout(() => {
|
||
mainWindow.loadURL('http://localhost:3000');
|
||
}, 1500);
|
||
}
|
||
|
||
function startServer() {
|
||
const appPath = __dirname;
|
||
console.log("Запуск сервера из:", appPath);
|
||
|
||
serverProcess = spawn('node', ['server/server.js'], {
|
||
cwd: appPath,
|
||
shell: true
|
||
});
|
||
|
||
serverProcess.stdout.on('data', (data) => {
|
||
console.log(`[Сервер]: ${data}`);
|
||
});
|
||
|
||
serverProcess.stderr.on('data', (data) => {
|
||
console.error(`[Ошибка сервера]: ${data}`);
|
||
});
|
||
|
||
serverProcess.on('close', (code) => {
|
||
console.log(`Сервер завершил работу с кодом ${code}`);
|
||
});
|
||
}
|
||
|
||
// --- ФУНКЦИЯ: Убийство всех процессов на порту 3000 ---
|
||
function killPort3000() {
|
||
console.log("🛑 Проверка и очистка порта 3000...");
|
||
|
||
exec('netstat -ano | findstr :3000', (err, stdout, stderr) => {
|
||
if (err || !stdout) {
|
||
console.log("Порт 3000 свободен.");
|
||
return;
|
||
}
|
||
|
||
const lines = stdout.split('\n');
|
||
const pidsToKill = new Set();
|
||
|
||
lines.forEach(line => {
|
||
const parts = line.trim().split(/\s+/);
|
||
if (parts.length > 4) {
|
||
const pid = parts[parts.length - 1];
|
||
if (pid && line.includes('LISTENING')) {
|
||
pidsToKill.add(pid);
|
||
}
|
||
}
|
||
});
|
||
|
||
if (pidsToKill.size > 0) {
|
||
console.log(`🔪 Найдено процессов: ${Array.from(pidsToKill).join(', ')}`);
|
||
|
||
const killCommand = `taskkill /F /PID ${Array.from(pidsToKill).join(' /PID ')}`;
|
||
|
||
exec(killCommand, (killErr) => {
|
||
if (killErr) {
|
||
console.error("Не удалось убить процессы:", killErr);
|
||
} else {
|
||
console.log("✅ Процессы на порту 3000 успешно остановлены.");
|
||
}
|
||
});
|
||
}
|
||
});
|
||
|
||
}
|
||
|
||
app.on('window-all-closed', () => {
|
||
if (process.platform !== 'darwin') {
|
||
app.quit();
|
||
}
|
||
});
|
||
|
||
app.whenReady().then(createWindow); |