import { resolve } from "node:path";
import { assertKnownArgs, parseArgs, reportCliError, requiredArg } from "./cli.ts";
import { prepareReleaseBundle } from "./release-bundle.ts";

async function main(): Promise<void> {
  const args = parseArgs(process.argv.slice(2));
  assertKnownArgs(args, [
    "config", "out", "private-key", "trusted-public-key", "key-id", "schema-ref", "schema-dir",
  ]);
  const manifest = await prepareReleaseBundle({
    sourcePath: resolve(requiredArg(args, "config")),
    releaseDirectory: resolve(requiredArg(args, "out")),
    privateKeyPath: resolve(requiredArg(args, "private-key")),
    trustedPublicKeyPath: resolve(requiredArg(args, "trusted-public-key")),
    keyId: requiredArg(args, "key-id"),
    schemaRef: requiredArg(args, "schema-ref"),
    schemaDirectory: resolve(requiredArg(args, "schema-dir")),
  });
  process.stdout.write(
    `Bundle ${manifest.payload.releaseId} signé et vérifié (${manifest.payload.artifacts.length} artefact(s)).\n`,
  );
}

main().catch(reportCliError);
