@sdxc/kit 2026.10.20 → 2026.10.23
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 +178 -93
- package/changelog.jsonl +97 -0
- package/config.schema.json +160 -0
- package/dist/architecture/complexity.d.ts +73 -0
- package/dist/architecture/complexity.js +185 -0
- package/dist/architecture/crap.d.ts +62 -0
- package/dist/architecture/crap.js +121 -0
- package/dist/architecture/graph.d.ts +55 -0
- package/dist/architecture/graph.js +194 -0
- package/dist/architecture/layers.d.ts +29 -0
- package/dist/architecture/layers.js +81 -0
- package/dist/architecture/mermaid.d.ts +20 -0
- package/dist/architecture/mermaid.js +51 -0
- package/dist/cli.js +9 -0
- package/dist/commands/create.js +4 -3
- package/dist/commands/deploy.js +10 -9
- package/dist/commands/destroy.d.ts +1 -1
- package/dist/commands/diagram.d.ts +17 -0
- package/dist/commands/diagram.js +45 -0
- package/dist/commands/fix.d.ts +69 -0
- package/dist/commands/fix.js +102 -0
- package/dist/commands/make.d.ts +77 -2
- package/dist/commands/make.js +53 -1
- package/dist/commands/run.d.ts +15 -11
- package/dist/commands/run.js +21 -16
- package/dist/commands/scaffold.d.ts +2 -1
- package/dist/commands/scaffold.js +1 -0
- package/dist/commands/setup.d.ts +26 -0
- package/dist/commands/setup.js +33 -0
- package/dist/config.d.ts +54 -0
- package/dist/config.js +92 -1
- package/dist/contract.d.ts +56 -3
- package/dist/contract.js +140 -7
- package/dist/dependencies.d.ts +2 -1
- package/dist/dependencies.js +11 -2
- package/dist/deploy/plan.d.ts +5 -0
- package/dist/deploy/plan.js +10 -0
- package/dist/destroy.d.ts +4 -4
- package/dist/destroy.js +24 -11
- package/dist/diagram.d.ts +32 -0
- package/dist/diagram.js +47 -0
- package/dist/doctor/checks.d.ts +2 -1
- package/dist/doctor/checks.js +26 -1
- package/dist/doctor/complexity.d.ts +13 -0
- package/dist/doctor/complexity.js +52 -0
- package/dist/doctor/crap.d.ts +14 -0
- package/dist/doctor/crap.js +67 -0
- package/dist/doctor/dependencies.d.ts +13 -0
- package/dist/doctor/dependencies.js +42 -0
- package/dist/doctor/duplication.d.ts +19 -0
- package/dist/doctor/duplication.js +106 -0
- package/dist/doctor/fan-out.d.ts +12 -0
- package/dist/doctor/fan-out.js +42 -0
- package/dist/doctor/feature-scatter.d.ts +15 -0
- package/dist/doctor/feature-scatter.js +84 -0
- package/dist/doctor/jsdoc.d.ts +44 -0
- package/dist/doctor/jsdoc.js +201 -0
- package/dist/doctor/module-size.d.ts +13 -0
- package/dist/doctor/module-size.js +50 -0
- package/dist/doctor/mutation.d.ts +20 -0
- package/dist/doctor/mutation.js +97 -0
- package/dist/doctor/orphans.d.ts +13 -0
- package/dist/doctor/orphans.js +50 -0
- package/dist/doctor/project.js +8 -5
- package/dist/doctor/result-throws.d.ts +17 -0
- package/dist/doctor/result-throws.js +72 -0
- package/dist/doctor/test-ratio.d.ts +12 -0
- package/dist/doctor/test-ratio.js +31 -0
- package/dist/fix/commands.d.ts +37 -0
- package/dist/fix/commands.js +63 -0
- package/dist/fix/emails.d.ts +21 -0
- package/dist/fix/emails.js +159 -0
- package/dist/fix/env.d.ts +29 -0
- package/dist/fix/env.js +58 -0
- package/dist/fix/index.d.ts +11 -0
- package/dist/fix/index.js +11 -0
- package/dist/fix/models.d.ts +20 -0
- package/dist/fix/models.js +95 -0
- package/dist/lock.js +12 -2
- package/dist/make/binding.d.ts +42 -0
- package/dist/make/binding.js +237 -0
- package/dist/make/generate.d.ts +11 -2
- package/dist/make/generate.js +47 -16
- package/dist/make/imports.js +7 -5
- package/dist/make/index.d.ts +4 -2
- package/dist/make/index.js +4 -1
- package/dist/make/kinds.d.ts +5 -0
- package/dist/make/kinds.js +14 -2
- package/dist/make/schema-diff.js +8 -1
- package/dist/make/stubs.d.ts +7 -2
- package/dist/make/stubs.js +8 -3
- package/dist/make/test.js +1 -0
- package/dist/models.js +7 -7
- package/dist/routes/modules.js +8 -5
- package/dist/routes/parse.js +8 -7
- package/dist/routes/routers.js +9 -5
- package/dist/scaffold/index.d.ts +2 -0
- package/dist/scaffold/index.js +13 -4
- package/dist/scaffold/render.d.ts +11 -1
- package/dist/scaffold/render.js +86 -1
- package/dist/setup/auth.d.ts +12 -0
- package/dist/setup/auth.js +209 -0
- package/dist/setup/billing.d.ts +11 -0
- package/dist/setup/billing.js +116 -0
- package/dist/setup/catalog.d.ts +25 -0
- package/dist/setup/catalog.js +34 -0
- package/dist/setup/credit.d.ts +13 -0
- package/dist/setup/credit.js +21 -0
- package/dist/setup/flags.d.ts +11 -0
- package/dist/setup/flags.js +62 -0
- package/dist/setup/index.d.ts +25 -0
- package/dist/setup/index.js +53 -0
- package/dist/setup/plan.d.ts +38 -0
- package/dist/setup/plan.js +62 -0
- package/dist/testing.js +3 -3
- package/dist/ui/layout.js +1 -1
- package/dist/ui/registry.d.ts +2 -2
- package/dist/upgrade/project.d.ts +2 -2
- package/dist/upgrade/prompt.js +7 -5
- package/package.json +10 -7
- package/registry/ui/attachment.json +2 -2
- package/registry/ui/sidebar.json +3 -3
- package/registry/ui/typeset.json +3 -3
- package/stubs/command.spec.stub +15 -0
- package/stubs/controller.api.spec.stub +15 -0
- package/stubs/controller.spec.stub +15 -0
- package/stubs/mcp.spec.stub +16 -0
- package/stubs/scaffold.migration.stub +2 -1
- package/stubs/scaffold.spec.stub +7 -0
- package/templates/app/AGENTS.md +21 -4
- package/templates/app/README.md +6 -3
- package/templates/app/app/commands/typegen.test.ts +20 -1
- package/templates/app/app/commands/typegen.ts +16 -2
- package/templates/app/app/initializers/cli.ts +7 -0
- package/templates/app/app/initializers/mail-transport.test.ts +8 -2
- package/templates/app/app/test/cloudflare-workers.ts +2 -0
- package/templates/app/package.json +1 -0
- package/templates/app/spec/config.jsonc +16 -0
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `sdxc make secret`, `make binding` and `make do`: edit `cloudflare.config.ts`, append
|
|
3
|
+
* `.env.example` for secrets, re-export a Durable Object from `bootstrap/worker.ts`, and print
|
|
4
|
+
* `sdxc run typegen` so agents stop hand-editing three places.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
10
|
+
import { dirname, join } from "node:path";
|
|
11
|
+
import { failure, isFailure, success, wrap } from "@sdxc/result";
|
|
12
|
+
import { addHeaderTags, headerTags, resolveAuthor } from "../author.js";
|
|
13
|
+
import { unifiedDiff } from "../diff.js";
|
|
14
|
+
import { CONFIG_FILE, ENV_EXAMPLE_FILE, declaredBindings, exampleKeys } from "../doctor/env.js";
|
|
15
|
+
import { parseName } from "./names.js";
|
|
16
|
+
/** Binding kinds `make binding` accepts; `secret` also appends `.env.example`. */
|
|
17
|
+
export const BINDING_KINDS = [
|
|
18
|
+
"secret",
|
|
19
|
+
"kv",
|
|
20
|
+
"d1",
|
|
21
|
+
"r2",
|
|
22
|
+
"queue",
|
|
23
|
+
"rateLimit",
|
|
24
|
+
"sendEmail",
|
|
25
|
+
"durableObject",
|
|
26
|
+
];
|
|
27
|
+
/** Plans and optionally writes a `bindings.<kind>()` entry for `NAME`. */
|
|
28
|
+
export async function makeBinding(ctx, root, input) {
|
|
29
|
+
let key = bindingKey(input.name);
|
|
30
|
+
if (isFailure(key))
|
|
31
|
+
return key;
|
|
32
|
+
let config = await wrap(() => readFile(join(root, CONFIG_FILE), "utf8"));
|
|
33
|
+
if (isFailure(config))
|
|
34
|
+
return failure(new Error(`No ${CONFIG_FILE} in the project`));
|
|
35
|
+
let existing = declaredBindings(config.data);
|
|
36
|
+
if (existing.has(key.data) && !input.force) {
|
|
37
|
+
return failure(new Error(`${key.data} is already declared in ${CONFIG_FILE}; pass --force to replace it`));
|
|
38
|
+
}
|
|
39
|
+
let bindingLine = bindingCall(input.kind, key.data);
|
|
40
|
+
let afterConfig = insertEnvBinding(config.data, key.data, bindingLine);
|
|
41
|
+
if (isFailure(afterConfig))
|
|
42
|
+
return afterConfig;
|
|
43
|
+
let exampleEdit = null;
|
|
44
|
+
if (input.kind === "secret") {
|
|
45
|
+
let example = await wrap(() => readFile(join(root, ENV_EXAMPLE_FILE), "utf8"));
|
|
46
|
+
let before = isFailure(example) ? "" : example.data;
|
|
47
|
+
if (!exampleKeys(before).has(key.data)) {
|
|
48
|
+
let after;
|
|
49
|
+
if (before === "")
|
|
50
|
+
after = `${key.data}=\n`;
|
|
51
|
+
else if (before.endsWith("\n"))
|
|
52
|
+
after = `${before}${key.data}=\n`;
|
|
53
|
+
else
|
|
54
|
+
after = `${before}\n${key.data}=\n`;
|
|
55
|
+
exampleEdit = { before, after };
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (input.dryRun) {
|
|
59
|
+
ctx.stdout(unifiedDiff(CONFIG_FILE, config.data, afterConfig.data));
|
|
60
|
+
if (exampleEdit !== null) {
|
|
61
|
+
ctx.stdout(unifiedDiff(ENV_EXAMPLE_FILE, exampleEdit.before === "" ? null : exampleEdit.before, exampleEdit.after));
|
|
62
|
+
}
|
|
63
|
+
return success(undefined);
|
|
64
|
+
}
|
|
65
|
+
await writeFile(join(root, CONFIG_FILE), afterConfig.data);
|
|
66
|
+
ctx.stdout(`edited ${CONFIG_FILE}\n`);
|
|
67
|
+
if (exampleEdit !== null) {
|
|
68
|
+
await writeFile(join(root, ENV_EXAMPLE_FILE), exampleEdit.after);
|
|
69
|
+
ctx.stdout(`edited ${ENV_EXAMPLE_FILE}\n`);
|
|
70
|
+
}
|
|
71
|
+
ctx.stdout("Next: sdxc run typegen\n");
|
|
72
|
+
return success(undefined);
|
|
73
|
+
}
|
|
74
|
+
/** `make secret NAME` — shorthand for `make binding secret NAME`. */
|
|
75
|
+
export async function makeSecret(ctx, root, input) {
|
|
76
|
+
return makeBinding(ctx, root, { ...input, kind: "secret" });
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Writes a Durable Object class stub, re-exports it from `bootstrap/worker.ts`, and declares
|
|
80
|
+
* the binding plus `exports.durableObject` in `cloudflare.config.ts`.
|
|
81
|
+
*/
|
|
82
|
+
export async function makeDo(ctx, root, packageRoot, input) {
|
|
83
|
+
let parsed = parseName(input.name);
|
|
84
|
+
if (isFailure(parsed))
|
|
85
|
+
return parsed;
|
|
86
|
+
let className = parsed.data.pascal.endsWith("DO")
|
|
87
|
+
? parsed.data.pascal
|
|
88
|
+
: `${parsed.data.pascal}DO`;
|
|
89
|
+
let binding = input.binding !== undefined
|
|
90
|
+
? bindingKey(input.binding)
|
|
91
|
+
: success(snakeUpper(parsed.data.base.replace(/-?do$/, "") || parsed.data.base));
|
|
92
|
+
if (isFailure(binding))
|
|
93
|
+
return binding;
|
|
94
|
+
let folder = input.role === "model" ? "app/models" : "app/services";
|
|
95
|
+
let file = `${folder}/${parsed.data.segments.join("/")}.ts`;
|
|
96
|
+
let stub = addHeaderTags(durableObjectStub(className), headerTags(await resolveAuthor(join(root, dirname(file)))));
|
|
97
|
+
let config = await wrap(() => readFile(join(root, CONFIG_FILE), "utf8"));
|
|
98
|
+
if (isFailure(config))
|
|
99
|
+
return failure(new Error(`No ${CONFIG_FILE} in the project`));
|
|
100
|
+
let worker = await wrap(() => readFile(join(root, "bootstrap/worker.ts"), "utf8"));
|
|
101
|
+
if (isFailure(worker))
|
|
102
|
+
return failure(new Error("No bootstrap/worker.ts in the project"));
|
|
103
|
+
let existing = declaredBindings(config.data);
|
|
104
|
+
if (existing.has(binding.data) && !input.force) {
|
|
105
|
+
return failure(new Error(`${binding.data} is already declared in ${CONFIG_FILE}; pass --force`));
|
|
106
|
+
}
|
|
107
|
+
let afterConfig = insertDurableObject(config.data, binding.data, className);
|
|
108
|
+
if (isFailure(afterConfig))
|
|
109
|
+
return afterConfig;
|
|
110
|
+
let exportLine = `export { ${className} } from "~/${file.replace(/\.ts$/, "")}";\n`;
|
|
111
|
+
let afterWorker = worker.data.includes(`export { ${className} }`)
|
|
112
|
+
? worker.data
|
|
113
|
+
: `${worker.data.trimEnd()}\n\n${exportLine}`;
|
|
114
|
+
if (input.dryRun) {
|
|
115
|
+
ctx.stdout(unifiedDiff(file, null, stub));
|
|
116
|
+
ctx.stdout(unifiedDiff(CONFIG_FILE, config.data, afterConfig.data));
|
|
117
|
+
ctx.stdout(unifiedDiff("bootstrap/worker.ts", worker.data, afterWorker));
|
|
118
|
+
return success(undefined);
|
|
119
|
+
}
|
|
120
|
+
await mkdir(dirname(join(root, file)), { recursive: true });
|
|
121
|
+
let written = await wrap(() => writeFile(join(root, file), stub, { flag: input.force ? "w" : "wx" }));
|
|
122
|
+
if (isFailure(written)) {
|
|
123
|
+
if (written.error.code === "EEXIST") {
|
|
124
|
+
return failure(new Error(`${file} already exists; pass --force to replace it`));
|
|
125
|
+
}
|
|
126
|
+
return written;
|
|
127
|
+
}
|
|
128
|
+
ctx.stdout(`wrote ${file}\n`);
|
|
129
|
+
await writeFile(join(root, CONFIG_FILE), afterConfig.data);
|
|
130
|
+
ctx.stdout(`edited ${CONFIG_FILE}\n`);
|
|
131
|
+
await writeFile(join(root, "bootstrap/worker.ts"), afterWorker);
|
|
132
|
+
ctx.stdout("edited bootstrap/worker.ts\n");
|
|
133
|
+
ctx.stdout("Next: sdxc run typegen\n");
|
|
134
|
+
void packageRoot;
|
|
135
|
+
return success(undefined);
|
|
136
|
+
}
|
|
137
|
+
/** `NAME` as an env binding key: already `SCREAMING_SNAKE` or derived from kebab/camel. */
|
|
138
|
+
function bindingKey(name) {
|
|
139
|
+
if (/^[A-Z][A-Z0-9_]*$/.test(name))
|
|
140
|
+
return success(name);
|
|
141
|
+
let parsed = parseName(name.replaceAll("_", "-"));
|
|
142
|
+
if (isFailure(parsed)) {
|
|
143
|
+
return failure(new Error(`"${name}" is not a binding name`));
|
|
144
|
+
}
|
|
145
|
+
return success(snakeUpper(parsed.data.base));
|
|
146
|
+
}
|
|
147
|
+
function snakeUpper(kebab) {
|
|
148
|
+
return kebab.replaceAll("-", "_").toUpperCase();
|
|
149
|
+
}
|
|
150
|
+
function bindingCall(kind, key) {
|
|
151
|
+
if (kind === "secret")
|
|
152
|
+
return `${key}: bindings.secret()`;
|
|
153
|
+
if (kind === "sendEmail")
|
|
154
|
+
return `${key}: bindings.sendEmail()`;
|
|
155
|
+
if (kind === "rateLimit") {
|
|
156
|
+
return `${key}: bindings.rateLimit({ namespace: "1001", simple: { limit: 100, period: 60 } })`;
|
|
157
|
+
}
|
|
158
|
+
if (kind === "kv") {
|
|
159
|
+
return `${key}: bindings.kv({ id: "00000000000000000000000000000000" })`;
|
|
160
|
+
}
|
|
161
|
+
if (kind === "d1") {
|
|
162
|
+
return `${key}: bindings.d1({ name: manifest.name, id: "00000000-0000-4000-8000-000000000000" })`;
|
|
163
|
+
}
|
|
164
|
+
if (kind === "r2")
|
|
165
|
+
return `${key}: bindings.r2({ name: "${key.toLowerCase()}" })`;
|
|
166
|
+
if (kind === "queue")
|
|
167
|
+
return `${key}: bindings.queue({ name: manifest.name })`;
|
|
168
|
+
return `${key}: bindings.durableObject({ worker: /* self */, exportName: "TODO" })`;
|
|
169
|
+
}
|
|
170
|
+
/** Inserts a binding property into the Worker's `env: { … }` object. */
|
|
171
|
+
function insertEnvBinding(source, key, line) {
|
|
172
|
+
let env = /env\s*:\s*\{/.exec(source);
|
|
173
|
+
if (env === null || env.index === undefined) {
|
|
174
|
+
return failure(new Error(`${CONFIG_FILE} has no env: { … } block to edit`));
|
|
175
|
+
}
|
|
176
|
+
let start = env.index + env[0].length;
|
|
177
|
+
let stripped = source.replace(new RegExp(`(?:^|\\n)(\\s*)${key}\\s*:\\s*bindings\\.\\w+\\s*\\([^)]*\\)\\s*,?`, "m"), "\n");
|
|
178
|
+
let envAgain = /env\s*:\s*\{/.exec(stripped);
|
|
179
|
+
if (envAgain === null || envAgain.index === undefined) {
|
|
180
|
+
return failure(new Error(`${CONFIG_FILE} env block could not be re-found after cleanup`));
|
|
181
|
+
}
|
|
182
|
+
start = envAgain.index + envAgain[0].length;
|
|
183
|
+
let indent = "\t\t\t";
|
|
184
|
+
return success(`${stripped.slice(0, start)}\n${indent}${line},${stripped.slice(start)}`);
|
|
185
|
+
}
|
|
186
|
+
/** Declares the DO export and env binding in `cloudflare.config.ts`. */
|
|
187
|
+
function insertDurableObject(source, binding, className) {
|
|
188
|
+
let withExport = source;
|
|
189
|
+
if (!source.includes(`exports`) || !/exports\s*:\s*\{/.test(source)) {
|
|
190
|
+
let worker = /worker\s*:\s*\{/.exec(source);
|
|
191
|
+
if (worker === null || worker.index === undefined) {
|
|
192
|
+
return failure(new Error(`${CONFIG_FILE} has no worker: { … } block`));
|
|
193
|
+
}
|
|
194
|
+
let insertAt = worker.index + worker[0].length;
|
|
195
|
+
withExport = `${source.slice(0, insertAt)}\n\t\texports: {\n\t\t\t${className}: exports.durableObject({ storage: "sqlite" }),\n\t\t},${source.slice(insertAt)}`;
|
|
196
|
+
if (!/import\s*\{[^}]*\bexports\b/.test(withExport) &&
|
|
197
|
+
withExport.includes('from "cf/config"')) {
|
|
198
|
+
withExport = withExport.replace(/import\s*\{([^}]+)\}\s*from\s*"cf\/config"/, (_match, names) => {
|
|
199
|
+
let list = names.split(",").map((part) => part.trim());
|
|
200
|
+
if (!list.includes("exports"))
|
|
201
|
+
list.push("exports");
|
|
202
|
+
return `import { ${list.join(", ")} } from "cf/config"`;
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
else if (!withExport.includes(`${className}:`)) {
|
|
207
|
+
let exportsBlock = /exports\s*:\s*\{/.exec(withExport);
|
|
208
|
+
if (exportsBlock === null || exportsBlock.index === undefined) {
|
|
209
|
+
return failure(new Error(`${CONFIG_FILE} exports block is malformed`));
|
|
210
|
+
}
|
|
211
|
+
let at = exportsBlock.index + exportsBlock[0].length;
|
|
212
|
+
withExport = `${withExport.slice(0, at)}\n\t\t\t${className}: exports.durableObject({ storage: "sqlite" }),${withExport.slice(at)}`;
|
|
213
|
+
}
|
|
214
|
+
let line = `${binding}: bindings.durableObject({ worker: manifest.name, exportName: "${className}" })`;
|
|
215
|
+
return insertEnvBinding(withExport, binding, line);
|
|
216
|
+
}
|
|
217
|
+
/** Minimal Durable Object class stub; `@author` / `@copyright` are added by {@link makeDo}. */
|
|
218
|
+
function durableObjectStub(className) {
|
|
219
|
+
return `/**
|
|
220
|
+
* The ${className} Durable Object: fill in its storage and request handlers.
|
|
221
|
+
*/
|
|
222
|
+
|
|
223
|
+
import { DurableObject } from "cloudflare:workers";
|
|
224
|
+
|
|
225
|
+
/** Durable Object class re-exported from bootstrap/worker.ts for its binding. */
|
|
226
|
+
export class ${className} extends DurableObject<Cloudflare.Env> {
|
|
227
|
+
/**
|
|
228
|
+
* Handles a fetch against this object.
|
|
229
|
+
*
|
|
230
|
+
* @param request - The inbound request.
|
|
231
|
+
*/
|
|
232
|
+
async fetch(request: Request): Promise<Response> {
|
|
233
|
+
return new Response("${className}", { status: 200 });
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
`;
|
|
237
|
+
}
|
package/dist/make/generate.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Plans the files one `sdxc make` run writes: the file of the requested kind at the folder the
|
|
3
|
-
* layout gives it
|
|
4
|
-
* policy writes its test alone.
|
|
3
|
+
* layout gives it, when tests are on its test beside it, and when specs are on a pairing `.spec`
|
|
4
|
+
* under `spec/`, each rendered from its stub; a policy writes its test alone.
|
|
5
5
|
* Nothing is written while planning, and a file that already exists stops the plan unless the
|
|
6
6
|
* request replaces it.
|
|
7
7
|
*
|
|
@@ -23,10 +23,14 @@ export interface MakeRequest {
|
|
|
23
23
|
variant: string | null;
|
|
24
24
|
/** Whether to write a test beside the file. */
|
|
25
25
|
tests: boolean;
|
|
26
|
+
/** Whether to write a pairing `.spec` under `spec/` for kinds that support one. */
|
|
27
|
+
specs: boolean;
|
|
26
28
|
/** The migration chain, for `make migration`. */
|
|
27
29
|
chain?: string;
|
|
28
30
|
/** The route contract a controller is declared in, a module in `routes/`. */
|
|
29
31
|
routes: string;
|
|
32
|
+
/** The route pattern a controller answers, for the pairing `.spec`'s URL. */
|
|
33
|
+
url?: string;
|
|
30
34
|
/** Replace a file the plan would write that already exists, instead of stopping. */
|
|
31
35
|
replace?: boolean;
|
|
32
36
|
/** Plan the test alone, for a file that exists already. */
|
|
@@ -50,6 +54,11 @@ export interface Plan {
|
|
|
50
54
|
* @param overrides - Placeholder values a declaration decided, such as a job's reused group.
|
|
51
55
|
*/
|
|
52
56
|
export declare function plan(request: MakeRequest, root: string, config: Config, packageRoot: string, overrides?: Readonly<Record<string, string>>): Promise<Result<Plan, Error>>;
|
|
57
|
+
/**
|
|
58
|
+
* The pairing `.spec` path for `request`: controllers under `spec/`, commands under
|
|
59
|
+
* `spec/commands/`, MCP tools under `spec/mcp/`.
|
|
60
|
+
*/
|
|
61
|
+
export declare function specPathOf(request: Pick<MakeRequest, "kind" | "name">): string;
|
|
53
62
|
/** Whether `path`, relative to `root`, exists. */
|
|
54
63
|
export declare function exists(root: string, path: string): Promise<boolean>;
|
|
55
64
|
/** The folder a kind's files go in; a migration's comes from its chain instead. */
|
package/dist/make/generate.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Plans the files one `sdxc make` run writes: the file of the requested kind at the folder the
|
|
3
|
-
* layout gives it
|
|
4
|
-
* policy writes its test alone.
|
|
3
|
+
* layout gives it, when tests are on its test beside it, and when specs are on a pairing `.spec`
|
|
4
|
+
* under `spec/`, each rendered from its stub; a policy writes its test alone.
|
|
5
5
|
* Nothing is written while planning, and a file that already exists stops the plan unless the
|
|
6
6
|
* request replaces it.
|
|
7
7
|
*
|
|
@@ -44,9 +44,18 @@ export async function plan(request, root, config, packageRoot, overrides = {}) {
|
|
|
44
44
|
}
|
|
45
45
|
let path = `${folder}/${file}${kind.extension}`;
|
|
46
46
|
let testPath = `${folder}/${file}.test${kind.extension}`;
|
|
47
|
+
let specPath = specPathOf(request);
|
|
47
48
|
let writesTest = kind.tests && (request.tests || request.testAlone === true);
|
|
49
|
+
let writesSpec = kind.specs === true &&
|
|
50
|
+
request.specs &&
|
|
51
|
+
request.testAlone !== true &&
|
|
52
|
+
request.variant !== "resource";
|
|
48
53
|
let writesMain = kind.testOnly !== true && request.testAlone !== true;
|
|
49
|
-
let paths = [
|
|
54
|
+
let paths = [
|
|
55
|
+
...(writesMain ? [path] : []),
|
|
56
|
+
...(writesTest ? [testPath] : []),
|
|
57
|
+
...(writesSpec ? [specPath] : []),
|
|
58
|
+
];
|
|
50
59
|
for (let candidate of paths) {
|
|
51
60
|
if (request.replace !== true && (await exists(root, candidate))) {
|
|
52
61
|
return failure(new Error(`${candidate} already exists; sdxc make leaves it as it is unless --force`));
|
|
@@ -56,19 +65,37 @@ export async function plan(request, root, config, packageRoot, overrides = {}) {
|
|
|
56
65
|
Object.assign(values, overrides);
|
|
57
66
|
let files = [];
|
|
58
67
|
if (writesMain) {
|
|
59
|
-
let main = await renderFile(request, values,
|
|
68
|
+
let main = await renderFile(request, values, "main", root, config, packageRoot);
|
|
60
69
|
if (isFailure(main))
|
|
61
70
|
return main;
|
|
62
71
|
files.push({ path, content: main.data });
|
|
63
72
|
}
|
|
64
73
|
if (writesTest) {
|
|
65
|
-
let test = await renderFile(request, values,
|
|
74
|
+
let test = await renderFile(request, values, "test", root, config, packageRoot);
|
|
66
75
|
if (isFailure(test))
|
|
67
76
|
return test;
|
|
68
77
|
files.push({ path: testPath, content: test.data });
|
|
69
78
|
}
|
|
79
|
+
if (writesSpec) {
|
|
80
|
+
let spec = await renderFile(request, values, "spec", root, config, packageRoot);
|
|
81
|
+
if (isFailure(spec))
|
|
82
|
+
return spec;
|
|
83
|
+
files.push({ path: specPath, content: spec.data });
|
|
84
|
+
}
|
|
70
85
|
return success({ files, values });
|
|
71
86
|
}
|
|
87
|
+
/**
|
|
88
|
+
* The pairing `.spec` path for `request`: controllers under `spec/`, commands under
|
|
89
|
+
* `spec/commands/`, MCP tools under `spec/mcp/`.
|
|
90
|
+
*/
|
|
91
|
+
export function specPathOf(request) {
|
|
92
|
+
let file = request.name.segments.join("/");
|
|
93
|
+
if (request.kind === "command")
|
|
94
|
+
return `spec/commands/${file}.spec`;
|
|
95
|
+
if (request.kind === "mcp")
|
|
96
|
+
return `spec/mcp/${file}.spec`;
|
|
97
|
+
return `spec/${file}.spec`;
|
|
98
|
+
}
|
|
72
99
|
/** Whether `path`, relative to `root`, exists. */
|
|
73
100
|
export async function exists(root, path) {
|
|
74
101
|
return isSuccess(await wrap(() => stat(join(root, path))));
|
|
@@ -78,23 +105,25 @@ export function folderOf(kind, variant) {
|
|
|
78
105
|
return kind === "mcp" && variant === "resource" ? MCP_RESOURCES_FOLDER : KINDS[kind].folder;
|
|
79
106
|
}
|
|
80
107
|
/**
|
|
81
|
-
* Renders the kind's stub, or its
|
|
82
|
-
* variant without its own
|
|
83
|
-
* contract's stub when one exists (`controller.api.stub`
|
|
84
|
-
*
|
|
108
|
+
* Renders the kind's stub, its test stub or its pairing `.spec` stub with `values`. A variant's
|
|
109
|
+
* own stub wins; a variant without its own companion stub uses the kind's. A controller with no
|
|
110
|
+
* variant takes its contract's stub when one exists (`controller.api.stub` /
|
|
111
|
+
* `controller.api.spec.stub` for `routes/api`), else the kind's, and a guarded one its contract's
|
|
112
|
+
* `resource` stub the same way.
|
|
85
113
|
*/
|
|
86
|
-
async function renderFile(request, values,
|
|
87
|
-
let
|
|
114
|
+
async function renderFile(request, values, role, root, config, packageRoot) {
|
|
115
|
+
let companion = role !== "main";
|
|
116
|
+
let names = [stubName(request.kind, request.variant, role)];
|
|
88
117
|
let contractual = request.variant === null || request.variant === "resource";
|
|
89
118
|
if (request.kind === "controller" && contractual) {
|
|
90
119
|
let variant = [request.routes, request.variant].filter((part) => part !== null).join(".");
|
|
91
|
-
names.unshift(stubName(request.kind, variant,
|
|
120
|
+
names.unshift(stubName(request.kind, variant, role));
|
|
92
121
|
}
|
|
93
|
-
if (request.kind === "controller" && request.variant?.startsWith("resource") &&
|
|
94
|
-
names.push(stubName(request.kind, request.routes,
|
|
122
|
+
if (request.kind === "controller" && request.variant?.startsWith("resource") && companion) {
|
|
123
|
+
names.push(stubName(request.kind, request.routes, role));
|
|
95
124
|
}
|
|
96
|
-
if (request.variant !== null &&
|
|
97
|
-
names.push(stubName(request.kind, null,
|
|
125
|
+
if (request.variant !== null && companion)
|
|
126
|
+
names.push(stubName(request.kind, null, role));
|
|
98
127
|
let stub = await loadStub(names, root, config.make.stubs, packageRoot);
|
|
99
128
|
if (isFailure(stub))
|
|
100
129
|
return stub;
|
|
@@ -125,6 +154,8 @@ async function placeholders(request, root, file) {
|
|
|
125
154
|
command: name.segments.join(" "),
|
|
126
155
|
member: memberAccess(name.segments),
|
|
127
156
|
words: name.segments.map((segment) => literal(segment)).join(", "),
|
|
157
|
+
args: name.segments.map((segment) => literal(segment)).join(" "),
|
|
158
|
+
url: request.url ?? `/${name.segments.join("/")}`,
|
|
128
159
|
table: pluralize(name.snake),
|
|
129
160
|
attributes: "",
|
|
130
161
|
ability: "",
|
package/dist/make/imports.js
CHANGED
|
@@ -38,11 +38,13 @@ export function ensureImport(filename, source, what, matches, specifier, preferr
|
|
|
38
38
|
});
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
|
-
let clause
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
41
|
+
let clause;
|
|
42
|
+
if (what.kind === "namespace")
|
|
43
|
+
clause = `* as ${preferred}`;
|
|
44
|
+
else if (what.kind === "default")
|
|
45
|
+
clause = preferred;
|
|
46
|
+
else
|
|
47
|
+
clause = `{ ${what.name} }`;
|
|
46
48
|
let local = what.kind === "named" ? what.name : preferred;
|
|
47
49
|
let statement = `import ${clause} from ${literal(specifier)};`;
|
|
48
50
|
return success({ source: insertImport(source, references.data, statement), local });
|
package/dist/make/index.d.ts
CHANGED
|
@@ -20,6 +20,8 @@ export interface MakeInput {
|
|
|
20
20
|
name: string;
|
|
21
21
|
/** Whether to write a test beside the file; `undefined` takes `make.tests`. */
|
|
22
22
|
test: boolean | undefined;
|
|
23
|
+
/** Whether to write a pairing `.spec`; `undefined` takes `make.specs`. */
|
|
24
|
+
spec?: boolean | undefined;
|
|
23
25
|
dryRun: boolean;
|
|
24
26
|
/** `make job`: a scheduled job with this expression. */
|
|
25
27
|
cron?: string | undefined;
|
|
@@ -50,8 +52,8 @@ export interface MakeInput {
|
|
|
50
52
|
/** `make model`: the companions to leave out. */
|
|
51
53
|
skip?: readonly ModelCompanion[] | undefined;
|
|
52
54
|
/**
|
|
53
|
-
* Replace the file and its
|
|
54
|
-
* mapping and mount, and an existing model keeps its migrations.
|
|
55
|
+
* Replace the file, its test and its pairing `.spec` when they exist. A file that existed
|
|
56
|
+
* keeps its declaration, mapping and mount, and an existing model keeps its migrations.
|
|
55
57
|
*/
|
|
56
58
|
force?: boolean | undefined;
|
|
57
59
|
}
|
package/dist/make/index.js
CHANGED
|
@@ -101,8 +101,10 @@ export async function makeUnformatted(ctx, project, packageRoot, input) {
|
|
|
101
101
|
guarded: input.guardedBy !== undefined,
|
|
102
102
|
}),
|
|
103
103
|
tests: input.test ?? config.make.tests,
|
|
104
|
+
specs: input.spec ?? config.make.specs,
|
|
104
105
|
chain: input.chain,
|
|
105
106
|
routes: input.routes ?? config.make.routes,
|
|
107
|
+
url: pattern ?? `/${name.data.segments.join("/")}`,
|
|
106
108
|
replace: input.force === true,
|
|
107
109
|
};
|
|
108
110
|
let replacing = input.force === true && (await existsAlready(root, request));
|
|
@@ -337,6 +339,7 @@ async function modelCompanions(project, packageRoot, companion) {
|
|
|
337
339
|
name: name.data,
|
|
338
340
|
variant: null,
|
|
339
341
|
tests: false,
|
|
342
|
+
specs: false,
|
|
340
343
|
chain: companion.chain,
|
|
341
344
|
}, root, config, packageRoot);
|
|
342
345
|
if (isFailure(migration))
|
|
@@ -349,7 +352,7 @@ async function modelCompanions(project, packageRoot, companion) {
|
|
|
349
352
|
return source === null ? [] : [`\t${field.column}: ${source},`];
|
|
350
353
|
});
|
|
351
354
|
let attributes = values.length === 0 ? "" : `\n${values.join("\n")}\n`;
|
|
352
|
-
let factory = await plan({ ...request, kind: "factory", variant: null, tests: false }, root, config, packageRoot, { attributes });
|
|
355
|
+
let factory = await plan({ ...request, kind: "factory", variant: null, tests: false, specs: false }, root, config, packageRoot, { attributes });
|
|
353
356
|
if (isFailure(factory))
|
|
354
357
|
return factory;
|
|
355
358
|
let [file] = factory.data.files;
|
package/dist/make/kinds.d.ts
CHANGED
|
@@ -15,6 +15,11 @@ export interface Kind {
|
|
|
15
15
|
extension: ".ts" | ".tsx" | ".sql";
|
|
16
16
|
/** Whether `make.tests` writes a test beside the file. */
|
|
17
17
|
tests: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Whether `make.specs` writes a pairing `.spec` under `spec/` for black-box coverage of the
|
|
20
|
+
* generated surface (a page, an API route, a command or an MCP tool).
|
|
21
|
+
*/
|
|
22
|
+
specs?: boolean;
|
|
18
23
|
/**
|
|
19
24
|
* Whether the kind writes its test alone: a policy's abilities and grants are edits to the
|
|
20
25
|
* app's catalog and policy, so the only file it writes is the test that checks them.
|
package/dist/make/kinds.js
CHANGED
|
@@ -32,8 +32,18 @@ export const KINDS = {
|
|
|
32
32
|
folder: "app/http/controllers",
|
|
33
33
|
extension: ".tsx",
|
|
34
34
|
tests: true,
|
|
35
|
+
specs: true,
|
|
35
36
|
nested: true,
|
|
36
|
-
placeholders: [
|
|
37
|
+
placeholders: [
|
|
38
|
+
"path",
|
|
39
|
+
"routes",
|
|
40
|
+
"ability",
|
|
41
|
+
"access",
|
|
42
|
+
"pageAccess",
|
|
43
|
+
"params",
|
|
44
|
+
"sampleParams",
|
|
45
|
+
"url",
|
|
46
|
+
],
|
|
37
47
|
},
|
|
38
48
|
middleware: {
|
|
39
49
|
folder: "app/http/middleware",
|
|
@@ -113,6 +123,7 @@ export const KINDS = {
|
|
|
113
123
|
folder: "app/mcp/tools",
|
|
114
124
|
extension: ".ts",
|
|
115
125
|
tests: true,
|
|
126
|
+
specs: true,
|
|
116
127
|
nested: true,
|
|
117
128
|
placeholders: ["path", "tool", "uri"],
|
|
118
129
|
},
|
|
@@ -120,8 +131,9 @@ export const KINDS = {
|
|
|
120
131
|
folder: "app/commands",
|
|
121
132
|
extension: ".ts",
|
|
122
133
|
tests: true,
|
|
134
|
+
specs: true,
|
|
123
135
|
nested: true,
|
|
124
|
-
placeholders: ["command", "member", "words"],
|
|
136
|
+
placeholders: ["command", "member", "words", "args"],
|
|
125
137
|
},
|
|
126
138
|
};
|
|
127
139
|
/** What `make model` writes beside the model unless `--skip` names it. */
|
package/dist/make/schema-diff.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
* @copyright Sergio Xalambrí 2026
|
|
8
8
|
*/
|
|
9
9
|
import { DatabaseSync } from "node:sqlite";
|
|
10
|
+
import { iife } from "@sdxc/iife";
|
|
10
11
|
import { failure, isFailure, success, wrap } from "@sdxc/result";
|
|
11
12
|
import { checksOf, columnClause, createTable, createTableStatement, defaultClause, isKey, isNotNull, quote, uniqueIndex, } from "./schema.js";
|
|
12
13
|
/**
|
|
@@ -199,7 +200,13 @@ function diffTable(db, table, current) {
|
|
|
199
200
|
* `INTEGER PRIMARY KEY` as nullable.
|
|
200
201
|
*/
|
|
201
202
|
function columnDifference(table, name, definition, existing) {
|
|
202
|
-
let generated =
|
|
203
|
+
let generated = iife(() => {
|
|
204
|
+
if (definition.computed === undefined)
|
|
205
|
+
return 0;
|
|
206
|
+
if (definition.computed.stored)
|
|
207
|
+
return 3;
|
|
208
|
+
return 2;
|
|
209
|
+
});
|
|
203
210
|
let hidden = existing.hidden === 2 || existing.hidden === 3 ? existing.hidden : 0;
|
|
204
211
|
if (generated !== hidden)
|
|
205
212
|
return "changes how it is generated";
|
package/dist/make/stubs.d.ts
CHANGED
|
@@ -20,8 +20,13 @@ export interface CopiedStubs {
|
|
|
20
20
|
written: string[];
|
|
21
21
|
kept: string[];
|
|
22
22
|
}
|
|
23
|
-
/**
|
|
24
|
-
export
|
|
23
|
+
/** Whether a stub is the main file, its Vitest sibling, or its pairing `.spec`. */
|
|
24
|
+
export type StubRole = "main" | "test" | "spec";
|
|
25
|
+
/**
|
|
26
|
+
* The stub file name for `kind`, its `variant` and `role`: `controller.api.spec.stub`,
|
|
27
|
+
* `command.test.stub`, `mcp.stub`.
|
|
28
|
+
*/
|
|
29
|
+
export declare function stubName(kind: KindName, variant: string | null, role?: StubRole): string;
|
|
25
30
|
/** The folder of built-in stubs in the `@sdxc/kit` package at `packageRoot`. */
|
|
26
31
|
export declare function builtinStubsDir(packageRoot: string): string;
|
|
27
32
|
/**
|
package/dist/make/stubs.js
CHANGED
|
@@ -11,9 +11,14 @@ import { join } from "node:path";
|
|
|
11
11
|
import { failure, isFailure, isSuccess, success, wrap } from "@sdxc/result";
|
|
12
12
|
/** A placeholder as stubs spell it: `{{name}}`, spaces inside the braces allowed. */
|
|
13
13
|
const PLACEHOLDER = /\{\{\s*([A-Za-z]+)\s*\}\}/g;
|
|
14
|
-
/**
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
/**
|
|
15
|
+
* The stub file name for `kind`, its `variant` and `role`: `controller.api.spec.stub`,
|
|
16
|
+
* `command.test.stub`, `mcp.stub`.
|
|
17
|
+
*/
|
|
18
|
+
export function stubName(kind, variant, role = "main") {
|
|
19
|
+
return [kind, variant, role === "main" ? null : role, "stub"]
|
|
20
|
+
.filter((part) => part !== null)
|
|
21
|
+
.join(".");
|
|
17
22
|
}
|
|
18
23
|
/** The folder of built-in stubs in the `@sdxc/kit` package at `packageRoot`. */
|
|
19
24
|
export function builtinStubsDir(packageRoot) {
|
package/dist/make/test.js
CHANGED
package/dist/models.js
CHANGED
|
@@ -36,13 +36,13 @@ async function modelFiles(root) {
|
|
|
36
36
|
function columnsOf(table) {
|
|
37
37
|
return Object.entries(table.columns).map(([name, definition]) => {
|
|
38
38
|
let reference = definition.references;
|
|
39
|
-
let key =
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
39
|
+
let key = null;
|
|
40
|
+
if (table.primaryKey.includes(name))
|
|
41
|
+
key = "primary";
|
|
42
|
+
else if (reference !== undefined)
|
|
43
|
+
key = `${reference.table.name}.${reference.columns.join(", ")}`;
|
|
44
|
+
else if (definition.unique !== undefined && definition.unique !== false)
|
|
45
|
+
key = "unique";
|
|
46
46
|
return {
|
|
47
47
|
name,
|
|
48
48
|
type: definition.type,
|
package/dist/routes/modules.js
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
import { readFile, stat } from "node:fs/promises";
|
|
10
10
|
import { join, posix } from "node:path";
|
|
11
11
|
import { findModuleReferences, tokenize } from "@sdxc/codemod";
|
|
12
|
+
import { iife } from "@sdxc/iife";
|
|
12
13
|
import { isFailure, isSuccess, wrap } from "@sdxc/result";
|
|
13
14
|
import { applyHelper, helperName } from "./helpers.js";
|
|
14
15
|
import { Reader } from "./parse.js";
|
|
@@ -201,11 +202,13 @@ export class Project {
|
|
|
201
202
|
let tokens = tokenize(path, source.data);
|
|
202
203
|
let references = findModuleReferences(path, source.data);
|
|
203
204
|
if (isFailure(tokens) || isFailure(references)) {
|
|
204
|
-
let error =
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
205
|
+
let error = iife(() => {
|
|
206
|
+
if (isFailure(tokens))
|
|
207
|
+
return tokens.error;
|
|
208
|
+
if (isFailure(references))
|
|
209
|
+
return references.error;
|
|
210
|
+
return null;
|
|
211
|
+
});
|
|
209
212
|
this.diagnostics.push(`${path}: could not be read (${error?.message ?? "unknown error"})`);
|
|
210
213
|
return null;
|
|
211
214
|
}
|