import type { agent } from "@crosscode/contracts"; /** * The `crosscode` arguments for a job (AGENT-1.0-PLAN ยง4): built from the checked arguments only, * as an array passed straight to the process (never through a shell). Jobs the agent handles * itself (update-agent, check-ports) have none. * * `--config ` is added by the runner when the job brings config; `--json` always. */ export function crosscodeArgs(job: agent.AgentJob, opts: { exportFile?: string } = {}): string[] | null { switch (job.type) { case "install": { const a = job.args; const product = a.products[0]; if (a.products.length !== 1 || (product !== "events" && product !== "accounts")) throw new Error("This agent installs one product at a time: Events or Accounts."); const domain = a.domains[product]; const ports = a.ports[product]; if (!domain || !ports) throw new Error(`An install of ${product} needs its domain and ports.`); // The church's address: the product's domain without "." (crosscode keeps it in // church.env ADDRESS for the products added later; the domain itself goes in ---domain). const address = domain.startsWith(`${product}.`) ? domain.slice(product.length + 1) : domain; const out = ["install", a.church, "--products", product, "--domain", address, `--${product}-domain`, domain, "--api-port", String(ports.api), "--web-port", String(ports.web), "--frp"]; const v = a.versions?.[product]; if (v) out.push("--version", v); return out; } case "add-product": { // A product next to the church's others (phase 3: Accounts next to Events), behind frp. const a = job.args; const out = ["add", a.church, a.product, "--domain", a.domain, "--api-port", String(a.ports.api), "--web-port", String(a.ports.web), "--frp"]; if (a.version) out.push("--version", a.version); return out; } case "deploy": { const out = ["deploy", job.args.church, ...job.args.products]; for (const v of job.args.versions ?? []) out.push("--version", v); if (job.args.check) out.push("--check"); return out; } case "adopt": { const out = ["adopt", job.args.church, "--events-dir", job.args.eventsDir]; if (job.args.version) out.push("--version", job.args.version); if (job.args.address) out.push("--address", job.args.address); return out; } case "adopt-undo": return ["adopt", job.args.church, "--undo"]; case "restart": return ["restart", job.args.church, ...(job.args.product ? [job.args.product] : [])]; case "seed-demo": return ["demo", job.args.church, "--profile", job.args.profile, ...(job.args.adminEmail ? ["--admin-email", job.args.adminEmail] : [])]; case "export": if (!opts.exportFile) throw new Error("export needs a file to write"); return ["export", job.args.church, "--out", opts.exportFile]; case "uninstall": return ["uninstall", job.args.church, "--confirm", job.args.church]; case "purge": return ["purge", job.args.church, "--confirm", job.args.church]; case "status": return ["status", ...(job.args.church ? [job.args.church] : []), "--no-fetch"]; case "update-agent": case "check-ports": return null; } }