39 lines
1.2 KiB
JavaScript
39 lines
1.2 KiB
JavaScript
// Waits for Vite dev server, then launches Electron with ELECTRON_RUN_AS_NODE
|
|
// removed from environment (any value of that variable causes Electron to run
|
|
// as plain Node.js instead of a GUI app).
|
|
const { spawn } = require('child_process');
|
|
const http = require('http');
|
|
const electronPath = require('electron');
|
|
|
|
const VITE_URL = 'http://localhost:5173';
|
|
const POLL_INTERVAL = 500;
|
|
const MAX_WAIT_MS = 60000;
|
|
|
|
function waitForVite(url, timeout) {
|
|
return new Promise((resolve, reject) => {
|
|
const deadline = Date.now() + timeout;
|
|
const check = () => {
|
|
http.get(url, (res) => {
|
|
res.resume();
|
|
resolve();
|
|
}).on('error', () => {
|
|
if (Date.now() >= deadline) return reject(new Error('Timed out waiting for ' + url));
|
|
setTimeout(check, POLL_INTERVAL);
|
|
});
|
|
};
|
|
check();
|
|
});
|
|
}
|
|
|
|
console.log('Waiting for Vite at', VITE_URL, '...');
|
|
waitForVite(VITE_URL, MAX_WAIT_MS).then(() => {
|
|
console.log('Vite ready — starting Electron');
|
|
const env = { ...process.env };
|
|
delete env.ELECTRON_RUN_AS_NODE;
|
|
const child = spawn(electronPath, ['.'], { stdio: 'inherit', env });
|
|
child.on('close', code => process.exit(code ?? 0));
|
|
}).catch(err => {
|
|
console.error(err.message);
|
|
process.exit(1);
|
|
});
|