#!/usr/bin/env node

/**
 * AggRoot 一键构建脚本
 * 按依赖顺序编译所有 workspace 包
 */

import { execSync } from "node:child_process";

const packages = [
  { name: "@aggroot/protocol", label: "Protocol (共享类型)" },
  { name: "@aggroot/server", label: "Server (网关服务)" },
  { name: "@aggroot/web", label: "Web (前端)" },
  { name: "aggroot", label: "AggRoot (主包)" },
];

function run(cmd) {
  execSync(cmd, { stdio: "inherit", shell: true });
}

function build(pkg) {
  const cmd = pkg.name === "aggroot"
    ? "npm run build"
    : `npm run build -w ${pkg.name}`;
  run(cmd);
}

// --- main ---
console.log("=== AggRoot 全量构建 ===\n");

for (const pkg of packages) {
  console.log(`>> 构建 ${pkg.label} ...`);
  try {
    build(pkg);
  } catch {
    console.error(`\n!! 构建 ${pkg.label} 失败，已中止`);
    process.exit(1);
  }
  console.log(`>> ${pkg.label} 完成\n`);
}

console.log("=== 全部构建完成 ===");
