| 1 | #!/usr/bin/env node |
| 2 | // Render apps/diffinery/wrangler.deploy.jsonc from wrangler.jsonc plus values |
| 3 | // discovered by scripts/bootstrap.sh (real D1 id, Access team domain and AUD). |
| 4 | // |
| 5 | // node scripts/render-config.mjs --d1 <uuid> [--team <x.cloudflareaccess.com>] [--aud <tag>] |
| 6 | import { readFileSync, writeFileSync } from "node:fs"; |
| 7 | import { dirname, join } from "node:path"; |
| 8 | import { fileURLToPath } from "node:url"; |
| 9 | import { parseArgs } from "node:util"; |
| 10 | |
| 11 | const { values } = parseArgs({ options: { d1: { type: "string" }, team: { type: "string" }, aud: { type: "string" }, "public-url": { type: "string" } } }); |
| 12 | if (!values.d1) throw new Error("--d1 <database id> is required"); |
| 13 | |
| 14 | /** Strip // and /* *\/ comments and trailing commas from JSONC, leaving strings intact. */ |
| 15 | export function parseJsonc(text) { |
| 16 | let out = ""; |
| 17 | for (let i = 0; i < text.length; i++) { |
| 18 | const ch = text[i]; |
| 19 | if (ch === '"') { |
| 20 | let j = i + 1; |
| 21 | while (j < text.length && text[j] !== '"') j += text[j] === "\\" ? 2 : 1; |
| 22 | out += text.slice(i, j + 1); |
| 23 | i = j; |
| 24 | } else if (ch === "/" && text[i + 1] === "/") { |
| 25 | while (i < text.length && text[i] !== "\n") i++; |
| 26 | out += "\n"; |
| 27 | } else if (ch === "/" && text[i + 1] === "*") { |
| 28 | i = text.indexOf("*/", i + 2) + 1; |
| 29 | } else out += ch; |
| 30 | } |
| 31 | return JSON.parse(out.replace(/,(\s*[}\]])/g, "$1")); |
| 32 | } |
| 33 | |
| 34 | const appDir = join(dirname(fileURLToPath(import.meta.url)), "..", "apps", "diffinery"); |
| 35 | const cfg = parseJsonc(readFileSync(join(appDir, "wrangler.jsonc"), "utf8")); |
| 36 | delete cfg.env; |
| 37 | delete cfg.$schema; |
| 38 | cfg.d1_databases[0].database_id = values.d1; |
| 39 | if (values.team) cfg.vars.ACCESS_TEAM_DOMAIN = values.team; |
| 40 | if (values.aud) cfg.vars.ACCESS_AUD = values.aud; |
| 41 | if (values["public-url"]) cfg.vars.PUBLIC_URL = values["public-url"]; |
| 42 | writeFileSync(join(appDir, "wrangler.deploy.jsonc"), `// Generated by scripts/render-config.mjs; do not edit.\n${JSON.stringify(cfg, null, 2)}\n`); |
| 43 | console.log(`wrote ${join(appDir, "wrangler.deploy.jsonc")}`); |