import { spawnSync } from "node:child_process";
import path from "node:path";
import { fileURLToPath } from "node:url";

const rootDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");

const result = spawnSync("bun", ["x", "vite", "build"], {
  cwd: rootDir,
  stdio: "inherit",
  env: {
    ...process.env,
    NODE_ENV: process.env.NODE_ENV || "production",
  },
});

if (result.status !== 0) {
  process.exit(result.status ?? 1);
}

const syncResult = spawnSync(
  "bun",
  [path.join(rootDir, "scripts/sync-electrobun-views.mjs")],
  {
    cwd: rootDir,
    stdio: "inherit",
    env: process.env,
  },
);

if (syncResult.status !== 0) {
  process.exit(syncResult.status ?? 1);
}

console.log(
  `build-renderer: output at ${path.join(rootDir, "dist/mainview")}`,
);
