// SPDX-License-Identifier: LicenseRef-ConusAI-Template-1.0 // The linked service provisions the self-hosted backend; this runner installs only the app's // already-scoped platform service credentials in that backend, then deploys the generated app. // The temporary env file is mode 0600 and removed on both success and failure. import { spawnSync } from "node:child_process" import { mkdtempSync, rmSync, writeFileSync } from "node:fs" import { tmpdir } from "node:os" import { join } from "node:path" import { fileURLToPath } from "node:url" const required = (name) => { const value = process.env[name]?.trim() if (!value || /[\r\n]/.test(value)) throw new Error(`Convex deploy is missing a valid ${name}`) return value } const root = fileURLToPath(new URL("../../", import.meta.url)) const cli = join(root, "node_modules/convex/bin/main.js") const variables = ["CONUSAI_API_URL", "CONUSAI_API_TOKEN", "CONUSAI_PROJECT_ID", "CONUSAI_ORG_SLUG"] const optional = ["AI_MODEL", "DOCUMENTS_EXCLUDED_PAGE_CLASSES"] // The support channel's public key, for the support module's effect actions (convex/lib/effects.ts). optional.push("CONUSAI_SUPPORT_CHANNEL") // kit:support // kit:convex-better-auth:begin -- the account actions end a deleted person's Better Auth account // (convex/lib/effects.ts): the Better Auth deployment's URL, its admin key and the identity namespace. if (process.env.AUTH_PROVIDER?.trim() === "convex-better-auth") { variables.push("AUTH_PROVIDER", "AUTH_IDENTITY_NAMESPACE", "BETTER_AUTH_CONVEX_URL", "BETTER_AUTH_CONVEX_ADMIN_KEY") } // kit:convex-better-auth:end for (const name of optional) { const value = process.env[name]?.trim() if (value) { if (/[\r\n]/.test(value)) throw new Error(`Convex deploy has an invalid ${name}`) variables.push(name) } } const contents = `${variables.map((name) => `${name}=${required(name)}`).join("\n")}\n` const directory = mkdtempSync(join(tmpdir(), "conusai-convex-env-")) const envFile = join(directory, ".env") try { writeFileSync(envFile, contents, { mode: 0o600, flag: "wx" }) const seeded = spawnSync(process.execPath, [cli, "env", "set", "--from-file", envFile], { cwd: root, env: process.env, stdio: "ignore", }) if (seeded.error || seeded.status !== 0) { throw new Error("Convex platform service environment sync failed") } const deployed = spawnSync(process.execPath, [cli, "deploy", "--typecheck", "disable"], { cwd: root, env: process.env, stdio: "inherit", }) if (deployed.error) throw deployed.error if (deployed.status !== 0) process.exitCode = deployed.status ?? 1 // kit:convex-better-auth:begin -- Better Auth's functions go to their own Convex deployment. if (deployed.status === 0 && process.env.AUTH_PROVIDER?.trim() === "convex-better-auth") { const betterAuth = spawnSync(process.execPath, [join(root, "better-auth/deploy.mjs")], { env: process.env, stdio: "inherit", }) if (betterAuth.error || betterAuth.status !== 0) process.exitCode = 1 } // kit:convex-better-auth:end } finally { rmSync(directory, { recursive: true, force: true }) }