NextNodeTemplate/frontend/scripts/devWithPort.js
DosAi ea61c5f493
Some checks failed
CI/CD / lint-and-build (push) Has been cancelled
feat: Add automatic free port finder for frontend and backend
2025-11-02 17:39:38 +03:00

43 lines
1.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Скрипт для запуска Next.js dev с автоматическим поиском свободного порта
const { spawn } = require('child_process');
const { findFreePort } = require('./findPort');
const DEFAULT_PORT = 3000;
(async () => {
try {
const port = await findFreePort(DEFAULT_PORT);
if (port !== DEFAULT_PORT) {
console.log(`⚠️ Порт ${DEFAULT_PORT} занят, используем порт ${port}`);
} else {
console.log(`✅ Порт ${port} свободен`);
}
// Запускаем Next.js dev с найденным портом
const nextDev = spawn('npx', ['next', 'dev', '--turbopack', '-p', String(port)], {
stdio: 'inherit',
shell: true,
});
nextDev.on('error', (error) => {
console.error('Ошибка запуска Next.js:', error);
process.exit(1);
});
nextDev.on('exit', (code) => {
process.exit(code || 0);
});
// Обработка Ctrl+C
process.on('SIGINT', () => {
nextDev.kill('SIGINT');
});
} catch (error) {
console.error('Ошибка поиска порта:', error.message);
process.exit(1);
}
})();