@wwchen/stack 0.9.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.
- package/README.md +50 -23
- package/blocks/AGENTS.md +11 -31
- package/blocks/CLAUDE.md +10 -36
- package/blocks/env.example +1 -18
- package/blocks/gitignore +1 -8
- package/dist/cli/builtin.d.ts +7 -0
- package/dist/cli/builtin.js +42 -0
- package/dist/cli/check.d.ts +5 -0
- package/dist/cli/check.js +39 -0
- package/dist/cli/context.d.ts +4 -35
- package/dist/cli/context.js +1 -23
- package/dist/cli/discover.d.ts +15 -0
- package/dist/cli/discover.js +156 -0
- package/dist/cli/generate.d.ts +6 -0
- package/dist/cli/generate.js +94 -0
- package/dist/cli/host.d.ts +8 -0
- package/dist/cli/host.js +146 -0
- package/dist/cli/index.js +4 -71
- package/dist/cli/owned.d.ts +33 -0
- package/dist/cli/owned.js +151 -0
- package/dist/cli/sync.d.ts +19 -0
- package/dist/cli/sync.js +231 -0
- package/dist/configs/vitest.d.ts +3 -1
- package/dist/configs/vitest.js +8 -11
- package/dist/connections/env.d.ts +8 -0
- package/dist/connections/env.js +15 -0
- package/dist/connections/index.d.ts +1 -1
- package/dist/connections/index.js +1 -1
- package/dist/connections/table.d.ts +5 -3
- package/dist/connections/table.js +3 -3
- package/dist/env/core-env.d.ts +23 -0
- package/dist/env/core-env.js +24 -0
- package/dist/env/define-env.d.ts +22 -10
- package/dist/env/define-env.js +72 -17
- package/dist/env/index.d.ts +2 -2
- package/dist/env/index.js +1 -1
- package/dist/http/client-ip.d.ts +6 -0
- package/dist/http/client-ip.js +21 -0
- package/dist/http/index.d.ts +2 -0
- package/dist/http/index.js +2 -0
- package/dist/http/safe-next.d.ts +11 -0
- package/dist/http/safe-next.js +30 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -1
- package/dist/next/api.d.ts +9 -8
- package/dist/next/api.js +10 -8
- package/dist/next/config.d.ts +2 -0
- package/dist/next/config.js +10 -6
- package/dist/next/health.d.ts +5 -7
- package/dist/next/health.js +16 -17
- package/dist/stack/contracts.d.ts +202 -0
- package/dist/stack/contracts.js +66 -0
- package/dist/vercel/index.d.ts +28 -0
- package/dist/vercel/index.js +90 -0
- package/files/.claude/rules/stack-ui.md +24 -0
- package/files/next.config.ts +6 -0
- package/files/src/app/api/health/route.ts +9 -0
- package/package.json +16 -29
- package/dist/cli/db.d.ts +0 -4
- package/dist/cli/db.js +0 -43
- package/dist/cli/dev.d.ts +0 -4
- package/dist/cli/dev.js +0 -70
- package/dist/cli/doctor.d.ts +0 -40
- package/dist/cli/doctor.js +0 -166
- package/dist/cli/migrate-suite.d.ts +0 -2
- package/dist/cli/migrate-suite.js +0 -491
- package/dist/cli/neon.d.ts +0 -79
- package/dist/cli/neon.js +0 -384
- package/dist/cli/run.d.ts +0 -2
- package/dist/cli/run.js +0 -29
- package/dist/cli/sync-blocks.d.ts +0 -14
- package/dist/cli/sync-blocks.js +0 -93
- package/dist/cli/user-create.d.ts +0 -6
- package/dist/cli/user-create.js +0 -77
- package/dist/cli/vercel-build.d.ts +0 -4
- package/dist/cli/vercel-build.js +0 -61
- package/dist/db/explaining-pool.d.ts +0 -11
- package/dist/db/explaining-pool.js +0 -55
- package/dist/db/index.d.ts +0 -20
- package/dist/db/index.js +0 -31
- package/dist/db/migration-status.d.ts +0 -5
- package/dist/db/migration-status.js +0 -38
- package/dist/db/schema-changes.d.ts +0 -13
- package/dist/db/schema-changes.js +0 -99
- package/dist/db/ssl.d.ts +0 -4
- package/dist/db/ssl.js +0 -17
- package/dist/db/target.d.ts +0 -19
- package/dist/db/target.js +0 -65
- package/dist/db/testing/index.d.ts +0 -23
- package/dist/db/testing/index.js +0 -67
- package/dist/db/testing/isolate.d.ts +0 -1
- package/dist/db/testing/isolate.js +0 -3
- package/dist/db/testing/pglite.d.ts +0 -20
- package/dist/db/testing/pglite.js +0 -79
- package/dist/env/stack-env.d.ts +0 -37
- package/dist/env/stack-env.js +0 -54
- package/dist/env/testing.d.ts +0 -6
- package/dist/env/testing.js +0 -13
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import type { z } from "zod";
|
|
2
|
+
/** The file that gives the app's packages their settings. */
|
|
3
|
+
export declare const STACK_CONFIG_FILE = "stack.config.ts";
|
|
4
|
+
/** The built-in package's name, which its blocks' markers carry. */
|
|
5
|
+
export declare const BUILTIN_PACKAGE = "stack";
|
|
6
|
+
/** The core's npm name, which owns its files in the files record and in headers. */
|
|
7
|
+
export declare const CORE_PACKAGE = "@wwchen/stack";
|
|
8
|
+
/** The file recording which package owns each file `stack sync` writes. */
|
|
9
|
+
export declare const FILES_RECORD = "stack.files.json";
|
|
10
|
+
/** The modules `stack sync` generates from the packages' runtime pieces, relative to the app's root. */
|
|
11
|
+
export declare const GENERATED: {
|
|
12
|
+
readonly env: "src/stack/env.ts";
|
|
13
|
+
readonly health: "src/stack/health.ts";
|
|
14
|
+
readonly next: "src/stack/next.ts";
|
|
15
|
+
};
|
|
16
|
+
/** The files `stack sync` writes packages' marked blocks into. */
|
|
17
|
+
export declare const BLOCK_FILES: {
|
|
18
|
+
readonly agents: "AGENTS.md";
|
|
19
|
+
readonly claude: "CLAUDE.md";
|
|
20
|
+
readonly gitignore: ".gitignore";
|
|
21
|
+
readonly envExample: ".env.example";
|
|
22
|
+
};
|
|
23
|
+
export type BlockFile = (typeof BLOCK_FILES)[keyof typeof BLOCK_FILES];
|
|
24
|
+
/** A package's marked block: the file it goes in, and the URL of the file its text is read from. */
|
|
25
|
+
export type Block = {
|
|
26
|
+
file: BlockFile;
|
|
27
|
+
source: URL;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* A package's blocks for `files`, each read from `dir` (the package's `blocks/` directory) under its file's name without the leading dot (`gitignore`, `env.example`), because npm leaves a `.gitignore` out of a published package.
|
|
31
|
+
*/
|
|
32
|
+
export declare const packageBlocks: (dir: URL, files: readonly BlockFile[]) => Block[];
|
|
33
|
+
/** How much a `stack doctor` finding matters: an error fails the command. */
|
|
34
|
+
export declare const SEVERITY: {
|
|
35
|
+
readonly error: "error";
|
|
36
|
+
readonly warning: "warning";
|
|
37
|
+
readonly info: "info";
|
|
38
|
+
};
|
|
39
|
+
export type Severity = (typeof SEVERITY)[keyof typeof SEVERITY];
|
|
40
|
+
/** What `NODE_ENV` holds: tooling sets it (next dev and build, Vitest, Vercel's runtime). */
|
|
41
|
+
export declare const NODE_ENVS: {
|
|
42
|
+
readonly development: "development";
|
|
43
|
+
readonly test: "test";
|
|
44
|
+
readonly production: "production";
|
|
45
|
+
};
|
|
46
|
+
export type NodeEnv = (typeof NODE_ENVS)[keyof typeof NODE_ENVS];
|
|
47
|
+
/** Vercel's environments, which a variable targets and `VERCEL_ENV` names. */
|
|
48
|
+
export declare const VERCEL_TARGET: {
|
|
49
|
+
readonly production: "production";
|
|
50
|
+
readonly preview: "preview";
|
|
51
|
+
readonly development: "development";
|
|
52
|
+
};
|
|
53
|
+
export type VercelTarget = (typeof VERCEL_TARGET)[keyof typeof VERCEL_TARGET];
|
|
54
|
+
export type Env = Record<string, string | undefined>;
|
|
55
|
+
export type ExecResult = {
|
|
56
|
+
stdout: string;
|
|
57
|
+
code: number;
|
|
58
|
+
};
|
|
59
|
+
export type ExecOptions = {
|
|
60
|
+
/** The child's environment; defaults to the CLI's own. */
|
|
61
|
+
env?: Env;
|
|
62
|
+
/** Hand the child this terminal (a dev server, a build) instead of capturing its stdout. */
|
|
63
|
+
inherit?: boolean;
|
|
64
|
+
/**
|
|
65
|
+
* Written to the child's stdin, then closed: how a secret reaches another CLI without appearing in its arguments. Pass a value already held in full, never a stream still arriving: a CLI that reads stdin at start-up takes whatever is there, which may be nothing yet.
|
|
66
|
+
*/
|
|
67
|
+
input?: string;
|
|
68
|
+
};
|
|
69
|
+
/** What a command receives from the `stack` CLI host. */
|
|
70
|
+
export type CommandContext = {
|
|
71
|
+
cwd: string;
|
|
72
|
+
env: Env;
|
|
73
|
+
exec(command: string, args: string[], options?: ExecOptions): Promise<ExecResult>;
|
|
74
|
+
log(message: string): void;
|
|
75
|
+
error(message: string): void;
|
|
76
|
+
/** Imports a file of the app by its root-relative path. */
|
|
77
|
+
importApp(path: string): Promise<Record<string, unknown>>;
|
|
78
|
+
/** Every composed package, the built-in one first. */
|
|
79
|
+
packages: readonly StackPackage[];
|
|
80
|
+
};
|
|
81
|
+
/** A `stack` command; it resolves to the exit code. */
|
|
82
|
+
export type Command = (ctx: CommandContext, args: string[]) => Promise<number>;
|
|
83
|
+
/** A step run before `next build`; a throw fails the build. */
|
|
84
|
+
export type BuildStep = {
|
|
85
|
+
name: string;
|
|
86
|
+
run(ctx: CommandContext): Promise<void>;
|
|
87
|
+
};
|
|
88
|
+
export type Finding = {
|
|
89
|
+
severity: Severity;
|
|
90
|
+
message: string;
|
|
91
|
+
};
|
|
92
|
+
/** One Vercel environment variable, metadata only: there is deliberately no `value`. */
|
|
93
|
+
export type EnvVarMeta = {
|
|
94
|
+
id: string;
|
|
95
|
+
key: string;
|
|
96
|
+
type: string;
|
|
97
|
+
target: string[];
|
|
98
|
+
gitBranch?: string;
|
|
99
|
+
configurationId?: string;
|
|
100
|
+
comment?: string;
|
|
101
|
+
};
|
|
102
|
+
/** The linked Vercel project's metadata, never a secret value. */
|
|
103
|
+
export type DoctorFacts = {
|
|
104
|
+
project: string;
|
|
105
|
+
/** The project's framework preset; null is Vercel's "Other". */
|
|
106
|
+
framework: string | null;
|
|
107
|
+
/** `vercel.json`'s `framework`, which overrides the preset; undefined when it sets none. */
|
|
108
|
+
vercelJsonFramework?: string | null;
|
|
109
|
+
envs: EnvVarMeta[];
|
|
110
|
+
/** The API had more pages of variables than were read. */
|
|
111
|
+
truncated?: boolean;
|
|
112
|
+
};
|
|
113
|
+
export type DoctorCheck = (facts: DoctorFacts) => Promise<Finding[]>;
|
|
114
|
+
/** What a package's `./stack` entry gives the `stack` CLI. */
|
|
115
|
+
export type StackPackage = {
|
|
116
|
+
/** The package's npm name (`@scope/name`; the built-in package is `stack`), which its factory returns as its package.json names it: its blocks' markers, the owner `stack.files.json` records for its files, its settings' key in `stack.config.ts`, and how `stack` names it in the usage and in a failure. */
|
|
117
|
+
name: string;
|
|
118
|
+
/** Each command's loader, called only when that command runs. */
|
|
119
|
+
commands?: Record<string, () => Promise<Command>>;
|
|
120
|
+
buildSteps?: BuildStep[];
|
|
121
|
+
doctorChecks?: DoctorCheck[];
|
|
122
|
+
/** The marked blocks `sync` writes. */
|
|
123
|
+
blocks?: Block[];
|
|
124
|
+
/** Checks `stack check` runs, which CI runs. */
|
|
125
|
+
checks?: Check[];
|
|
126
|
+
/** Whole files `stack sync` owns in the app. */
|
|
127
|
+
files?: OwnedFile[];
|
|
128
|
+
/** The pieces `stack sync` wires into the app's generated modules. */
|
|
129
|
+
runtime?: {
|
|
130
|
+
/** An `EnvFragment`, for `src/stack/env.ts`. */
|
|
131
|
+
env?: Export;
|
|
132
|
+
/** A `(env) => HealthCheck`, for `src/stack/health.ts`. */
|
|
133
|
+
health?: Export;
|
|
134
|
+
/** Files a route reads at run time, by route, for `src/stack/next.ts`. */
|
|
135
|
+
tracing?: Record<string, string[]>;
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
/** A step of `stack check`; a throw fails it. */
|
|
139
|
+
export type Check = {
|
|
140
|
+
name: string;
|
|
141
|
+
run(ctx: CommandContext): Promise<void>;
|
|
142
|
+
};
|
|
143
|
+
/** A whole file `stack sync` owns: its path relative to the app's root, the URL of the file it is written from, and, for a file that carries the package's settings, `render`, which makes the text written from the source's. */
|
|
144
|
+
export type OwnedFile = {
|
|
145
|
+
path: string;
|
|
146
|
+
source: URL;
|
|
147
|
+
render?: (source: string) => string;
|
|
148
|
+
};
|
|
149
|
+
/** An import specifier and an export name. */
|
|
150
|
+
export type Export = {
|
|
151
|
+
from: string;
|
|
152
|
+
name: string;
|
|
153
|
+
};
|
|
154
|
+
/**
|
|
155
|
+
* `stack check`'s checks that the app has each of `paths`: files of the app's own that a package's owned files or commands read, which `pkg`'s README shows under `section`. One check per file, named by its path, so each missing file fails on a line of its own with where to find it.
|
|
156
|
+
*/
|
|
157
|
+
export declare function starterFileChecks(pkg: string, section: string, paths: readonly string[]): Check[];
|
|
158
|
+
/** A package's `./stack` entry's default export: the package, from the app's settings for it (undefined when `stack.config.ts` gives none). It throws, naming the setting, when it needs settings and has none. */
|
|
159
|
+
export type StackFactory<S = never> = (settings: S | undefined) => StackPackage;
|
|
160
|
+
/** A package's settings in `stack.config.ts`, made by that package's helper (`authSettings({ … })`). */
|
|
161
|
+
export type PackageSettings<S = unknown> = {
|
|
162
|
+
package: string;
|
|
163
|
+
settings: S;
|
|
164
|
+
};
|
|
165
|
+
/** The app's `stack.config.ts`: the settings it gives the packages its package.json lists. */
|
|
166
|
+
export type StackConfig = {
|
|
167
|
+
settings: PackageSettings[];
|
|
168
|
+
};
|
|
169
|
+
/** The default export of the app's `stack.config.ts`. Throws on two settings for one package; the `stack` CLI refuses settings for a package package.json does not list. */
|
|
170
|
+
export declare function defineStack(config: {
|
|
171
|
+
settings: PackageSettings[];
|
|
172
|
+
}): StackConfig;
|
|
173
|
+
export type GateResult<P> = {
|
|
174
|
+
ok: true;
|
|
175
|
+
principal: P;
|
|
176
|
+
} | {
|
|
177
|
+
ok: false;
|
|
178
|
+
status: 401 | 404;
|
|
179
|
+
};
|
|
180
|
+
/** Decides who a request is; `apiHandler` answers 401 and 404 with its own envelopes. */
|
|
181
|
+
export type Gate<P> = (headers: Headers) => Promise<GateResult<P>>;
|
|
182
|
+
/** A health probe; its fields join the health response. */
|
|
183
|
+
export type HealthCheck = {
|
|
184
|
+
name: string;
|
|
185
|
+
check(): Promise<Record<string, string>>;
|
|
186
|
+
};
|
|
187
|
+
/** A fragment's variable names: a name checked against them (`"SOME_NAME" satisfies VariableOf<typeof someEnv>`) fails the typecheck when the fragment drops it. */
|
|
188
|
+
export type VariableOf<F extends EnvFragment> = keyof F["schema"] & string;
|
|
189
|
+
/** A package's share of the app's env schema, which the app merges in `defineEnv({ fragments })`. */
|
|
190
|
+
export type EnvFragment<S extends z.ZodRawShape = z.ZodRawShape> = {
|
|
191
|
+
/** The fragment's export name (`coreEnv`), by which `defineEnv` names it in a mistake. */
|
|
192
|
+
name: string;
|
|
193
|
+
schema: S;
|
|
194
|
+
/** Required (non-blank, no default) when NODE_ENV is production. */
|
|
195
|
+
productionRequired?: readonly (keyof S & string)[];
|
|
196
|
+
/** Named all-or-none groups: either every member is set or none is. */
|
|
197
|
+
groups?: Readonly<Record<string, readonly (keyof S & string)[]>>;
|
|
198
|
+
/** Values an unset variable takes during a build or with validation skipped, where nothing connects to what it names. */
|
|
199
|
+
buildPlaceholders?: Partial<Record<keyof S & string, string>>;
|
|
200
|
+
/** Values `stackVitestConfig` puts in `test.env`. */
|
|
201
|
+
testValues?: Partial<Record<keyof S & string, string>>;
|
|
202
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/** The file that gives the app's packages their settings. */
|
|
2
|
+
export const STACK_CONFIG_FILE = "stack.config.ts";
|
|
3
|
+
/** The built-in package's name, which its blocks' markers carry. */
|
|
4
|
+
export const BUILTIN_PACKAGE = "stack";
|
|
5
|
+
/** The core's npm name, which owns its files in the files record and in headers. */
|
|
6
|
+
export const CORE_PACKAGE = "@wwchen/stack";
|
|
7
|
+
/** The file recording which package owns each file `stack sync` writes. */
|
|
8
|
+
export const FILES_RECORD = "stack.files.json";
|
|
9
|
+
/** The modules `stack sync` generates from the packages' runtime pieces, relative to the app's root. */
|
|
10
|
+
export const GENERATED = {
|
|
11
|
+
env: "src/stack/env.ts",
|
|
12
|
+
health: "src/stack/health.ts",
|
|
13
|
+
next: "src/stack/next.ts",
|
|
14
|
+
};
|
|
15
|
+
/** The files `stack sync` writes packages' marked blocks into. */
|
|
16
|
+
export const BLOCK_FILES = {
|
|
17
|
+
agents: "AGENTS.md",
|
|
18
|
+
claude: "CLAUDE.md",
|
|
19
|
+
gitignore: ".gitignore",
|
|
20
|
+
envExample: ".env.example",
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* A package's blocks for `files`, each read from `dir` (the package's `blocks/` directory) under its file's name without the leading dot (`gitignore`, `env.example`), because npm leaves a `.gitignore` out of a published package.
|
|
24
|
+
*/
|
|
25
|
+
export const packageBlocks = (dir, files) => files.map((file) => ({ file, source: new URL(file.replace(/^\./, ""), dir) }));
|
|
26
|
+
/** How much a `stack doctor` finding matters: an error fails the command. */
|
|
27
|
+
export const SEVERITY = { error: "error", warning: "warning", info: "info" };
|
|
28
|
+
/** What `NODE_ENV` holds: tooling sets it (next dev and build, Vitest, Vercel's runtime). */
|
|
29
|
+
export const NODE_ENVS = {
|
|
30
|
+
development: "development",
|
|
31
|
+
test: "test",
|
|
32
|
+
production: "production",
|
|
33
|
+
};
|
|
34
|
+
/** Vercel's environments, which a variable targets and `VERCEL_ENV` names. */
|
|
35
|
+
export const VERCEL_TARGET = {
|
|
36
|
+
production: "production",
|
|
37
|
+
preview: "preview",
|
|
38
|
+
development: "development",
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* `stack check`'s checks that the app has each of `paths`: files of the app's own that a package's owned files or commands read, which `pkg`'s README shows under `section`. One check per file, named by its path, so each missing file fails on a line of its own with where to find it.
|
|
42
|
+
*/
|
|
43
|
+
export function starterFileChecks(pkg, section, paths) {
|
|
44
|
+
return paths.map((path) => ({
|
|
45
|
+
name: path,
|
|
46
|
+
async run(ctx) {
|
|
47
|
+
const { existsSync } = await import("node:fs");
|
|
48
|
+
if (!existsSync(`${ctx.cwd}/${path}`)) {
|
|
49
|
+
throw new Error(`missing: write it as ${pkg}'s README shows it, under "${section}".`);
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
}));
|
|
53
|
+
}
|
|
54
|
+
/** The default export of the app's `stack.config.ts`. Throws on two settings for one package; the `stack` CLI refuses settings for a package package.json does not list. */
|
|
55
|
+
export function defineStack(config) {
|
|
56
|
+
const seen = new Set();
|
|
57
|
+
for (const entry of config.settings ?? []) {
|
|
58
|
+
const name = entry?.package;
|
|
59
|
+
if (typeof name !== "string")
|
|
60
|
+
continue;
|
|
61
|
+
if (seen.has(name))
|
|
62
|
+
throw new Error(`${STACK_CONFIG_FILE}: two settings for ${name}`);
|
|
63
|
+
seen.add(name);
|
|
64
|
+
}
|
|
65
|
+
return config;
|
|
66
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type CommandContext, type DoctorCheck, type EnvVarMeta } from "../stack/contracts.ts";
|
|
2
|
+
export type { EnvVarMeta };
|
|
3
|
+
/** What the reader needs of a command's context: its directory and its `exec`. */
|
|
4
|
+
export type VercelContext = Pick<CommandContext, "cwd" | "exec">;
|
|
5
|
+
/** The linked project: its id, and the `?teamId=` query its team's API calls carry ("" for a personal account). */
|
|
6
|
+
export type LinkedProject = {
|
|
7
|
+
id: string;
|
|
8
|
+
query: string;
|
|
9
|
+
};
|
|
10
|
+
/** A non-empty string, or undefined: how a field of an API answer or a link file is read. */
|
|
11
|
+
export declare const nonEmptyString: (value: unknown) => string | undefined;
|
|
12
|
+
/** A JSON file's object; undefined when there is no such file or it holds no object (`null`, a number). */
|
|
13
|
+
export declare function readJsonFile(path: string): Record<string, unknown> | undefined;
|
|
14
|
+
/** The project `vercel link` wrote in `.vercel/project.json`. */
|
|
15
|
+
export declare function linkedProject(ctx: Pick<VercelContext, "cwd">): LinkedProject;
|
|
16
|
+
/** Runs `vercel api <path>`; a failure names the endpoint and exit code, never the response. */
|
|
17
|
+
export declare function vercelApi(ctx: VercelContext, path: string): Promise<Record<string, unknown>>;
|
|
18
|
+
/** The metadata of one API variable; its value (and anything else) is not copied. */
|
|
19
|
+
export declare function envVarMeta(raw: unknown): EnvVarMeta;
|
|
20
|
+
/** The first page of the project's variables, metadata only; `truncated` when the API has more pages. */
|
|
21
|
+
export declare function listEnvs(ctx: VercelContext, project: LinkedProject): Promise<{
|
|
22
|
+
envs: EnvVarMeta[];
|
|
23
|
+
truncated: boolean;
|
|
24
|
+
}>;
|
|
25
|
+
/**
|
|
26
|
+
* `stack doctor`'s check that each of `keys`, a package's secrets, holds Preview's own value, never production's (runbook §3): a warning for each that is one variable for Production and Preview, in the order given.
|
|
27
|
+
*/
|
|
28
|
+
export declare function previewOwnSecretsCheck(keys: readonly string[]): DoctorCheck;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// The linked Vercel project's metadata, read through the Vercel CLI (`vercel api`): the project `vercel link` wrote, its variables' metadata and the API's answers. Never asks the API to decrypt: a variable's value is dropped the moment the response is parsed, and never printed. For the `stack` commands that audit or change the project's settings; never part of the app's runtime, and it loads nothing but Node's own modules.
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { SEVERITY, VERCEL_TARGET, } from "../stack/contracts.js";
|
|
5
|
+
/** A non-empty string, or undefined: how a field of an API answer or a link file is read. */
|
|
6
|
+
export const nonEmptyString = (value) => typeof value === "string" && value !== "" ? value : undefined;
|
|
7
|
+
/** A JSON file's object; undefined when there is no such file or it holds no object (`null`, a number). */
|
|
8
|
+
export function readJsonFile(path) {
|
|
9
|
+
let text;
|
|
10
|
+
try {
|
|
11
|
+
text = readFileSync(path, "utf8");
|
|
12
|
+
}
|
|
13
|
+
catch (error) {
|
|
14
|
+
if (error.code === "ENOENT")
|
|
15
|
+
return undefined;
|
|
16
|
+
throw error;
|
|
17
|
+
}
|
|
18
|
+
let parsed;
|
|
19
|
+
try {
|
|
20
|
+
parsed = JSON.parse(text);
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
throw new Error(`${path} is not valid JSON.`);
|
|
24
|
+
}
|
|
25
|
+
return typeof parsed === "object" && parsed !== null
|
|
26
|
+
? parsed
|
|
27
|
+
: undefined;
|
|
28
|
+
}
|
|
29
|
+
/** The project `vercel link` wrote in `.vercel/project.json`. */
|
|
30
|
+
export function linkedProject(ctx) {
|
|
31
|
+
const link = readJsonFile(join(ctx.cwd, ".vercel", "project.json"));
|
|
32
|
+
if (!link) {
|
|
33
|
+
throw new Error("this directory is not linked to a Vercel project: run `vercel link`.");
|
|
34
|
+
}
|
|
35
|
+
const id = nonEmptyString(link.projectId);
|
|
36
|
+
if (!id)
|
|
37
|
+
throw new Error(".vercel/project.json has no projectId: run `vercel link`.");
|
|
38
|
+
const orgId = nonEmptyString(link.orgId);
|
|
39
|
+
return { id, query: orgId?.startsWith("team_") ? `?teamId=${encodeURIComponent(orgId)}` : "" };
|
|
40
|
+
}
|
|
41
|
+
/** Runs `vercel api <path>`; a failure names the endpoint and exit code, never the response. */
|
|
42
|
+
export async function vercelApi(ctx, path) {
|
|
43
|
+
const { stdout, code } = await ctx.exec("vercel", ["api", path, "--raw"]);
|
|
44
|
+
if (code !== 0) {
|
|
45
|
+
throw new Error(`vercel api ${path.split("?")[0]} failed (exit ${code}). Is the Vercel CLI signed in (\`vercel login\`)?`);
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
return JSON.parse(stdout);
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
throw new Error(`vercel api ${path.split("?")[0]} did not answer with JSON.`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/** The metadata of one API variable; its value (and anything else) is not copied. */
|
|
55
|
+
export function envVarMeta(raw) {
|
|
56
|
+
const r = (raw ?? {});
|
|
57
|
+
const target = Array.isArray(r.target) ? r.target : r.target === undefined ? [] : [r.target];
|
|
58
|
+
return {
|
|
59
|
+
id: String(r.id ?? ""),
|
|
60
|
+
key: String(r.key ?? ""),
|
|
61
|
+
type: String(r.type ?? ""),
|
|
62
|
+
target: target.filter((t) => typeof t === "string"),
|
|
63
|
+
gitBranch: nonEmptyString(r.gitBranch),
|
|
64
|
+
configurationId: nonEmptyString(r.configurationId),
|
|
65
|
+
comment: nonEmptyString(r.comment),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/** The first page of the project's variables, metadata only; `truncated` when the API has more pages. */
|
|
69
|
+
export async function listEnvs(ctx, project) {
|
|
70
|
+
const listed = await vercelApi(ctx, `/v10/projects/${encodeURIComponent(project.id)}/env${project.query}`);
|
|
71
|
+
const next = listed.pagination?.next;
|
|
72
|
+
return {
|
|
73
|
+
envs: (Array.isArray(listed.envs) ? listed.envs : []).map(envVarMeta),
|
|
74
|
+
truncated: next !== undefined && next !== null,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* `stack doctor`'s check that each of `keys`, a package's secrets, holds Preview's own value, never production's (runbook §3): a warning for each that is one variable for Production and Preview, in the order given.
|
|
79
|
+
*/
|
|
80
|
+
export function previewOwnSecretsCheck(keys) {
|
|
81
|
+
const previewOwnSecrets = async ({ envs }) => keys
|
|
82
|
+
.filter((key) => envs.some((v) => v.key === key &&
|
|
83
|
+
v.target.includes(VERCEL_TARGET.production) &&
|
|
84
|
+
v.target.includes(VERCEL_TARGET.preview)))
|
|
85
|
+
.map((key) => ({
|
|
86
|
+
severity: SEVERITY.warning,
|
|
87
|
+
message: `${key} is one variable for Production and Preview: give Preview its own value, so a preview can never use production's.`,
|
|
88
|
+
}));
|
|
89
|
+
return previewOwnSecrets;
|
|
90
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
---
|
|
2
|
+
description: LDS design system, layout and accessibility conventions for pages and components (owned by @wwchen/stack)
|
|
3
|
+
paths:
|
|
4
|
+
- "src/app/**"
|
|
5
|
+
- "src/components/**"
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
<!-- Owned by @wwchen/stack: `stack sync` writes this file. Put this app's own UI rules in another file under .claude/rules/. -->
|
|
9
|
+
|
|
10
|
+
# UI conventions (stack)
|
|
11
|
+
|
|
12
|
+
- **LDS is the design system**: `@lew-ds/lds` and its React binding `@lew-ds/lds-react`. Import every component from `@wwchen/stack/ui`, never from the package directly: that module routes `Button`, `IconButton`, `Link` and `Row` hrefs through `next/link`. Components emit LDS's own markup and LDS's stylesheet paints them. **Never hand-roll a pill, badge, button, input, table or banner**, and never restyle an `lds-*` class from a call site. If LDS lacks something, fix it upstream in LDS.
|
|
13
|
+
- **Tailwind is for layout** (flex, grid, gap, width, alignment, truncation). Colour, type, radius and shadow utilities exist only as LDS roles, from `@wwchen/stack/ui/globals.css`: `bg-bg`, `bg-surface`, `border-border`, `border-border-subdued`, `text-fg`, `text-fg-subdued`, `text-icon`, `text-accent`, `text-link`, `text-danger`; `text-micro` · `text-secondary` · `text-small` · `text-copy` · `text-emphasis` · `text-section` · `text-headline`; `rounded-card`, `shadow-panel`. No raw hex or px in a component.
|
|
14
|
+
- Which component: an action is a **Button** (one `emphasis="strong"` per view for the main action; `variant="tertiary"` for quiet and cancel); an icon-only action is an **IconButton**, whose one `label` is both its `aria-label` and its Tooltip. Navigation is a **Link**. A list of like things is **Row**s; tabular data is a **Table** with a `<caption>` or `aria-label`. A condition the page states until it changes is a **Banner**; a one-line status beside a control is **Inline** (`role="alert"` for an error).
|
|
15
|
+
- **Icons:** a glyph the app names must be in `APP_ICONS` (`src/components/app-icons.ts`); only those, plus what LDS's components draw, are inlined into the page by `StackRoot`. The icons test (`checkIcons`) keeps the list exact.
|
|
16
|
+
- **Dark mode** is `theme-product mode-dark` on `<html>`, written by the server from the theme cookie in the first response (`resolveTheme`, `htmlClassName`). No client-side theme fix-up. The Tailwind `dark:` variant keys on `.mode-dark`.
|
|
17
|
+
- Every list and async surface specifies loading, empty and error. The empty state names the next action, never "No data".
|
|
18
|
+
- **Storybook** shows every UI state from fixtures: stories sit beside their component as `*.stories.tsx` and render the real components, never a hand-built look. A design change lands in the component and its stories (narrow and wide) in the same change. Check a changed story wide and narrow (`PHONE` and `SMALL_PHONE` from `@wwchen/stack/storybook`), in light and dark; CI's `pnpm storybook:build` only proves every story compiles.
|
|
19
|
+
- Server components by default; `"use client"` on the smallest leaf that needs state or an event handler, never on a layout.
|
|
20
|
+
- Content: sentence case, specific verbs on actions, errors that state the fix.
|
|
21
|
+
- `font-variant-numeric: tabular-nums` (`data-numeric`) on every money and count figure.
|
|
22
|
+
- **Layouts respond to the width a component has (container queries), never to the device.** No user-agent checks; prefer a fluid layout (wrap, `minmax`) to a hard switch.
|
|
23
|
+
- Accessibility is a build gate, not polish: one `h1` per page, landmarks on every layout, a visible focus ring at 3:1 or more, a label on every input, errors as text and not colour alone, 24×24 minimum pointer targets, usable at 200% zoom and 320 CSS px wide with no horizontal scroll.
|
|
24
|
+
- Never render free text from a URL. A page reads short codes from the query (`?sent=1`) and says its own fixed sentence.
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// Owned by @wwchen/stack: `stack sync` writes this file. Put this app's own Next config in `next.config.app.ts`; `withStack` adds the security headers and the production CSP, and `tracing` (generated from the packages) names the files each route reads at run time.
|
|
2
|
+
import { withStack } from "@wwchen/stack/next";
|
|
3
|
+
import appConfig from "./next.config.app.ts";
|
|
4
|
+
import { tracing } from "./src/stack/next.ts";
|
|
5
|
+
|
|
6
|
+
export default withStack(appConfig, { tracing });
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Owned by @wwchen/stack: `stack sync` writes this file. Liveness: always 200, `{ ok: true }` without touching the database. With `Authorization: Bearer $HEALTH_TOKEN` it also reports what each package's health check probes.
|
|
2
|
+
|
|
3
|
+
import { healthRoute } from "@wwchen/stack/next";
|
|
4
|
+
import { env } from "@/lib/env";
|
|
5
|
+
import { healthChecks } from "@/stack/health";
|
|
6
|
+
|
|
7
|
+
export const dynamic = "force-dynamic";
|
|
8
|
+
|
|
9
|
+
export const GET = healthRoute({ token: env.HEALTH_TOKEN, checks: healthChecks(env) });
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wwchen/stack",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Shared Next.js
|
|
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,22 +33,22 @@
|
|
|
32
33
|
"types": "./dist/env/index.d.ts",
|
|
33
34
|
"default": "./dist/env/index.js"
|
|
34
35
|
},
|
|
35
|
-
"./
|
|
36
|
-
"types": "./dist/
|
|
37
|
-
"default": "./dist/
|
|
36
|
+
"./http": {
|
|
37
|
+
"types": "./dist/http/index.d.ts",
|
|
38
|
+
"default": "./dist/http/index.js"
|
|
38
39
|
},
|
|
39
|
-
"./
|
|
40
|
-
"types": "./dist/
|
|
41
|
-
"default": "./dist/
|
|
42
|
-
},
|
|
43
|
-
"./db/testing/isolate": {
|
|
44
|
-
"types": "./dist/db/testing/isolate.d.ts",
|
|
45
|
-
"default": "./dist/db/testing/isolate.js"
|
|
40
|
+
"./vercel": {
|
|
41
|
+
"types": "./dist/vercel/index.d.ts",
|
|
42
|
+
"default": "./dist/vercel/index.js"
|
|
46
43
|
},
|
|
47
44
|
"./connections": {
|
|
48
45
|
"types": "./dist/connections/index.d.ts",
|
|
49
46
|
"default": "./dist/connections/index.js"
|
|
50
47
|
},
|
|
48
|
+
"./connections/env": {
|
|
49
|
+
"types": "./dist/connections/env.d.ts",
|
|
50
|
+
"default": "./dist/connections/env.js"
|
|
51
|
+
},
|
|
51
52
|
"./jobs": {
|
|
52
53
|
"types": "./dist/jobs/index.d.ts",
|
|
53
54
|
"default": "./dist/jobs/index.js"
|
|
@@ -83,20 +84,13 @@
|
|
|
83
84
|
"./blocks/*": "./blocks/*"
|
|
84
85
|
},
|
|
85
86
|
"dependencies": {
|
|
86
|
-
"@vercel/functions": "3.12.0",
|
|
87
|
-
"@wwchen/stack-auth": "0.9.1",
|
|
88
|
-
"@wwchen/stack-notify": "0.9.1",
|
|
89
|
-
"pg": "8.23.1",
|
|
90
87
|
"zod": "4.6.7"
|
|
91
88
|
},
|
|
92
89
|
"peerDependencies": {
|
|
93
|
-
"@electric-sql/pglite": "0.5.10",
|
|
94
|
-
"@electric-sql/pglite-socket": "0.2.13",
|
|
95
90
|
"@lew-ds/lds": "1.3.0",
|
|
96
91
|
"@lew-ds/lds-react": "2.1.0",
|
|
97
92
|
"@lew-ds/open-icons": "1.1.0",
|
|
98
93
|
"@storybook/nextjs-vite": "10.6.2",
|
|
99
|
-
"drizzle-kit": "0.31.11",
|
|
100
94
|
"drizzle-orm": "0.45.4",
|
|
101
95
|
"next": "16.4.6",
|
|
102
96
|
"react": "19.3.1",
|
|
@@ -105,16 +99,10 @@
|
|
|
105
99
|
"typescript": "~6.0.3"
|
|
106
100
|
},
|
|
107
101
|
"peerDependenciesMeta": {
|
|
108
|
-
"@electric-sql/pglite": {
|
|
109
|
-
"optional": true
|
|
110
|
-
},
|
|
111
|
-
"@electric-sql/pglite-socket": {
|
|
112
|
-
"optional": true
|
|
113
|
-
},
|
|
114
102
|
"@storybook/nextjs-vite": {
|
|
115
103
|
"optional": true
|
|
116
104
|
},
|
|
117
|
-
"drizzle-
|
|
105
|
+
"drizzle-orm": {
|
|
118
106
|
"optional": true
|
|
119
107
|
},
|
|
120
108
|
"storybook": {
|
|
@@ -123,13 +111,12 @@
|
|
|
123
111
|
},
|
|
124
112
|
"devDependencies": {
|
|
125
113
|
"@electric-sql/pglite": "0.5.10",
|
|
126
|
-
"@electric-sql/pglite-socket": "0.2.13",
|
|
127
114
|
"@storybook/nextjs-vite": "10.6.2",
|
|
128
115
|
"@types/node": "^24.19.2",
|
|
129
|
-
"@types/pg": "^8.23.1",
|
|
130
116
|
"@types/react": "19.3.0",
|
|
131
117
|
"@types/react-dom": "19.3.0",
|
|
132
118
|
"drizzle-kit": "0.31.11",
|
|
119
|
+
"drizzle-orm": "0.45.4",
|
|
133
120
|
"storybook": "10.6.2",
|
|
134
121
|
"tailwindcss": "4.3.3",
|
|
135
122
|
"typescript": "~6.0.3",
|
package/dist/cli/db.d.ts
DELETED
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