@wwchen/stack 0.10.1 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -25
- package/blocks/AGENTS.md +8 -20
- package/blocks/CLAUDE.md +9 -27
- package/blocks/env.example +1 -7
- package/blocks/gitignore +0 -6
- package/dist/cli/builtin.d.ts +4 -3
- package/dist/cli/builtin.js +31 -36
- package/dist/cli/check.d.ts +5 -0
- package/dist/cli/check.js +39 -0
- package/dist/cli/context.d.ts +1 -18
- package/dist/cli/context.js +0 -22
- 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 +1 -1
- package/dist/cli/host.js +90 -72
- package/dist/cli/owned.d.ts +33 -0
- package/dist/cli/owned.js +151 -0
- package/dist/cli/{sync-blocks.d.ts → sync.d.ts} +6 -2
- package/dist/cli/{sync-blocks.js → sync.js} +46 -18
- package/dist/configs/vitest.d.ts +3 -1
- package/dist/configs/vitest.js +8 -6
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/next/config.d.ts +2 -0
- package/dist/next/config.js +10 -6
- package/dist/next/health.d.ts +1 -1
- package/dist/next/health.js +1 -1
- package/dist/stack/contracts.d.ts +65 -15
- package/dist/stack/contracts.js +30 -6
- 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 +6 -37
- 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/neon.d.ts +0 -79
- package/dist/cli/neon.js +0 -394
- package/dist/cli/run.d.ts +0 -2
- package/dist/cli/run.js +0 -29
- package/dist/db/branches.d.ts +0 -11
- package/dist/db/branches.js +0 -19
- package/dist/db/doctor.d.ts +0 -9
- package/dist/db/doctor.js +0 -120
- package/dist/db/env.d.ts +0 -14
- package/dist/db/env.js +0 -15
- package/dist/db/explaining-pool.d.ts +0 -11
- package/dist/db/explaining-pool.js +0 -55
- package/dist/db/health.d.ts +0 -10
- package/dist/db/health.js +0 -26
- package/dist/db/index.d.ts +0 -21
- package/dist/db/index.js +0 -32
- 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 -66
- 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/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
|
+
}
|
package/dist/cli/host.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type StackPackage } from "../stack/contracts.ts";
|
|
2
2
|
import { type Cli } from "./context.ts";
|
|
3
3
|
/**
|
|
4
|
-
* The built-in package, then the
|
|
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
5
|
*/
|
|
6
6
|
export declare function loadPackages(cli: Cli): Promise<StackPackage[]>;
|
|
7
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. */
|
package/dist/cli/host.js
CHANGED
|
@@ -1,65 +1,97 @@
|
|
|
1
|
-
// The `stack` CLI host: it composes the built-in package with the packages the app's `stack.config.ts`
|
|
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
2
|
import { existsSync } from "node:fs";
|
|
3
3
|
import { join } from "node:path";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import { builtinPackage } from "./builtin.js";
|
|
7
|
-
import { CliError, importApp } from "./context.js";
|
|
8
|
-
|
|
9
|
-
const STACK_ENTRY = "./stack";
|
|
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";
|
|
10
9
|
const messageOf = (error) => (error instanceof Error ? error.message : String(error));
|
|
11
|
-
/** What
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
return
|
|
21
|
-
}
|
|
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;
|
|
22
20
|
let config;
|
|
23
21
|
try {
|
|
24
22
|
config = (await importApp(cli, STACK_CONFIG_FILE)).default;
|
|
25
23
|
}
|
|
26
24
|
catch (error) {
|
|
27
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
|
+
}
|
|
28
30
|
throw new CliError(message.startsWith(`${STACK_CONFIG_FILE}:`) ? message : `${STACK_CONFIG_FILE}: ${message}`);
|
|
29
31
|
}
|
|
30
|
-
const
|
|
31
|
-
if (!Array.isArray(
|
|
32
|
-
|
|
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.`);
|
|
33
38
|
}
|
|
34
|
-
const problems =
|
|
35
|
-
|
|
36
|
-
|
|
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
|
+
}
|
|
37
47
|
});
|
|
38
48
|
if (problems.length > 0)
|
|
39
49
|
throw new CliError([...problems, "Nothing was run."].join("\n"));
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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}.`;
|
|
43
56
|
}
|
|
44
|
-
|
|
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.`;
|
|
45
63
|
}
|
|
46
64
|
/**
|
|
47
|
-
*
|
|
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.`
|
|
48
66
|
*/
|
|
49
|
-
function
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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
|
+
}
|
|
61
91
|
}
|
|
62
|
-
|
|
92
|
+
if (problems.length > 0)
|
|
93
|
+
throw new CliError([...problems, "Nothing was run."].join("\n"));
|
|
94
|
+
return packages;
|
|
63
95
|
}
|
|
64
96
|
/** Each command, sorted, with the packages that give it. */
|
|
65
97
|
function commandOwners(packages) {
|
|
@@ -71,45 +103,30 @@ function commandOwners(packages) {
|
|
|
71
103
|
}
|
|
72
104
|
return new Map([...owners].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)));
|
|
73
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. */
|
|
74
115
|
function usage(owners) {
|
|
75
116
|
const lines = ["usage: stack <command>"];
|
|
76
|
-
for (const [command, [pkg]] of owners)
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
/** A JSON file's object, or undefined when it is missing or does not parse: a broken install is pnpm's and the typecheck's to report, not the host's. */
|
|
81
|
-
function manifest(path) {
|
|
82
|
-
try {
|
|
83
|
-
return readJsonFile(path);
|
|
84
|
-
}
|
|
85
|
-
catch {
|
|
86
|
-
return undefined;
|
|
117
|
+
for (const [command, [pkg]] of owners) {
|
|
118
|
+
if (command !== SYNC_SECOND_NAME)
|
|
119
|
+
lines.push(` ${command} (${pkg?.name})`);
|
|
87
120
|
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* The app's dependencies (`dependencies` and `devDependencies` in its package.json) that are stack packages, by the `./stack` entry in their installed package.json's `exports`, and that no composed package is named for: each would otherwise do nothing, with no word. Only the listed dependencies are read, none of theirs.
|
|
91
|
-
*/
|
|
92
|
-
function uncomposed(cwd, packages) {
|
|
93
|
-
const app = manifest(join(cwd, "package.json"));
|
|
94
|
-
const names = new Set([app?.dependencies, app?.devDependencies].flatMap((deps) => typeof deps === "object" && deps !== null ? Object.keys(deps) : []));
|
|
95
|
-
const composed = new Set(packages.map((p) => p.name));
|
|
96
|
-
return [...names].filter((name) => {
|
|
97
|
-
if (composed.has(name))
|
|
98
|
-
return false;
|
|
99
|
-
const exports = manifest(join(cwd, "node_modules", name, "package.json"))?.exports;
|
|
100
|
-
return typeof exports === "object" && exports !== null && STACK_ENTRY in exports;
|
|
101
|
-
});
|
|
121
|
+
return lines.join("\n");
|
|
102
122
|
}
|
|
103
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. */
|
|
104
124
|
export async function runCommand(cli, argv) {
|
|
105
125
|
const packages = await loadPackages(cli);
|
|
106
126
|
const owners = commandOwners(packages);
|
|
107
127
|
const clashes = [...owners].filter(([, pkgs]) => pkgs.length > 1);
|
|
108
|
-
if (clashes.length > 0)
|
|
109
|
-
throw new CliError(clashes
|
|
110
|
-
.map(([command, pkgs]) => `${STACK_CONFIG_FILE}: more than one package gives the command ${command}: ${pkgs.map((p) => p.name).join(", ")}. Compose only one of them; nothing was run.`)
|
|
111
|
-
.join("\n"));
|
|
112
|
-
}
|
|
128
|
+
if (clashes.length > 0)
|
|
129
|
+
throw new CliError(clashes.map(([c, pkgs]) => clash(c, pkgs)).join("\n"));
|
|
113
130
|
const [command, ...args] = argv;
|
|
114
131
|
const load = command === undefined ? undefined : owners.get(command)?.[0]?.commands?.[command];
|
|
115
132
|
if (!load) {
|
|
@@ -122,6 +139,7 @@ export async function runCommand(cli, argv) {
|
|
|
122
139
|
exec: cli.exec,
|
|
123
140
|
log: cli.log,
|
|
124
141
|
error: cli.error,
|
|
142
|
+
importApp: (path) => importApp(cli, path),
|
|
125
143
|
packages,
|
|
126
144
|
};
|
|
127
145
|
return (await load())(ctx, args);
|
|
@@ -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;
|