Cotton code
44 lines · 2.1 KB · TypeScriptRaw
/** * Switches the site follows from the live server, read from the one place they are set, so flipping one there * flips the site on its next build. * * ACCOUNT_DELETE: live's own switch for deleting an account (apps/business/wrangler.jsonc, top-level `vars`; * the server reads it in src/account_delete.ts `switchedOn`, and only "on" is on). While it is off the apps cannot * delete, and live's door hands /delete to this site, whose page (pages.ts `deletePage`) says to ask by email, as * help and the privacy policy do. Turn it on there and rebuild: the door then answers /delete with its own form. */import { readFileSync } from "node:fs";import path from "node:path";import { fileURLToPath } from "node:url";const WRANGLER = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../../../business/wrangler.jsonc");/** JSON with comments and trailing commas, as wrangler.jsonc allows, to plain JSON. Strings are left alone. */export function jsonc(text: string): unknown {  let out = "";  for (let i = 0; i < text.length; i++) {    const c = text[i]!;    if (c === '"') {      const start = i;      for (i++; i < text.length && text[i] !== '"'; i++) if (text[i] === "\\") i++;      out += text.slice(start, i + 1);    } else if (c === "/" && text[i + 1] === "/") {      while (i < text.length && text[i] !== "\n") i++;      out += "\n";    } else if (c === "/" && text[i + 1] === "*") {      i = text.indexOf("*/", i + 2) + 1;    } else out += c;  }  return JSON.parse(out.replace(/,(\s*[}\]])/g, "$1"));}export function accountDeleteOn(file = WRANGLER): boolean {  const config = jsonc(readFileSync(file, "utf8")) as { vars?: Record<string, string> };  return config.vars?.ACCOUNT_DELETE === "on";}/** Keeps the text marked for the switch's state: <!-- delete:on -->…<!-- /delete:on --> and the same for off. */export const forDelete = (html: string, on: boolean): string =>  html    .replace(on ? /<!-- delete:off -->[\s\S]*?<!-- \/delete:off -->\n?/g : /<!-- delete:on -->[\s\S]*?<!-- \/delete:on -->\n?/g, "")    .replace(/<!-- \/?delete:(?:on|off) -->\n?/g, "");