@wwchen/stack 0.6.3 → 0.7.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 +9 -8
- package/blocks/env.example +1 -2
- package/dist/auth/better-auth/index.d.ts +2 -316
- package/dist/auth/better-auth/index.js +2 -237
- package/dist/auth/index.d.ts +2 -58
- package/dist/auth/index.js +2 -120
- package/dist/auth/proxy.d.ts +2 -14
- package/dist/auth/proxy.js +2 -50
- package/dist/cli/context.d.ts +6 -3
- package/dist/cli/context.js +3 -0
- package/dist/cli/db.d.ts +3 -0
- package/dist/cli/db.js +26 -0
- package/dist/cli/index.js +14 -3
- package/dist/cli/migrate-suite.d.ts +2 -0
- package/dist/cli/migrate-suite.js +787 -0
- package/dist/cli/neon.d.ts +11 -44
- package/dist/cli/neon.js +15 -56
- package/dist/cli/sync-blocks.d.ts +1 -3
- package/dist/cli/sync-blocks.js +2 -6
- package/dist/cli/vercel-build.js +5 -16
- package/dist/configs/vitest.js +3 -7
- package/dist/connections/index.d.ts +2 -8
- package/dist/connections/index.js +3 -9
- package/dist/connections/providers.d.ts +1 -3
- package/dist/connections/table.d.ts +2 -3
- package/dist/connections/table.js +1 -2
- package/dist/db/explaining-pool.d.ts +1 -6
- package/dist/db/explaining-pool.js +3 -10
- package/dist/db/index.d.ts +1 -3
- package/dist/db/index.js +5 -13
- package/dist/db/migration-status.d.ts +1 -4
- package/dist/db/migration-status.js +1 -4
- package/dist/db/schema-changes.d.ts +13 -0
- package/dist/db/schema-changes.js +99 -0
- package/dist/db/ssl.d.ts +1 -5
- package/dist/db/ssl.js +1 -5
- package/dist/db/target.d.ts +2 -9
- package/dist/db/target.js +2 -9
- package/dist/db/testing/index.d.ts +3 -7
- package/dist/db/testing/index.js +3 -7
- package/dist/db/testing/isolate.js +1 -2
- package/dist/db/testing/pglite.d.ts +2 -6
- package/dist/db/testing/pglite.js +2 -6
- package/dist/env/define-env.d.ts +3 -9
- package/dist/env/define-env.js +6 -16
- package/dist/env/load-env-file.d.ts +4 -9
- package/dist/env/load-env-file.js +5 -11
- package/dist/env/stack-env.d.ts +1 -3
- package/dist/env/stack-env.js +3 -7
- package/dist/env/testing.d.ts +1 -3
- package/dist/env/testing.js +2 -5
- package/dist/http/guards.d.ts +3 -10
- package/dist/http/guards.js +4 -12
- package/dist/jobs/index.d.ts +2 -9
- package/dist/jobs/index.js +2 -9
- package/dist/mail/index.d.ts +4 -45
- package/dist/mail/index.js +4 -97
- package/dist/next/api.d.ts +7 -19
- package/dist/next/api.js +6 -15
- package/dist/next/config.js +10 -32
- package/dist/next/cron.d.ts +2 -6
- package/dist/next/cron.js +3 -8
- package/dist/next/health.d.ts +3 -8
- package/dist/next/health.js +3 -8
- package/dist/testing/next.d.ts +1 -4
- package/dist/testing/next.js +2 -7
- package/dist/ui/app.css +5 -16
- package/dist/ui/globals.css +6 -20
- package/dist/ui/icons.d.ts +1 -3
- package/dist/ui/icons.js +3 -10
- package/dist/ui/index.d.ts +4 -12
- package/dist/ui/index.js +4 -12
- package/dist/ui/theme.d.ts +3 -9
- package/dist/ui/theme.js +3 -9
- package/package.json +15 -9
- package/dist/auth/better-auth/tables.d.ts +0 -628
- package/dist/auth/better-auth/tables.js +0 -76
- package/dist/auth/better-auth/users.d.ts +0 -27
- package/dist/auth/better-auth/users.js +0 -128
- package/dist/auth/safe-next.d.ts +0 -17
- package/dist/auth/safe-next.js +0 -36
- package/dist/auth/types.d.ts +0 -47
- package/dist/auth/types.js +0 -5
- package/dist/http/client-ip.d.ts +0 -12
- package/dist/http/client-ip.js +0 -28
- package/dist/http/rate-limit.d.ts +0 -34
- package/dist/http/rate-limit.js +0 -79
package/dist/auth/index.d.ts
CHANGED
|
@@ -1,58 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* The app's one auth object: `export const auth = createAuth({ provider })` in `src/lib/auth.ts`,
|
|
4
|
-
* and every session read, sign-in and sign-out goes through it. Which provider runs sign-in is
|
|
5
|
-
* invisible past this point.
|
|
6
|
-
*
|
|
7
|
-
* The sign-in functions are plain async functions that read the request through `next/headers`;
|
|
8
|
-
* the app's thin `actions.ts` marks them `"use server"` (the package ships no server-action module).
|
|
9
|
-
* They must run as server actions: that is where Next lets a cookie be set, and the provider sets
|
|
10
|
-
* its cookies there — Better Auth's `nextCookies()` writes each `Set-Cookie` of an in-process call
|
|
11
|
-
* (Google's signed state cookie, sign-out's cleared session) into `cookies()`.
|
|
12
|
-
*/
|
|
13
|
-
export type CreateAuthOptions = {
|
|
14
|
-
provider: AuthProvider;
|
|
15
|
-
/**
|
|
16
|
-
* Sign-in requests allowed per client address per hour (default 10). Spent by every magic-link
|
|
17
|
-
* request and Google start, whatever the address typed. `Infinity` turns the limit off.
|
|
18
|
-
*/
|
|
19
|
-
rateLimitPerHour?: number;
|
|
20
|
-
/** Proxies in front of the app that append to `X-Forwarded-For` (default 0, as on Vercel). */
|
|
21
|
-
trustedProxyHops?: number;
|
|
22
|
-
};
|
|
23
|
-
export type StackAuth = {
|
|
24
|
-
/** The session, or null. Reads `headers` when given (route handlers), else the request's. */
|
|
25
|
-
getSession(headers?: Headers): Promise<Session | null>;
|
|
26
|
-
/**
|
|
27
|
-
* The session, or a redirect to `/sign-in?next=<nextPath>`. A server layout cannot see the
|
|
28
|
-
* request path in Next 16, so the caller supplies it; the proxy redirects with the real one.
|
|
29
|
-
*/
|
|
30
|
-
requireSession(nextPath?: string): Promise<Session>;
|
|
31
|
-
/**
|
|
32
|
-
* An admin's session, or the not-found page: a signed-in user who is not an admin gets the same
|
|
33
|
-
* 404 a missing page does, so admin pages do not announce themselves. Pages only — `notFound()`
|
|
34
|
-
* throws, so API routes check `session.user.role` and answer the envelope instead.
|
|
35
|
-
*/
|
|
36
|
-
requireAdmin(): Promise<Session>;
|
|
37
|
-
/**
|
|
38
|
-
* Sends a sign-in link. `{ ok: true }` for every address, known or not (the provider sends
|
|
39
|
-
* nothing to an address nobody added). `rate_limited` once this caller's budget is spent.
|
|
40
|
-
*/
|
|
41
|
-
requestMagicLink(input: {
|
|
42
|
-
email: string;
|
|
43
|
-
next?: string;
|
|
44
|
-
}): Promise<AuthResult>;
|
|
45
|
-
/** Starts Google sign-in; `redirect` is Google's URL, for the action to `redirect()` to. */
|
|
46
|
-
startGoogleSignIn(next?: string): Promise<AuthResult>;
|
|
47
|
-
/** Ends the session and clears its cookie. A failure is logged and swallowed. */
|
|
48
|
-
signOut(): Promise<void>;
|
|
49
|
-
/** For `app/api/auth/[...all]/route.ts`: `export const GET = auth.handler, POST = auth.handler`. */
|
|
50
|
-
handler(req: Request): Promise<Response>;
|
|
51
|
-
/** Invite-only user management. */
|
|
52
|
-
users: AuthProvider["users"];
|
|
53
|
-
};
|
|
54
|
-
export declare function createAuth(opts: CreateAuthOptions): StackAuth;
|
|
55
|
-
export { clientIp } from "../http/client-ip.ts";
|
|
56
|
-
export { type RateLimitInput, rateLimit } from "../http/rate-limit.ts";
|
|
57
|
-
export { safeNext } from "./safe-next.ts";
|
|
58
|
-
export type { AppUser, AuthErrorCode, AuthProvider, AuthResult, Role, Session, } from "./types.ts";
|
|
1
|
+
/** @deprecated Import from `@wwchen/stack-auth`. */
|
|
2
|
+
export * from "@wwchen/stack-auth";
|
package/dist/auth/index.js
CHANGED
|
@@ -1,120 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { safeNext } from "./safe-next.js";
|
|
4
|
-
const SIGN_IN_PATH = "/sign-in";
|
|
5
|
-
/**
|
|
6
|
-
* `redirect` and `notFound`, imported on call by their bare name. Next's bundler gives server code
|
|
7
|
-
* its own versions only for the bare `next/navigation`; `next/navigation.js` is the client module,
|
|
8
|
-
* which cannot load in a route handler. Plain Node (the `stack` CLI importing the app's auth) cannot
|
|
9
|
-
* resolve the bare name, and never calls this.
|
|
10
|
-
*/
|
|
11
|
-
const navigation = () => import("next/navigation");
|
|
12
|
-
/**
|
|
13
|
-
* The bucket the sign-in functions spend, under the path of the route that does the same thing, so
|
|
14
|
-
* a raw POST to that route and the server action share one budget: neither is a way around the
|
|
15
|
-
* other.
|
|
16
|
-
*/
|
|
17
|
-
const SIGN_IN_BUDGET_PATH = "/api/auth/sign-in/magic-link";
|
|
18
|
-
/**
|
|
19
|
-
* Better Auth's password routes. Passwords are off unless an app opts in with
|
|
20
|
-
* `betterAuthProvider({ emailAndPassword })`; it then calls
|
|
21
|
-
* `provider.instance.api.signInEmail / changePassword / verifyPassword` in-process from its own
|
|
22
|
-
* server action, with a budget of its own. Served here too, each would be a second door with its
|
|
23
|
-
* own budget, multiplying the guesses an attacker gets. Answered 404 whether or not the option is
|
|
24
|
-
* on (off, Better Auth refuses them anyway).
|
|
25
|
-
*/
|
|
26
|
-
const NOT_SERVED = new Set([
|
|
27
|
-
"/api/auth/sign-in/email",
|
|
28
|
-
"/api/auth/change-password",
|
|
29
|
-
"/api/auth/verify-password",
|
|
30
|
-
]);
|
|
31
|
-
export function createAuth(opts) {
|
|
32
|
-
const { provider } = opts;
|
|
33
|
-
const budget = {
|
|
34
|
-
limit: opts.rateLimitPerHour ?? 10,
|
|
35
|
-
trustedProxyHops: opts.trustedProxyHops ?? 0,
|
|
36
|
-
};
|
|
37
|
-
/** Spends one sign-in request from this caller's budget; true when there was none left. */
|
|
38
|
-
async function budgetSpent() {
|
|
39
|
-
const headers = await requestHeaders();
|
|
40
|
-
return rateLimit({ path: SIGN_IN_BUDGET_PATH, headers, ...budget }) !== null;
|
|
41
|
-
}
|
|
42
|
-
async function getSession(headers) {
|
|
43
|
-
return provider.getSession(headers ?? (await requestHeaders()));
|
|
44
|
-
}
|
|
45
|
-
async function requireSession(nextPath = "/") {
|
|
46
|
-
const current = await getSession();
|
|
47
|
-
if (!current) {
|
|
48
|
-
const { redirect } = await navigation();
|
|
49
|
-
return redirect(`${SIGN_IN_PATH}?next=${encodeURIComponent(safeNext(nextPath))}`);
|
|
50
|
-
}
|
|
51
|
-
return current;
|
|
52
|
-
}
|
|
53
|
-
return {
|
|
54
|
-
getSession,
|
|
55
|
-
requireSession,
|
|
56
|
-
async requireAdmin() {
|
|
57
|
-
const current = await requireSession();
|
|
58
|
-
if (current.user.role !== "admin")
|
|
59
|
-
return (await navigation()).notFound();
|
|
60
|
-
return current;
|
|
61
|
-
},
|
|
62
|
-
// The provider passes `next` through as given, so it is made safe here, before any provider
|
|
63
|
-
// sees it. The budget is spent by every request, known address or not, so it says nothing.
|
|
64
|
-
async requestMagicLink(input) {
|
|
65
|
-
const next = safeNext(input.next);
|
|
66
|
-
if (await budgetSpent())
|
|
67
|
-
return { ok: false, code: "rate_limited" };
|
|
68
|
-
return provider.signIn.magicLink(input.email, next);
|
|
69
|
-
},
|
|
70
|
-
async startGoogleSignIn(next) {
|
|
71
|
-
const target = safeNext(next);
|
|
72
|
-
if (await budgetSpent())
|
|
73
|
-
return { ok: false, code: "rate_limited" };
|
|
74
|
-
return provider.signIn.oauth("google", target);
|
|
75
|
-
},
|
|
76
|
-
async signOut() {
|
|
77
|
-
const headers = await requestHeaders();
|
|
78
|
-
try {
|
|
79
|
-
await provider.signOut(headers);
|
|
80
|
-
}
|
|
81
|
-
catch (error) {
|
|
82
|
-
// The caller is sent to sign-in either way, and a session that survived still expires.
|
|
83
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
84
|
-
console.error("[auth] sign-out failed", { message });
|
|
85
|
-
}
|
|
86
|
-
},
|
|
87
|
-
async handler(req) {
|
|
88
|
-
const path = routePath(req);
|
|
89
|
-
if (NOT_SERVED.has(path) || [...NOT_SERVED].some((route) => path.startsWith(`${route}/`))) {
|
|
90
|
-
return Response.json({ error: { code: "not_found", message: "Not found." } }, { status: 404 });
|
|
91
|
-
}
|
|
92
|
-
// Every route, per path: asking for links is limited apart from following them.
|
|
93
|
-
return rateLimit({ path, headers: req.headers, ...budget }) ?? provider.handler(req);
|
|
94
|
-
},
|
|
95
|
-
users: provider.users,
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* The request's path in one canonical spelling: percent-decoded, lower-cased, without repeated or
|
|
100
|
-
* trailing slashes. Better Auth routes none of the other spellings, so this is a backstop: if
|
|
101
|
-
* a router ever does, `/api/auth/sign-in/%65mail/` is still a path that is not served, and a
|
|
102
|
-
* spelling of the sign-in path is not a fresh budget.
|
|
103
|
-
*/
|
|
104
|
-
function routePath(req) {
|
|
105
|
-
const raw = new URL(req.url).pathname;
|
|
106
|
-
let decoded = raw;
|
|
107
|
-
try {
|
|
108
|
-
decoded = decodeURIComponent(raw);
|
|
109
|
-
}
|
|
110
|
-
catch {
|
|
111
|
-
// Malformed escapes: compare the raw path.
|
|
112
|
-
}
|
|
113
|
-
return decoded
|
|
114
|
-
.toLowerCase()
|
|
115
|
-
.replace(/\/{2,}/g, "/")
|
|
116
|
-
.replace(/(.)\/$/, "$1");
|
|
117
|
-
}
|
|
118
|
-
export { clientIp } from "../http/client-ip.js";
|
|
119
|
-
export { rateLimit } from "../http/rate-limit.js";
|
|
120
|
-
export { safeNext } from "./safe-next.js";
|
|
1
|
+
/** @deprecated Import from `@wwchen/stack-auth`. */
|
|
2
|
+
export * from "@wwchen/stack-auth";
|
package/dist/auth/proxy.d.ts
CHANGED
|
@@ -1,14 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* For the app's `export const config = { matcher: [...] }`: page routes only. Next reads the
|
|
4
|
-
* matcher statically at build time, so the app's `src/proxy.ts` writes this value out literally
|
|
5
|
-
* rather than importing it; this export is what a test compares that literal against.
|
|
6
|
-
*/
|
|
7
|
-
export declare const proxyMatcher: string[];
|
|
8
|
-
export type SessionProxyOptions = {
|
|
9
|
-
/** Where an anonymous request is sent. Defaults to `/sign-in`. */
|
|
10
|
-
signInPath?: string;
|
|
11
|
-
/** Paths anyone may see. Each covers itself and everything under it; `/` covers only `/`. */
|
|
12
|
-
publicPaths?: string[];
|
|
13
|
-
};
|
|
14
|
-
export declare function sessionProxy(opts?: SessionProxyOptions): (req: NextRequest) => NextResponse;
|
|
1
|
+
/** @deprecated Import from `@wwchen/stack-auth/proxy`. */
|
|
2
|
+
export * from "@wwchen/stack-auth/proxy";
|
package/dist/auth/proxy.js
CHANGED
|
@@ -1,50 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* The redirect half of route protection, for the app's `src/proxy.ts` (Next 16's name for
|
|
4
|
-
* middleware), and the only place the request path is knowable: a Next 16 server layout receives no
|
|
5
|
-
* pathname, so `/sign-in?next=<path>` cannot be built there. Running before routing also means an
|
|
6
|
-
* anonymous request is redirected rather than answered with a 404 by a page it cannot see yet.
|
|
7
|
-
*
|
|
8
|
-
* This checks only for the PRESENCE of a session cookie: a proxy has no database. It is a
|
|
9
|
-
* convenience — the real enforcement is `requireSession()` in the `(app)` layout, and every API
|
|
10
|
-
* route guards itself. Its own module, `@wwchen/stack/auth/proxy`, so the proxy bundle never pulls
|
|
11
|
-
* in the database or the auth provider.
|
|
12
|
-
*/
|
|
13
|
-
// Better Auth names its cookie `<prefix>.session_token`, and prefixes it `__Secure-` when the
|
|
14
|
-
// cookie is secure — which it is whenever the app's URL is https.
|
|
15
|
-
const SESSION_COOKIES = ["better-auth.session_token", "__Secure-better-auth.session_token"];
|
|
16
|
-
/**
|
|
17
|
-
* Paths the proxy never redirects, matching what `proxyMatcher` leaves out — so the proxy is right
|
|
18
|
-
* even when an app widens its matcher.
|
|
19
|
-
*
|
|
20
|
-
* `/api/**` is excluded for a reason, not for tidiness: an unauthenticated API call must answer 401
|
|
21
|
-
* with the `{ error: { code, message } }` envelope from its own route; a redirect would hand the
|
|
22
|
-
* caller a 307 to an HTML page. Sign-in must stay out too, or an anonymous visitor redirects to it
|
|
23
|
-
* forever.
|
|
24
|
-
*/
|
|
25
|
-
const EXCLUDED = /^\/(?:api(?:\/|$)|sign-in(?:\/|$)|_next\/|favicon\.ico$|.*\.(?:svg|png|jpg|jpeg|gif|webp|ico|txt|xml|webmanifest)$)/;
|
|
26
|
-
/**
|
|
27
|
-
* For the app's `export const config = { matcher: [...] }`: page routes only. Next reads the
|
|
28
|
-
* matcher statically at build time, so the app's `src/proxy.ts` writes this value out literally
|
|
29
|
-
* rather than importing it; this export is what a test compares that literal against.
|
|
30
|
-
*/
|
|
31
|
-
export const proxyMatcher = [
|
|
32
|
-
"/((?!api(?:/|$)|sign-in(?:/|$)|_next/|favicon\\.ico$|.*\\.(?:svg|png|jpg|jpeg|gif|webp|ico|txt|xml|webmanifest)$).*)",
|
|
33
|
-
];
|
|
34
|
-
export function sessionProxy(opts = {}) {
|
|
35
|
-
const signInPath = opts.signInPath ?? "/sign-in";
|
|
36
|
-
const open = [signInPath, ...(opts.publicPaths ?? [])];
|
|
37
|
-
const isOpen = (pathname) => EXCLUDED.test(pathname) ||
|
|
38
|
-
open.some((path) => pathname === path || (path !== "/" && pathname.startsWith(`${path}/`)));
|
|
39
|
-
return (req) => {
|
|
40
|
-
const { pathname, search } = req.nextUrl;
|
|
41
|
-
const signedIn = SESSION_COOKIES.some((name) => req.cookies.has(name));
|
|
42
|
-
if (signedIn || isOpen(pathname))
|
|
43
|
-
return NextResponse.next();
|
|
44
|
-
const target = req.nextUrl.clone();
|
|
45
|
-
target.pathname = signInPath;
|
|
46
|
-
target.search = "";
|
|
47
|
-
target.searchParams.set("next", `${pathname}${search}`);
|
|
48
|
-
return NextResponse.redirect(target);
|
|
49
|
-
};
|
|
50
|
-
}
|
|
1
|
+
/** @deprecated Import from `@wwchen/stack-auth/proxy`. */
|
|
2
|
+
export * from "@wwchen/stack-auth/proxy";
|
package/dist/cli/context.d.ts
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/** The stack's own package.json: the installed version, and the suite packages it depends on. It sits two levels above both `src/cli/` and `dist/cli/`. */
|
|
2
|
+
export declare const STACK_PACKAGE: {
|
|
3
|
+
version: string;
|
|
4
|
+
dependencies: Record<string, string>;
|
|
5
|
+
};
|
|
1
6
|
export type Env = Record<string, string | undefined>;
|
|
2
7
|
export type ExecResult = {
|
|
3
8
|
stdout: string;
|
|
@@ -19,9 +24,7 @@ export type LocalDatabase = {
|
|
|
19
24
|
stop(): Promise<void>;
|
|
20
25
|
};
|
|
21
26
|
/**
|
|
22
|
-
* Everything the CLI touches outside its own process, so a test can replace it. Only the entry
|
|
23
|
-
* point reads `process.env`: it passes it in as `env`, and nothing below reads it directly.
|
|
24
|
-
* `exec` runs a program with an argument array, never a shell string.
|
|
27
|
+
* Everything the CLI touches outside its own process, so a test can replace it. Only the entry point reads `process.env`: it passes it in as `env`, and nothing below reads it directly. `exec` runs a program with an argument array, never a shell string.
|
|
25
28
|
*/
|
|
26
29
|
export type CliDeps = {
|
|
27
30
|
env: Env;
|
package/dist/cli/context.js
CHANGED
|
@@ -6,6 +6,7 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
|
|
|
6
6
|
}
|
|
7
7
|
return path;
|
|
8
8
|
};
|
|
9
|
+
import { readFileSync } from "node:fs";
|
|
9
10
|
import { userInfo } from "node:os";
|
|
10
11
|
import { join } from "node:path";
|
|
11
12
|
import { pathToFileURL } from "node:url";
|
|
@@ -13,6 +14,8 @@ import pg from "pg";
|
|
|
13
14
|
import { explicitSslMode } from "../db/ssl.js";
|
|
14
15
|
import { startPgliteServer } from "../db/testing/pglite.js";
|
|
15
16
|
import { loadEnvFile } from "../env/index.js";
|
|
17
|
+
/** The stack's own package.json: the installed version, and the suite packages it depends on. It sits two levels above both `src/cli/` and `dist/cli/`. */
|
|
18
|
+
export const STACK_PACKAGE = JSON.parse(readFileSync(new URL("../../package.json", import.meta.url), "utf8"));
|
|
16
19
|
export function resolveDeps(deps) {
|
|
17
20
|
return {
|
|
18
21
|
log: (message) => console.log(message),
|
package/dist/cli/db.d.ts
ADDED
package/dist/cli/db.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
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: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`.
|
|
4
|
+
//
|
|
5
|
+
// Both run in the app's root and import its schema with Node's type stripping, so it must use relative `.ts` imports.
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import { pendingStatements } from "../db/schema-changes.js";
|
|
8
|
+
import { importApp, STACK_PACKAGE } from "./context.js";
|
|
9
|
+
async function pending(cli) {
|
|
10
|
+
const schema = await importApp(cli, "src/db/schema.ts");
|
|
11
|
+
return pendingStatements(schema, join(cli.cwd, "drizzle"));
|
|
12
|
+
}
|
|
13
|
+
export async function dbCheck(cli) {
|
|
14
|
+
const statements = await pending(cli);
|
|
15
|
+
if (statements.length === 0)
|
|
16
|
+
return 0;
|
|
17
|
+
cli.error(`src/db/schema.ts has changes drizzle/ has no migration for:\n\n${statements.join("\n")}\n\nRun \`pnpm exec drizzle-kit generate\`, review the SQL and commit it.`);
|
|
18
|
+
return 1;
|
|
19
|
+
}
|
|
20
|
+
export async function dbUpgrade(cli) {
|
|
21
|
+
if ((await pending(cli)).length === 0)
|
|
22
|
+
return 0;
|
|
23
|
+
const name = `stack_${STACK_PACKAGE.version.replaceAll(".", "_")}`;
|
|
24
|
+
cli.log(`generating drizzle/ migration ${name} for the schema's changes`);
|
|
25
|
+
return (await cli.exec("pnpm", ["exec", "drizzle-kit", "generate", "--name", name], { inherit: true })).code;
|
|
26
|
+
}
|
package/dist/cli/index.js
CHANGED
|
@@ -3,8 +3,10 @@ import { spawn } from "node:child_process";
|
|
|
3
3
|
import { realpathSync } from "node:fs";
|
|
4
4
|
import { fileURLToPath } from "node:url";
|
|
5
5
|
import { CliError, resolveDeps } from "./context.js";
|
|
6
|
+
import { dbCheck, dbUpgrade } from "./db.js";
|
|
6
7
|
import { dev } from "./dev.js";
|
|
7
8
|
import { doctor } from "./doctor.js";
|
|
9
|
+
import { migrateSuite } from "./migrate-suite.js";
|
|
8
10
|
import { devSetup, neonSeed, resetPreview, withNeon } from "./neon.js";
|
|
9
11
|
import { runScript } from "./run.js";
|
|
10
12
|
import { syncBlocksCommand } from "./sync-blocks.js";
|
|
@@ -23,7 +25,11 @@ const USAGE = `usage: stack <command>
|
|
|
23
25
|
user:create <email> [--admin] add a user through the app's auth; --preview: on the shared
|
|
24
26
|
preview branch (your own email only, to sign in on a preview)
|
|
25
27
|
user:role <email> <admin|user> change a user's role through the app's auth; --preview as above
|
|
26
|
-
sync-blocks [--check] refresh the stack-owned blocks in AGENTS.md, CLAUDE.md, .gitignore, .env.example
|
|
28
|
+
sync-blocks [--check] refresh the stack-owned blocks in AGENTS.md, CLAUDE.md, .gitignore, .env.example
|
|
29
|
+
db:check fail when src/db/schema.ts has changes drizzle/ has no migration for
|
|
30
|
+
db:upgrade generate drizzle/'s migration for the schema's changes, named stack_<version>
|
|
31
|
+
migrate-suite [--expect <version>] move the app onto the suite's packages (@wwchen/stack-auth, -notify);
|
|
32
|
+
--expect: refuse unless the installed stack is that version`;
|
|
27
33
|
/** The testable entry: every outside effect arrives through `deps`; the exit code comes back. */
|
|
28
34
|
export async function runCli(argv, deps) {
|
|
29
35
|
const cli = resolveDeps(deps);
|
|
@@ -54,6 +60,12 @@ export async function runCli(argv, deps) {
|
|
|
54
60
|
return await userRole(cli, args);
|
|
55
61
|
case "sync-blocks":
|
|
56
62
|
return await syncBlocksCommand(cli, args);
|
|
63
|
+
case "db:check":
|
|
64
|
+
return await dbCheck(cli);
|
|
65
|
+
case "db:upgrade":
|
|
66
|
+
return await dbUpgrade(cli);
|
|
67
|
+
case "migrate-suite":
|
|
68
|
+
return await migrateSuite(cli, args);
|
|
57
69
|
}
|
|
58
70
|
}
|
|
59
71
|
catch (error) {
|
|
@@ -100,8 +112,7 @@ export function exec(command, args, options = {}) {
|
|
|
100
112
|
});
|
|
101
113
|
}
|
|
102
114
|
/**
|
|
103
|
-
* Whether Node is running this file as its program, through any symlink (node_modules/.bin).
|
|
104
|
-
* Not `import.meta.main`: that needs Node 24.2, and the package accepts any 24.x.
|
|
115
|
+
* Whether Node is running this file as its program, through any symlink (node_modules/.bin). Not `import.meta.main`: that needs Node 24.2, and the package accepts any 24.x.
|
|
105
116
|
*/
|
|
106
117
|
function isEntryPoint() {
|
|
107
118
|
const program = process.argv[1];
|