60 lines
1.6 KiB
TypeScript
60 lines
1.6 KiB
TypeScript
import { config as loadEnv } from "dotenv"
|
|
import { ConvexHttpClient } from "convex/browser"
|
|
import { makeFunctionReference } from "convex/server"
|
|
import { resolveManualsTenantDomain } from "../lib/manuals-tenant"
|
|
|
|
loadEnv({ path: ".env.local" })
|
|
loadEnv({ path: ".env.staging", override: false })
|
|
|
|
const BACKFILL_MUTATION = makeFunctionReference<"mutation">(
|
|
"manuals:backfillTenantVisibility"
|
|
)
|
|
|
|
function parseArgs(argv: string[]) {
|
|
const domainFlagIndex = argv.indexOf("--domain")
|
|
const domain =
|
|
domainFlagIndex >= 0 ? (argv[domainFlagIndex + 1] || "").trim() : ""
|
|
|
|
return {
|
|
domain,
|
|
dryRun: argv.includes("--dry-run"),
|
|
}
|
|
}
|
|
|
|
function readConvexUrl() {
|
|
const convexUrl = process.env.NEXT_PUBLIC_CONVEX_URL || process.env.CONVEX_URL
|
|
if (!convexUrl) {
|
|
throw new Error(
|
|
"NEXT_PUBLIC_CONVEX_URL (or CONVEX_URL) is required for Convex backfill."
|
|
)
|
|
}
|
|
|
|
return convexUrl
|
|
}
|
|
|
|
async function main() {
|
|
const args = parseArgs(process.argv.slice(2))
|
|
const domain = resolveManualsTenantDomain({
|
|
envTenantDomain: args.domain || process.env.MANUALS_TENANT_DOMAIN,
|
|
envSiteDomain: process.env.NEXT_PUBLIC_SITE_DOMAIN,
|
|
})
|
|
|
|
if (!domain) {
|
|
throw new Error(
|
|
"Could not resolve tenant domain. Pass --domain or set MANUALS_TENANT_DOMAIN / NEXT_PUBLIC_SITE_DOMAIN."
|
|
)
|
|
}
|
|
|
|
const convex = new ConvexHttpClient(readConvexUrl())
|
|
const result = await convex.mutation(BACKFILL_MUTATION, {
|
|
domain,
|
|
dryRun: args.dryRun,
|
|
})
|
|
|
|
console.log("[manuals-tenant-backfill] result", result)
|
|
}
|
|
|
|
main().catch((error) => {
|
|
console.error("[manuals-tenant-backfill] failed", error)
|
|
process.exit(1)
|
|
})
|