canina/scripts/open-browsers.js
parsa aghaei 376fea356f
Some checks failed
Deploy Canina / deploy (push) Successful in 1m48s
E2E Playwright Tests / Run Full E2E & Security Suites (push) Failing after 6s
fix(dev): set local store port to 3300 and admin port to 3400 to avoid Windows port exclusions
2026-08-29 14:10:52 +03:30

49 lines
1.5 KiB
JavaScript

const { exec } = require('child_process');
const http = require('http');
const URLS = [
{ url: 'http://localhost:3300', name: 'Frontend Store' },
{ url: 'http://127.0.0.1:3400', name: 'Admin Panel' },
{ url: 'http://localhost:4400/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);
});