@wwchen/stack 0.9.0 → 0.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -13
- package/blocks/AGENTS.md +4 -12
- package/blocks/CLAUDE.md +8 -15
- package/blocks/env.example +0 -11
- package/blocks/gitignore +1 -2
- package/dist/cli/builtin.d.ts +6 -0
- package/dist/cli/builtin.js +47 -0
- package/dist/cli/context.d.ts +6 -20
- package/dist/cli/context.js +2 -2
- package/dist/cli/host.d.ts +8 -0
- package/dist/cli/host.js +128 -0
- package/dist/cli/index.js +4 -71
- package/dist/cli/neon.d.ts +5 -5
- package/dist/cli/neon.js +20 -10
- package/dist/cli/run.js +1 -1
- package/dist/cli/sync-blocks.d.ts +9 -8
- package/dist/cli/sync-blocks.js +157 -47
- package/dist/configs/vitest.js +1 -6
- 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/db/branches.d.ts +11 -0
- package/dist/db/branches.js +19 -0
- package/dist/db/doctor.d.ts +9 -0
- package/dist/db/doctor.js +120 -0
- package/dist/db/env.d.ts +14 -0
- package/dist/db/env.js +15 -0
- package/dist/db/health.d.ts +10 -0
- package/dist/db/health.js +26 -0
- package/dist/db/index.d.ts +1 -0
- package/dist/db/index.js +1 -0
- package/dist/db/target.js +2 -1
- 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/health.d.ts +5 -7
- package/dist/next/health.js +16 -17
- package/dist/stack/contracts.d.ts +152 -0
- package/dist/stack/contracts.js +42 -0
- package/dist/vercel/index.d.ts +28 -0
- package/dist/vercel/index.js +90 -0
- package/package.json +22 -4
- 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/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/env/stack-env.d.ts +0 -37
- package/dist/env/stack-env.js +0 -54
package/dist/cli/neon.js
CHANGED
|
@@ -4,8 +4,9 @@
|
|
|
4
4
|
//
|
|
5
5
|
// The branch tree: production -> `seed` (the app's migrations and seed rows, no production data) -> `dev/<user>` for each developer and one shared `preview`. Nothing local or preview is ever cut from production, so production's data never leaves it. Production is the project's default branch, whatever it is named (Neon calls a new project's `main`): `defaultBranch`.
|
|
6
6
|
import { endpointOf } from "../db/index.js";
|
|
7
|
+
import { VERCEL_TARGET } from "../stack/contracts.js";
|
|
8
|
+
import { linkedProject, listEnvs } from "../vercel/index.js";
|
|
7
9
|
import { CliError, importApp, loadAppEnv } from "./context.js";
|
|
8
|
-
import { readHosting } from "./doctor.js";
|
|
9
10
|
export const SEED = "seed";
|
|
10
11
|
export const PREVIEW = "preview";
|
|
11
12
|
/** Where the new seed is built; it takes the name `seed` only once it is clean. */
|
|
@@ -18,7 +19,7 @@ export function projectId(env) {
|
|
|
18
19
|
return id;
|
|
19
20
|
}
|
|
20
21
|
/**
|
|
21
|
-
* 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
|
|
22
|
+
* 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`.
|
|
22
23
|
*/
|
|
23
24
|
export function assertDevBranchName(name, production) {
|
|
24
25
|
const yours = "set NEON_DEV_BRANCH to your own branch (default dev/<os user>)";
|
|
@@ -58,7 +59,7 @@ export function defaultBranch(branches) {
|
|
|
58
59
|
return production;
|
|
59
60
|
}
|
|
60
61
|
/**
|
|
61
|
-
*
|
|
62
|
+
* 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).
|
|
62
63
|
*/
|
|
63
64
|
export async function branchEnv(cli, branch, production) {
|
|
64
65
|
const productionId = (production ?? defaultBranch(await listBranches(cli))).id;
|
|
@@ -134,7 +135,7 @@ export async function devBranchEnv(cli) {
|
|
|
134
135
|
return env;
|
|
135
136
|
}
|
|
136
137
|
/**
|
|
137
|
-
* The shared `preview` branch's `branchEnv`, for
|
|
138
|
+
* 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.
|
|
138
139
|
*/
|
|
139
140
|
export async function previewBranchEnv(cli) {
|
|
140
141
|
if (cli.env.DATABASE_URL || cli.env.ENV_FILE) {
|
|
@@ -157,7 +158,7 @@ export async function previewBranchEnv(cli) {
|
|
|
157
158
|
return env;
|
|
158
159
|
}
|
|
159
160
|
/**
|
|
160
|
-
* `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.
|
|
161
|
+
* `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.
|
|
161
162
|
*/
|
|
162
163
|
export async function devSetup(cli) {
|
|
163
164
|
const name = devBranchName(cli);
|
|
@@ -312,15 +313,15 @@ async function deleteOrWarn(cli, branch) {
|
|
|
312
313
|
}
|
|
313
314
|
}
|
|
314
315
|
/**
|
|
315
|
-
* Refuses `--set-vercel` when a variable it would replace is one Vercel variable for Preview and another environment. Reads metadata only
|
|
316
|
+
* Refuses `--set-vercel` when a variable it would replace is one Vercel variable for Preview and another environment. Reads metadata only, never a value.
|
|
316
317
|
*/
|
|
317
318
|
async function refuseSharedPreviewVariables(cli) {
|
|
318
|
-
const { envs } = await
|
|
319
|
+
const { envs } = await listEnvs(cli, linkedProject(cli));
|
|
319
320
|
for (const { name } of PREVIEW_VARIABLES) {
|
|
320
321
|
for (const v of envs) {
|
|
321
|
-
if (v.key !== name || !v.target.includes(
|
|
322
|
+
if (v.key !== name || !v.target.includes(VERCEL_TARGET.preview))
|
|
322
323
|
continue;
|
|
323
|
-
const others = v.target.filter((t) => t !==
|
|
324
|
+
const others = v.target.filter((t) => t !== VERCEL_TARGET.preview);
|
|
324
325
|
if (others.length > 0) {
|
|
325
326
|
throw new CliError(`${name} is one Vercel variable for Preview and ${others.join(", ")}; --set-vercel would change it there too. In Vercel, take Preview off it (Settings, Environment Variables), then run this again. Nothing was changed.`);
|
|
326
327
|
}
|
|
@@ -372,7 +373,16 @@ export async function resetPreview(cli, args = []) {
|
|
|
372
373
|
values.push({ name, value });
|
|
373
374
|
}
|
|
374
375
|
for (const { name, value } of values) {
|
|
375
|
-
const { code } = await cli.exec("vercel", [
|
|
376
|
+
const { code } = await cli.exec("vercel", [
|
|
377
|
+
"env",
|
|
378
|
+
"add",
|
|
379
|
+
name,
|
|
380
|
+
"--environment",
|
|
381
|
+
VERCEL_TARGET.preview,
|
|
382
|
+
"--sensitive",
|
|
383
|
+
"--force",
|
|
384
|
+
"--yes",
|
|
385
|
+
], { input: value });
|
|
376
386
|
if (code !== 0) {
|
|
377
387
|
throw new CliError(`vercel env add ${name} failed (exit ${code}). Is the Vercel CLI signed in (\`vercel login\`) ` +
|
|
378
388
|
"and this directory linked (`vercel link`)? Re-run with --set-vercel once it is.");
|
package/dist/cli/run.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// `stack run <script> [args...]` — runs one of the app's own scripts (`scripts/backfill.ts`, say) with this Node, type stripping and all, and the environment every other command gives the app: your Neon dev branch unless DATABASE_URL or ENV_FILE says otherwise, then the app's `.env` (or the file ENV_FILE names) for whatever is still unset. ENV_FILE is also the deliberate production opt-in, exactly as for `
|
|
1
|
+
// `stack run <script> [args...]` — runs one of the app's own scripts (`scripts/backfill.ts`, say) with this Node, type stripping and all, and the environment every other command gives the app: your Neon dev branch unless DATABASE_URL or ENV_FILE says otherwise, then the app's `.env` (or the file ENV_FILE names) for whatever is still unset. ENV_FILE is also the deliberate production opt-in, exactly as for `databaseEnv` (`@wwchen/stack/db/branches`). The arguments and the exit code pass through.
|
|
2
2
|
import { resolve } from "node:path";
|
|
3
3
|
import { assertDevDatabase } from "../db/index.js";
|
|
4
4
|
import { loadAppEnv } from "./context.js";
|
|
@@ -1,14 +1,15 @@
|
|
|
1
|
+
import { type Command, type StackPackage } from "../stack/contracts.ts";
|
|
1
2
|
/**
|
|
2
|
-
* Brings
|
|
3
|
+
* Brings every composed package's marked blocks in AGENTS.md, CLAUDE.md, .gitignore and .env.example up to date, removes the blocks of packages the app no longer composes, and returns the files that were (or, with `check`, would be) changed, and each block that was (or would be) removed. Every file is checked before any is written, so a malformed one leaves all of them untouched.
|
|
3
4
|
*/
|
|
4
|
-
export declare function syncBlocks(cwd: string, opts?: {
|
|
5
|
+
export declare function syncBlocks(cwd: string, packages: readonly StackPackage[], opts?: {
|
|
5
6
|
check?: boolean;
|
|
6
7
|
}): Promise<{
|
|
7
8
|
changed: string[];
|
|
9
|
+
removed: {
|
|
10
|
+
file: string;
|
|
11
|
+
name: string;
|
|
12
|
+
}[];
|
|
8
13
|
}>;
|
|
9
|
-
/** `stack sync-blocks [--check]`: exits 1 when `--check` finds a stale file. */
|
|
10
|
-
export declare
|
|
11
|
-
cwd: string;
|
|
12
|
-
log(m: string): void;
|
|
13
|
-
error(m: string): void;
|
|
14
|
-
}, args: string[]): Promise<number>;
|
|
14
|
+
/** `stack sync-blocks [--check]`: writes each composed package's blocks; exits 1 when `--check` finds a stale file. */
|
|
15
|
+
export declare const syncBlocksCommand: Command;
|
package/dist/cli/sync-blocks.js
CHANGED
|
@@ -1,18 +1,31 @@
|
|
|
1
1
|
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
2
|
import { join } from "node:path";
|
|
3
3
|
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { BLOCK_FILES, BUILTIN_PACKAGE, } from "../stack/contracts.js";
|
|
4
5
|
import { CliError } from "./context.js";
|
|
5
|
-
const MARKDOWN = {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
6
|
+
const MARKDOWN = {
|
|
7
|
+
begin: (name) => `<!-- BEGIN:${name} -->`,
|
|
8
|
+
end: (name) => `<!-- END:${name} -->`,
|
|
9
|
+
marker: /^<!-- (BEGIN|END):(\S+) -->$/,
|
|
10
|
+
};
|
|
11
|
+
const HASH = {
|
|
12
|
+
begin: (name) => `# BEGIN:${name}`,
|
|
13
|
+
end: (name) => `# END:${name}`,
|
|
14
|
+
marker: /^# (BEGIN|END):(\S+)$/,
|
|
15
|
+
};
|
|
16
|
+
/** Each managed file's marker style. */
|
|
17
|
+
const STYLES = {
|
|
18
|
+
[BLOCK_FILES.agents]: MARKDOWN,
|
|
19
|
+
[BLOCK_FILES.claude]: MARKDOWN,
|
|
20
|
+
[BLOCK_FILES.gitignore]: HASH,
|
|
21
|
+
[BLOCK_FILES.envExample]: HASH,
|
|
22
|
+
};
|
|
23
|
+
/** The files `sync-blocks` manages, in the order it reports them, and each one's marker style. */
|
|
24
|
+
const TARGETS = Object.values(BLOCK_FILES).map((file) => ({ file, style: STYLES[file] }));
|
|
25
|
+
/**
|
|
26
|
+
* Whether a marker name is a package's: the built-in one, or a scoped npm name. Every package that writes blocks has one of these names, so a block left by a package the app no longer composes is still recognised, while another tool's marked block (Next's `nextjs-agent-rules`) is never the stack's to touch.
|
|
27
|
+
*/
|
|
28
|
+
const isPackageName = (name) => name === BUILTIN_PACKAGE || /^@[^/\s]+\/[^/\s]+$/.test(name);
|
|
16
29
|
/** The lines of `source` with their offsets; `end` includes the line break, `text` does not. */
|
|
17
30
|
function linesOf(source) {
|
|
18
31
|
const lines = [];
|
|
@@ -26,68 +39,165 @@ function linesOf(source) {
|
|
|
26
39
|
return lines;
|
|
27
40
|
}
|
|
28
41
|
/**
|
|
29
|
-
*
|
|
42
|
+
* Every package block in the file, in file order. Anything ambiguous (a marker with stray whitespace, a marker without its partner, reversed, repeated or overlapping blocks) throws: guessing which text a package owns could delete the app's own.
|
|
43
|
+
*/
|
|
44
|
+
function findBlocks(file, lines, style) {
|
|
45
|
+
const fix = "Fix the markers by hand; nothing was changed.";
|
|
46
|
+
const marks = [];
|
|
47
|
+
lines.forEach((line, index) => {
|
|
48
|
+
const exact = style.marker.exec(line.text);
|
|
49
|
+
const match = exact ?? style.marker.exec(line.text.trim());
|
|
50
|
+
if (!match?.[1] || !match[2] || !isPackageName(match[2]))
|
|
51
|
+
return;
|
|
52
|
+
if (!exact) {
|
|
53
|
+
throw new CliError(`${file}: marker "${line.text.trim()}" has leading or trailing whitespace. ${fix}`);
|
|
54
|
+
}
|
|
55
|
+
marks.push({ kind: match[1], name: match[2], index });
|
|
56
|
+
});
|
|
57
|
+
const blocks = [];
|
|
58
|
+
for (const name of new Set(marks.map((m) => m.name))) {
|
|
59
|
+
const begins = marks.filter((m) => m.name === name && m.kind === "BEGIN");
|
|
60
|
+
const ends = marks.filter((m) => m.name === name && m.kind === "END");
|
|
61
|
+
const [begin] = begins;
|
|
62
|
+
const [end] = ends;
|
|
63
|
+
if (begins.length !== 1 || ends.length !== 1 || !begin || !end) {
|
|
64
|
+
throw new CliError(`${file}: expected exactly one "${style.begin(name)}" and one "${style.end(name)}", found ${begins.length} and ${ends.length}. ${fix}`);
|
|
65
|
+
}
|
|
66
|
+
if (end.index < begin.index) {
|
|
67
|
+
throw new CliError(`${file}: "${style.end(name)}" comes before "${style.begin(name)}". ${fix}`);
|
|
68
|
+
}
|
|
69
|
+
blocks.push({ name, begin: begin.index, end: end.index });
|
|
70
|
+
}
|
|
71
|
+
blocks.sort((a, b) => a.begin - b.begin);
|
|
72
|
+
blocks.forEach((block, i) => {
|
|
73
|
+
const previous = blocks[i - 1];
|
|
74
|
+
if (previous && block.begin < previous.end) {
|
|
75
|
+
throw new CliError(`${file}: the blocks of ${previous.name} and ${block.name} overlap. ${fix}`);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
return blocks;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Brings `source`'s package blocks in line with `wanted` (each package's name and block lines, in config order): a present block's text is replaced, a block for a package not in `wanted` is removed with the blank line before it, and a missing one is appended after the last block (or at the end of the file), so text outside every block is kept byte for byte. `removed` names the packages whose blocks went, in file order.
|
|
30
82
|
*/
|
|
31
|
-
function apply(file, source,
|
|
83
|
+
function apply(file, source, wanted, style) {
|
|
32
84
|
const eol = source.includes("\r\n") ? "\r\n" : "\n";
|
|
33
|
-
const block = body.map((line) => line + eol).join("");
|
|
34
85
|
const lines = linesOf(source);
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
86
|
+
const found = findBlocks(file, lines, style);
|
|
87
|
+
const bodies = new Map(wanted.map(({ name, body }) => [name, body]));
|
|
88
|
+
const marked = (name, body) => [style.begin(name), ...body, style.end(name)].map((line) => line + eol).join("");
|
|
89
|
+
// Each edit replaces `source.slice(start, end)` with `text`; none overlap.
|
|
90
|
+
const edits = [];
|
|
91
|
+
let last;
|
|
92
|
+
const removed = [];
|
|
93
|
+
for (const block of found) {
|
|
94
|
+
const begin = lines[block.begin];
|
|
95
|
+
const end = lines[block.end];
|
|
96
|
+
const body = bodies.get(block.name);
|
|
97
|
+
if (body) {
|
|
98
|
+
edits.push({ start: begin.end, end: end.start, text: body.map((l) => l + eol).join("") });
|
|
99
|
+
last = end;
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
const before = lines[block.begin - 1];
|
|
103
|
+
const blank = before !== undefined && before.text === "";
|
|
104
|
+
edits.push({ start: blank ? before.start : begin.start, end: end.end, text: "" });
|
|
105
|
+
removed.push(block.name);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const present = new Set(found.map((b) => b.name));
|
|
109
|
+
const added = wanted
|
|
110
|
+
.filter(({ name }) => !present.has(name))
|
|
111
|
+
.map(({ name, body }) => marked(name, body))
|
|
112
|
+
.join(eol);
|
|
113
|
+
if (added && last) {
|
|
114
|
+
const lead = source.slice(last.start, last.end).endsWith("\n") ? "" : eol;
|
|
115
|
+
edits.push({ start: last.end, end: last.end, text: `${lead}${eol}${added}` });
|
|
39
116
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
117
|
+
// Forward, with an insertion before a removal that starts where it does.
|
|
118
|
+
edits.sort((a, b) => a.start - b.start || a.end - b.end);
|
|
119
|
+
let out = "";
|
|
120
|
+
let cursor = 0;
|
|
121
|
+
for (const edit of edits) {
|
|
122
|
+
out += source.slice(cursor, edit.start) + edit.text;
|
|
123
|
+
cursor = edit.end;
|
|
45
124
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
125
|
+
out += source.slice(cursor);
|
|
126
|
+
if (!added || last)
|
|
127
|
+
return { text: out, removed };
|
|
128
|
+
const lead = out === "" ? "" : out.endsWith("\n") ? eol : eol + eol;
|
|
129
|
+
return { text: `${out}${lead}${added}`, removed };
|
|
130
|
+
}
|
|
131
|
+
/** A block's lines, without trailing blank lines. */
|
|
132
|
+
function readBody(pkg, source) {
|
|
133
|
+
let text;
|
|
134
|
+
try {
|
|
135
|
+
text = readFileSync(fileURLToPath(source), "utf8");
|
|
50
136
|
}
|
|
51
|
-
|
|
52
|
-
throw new CliError(`${
|
|
137
|
+
catch (error) {
|
|
138
|
+
throw new CliError(`${pkg}: its block ${source.href} cannot be read (${error instanceof Error ? error.message : String(error)}). Reinstall the package; nothing was changed.`);
|
|
53
139
|
}
|
|
54
|
-
return
|
|
140
|
+
return text.replace(/\s+$/, "").split(/\r?\n/);
|
|
55
141
|
}
|
|
56
|
-
/**
|
|
57
|
-
function
|
|
58
|
-
|
|
142
|
+
/** Each managed file's blocks, package by package in config order. */
|
|
143
|
+
function wantedBlocks(packages) {
|
|
144
|
+
const wanted = new Map(TARGETS.map(({ file }) => [file, []]));
|
|
145
|
+
for (const pkg of packages) {
|
|
146
|
+
if (!pkg.blocks?.length)
|
|
147
|
+
continue;
|
|
148
|
+
if (!isPackageName(pkg.name)) {
|
|
149
|
+
throw new CliError(`${pkg.name}: a package that writes blocks needs a scoped name (@scope/name), so its block can be found once the app stops composing it. Nothing was changed.`);
|
|
150
|
+
}
|
|
151
|
+
for (const { file, source } of pkg.blocks) {
|
|
152
|
+
const list = wanted.get(file);
|
|
153
|
+
if (!list) {
|
|
154
|
+
throw new CliError(`${pkg.name}: sync-blocks writes no block into ${file}; it manages ${TARGETS.map((t) => t.file).join(", ")}. Nothing was changed.`);
|
|
155
|
+
}
|
|
156
|
+
if (list.some((b) => b.name === pkg.name)) {
|
|
157
|
+
throw new CliError(`${pkg.name}: gives two blocks for ${file}. Nothing was changed.`);
|
|
158
|
+
}
|
|
159
|
+
list.push({ name: pkg.name, body: readBody(pkg.name, source) });
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return wanted;
|
|
59
163
|
}
|
|
60
164
|
/**
|
|
61
|
-
* Brings
|
|
165
|
+
* Brings every composed package's marked blocks in AGENTS.md, CLAUDE.md, .gitignore and .env.example up to date, removes the blocks of packages the app no longer composes, and returns the files that were (or, with `check`, would be) changed, and each block that was (or would be) removed. Every file is checked before any is written, so a malformed one leaves all of them untouched.
|
|
62
166
|
*/
|
|
63
|
-
export async function syncBlocks(cwd, opts = {}) {
|
|
167
|
+
export async function syncBlocks(cwd, packages, opts = {}) {
|
|
168
|
+
const wanted = wantedBlocks(packages);
|
|
64
169
|
const writes = [];
|
|
65
|
-
|
|
170
|
+
const removed = [];
|
|
171
|
+
for (const { file, style } of TARGETS) {
|
|
66
172
|
const path = join(cwd, file);
|
|
67
173
|
const before = existsSync(path) ? readFileSync(path, "utf8") : "";
|
|
68
|
-
const after = apply(file, before,
|
|
69
|
-
if (after !== before)
|
|
70
|
-
writes.push({ path, file, text: after });
|
|
174
|
+
const after = apply(file, before, wanted.get(file) ?? [], style);
|
|
175
|
+
if (after.text !== before)
|
|
176
|
+
writes.push({ path, file, text: after.text });
|
|
177
|
+
removed.push(...after.removed.map((name) => ({ file, name })));
|
|
71
178
|
}
|
|
72
179
|
if (!opts.check)
|
|
73
180
|
for (const w of writes)
|
|
74
181
|
writeFileSync(w.path, w.text);
|
|
75
|
-
return { changed: writes.map((w) => w.file) };
|
|
182
|
+
return { changed: writes.map((w) => w.file), removed };
|
|
76
183
|
}
|
|
77
|
-
/** `stack sync-blocks [--check]`: exits 1 when `--check` finds a stale file. */
|
|
78
|
-
export async
|
|
184
|
+
/** `stack sync-blocks [--check]`: writes each composed package's blocks; exits 1 when `--check` finds a stale file. */
|
|
185
|
+
export const syncBlocksCommand = async (ctx, args) => {
|
|
79
186
|
const unknown = args.filter((a) => a !== "--check");
|
|
80
187
|
if (unknown.length > 0)
|
|
81
188
|
throw new CliError(`sync-blocks: unknown argument ${unknown[0]}`);
|
|
82
189
|
const check = args.includes("--check");
|
|
83
|
-
const { changed } = await syncBlocks(
|
|
190
|
+
const { changed, removed } = await syncBlocks(ctx.cwd, ctx.packages, { check });
|
|
84
191
|
if (check && changed.length > 0) {
|
|
85
|
-
|
|
192
|
+
ctx.error(`stale stack blocks: ${changed.join(", ")}. Run \`stack sync-blocks\`.`);
|
|
86
193
|
return 1;
|
|
87
194
|
}
|
|
195
|
+
// Removing a block is the one thing sync-blocks deletes, so each is said: a package the app composes no longer, or one stack.config.ts drops by mistake.
|
|
196
|
+
for (const { file, name } of removed)
|
|
197
|
+
ctx.log(`removed ${name}'s block from ${file}`);
|
|
88
198
|
for (const file of changed)
|
|
89
|
-
|
|
199
|
+
ctx.log(`updated ${file}`);
|
|
90
200
|
if (changed.length === 0)
|
|
91
|
-
|
|
201
|
+
ctx.log("stack blocks are up to date");
|
|
92
202
|
return 0;
|
|
93
|
-
}
|
|
203
|
+
};
|
package/dist/configs/vitest.js
CHANGED
|
@@ -33,12 +33,7 @@ export function stackVitestConfig(overrides = {}, options = {}) {
|
|
|
33
33
|
...toArray(include),
|
|
34
34
|
],
|
|
35
35
|
exclude: ["node_modules/**", ".next/**", "dist/**", "tests/e2e/**", ...toArray(exclude)],
|
|
36
|
-
env: {
|
|
37
|
-
TZ: "UTC",
|
|
38
|
-
// A fixed, test-only value so the suite is self-sufficient on a clean checkout (CI has no `.env`). It is not a secret and must never be used outside tests.
|
|
39
|
-
BETTER_AUTH_SECRET: "test-only-better-auth-secret-not-a-real-secret",
|
|
40
|
-
...env,
|
|
41
|
-
},
|
|
36
|
+
env: { TZ: "UTC", ...env },
|
|
42
37
|
},
|
|
43
38
|
};
|
|
44
39
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/** Connected accounts' variables, an env fragment for `defineEnv({ fragments })`. */
|
|
3
|
+
export declare const connectionsEnv: {
|
|
4
|
+
readonly name: "connectionsEnv";
|
|
5
|
+
readonly schema: {
|
|
6
|
+
readonly TOKEN_ENCRYPTION_KEY: z.ZodOptional<z.ZodString>;
|
|
7
|
+
};
|
|
8
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Loaded by the app's `env.ts`, so it imports nothing but zod: never the connections module itself.
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
/** Connected accounts' variables, an env fragment for `defineEnv({ fragments })`. */
|
|
4
|
+
export const connectionsEnv = {
|
|
5
|
+
name: "connectionsEnv",
|
|
6
|
+
schema: {
|
|
7
|
+
// Encrypts connected-account tokens (`@wwchen/stack/connections`): 32 bytes, base64 (`openssl rand -base64 32`). Keep it forever: a new key makes every stored token unreadable.
|
|
8
|
+
TOKEN_ENCRYPTION_KEY: z
|
|
9
|
+
.string()
|
|
10
|
+
.refine((v) => Buffer.from(v, "base64").length === 32, {
|
|
11
|
+
message: "must be 32 bytes, base64-encoded (openssl rand -base64 32)",
|
|
12
|
+
})
|
|
13
|
+
.optional(),
|
|
14
|
+
},
|
|
15
|
+
};
|
|
@@ -23,7 +23,7 @@ export type ConnectionsOptions = {
|
|
|
23
23
|
db: Database;
|
|
24
24
|
table: ConnectionTable;
|
|
25
25
|
providers: ConnectionProvider[];
|
|
26
|
-
/** Signs the OAuth state
|
|
26
|
+
/** Signs the OAuth state: a long secret of the app's own; a sub-key is derived from it. */
|
|
27
27
|
secret: string;
|
|
28
28
|
/** `TOKEN_ENCRYPTION_KEY`: 32 bytes, base64. Checked on first use, not at construction. */
|
|
29
29
|
encryptionKey: string | undefined;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createHmac, randomBytes, timingSafeEqual } from "node:crypto";
|
|
2
|
-
import { safeNext } from "@wwchen/stack-auth";
|
|
3
2
|
import { and, asc, eq } from "drizzle-orm";
|
|
3
|
+
import { safeNext } from "../http/index.js";
|
|
4
4
|
import { decryptToken, encryptToken, parseEncryptionKey } from "./crypto.js";
|
|
5
5
|
/**
|
|
6
6
|
* Connected accounts: a signed-in person connects an account at another service so the app can work on their behalf. This is deliberately not sign-in — a connection never signs anyone in and never creates a user; people sign in by email.
|
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type AnyPgColumn } from "drizzle-orm/pg-core";
|
|
2
2
|
/**
|
|
3
|
-
* The `connection` table: an account at another service that a signed-in person connected so the app can work on their behalf. The app re-exports it from its schema next to
|
|
3
|
+
* The `connection` table: an account at another service that a signed-in person connected so the app can work on their behalf. The app re-exports it from its schema next to its user table, so drizzle-kit generates it into the app's own migrations.
|
|
4
4
|
*
|
|
5
5
|
* - One connection per person per provider (reconnecting replaces the token).
|
|
6
6
|
* - One person per provider account (`subject`): an account already connected to someone else is refused, so two people can never share one account's data.
|
|
7
7
|
* - Removing the person removes their connections (the foreign key cascades in the same statement).
|
|
8
8
|
* - `token` is only ever ciphertext (`crypto.ts`).
|
|
9
9
|
*/
|
|
10
|
-
export declare function connectionTable(
|
|
10
|
+
export declare function connectionTable(users: {
|
|
11
|
+
id: AnyPgColumn;
|
|
12
|
+
}): import("drizzle-orm/pg-core").PgTableWithColumns<{
|
|
11
13
|
name: "connection";
|
|
12
14
|
schema: undefined;
|
|
13
15
|
columns: {
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import { pgTable, text, timestamp, unique, uuid } from "drizzle-orm/pg-core";
|
|
2
2
|
/**
|
|
3
|
-
* The `connection` table: an account at another service that a signed-in person connected so the app can work on their behalf. The app re-exports it from its schema next to
|
|
3
|
+
* The `connection` table: an account at another service that a signed-in person connected so the app can work on their behalf. The app re-exports it from its schema next to its user table, so drizzle-kit generates it into the app's own migrations.
|
|
4
4
|
*
|
|
5
5
|
* - One connection per person per provider (reconnecting replaces the token).
|
|
6
6
|
* - One person per provider account (`subject`): an account already connected to someone else is refused, so two people can never share one account's data.
|
|
7
7
|
* - Removing the person removes their connections (the foreign key cascades in the same statement).
|
|
8
8
|
* - `token` is only ever ciphertext (`crypto.ts`).
|
|
9
9
|
*/
|
|
10
|
-
export function connectionTable(
|
|
10
|
+
export function connectionTable(users) {
|
|
11
11
|
return pgTable("connection", {
|
|
12
12
|
id: uuid("id").primaryKey().defaultRandom(),
|
|
13
13
|
userId: uuid("user_id")
|
|
14
14
|
.notNull()
|
|
15
|
-
.references(() =>
|
|
15
|
+
.references(() => users.id, { onDelete: "cascade" }),
|
|
16
16
|
provider: text("provider").notNull(),
|
|
17
17
|
subject: text("subject").notNull(),
|
|
18
18
|
name: text("name"),
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { loadAppEnv } from "../cli/context.ts";
|
|
2
|
+
import type { CommandContext } from "../stack/contracts.ts";
|
|
3
|
+
/** Loads the app's `.env`, or the file `ENV_FILE` names (the deliberate production opt-in), into `ctx.env`, filling in only what is still unset: what every `stack` command that runs the app's code does after `databaseEnv`. */
|
|
4
|
+
export { loadAppEnv };
|
|
5
|
+
/**
|
|
6
|
+
* The variables that point a command at its database, to assign onto `ctx.env` before the app's modules load: your Neon dev branch's URLs and production's host (`dev/<username>`, or NEON_DEV_BRANCH), or nothing when DATABASE_URL or ENV_FILE already names a database. With `preview`, the shared `preview` branch's, refusing when DATABASE_URL or ENV_FILE is also set, and refusing a `preview` that is production's database. Every refusal throws with the reason; the Neon CLI runs through `ctx.exec`, and `[neon] <branch>` goes to `ctx.error`.
|
|
7
|
+
*/
|
|
8
|
+
export declare function databaseEnv(ctx: Pick<CommandContext, "cwd" | "env" | "exec" | "log" | "error">, opts: {
|
|
9
|
+
preview: boolean;
|
|
10
|
+
username?: string;
|
|
11
|
+
}): Promise<Record<string, string>>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// `@wwchen/stack/db/branches`: the Neon branch a package's command works on, for a command that reaches the app's database. Its own export, never re-exported from `@wwchen/stack/db`: the app's runtime bundles that one, and this module loads the CLI's Neon helpers.
|
|
2
|
+
import { loadAppEnv, resolveDeps } from "../cli/context.js";
|
|
3
|
+
import { devBranchEnv, previewBranchEnv } from "../cli/neon.js";
|
|
4
|
+
/** Loads the app's `.env`, or the file `ENV_FILE` names (the deliberate production opt-in), into `ctx.env`, filling in only what is still unset: what every `stack` command that runs the app's code does after `databaseEnv`. */
|
|
5
|
+
export { loadAppEnv };
|
|
6
|
+
/**
|
|
7
|
+
* The variables that point a command at its database, to assign onto `ctx.env` before the app's modules load: your Neon dev branch's URLs and production's host (`dev/<username>`, or NEON_DEV_BRANCH), or nothing when DATABASE_URL or ENV_FILE already names a database. With `preview`, the shared `preview` branch's, refusing when DATABASE_URL or ENV_FILE is also set, and refusing a `preview` that is production's database. Every refusal throws with the reason; the Neon CLI runs through `ctx.exec`, and `[neon] <branch>` goes to `ctx.error`.
|
|
8
|
+
*/
|
|
9
|
+
export async function databaseEnv(ctx, opts) {
|
|
10
|
+
const cli = resolveDeps({
|
|
11
|
+
cwd: ctx.cwd,
|
|
12
|
+
env: ctx.env,
|
|
13
|
+
exec: ctx.exec,
|
|
14
|
+
log: ctx.log,
|
|
15
|
+
error: ctx.error,
|
|
16
|
+
...(opts.username === undefined ? {} : { username: opts.username }),
|
|
17
|
+
});
|
|
18
|
+
return opts.preview ? previewBranchEnv(cli) : devBranchEnv(cli);
|
|
19
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type BuildStep, type DoctorFacts, type Finding } from "../stack/contracts.ts";
|
|
2
|
+
/** Every variable the stack's env reads a database connection from. */
|
|
3
|
+
export declare const DATABASE_URL_VARIABLES: readonly ["DATABASE_URL", "DATABASE_URL_UNPOOLED"];
|
|
4
|
+
/** What the Neon Vercel integration writes on every variable it creates. */
|
|
5
|
+
export declare const NEON_INTEGRATION_COMMENT = "The Neon Postgres integration sets this environment variable automatically";
|
|
6
|
+
/** Migrates a Preview or Production deployment's database before `next build`; a refusal or a failed migration throws, which fails the build. */
|
|
7
|
+
export declare const migrateStep: BuildStep;
|
|
8
|
+
/** The database's and the core's variables, on metadata alone. */
|
|
9
|
+
export declare function databaseChecks(h: DoctorFacts): Promise<Finding[]>;
|