Cotton code
84 lines · 4.2 KB · TypeScriptRaw
#!/usr/bin/env tsx/** * gen-kind — scaffold a record kind from one declaration. * *     pnpm gen:kind path/to/alarm.json [--pr=#1400] * * Reads the declaration, refuses it with every problem listed if it is not * whole, and otherwise writes the kind into every mechanical seam the gates * name (`scripts/lib/gen-kind.ts` lists them): the kind's DDL and a bump of * `SCHEMA_VERSION` in `schema/ddl.rs` — never a ladder rung or a migrator * test, since the store's ladder is one rung (9a). It prints what it wrote, * the SHAPE ledger line for the change, and what stays by hand. Run it once per kind, on a * clean tree, and read the diff — the checked-in code is the declaration. * * `--example` prints a complete declaration to start from. */import { readFileSync } from "node:fs";import { resolve } from "node:path";import { fileURLToPath } from "node:url";import { type KindDeclaration, runGenKind } from "./lib/gen-kind";export const EXAMPLE: KindDeclaration = {  kind: "timer",  table: "cotton_timers",  why: "A timer is a span the phone counts down, kept apart from alarms because it is a duration and not an instant, and apart from events because it has no place and no guests.",  columns: [    { name: "label", sql: "TEXT NOT NULL", codec: "Text" },    { name: "seconds", sql: "INTEGER NOT NULL", codec: "Int", comment: "The span, whole seconds; the phone counts it down from the moment it starts." },    { name: "running", sql: "INTEGER NOT NULL DEFAULT 0", codec: "Bool" },  ],  title_column: "label",  checks: ["running IN (0, 1)", "seconds > 0"],  indexes: ["CREATE INDEX IF NOT EXISTS idx_cotton_timers_running ON cotton_timers (workspace_id, running)"],  facilities: {    fts: true,    embedded: "A label of three words has nothing a nearest-neighbour search would find that FTS does not.",    search: "fts",    versioned: true,    commentable: true,    summarized: "There is nothing to summarise about an instant and a label.",    project_linkable: "A timer belongs to a kitchen, not to a project.",    linkable: true,    route_target: "A capture never becomes a timer; the phone's clock does.",    addressable: true,    binnable: true,    shareable: "A timer is the phone's own; the sharing design has not asked about it.",  },  fts_columns: ["label"],  history: { fields: [{ column: "label", label: "Label" }, { column: "seconds", label: "Seconds", form: "Number" }, { column: "running", label: "Running", form: "Flag" }], restore: ["label", "seconds", "running"] },  words: {    meaning: "A timer is one span the phone counts down, with the words it shows when it reaches zero.",    feeds: "The owner starts timers on the phone's clock screen; an assistant starts one with create_timer when asked to count a span down.",  },  section: "calendar",  verbs: { create: "create_timer", list: "list_timers", get: "get_timer", update: "update_timer", remove: { not: "A timer is binned from the phone; the bin's own tools restore and empty it." } },  routes: { list: "/timers", page: "/timers/[id]" },};function main(argv: string[]): number {  if (argv.includes("--example")) {    process.stdout.write(`${JSON.stringify(EXAMPLE, null, 2)}\n`);    return 0;  }  const file = argv.find((arg) => !arg.startsWith("--"));  if (!file) {    process.stderr.write("usage: pnpm gen:kind <declaration.json> [--pr=#N]   (pnpm gen:kind --example prints one)\n");    return 2;  }  const decl = JSON.parse(readFileSync(resolve(file), "utf8")) as KindDeclaration;  const root = resolve(fileURLToPath(new URL(".", import.meta.url)), "..");  try {    const pr = argv.find((arg) => arg.startsWith("--pr="))?.slice("--pr=".length);    const result = runGenKind(decl, root, { pr });    process.stdout.write(`gen-kind: ${decl.kind} is schema v${result.version}. Wrote:\n${result.written.map((f) => `  ${f}`).join("\n")}\n\nThe shape ledger line:\n  ${result.shapeLine}\n\nBy hand, before the gates go green:\n${result.handSeams.map((line) => `  - ${line}`).join("\n")}\n`);    return 0;  } catch (error) {    process.stderr.write(`gen-kind: ${(error as Error).message}\n`);    return 1;  }}const isMain = process.argv[1] !== undefined && /gen-kind\.ts$/.test(process.argv[1]);if (isMain) process.exitCode = main(process.argv.slice(2));