clone

nostr relays on demand at <name>.bind.ws: claim one with your key, pay in zaps past the free allowance

dependabot/npm_and_yarn/npm-f01a459b0f
bindws / scripts / build / build-templates.mjs
build-templates.mjs 1.7K
1// Folds relay-templates/*.jsonc into src/gen/templates.ts, the presets the2// relay offers, in file order. `--check` rebuilds in memory and fails when3// the committed file is stale, so typecheck catches a forgotten rebuild.4import { readdirSync, readFileSync, writeFileSync } from "node:fs";5import { stripComments } from "../check/jsonc.mjs";67const DIR = new URL("../../relay-templates/", import.meta.url);8const OUT = new URL("../../src/gen/templates.ts", import.meta.url);9const files = readdirSync(DIR).filter((f) => f.endsWith(".jsonc")).sort();10// The folder's README is the catalog: every template has a row there.11const readme = readFileSync(new URL("README.md", DIR), "utf8");12const missing = files.filter((f) => !readme.includes(`](${f})`));13if (missing.length) {14 console.error(`relay-templates/README.md has no row for: ${missing.join(", ")}`);15 process.exit(1);16}17const templates = files.map((f) => {18 const doc = JSON.parse(stripComments(readFileSync(new URL(f, DIR), "utf8")));19 delete doc.$schema;20 return { name: f.replace(/^\d+-/, "").replace(/\.jsonc$/, ""), document: doc };21});22const file = `// Generated by scripts/build/build-templates.mjs from relay-templates/. Do not edit; run npm run build:templates.23export const TEMPLATES: { name: string; document: unknown }[] = ${JSON.stringify(templates, null, 2)};24`;25if (process.argv.includes("--check")) {26 let cur = "";27 try {28 cur = readFileSync(OUT, "utf8");29 } catch {30 /* missing */31 }32 if (cur !== file) {33 console.error("src/gen/templates.ts is stale: run npm run build:templates");34 process.exit(1);35 }36} else {37 writeFileSync(OUT, file);38 console.log(`src/gen/templates.ts: ${templates.length} templates`);39}