@wwchen/stack 0.3.0 → 0.4.0

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 CHANGED
@@ -58,8 +58,8 @@ The callback lands on `next` with `?connection=<provider>&result=<code>`, where
58
58
  | `stack neon reset-preview [--set-vercel]` | re-cuts the shared `preview` branch from `seed`; `--set-vercel` writes its URLs as Vercel's sensitive Preview `DATABASE_URL`/`DATABASE_URL_UNPOOLED` |
59
59
  | `stack vercel-build` | Vercel's build command: migrate the deployment's database, then `next build` |
60
60
  | `stack doctor` | read-only audit of the linked Vercel project (framework, Neon integration, shared secrets, database variables); exits 1 on an error |
61
- | `stack user:create <email> [--admin]` | adds a user through the app's `src/lib/auth.ts` |
62
- | `stack user:role <email> <admin\|user>` | changes a user's role through the app's `src/lib/auth.ts`; never leaves the app without an admin |
61
+ | `stack user:create <email> [--admin] [--preview]` | adds a user through the app's `src/lib/auth.ts`; `--preview` adds your own email to the shared `preview` branch so you can sign in on a preview (`docs/runbook.md`, "Signing in on a preview") |
62
+ | `stack user:role <email> <admin\|user> [--preview]` | changes a user's role through the app's `src/lib/auth.ts`; never leaves the app without an admin |
63
63
  | `stack sync-blocks [--check]` | refreshes the stack blocks in `AGENTS.md`, `CLAUDE.md`, `.gitignore`, `.env.example` |
64
64
 
65
65
  `NEON_PROJECT_ID` comes from the environment; nothing in the package names a project or domain.
package/blocks/AGENTS.md CHANGED
@@ -16,6 +16,7 @@
16
16
  | Migrate | `pnpm db:migrate` (your dev branch) |
17
17
  | Rebuild the `seed` branch (fake data; every dev and preview branch is cut from it) | `pnpm exec stack neon seed` |
18
18
  | Add a user | `stack user:create <email> [--admin]` (on your dev branch; production: the runbook's "First admin") |
19
+ | Sign in on a preview | `stack user:create <your email> --admin --preview` (your own email only; `stack neon reset-preview` drops it) |
19
20
  | Change a user's role | `stack user:role <email> <admin\|user>` |
20
21
  | Check the Vercel project's setup | `stack doctor` (read-only; exits 1 on an error) |
21
22
  | Refresh the stack blocks in this file | `stack sync-blocks` (`--check` in CI) |
package/blocks/CLAUDE.md CHANGED
@@ -18,6 +18,7 @@
18
18
  | Reset the shared preview branch | `stack neon reset-preview` (`--set-vercel` also writes its URLs into Vercel's Preview variables) |
19
19
  | Check the Vercel project's setup | `stack doctor` (read-only; exits 1 on an error) |
20
20
  | Add a user / an admin | `stack user:create <email> [--admin]` (on your dev branch; production: the runbook's "First admin") |
21
+ | Sign in on a preview | `stack user:create <your email> --admin --preview` (your own email only; `stack neon reset-preview` drops it) |
21
22
  | Change a user's role | `stack user:role <email> <admin\|user>` (keeps at least one admin) |
22
23
  | Refresh the stack blocks in this file | `stack sync-blocks` (`--check` in CI) |
23
24
 
package/dist/cli/index.js CHANGED
@@ -20,8 +20,9 @@ const USAGE = `usage: stack <command>
20
20
  --set-vercel writes its URLs into Vercel's Preview variables
21
21
  vercel-build Vercel's build command: migrate, then next build
22
22
  doctor audit the linked Vercel project's setup (read-only)
23
- user:create <email> [--admin] add a user through the app's auth
24
- user:role <email> <admin|user> change a user's role through the app's auth
23
+ user:create <email> [--admin] add a user through the app's auth; --preview: on the shared
24
+ preview branch (your own email only, to sign in on a preview)
25
+ user:role <email> <admin|user> change a user's role through the app's auth; --preview as above
25
26
  sync-blocks [--check] refresh the stack-owned blocks in AGENTS.md, CLAUDE.md, .gitignore, .env.example`;
26
27
  /** The testable entry: every outside effect arrives through `deps`; the exit code comes back. */
27
28
  export async function runCli(argv, deps) {
@@ -44,6 +44,10 @@ export declare function withNeon(cli: Cli, [command, ...args]: string[]): Promis
44
44
  * DATABASE_URL or ENV_FILE already says where the database is. Refuses the default branch.
45
45
  */
46
46
  export declare function devBranchEnv(cli: Cli): Promise<Record<string, string>>;
47
+ /**
48
+ * The shared `preview` branch's `branchEnv`, for `user:create --preview` and `user:role --preview`: previews hold only `db/seed.ts`'s fake users, so this is how a developer adds their own email to sign in on one. `--preview` names the database, so DATABASE_URL or ENV_FILE set as well is refused rather than overridden, as is a `preview` that is the default branch or answers with production's endpoint.
49
+ */
50
+ export declare function previewBranchEnv(cli: Cli): Promise<Record<string, string>>;
47
51
  /**
48
52
  * `stack dev-setup` — once per developer: makes your Neon dev branch from `seed` (never from
49
53
  * production) if it does not exist yet (`dev/<os user>`, or NEON_DEV_BRANCH) and migrates it. Safe
package/dist/cli/neon.js CHANGED
@@ -148,6 +148,29 @@ export async function devBranchEnv(cli) {
148
148
  cli.error(`[neon] ${name}`);
149
149
  return env;
150
150
  }
151
+ /**
152
+ * The shared `preview` branch's `branchEnv`, for `user:create --preview` and `user:role --preview`: previews hold only `db/seed.ts`'s fake users, so this is how a developer adds their own email to sign in on one. `--preview` names the database, so DATABASE_URL or ENV_FILE set as well is refused rather than overridden, as is a `preview` that is the default branch or answers with production's endpoint.
153
+ */
154
+ export async function previewBranchEnv(cli) {
155
+ if (cli.env.DATABASE_URL || cli.env.ENV_FILE) {
156
+ throw new CliError("--preview names the database itself; unset DATABASE_URL and ENV_FILE.");
157
+ }
158
+ const branches = await listBranches(cli);
159
+ const production = defaultBranch(branches);
160
+ const branch = branches.find((b) => b.name === PREVIEW);
161
+ if (!branch) {
162
+ throw new CliError(`no ${PREVIEW} branch yet; run \`stack neon reset-preview\` first.`);
163
+ }
164
+ const env = await branchEnv(cli, branch.id, production);
165
+ const productionEndpoint = endpointOf(env.PRODUCTION_DATABASE_HOST);
166
+ if (branch.default ||
167
+ endpointOf(env.DATABASE_URL) === productionEndpoint ||
168
+ endpointOf(env.DATABASE_URL_UNPOOLED) === productionEndpoint) {
169
+ throw new CliError(`${PREVIEW} is production's database; refusing to change it.`);
170
+ }
171
+ cli.error(`[neon] ${PREVIEW}`);
172
+ return env;
173
+ }
151
174
  /**
152
175
  * `stack dev-setup` — once per developer: makes your Neon dev branch from `seed` (never from
153
176
  * production) if it does not exist yet (`dev/<os user>`, or NEON_DEV_BRANCH) and migrates it. Safe
@@ -1,10 +1,11 @@
1
1
  import { CliError, importApp, loadAppEnv } from "./context.js";
2
- import { devBranchEnv } from "./neon.js";
2
+ import { devBranchEnv, PREVIEW, previewBranchEnv } from "./neon.js";
3
+ const PREVIEW_FLAG = "--preview";
3
4
  /**
4
- * The app's `auth.users`, with the database every command here uses: the dev branch's connection (when nothing else names a database) wins over `.env`, which fills in only what is still unset. The app's modules read process.env, so this sets it before importing them.
5
+ * The app's `auth.users`, with the database every command here uses: the dev branch's connection (when nothing else names a database), or the `preview` branch's with `--preview`, wins over `.env`, which fills in only what is still unset. The app's modules read process.env, so this sets it before importing them.
5
6
  */
6
- async function appUsers(cli) {
7
- Object.assign(cli.env, await devBranchEnv(cli));
7
+ async function appUsers(cli, preview) {
8
+ Object.assign(cli.env, preview ? await previewBranchEnv(cli) : await devBranchEnv(cli));
8
9
  loadAppEnv(cli);
9
10
  const auth = (await importApp(cli, "src/lib/auth.ts")).auth;
10
11
  if (!auth?.users)
@@ -13,17 +14,20 @@ async function appUsers(cli) {
13
14
  }
14
15
  export async function userCreate(cli, args) {
15
16
  const admin = args.includes("--admin");
16
- const [email] = args.filter((arg) => arg !== "--admin");
17
- if (!email) {
18
- cli.error("usage: stack user:create <email> [--admin]");
17
+ const preview = args.includes(PREVIEW_FLAG);
18
+ const [email, ...rest] = args.filter((arg) => arg !== "--admin" && arg !== PREVIEW_FLAG);
19
+ if (!email || rest.length > 0) {
20
+ cli.error("usage: stack user:create <email> [--admin] [--preview]");
19
21
  return 2;
20
22
  }
21
- const users = await appUsers(cli);
23
+ const users = await appUsers(cli, preview);
22
24
  if (!users.add)
23
25
  throw new CliError("src/lib/auth.ts must export `auth` (a StackAuth).");
24
26
  try {
25
27
  const user = await users.add(email, admin ? "admin" : "user");
26
- cli.log(`created ${user.email}${admin ? " as an admin" : ""}. They sign in with a magic link or Google.`);
28
+ cli.log(preview
29
+ ? `created ${user.email}${admin ? " as an admin" : ""} on ${PREVIEW}: sign in on a preview with a magic link. \`stack neon reset-preview\` drops it.`
30
+ : `created ${user.email}${admin ? " as an admin" : ""}. They sign in with a magic link or Google.`);
27
31
  return 0;
28
32
  }
29
33
  catch (error) {
@@ -39,12 +43,13 @@ const isRole = (value) => ROLES.includes(value);
39
43
  * `stack user:role <email> <admin|user>`. The email is matched as it is stored (trimmed, lower-cased). The rule that the app always keeps an admin is `setRole`'s, enforced in its own transaction: its refusal is reported as is, and nothing changes.
40
44
  */
41
45
  export async function userRole(cli, args) {
42
- const [email, role, ...rest] = args;
46
+ const preview = args.includes(PREVIEW_FLAG);
47
+ const [email, role, ...rest] = args.filter((arg) => arg !== PREVIEW_FLAG);
43
48
  if (!email || !isRole(role) || rest.length > 0) {
44
- cli.error("usage: stack user:role <email> <admin|user>");
49
+ cli.error("usage: stack user:role <email> <admin|user> [--preview]");
45
50
  return 2;
46
51
  }
47
- const users = await appUsers(cli);
52
+ const users = await appUsers(cli, preview);
48
53
  if (!users.list || !users.setRole) {
49
54
  throw new CliError("src/lib/auth.ts must export `auth` (a StackAuth).");
50
55
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wwchen/stack",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Shared Next.js, Neon, Better Auth, Resend and lew-ds building blocks.",
5
5
  "license": "MIT",
6
6
  "repository": {