49 lines
1.5 KiB
JavaScript
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);
|
|
});
|