import fs from "node:fs/promises";
import path from "node:path";
import { execFileSync } from "node:child_process";
import { fileURLToPath } from "node:url";

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const rootDir = path.resolve(__dirname, "..");
const manifest = JSON.parse(await fs.readFile(path.join(rootDir, "manifest.json"), "utf8"));
const version = manifest.version;
const distZipPath = path.join(rootDir, "dist", `Sensa_basic_v${version}.zip`);
const siteDownloadsDir = path.join(rootDir, "site", "downloads");
const siteZipPath = path.join(siteDownloadsDir, `Sensa_basic_v${version}.zip`);

await fs.mkdir(siteDownloadsDir, { recursive: true });
await fs.copyFile(distZipPath, siteZipPath);

execFileSync("npx", ["wrangler@latest", "pages", "deploy", "site", "--project-name", "sensa", "--commit-dirty=true"], {
  cwd: rootDir,
  stdio: "inherit"
});
