@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,156 @@
|
|
|
1
|
+
import { existsSync, realpathSync } from "node:fs";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { basename, dirname, join } from "node:path";
|
|
4
|
+
import { CORE_PACKAGE } from "../stack/contracts.js";
|
|
5
|
+
import { readJsonFile } from "../vercel/index.js";
|
|
6
|
+
import { CliError } from "./context.js";
|
|
7
|
+
/** Node's lookup of a package from `from`: `node_modules/<name>` in `from` and each ancestor that is not itself a `node_modules` folder. */
|
|
8
|
+
function locate(name, from) {
|
|
9
|
+
for (let dir = from;; dir = dirname(dir)) {
|
|
10
|
+
if (basename(dir) !== "node_modules") {
|
|
11
|
+
const candidate = join(dir, "node_modules", name);
|
|
12
|
+
if (existsSync(join(candidate, "package.json")))
|
|
13
|
+
return realpathSync(candidate);
|
|
14
|
+
}
|
|
15
|
+
if (dirname(dir) === dir)
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/** The installed package at `dir`'s manifest, which `name` is named for when it does not parse. */
|
|
20
|
+
function manifestOf(dir, name) {
|
|
21
|
+
try {
|
|
22
|
+
return (readJsonFile(join(dir, "package.json")) ?? {});
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
throw new CliError(`${name}: its package.json is not valid JSON: run \`pnpm install --force\`.`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/** A stack package declares a `./stack` export. */
|
|
29
|
+
function isStackPackage(manifest) {
|
|
30
|
+
const exports = manifest.exports;
|
|
31
|
+
return typeof exports === "object" && exports !== null && "./stack" in exports;
|
|
32
|
+
}
|
|
33
|
+
const requiredPeers = (manifest) => Object.keys(manifest.peerDependencies ?? {}).filter((peer) => manifest.peerDependenciesMeta?.[peer]?.optional !== true);
|
|
34
|
+
/** Names joined as prose: `A`, `A and B`, `A, B and C`. */
|
|
35
|
+
function joinNames(names) {
|
|
36
|
+
return names.length < 2
|
|
37
|
+
? names.join("")
|
|
38
|
+
: `${names.slice(0, -1).join(", ")} and ${names[names.length - 1]}`;
|
|
39
|
+
}
|
|
40
|
+
/** Packages that lie on a cycle: each reaches itself through `needs`. */
|
|
41
|
+
function onCycle(stuck) {
|
|
42
|
+
const needs = new Map(stuck.map((p) => [p.name, p.needs]));
|
|
43
|
+
const reaches = (from, target) => {
|
|
44
|
+
const seen = new Set();
|
|
45
|
+
const queue = [...(needs.get(from) ?? [])];
|
|
46
|
+
for (let next = queue.pop(); next !== undefined; next = queue.pop()) {
|
|
47
|
+
if (next === target)
|
|
48
|
+
return true;
|
|
49
|
+
if (seen.has(next))
|
|
50
|
+
continue;
|
|
51
|
+
seen.add(next);
|
|
52
|
+
queue.push(...(needs.get(next) ?? []));
|
|
53
|
+
}
|
|
54
|
+
return false;
|
|
55
|
+
};
|
|
56
|
+
return stuck
|
|
57
|
+
.map((p) => p.name)
|
|
58
|
+
.filter((name) => reaches(name, name))
|
|
59
|
+
.sort();
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The stack packages the app at `cwd` lists in `dependencies` and `devDependencies`, each after the ones it needs (ties by name). Refuses with a `CliError` when `cwd` has no package.json, a listed package is not installed, a package is not at `coreVersion`, a stack peer is not listed, packages need each other, a package.json does not parse, or a package's `./stack` entry does not exist.
|
|
63
|
+
*/
|
|
64
|
+
export function discover(cwd, coreVersion) {
|
|
65
|
+
const manifest = join(cwd, "package.json");
|
|
66
|
+
// Every command reads the app from its root, and `stack sync` writes there: a folder with no package.json is not an app's root.
|
|
67
|
+
if (!existsSync(manifest)) {
|
|
68
|
+
throw new CliError(`${cwd} has no package.json: run \`stack\` from the app's root.`);
|
|
69
|
+
}
|
|
70
|
+
let app;
|
|
71
|
+
try {
|
|
72
|
+
app = (readJsonFile(manifest) ?? {});
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
throw new CliError("package.json is not valid JSON: fix it, then run again.");
|
|
76
|
+
}
|
|
77
|
+
const listed = [
|
|
78
|
+
...new Set([...Object.keys(app.dependencies ?? {}), ...Object.keys(app.devDependencies ?? {})]),
|
|
79
|
+
].sort();
|
|
80
|
+
const dirs = new Map();
|
|
81
|
+
const missing = [];
|
|
82
|
+
for (const name of listed) {
|
|
83
|
+
const dir = locate(name, cwd);
|
|
84
|
+
if (dir)
|
|
85
|
+
dirs.set(name, dir);
|
|
86
|
+
else
|
|
87
|
+
missing.push(`${name} is in package.json but not installed: run \`pnpm install\`.`);
|
|
88
|
+
}
|
|
89
|
+
if (missing.length > 0)
|
|
90
|
+
throw new CliError(missing.join("\n"));
|
|
91
|
+
const stack = listed.filter((name) => {
|
|
92
|
+
const dir = dirs.get(name);
|
|
93
|
+
return name !== CORE_PACKAGE && isStackPackage(manifestOf(dir, name));
|
|
94
|
+
});
|
|
95
|
+
const wrong = stack
|
|
96
|
+
.map((name) => ({ name, version: manifestOf(dirs.get(name), name).version }))
|
|
97
|
+
.filter((p) => p.version !== coreVersion);
|
|
98
|
+
if (wrong.length > 0) {
|
|
99
|
+
const parts = [
|
|
100
|
+
...wrong.map((p) => `${p.name} is ${p.version}`),
|
|
101
|
+
`${CORE_PACKAGE} is ${coreVersion}`,
|
|
102
|
+
];
|
|
103
|
+
throw new CliError(`${joinNames(parts)}: the suite is one version. Install every ${CORE_PACKAGE} package at ${coreVersion}.`);
|
|
104
|
+
}
|
|
105
|
+
const unlisted = [];
|
|
106
|
+
const found = new Map();
|
|
107
|
+
const require = createRequire(join(cwd, "package.json"));
|
|
108
|
+
for (const name of stack) {
|
|
109
|
+
const dir = dirs.get(name);
|
|
110
|
+
const manifest = manifestOf(dir, name);
|
|
111
|
+
const needs = [];
|
|
112
|
+
for (const peer of requiredPeers(manifest)) {
|
|
113
|
+
if (peer === CORE_PACKAGE)
|
|
114
|
+
continue;
|
|
115
|
+
const peerDir = locate(peer, dir);
|
|
116
|
+
if (!peerDir || !isStackPackage(manifestOf(peerDir, peer)))
|
|
117
|
+
continue;
|
|
118
|
+
if (stack.includes(peer))
|
|
119
|
+
needs.push(peer);
|
|
120
|
+
else
|
|
121
|
+
unlisted.push(`${name} needs ${peer}: \`pnpm add ${peer}@${coreVersion}\`.`);
|
|
122
|
+
}
|
|
123
|
+
let entry;
|
|
124
|
+
try {
|
|
125
|
+
entry = require.resolve(`${name}/stack`);
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
throw new CliError(`${name}: its ./stack entry does not exist: build the package (\`pnpm -r build\` in a workspace) or run \`pnpm install\`.`);
|
|
129
|
+
}
|
|
130
|
+
found.set(name, {
|
|
131
|
+
name,
|
|
132
|
+
version: manifest.version,
|
|
133
|
+
dir,
|
|
134
|
+
entry,
|
|
135
|
+
needs: needs.sort(),
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
if (unlisted.length > 0)
|
|
139
|
+
throw new CliError(`${unlisted.sort().join("\n")} Nothing was run.`);
|
|
140
|
+
const ordered = [];
|
|
141
|
+
const pending = new Set(found.keys());
|
|
142
|
+
for (;;) {
|
|
143
|
+
const ready = [...pending]
|
|
144
|
+
.filter((name) => found.get(name).needs.every((need) => !pending.has(need)))
|
|
145
|
+
.sort()[0];
|
|
146
|
+
if (ready === undefined)
|
|
147
|
+
break;
|
|
148
|
+
pending.delete(ready);
|
|
149
|
+
ordered.push(found.get(ready));
|
|
150
|
+
}
|
|
151
|
+
if (pending.size > 0) {
|
|
152
|
+
const stuck = [...pending].map((name) => found.get(name));
|
|
153
|
+
throw new CliError(`${joinNames(onCycle(stuck))} need each other: nothing was run.`);
|
|
154
|
+
}
|
|
155
|
+
return ordered;
|
|
156
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type StackPackage } from "../stack/contracts.ts";
|
|
2
|
+
import type { Output } from "./owned.ts";
|
|
3
|
+
/**
|
|
4
|
+
* The modules `stack sync` generates from the packages' `runtime` pieces, owned by the core and written in Biome's format: the env fragments, the health checks and the tracing, each in package order.
|
|
5
|
+
*/
|
|
6
|
+
export declare function generatedModules(packages: readonly StackPackage[]): Output[];
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { CORE_PACKAGE, GENERATED } from "../stack/contracts.js";
|
|
2
|
+
const HEADER = "// Owned by @wwchen/stack: `stack sync` writes this file from the packages in package.json.\n";
|
|
3
|
+
/** Biome's line width, which the modules are formatted to. */
|
|
4
|
+
const WIDTH = 100;
|
|
5
|
+
/** The names each module exports, sorted by module, so Biome's import order finds nothing to change. */
|
|
6
|
+
function importLines(imports, types = []) {
|
|
7
|
+
const group = (list, keyword) => {
|
|
8
|
+
const byModule = new Map();
|
|
9
|
+
for (const { from, name } of list)
|
|
10
|
+
byModule.set(from, (byModule.get(from) ?? new Set()).add(name));
|
|
11
|
+
return [...byModule].map(([from, names]) => ({
|
|
12
|
+
from,
|
|
13
|
+
line: `${keyword} { ${[...names].sort().join(", ")} } from "${from}";`,
|
|
14
|
+
}));
|
|
15
|
+
};
|
|
16
|
+
return [...group(imports, "import"), ...group(types, "import type")]
|
|
17
|
+
.sort((a, b) => importOrder(a.from, b.from))
|
|
18
|
+
.map(({ line }) => line);
|
|
19
|
+
}
|
|
20
|
+
/** Biome's import order for bare specifiers: segment by segment, ASCII, case-insensitively, so `@wwchen/stack/env` comes before `@wwchen/stack-db/env`. */
|
|
21
|
+
const importOrder = (a, b) => {
|
|
22
|
+
const [x, y] = [a.toLowerCase().split("/"), b.toLowerCase().split("/")];
|
|
23
|
+
for (let i = 0; i < Math.min(x.length, y.length); i++) {
|
|
24
|
+
const [p, q] = [x[i], y[i]];
|
|
25
|
+
if (p !== q)
|
|
26
|
+
return p < q ? -1 : 1;
|
|
27
|
+
}
|
|
28
|
+
return x.length - y.length;
|
|
29
|
+
};
|
|
30
|
+
/** `prefix` + `[a, b]` + `suffix`, on one line when it fits and otherwise one element a line. */
|
|
31
|
+
function list(prefix, items, suffix, indent = "") {
|
|
32
|
+
const oneLine = `${indent}${prefix}[${items.join(", ")}]${suffix}`;
|
|
33
|
+
if (oneLine.length <= WIDTH)
|
|
34
|
+
return oneLine;
|
|
35
|
+
return [
|
|
36
|
+
`${indent}${prefix}[`,
|
|
37
|
+
...items.map((item) => `${indent} ${item},`),
|
|
38
|
+
`${indent}]${suffix}`,
|
|
39
|
+
].join("\n");
|
|
40
|
+
}
|
|
41
|
+
const packageRuntimes = (packages) => packages.flatMap((pkg) => (pkg.runtime ? [pkg.runtime] : []));
|
|
42
|
+
function envModule(packages) {
|
|
43
|
+
const fragments = packageRuntimes(packages).flatMap((r) => (r.env ? [r.env] : []));
|
|
44
|
+
const lines = [
|
|
45
|
+
HEADER.trimEnd(),
|
|
46
|
+
"",
|
|
47
|
+
...importLines(fragments),
|
|
48
|
+
"",
|
|
49
|
+
list("export const fragments = ", fragments.map(({ name }) => name), " as const;"),
|
|
50
|
+
];
|
|
51
|
+
return `${lines.join("\n")}\n`;
|
|
52
|
+
}
|
|
53
|
+
function healthModule(packages) {
|
|
54
|
+
const checks = packageRuntimes(packages).flatMap((r) => (r.health ? [r.health] : []));
|
|
55
|
+
const types = [{ from: "@wwchen/stack", name: "HealthCheck" }];
|
|
56
|
+
const lines = [HEADER.trimEnd(), "", ...importLines(checks, types), ""];
|
|
57
|
+
if (checks.length === 0) {
|
|
58
|
+
lines.push("export const healthChecks = (_env: object): HealthCheck[] => [];");
|
|
59
|
+
return `${lines.join("\n")}\n`;
|
|
60
|
+
}
|
|
61
|
+
const parts = checks.map(({ name }) => `Parameters<typeof ${name}>[0]`);
|
|
62
|
+
const oneLine = `type HealthEnv = ${parts.join(" & ")};`;
|
|
63
|
+
lines.push(oneLine.length <= WIDTH
|
|
64
|
+
? oneLine
|
|
65
|
+
: `type HealthEnv = ${parts.map((part, i) => (i === 0 ? part : ` ${part}`)).join(" &\n")};`, "", list("export const healthChecks = (env: HealthEnv): HealthCheck[] => ", checks.map(({ name }) => `${name}(env)`), ";"));
|
|
66
|
+
return `${lines.join("\n")}\n`;
|
|
67
|
+
}
|
|
68
|
+
function nextModule(packages) {
|
|
69
|
+
const tracing = new Map();
|
|
70
|
+
for (const { tracing: byRoute } of packageRuntimes(packages)) {
|
|
71
|
+
for (const [route, globs] of Object.entries(byRoute ?? {})) {
|
|
72
|
+
const merged = tracing.get(route) ?? [];
|
|
73
|
+
for (const glob of globs)
|
|
74
|
+
if (!merged.includes(glob))
|
|
75
|
+
merged.push(glob);
|
|
76
|
+
tracing.set(route, merged);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const head = `${HEADER}\nexport const tracing: Record<string, string[]> =`;
|
|
80
|
+
if (tracing.size === 0)
|
|
81
|
+
return `${head} {};\n`;
|
|
82
|
+
const entries = [...tracing].map(([route, globs]) => list(`${JSON.stringify(route)}: `, globs.map((glob) => JSON.stringify(glob)), ",", " "));
|
|
83
|
+
return `${head} {\n${entries.join("\n")}\n};\n`;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* The modules `stack sync` generates from the packages' `runtime` pieces, owned by the core and written in Biome's format: the env fragments, the health checks and the tracing, each in package order.
|
|
87
|
+
*/
|
|
88
|
+
export function generatedModules(packages) {
|
|
89
|
+
return [
|
|
90
|
+
{ path: GENERATED.env, text: envModule(packages) },
|
|
91
|
+
{ path: GENERATED.health, text: healthModule(packages) },
|
|
92
|
+
{ path: GENERATED.next, text: nextModule(packages) },
|
|
93
|
+
].map((module) => ({ ...module, owner: CORE_PACKAGE }));
|
|
94
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type StackPackage } from "../stack/contracts.ts";
|
|
2
|
+
import { type Cli } from "./context.ts";
|
|
3
|
+
/**
|
|
4
|
+
* The built-in package, then each stack package the app's package.json lists (`discover` orders them), built by calling its `./stack` entry's default export with the settings `stack.config.ts` gives it, or undefined. Refuses with every problem found, then `Nothing was run.`
|
|
5
|
+
*/
|
|
6
|
+
export declare function loadPackages(cli: Cli): Promise<StackPackage[]>;
|
|
7
|
+
/** Runs `argv`'s command from whichever package gives it, and resolves to its exit code: 2 with the usage for no command or an unknown one. */
|
|
8
|
+
export declare function runCommand(cli: Cli, argv: string[]): Promise<number>;
|
package/dist/cli/host.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// The `stack` CLI host: it composes the built-in package with the stack packages the app's package.json lists, each built by its `./stack` entry's factory from the settings `stack.config.ts` gives it, refuses a command two packages claim, and runs the one command asked for.
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { pathToFileURL } from "node:url";
|
|
5
|
+
import { BUILTIN_PACKAGE, STACK_CONFIG_FILE, } from "../stack/contracts.js";
|
|
6
|
+
import { builtinPackage, SYNC_SECOND_NAME } from "./builtin.js";
|
|
7
|
+
import { CliError, importApp, STACK_PACKAGE } from "./context.js";
|
|
8
|
+
import { discover } from "./discover.js";
|
|
9
|
+
const messageOf = (error) => (error instanceof Error ? error.message : String(error));
|
|
10
|
+
/** What a value is, for a message: `null`, or its `typeof`, with its article. */
|
|
11
|
+
const describeValue = (value) => value === null ? "null" : `${/^[aeiou]/.test(typeof value) ? "an" : "a"} ${typeof value}`;
|
|
12
|
+
const REWRITE = `export \`defineStack({ settings })\`. \`pnpm dlx @wwchen/stack-upgrade\` rewrites it.`;
|
|
13
|
+
/** Node's link-time refusal of a named import from a package's `/stack` entry: a stack.config.ts from 0.10 imports each package's factory by name, and an entry's factory is its default export. */
|
|
14
|
+
const OLD_FACTORY_IMPORT = /The requested module '(@wwchen\/stack[\w-]*\/stack)' does not provide an export named '([^']+)'/;
|
|
15
|
+
/** The settings `stack.config.ts` gives, by package; empty when the app has no such file. */
|
|
16
|
+
async function readSettings(cli) {
|
|
17
|
+
const settings = new Map();
|
|
18
|
+
if (!existsSync(join(cli.cwd, STACK_CONFIG_FILE)))
|
|
19
|
+
return settings;
|
|
20
|
+
let config;
|
|
21
|
+
try {
|
|
22
|
+
config = (await importApp(cli, STACK_CONFIG_FILE)).default;
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
const message = messageOf(error);
|
|
26
|
+
const old = OLD_FACTORY_IMPORT.exec(message);
|
|
27
|
+
if (old) {
|
|
28
|
+
throw new CliError(`${STACK_CONFIG_FILE}: ${old[2]} is not an export of ${old[1]}: a stack.config.ts from 0.10 imports packages' factories by name and lists packages, which come from package.json: ${REWRITE}`);
|
|
29
|
+
}
|
|
30
|
+
throw new CliError(message.startsWith(`${STACK_CONFIG_FILE}:`) ? message : `${STACK_CONFIG_FILE}: ${message}`);
|
|
31
|
+
}
|
|
32
|
+
const given = config?.settings;
|
|
33
|
+
if (!Array.isArray(given)) {
|
|
34
|
+
const listsPackages = Array.isArray(config?.packages);
|
|
35
|
+
throw new CliError(listsPackages
|
|
36
|
+
? `${STACK_CONFIG_FILE}: its default export lists packages, which come from package.json: ${REWRITE}`
|
|
37
|
+
: `${STACK_CONFIG_FILE}: its default export is not a composition: export \`defineStack({ settings })\` as its default.`);
|
|
38
|
+
}
|
|
39
|
+
const problems = [];
|
|
40
|
+
given.forEach((entry, index) => {
|
|
41
|
+
if (typeof entry?.package === "string" && entry.package !== "") {
|
|
42
|
+
settings.set(entry.package, entry.settings);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
problems.push(`${STACK_CONFIG_FILE}: settings[${index}] is not a package's settings: make it with the package's helper, \`{ package, settings }\`.`);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
if (problems.length > 0)
|
|
49
|
+
throw new CliError([...problems, "Nothing was run."].join("\n"));
|
|
50
|
+
return settings;
|
|
51
|
+
}
|
|
52
|
+
/** Why `built` is not the package `name` named: undefined when it is. */
|
|
53
|
+
function notThePackage(name, built) {
|
|
54
|
+
if (typeof built !== "object" || built === null) {
|
|
55
|
+
return `its factory returned ${describeValue(built)}, not a package: return an object named ${name}.`;
|
|
56
|
+
}
|
|
57
|
+
const given = built.name;
|
|
58
|
+
if (given === name)
|
|
59
|
+
return undefined;
|
|
60
|
+
return typeof given === "string"
|
|
61
|
+
? `its factory returned a package named "${given}": name it ${name}, as package.json does.`
|
|
62
|
+
: `its factory returned an object with no name: name it ${name}, as package.json does.`;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The built-in package, then each stack package the app's package.json lists (`discover` orders them), built by calling its `./stack` entry's default export with the settings `stack.config.ts` gives it, or undefined. Refuses with every problem found, then `Nothing was run.`
|
|
66
|
+
*/
|
|
67
|
+
export async function loadPackages(cli) {
|
|
68
|
+
const installed = discover(cli.cwd, STACK_PACKAGE.version);
|
|
69
|
+
const settings = await readSettings(cli);
|
|
70
|
+
const problems = [...settings.keys()]
|
|
71
|
+
.filter((name) => !installed.some((p) => p.name === name))
|
|
72
|
+
.map((name) => `${STACK_CONFIG_FILE}: settings for ${name}, which package.json does not list.`);
|
|
73
|
+
const packages = [builtinPackage()];
|
|
74
|
+
for (const { name, entry } of installed) {
|
|
75
|
+
try {
|
|
76
|
+
const factory = (await cli.importModule(pathToFileURL(entry).href)).default;
|
|
77
|
+
if (typeof factory !== "function") {
|
|
78
|
+
problems.push(`${name}: its ./stack entry's default export is not a function: export the package's factory as the default.`);
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
const built = factory(settings.get(name));
|
|
82
|
+
const problem = notThePackage(name, built);
|
|
83
|
+
if (problem)
|
|
84
|
+
problems.push(`${name}: ${problem}`);
|
|
85
|
+
else
|
|
86
|
+
packages.push(built);
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
problems.push(`${name}: ${messageOf(error)}`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (problems.length > 0)
|
|
93
|
+
throw new CliError([...problems, "Nothing was run."].join("\n"));
|
|
94
|
+
return packages;
|
|
95
|
+
}
|
|
96
|
+
/** Each command, sorted, with the packages that give it. */
|
|
97
|
+
function commandOwners(packages) {
|
|
98
|
+
const owners = new Map();
|
|
99
|
+
for (const pkg of packages) {
|
|
100
|
+
for (const command of Object.keys(pkg.commands ?? {})) {
|
|
101
|
+
owners.set(command, [...(owners.get(command) ?? []), pkg]);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return new Map([...owners].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)));
|
|
105
|
+
}
|
|
106
|
+
/** Why `command`, which every package of `pkgs` gives, cannot run, and which dependency to remove. */
|
|
107
|
+
function clash(command, pkgs) {
|
|
108
|
+
const listed = pkgs.filter((p) => p.name !== BUILTIN_PACKAGE).map((p) => p.name);
|
|
109
|
+
if (listed.length < pkgs.length) {
|
|
110
|
+
return `${command} is a command of the stack itself, and ${listed.join(", ")} ${listed.length === 1 ? "gives" : "give"} it too: remove ${listed.length === 1 ? "it" : "them"} from package.json's dependencies; nothing was run.`;
|
|
111
|
+
}
|
|
112
|
+
return `more than one package gives the command ${command}: ${listed.join(", ")}. Remove all but one of them from package.json's dependencies; nothing was run.`;
|
|
113
|
+
}
|
|
114
|
+
/** Every command but `sync`'s second name, each with the package that gives it. */
|
|
115
|
+
function usage(owners) {
|
|
116
|
+
const lines = ["usage: stack <command>"];
|
|
117
|
+
for (const [command, [pkg]] of owners) {
|
|
118
|
+
if (command !== SYNC_SECOND_NAME)
|
|
119
|
+
lines.push(` ${command} (${pkg?.name})`);
|
|
120
|
+
}
|
|
121
|
+
return lines.join("\n");
|
|
122
|
+
}
|
|
123
|
+
/** Runs `argv`'s command from whichever package gives it, and resolves to its exit code: 2 with the usage for no command or an unknown one. */
|
|
124
|
+
export async function runCommand(cli, argv) {
|
|
125
|
+
const packages = await loadPackages(cli);
|
|
126
|
+
const owners = commandOwners(packages);
|
|
127
|
+
const clashes = [...owners].filter(([, pkgs]) => pkgs.length > 1);
|
|
128
|
+
if (clashes.length > 0)
|
|
129
|
+
throw new CliError(clashes.map(([c, pkgs]) => clash(c, pkgs)).join("\n"));
|
|
130
|
+
const [command, ...args] = argv;
|
|
131
|
+
const load = command === undefined ? undefined : owners.get(command)?.[0]?.commands?.[command];
|
|
132
|
+
if (!load) {
|
|
133
|
+
cli.error(usage(owners));
|
|
134
|
+
return 2;
|
|
135
|
+
}
|
|
136
|
+
const ctx = {
|
|
137
|
+
cwd: cli.cwd,
|
|
138
|
+
env: cli.env,
|
|
139
|
+
exec: cli.exec,
|
|
140
|
+
log: cli.log,
|
|
141
|
+
error: cli.error,
|
|
142
|
+
importApp: (path) => importApp(cli, path),
|
|
143
|
+
packages,
|
|
144
|
+
};
|
|
145
|
+
return (await load())(ctx, args);
|
|
146
|
+
}
|
package/dist/cli/index.js
CHANGED
|
@@ -2,85 +2,18 @@
|
|
|
2
2
|
import { spawn } from "node:child_process";
|
|
3
3
|
import { realpathSync } from "node:fs";
|
|
4
4
|
import { fileURLToPath } from "node:url";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import { dev } from "./dev.js";
|
|
8
|
-
import { doctor } from "./doctor.js";
|
|
9
|
-
import { migrateSuite } from "./migrate-suite.js";
|
|
10
|
-
import { devSetup, neonSeed, resetPreview, withNeon } from "./neon.js";
|
|
11
|
-
import { runScript } from "./run.js";
|
|
12
|
-
import { syncBlocksCommand } from "./sync-blocks.js";
|
|
13
|
-
import { userCreate, userRole } from "./user-create.js";
|
|
14
|
-
import { vercelBuild } from "./vercel-build.js";
|
|
15
|
-
const USAGE = `usage: stack <command>
|
|
16
|
-
with-neon <command> [args] run a command against your Neon dev branch
|
|
17
|
-
dev [--pglite] [next dev args] next dev on your dev branch; --pglite: on PGlite in .local-db/, no Neon
|
|
18
|
-
dev-setup create your dev branch from seed and migrate it
|
|
19
|
-
run <script> [args] run an app script with Node, with the app's database and .env
|
|
20
|
-
neon seed (re)create the seed branch and load the app's seed data
|
|
21
|
-
neon reset-preview [--set-vercel] (re)create the shared preview branch from seed;
|
|
22
|
-
--set-vercel writes its URLs into Vercel's Preview variables
|
|
23
|
-
vercel-build Vercel's build command: migrate, then next build
|
|
24
|
-
doctor audit the linked Vercel project's setup (read-only)
|
|
25
|
-
user:create <email> [--admin] add a user through the app's auth; --preview: on the shared
|
|
26
|
-
preview branch (your own email only, to sign in on a preview)
|
|
27
|
-
user:role <email> <admin|user> change a user's role through the app's auth; --preview as above
|
|
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:generate <name> [flags] drizzle-kit generate, named for what the migration does (snake_case)
|
|
31
|
-
db:upgrade generate drizzle/'s migration for the schema's changes, named stack_<version>
|
|
32
|
-
migrate-suite [--expect <version>] move the app onto the suite's packages (@wwchen/stack-auth, -notify);
|
|
33
|
-
--expect: refuse unless the installed stack is that version`;
|
|
5
|
+
import { resolveDeps } from "./context.js";
|
|
6
|
+
import { runCommand } from "./host.js";
|
|
34
7
|
/** The testable entry: every outside effect arrives through `deps`; the exit code comes back. */
|
|
35
8
|
export async function runCli(argv, deps) {
|
|
36
9
|
const cli = resolveDeps(deps);
|
|
37
|
-
const [command, ...args] = argv;
|
|
38
10
|
try {
|
|
39
|
-
|
|
40
|
-
case "with-neon":
|
|
41
|
-
return await withNeon(cli, args);
|
|
42
|
-
case "dev":
|
|
43
|
-
return await dev(cli, args);
|
|
44
|
-
case "dev-setup":
|
|
45
|
-
return await devSetup(cli);
|
|
46
|
-
case "run":
|
|
47
|
-
return await runScript(cli, args);
|
|
48
|
-
case "neon":
|
|
49
|
-
if (args[0] === "seed")
|
|
50
|
-
return await neonSeed(cli);
|
|
51
|
-
if (args[0] === "reset-preview")
|
|
52
|
-
return await resetPreview(cli, args.slice(1));
|
|
53
|
-
break;
|
|
54
|
-
case "vercel-build":
|
|
55
|
-
return await vercelBuild(cli);
|
|
56
|
-
case "doctor":
|
|
57
|
-
return await doctor(cli);
|
|
58
|
-
case "user:create":
|
|
59
|
-
return await userCreate(cli, args);
|
|
60
|
-
case "user:role":
|
|
61
|
-
return await userRole(cli, args);
|
|
62
|
-
case "sync-blocks":
|
|
63
|
-
return await syncBlocksCommand(cli, args);
|
|
64
|
-
case "db:check":
|
|
65
|
-
return await dbCheck(cli);
|
|
66
|
-
case "db:generate":
|
|
67
|
-
return await dbGenerate(cli, args);
|
|
68
|
-
case "db:upgrade":
|
|
69
|
-
return await dbUpgrade(cli);
|
|
70
|
-
case "migrate-suite":
|
|
71
|
-
return await migrateSuite(cli, args);
|
|
72
|
-
}
|
|
11
|
+
return await runCommand(cli, argv);
|
|
73
12
|
}
|
|
74
13
|
catch (error) {
|
|
75
|
-
cli.error(error instanceof
|
|
76
|
-
? error.message
|
|
77
|
-
: error instanceof Error
|
|
78
|
-
? error.message
|
|
79
|
-
: String(error));
|
|
14
|
+
cli.error(error instanceof Error ? error.message : String(error));
|
|
80
15
|
return 1;
|
|
81
16
|
}
|
|
82
|
-
cli.error(USAGE);
|
|
83
|
-
return 2;
|
|
84
17
|
}
|
|
85
18
|
/**
|
|
86
19
|
* Runs a program with an argument array (never a shell), forwarding signals to an inherited child. `input` is written to the child's stdin whole, then closed.
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type StackPackage } from "../stack/contracts.ts";
|
|
2
|
+
/** A file `stack sync` writes whole: its path relative to the app's root, the package that owns it (an npm name) and its text. */
|
|
3
|
+
export type Output = {
|
|
4
|
+
path: string;
|
|
5
|
+
owner: string;
|
|
6
|
+
text: string;
|
|
7
|
+
};
|
|
8
|
+
/** The name a package owns files under: the built-in package is the core. */
|
|
9
|
+
export declare const ownerOf: (pkg: StackPackage) => string;
|
|
10
|
+
/** Each package's `files`, read from their sources and made by their `render` where they have one, in package order. */
|
|
11
|
+
export declare function ownedOutputs(packages: readonly StackPackage[]): Output[];
|
|
12
|
+
/** What a sync of the owned files does, planned before anything is written. */
|
|
13
|
+
export type FilePlan = {
|
|
14
|
+
/** The files to write, whole. */
|
|
15
|
+
writes: Output[];
|
|
16
|
+
/** The recorded files to delete, with their recorded owners. */
|
|
17
|
+
deletes: {
|
|
18
|
+
path: string;
|
|
19
|
+
owner: string;
|
|
20
|
+
}[];
|
|
21
|
+
/** The record's text after the sync; null when no file is owned and the record goes. */
|
|
22
|
+
record: string | null;
|
|
23
|
+
/** Whether the record on disk differs from `record`. */
|
|
24
|
+
recordChanged: boolean;
|
|
25
|
+
};
|
|
26
|
+
/** The paths the plan changes, each named once, in the order they are applied. */
|
|
27
|
+
export declare const changedPaths: (plan: FilePlan) => string[];
|
|
28
|
+
/**
|
|
29
|
+
* Plans the owned files against the app's disk and its record: writes a path only when it is absent or recorded, deletes every recorded path no output claims, and refuses, with nothing planned, an unsafe path, a path two packages own and an existing file the record does not name. Every refusal is reported together.
|
|
30
|
+
*/
|
|
31
|
+
export declare function planFiles(cwd: string, outputs: readonly Output[]): FilePlan;
|
|
32
|
+
/** Applies a plan: writes the files, deletes the recorded ones, then writes or removes the record. */
|
|
33
|
+
export declare function applyFiles(cwd: string, plan: FilePlan): void;
|