@wwchen/stack 0.10.1 → 0.11.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.
Files changed (71) hide show
  1. package/README.md +42 -25
  2. package/blocks/AGENTS.md +8 -20
  3. package/blocks/CLAUDE.md +9 -27
  4. package/blocks/env.example +1 -7
  5. package/blocks/gitignore +0 -6
  6. package/dist/cli/builtin.d.ts +4 -3
  7. package/dist/cli/builtin.js +31 -36
  8. package/dist/cli/check.d.ts +5 -0
  9. package/dist/cli/check.js +39 -0
  10. package/dist/cli/context.d.ts +1 -18
  11. package/dist/cli/context.js +0 -22
  12. package/dist/cli/discover.d.ts +15 -0
  13. package/dist/cli/discover.js +156 -0
  14. package/dist/cli/generate.d.ts +6 -0
  15. package/dist/cli/generate.js +94 -0
  16. package/dist/cli/host.d.ts +1 -1
  17. package/dist/cli/host.js +90 -72
  18. package/dist/cli/owned.d.ts +33 -0
  19. package/dist/cli/owned.js +151 -0
  20. package/dist/cli/{sync-blocks.d.ts → sync.d.ts} +6 -2
  21. package/dist/cli/{sync-blocks.js → sync.js} +46 -18
  22. package/dist/configs/vitest.d.ts +3 -1
  23. package/dist/configs/vitest.js +8 -6
  24. package/dist/index.d.ts +2 -2
  25. package/dist/index.js +1 -1
  26. package/dist/next/config.d.ts +2 -0
  27. package/dist/next/config.js +10 -6
  28. package/dist/next/health.d.ts +1 -1
  29. package/dist/next/health.js +1 -1
  30. package/dist/stack/contracts.d.ts +65 -15
  31. package/dist/stack/contracts.js +30 -6
  32. package/files/.claude/rules/stack-ui.md +24 -0
  33. package/files/next.config.ts +6 -0
  34. package/files/src/app/api/health/route.ts +9 -0
  35. package/package.json +6 -37
  36. package/dist/cli/db.d.ts +0 -4
  37. package/dist/cli/db.js +0 -43
  38. package/dist/cli/dev.d.ts +0 -4
  39. package/dist/cli/dev.js +0 -70
  40. package/dist/cli/neon.d.ts +0 -79
  41. package/dist/cli/neon.js +0 -394
  42. package/dist/cli/run.d.ts +0 -2
  43. package/dist/cli/run.js +0 -29
  44. package/dist/db/branches.d.ts +0 -11
  45. package/dist/db/branches.js +0 -19
  46. package/dist/db/doctor.d.ts +0 -9
  47. package/dist/db/doctor.js +0 -120
  48. package/dist/db/env.d.ts +0 -14
  49. package/dist/db/env.js +0 -15
  50. package/dist/db/explaining-pool.d.ts +0 -11
  51. package/dist/db/explaining-pool.js +0 -55
  52. package/dist/db/health.d.ts +0 -10
  53. package/dist/db/health.js +0 -26
  54. package/dist/db/index.d.ts +0 -21
  55. package/dist/db/index.js +0 -32
  56. package/dist/db/migration-status.d.ts +0 -5
  57. package/dist/db/migration-status.js +0 -38
  58. package/dist/db/schema-changes.d.ts +0 -13
  59. package/dist/db/schema-changes.js +0 -99
  60. package/dist/db/ssl.d.ts +0 -4
  61. package/dist/db/ssl.js +0 -17
  62. package/dist/db/target.d.ts +0 -19
  63. package/dist/db/target.js +0 -66
  64. package/dist/db/testing/index.d.ts +0 -23
  65. package/dist/db/testing/index.js +0 -67
  66. package/dist/db/testing/isolate.d.ts +0 -1
  67. package/dist/db/testing/isolate.js +0 -3
  68. package/dist/db/testing/pglite.d.ts +0 -20
  69. package/dist/db/testing/pglite.js +0 -79
  70. package/dist/env/testing.d.ts +0 -6
  71. package/dist/env/testing.js +0 -13
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@wwchen/stack",
3
- "version": "0.10.1",
4
- "description": "Shared Next.js, Neon and lew-ds building blocks, and the stack CLI that composes an app's packages.",
3
+ "version": "0.11.0",
4
+ "description": "Shared Next.js and lew-ds building blocks, and the stack CLI that composes an app's packages.",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
@@ -15,7 +15,8 @@
15
15
  "files": [
16
16
  "dist",
17
17
  "configs",
18
- "blocks"
18
+ "blocks",
19
+ "files"
19
20
  ],
20
21
  "bin": {
21
22
  "stack": "dist/cli/index.js"
@@ -32,26 +33,6 @@
32
33
  "types": "./dist/env/index.d.ts",
33
34
  "default": "./dist/env/index.js"
34
35
  },
35
- "./db": {
36
- "types": "./dist/db/index.d.ts",
37
- "default": "./dist/db/index.js"
38
- },
39
- "./db/env": {
40
- "types": "./dist/db/env.d.ts",
41
- "default": "./dist/db/env.js"
42
- },
43
- "./db/branches": {
44
- "types": "./dist/db/branches.d.ts",
45
- "default": "./dist/db/branches.js"
46
- },
47
- "./db/testing": {
48
- "types": "./dist/db/testing/index.d.ts",
49
- "default": "./dist/db/testing/index.js"
50
- },
51
- "./db/testing/isolate": {
52
- "types": "./dist/db/testing/isolate.d.ts",
53
- "default": "./dist/db/testing/isolate.js"
54
- },
55
36
  "./http": {
56
37
  "types": "./dist/http/index.d.ts",
57
38
  "default": "./dist/http/index.js"
@@ -103,18 +84,13 @@
103
84
  "./blocks/*": "./blocks/*"
104
85
  },
105
86
  "dependencies": {
106
- "@vercel/functions": "3.12.0",
107
- "pg": "8.23.1",
108
87
  "zod": "4.6.7"
109
88
  },
110
89
  "peerDependencies": {
111
- "@electric-sql/pglite": "0.5.10",
112
- "@electric-sql/pglite-socket": "0.2.13",
113
90
  "@lew-ds/lds": "1.3.0",
114
91
  "@lew-ds/lds-react": "2.1.0",
115
92
  "@lew-ds/open-icons": "1.1.0",
116
93
  "@storybook/nextjs-vite": "10.6.2",
117
- "drizzle-kit": "0.31.11",
118
94
  "drizzle-orm": "0.45.4",
119
95
  "next": "16.4.6",
120
96
  "react": "19.3.1",
@@ -123,16 +99,10 @@
123
99
  "typescript": "~6.0.3"
124
100
  },
125
101
  "peerDependenciesMeta": {
126
- "@electric-sql/pglite": {
127
- "optional": true
128
- },
129
- "@electric-sql/pglite-socket": {
130
- "optional": true
131
- },
132
102
  "@storybook/nextjs-vite": {
133
103
  "optional": true
134
104
  },
135
- "drizzle-kit": {
105
+ "drizzle-orm": {
136
106
  "optional": true
137
107
  },
138
108
  "storybook": {
@@ -141,13 +111,12 @@
141
111
  },
142
112
  "devDependencies": {
143
113
  "@electric-sql/pglite": "0.5.10",
144
- "@electric-sql/pglite-socket": "0.2.13",
145
114
  "@storybook/nextjs-vite": "10.6.2",
146
115
  "@types/node": "^24.19.2",
147
- "@types/pg": "^8.23.1",
148
116
  "@types/react": "19.3.0",
149
117
  "@types/react-dom": "19.3.0",
150
118
  "drizzle-kit": "0.31.11",
119
+ "drizzle-orm": "0.45.4",
151
120
  "storybook": "10.6.2",
152
121
  "tailwindcss": "4.3.3",
153
122
  "typescript": "~6.0.3",
package/dist/cli/db.d.ts DELETED
@@ -1,4 +0,0 @@
1
- import { type Cli } from "./context.ts";
2
- export declare function dbCheck(cli: Cli): Promise<number>;
3
- export declare function dbGenerate(cli: Cli, [name, ...flags]: string[]): Promise<number>;
4
- export declare function dbUpgrade(cli: Cli): Promise<number>;
package/dist/cli/db.js DELETED
@@ -1,43 +0,0 @@
1
- // `stack db:check` — fails when the app's `src/db/schema.ts` has changes `drizzle/` has no migration for: a package table that changed in an upgrade, or the app's own, never generated. App CI runs it.
2
- //
3
- // `stack db:generate <name> [drizzle-kit flags]` — the app's `pnpm db:generate`: drizzle-kit generate with a name the person chose, so `drizzle/` says what each migration does instead of drizzle-kit's random words. Flags after the name pass through (`--custom` for an empty migration to write by hand).
4
- //
5
- // `stack db:upgrade` — after the stack is upgraded, generates the migration for its tables' changes (`stack_<version>`), so the app's one history carries them. Copier runs it after `pnpm install`.
6
- //
7
- // Both run in the app's root and import its schema with Node's type stripping, so it must use relative `.ts` imports.
8
- import { join } from "node:path";
9
- import { pendingStatements } from "../db/schema-changes.js";
10
- import { importApp, STACK_PACKAGE } from "./context.js";
11
- async function pending(cli) {
12
- const schema = await importApp(cli, "src/db/schema.ts");
13
- return pendingStatements(schema, join(cli.cwd, "drizzle"));
14
- }
15
- export async function dbCheck(cli) {
16
- const statements = await pending(cli);
17
- if (statements.length === 0)
18
- return 0;
19
- cli.error(`src/db/schema.ts has changes drizzle/ has no migration for:\n\n${statements.join("\n")}\n\nRun \`pnpm db:generate <name>\`, review the SQL and commit it.`);
20
- return 1;
21
- }
22
- /** A name says what the migration does, in snake_case. `stack_` is the upgrade's, so an app's own migration never reads as the stack's. */
23
- const MIGRATION_NAME = /^[a-z][a-z0-9_]*$/;
24
- export async function dbGenerate(cli, [name, ...flags]) {
25
- if (!name || !MIGRATION_NAME.test(name)) {
26
- cli.error("usage: stack db:generate <name> [drizzle-kit flags]\nName the migration for what it does, in snake_case: add_widgets, drop_legacy_tokens.");
27
- return 2;
28
- }
29
- if (name.startsWith("stack_")) {
30
- cli.error(`${name}: names starting stack_ are the stack upgrade's own; name the change itself.`);
31
- return 2;
32
- }
33
- return (await cli.exec("pnpm", ["exec", "drizzle-kit", "generate", "--name", name, ...flags], {
34
- inherit: true,
35
- })).code;
36
- }
37
- export async function dbUpgrade(cli) {
38
- if ((await pending(cli)).length === 0)
39
- return 0;
40
- const name = `stack_${STACK_PACKAGE.version.replaceAll(".", "_")}`;
41
- cli.log(`generating drizzle/ migration ${name} for the schema's changes`);
42
- return (await cli.exec("pnpm", ["exec", "drizzle-kit", "generate", "--name", name], { inherit: true })).code;
43
- }
package/dist/cli/dev.d.ts DELETED
@@ -1,4 +0,0 @@
1
- import { type Cli } from "./context.ts";
2
- /** Where `--pglite` keeps its database, at the app's root; git-ignored by the stack's block. */
3
- export declare const LOCAL_DB_DIR = ".local-db";
4
- export declare function dev(cli: Cli, args: string[]): Promise<number>;
package/dist/cli/dev.js DELETED
@@ -1,70 +0,0 @@
1
- // `stack dev [--pglite] [next dev args]` — the app's dev server.
2
- //
3
- // - Plain: `next dev` on your Neon dev branch, as `stack with-neon next dev`.
4
- // - `--pglite`: no Neon at all. PGlite (the app's own devDependency) is served on a free loopback port with its data in `.local-db/`, migrated with the app's migrations, seeded with its `db/seed.ts` the first time, and `next dev` runs against it. The server stops when `next dev` exits. Delete `.local-db/` to start again from the seed.
5
- //
6
- // PGlite serves one connection at a time: while `next dev` runs, nothing else (a script, a migration) can use the database.
7
- import { existsSync, mkdirSync, rmSync } from "node:fs";
8
- import { join } from "node:path";
9
- import { loadAppEnv } from "./context.js";
10
- import { migrate, runAppSeed, withNeon } from "./neon.js";
11
- /** Where `--pglite` keeps its database, at the app's root; git-ignored by the stack's block. */
12
- export const LOCAL_DB_DIR = ".local-db";
13
- export async function dev(cli, args) {
14
- const local = args.includes("--pglite");
15
- const nextArgs = args.filter((arg) => arg !== "--pglite");
16
- if (!local)
17
- return withNeon(cli, ["pnpm", "exec", "next", "dev", ...nextArgs]);
18
- return devPglite(cli, nextArgs);
19
- }
20
- async function devPglite(cli, nextArgs) {
21
- const dataDir = join(cli.cwd, LOCAL_DB_DIR);
22
- const fresh = !existsSync(dataDir);
23
- mkdirSync(dataDir, { recursive: true });
24
- const server = await cli.startLocalDatabase(dataDir).catch((error) => {
25
- if (fresh)
26
- rmSync(dataDir, { recursive: true, force: true });
27
- throw error;
28
- });
29
- let ready = false;
30
- // Ctrl-C reaches every process in the terminal's group: `next dev` and a migration stop on their own, and this one waits for them, then stops the database and removes a new one it could not finish.
31
- let interrupted = false;
32
- const onSignal = () => {
33
- interrupted = true;
34
- };
35
- process.on("SIGINT", onSignal);
36
- process.on("SIGTERM", onSignal);
37
- try {
38
- cli.error(`[local] PGlite on ${server.url}, data in ${LOCAL_DB_DIR}/`);
39
- // Every child, and the app's modules imported here, read this database and never Neon's: what is set here wins over `.env`, and an empty UNPOOLED keeps one from `.env` (a Neon branch's direct URL) from sending the migration elsewhere.
40
- Object.assign(cli.env, { DATABASE_URL: server.url, DATABASE_URL_UNPOOLED: "" });
41
- loadAppEnv(cli);
42
- const migrated = await migrate(cli, cli.env);
43
- if (migrated !== 0) {
44
- cli.error(`[local] the migration failed (exit ${migrated}); not starting next dev.`);
45
- return migrated;
46
- }
47
- if (interrupted)
48
- return 130;
49
- if (fresh) {
50
- await runAppSeed(cli);
51
- cli.error("[local] seeded from db/seed.ts");
52
- }
53
- if (interrupted)
54
- return 130;
55
- ready = true;
56
- const { code } = await cli.exec("pnpm", ["exec", "next", "dev", ...nextArgs], {
57
- env: cli.env,
58
- inherit: true,
59
- });
60
- return code;
61
- }
62
- finally {
63
- process.off("SIGINT", onSignal);
64
- process.off("SIGTERM", onSignal);
65
- await server.stop();
66
- // A database this run created but could not migrate or seed would never be seeded again.
67
- if (fresh && !ready)
68
- rmSync(dataDir, { recursive: true, force: true });
69
- }
70
- }
@@ -1,79 +0,0 @@
1
- import { type Cli, type Env } from "./context.ts";
2
- export declare const SEED = "seed";
3
- export declare const PREVIEW = "preview";
4
- export type Branch = {
5
- id: string;
6
- name: string;
7
- default: boolean;
8
- parent_id?: string;
9
- };
10
- /** Where the new seed is built; it takes the name `seed` only once it is clean. */
11
- export declare const SEED_NEXT = "seed-next";
12
- export declare function projectId(env: Env): string;
13
- /**
14
- * Refuses a dev branch name that is not a dev branch: production (whatever this project calls its default branch, and the name `production` itself), the shared `seed` and `preview`, and `seed-next` (the seed being built, which may briefly hold production's data). One check for every command that uses your dev branch: with-neon, dev-setup, and a package's command through `databaseEnv`.
15
- */
16
- export declare function assertDevBranchName(name: string, production: Branch): void;
17
- /** `dev/<os user>` unless NEON_DEV_BRANCH names another. */
18
- export declare const devBranchName: (cli: Cli) => string;
19
- export declare function findBranch(cli: Cli, name: string): Promise<Branch | undefined>;
20
- /** Production: the project's default branch (Neon's `default` flag), not a branch name. */
21
- export declare function defaultBranch(branches: Branch[]): Branch;
22
- /**
23
- * The variables local work needs: `branch`'s URLs, and the host of production (the default branch; `production` when the caller already has it, else listed).
24
- */
25
- export declare function branchEnv(cli: Cli, branch: string, production?: Branch): Promise<Record<string, string>>;
26
- /** The migrations, run against whatever database `env` names, through the direct endpoint. */
27
- export declare function migrate(cli: Pick<Cli, "exec">, env: Env): Promise<number>;
28
- /**
29
- * `stack with-neon <command> [args]` — runs a local command against your Neon dev branch with no `.env`: the connection strings and production's host come from the Neon CLI on every run.
30
- *
31
- * Steps aside, and runs the command untouched, when something else already says where the database is: DATABASE_URL in the environment (Vercel, an e2e run, a local Postgres), ENV_FILE (a deliberate production command). A `.env` never decides: what this sets wins over it, in Next and in `loadEnvFile` alike.
32
- */
33
- export declare function withNeon(cli: Cli, [command, ...args]: string[]): Promise<number>;
34
- /**
35
- * Your dev branch's `branchEnv`, as `with-neon` hands it to a command; nothing (`{}`) when DATABASE_URL or ENV_FILE already says where the database is. Refuses the default branch.
36
- */
37
- export declare function devBranchEnv(cli: Cli): Promise<Record<string, string>>;
38
- /**
39
- * The shared `preview` branch's `branchEnv`, for a package's command run with `--preview` (`databaseEnv({ preview: true })`): previews hold only `db/seed.ts`'s rows, so this is how a developer changes what one holds. `--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.
40
- */
41
- export declare function previewBranchEnv(cli: Cli): Promise<Record<string, string>>;
42
- /**
43
- * `stack dev-setup` — once per developer: makes your Neon dev branch from `seed` (never from production) if it does not exist yet (`dev/<os user>`, or NEON_DEV_BRANCH) and migrates it. Safe to re-run. There is no `.env` to write: `stack with-neon` reads the branch from the Neon CLI on every command.
44
- */
45
- export declare function devSetup(cli: Cli): Promise<number>;
46
- /**
47
- * Schemas Neon's own services install, which hold functions and settings rather than anyone's rows: `auth` is the pg_session_jwt extension the Data API installs (Neon: "do not install this extension manually"), `pgrst` is the Data API's PostgREST configuration. Left in place so the branch keeps working like any other. Neon Auth's `neon_auth` is *not* here: it holds users.
48
- */
49
- export declare const NEON_SCHEMAS: readonly string[];
50
- /**
51
- * Empties a database down to an empty `public`: drops every schema but Postgres's own (`pg_catalog`, `pg_toast*`, `information_schema`, any `pg_*`) and Neon's service schemas, with everything in them — tables, rows, drizzle's journal, Neon Auth's users. Then the migrations run from nothing, as on a new database. A schema-only branch needs this as much as a full copy: it keeps drizzle's journal *table* but not its rows, so migrate would re-run 0000 into tables that already exist. Large objects belong to the database rather than to a schema, so no drop removes them: every one is unlinked too.
52
- *
53
- * Neon's schemas are kept by name, so first it checks they hold what Neon puts there: functions and settings, no rows. A table, partitioned table, materialized view, sequence or foreign table in one (an app's own `auth.accounts`, say) would carry production's rows into the seed, so it refuses before dropping anything.
54
- */
55
- export declare function emptyDatabase(sql: (statement: string) => Promise<Record<string, unknown>[]>): Promise<string[]>;
56
- /**
57
- * `stack neon seed` — (re)creates the `seed` branch: exactly the app's migrations plus its seed rows, and none of production's data. Cuts it from production (the project's default branch) as Neon's `--schema-only` branch (a normal branch where that is unsupported), empties it of every non-system schema either way (`emptyDatabase`), migrates it from nothing and runs the app's `db/seed.ts`: `export default async function seed(db)`.
58
- *
59
- * The new seed is built as `seed-next` and takes the name `seed` only once it is clean, so a failure part-way never leaves a branch holding production's data under the name every dev and preview branch is cut from; `seed-next` is deleted on any failure. Neon will not delete a branch that has children, so a seed with dev/preview branches on it is refused up front: remove them yourself (this never deletes them).
60
- */
61
- export declare function neonSeed(cli: Cli): Promise<number>;
62
- /**
63
- * Runs the app's `db/seed.ts` (`export default async function seed(db)`) on the app's own `db` (`src/lib/db.ts`), wherever `cli.env` points it, then closes that client's pool: on PGlite it holds the one connection the server allows.
64
- */
65
- export declare function runAppSeed(cli: Cli): Promise<void>;
66
- /** The Preview variables `--set-vercel` writes, and which of the branch's URLs each holds. */
67
- export declare const PREVIEW_VARIABLES: readonly [{
68
- readonly name: "DATABASE_URL";
69
- readonly pooled: true;
70
- }, {
71
- readonly name: "DATABASE_URL_UNPOOLED";
72
- readonly pooled: false;
73
- }];
74
- /**
75
- * `stack neon reset-preview [--set-vercel]` — (re)creates the one shared `preview` branch from `seed`. A build cannot change the DATABASE_URL a preview deployment uses at runtime, so previews share this branch, and `stack vercel-build` migrates it on each preview build; run this again to start it clean.
76
- *
77
- * Without `--set-vercel` it prints the pooled URL, to set by hand as Preview's DATABASE_URL. With it, it writes the pooled URL as DATABASE_URL and the direct one as DATABASE_URL_UNPOOLED into the linked Vercel project, Preview only, as sensitive variables replacing any there, and prints neither. Each value is read from the Neon CLI in full before `vercel env add` starts, then handed over on its stdin: never in its arguments, where `ps` would show it, and never piped from a command still running, which `vercel env add` would read as empty.
78
- */
79
- export declare function resetPreview(cli: Cli, args?: string[]): Promise<number>;