Files
loki/scripts/test/env-example-covers-src.ts
CatoandClaude Fable 5.1 f260f0bc52 feat(selfhost): a copy of Loki is a copy — own hostname, own OrangeCat, a complete env contract (#1023)
* feat(selfhost): a copy of Loki is a copy — own hostname, own OrangeCat, a complete env contract

- APP_DOMAIN reads NEXT_PUBLIC_APP_DOMAIN (fallback loki.orangecat.ch), so
  callbacks, emails and copy carry a fork's own name; brand-sync still
  holds the shell mirror to the fallback.
- orangeCatProjectUrl() in config/orangecat.ts replaces the literal
  https://orangecat.ch/projects/… in three components, built on
  NEXT_PUBLIC_ORANGECAT_URL.
- .env.example names every variable src/ reads — 68 were not named, so a
  fork found Google sign-in, embeddings, the Telegram bot or the daemon
  token only by grepping process.env — each with the file that reads it,
  and scripts/test/env-example-covers-src.ts (picked up by test:unit) fails
  when one is missing. A comment that mentions a variable is not a read.
- README: "Run your own copy" — docker compose up, /setup, hostname,
  optional OrangeCat, agents on your own machine, the env contract; the
  name and identity are not part of the licence.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TtBnGxwf5ddXTzRNyjHVP7

* fix(deploy): hand the newly declared NEXT_PUBLIC_* to the build, readers treat "" as unset

public-env-inlined on 070f951b: declaring NEXT_PUBLIC_APP_DOMAIN,
NEXT_PUBLIC_LOKI_BRIDGE_URL and the two ecosystem addresses in
.env.example made them "something an operator may set" — and none reached
the build, so a box that set one would render a different tree than the
browser (the production-only hydration failure that check exists for).

deploy.yml's build step now inlines all four from repo variables. An unset
variable arrives as "", so the readers use || rather than ??: an empty
inlined value falls back to the same default on both sides.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TtBnGxwf5ddXTzRNyjHVP7

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-03 01:09:28 -07:00

54 lines
1.9 KiB
TypeScript

/**
* `.env.example` is the contract a copy of this repo is configured from. Every
* variable `src/` reads must be named there, with the file that reads it.
*
* Before this gate, 70 of them were not: a fork found the switch for Google
* sign-in, embeddings, the Telegram bot or the daemon token only by grepping
* `process.env`. A variable may be documented commented out (`# KEY=`): the
* contract is the NAME and where it is read, not a value. Dynamic reads
* (`process.env[name]`) are the helpers that take the name as an argument and
* are not caught here by design.
*/
import assert from "node:assert/strict";
import { readFileSync, readdirSync, statSync } from "node:fs";
import { join } from "node:path";
const NOT_CONFIG = new Set([
"NODE_ENV",
"NEXT_RUNTIME",
"HOME",
"PATH",
"NEXT_PUBLIC_",
"NEXT_PUBLIC_X",
]);
function* files(dir: string): Generator<string> {
for (const name of readdirSync(dir)) {
const p = join(dir, name);
if (statSync(p).isDirectory()) yield* files(p);
else if (/\.tsx?$/.test(name)) yield p;
}
}
const example = readFileSync(".env.example", "utf8");
const named = new Set([...example.matchAll(/^#?\s*([A-Z0-9_]+)=/gm)].map((m) => m[1]));
const read = new Map<string, string>();
for (const file of files("src")) {
for (const line of readFileSync(file, "utf8").split("\n")) {
// A comment that mentions a variable is not a read of it.
if (/^\s*(\/\/|\*|\/\*)/.test(line)) continue;
for (const m of line.matchAll(/process\.env\.([A-Z0-9_]+)/g)) {
if (!read.has(m[1])) read.set(m[1], file);
}
}
}
const missing = [...read]
.filter(([k]) => !named.has(k) && !NOT_CONFIG.has(k))
.map(([k, f]) => `${k} (read by ${f})`);
assert.deepEqual(
missing,
[],
`add each to .env.example (a \`# KEY=\` line with the reader is enough):\n${missing.join("\n")}`,
);
console.log(`env-example-covers-src: ${read.size} variables read, all named`);