|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +// Generates wrangler.dev.jsonc files for the dev-router and workshop-backend |
| 4 | +// workers (with dynamically-discovered gatekeeper service bindings), then |
| 5 | +// launches `wrangler dev` with all discovered workers. |
| 6 | +// |
| 7 | +// Flags: |
| 8 | +// --use-workers-ai-binding Include the Workers AI binding in |
| 9 | +// workshop-backend (requires Cloudflare login). |
| 10 | + |
| 11 | +import { readFileSync, writeFileSync, readdirSync, statSync } from "node:fs"; |
| 12 | +import { execFileSync } from "node:child_process"; |
| 13 | +import { join, dirname } from "node:path"; |
| 14 | +import { fileURLToPath } from "node:url"; |
| 15 | +import { parse } from "jsonc-parser"; |
| 16 | + |
| 17 | +const ROOT = dirname(fileURLToPath(import.meta.url)); |
| 18 | +const PACKAGES_DIR = join(ROOT, "packages"); |
| 19 | + |
| 20 | +const useWorkersAi = process.argv.includes("--use-workers-ai-binding"); |
| 21 | + |
| 22 | +// --------------------------------------------------------------------------- |
| 23 | +// Discover gatekeeper packages. |
| 24 | +// --------------------------------------------------------------------------- |
| 25 | +function findGatekeepers(parentDir) { |
| 26 | + try { |
| 27 | + return readdirSync(parentDir) |
| 28 | + .filter(name => name.startsWith("gatekeeper-")) |
| 29 | + .filter(name => { |
| 30 | + try { |
| 31 | + return statSync(join(parentDir, name, "wrangler.jsonc")).isFile(); |
| 32 | + } catch { |
| 33 | + return false; |
| 34 | + } |
| 35 | + }); |
| 36 | + } catch { |
| 37 | + return []; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +const gatekeepers = findGatekeepers(PACKAGES_DIR); |
| 42 | + |
| 43 | +// Helper: "gatekeeper-github" -> "GATEKEEPER_GITHUB" |
| 44 | +function bindingName(pkg) { |
| 45 | + return pkg.toUpperCase().replaceAll("-", "_"); |
| 46 | +} |
| 47 | + |
| 48 | +// --------------------------------------------------------------------------- |
| 49 | +// Generate wrangler.dev.jsonc (dev-router with gatekeeper service bindings). |
| 50 | +// --------------------------------------------------------------------------- |
| 51 | +{ |
| 52 | + const srcPath = join(ROOT, "wrangler.jsonc"); |
| 53 | + const config = parse(readFileSync(srcPath, "utf8")); |
| 54 | + |
| 55 | + config.services = config.services || []; |
| 56 | + for (const gk of gatekeepers) { |
| 57 | + config.services.push({ binding: bindingName(gk), service: gk }); |
| 58 | + } |
| 59 | + |
| 60 | + const outPath = join(ROOT, "wrangler.dev.jsonc"); |
| 61 | + writeFileSync(outPath, JSON.stringify(config, null, 2) + "\n"); |
| 62 | + console.log(`generated: ${outPath}`); |
| 63 | +} |
| 64 | + |
| 65 | +// --------------------------------------------------------------------------- |
| 66 | +// Generate packages/workshop-backend/wrangler.dev.jsonc (with gatekeeper |
| 67 | +// service bindings using the GatekeeperVendor entrypoint). |
| 68 | +// --------------------------------------------------------------------------- |
| 69 | +{ |
| 70 | + const srcPath = join(ROOT, "packages", "workshop-backend", "wrangler.jsonc"); |
| 71 | + const config = parse(readFileSync(srcPath, "utf8")); |
| 72 | + |
| 73 | + config.services = config.services || []; |
| 74 | + |
| 75 | + // For local testing, create an account named "admin" to test admin features. |
| 76 | + config.vars = config.vars || {}; |
| 77 | + config.vars.ADMINS = ["admin"]; |
| 78 | + |
| 79 | + for (const gk of gatekeepers) { |
| 80 | + config.services.push({ |
| 81 | + binding: bindingName(gk), |
| 82 | + service: gk, |
| 83 | + entrypoint: "GatekeeperVendor", |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + if (useWorkersAi) { |
| 88 | + config.ai = { binding: "WORKERS_AI" }; |
| 89 | + } |
| 90 | + |
| 91 | + const outPath = join(ROOT, "packages", "workshop-backend", "wrangler.dev.jsonc"); |
| 92 | + writeFileSync(outPath, JSON.stringify(config, null, 2) + "\n"); |
| 93 | + console.log(`generated: ${outPath}`); |
| 94 | +} |
| 95 | + |
| 96 | +// --------------------------------------------------------------------------- |
| 97 | +// Build the wrangler dev command and exec it. |
| 98 | +// --------------------------------------------------------------------------- |
| 99 | + |
| 100 | +const configs = [ |
| 101 | + "wrangler.dev.jsonc", |
| 102 | + join("packages", "workshop-backend", "wrangler.dev.jsonc"), |
| 103 | + ...gatekeepers.map(gk => join("packages", gk, "wrangler.jsonc")), |
| 104 | +]; |
| 105 | + |
| 106 | +const args = configs.flatMap(c => ["-c", c]); |
| 107 | +console.log(`\nStarting: wrangler dev ${args.join(" ")}\n`); |
| 108 | + |
| 109 | +try { |
| 110 | + execFileSync("pnpm", ["exec", "wrangler", "dev", ...args], |
| 111 | + { stdio: "inherit", cwd: ROOT }); |
| 112 | +} catch (e) { |
| 113 | + // wrangler was killed or exited with an error; the output was already shown |
| 114 | + // via stdio: "inherit", so just propagate the exit code. |
| 115 | + process.exit(e.status ?? 1); |
| 116 | +} |
0 commit comments