Files
loki/scripts/provision-widget.ts
CatoandClaude Fable 5.1 95c40a9cd8 refactor: rename the product from FleetCrown to Loki (#695)
The product, its assistant, the repo, the host and every unit on the box now
share one name. 638 files, ~3,000 mentions: UI copy, config, env keys
(FLEETCROWN_* -> LOKI_*), systemd units (fleetcrown-* -> loki-*), docs,
articles, the deploy workflow other repos reuse, and nine file names.

- Three article slugs carried the old name and had been shared; permanent
  redirects keep those links alive (next.config.ts).
- test-no-retired-name.sh now also forbids the old product name across the
  whole repo, so the rename cannot creep back through templates and examples.
- ASSISTANT_STACK collapses the product and the assistant into one entry,
  because they are now the same name.

Infra was cut over ahead of this merge: /opt/loki, loki-* units, the `loki`
database and role, loki.orangecat.ch (old host 308s to it; /api, /widget,
/embed keep proxying), GitHub repos bitbaum/loki and bitbaum/loki-releases.
The Resend sending domain stays fleetcrown.orangecat.ch until the new domain
is verified in DNS.


Claude-Session: https://claude.ai/code/session_016YJVAanfj8keVSXyZnTemh

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-13 16:13:10 -07:00

117 lines
4.0 KiB
TypeScript

/**
* Give a new site a Loki project and a widget token.
*
* npx tsx scripts/provision-widget.ts <slug> <title> <host>
*
* Prints the site's env fragment to stdout — nothing else, so `new-site.sh` can
* append it verbatim. Everything human-readable goes to stderr.
*
* NEXT_PUBLIC_FC_WIDGET_TOKEN=<token>
* NEXT_PUBLIC_FC_PROJECT_ID=<uuid>
*
* The project id is emitted because the scaffold's day-zero page links its
* owner to their own Loki project. Without it that link can only reach
* the project LIST — so the single call to action on a brand-new site would
* ask the person who just received it to go find themselves in a list.
*
* WHY THIS IS PART OF SPINNING UP A SITE
*
* The widget is the reason this studio is not a dev shop. The owner looks at
* their own site, points at what they do not like, and an agent changes it —
* without emailing the founder. A site shipped WITHOUT it is a site that
* generates support requests to a person, which is the one thing the model
* exists to avoid. So it is provisioned at birth, not "later".
*
* Idempotent: an existing project of the same name is reused, and an existing
* token is returned rather than rotated — rotating would silently invalidate a
* snippet already deployed on a live site.
*/
import { repoUrlFor } from "../src/config/github-owner";
import { db } from "@/db";
import { entities, users } from "@/db/schema";
import { and, eq } from "drizzle-orm";
import { ENTITY_TYPE } from "@/lib/constants/statuses";
import { createProject } from "@/db/queries/projects";
import { upsertWidgetToken } from "@/db/queries/widget-tokens";
const [slug, title, host] = process.argv.slice(2);
function die(message: string): never {
console.error(`✗ ${message}`);
process.exit(1);
}
if (!slug || !title || !host) {
die("usage: provision-widget.ts <slug> <title> <host>");
}
/**
* The owner. LOKI_OWNER_EMAIL when set; otherwise the single user in the
* database. Refuses to guess when there is more than one — picking an owner
* wrong means the site's feedback lands in a stranger's inbox.
*/
async function resolveOwner(): Promise<string> {
const email = process.env.LOKI_OWNER_EMAIL;
if (email) {
const row = await db.query.users.findFirst({
where: eq(users.email, email),
columns: { id: true },
});
if (!row) die(`no user with email ${email}`);
return row.id;
}
const all = await db.select({ id: users.id }).from(users).limit(2);
if (all.length === 0) die("no users in the database");
if (all.length > 1) die("more than one user — set LOKI_OWNER_EMAIL");
return all[0].id;
}
async function main(): Promise<void> {
const userId = await resolveOwner();
let projectId: string;
const existing = await db.query.entities.findFirst({
where: and(
eq(entities.userId, userId),
eq(entities.name, title),
eq(entities.type, ENTITY_TYPE.PROJECT),
),
columns: { id: true },
});
if (existing) {
projectId = existing.id;
console.error(`↻ project "${title}" already exists (${projectId})`);
} else {
const created = await createProject(
userId,
{
name: title,
description: `Website at https://${host}`,
// Owner from the environment, matching _box-env.sh — hardcoding it
// here is what made a rename touch this file at all.
gitUrl: repoUrlFor(slug),
},
"new-site.sh",
);
projectId = created.id;
console.error(`+ created project "${title}" (${projectId})`);
}
// Origins bound to this site's host, so a token leaked from the page cannot
// be replayed from somewhere else.
const token = await upsertWidgetToken(userId, projectId, {
origins: [`https://${host}`],
});
if (!token) die("failed to mint a widget token");
console.error(`✓ widget token bound to https://${host}`);
process.stdout.write(
`NEXT_PUBLIC_FC_WIDGET_TOKEN=${token.token}\nNEXT_PUBLIC_FC_PROJECT_ID=${projectId}\n`,
);
}
main()
.then(() => process.exit(0))
.catch((err) => die(err instanceof Error ? err.message : String(err)));