feat: add scripts/open-browsers.js to automatically open services on dev startup

This commit is contained in:
پارسا آقایی 2026-07-10 17:40:35 +03:30
parent 1c07efcc91
commit 6db28f6231
2 changed files with 49 additions and 1 deletions

View File

@ -7,7 +7,7 @@
"dev:backend": "cd backend && npm run start:dev",
"dev:frontend": "cd frontend/application && npm run dev",
"dev:admin": "cd frontend/admin-panel && npm run dev",
"dev": "concurrently -c \"blue,magenta,green\" -n \"backend,store,admin\" \"npm run dev:backend\" \"npm run dev:frontend\" \"npm run dev:admin\"",
"dev": "concurrently -c \"blue,magenta,green,yellow\" -n \"backend,store,admin,open\" \"npm run dev:backend\" \"npm run dev:frontend\" \"npm run dev:admin\" \"node scripts/open-browsers.js\"",
"build:backend": "cd backend && npm run build",
"build:frontend": "cd frontend/application && npm run build",
"build:admin": "cd frontend/admin-panel && npm run build",

48
scripts/open-browsers.js Normal file
View File

@ -0,0 +1,48 @@
const { exec } = require('child_process');
const http = require('http');
const URLS = [
{ url: 'http://localhost:4000', name: 'Frontend Store' },
{ url: 'http://127.0.0.1:5050', name: 'Admin Panel' },
{ url: 'http://localhost:4001/api/docs', name: 'Backend API Docs' }
];
function openUrl(url) {
// On Windows, the start command needs to handle query parameters or URLs correctly.
// Using start "" "url" is the safest way to open urls in Windows Command Prompt/PowerShell.
const startCmd = process.platform === 'win32'
? 'start ""'
: process.platform === 'darwin'
? 'open'
: 'xdg-open';
exec(`${startCmd} "${url}"`, (err) => {
if (err) {
console.error(`[open-browsers] Failed to open ${url}:`, err);
}
});
}
function checkAndOpen(item) {
const req = http.get(item.url, (res) => {
// If we get a response (any status code like 200, 302, 404), the server is listening!
console.log(`\x1b[33m[open-browsers] ${item.name} is ready! Opening in browser...\x1b[0m`);
openUrl(item.url);
});
req.on('error', () => {
// Connection refused or not ready, retry in 1.5 seconds
setTimeout(() => checkAndOpen(item), 1500);
});
// Set a timeout for the request itself to prevent hanging
req.setTimeout(5000, () => {
req.destroy();
});
}
console.log('\x1b[33m[open-browsers] Waiting for local services to start...\x1b[0m');
URLS.forEach((item) => {
// Delay the initial check slightly to let servers initialize
setTimeout(() => checkAndOpen(item), 1000);
});