Cotton code
160 lines · 7.9 KB · TypeScriptRaw
import { readFileSync } from "node:fs";/** * THE APPS: apps.json, at the root of this package, is the one file that says which apps the site shows, in what * order, where each stands in each store, with which pictures, colours and words. Adding an app, or moving one into a * store, is one edit there. This file types it and checks it as it loads. * * Where an app stands is its `status` (src/site/state.ts draws it, on the home page's list and on the app's page): *   testing   its TestFlight and Google Play test links, which open only for invited testers. *   released  "Get it on the App Store" / "Get it on Google Play", linking to the listings. * `links` holds all four; `registry` names the app in apps/registry.json, whose ids the links must match. * `stage` is kept for what an app is (private: family and friends); the site's words no longer read it. */export type Stage = "works" | "testing" | "private" | "public";export type Status = "testing" | "released";export interface Links {  /** https://testflight.apple.com/v1/app/<App Store Connect id> */  testFlight: string;  /** https://play.google.com/apps/internaltest/<id>: the Play Console's internal-testing link */  playTest: string;  /** https://apps.apple.com/app/id<App Store Connect id> */  appStore: string;  /** https://play.google.com/store/apps/details?id=<package> */  googlePlay: string;}export interface Look {  /** The browser bar's colour on the app's page, light then dark. */  theme: [string, string];  /** The app's paper: the custom properties of its class in cotton.css (--ground, --ink, --muted, --line, --accent…). */  paper: Record<string, string>;  paperDark: Record<string, string>;}export interface Shot {  /** A file in static/shots/<app id>/, without extension: <name>.webp and <name>@2x.webp. */  file: string;  /** The picture it is made from (a screenshot of the app with invented data), from the repo root. */  source: string;  /** What the picture shows, for someone who cannot see it. */  alt: string;  /** The line under it on the app's page. */  caption: string;  /** Where a pen mark would sit (kept from the first design; this design draws none). */  mark?: { ring?: [number, number, number, number]; arrow?: [number, number, number, number] };}/** One of the two screens on the home page's sheet. */export interface HomeShot {  file: string;  alt: string;  /** Loaded with the page (the first screen of the first sheet); the rest load as they come near. */  eager?: boolean;  /** A shot shown instead when the reader's phone is dark. */  dark?: string;}export interface App {  id: string;  /** The app's Android flavour where it differs from the id (tools/site_shots.sh walks it). */  android?: string;  /** The name under the icon. */  name: string;  /** The name in the stores and on its page. */  full: string;  /** One line: what it is (ends with a full stop). */  line: string;  /** The page's description, for search and link previews. */  description: string;  stage: Stage;  /** The app's slug in apps/registry.json. */  registry: string;  status: Status;  links: Links;  /** Has a page of its own at /<id>. */  page: boolean;  /** The app's own store icon, from the repo root; served as /icons/<id>.webp. */  iconSource: string;  /** The touch its name wears: Days' pen ring, Words' cursor, Story's full stop, Comms' quote mark. */  flourish?: "ring" | "cursor" | "stop" | "quote";  look: Look;  home: HomeShot[];  /** The app's page: four to six screens with their captions. */  shots: Shot[];}/** * An app's page in the owner's own voice (owner, 25 Nov 2026), from app-pages.json: `why`, one short first-person * paragraph under the name (empty until the coordinator gives it: the page then holds a blank, so a live build * refuses it); `has`, what it has now, and `coming`, what is decided and not built, each one plain comma-separated * sentence checked against the code (the lane's report names the file and line for every item); `account`, one * plain line under them. */export interface AppPage {  why: string;  has: string;  coming: string;  /** One plain line under the two sentences: whether it needs an account, and what one adds. */  account: string;}/** What each of an app's four links looks like. */export const LINK: Record<keyof Links, RegExp> = {  testFlight: /^https:\/\/testflight\.apple\.com\/v1\/app\/\d+$/,  playTest: /^https:\/\/play\.google\.com\/apps\/internaltest\/\d+$/,  appStore: /^https:\/\/apps\.apple\.com\/app\/id\d+$/,  googlePlay: /^https:\/\/play\.google\.com\/store\/apps\/details\?id=com\.cottondb\.[\w.]+$/,};const file = new URL("../../apps.json", import.meta.url);/** The apps, in order, checked: a mistake in apps.json stops the build with its name. */export const APPS: App[] = check((JSON.parse(readFileSync(file, "utf8")) as { apps: App[] }).apps);function check(apps: App[]): App[] {  const ids = new Set<string>();  const hex = /^#[0-9A-Fa-f]{6}$/;  for (const a of apps) {    const where = `apps.json: ${a.id ?? "an app"}`;    if (!/^[a-z][a-z0-9-]*$/.test(a.id)) throw new Error(`${where}: the id must be lower-case letters`);    if (ids.has(a.id)) throw new Error(`${where}: the id is used twice`);    ids.add(a.id);    if (!["works", "testing", "private", "public"].includes(a.stage)) throw new Error(`${where}: stage ${a.stage} is not one of works, testing, private, public`);    if (!["testing", "released"].includes(a.status)) throw new Error(`${where}: status ${a.status} is not testing or released`);    for (const [k, re] of Object.entries(LINK)) if (!re.test(a.links?.[k as keyof Links] ?? "")) throw new Error(`${where}: links.${k} is not a ${k} link`);    for (const k of ["name", "full", "line", "description"] as const) if (!a[k]) throw new Error(`${where}: ${k} is missing`);    if (!a.look?.theme?.every((c) => hex.test(c))) throw new Error(`${where}: look.theme must be two colours like #1D3FD8`);    for (const k of ["--ground", "--ink", "--muted", "--line", "--accent"])      if (!a.look.paper[k] || !a.look.paperDark[k]) throw new Error(`${where}: look.paper and look.paperDark need ${k}`);    for (const p of [a.look.paper, a.look.paperDark])      for (const [k, v] of Object.entries(p))        if (!/^--[a-z]+$/.test(k) || /[;{}<>]/.test(v)) throw new Error(`${where}: look paper ${k}: ${v} is not a plain custom property`);    if (a.page && (a.shots.length < 4 || a.shots.length > 6)) throw new Error(`${where}: an app's page shows four to six screens`);    for (const s of a.shots) if (!s.alt || !s.caption) throw new Error(`${where}: shot ${s.file} needs alt and caption`);    const files = new Set(a.shots.map((s) => s.file));    for (const h of a.home) {      if (!h.alt) throw new Error(`${where}: home shot ${h.file} needs alt`);      if (!files.has(h.file) || (h.dark && !files.has(h.dark))) throw new Error(`${where}: home shot ${h.file} is not one of its shots`);    }  }  return apps;}const pagesFile = new URL("../../app-pages.json", import.meta.url);/** Each app's page words, by id, checked: every app with a page has its entry, and nothing else does. */export const APP_PAGES: Record<string, AppPage> = checkPages(JSON.parse(readFileSync(pagesFile, "utf8")) as Record<string, AppPage>);function checkPages(pages: Record<string, AppPage>): Record<string, AppPage> {  const withPage = APPS.filter((a) => a.page).map((a) => a.id);  for (const id of withPage) {    const p = pages[id];    if (!p) throw new Error(`app-pages.json: ${id} has a page and no entry`);    for (const k of ["why", "has", "coming", "account"] as const) if (typeof p[k] !== "string") throw new Error(`app-pages.json: ${id}.${k} must be a string`);    for (const k of ["has", "coming", "account"] as const) if (!p[k].trim()) throw new Error(`app-pages.json: ${id}.${k} is empty`);  }  for (const id of Object.keys(pages)) if (!withPage.includes(id)) throw new Error(`app-pages.json: ${id} is not an app with a page`);  return pages;}