| 1 | #!/usr/bin/env node |
| 2 | /** |
| 3 | * check-kv-id.mjs — pre-deploy check that wrangler.jsonc has |
| 4 | * real KV namespace IDs, not placeholders. |
| 5 | * |
| 6 | * Prints the exact `wrangler kv namespace create` command to run |
| 7 | * when a placeholder is found, then exits non-zero. |
| 8 | */ |
| 9 | import { readFileSync } from "node:fs"; |
| 10 | import { join, dirname } from "node:path"; |
| 11 | import { fileURLToPath } from "node:url"; |
| 12 | |
| 13 | const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 14 | const cfgPath = join(__dirname, "..", "wrangler.jsonc"); |
| 15 | const raw = readFileSync(cfgPath, "utf-8"); |
| 16 | |
| 17 | // Parse JSONC (strip comments, trailing commas). |
| 18 | // Use a two-pass approach to avoid mangling URLs: first strip |
| 19 | // line comments that look like comments (preceded by whitespace |
| 20 | // or comma, not part of ://), then strip block comments. |
| 21 | const stripped = raw |
| 22 | .replace(/(^|[,\s])\/\/[^\n]*/gm, "$1") // line comments (skips :// in URLs) |
| 23 | .replace(/\/\*[\s\S]*?\*\//g, "") // block comments |
| 24 | .replace(/,\s*}/g, "}") // trailing commas |
| 25 | .replace(/,\s*]/g, "]"); |
| 26 | const cfg = JSON.parse(stripped); |
| 27 | |
| 28 | const nss = cfg.kv_namespaces; |
| 29 | if (!Array.isArray(nss) || nss.length === 0) { |
| 30 | console.log("No KV namespaces defined — skipping check."); |
| 31 | process.exit(0); |
| 32 | } |
| 33 | |
| 34 | let dirty = false; |
| 35 | for (const ns of nss) { |
| 36 | if (ns.id === "REPLACE_WITH_KV_ID") { |
| 37 | dirty = true; |
| 38 | console.error(""); |
| 39 | console.error("❌ KV namespace %s has placeholder id.", ns.binding); |
| 40 | console.error(" Run this command and paste the returned id into wrangler.jsonc:"); |
| 41 | console.error(""); |
| 42 | console.error(" npx wrangler kv namespace create %s", ns.binding); |
| 43 | console.error(""); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | if (dirty) { |
| 48 | process.exit(1); |
| 49 | } |
| 50 | |
| 51 | console.log("✅ All KV namespace IDs are set."); |
| 52 |