/** * The development server, read from config/dev-server.json: the one place * that names it (the Worker's dev deploy, every app's dev build and these * scripts read it). `host` is the dev Worker's host; `fakeGoogleHost` the * stand-in Google the dev Worker's calendar sign-in reaches. */ import { readFileSync } from "node:fs"; import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; export const DEV_SERVER_FILE = join(dirname(fileURLToPath(import.meta.url)), "..", "..", "..", "config", "dev-server.json"); type DevServer = { host: string; fakeGoogleHost?: string; personalSlug?: string }; function read(file = DEV_SERVER_FILE): DevServer { let parsed: Partial; try { parsed = JSON.parse(readFileSync(file, "utf8")) as Partial; } catch (e) { throw new Error(`${file} could not be read (${(e as Error).message}): it names the dev Worker ({"host": ""})`); } if (typeof parsed.host !== "string" || !/^[A-Za-z0-9.-]+$/.test(parsed.host)) throw new Error(`${file}: "host" is no host name`); return parsed as DevServer; } /** The dev Worker's host, e.g. `cottondb-dev..workers.dev`. */ export function devHost(file?: string): string { return read(file).host; } /** The dev Worker's address, `https://`. */ export function devBase(file?: string): string { return `https://${devHost(file)}`; } /** The slug of the personal object the import and the dev scripts address (`/b/`). */ export function personalSlug(file?: string): string { const slug = read(file).personalSlug; if (typeof slug !== "string" || !/^[a-z0-9-]+$/.test(slug)) throw new Error(`${file ?? DEV_SERVER_FILE}: "personalSlug" is no slug`); return slug; } /** The vars a dev deploy passes to wrangler (`--var NAME:value`), which wrangler.jsonc's env.dev leaves out. */ export function devDeployVars(file?: string): Record { const server = read(file); return { OPEN_HOST: server.host, ...(server.fakeGoogleHost ? { GOOGLE_BASE: `https://${server.fakeGoogleHost}` } : {}), }; }