create-user-hub 0.3.1 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/index.js +27 -19
- package/package.json +1 -1
- package/template/.env.example +3 -5
- package/template/CLAUDE.md +7 -6
- package/template/README.md +7 -7
- package/template/dockerignore +1 -1
- package/template/package.json +6 -5
- package/template/src/routes/__root.tsx +14 -0
- package/template/scripts/setup.ts +0 -586
package/README.md
CHANGED
|
@@ -10,13 +10,13 @@ bun create user-hub@latest my-app
|
|
|
10
10
|
|
|
11
11
|
It asks for your hub's URL (from your hub's admin) and checks the hub answers. Then it signs the new app in to the hub with your account: paste an API key from the hub's account page (it shows as stars), or press Enter and approve the sign-in in your browser. The app's `.env.local` gets `HUB_URL` and `HUB_API_KEY`.
|
|
12
12
|
|
|
13
|
-
Scripts and coding agents are never asked: pass `--hub-url` (or set `HUB_URL`), and set `HUB_API_KEY` in the environment if you have one (for example through your secret manager) to have it checked and saved. Without a key, sign in later from the app's folder
|
|
13
|
+
Scripts and coding agents are never asked: pass `--hub-url` (or set `HUB_URL`), and set `HUB_API_KEY` in the environment if you have one (for example through your secret manager) to have it checked and saved. Without a key, sign in later from the app's folder with `bunx user-hub login` (a coding agent runs `bunx user-hub login --browser` and shows you the link to approve). The key is never a command-line flag, so it stays out of shell history.
|
|
14
14
|
|
|
15
15
|
You get a TanStack Start + Convex app with Tailwind, shadcn (dark theme) and Vite+ (Oxfmt, Oxlint), plus:
|
|
16
16
|
|
|
17
17
|
- **Sign-in** from [`@user-hub/auth`](https://www.npmjs.com/package/@user-hub/auth): a signed-in home page, a facilitator-only page, sign-out, and notes saved per person.
|
|
18
|
-
- **For coding agents:** `CLAUDE.md` (also as `AGENTS.md`) and the `user-hub-auth`
|
|
19
|
-
- **`bun run setup`:** one command that creates your Convex project, registers the app with your hub in an event (a workshop: it asks which, or creates one) and sets the values it needs.
|
|
18
|
+
- **For coding agents:** `CLAUDE.md` (also as `AGENTS.md`) and the `user-hub-auth` (sign-in) and `user-hub-cli` (the hub from the terminal) skills in `.claude/skills`.
|
|
19
|
+
- **`bun run setup`:** one command (`user-hub setup`, from the [`user-hub`](https://www.npmjs.com/package/user-hub) CLI) that creates your Convex project, registers the app with your hub in an event (a workshop: it asks which, or creates one) and sets the values it needs. The same CLI runs the event's people and apps from the terminal (`bunx user-hub --help`).
|
|
20
20
|
|
|
21
21
|
## Then
|
|
22
22
|
|
package/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
2
|
// bun create user-hub@latest [my-app] [--hub-url <url>]
|
|
3
3
|
// Copies the starter, names it, saves the hub's URL to .env.local, installs
|
|
4
|
-
// it, adds the agent
|
|
4
|
+
// it, adds the agent skills and signs the new app in to the hub.
|
|
5
5
|
//
|
|
6
|
-
// Signing in is `hub
|
|
6
|
+
// Signing in is `user-hub login` from the user-hub CLI, run inside the new app:
|
|
7
7
|
// it saves HUB_URL and HUB_API_KEY to .env.local. People in a terminal paste
|
|
8
8
|
// an API key or approve the sign-in in their browser. Scripts and agents set
|
|
9
9
|
// HUB_URL (or --hub-url) and, if they have one, HUB_API_KEY in the
|
|
@@ -23,7 +23,11 @@ import { basename, join, resolve } from "node:path";
|
|
|
23
23
|
import { parseArgs } from "node:util";
|
|
24
24
|
|
|
25
25
|
const TEMPLATE = join(import.meta.dir, "template");
|
|
26
|
-
|
|
26
|
+
// The skills ship inside the packages they describe.
|
|
27
|
+
const SKILLS = {
|
|
28
|
+
"user-hub-auth": "node_modules/@user-hub/auth/skills/user-hub-auth",
|
|
29
|
+
"user-hub-cli": "node_modules/user-hub/skills/user-hub-cli",
|
|
30
|
+
};
|
|
27
31
|
const interactive = Boolean(process.stdin.isTTY);
|
|
28
32
|
|
|
29
33
|
function fail(message) {
|
|
@@ -101,13 +105,16 @@ await cp(TEMPLATE, dest, { recursive: true });
|
|
|
101
105
|
await rename(join(dest, "gitignore"), join(dest, ".gitignore"));
|
|
102
106
|
await rename(join(dest, "dockerignore"), join(dest, ".dockerignore"));
|
|
103
107
|
|
|
104
|
-
// Testing unreleased
|
|
105
|
-
// file:/path/to/user-hub-auth)
|
|
108
|
+
// Testing unreleased packages: CREATE_USER_HUB_AUTH_SPEC and
|
|
109
|
+
// CREATE_USER_HUB_CLI_SPEC (for example file:/path/to/user-hub-auth) replace
|
|
110
|
+
// the version ranges of @user-hub/auth and user-hub before installing.
|
|
106
111
|
const authSpec = process.env.CREATE_USER_HUB_AUTH_SPEC;
|
|
107
|
-
|
|
112
|
+
const cliSpec = process.env.CREATE_USER_HUB_CLI_SPEC;
|
|
113
|
+
if (authSpec || cliSpec) {
|
|
108
114
|
const path = join(dest, "package.json");
|
|
109
115
|
const pkg = JSON.parse(await readFile(path, "utf8"));
|
|
110
|
-
pkg.dependencies["@user-hub/auth"] = authSpec;
|
|
116
|
+
if (authSpec) pkg.dependencies["@user-hub/auth"] = authSpec;
|
|
117
|
+
if (cliSpec) pkg.devDependencies["user-hub"] = cliSpec;
|
|
111
118
|
await writeFile(path, `${JSON.stringify(pkg, null, 2)}\n`);
|
|
112
119
|
}
|
|
113
120
|
|
|
@@ -128,7 +135,7 @@ for (const file of [
|
|
|
128
135
|
}
|
|
129
136
|
await symlink("CLAUDE.md", join(dest, "AGENTS.md"));
|
|
130
137
|
|
|
131
|
-
// .env.local is gitignored. hub
|
|
138
|
+
// .env.local is gitignored. user-hub login adds HUB_API_KEY next to the URL.
|
|
132
139
|
if (hubUrl) {
|
|
133
140
|
await writeFile(join(dest, ".env.local"), `HUB_URL=${hubUrl}\n`, {
|
|
134
141
|
mode: 0o600,
|
|
@@ -165,20 +172,21 @@ if (!installed || interrupted) {
|
|
|
165
172
|
process.exit(interrupted ? 130 : 1);
|
|
166
173
|
}
|
|
167
174
|
|
|
168
|
-
//
|
|
169
|
-
// for Claude Code.
|
|
175
|
+
// One copy of each skill for every agent, linked for Claude Code.
|
|
170
176
|
await mkdir(join(dest, ".agents/skills"), { recursive: true });
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
177
|
+
for (const [name, from] of Object.entries(SKILLS)) {
|
|
178
|
+
await cp(join(dest, from), join(dest, ".agents/skills", name), {
|
|
179
|
+
recursive: true,
|
|
180
|
+
dereference: true,
|
|
181
|
+
});
|
|
182
|
+
}
|
|
175
183
|
await mkdir(join(dest, ".claude"), { recursive: true });
|
|
176
184
|
await symlink("../.agents/skills", join(dest, ".claude/skills"));
|
|
177
185
|
|
|
178
186
|
await run(["git", "init", "-q"]);
|
|
179
187
|
|
|
180
188
|
// Sign the new app in: a key from the environment is checked with the hub and
|
|
181
|
-
// saved; in a terminal, hub
|
|
189
|
+
// saved; in a terminal, user-hub asks for a key or a browser sign-in.
|
|
182
190
|
let signedIn = false;
|
|
183
191
|
if (hubUrl && (apiKey || interactive)) {
|
|
184
192
|
// --no-install: the CLI installed above, never an npm package of that name.
|
|
@@ -186,13 +194,13 @@ if (hubUrl && (apiKey || interactive)) {
|
|
|
186
194
|
"bun",
|
|
187
195
|
"x",
|
|
188
196
|
"--no-install",
|
|
189
|
-
"hub
|
|
197
|
+
"user-hub",
|
|
190
198
|
"login",
|
|
191
199
|
"--hub",
|
|
192
200
|
hubUrl,
|
|
193
201
|
];
|
|
194
202
|
console.log("\nSigning in to the hub");
|
|
195
|
-
// Ctrl-C belongs to hub
|
|
203
|
+
// Ctrl-C belongs to user-hub; the app exists either way, so still say what
|
|
196
204
|
// is next.
|
|
197
205
|
const ignore = () => {};
|
|
198
206
|
process.on("SIGINT", ignore);
|
|
@@ -212,7 +220,7 @@ ${
|
|
|
212
220
|
signedIn
|
|
213
221
|
? ""
|
|
214
222
|
: `
|
|
215
|
-
0. bunx hub
|
|
223
|
+
0. bunx user-hub login signs this app in to the hub${hubUrl ? "" : "\n (its URL comes from your hub's admin)"}
|
|
216
224
|
Paste an API key from the hub's account page, or press
|
|
217
225
|
Enter to approve the sign-in in your browser. A coding
|
|
218
226
|
agent runs it with --browser and shows you the link.
|
|
@@ -222,7 +230,7 @@ ${
|
|
|
222
230
|
2. bun run dev http://localhost:3000
|
|
223
231
|
|
|
224
232
|
Using a coding agent? It reads CLAUDE.md (AGENTS.md) and the
|
|
225
|
-
user-hub-auth
|
|
233
|
+
user-hub-auth and user-hub-cli skills in .claude/skills.
|
|
226
234
|
`);
|
|
227
235
|
|
|
228
236
|
// A key passed in by a script that the hub refused is an error for it to see.
|
package/package.json
CHANGED
package/template/.env.example
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
# Saved by bunx hub
|
|
1
|
+
# Saved by bunx user-hub login (bun run setup runs it when they are missing).
|
|
2
2
|
# Never commit them; never share the API key.
|
|
3
3
|
HUB_URL=https://hub.example.com
|
|
4
4
|
HUB_API_KEY=
|
|
5
5
|
|
|
6
6
|
# Written by bun run setup and the Convex CLI:
|
|
7
|
-
# CONVEX_DEPLOYMENT, VITE_CONVEX_URL, VITE_HUB_URL
|
|
8
|
-
#
|
|
9
|
-
# once registered), HUB_APP_DEV_URL (its development URL),
|
|
10
|
-
# HUB_APP_PROD_URL (once given)
|
|
7
|
+
# CONVEX_DEPLOYMENT, VITE_CONVEX_URL, VITE_HUB_URL. The app's event, name and
|
|
8
|
+
# URLs in the hub go to user-hub.json, which is committed.
|
package/template/CLAUDE.md
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
# __APP_NAME__
|
|
2
2
|
|
|
3
|
-
A TanStack Start + Convex app whose sign-in comes from User Hub: people scan their badge and land here signed in. All auth comes from `@user-hub/auth
|
|
3
|
+
A TanStack Start + Convex app whose sign-in comes from User Hub: people scan their badge and land here signed in. All auth comes from `@user-hub/auth`. In `.claude/skills`, follow the `user-hub-auth` skill for sign-in, roles and wiring, and the `user-hub-cli` skill for any `user-hub` command (signing in, setup, the event's people and apps).
|
|
4
4
|
|
|
5
5
|
## How it is wired
|
|
6
6
|
|
|
7
7
|
- `convex/hub.ts`: this app's key in the hub (`APP_KEY`, made from its event's slug and its name), written by `bun run setup`.
|
|
8
|
-
- The app belongs to one hub **event** (a workshop): its slug is `
|
|
8
|
+
- The app belongs to one hub **event** (a workshop): its slug is `event` in `user-hub.json` (with the app's name and URLs in the hub), saved by `bun run setup`. Commit it; it holds nothing secret.
|
|
9
9
|
- `convex/auth.config.ts`: `hubAuth({ app: APP_KEY })`; Convex trusts hub tokens through `HUB_ISSUER` and `HUB_JWKS` on the deployment.
|
|
10
|
-
- `src/routes/__root.tsx`: `ConvexHubAuthProvider` and `HubSignedIn` inside `<body>`. Never move them outside `<html>` or into the router's `Wrap`.
|
|
10
|
+
- `src/routes/__root.tsx`: `ConvexHubAuthProvider` and `HubSignedIn` inside `<body>`. Never move them outside `<html>` or into the router's `Wrap`. Its `beforeLoad` reads the person's token on the server (`getHubToken`) and passes it as `initialToken`, so pages render signed in from the server: keep page components safe to render there (no `window` or `document` outside effects).
|
|
11
11
|
- People: `useHubUser()` in components; `requireHubUser(ctx)` / `requireFacilitator(ctx)` in Convex functions. Key data on the person's hub id; check roles on the server.
|
|
12
12
|
- Sign out: `useHubSignOut()`.
|
|
13
13
|
|
|
@@ -15,7 +15,8 @@ A TanStack Start + Convex app whose sign-in comes from User Hub: people scan the
|
|
|
15
15
|
|
|
16
16
|
- **Bun only:** `bun` and `bunx`; never `npm`, `npx`, `pnpm` or `yarn`.
|
|
17
17
|
- **Never hand-roll auth:** no login screens, passwords, sessions or token handling. If something is missing, it belongs in `@user-hub/auth`.
|
|
18
|
-
- **Secrets:** `HUB_API_KEY` lives only in `.env.local`, saved by `hub
|
|
18
|
+
- **Secrets:** `HUB_API_KEY` lives only in `.env.local`, saved by `user-hub login`. Never ask for it (or a password) in chat, write it yourself, print it, copy it, commit it, or print `.env.local`. When the folder is not signed in (`bunx user-hub whoami` exits with code 4), ask the human to run `bunx user-hub login`, or run `bunx user-hub login --browser` and give them the link it prints (the skill's "Signing in").
|
|
19
|
+
- **The hub from the terminal:** `bunx user-hub` (a devDependency) also lists and changes this event's people and apps (`user-hub people …`, `user-hub apps …`, `--help`). Run those only when the human asks; badge links it prints are lasting sign-in links, so never put them in code, commits or logs.
|
|
19
20
|
- **Convex:** public functions are thin wrappers (validators, an auth check, one call into `convex/model/`); every function has `args` and `returns` validators; reads use indexes and are bounded.
|
|
20
21
|
- **Convex AI files** are off (`"aiFiles": { "enabled": false }` in `convex.json`), so `convex dev` never asks to add them: they would append a block telling agents to use `npx` to this file and add its own skills. To add them anyway, run `bunx convex ai-files enable`.
|
|
21
22
|
- **UI:** shadcn components only (`bunx --bun shadcn@latest add …`), dark theme, works on mobile. Lists are `Item`s in an `ItemGroup` with actions in a `ButtonGroup`, never tables; forms use `Field`.
|
|
@@ -25,6 +26,6 @@ A TanStack Start + Convex app whose sign-in comes from User Hub: people scan the
|
|
|
25
26
|
|
|
26
27
|
## Commands
|
|
27
28
|
|
|
28
|
-
`bun run setup` (once; signs the folder in first when run in a terminal and it is not; in a terminal it asks which hub event the app is for, or creates one, and saves it
|
|
29
|
+
`bun run setup` (`user-hub setup`; once; signs the folder in first when run in a terminal and it is not; in a terminal it asks which hub event the app is for, or creates one, and saves it in `user-hub.json`), `bun run setup --event <slug>` (the event without asking: an agent asks the human which event and passes it, `bunx user-hub events list` lists them; never create an event unless the human asks, and then ask for its time zone too; with another event or `--name` than the ones in `user-hub.json`, the app is registered anew with a new app key and the earlier registration is left as it was), `bun run setup --prod <url>` (once deployed: registers the production URL so badge scans can reach the app; see the skill's "Production URL"), `bun run setup --dev <url>` (change where the hub opens the app for developers; first registered as `http://localhost:` plus the port in the `dev` script, kept on later runs; change the port in the `dev` script too: it is defined only there, and `server.strictPort` in `vite.config.ts` makes the dev server stop when the port is taken rather than move to another one the hub does not know), `bun run setup --unlisted` / `--listed` (keep the app out of the hub's badge app list, or put it back; new apps are listed), `bun run dev`, `bun run check` (`vp check`: format, lint and type-check), `bun run fix` (`vp check --fix`), `bun run typecheck`, `bun run build`, `just build` / `just push` (deploy Convex to production and build the Docker image against it; `IMAGE` sets the registry path; the container needs no env). Lint and format rules live in `vite.config.ts` (Vite+); `vp` is project-local, so run it through the scripts or `bunx vp`.
|
|
29
30
|
|
|
30
|
-
**Hub moved:** sign the folder in to the new hub (`bunx hub
|
|
31
|
+
**Hub moved:** sign the folder in to the new hub (`bunx user-hub login --hub <new url>`, replacing `HUB_URL` and `HUB_API_KEY`), run `bun run setup --hub-moved` (with `--event <slug>` when the new hub has no event with this app's saved event; it updates this app's registration and sets `HUB_ISSUER`/`HUB_JWKS` on Convex, production included once a production URL is known, and `VITE_HUB_URL`), then `just push`, because `VITE_HUB_URL` is built into the frontend.
|
package/template/README.md
CHANGED
|
@@ -7,12 +7,12 @@ A TanStack Start + Convex app with User Hub sign-in: people scan their badge and
|
|
|
7
7
|
1. Sign this folder in to your hub (your hub's admin gives you its URL and an account):
|
|
8
8
|
|
|
9
9
|
```sh
|
|
10
|
-
bunx hub
|
|
10
|
+
bunx user-hub login --hub https://hub.example.com
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
Paste an API key from the hub's account page, or press Enter and approve the sign-in in your browser. It saves `HUB_URL` and `HUB_API_KEY` to `.env.local`. Skip this if you signed in when the app was created; `bun run setup` also asks when this folder is not signed in.
|
|
14
14
|
|
|
15
|
-
2. `bun run setup
|
|
15
|
+
2. `bun run setup` (`user-hub setup`): creates your Convex project, registers the app with the hub and sets everything up. The hub runs many workshops, called events, and the app belongs to one: setup lists the active events and asks which (or creates a new one, asking for its name and time zone). Without a terminal, pass it: `bun run setup --event <slug>` (`bunx user-hub events list` lists the slugs). If another app already has this name in the event, it asks you for a new one (or run `bun run setup --name <name>`). New apps show in the hub's badge app list once they have a production URL; `bun run setup --unlisted` keeps this one out and `--listed` puts it back. Safe to run again. Setup saves the app's event, name and URLs to `user-hub.json`: commit it. `--name` or `--event` with another name or event than the saved ones registers the app anew, with a new app key; setup leaves the earlier registration as it was.
|
|
16
16
|
3. `bun run dev`, then open http://localhost:3000. You are sent to the hub to pick who to be, then back signed in. If something else already uses that port, the dev server stops with an error instead of moving to another port, because the hub only sends you back to the registered URL. To use another port, change it in `package.json`'s `dev` script and run `bun run setup --dev http://localhost:<port>`; later runs of setup keep the development URL.
|
|
17
17
|
4. Once the app is deployed: `bun run setup --prod https://your-app.example.com`, so badge scans can reach it. Setup asks for this the first time; press Enter to skip until you deploy.
|
|
18
18
|
|
|
@@ -27,17 +27,17 @@ Then serve the container (port 3000) at your production URL and make sure the hu
|
|
|
27
27
|
|
|
28
28
|
### Hub moved
|
|
29
29
|
|
|
30
|
-
Sign this folder in to the new hub (`bunx hub
|
|
30
|
+
Sign this folder in to the new hub (`bunx user-hub login --hub <new url>`, which replaces `HUB_URL` and `HUB_API_KEY` in `.env.local`) and run `bun run setup --hub-moved` (it asks for the event again when the new hub has no event with the slug in `user-hub.json`, or pass `--event <slug>`): it updates this app's registration with the new hub and sets `HUB_ISSUER`, `HUB_JWKS` (on the production Convex deployment too, once it has a production URL) and `VITE_HUB_URL`. Then `just push`, because `VITE_HUB_URL` is built into the frontend.
|
|
31
31
|
|
|
32
32
|
## What's inside
|
|
33
33
|
|
|
34
|
-
- `src/routes/__root.tsx`: the hub providers inside `<body>`; `HubSignedIn` shows pages only to signed-in people.
|
|
34
|
+
- `src/routes/__root.tsx`: the hub providers inside `<body>`; `HubSignedIn` shows pages only to signed-in people. The server reads the person's token from a cookie, so pages render signed in from the first paint.
|
|
35
35
|
- `src/routes/index.tsx`: your name, team and role, what the server sees, sign-out, and notes saved per person.
|
|
36
36
|
- `src/routes/facilitator.tsx`: a facilitator-only page.
|
|
37
37
|
- `convex/`: notes keyed on the person's hub id. Public functions check who is asking (`requireHubUser`, `requireFacilitator`) and call `convex/model/`.
|
|
38
38
|
|
|
39
39
|
## Commands
|
|
40
40
|
|
|
41
|
-
`bun run dev`, `bun run check` (Vite+: format, lint and type-check), `bun run fix` (the same, fixing what it can), `bun run typecheck`, `bun run build`, `bun run start` (production server), `just build`, `just push`.
|
|
41
|
+
`bun run dev`, `bun run setup`, `bunx user-hub --help` (the hub from the terminal: this event's people and apps), `bun run check` (Vite+: format, lint and type-check), `bun run fix` (the same, fixing what it can), `bun run typecheck`, `bun run build`, `bun run start` (production server), `just build`, `just push`.
|
|
42
42
|
|
|
43
|
-
Coding agents: see `CLAUDE.md` and the `user-hub-auth`
|
|
43
|
+
Coding agents: see `CLAUDE.md` and the `user-hub-auth` and `user-hub-cli` skills in `.claude/skills`.
|
package/template/dockerignore
CHANGED
package/template/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"private": true,
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
|
-
"setup": "
|
|
6
|
+
"setup": "user-hub setup",
|
|
7
7
|
"dev": "convex dev --start \"vp dev --port 3000\"",
|
|
8
8
|
"build": "vp build",
|
|
9
9
|
"start": "bun run server.ts",
|
|
@@ -18,12 +18,12 @@
|
|
|
18
18
|
"@fontsource-variable/oxanium": "^5.3.0",
|
|
19
19
|
"@tailwindcss/vite": "^4.3.5",
|
|
20
20
|
"@tanstack/react-router": "^1.170.46",
|
|
21
|
-
"@tanstack/react-start": "^1.169.
|
|
22
|
-
"@user-hub/auth": "^0.
|
|
21
|
+
"@tanstack/react-start": "^1.169.7",
|
|
22
|
+
"@user-hub/auth": "^0.6.0",
|
|
23
23
|
"class-variance-authority": "^0.7.1",
|
|
24
24
|
"cn": "^0.5.2",
|
|
25
25
|
"convex": "^1.51.0",
|
|
26
|
-
"lucide-react": "^1.
|
|
26
|
+
"lucide-react": "^1.76.0",
|
|
27
27
|
"react": "^19.3.1",
|
|
28
28
|
"react-dom": "^19.3.1",
|
|
29
29
|
"shadcn": "^4.23.0",
|
|
@@ -35,8 +35,9 @@
|
|
|
35
35
|
"@types/react": "^19.3.0",
|
|
36
36
|
"@types/react-dom": "^19.3.0",
|
|
37
37
|
"@vitejs/plugin-react": "^6.3.0",
|
|
38
|
-
"oxc-transform-react": "^0.
|
|
38
|
+
"oxc-transform-react": "^0.158.0",
|
|
39
39
|
"typescript": "^7.0.2",
|
|
40
|
+
"user-hub": "^0.1.0",
|
|
40
41
|
"vite": "npm:@voidzero-dev/vite-plus-core@8.3.8000",
|
|
41
42
|
"vite-plus": "1.7.0"
|
|
42
43
|
},
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { createRootRoute, HeadContent, Scripts } from "@tanstack/react-router";
|
|
2
|
+
import { createServerFn } from "@tanstack/react-start";
|
|
2
3
|
import {
|
|
3
4
|
ConvexHubAuthProvider,
|
|
4
5
|
HubSignedIn,
|
|
5
6
|
} from "@user-hub/auth/convex-react";
|
|
7
|
+
import { getHubToken } from "@user-hub/auth/start";
|
|
6
8
|
import { ConvexReactClient } from "convex/react";
|
|
7
9
|
|
|
8
10
|
import {
|
|
@@ -23,7 +25,17 @@ if (!CONVEX_URL) {
|
|
|
23
25
|
}
|
|
24
26
|
const convex = new ConvexReactClient(CONVEX_URL);
|
|
25
27
|
|
|
28
|
+
// The signed-in person's token from the request, so the server renders them
|
|
29
|
+
// instead of the loading state.
|
|
30
|
+
const fetchHubToken = createServerFn({ method: "GET" }).handler(() =>
|
|
31
|
+
getHubToken({ app: APP_KEY, hubUrl: import.meta.env.VITE_HUB_URL }),
|
|
32
|
+
);
|
|
33
|
+
|
|
26
34
|
export const Route = createRootRoute({
|
|
35
|
+
// On the server only: in the browser the provider already has the token.
|
|
36
|
+
beforeLoad: async () => ({
|
|
37
|
+
hubToken: import.meta.env.SSR ? await fetchHubToken() : null,
|
|
38
|
+
}),
|
|
27
39
|
head: () => ({
|
|
28
40
|
meta: [
|
|
29
41
|
{ charSet: "utf-8" },
|
|
@@ -38,6 +50,7 @@ export const Route = createRootRoute({
|
|
|
38
50
|
// The hub providers go inside <body>, never around <html>: they always render
|
|
39
51
|
// their children, and HubSignedIn decides what a visitor sees.
|
|
40
52
|
function RootDocument({ children }: { children: React.ReactNode }) {
|
|
53
|
+
const { hubToken } = Route.useRouteContext();
|
|
41
54
|
return (
|
|
42
55
|
<html lang="en" className="dark">
|
|
43
56
|
<head>
|
|
@@ -48,6 +61,7 @@ function RootDocument({ children }: { children: React.ReactNode }) {
|
|
|
48
61
|
client={convex}
|
|
49
62
|
app={APP_KEY}
|
|
50
63
|
hubUrl={import.meta.env.VITE_HUB_URL}
|
|
64
|
+
initialToken={hubToken}
|
|
51
65
|
>
|
|
52
66
|
<HubSignedIn signedOut={<SignedOut />} loading={<Loading />}>
|
|
53
67
|
{children}
|
|
@@ -1,586 +0,0 @@
|
|
|
1
|
-
// Setup: creates the Convex project, registers this app with User Hub in an
|
|
2
|
-
// event (a workshop) and sets the values that make Convex trust the hub's
|
|
3
|
-
// tokens.
|
|
4
|
-
// Run: bun run setup [--event <slug>] [--name <name>] [--prod <url>]
|
|
5
|
-
// [--dev <url>] [--listed|--unlisted]. Safe to run again; run it with --prod
|
|
6
|
-
// once the app is deployed so badge scans can reach it. --event picks the
|
|
7
|
-
// hub event the app belongs to (`bunx hub-user events` lists them); without
|
|
8
|
-
// it, the event saved in .env.local (HUB_EVENT) is used, and in a terminal
|
|
9
|
-
// setup asks for one the first time (or creates one). --name sets the app's
|
|
10
|
-
// name in the hub (default: the saved HUB_APP_NAME, else the package name).
|
|
11
|
-
// An event or name other than the saved ones registers the app anew, with a
|
|
12
|
-
// new key; the earlier registration is left as it was. --dev changes where the
|
|
13
|
-
// hub opens the app for developers (first registered as
|
|
14
|
-
// http://localhost:<the port in package.json's dev script>); later runs keep
|
|
15
|
-
// it. --unlisted keeps the app out of the hub's badge app list (new apps are
|
|
16
|
-
// listed), --listed puts it back; without either, the hub keeps the current
|
|
17
|
-
// setting.
|
|
18
|
-
// Needs HUB_URL and HUB_API_KEY in .env.local (Bun loads it), which
|
|
19
|
-
// `bunx hub-user login` saves; in a terminal, setup runs it when they are
|
|
20
|
-
// missing or the hub refuses the key.
|
|
21
|
-
|
|
22
|
-
import { existsSync, renameSync } from "node:fs";
|
|
23
|
-
import { chmod, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
24
|
-
|
|
25
|
-
const ENV_FILE = ".env.local";
|
|
26
|
-
const HUB_FILE = "convex/hub.ts";
|
|
27
|
-
const AUTH_CONFIG = "convex/auth.config.ts";
|
|
28
|
-
const AUTH_CONFIG_ASIDE = "convex/auth.config.ts.setup";
|
|
29
|
-
|
|
30
|
-
// The options that take a value, and the form to show when it is missing.
|
|
31
|
-
const VALUE_FLAGS = {
|
|
32
|
-
event: "--event <slug> (`bunx hub-user events` lists the slugs)",
|
|
33
|
-
name: '--name "<the app\'s name in the hub>"',
|
|
34
|
-
prod: "--prod https://your-app.example.com",
|
|
35
|
-
dev: "--dev http://localhost:<port>",
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
// The value after --flag on the command line, or undefined when the option
|
|
39
|
-
// is left out. Given without a value (nothing after it, or another flag), it
|
|
40
|
-
// stops setup: running on as if it were left out would quietly do something
|
|
41
|
-
// else, such as a listing-only update for `--prod --unlisted`.
|
|
42
|
-
function flag(name: keyof typeof VALUE_FLAGS) {
|
|
43
|
-
const at = process.argv.indexOf(`--${name}`);
|
|
44
|
-
if (at === -1) return undefined;
|
|
45
|
-
const value = process.argv[at + 1]?.trim();
|
|
46
|
-
if (!value || value.startsWith("--")) {
|
|
47
|
-
fail(`--${name} needs a value:
|
|
48
|
-
|
|
49
|
-
bun run setup ${VALUE_FLAGS[name]}`);
|
|
50
|
-
}
|
|
51
|
-
return value;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function step(message: string) {
|
|
55
|
-
console.log(`\n▸ ${message}`);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function fail(message: string): never {
|
|
59
|
-
console.error(`\n✗ ${message}`);
|
|
60
|
-
process.exit(1);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// capture keeps stdout; hub-user also keeps stderr, so its "run `bunx
|
|
64
|
-
// hub-user login`" hint can give way to setup's own message (setup runs the
|
|
65
|
-
// login itself) and other errors are printed as they are.
|
|
66
|
-
async function run(
|
|
67
|
-
command: string[],
|
|
68
|
-
{ capture = false, hubUser = false } = {},
|
|
69
|
-
) {
|
|
70
|
-
const child = Bun.spawn(command, {
|
|
71
|
-
stdin: "inherit",
|
|
72
|
-
stdout: capture ? "pipe" : "inherit",
|
|
73
|
-
stderr: hubUser ? "pipe" : "inherit",
|
|
74
|
-
env: process.env,
|
|
75
|
-
});
|
|
76
|
-
const [output, errors] = await Promise.all([
|
|
77
|
-
capture ? new Response(child.stdout).text() : "",
|
|
78
|
-
hubUser ? new Response(child.stderr).text() : "",
|
|
79
|
-
]);
|
|
80
|
-
const code = await child.exited;
|
|
81
|
-
return { ok: code === 0, code, output, errors };
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// hub-user from this app's node_modules; --no-install never fetches a package
|
|
85
|
-
// of that name from npm.
|
|
86
|
-
function hubUser(...args: string[]) {
|
|
87
|
-
return run(["bunx", "--no-install", "hub-user", ...args], {
|
|
88
|
-
capture: true,
|
|
89
|
-
hubUser: true,
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
async function readEnv() {
|
|
94
|
-
const file = Bun.file(ENV_FILE);
|
|
95
|
-
return (await file.exists()) ? await file.text() : "";
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// Set or replace KEY=value lines in .env.local, keeping every other line.
|
|
99
|
-
// Values that belong together (a registration's event and name) are saved in
|
|
100
|
-
// one write: the new content goes to a temporary file in the same folder
|
|
101
|
-
// (ignored by .env.* in .gitignore), which is then renamed over .env.local.
|
|
102
|
-
// A rename on the same filesystem replaces the file atomically, so a stopped
|
|
103
|
-
// run leaves the old file or the new one, never a mix. The renamed file keeps
|
|
104
|
-
// the temporary file's mode, so that gets .env.local's own mode first (600
|
|
105
|
-
// from hub-user login; 600 for a new file too, as it holds the API key).
|
|
106
|
-
async function writeEnv(values: Record<string, string>) {
|
|
107
|
-
const keys = Object.keys(values);
|
|
108
|
-
const lines = (await readEnv())
|
|
109
|
-
.split("\n")
|
|
110
|
-
.filter((line) => line && !keys.some((key) => line.startsWith(`${key}=`)));
|
|
111
|
-
const added = Object.entries(values).map(([key, value]) => `${key}=${value}`);
|
|
112
|
-
const mode = existsSync(ENV_FILE)
|
|
113
|
-
? (await stat(ENV_FILE)).mode & 0o777
|
|
114
|
-
: 0o600;
|
|
115
|
-
const temporary = `${ENV_FILE}.${process.pid}.tmp`;
|
|
116
|
-
try {
|
|
117
|
-
await writeFile(temporary, `${[...lines, ...added].join("\n")}\n`, {
|
|
118
|
-
mode,
|
|
119
|
-
});
|
|
120
|
-
// The umask can narrow the mode a new file gets; set it exactly.
|
|
121
|
-
await chmod(temporary, mode);
|
|
122
|
-
await rename(temporary, ENV_FILE);
|
|
123
|
-
} finally {
|
|
124
|
-
await rm(temporary, { force: true });
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
// Signs this folder in to the hub with `hub-user login` (an API key or the
|
|
129
|
-
// browser), then picks up the HUB_URL and HUB_API_KEY it saved, which this
|
|
130
|
-
// process read from .env.local before they were there.
|
|
131
|
-
async function signIn() {
|
|
132
|
-
step("Signing this folder in to User Hub");
|
|
133
|
-
const hub = process.env.HUB_URL;
|
|
134
|
-
const login = await run([
|
|
135
|
-
"bunx",
|
|
136
|
-
"--no-install",
|
|
137
|
-
"hub-user",
|
|
138
|
-
"login",
|
|
139
|
-
...(hub ? ["--hub", hub] : []),
|
|
140
|
-
]);
|
|
141
|
-
if (!login.ok) fail("Not signed in to the hub. Run bun run setup again.");
|
|
142
|
-
const saved = await readEnv();
|
|
143
|
-
for (const key of ["HUB_URL", "HUB_API_KEY"]) {
|
|
144
|
-
const value = new RegExp(`^${key}=(.*)$`, "m").exec(saved)?.[1];
|
|
145
|
-
if (value) process.env[key] = value;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// What to run when this folder is not signed in and nobody can be asked.
|
|
150
|
-
function loginHelp(problem: string, again = "bun run setup") {
|
|
151
|
-
const hub = process.env.HUB_URL ? "" : " --hub https://<your hub>";
|
|
152
|
-
return `${problem} Sign the folder in, then run ${again} again:
|
|
153
|
-
|
|
154
|
-
bunx hub-user login${hub}
|
|
155
|
-
|
|
156
|
-
It asks you to paste an API key from the hub's account page, or to approve
|
|
157
|
-
the sign-in in your browser. A coding agent runs it with --browser and shows
|
|
158
|
-
you the link to open.`;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
// hub-user, signing the folder in again once (in a terminal) when the hub
|
|
162
|
-
// refuses the API key: hub-user exits with 4 then.
|
|
163
|
-
let signedInAgain = false;
|
|
164
|
-
async function signedInHubUser(...args: string[]) {
|
|
165
|
-
for (;;) {
|
|
166
|
-
const result = await hubUser(...args);
|
|
167
|
-
if (result.code !== 4) return result;
|
|
168
|
-
if (!process.stdin.isTTY || signedInAgain) {
|
|
169
|
-
fail(loginHelp("The hub did not accept this folder's API key."));
|
|
170
|
-
}
|
|
171
|
-
console.log("\n The hub did not accept this folder's API key.");
|
|
172
|
-
await signIn();
|
|
173
|
-
signedInAgain = true;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
type HubEvent = {
|
|
178
|
-
slug: string;
|
|
179
|
-
name: string;
|
|
180
|
-
status: "active" | "ended";
|
|
181
|
-
timeZone: string;
|
|
182
|
-
};
|
|
183
|
-
|
|
184
|
-
// What to run when no event is known and nobody can be asked.
|
|
185
|
-
const EVENT_HELP = `Pass the hub event this app belongs to:
|
|
186
|
-
|
|
187
|
-
bun run setup --event <slug>
|
|
188
|
-
|
|
189
|
-
\`bunx hub-user events\` lists the events and their slugs.`;
|
|
190
|
-
|
|
191
|
-
// Creates an event, asking for its name and time zone. Undefined when the
|
|
192
|
-
// slug is taken, so the caller asks again.
|
|
193
|
-
async function createEvent(): Promise<HubEvent | undefined> {
|
|
194
|
-
const name = (prompt(" Name of the new event:") ?? "").trim();
|
|
195
|
-
if (!name) fail("No event name given. Run bun run setup again.");
|
|
196
|
-
// Asked rather than taken from this machine, which in a sandbox or CI is
|
|
197
|
-
// often UTC; the hub shows the event's schedule in this zone.
|
|
198
|
-
const here = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
199
|
-
let timeZone: string;
|
|
200
|
-
for (;;) {
|
|
201
|
-
timeZone =
|
|
202
|
-
(
|
|
203
|
-
prompt(
|
|
204
|
-
` The event's time zone, such as Europe/Berlin (Enter for ${here}):`,
|
|
205
|
-
) ?? ""
|
|
206
|
-
).trim() || here;
|
|
207
|
-
try {
|
|
208
|
-
new Intl.DateTimeFormat("en", { timeZone });
|
|
209
|
-
break;
|
|
210
|
-
} catch {
|
|
211
|
-
console.log(` "${timeZone}" is not a time zone. Use an IANA name.`);
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
const created = await signedInHubUser(
|
|
215
|
-
"events",
|
|
216
|
-
"create",
|
|
217
|
-
"--name",
|
|
218
|
-
name,
|
|
219
|
-
"--time-zone",
|
|
220
|
-
timeZone,
|
|
221
|
-
"--json",
|
|
222
|
-
);
|
|
223
|
-
// hub-user exits with 3 when an event with that slug exists.
|
|
224
|
-
if (created.code === 3) {
|
|
225
|
-
const { slug } = JSON.parse(created.output) as { slug?: string };
|
|
226
|
-
console.log(
|
|
227
|
-
`\n An event with the slug "${slug ?? name}" already exists. Pick it below, or give the new event another name.`,
|
|
228
|
-
);
|
|
229
|
-
return undefined;
|
|
230
|
-
}
|
|
231
|
-
if (!created.ok) {
|
|
232
|
-
process.stderr.write(created.errors);
|
|
233
|
-
fail("The hub did not create the event; see the error above.");
|
|
234
|
-
}
|
|
235
|
-
const event = JSON.parse(created.output) as HubEvent;
|
|
236
|
-
console.log(
|
|
237
|
-
` Created the event "${event.name}" (slug ${event.slug}, time zone ${event.timeZone})`,
|
|
238
|
-
);
|
|
239
|
-
return event;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
// Asks which of the hub's active events this app belongs to, or creates one.
|
|
243
|
-
async function chooseEvent(): Promise<HubEvent> {
|
|
244
|
-
for (;;) {
|
|
245
|
-
const listed = await signedInHubUser("events", "--json");
|
|
246
|
-
if (!listed.ok) {
|
|
247
|
-
process.stderr.write(listed.errors);
|
|
248
|
-
fail("Could not list the hub's events; see the error above.");
|
|
249
|
-
}
|
|
250
|
-
const active = (JSON.parse(listed.output) as HubEvent[]).filter(
|
|
251
|
-
(event) => event.status === "active",
|
|
252
|
-
);
|
|
253
|
-
if (active.length === 0) {
|
|
254
|
-
console.log(
|
|
255
|
-
"\n The hub has no active events yet. Create the one this app is for.",
|
|
256
|
-
);
|
|
257
|
-
const created = await createEvent();
|
|
258
|
-
if (created) return created;
|
|
259
|
-
continue;
|
|
260
|
-
}
|
|
261
|
-
console.log("\n Which hub event is this app for?");
|
|
262
|
-
for (const [index, event] of active.entries()) {
|
|
263
|
-
console.log(` ${index + 1}. ${event.name} (${event.slug})`);
|
|
264
|
-
}
|
|
265
|
-
console.log(` ${active.length + 1}. Create a new event`);
|
|
266
|
-
for (;;) {
|
|
267
|
-
const answer = prompt(` Number (1-${active.length + 1}):`);
|
|
268
|
-
if (answer === null) fail("No event chosen. Run bun run setup again.");
|
|
269
|
-
const choice = Number(answer.trim());
|
|
270
|
-
if (choice === active.length + 1) break;
|
|
271
|
-
const event = active[choice - 1];
|
|
272
|
-
if (Number.isInteger(choice) && event) return event;
|
|
273
|
-
console.log(` Type a number from 1 to ${active.length + 1}.`);
|
|
274
|
-
}
|
|
275
|
-
const created = await createEvent();
|
|
276
|
-
if (created) return created;
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
// Every option with a value, read before anything changes, so a missing value
|
|
281
|
-
// stops setup here.
|
|
282
|
-
const flags = {
|
|
283
|
-
event: flag("event"),
|
|
284
|
-
name: flag("name"),
|
|
285
|
-
prod: flag("prod"),
|
|
286
|
-
dev: flag("dev"),
|
|
287
|
-
};
|
|
288
|
-
|
|
289
|
-
// Put back an auth config left aside by an interrupted run.
|
|
290
|
-
if (existsSync(AUTH_CONFIG_ASIDE) && !existsSync(AUTH_CONFIG)) {
|
|
291
|
-
renameSync(AUTH_CONFIG_ASIDE, AUTH_CONFIG);
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
// hub-user comes with @user-hub/auth.
|
|
295
|
-
if (!existsSync("node_modules/@user-hub/auth")) {
|
|
296
|
-
fail("Install the dependencies first: bun install");
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
// What .env.local remembers about this app's own registration in the hub.
|
|
300
|
-
// HUB_EVENT and HUB_APP_NAME are written together, only after a successful
|
|
301
|
-
// registration, so together they name the app's own entry; the URLs are the
|
|
302
|
-
// ones last sent.
|
|
303
|
-
const saved = {
|
|
304
|
-
event: process.env.HUB_EVENT?.trim() || undefined,
|
|
305
|
-
name: process.env.HUB_APP_NAME?.trim() || undefined,
|
|
306
|
-
devUrl: process.env.HUB_APP_DEV_URL?.trim() || undefined,
|
|
307
|
-
prodUrl: process.env.HUB_APP_PROD_URL?.trim() || undefined,
|
|
308
|
-
};
|
|
309
|
-
|
|
310
|
-
// Without a terminal nobody can be asked for the event, so say that first,
|
|
311
|
-
// with the sign-in steps when the folder is not signed in either: one failure
|
|
312
|
-
// tells the caller everything. In a terminal the event is asked after the
|
|
313
|
-
// sign-in, since listing the events needs it.
|
|
314
|
-
if (!process.stdin.isTTY && !flags.event && !saved.event) {
|
|
315
|
-
if (!process.env.HUB_URL || !process.env.HUB_API_KEY) {
|
|
316
|
-
fail(`${loginHelp(
|
|
317
|
-
"This folder is not signed in to the hub, and the app is not in a hub event yet.",
|
|
318
|
-
"bun run setup --event <slug>",
|
|
319
|
-
)}
|
|
320
|
-
|
|
321
|
-
\`bunx hub-user events\` lists the events and their slugs once the folder is
|
|
322
|
-
signed in.`);
|
|
323
|
-
}
|
|
324
|
-
fail(`This app is not in a hub event yet. ${EVENT_HELP}`);
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
// Sign-in first: nothing below works without it, and a refused key is better
|
|
328
|
-
// found before the Convex project and the questions.
|
|
329
|
-
if (!process.env.HUB_URL || !process.env.HUB_API_KEY) {
|
|
330
|
-
if (!process.stdin.isTTY) {
|
|
331
|
-
fail(loginHelp("This folder is not signed in to the hub."));
|
|
332
|
-
}
|
|
333
|
-
await signIn();
|
|
334
|
-
} else {
|
|
335
|
-
// hub-user whoami exits with 4 when the hub refuses the key.
|
|
336
|
-
const whoami = await hubUser("whoami", "--json");
|
|
337
|
-
if (whoami.code === 4) {
|
|
338
|
-
if (!process.stdin.isTTY) {
|
|
339
|
-
fail(loginHelp("The hub did not accept this folder's API key."));
|
|
340
|
-
}
|
|
341
|
-
console.log("\n The hub did not accept this folder's API key.");
|
|
342
|
-
await signIn();
|
|
343
|
-
} else if (!whoami.ok) {
|
|
344
|
-
process.stderr.write(whoami.errors);
|
|
345
|
-
fail(
|
|
346
|
-
`Could not check this folder's sign-in with ${process.env.HUB_URL}; see the error above.`,
|
|
347
|
-
);
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
// The hub event (workshop) the app belongs to: --event, else the saved one,
|
|
352
|
-
// else asked here (a run without a terminal stopped above), before the Convex
|
|
353
|
-
// project is created.
|
|
354
|
-
let event = flags.event ?? saved.event;
|
|
355
|
-
if (!event) {
|
|
356
|
-
step("Choosing the hub event this app belongs to");
|
|
357
|
-
event = (await chooseEvent()).slug;
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
// 1. Convex project. Its first push happens before the hub values exist, so
|
|
361
|
-
// the auth config sits aside for that push only (Convex refuses an auth
|
|
362
|
-
// config whose env vars are unset) and comes back straight after.
|
|
363
|
-
if (!/^CONVEX_DEPLOYMENT=/m.test(await readEnv())) {
|
|
364
|
-
step("Creating your Convex project (sign in to Convex if asked)");
|
|
365
|
-
await rename(AUTH_CONFIG, AUTH_CONFIG_ASIDE);
|
|
366
|
-
const restore = () => {
|
|
367
|
-
if (existsSync(AUTH_CONFIG_ASIDE))
|
|
368
|
-
renameSync(AUTH_CONFIG_ASIDE, AUTH_CONFIG);
|
|
369
|
-
};
|
|
370
|
-
const onInterrupt = () => {
|
|
371
|
-
restore();
|
|
372
|
-
process.exit(130);
|
|
373
|
-
};
|
|
374
|
-
process.on("SIGINT", onInterrupt);
|
|
375
|
-
try {
|
|
376
|
-
await run(["bunx", "convex", "dev", "--once"]);
|
|
377
|
-
} finally {
|
|
378
|
-
process.off("SIGINT", onInterrupt);
|
|
379
|
-
restore();
|
|
380
|
-
}
|
|
381
|
-
if (!/^CONVEX_DEPLOYMENT=/m.test(await readEnv())) {
|
|
382
|
-
fail("No Convex project was created. Run bun run setup again.");
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
// 2. Register with the hub, in the event. An app's key comes from its event
|
|
387
|
-
// and its name, so only the saved pair is this app's own registration,
|
|
388
|
-
// which later runs update. Any other pair (another event, another name) is
|
|
389
|
-
// a new app: registered with --new, so it never takes over another app of
|
|
390
|
-
// that name (if the name is taken, ask for another one), and with its URLs.
|
|
391
|
-
// Setup never removes the earlier registration.
|
|
392
|
-
step("Registering this app with User Hub");
|
|
393
|
-
const packageJson = (await Bun.file("package.json").json()) as {
|
|
394
|
-
name: string;
|
|
395
|
-
scripts: { dev?: string };
|
|
396
|
-
};
|
|
397
|
-
let appName = flags.name ?? saved.name ?? packageJson.name;
|
|
398
|
-
// Read again on each attempt: the event and the name can change below.
|
|
399
|
-
const isOwnRegistration = () => event === saved.event && appName === saved.name;
|
|
400
|
-
|
|
401
|
-
// Where badge scans send people. Asked once in a terminal; skipped until the
|
|
402
|
-
// app is deployed, then added with --prod. HUB_APP_PROD_URL remembers it.
|
|
403
|
-
let prodUrl = flags.prod;
|
|
404
|
-
if (!prodUrl && !saved.prodUrl && process.stdin.isTTY) {
|
|
405
|
-
prodUrl =
|
|
406
|
-
prompt(
|
|
407
|
-
" Production URL, where badge scans send people (Enter to skip until you deploy):",
|
|
408
|
-
)?.trim() || undefined;
|
|
409
|
-
}
|
|
410
|
-
// Where the hub opens the app for developers: first
|
|
411
|
-
// http://localhost:<port of the dev script>, then whatever --dev sets.
|
|
412
|
-
const devPort =
|
|
413
|
-
/--port (\d+)/.exec(packageJson.scripts.dev ?? "")?.[1] ?? "3000";
|
|
414
|
-
const devFlag = flags.dev;
|
|
415
|
-
// The URLs to send. The app's own registration gets only the ones given now
|
|
416
|
-
// (the hub keeps the rest); a new app starts without URLs, so it also gets
|
|
417
|
-
// the saved ones, or the default development URL.
|
|
418
|
-
function urlsToSend() {
|
|
419
|
-
if (isOwnRegistration()) return { dev: devFlag, prod: prodUrl };
|
|
420
|
-
return {
|
|
421
|
-
dev: devFlag ?? saved.devUrl ?? `http://localhost:${devPort}`,
|
|
422
|
-
prod: prodUrl ?? saved.prodUrl,
|
|
423
|
-
};
|
|
424
|
-
}
|
|
425
|
-
// Passed on to hub-user as is; without either the hub keeps its setting.
|
|
426
|
-
const listing = ["--listed", "--unlisted"].filter((arg) =>
|
|
427
|
-
process.argv.includes(arg),
|
|
428
|
-
);
|
|
429
|
-
if (listing.length > 1) fail("Pass --listed or --unlisted, not both.");
|
|
430
|
-
let sent: ReturnType<typeof urlsToSend>;
|
|
431
|
-
let registered: Awaited<ReturnType<typeof run>>;
|
|
432
|
-
for (;;) {
|
|
433
|
-
sent = urlsToSend();
|
|
434
|
-
registered = await signedInHubUser(
|
|
435
|
-
"register",
|
|
436
|
-
"--event",
|
|
437
|
-
event,
|
|
438
|
-
"--name",
|
|
439
|
-
appName,
|
|
440
|
-
...(sent.dev ? ["--dev", sent.dev] : []),
|
|
441
|
-
...(sent.prod ? ["--prod", sent.prod] : []),
|
|
442
|
-
...listing,
|
|
443
|
-
"--json",
|
|
444
|
-
...(isOwnRegistration() ? [] : ["--new"]),
|
|
445
|
-
);
|
|
446
|
-
// hub-user exits with 5 when no event has that slug, for example a saved
|
|
447
|
-
// event that was deleted from the hub.
|
|
448
|
-
if (registered.code === 5) {
|
|
449
|
-
const gone = `The hub has no event with the slug "${event}"${event === saved.event ? " (saved in .env.local as HUB_EVENT; it may have been deleted)" : ""}.`;
|
|
450
|
-
if (!process.stdin.isTTY) fail(`${gone} ${EVENT_HELP}`);
|
|
451
|
-
console.log(`\n ${gone}`);
|
|
452
|
-
event = (await chooseEvent()).slug;
|
|
453
|
-
continue;
|
|
454
|
-
}
|
|
455
|
-
// hub-user exits with 3 when --new finds the name taken, or when an app of
|
|
456
|
-
// another event has the same key.
|
|
457
|
-
if (registered.code !== 3) break;
|
|
458
|
-
const { error } = JSON.parse(registered.output) as { error?: string };
|
|
459
|
-
console.log(
|
|
460
|
-
`\n ${error ?? `An app called "${appName}" already exists in the hub.`}`,
|
|
461
|
-
);
|
|
462
|
-
if (!process.stdin.isTTY) {
|
|
463
|
-
fail("Choose another name: bun run setup --name <new name>");
|
|
464
|
-
}
|
|
465
|
-
appName = (prompt(" Choose another name for this app:") ?? "").trim();
|
|
466
|
-
if (!appName) fail("No name given. Run bun run setup again.");
|
|
467
|
-
}
|
|
468
|
-
if (!registered.ok) {
|
|
469
|
-
process.stderr.write(registered.errors);
|
|
470
|
-
fail(
|
|
471
|
-
sent.prod
|
|
472
|
-
? "The hub refused the registration; see the error above. Check HUB_URL and that the production URL is a full https:// address."
|
|
473
|
-
: "The hub refused the registration; see the error above. Check HUB_URL.",
|
|
474
|
-
);
|
|
475
|
-
}
|
|
476
|
-
const {
|
|
477
|
-
key,
|
|
478
|
-
hubUrl,
|
|
479
|
-
env,
|
|
480
|
-
listed,
|
|
481
|
-
event: registeredIn,
|
|
482
|
-
} = JSON.parse(registered.output) as {
|
|
483
|
-
key: string;
|
|
484
|
-
hubUrl: string;
|
|
485
|
-
event: { slug: string; name: string };
|
|
486
|
-
env: { HUB_ISSUER: string; HUB_JWKS: string };
|
|
487
|
-
// The stored setting; missing from hubs without the badge app list.
|
|
488
|
-
listed?: boolean;
|
|
489
|
-
};
|
|
490
|
-
// One write: a stopped run must never leave the new event with the old name,
|
|
491
|
-
// which the next run would take for this app's own registration.
|
|
492
|
-
await writeEnv({
|
|
493
|
-
HUB_EVENT: event,
|
|
494
|
-
HUB_APP_NAME: appName,
|
|
495
|
-
...(sent.prod && { HUB_APP_PROD_URL: sent.prod }),
|
|
496
|
-
...(sent.dev && { HUB_APP_DEV_URL: sent.dev }),
|
|
497
|
-
});
|
|
498
|
-
// What the hub now has; a URL not sent is the registered one, if known here.
|
|
499
|
-
const devUrl = sent.dev ?? saved.devUrl;
|
|
500
|
-
const production = sent.prod ?? saved.prodUrl;
|
|
501
|
-
console.log(
|
|
502
|
-
` Registered as "${appName}" in the event "${registeredIn.name}" (${registeredIn.slug}), app key: ${key}`,
|
|
503
|
-
);
|
|
504
|
-
if (saved.event && saved.name && !isOwnRegistration()) {
|
|
505
|
-
console.log(
|
|
506
|
-
` This is a new registration; setup left the earlier one ("${saved.name}" in the event "${saved.event}") as it was.`,
|
|
507
|
-
);
|
|
508
|
-
}
|
|
509
|
-
console.log(
|
|
510
|
-
` Development URL: ${devUrl ?? "the one registered in the hub"}${sent.dev ? "" : " (kept; bun run setup --dev <url> changes it)"}`,
|
|
511
|
-
);
|
|
512
|
-
// bun run dev must serve where the hub sends developers back to.
|
|
513
|
-
const devUrlPort = devUrl ? URL.parse(devUrl)?.port : undefined;
|
|
514
|
-
if (devUrlPort && devUrlPort !== devPort) {
|
|
515
|
-
console.log(
|
|
516
|
-
` Note: bun run dev serves port ${devPort} (package.json), not ${devUrlPort}. Change one so they match.`,
|
|
517
|
-
);
|
|
518
|
-
}
|
|
519
|
-
if (listed !== undefined) {
|
|
520
|
-
console.log(
|
|
521
|
-
listed
|
|
522
|
-
? ` Listed in the hub's badge app list${production ? "" : " once it has a production URL"} (bun run setup --unlisted hides it)`
|
|
523
|
-
: " Not in the hub's badge app list (bun run setup --listed adds it)",
|
|
524
|
-
);
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
// The key follows the registered name; keep the code in step with it.
|
|
528
|
-
const hubFile = await Bun.file(HUB_FILE).text();
|
|
529
|
-
const updated = hubFile.replace(/APP_KEY = ".*?"/, `APP_KEY = "${key}"`);
|
|
530
|
-
if (updated !== hubFile) {
|
|
531
|
-
await Bun.write(HUB_FILE, updated);
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
// 3. Values Convex and the frontend need.
|
|
535
|
-
step("Setting HUB_ISSUER and HUB_JWKS on Convex");
|
|
536
|
-
for (const [variable, value] of Object.entries(env)) {
|
|
537
|
-
if (!(await run(["bunx", "convex", "env", "set", variable, value])).ok) {
|
|
538
|
-
fail(`Could not set ${variable} on Convex.`);
|
|
539
|
-
}
|
|
540
|
-
}
|
|
541
|
-
await writeEnv({ VITE_HUB_URL: hubUrl });
|
|
542
|
-
|
|
543
|
-
// The production Convex deployment needs the same two values once the app is
|
|
544
|
-
// deployed. It may not exist yet: then say how to set them later.
|
|
545
|
-
let productionEnvSet = false;
|
|
546
|
-
if (production) {
|
|
547
|
-
step("Setting HUB_ISSUER and HUB_JWKS on the production Convex deployment");
|
|
548
|
-
productionEnvSet = true;
|
|
549
|
-
for (const [variable, value] of Object.entries(env)) {
|
|
550
|
-
const result = await run(
|
|
551
|
-
["bunx", "convex", "env", "set", "--prod", variable, value],
|
|
552
|
-
{ capture: true },
|
|
553
|
-
);
|
|
554
|
-
productionEnvSet &&= result.ok;
|
|
555
|
-
}
|
|
556
|
-
}
|
|
557
|
-
|
|
558
|
-
// 4. Push the functions now that auth is configured.
|
|
559
|
-
step("Deploying the Convex functions");
|
|
560
|
-
if (!(await run(["bunx", "convex", "dev", "--once"])).ok) {
|
|
561
|
-
fail("Convex did not accept the functions; see the error above.");
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
const productionNote = production
|
|
565
|
-
? productionEnvSet
|
|
566
|
-
? `Production: ${production}. Badge scans reach it once the app is scheduled
|
|
567
|
-
or forced live in the hub${listed ? ", or straight away while badges go to the app list" : ""}.
|
|
568
|
-
Deploy Convex with: bunx convex deploy`
|
|
569
|
-
: `Production: ${production} is registered with the hub, but HUB_ISSUER
|
|
570
|
-
and HUB_JWKS could not be set on the production Convex deployment (see the
|
|
571
|
-
error above). Run bun run setup again before \`bunx convex deploy\`.`
|
|
572
|
-
: `No production URL yet, so badge scans cannot reach this app. Once it is
|
|
573
|
-
deployed, run:
|
|
574
|
-
|
|
575
|
-
bun run setup --prod https://your-app.example.com
|
|
576
|
-
|
|
577
|
-
(or ask your coding agent to set up the production URL).`;
|
|
578
|
-
|
|
579
|
-
console.log(`
|
|
580
|
-
✓ Ready. Start the app:
|
|
581
|
-
|
|
582
|
-
bun run dev
|
|
583
|
-
|
|
584
|
-
Open ${devUrl ?? "the development URL registered in the hub"}: you are sent to the hub to pick who to be, then back signed in.
|
|
585
|
-
|
|
586
|
-
${productionNote}`);
|