Files
Palash Debnath bc6d564cfc fix(dev): spawn the real Bun so npm-installed Bun works on Windows (#4603)
* fix(dev): spawn the real Bun so npm-installed Bun works on Windows

bunCommand() prefers the running Bun, then npm_execpath, then the executable behind a Windows npm shim on PATH. A missing Bun gives an install hint from every script that spawns it, and a test guards against bare bun/bunx spawns. Builds on the report and first fix in #4525.

* Add changelog fragment for the Bun spawn fix
2026-12-02 20:25:13 +00:00

53 lines
1.7 KiB
JavaScript

import { spawnSync } from "node:child_process";
import { dirname, resolve } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import { bunCommand, explainBunError } from "../electron/scripts/bun-command.mjs";
import { assertSupportedNode } from "../electron/scripts/node-version.mjs";
const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
export function needsLocalBackendSetup({
env = process.env,
platform = process.platform,
arch = process.arch,
} = {}) {
if (String(env.OMNIVOICE_BACKEND_CMD ?? "").trim()) return false;
if (env.VOICESTUDIO_SKIP_BACKEND === "1") return false;
return platform !== "darwin" || arch !== "x64";
}
/** Validate Node before installing dependencies and preparing a local backend. */
export function prepareDev({
nodeVersion = process.versions.node,
env = process.env,
platform = process.platform,
arch = process.arch,
run = spawnSync,
log = console.log,
bun = bunCommand({ env, platform }),
} = {}) {
assertSupportedNode(nodeVersion);
const commands = [["install"]];
if (needsLocalBackendSetup({ env, platform, arch })) {
commands.push(["run", "setup:api"]);
} else {
log("[dev] Skipping local API setup; Electron will use the configured external backend.");
}
for (const args of commands) {
const result = run(bun, args, { cwd: root, env, stdio: "inherit" });
if (result.error) throw explainBunError(result.error);
if (result.status !== 0) return result.status ?? 1;
}
return 0;
}
if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) {
try {
process.exitCode = prepareDev();
} catch (error) {
console.error(`[dev] ${error.message}`);
process.exitCode = 1;
}
}