import { resolve } from "node:path";

import {
  buildP0aRuntimeAssetPlan,
  defaultReplicationRoot,
  serializeRuntimeAssetManifest,
  writeRuntimeAssetManifest,
} from "./lib/runtime-assets.mjs";

function option(name) {
  const index = process.argv.indexOf(name);
  return index >= 0 ? process.argv[index + 1] : undefined;
}

const replicationRoot = resolve(option("--replication-root") ?? defaultReplicationRoot());
const outputPath = resolve(option("--output") ?? "config/runtime-assets-manifest.json");
const plan = await buildP0aRuntimeAssetPlan({ replicationRoot });
writeRuntimeAssetManifest(outputPath, plan.manifest);
process.stdout.write(`${JSON.stringify({
  counts: plan.manifest.counts,
  manifest_bytes: Buffer.byteLength(serializeRuntimeAssetManifest(plan.manifest)),
  status: "generated",
})}\n`);
