import { cpSync, mkdirSync, rmSync } from "node:fs";
import { join } from "node:path";
import { spawnSync } from "node:child_process";

const sites = [
  "research",
  "vibes",
  "puzzles",
  "reflections",
  "poetry",
  "reviews",
  "art",
];

const rootDist = "dist";

rmSync(rootDist, { force: true, recursive: true });
mkdirSync(rootDist, { recursive: true });

for (const site of sites) {
  console.log(`\nBuilding ${site} -> ${rootDist}/${site}`);

  const result = spawnSync("npm", ["run", "build", "--prefix", `sites/${site}`], {
    stdio: "inherit",
  });

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

  cpSync(join("sites", site, "dist"), join(rootDist, site), {
    recursive: true,
  });
}
