69 lines
1.5 KiB
JavaScript
69 lines
1.5 KiB
JavaScript
const { spawn } = require("child_process");
|
|
const http = require("http");
|
|
const path = require("path");
|
|
const electronPath = require("electron");
|
|
const vitePath = path.join(path.dirname(require.resolve("vite/package.json")), "bin", "vite.js");
|
|
|
|
const port = process.env.VITE_PORT || "5173";
|
|
const devUrl = `http://127.0.0.1:${port}`;
|
|
|
|
function electronEnv(extra = {}) {
|
|
const env = { ...process.env, ...extra };
|
|
delete env.ELECTRON_RUN_AS_NODE;
|
|
return env;
|
|
}
|
|
|
|
const vite = spawn(process.execPath, [vitePath, "--host", "127.0.0.1", "--port", port], {
|
|
stdio: "inherit",
|
|
env: { ...process.env, BROWSER: "none" }
|
|
});
|
|
|
|
function waitForServer(url, retries = 90) {
|
|
return new Promise((resolve, reject) => {
|
|
const attempt = () => {
|
|
const request = http.get(url, (response) => {
|
|
response.resume();
|
|
resolve();
|
|
});
|
|
|
|
request.on("error", () => {
|
|
if (retries <= 0) {
|
|
reject(new Error(`Vite did not start at ${url}`));
|
|
return;
|
|
}
|
|
|
|
retries -= 1;
|
|
setTimeout(attempt, 300);
|
|
});
|
|
};
|
|
|
|
attempt();
|
|
});
|
|
}
|
|
|
|
let electron;
|
|
|
|
waitForServer(devUrl)
|
|
.then(() => {
|
|
electron = spawn(electronPath, ["."], {
|
|
stdio: "inherit",
|
|
env: electronEnv({ VITE_DEV_SERVER_URL: devUrl })
|
|
});
|
|
|
|
electron.on("exit", (code) => {
|
|
vite.kill();
|
|
process.exit(code ?? 0);
|
|
});
|
|
})
|
|
.catch((error) => {
|
|
console.error(error);
|
|
vite.kill();
|
|
process.exit(1);
|
|
});
|
|
|
|
process.on("SIGINT", () => {
|
|
if (electron) electron.kill();
|
|
vite.kill();
|
|
process.exit(0);
|
|
});
|