import { spawnSync } from "node:child_process";

const buildId = createBuildId();
const npmCommand = process.platform === "win32" ? "npm.cmd" : "npm";
const env = {
  ...process.env,
  ORG_CONTEXT_CHART_BUILD_ID: buildId,
};

for (const script of [
  "build:org-context-chart:full",
  "build:org-context-chart:min",
]) {
  const result = spawnSync(npmCommand, ["run", script], {
    env,
    stdio: "inherit",
  });
  if (result.status !== 0) {
    process.exit(result.status ?? 1);
  }
}

function createBuildId() {
  const now = new Date();
  const year = String(now.getFullYear()).slice(-2);
  const month = String(now.getMonth() + 1).padStart(2, "0");
  const random = String(Math.floor(Math.random() * 10000)).padStart(4, "0");
  return `${year}${month}${random}`;
}
