@sdxc/kit 2026.10.17 → 2026.10.21
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 +397 -22
- package/changelog.jsonl +1171 -1092
- package/config.schema.json +53 -1
- package/dist/cli.js +12 -0
- package/dist/commands/deploy.d.ts +23 -0
- package/dist/commands/deploy.js +167 -0
- package/dist/commands/destroy.d.ts +22 -0
- package/dist/commands/destroy.js +92 -0
- package/dist/commands/doctor.d.ts +26 -0
- package/dist/commands/doctor.js +65 -0
- package/dist/commands/initializers.d.ts +16 -0
- package/dist/commands/initializers.js +23 -0
- package/dist/commands/make.d.ts +78 -6
- package/dist/commands/make.js +134 -20
- package/dist/commands/middleware.d.ts +16 -0
- package/dist/commands/middleware.js +21 -0
- package/dist/commands/models.d.ts +18 -0
- package/dist/commands/models.js +29 -0
- package/dist/commands/notes.d.ts +17 -0
- package/dist/commands/notes.js +27 -0
- package/dist/commands/routes.d.ts +25 -0
- package/dist/commands/routes.js +50 -0
- package/dist/commands/run.d.ts +16 -0
- package/dist/commands/run.js +32 -11
- package/dist/commands/scaffold.d.ts +30 -0
- package/dist/commands/scaffold.js +34 -0
- package/dist/commands/schedule.d.ts +16 -0
- package/dist/commands/schedule.js +21 -0
- package/dist/commands/stats.d.ts +16 -0
- package/dist/commands/stats.js +21 -0
- package/dist/commands/ui/update.d.ts +6 -3
- package/dist/commands/ui/update.js +24 -7
- package/dist/commands/upgrade.d.ts +3 -1
- package/dist/commands/upgrade.js +88 -23
- package/dist/config.d.ts +20 -0
- package/dist/config.js +32 -1
- package/dist/contract.d.ts +101 -3
- package/dist/contract.js +371 -26
- package/dist/create/templates.js +3 -2
- package/dist/dependencies.d.ts +5 -1
- package/dist/dependencies.js +16 -1
- package/dist/deploy/plan.d.ts +49 -0
- package/dist/deploy/plan.js +68 -0
- package/dist/destroy.d.ts +40 -0
- package/dist/destroy.js +317 -0
- package/dist/doctor/check.d.ts +71 -0
- package/dist/doctor/check.js +53 -0
- package/dist/doctor/checks.d.ts +23 -0
- package/dist/doctor/checks.js +70 -0
- package/dist/doctor/define.d.ts +59 -0
- package/dist/doctor/define.js +16 -0
- package/dist/doctor/emails.d.ts +18 -0
- package/dist/doctor/emails.js +243 -0
- package/dist/doctor/env.d.ts +26 -0
- package/dist/doctor/env.js +88 -0
- package/dist/doctor/files.d.ts +14 -0
- package/dist/doctor/files.js +34 -0
- package/dist/doctor/fixture.d.ts +23 -0
- package/dist/doctor/fixture.js +46 -0
- package/dist/doctor/install.d.ts +17 -0
- package/dist/doctor/install.js +37 -0
- package/dist/doctor/installed.d.ts +11 -0
- package/dist/doctor/installed.js +79 -0
- package/dist/doctor/jsdoc.d.ts +44 -0
- package/dist/doctor/jsdoc.js +201 -0
- package/dist/doctor/layout.d.ts +23 -0
- package/dist/doctor/layout.js +83 -0
- package/dist/doctor/migrations.d.ts +16 -0
- package/dist/doctor/migrations.js +96 -0
- package/dist/doctor/models.d.ts +13 -0
- package/dist/doctor/models.js +69 -0
- package/dist/doctor/pins.d.ts +15 -0
- package/dist/doctor/pins.js +79 -0
- package/dist/doctor/project.d.ts +58 -0
- package/dist/doctor/project.js +261 -0
- package/dist/doctor/remix.d.ts +14 -0
- package/dist/doctor/remix.js +44 -0
- package/dist/doctor/routes.d.ts +11 -0
- package/dist/doctor/routes.js +49 -0
- package/dist/doctor/typegen.d.ts +13 -0
- package/dist/doctor/typegen.js +57 -0
- package/dist/doctor/ui.d.ts +14 -0
- package/dist/doctor/ui.js +51 -0
- package/dist/initializers.d.ts +25 -0
- package/dist/initializers.js +89 -0
- package/dist/lock.d.ts +2 -2
- package/dist/make/actions.d.ts +23 -0
- package/dist/make/actions.js +82 -0
- package/dist/make/declare.d.ts +31 -3
- package/dist/make/declare.js +284 -9
- package/dist/make/email.d.ts +23 -0
- package/dist/make/email.js +111 -0
- package/dist/make/from-fields.d.ts +30 -0
- package/dist/make/from-fields.js +162 -0
- package/dist/make/from-model.d.ts +39 -0
- package/dist/make/from-model.js +161 -0
- package/dist/make/generate.d.ts +25 -5
- package/dist/make/generate.js +76 -22
- package/dist/make/guard.d.ts +35 -0
- package/dist/make/guard.js +73 -0
- package/dist/make/imports.d.ts +6 -0
- package/dist/make/imports.js +19 -0
- package/dist/make/index.d.ts +49 -4
- package/dist/make/index.js +290 -29
- package/dist/make/kinds.d.ts +34 -2
- package/dist/make/kinds.js +92 -8
- package/dist/make/mount.d.ts +25 -0
- package/dist/make/mount.js +135 -0
- package/dist/make/schema-diff.d.ts +58 -0
- package/dist/make/schema-diff.js +609 -0
- package/dist/make/schema.d.ts +76 -0
- package/dist/make/schema.js +206 -0
- package/dist/make/stubs.d.ts +14 -7
- package/dist/make/stubs.js +24 -8
- package/dist/make/test.d.ts +43 -0
- package/dist/make/test.js +150 -0
- package/dist/make/trigger.d.ts +31 -0
- package/dist/make/trigger.js +163 -0
- package/dist/models.d.ts +42 -0
- package/dist/models.js +149 -0
- package/dist/notes.d.ts +28 -0
- package/dist/notes.js +66 -0
- package/dist/reference.d.ts +27 -0
- package/dist/reference.js +103 -0
- package/dist/releases.d.ts +9 -0
- package/dist/releases.js +29 -8
- package/dist/routes/chains.d.ts +34 -0
- package/dist/routes/chains.js +95 -0
- package/dist/routes/filter.d.ts +53 -0
- package/dist/routes/filter.js +147 -0
- package/dist/routes/format.d.ts +23 -0
- package/dist/routes/format.js +123 -0
- package/dist/routes/helpers.d.ts +21 -0
- package/dist/routes/helpers.js +165 -0
- package/dist/routes/index.d.ts +56 -0
- package/dist/routes/index.js +167 -0
- package/dist/routes/modules.d.ts +116 -0
- package/dist/routes/modules.js +235 -0
- package/dist/routes/parse.d.ts +161 -0
- package/dist/routes/parse.js +621 -0
- package/dist/routes/routers.d.ts +69 -0
- package/dist/routes/routers.js +327 -0
- package/dist/scaffold/fields.d.ts +129 -0
- package/dist/scaffold/fields.js +421 -0
- package/dist/scaffold/index.d.ts +39 -0
- package/dist/scaffold/index.js +622 -0
- package/dist/scaffold/render.d.ts +61 -0
- package/dist/scaffold/render.js +417 -0
- package/dist/schedule.d.ts +45 -0
- package/dist/schedule.js +124 -0
- package/dist/stats.d.ts +37 -0
- package/dist/stats.js +126 -0
- package/dist/ui/commands.d.ts +10 -3
- package/dist/ui/commands.js +85 -15
- package/dist/ui/registry.d.ts +8 -8
- package/dist/upgrade/agents.d.ts +49 -0
- package/dist/upgrade/agents.js +110 -0
- package/dist/upgrade/changelog.d.ts +2 -2
- package/dist/upgrade/project.d.ts +4 -0
- package/dist/upgrade/project.js +11 -1
- package/dist/upgrade/prompt.d.ts +11 -0
- package/dist/upgrade/prompt.js +50 -7
- package/package.json +6 -4
- package/registry/ui/alert-dialog.json +2 -2
- package/registry/ui/attachment.json +2 -2
- package/registry/ui/calendar.json +2 -2
- package/registry/ui/color-picker.json +2 -2
- package/registry/ui/command.json +2 -2
- package/registry/ui/date-picker.json +2 -2
- package/registry/ui/date-range-picker.json +2 -2
- package/registry/ui/dialog.json +2 -2
- package/registry/ui/file-trigger.json +2 -2
- package/registry/ui/heading-scope.json +2 -2
- package/registry/ui/message-scroller.json +2 -2
- package/registry/ui/number-field.json +2 -2
- package/registry/ui/range-calendar.json +2 -2
- package/registry/ui/resizable.json +2 -2
- package/registry/ui/select.json +2 -2
- package/registry/ui/shared-element.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.resource.stub +23 -0
- package/stubs/controller.api.spec.stub +15 -0
- package/stubs/controller.form.stub +2 -2
- package/stubs/controller.form.test.stub +2 -2
- package/stubs/controller.resource.form.stub +67 -0
- package/stubs/controller.resource.stub +28 -0
- package/stubs/controller.spec.stub +15 -0
- package/stubs/email.stub +9 -14
- package/stubs/email.test.stub +9 -9
- package/stubs/factory.stub +1 -1
- package/stubs/mcp.spec.stub +16 -0
- package/stubs/model.stub +3 -3
- package/stubs/policy.test.stub +34 -8
- package/stubs/scaffold.api.create.stub +34 -0
- package/stubs/scaffold.api.create.test.stub +37 -0
- package/stubs/scaffold.api.destroy.stub +34 -0
- package/stubs/scaffold.api.destroy.test.stub +44 -0
- package/stubs/scaffold.api.index.stub +23 -0
- package/stubs/scaffold.api.index.test.stub +31 -0
- package/stubs/scaffold.api.show.stub +27 -0
- package/stubs/scaffold.api.show.test.stub +41 -0
- package/stubs/scaffold.api.update.stub +44 -0
- package/stubs/scaffold.api.update.test.stub +46 -0
- package/stubs/scaffold.command.create.stub +22 -0
- package/stubs/scaffold.command.create.test.stub +30 -0
- package/stubs/scaffold.command.delete.stub +20 -0
- package/stubs/scaffold.command.delete.test.stub +39 -0
- package/stubs/scaffold.command.list.stub +18 -0
- package/stubs/scaffold.command.list.test.stub +31 -0
- package/stubs/scaffold.command.show.stub +19 -0
- package/stubs/scaffold.command.show.test.stub +37 -0
- package/stubs/scaffold.controller.destroy.stub +35 -0
- package/stubs/scaffold.controller.destroy.test.stub +65 -0
- package/stubs/scaffold.controller.edit.stub +97 -0
- package/stubs/scaffold.controller.edit.test.stub +91 -0
- package/stubs/scaffold.controller.index.stub +55 -0
- package/stubs/scaffold.controller.index.test.stub +32 -0
- package/stubs/scaffold.controller.new.stub +70 -0
- package/stubs/scaffold.controller.new.test.stub +81 -0
- package/stubs/scaffold.controller.show.stub +59 -0
- package/stubs/scaffold.controller.show.test.stub +43 -0
- package/stubs/scaffold.factory.stub +16 -0
- package/stubs/scaffold.form.stub +75 -0
- package/stubs/scaffold.mcp.create.stub +24 -0
- package/stubs/scaffold.mcp.create.test.stub +36 -0
- package/stubs/scaffold.mcp.delete.stub +31 -0
- package/stubs/scaffold.mcp.delete.test.stub +43 -0
- package/stubs/scaffold.mcp.get.stub +21 -0
- package/stubs/scaffold.mcp.get.test.stub +31 -0
- package/stubs/scaffold.mcp.list.stub +23 -0
- package/stubs/scaffold.mcp.list.test.stub +25 -0
- package/stubs/scaffold.mcp.update.stub +37 -0
- package/stubs/scaffold.mcp.update.test.stub +40 -0
- package/stubs/scaffold.migration.stub +3 -0
- package/stubs/scaffold.model.stub +35 -0
- package/stubs/scaffold.model.test.stub +25 -0
- package/stubs/scaffold.spec.stub +7 -0
- package/stubs/scaffold.test-database.stub +33 -0
- package/stubs/scaffold.test-mcp.stub +92 -0
- package/stubs/scaffold.validator.stub +18 -0
- package/stubs/scaffold.validator.test.stub +26 -0
- package/templates/app/AGENTS.md +167 -0
- package/templates/app/CLAUDE.md +1 -0
- package/templates/app/README.md +33 -15
- package/templates/app/app/commands/console.test.ts +26 -0
- package/templates/app/app/commands/console.ts +19 -0
- package/templates/app/app/commands/db/fresh.test.ts +57 -0
- package/templates/app/app/commands/db/fresh.ts +37 -0
- package/templates/app/app/commands/db/migrate.test.ts +17 -1
- package/templates/app/app/commands/db/migrate.ts +51 -18
- package/templates/app/app/commands/db/prepare.test.ts +39 -0
- package/templates/app/app/commands/db/prepare.ts +35 -0
- package/templates/app/app/commands/db/seed.test.ts +38 -0
- package/templates/app/app/commands/db/seed.ts +26 -0
- package/templates/app/app/commands/deploy.test.ts +34 -0
- package/templates/app/app/commands/deploy.ts +18 -0
- package/templates/app/app/commands/emails/preview.test.tsx +63 -0
- package/templates/app/app/commands/emails/preview.ts +24 -0
- package/templates/app/app/commands/index.ts +57 -2
- package/templates/app/app/commands/typegen.test.ts +20 -1
- package/templates/app/app/commands/typegen.ts +16 -2
- package/templates/app/app/emails/index.ts +10 -0
- package/templates/app/app/http/middleware/access.test.ts +43 -0
- package/templates/app/app/http/middleware/access.ts +41 -0
- package/templates/app/app/http/middleware/api.ts +12 -3
- package/templates/app/app/http/middleware/page.test.ts +4 -1
- package/templates/app/app/http/middleware/page.ts +7 -5
- package/templates/app/app/initializers/cli.ts +35 -2
- package/templates/app/app/initializers/dispatcher.ts +9 -1
- package/templates/app/app/initializers/mail-transport.test.ts +33 -0
- package/templates/app/app/initializers/mail-transport.ts +26 -0
- package/templates/app/app/initializers/mail.test.ts +17 -17
- package/templates/app/app/initializers/mail.ts +15 -7
- package/templates/app/app/initializers/router.test.ts +7 -0
- package/templates/app/app/initializers/router.tsx +13 -3
- package/templates/app/app/jobs/email.test.tsx +70 -0
- package/templates/app/app/jobs/email.ts +16 -0
- package/templates/app/app/jobs/index.ts +6 -2
- package/templates/app/app/policies/abilities.ts +13 -0
- package/templates/app/app/policies/policy.test.ts +15 -0
- package/templates/app/app/policies/policy.ts +30 -0
- package/templates/app/app/services/commands.ts +30 -0
- package/templates/app/app/services/migrations.test.ts +48 -1
- package/templates/app/app/services/migrations.ts +66 -9
- package/templates/app/app/test/cloudflare-workers.ts +2 -0
- package/templates/app/app/test/local-database.ts +67 -0
- package/templates/app/app/test/session.ts +28 -0
- package/templates/app/bootstrap/console.ts +13 -0
- package/templates/app/cloudflare.config.ts +3 -3
- package/templates/app/database/seed.ts +12 -0
- package/templates/app/package.json +3 -1
- package/templates/app/spec/config.jsonc +16 -0
- package/stubs/policy.stub +0 -16
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `sdxc make migration <name> field ...`: the name says what the migration does, as Rails reads
|
|
3
|
+
* it (`create-posts`, `add-slug-to-posts`, `remove-slug-from-posts`), and the fields say which
|
|
4
|
+
* columns, typed by the conventions `sdxc make model` and `sdxc scaffold` use.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { failure, isFailure, success, wrap } from "@sdxc/result";
|
|
10
|
+
import { column, table } from "remix/data-table";
|
|
11
|
+
import { buildColumn, fieldIndexes, fieldsTable, parseFields, resolveReferences, } from "../scaffold/fields.js";
|
|
12
|
+
import { readChain } from "./from-model.js";
|
|
13
|
+
import { replayChain } from "./schema-diff.js";
|
|
14
|
+
import { columnClause, createTableStatement, quote, tableShape, uniqueIndex } from "./schema.js";
|
|
15
|
+
/** The columns every table has, which no migration adds or drops one by one. */
|
|
16
|
+
const RESERVED_COLUMNS = new Set(["id", "created_at", "updated_at"]);
|
|
17
|
+
/**
|
|
18
|
+
* The intent `name` spells, or `null` when it spells none: `create-<table>`,
|
|
19
|
+
* `add-<words>-to-<table>` or `remove-<words>-from-<table>`. The last `to` or `from` starts the
|
|
20
|
+
* table's name, so `add-to-do-to-tasks` adds to `tasks`.
|
|
21
|
+
*/
|
|
22
|
+
export function migrationIntent(name) {
|
|
23
|
+
let words = name.base.split("-");
|
|
24
|
+
let [verb, ...rest] = words;
|
|
25
|
+
if (verb === "create" && rest.length > 0)
|
|
26
|
+
return { action: "create", table: rest.join("_") };
|
|
27
|
+
if (verb !== "add" && verb !== "remove")
|
|
28
|
+
return null;
|
|
29
|
+
let at = words.lastIndexOf(verb === "add" ? "to" : "from");
|
|
30
|
+
if (at < 1 || at === words.length - 1)
|
|
31
|
+
return null;
|
|
32
|
+
return { action: verb, table: words.slice(at + 1).join("_") };
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The content of the migration `name` that `specs` describe, applied to the replayed chain at
|
|
36
|
+
* `folder` before it is returned, so a table that already exists, a missing table or a column
|
|
37
|
+
* SQLite cannot drop fails here. A chain that does not replay is left unchecked.
|
|
38
|
+
*
|
|
39
|
+
* @param root - The project root, whose `app/models/` resolves each reference's table.
|
|
40
|
+
*/
|
|
41
|
+
export async function fieldMigration(root, folder, name, specs) {
|
|
42
|
+
let intent = migrationIntent(name);
|
|
43
|
+
if (intent === null) {
|
|
44
|
+
return failure(new Error(`"${name.base}" names no table to change; with fields, name the migration create-<table>, add-<columns>-to-<table> or remove-<columns>-from-<table>`));
|
|
45
|
+
}
|
|
46
|
+
let planned = await statementsOf(root, intent, specs);
|
|
47
|
+
if (isFailure(planned))
|
|
48
|
+
return planned;
|
|
49
|
+
let { summary, statements } = planned.data;
|
|
50
|
+
let applied = await applyToChain(root, folder, statements);
|
|
51
|
+
if (isFailure(applied))
|
|
52
|
+
return applied;
|
|
53
|
+
return success(`-- ${name.pascal}: ${summary}.\n\n${statements.join("\n\n")}\n`);
|
|
54
|
+
}
|
|
55
|
+
/** The statements `intent` needs for `specs`, and the line the file's header says it does. */
|
|
56
|
+
async function statementsOf(root, intent, specs) {
|
|
57
|
+
let { table: name } = intent;
|
|
58
|
+
if (intent.action === "remove") {
|
|
59
|
+
let columns = droppedColumns(specs);
|
|
60
|
+
if (isFailure(columns))
|
|
61
|
+
return columns;
|
|
62
|
+
return success({
|
|
63
|
+
summary: `drops ${columns.data.join(", ")} from ${name}`,
|
|
64
|
+
statements: columns.data.map((dropped) => `ALTER TABLE ${quote(name)} DROP COLUMN ${quote(dropped)};`),
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
let parsed = parseFields(specs);
|
|
68
|
+
if (isFailure(parsed))
|
|
69
|
+
return parsed;
|
|
70
|
+
let fields = await resolveReferences(root, parsed.data);
|
|
71
|
+
if (intent.action === "create") {
|
|
72
|
+
return success({
|
|
73
|
+
summary: `creates the ${name} table`,
|
|
74
|
+
statements: [
|
|
75
|
+
...createTableStatement(fieldsTable(name, fields)).split("\n\n"),
|
|
76
|
+
...fieldIndexes(name, fields),
|
|
77
|
+
],
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
let shape = addedColumns(name, fields);
|
|
81
|
+
if (isFailure(shape))
|
|
82
|
+
return shape;
|
|
83
|
+
return success({
|
|
84
|
+
summary: `adds ${fields.map((field) => field.column).join(", ")} to ${name}`,
|
|
85
|
+
statements: [
|
|
86
|
+
...fields.map((field) => `ALTER TABLE ${quote(name)} ADD COLUMN ${columnClause(shape.data, field.column, shape.data.columns[field.column], { constraints: true })};`),
|
|
87
|
+
...fields
|
|
88
|
+
.filter((field) => field.index === "unique")
|
|
89
|
+
.map((field) => uniqueIndex(shape.data, field.column)),
|
|
90
|
+
...fieldIndexes(name, fields),
|
|
91
|
+
],
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* The columns `fields` add to `name`, as a table holding only them beside its key. SQLite fills an added column
|
|
96
|
+
* of every existing row with its default, so a box defaults to unticked and any other field must
|
|
97
|
+
* be nullable; a foreign key stays nullable too, as D1 enforces it on the rows already there.
|
|
98
|
+
*/
|
|
99
|
+
function addedColumns(name, fields) {
|
|
100
|
+
let columns = { id: column.text().primaryKey() };
|
|
101
|
+
for (let field of fields) {
|
|
102
|
+
if (field.type === "boolean") {
|
|
103
|
+
columns[field.column] = column.boolean().default(false);
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
if (!field.nullable) {
|
|
107
|
+
return failure(new Error(`${field.column} needs a value for the rows ${name} already holds; write ${field.column}? to add it nullable, or declare it in the model and run make migration --from-model, which rebuilds the table`));
|
|
108
|
+
}
|
|
109
|
+
columns[field.column] = buildColumn(field, name, column);
|
|
110
|
+
}
|
|
111
|
+
let shape = tableShape(table({ name, columns }));
|
|
112
|
+
if (shape === null)
|
|
113
|
+
return failure(new Error(`Could not describe the columns added to ${name}`));
|
|
114
|
+
return success(shape);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* The snake_case columns `specs` name, each a field as `make model` takes it with its type and
|
|
118
|
+
* `?` ignored; `author:references` names `author_id`. A reserved, repeated or invalid name fails.
|
|
119
|
+
*/
|
|
120
|
+
function droppedColumns(specs) {
|
|
121
|
+
let columns = [];
|
|
122
|
+
for (let spec of specs) {
|
|
123
|
+
let [rawName = "", rawType = ""] = spec.split(":");
|
|
124
|
+
let words = rawName
|
|
125
|
+
.replace(/\?$/, "")
|
|
126
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
127
|
+
.split(/[\s_-]+/)
|
|
128
|
+
.filter((word) => word !== "")
|
|
129
|
+
.map((word) => word.toLowerCase());
|
|
130
|
+
if (rawType.replace(/\?$/, "") === "references" && words.at(-1) !== "id")
|
|
131
|
+
words.push("id");
|
|
132
|
+
let dropped = words.join("_");
|
|
133
|
+
if (!/^[a-z][a-z0-9_]*$/.test(dropped)) {
|
|
134
|
+
return failure(new Error(`"${spec}" is not a column name; start it with a letter`));
|
|
135
|
+
}
|
|
136
|
+
if (RESERVED_COLUMNS.has(dropped)) {
|
|
137
|
+
return failure(new Error(`${dropped} is a column of every table; it stays`));
|
|
138
|
+
}
|
|
139
|
+
if (columns.includes(dropped))
|
|
140
|
+
return failure(new Error(`${dropped} is listed twice`));
|
|
141
|
+
columns.push(dropped);
|
|
142
|
+
}
|
|
143
|
+
return success(columns);
|
|
144
|
+
}
|
|
145
|
+
/** Applies `statements` after the chain at `folder`, failing with SQLite's own reason. */
|
|
146
|
+
async function applyToChain(root, folder, statements) {
|
|
147
|
+
let migrations = await readChain(root, folder);
|
|
148
|
+
if (isFailure(migrations))
|
|
149
|
+
return success(undefined);
|
|
150
|
+
let replayed = replayChain(migrations.data);
|
|
151
|
+
if (isFailure(replayed))
|
|
152
|
+
return success(undefined);
|
|
153
|
+
let { db } = replayed.data;
|
|
154
|
+
let applied = wrap(() => db.exec(statements.join("\n")));
|
|
155
|
+
db.close();
|
|
156
|
+
if (isFailure(applied)) {
|
|
157
|
+
return failure(new Error(`The migration does not apply after ${folder}: ${applied.error.message}`, {
|
|
158
|
+
cause: applied.error,
|
|
159
|
+
}));
|
|
160
|
+
}
|
|
161
|
+
return success(undefined);
|
|
162
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `sdxc make migration --from-model`: loads a model module in the project's own runtime, reads
|
|
3
|
+
* the data-table tables it exports, replays the chain the migration goes into, and plans the
|
|
4
|
+
* statements that bring that chain's schema in step with the model.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { AnyCommandContext } from "@sdxc/cli";
|
|
10
|
+
import type { Result } from "@sdxc/result";
|
|
11
|
+
import type { Project } from "../middleware.js";
|
|
12
|
+
import type { Name } from "./names.js";
|
|
13
|
+
import type { MigrationSource, SchemaChange } from "./schema-diff.js";
|
|
14
|
+
import type { TableShape } from "./schema.js";
|
|
15
|
+
/** What `--from-model` found: the model's file and the change its tables need. */
|
|
16
|
+
export interface ModelMigration {
|
|
17
|
+
/** The model module, relative to the project root. */
|
|
18
|
+
path: string;
|
|
19
|
+
change: SchemaChange;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The content of the migration `name` that brings `chain` in step with `model`, with every
|
|
23
|
+
* warning printed and kept in the file as a SQL comment, or `null`, after saying so, when the
|
|
24
|
+
* chain already matches the model and nothing should be written.
|
|
25
|
+
*/
|
|
26
|
+
export declare function modelMigration(ctx: Pick<AnyCommandContext, "stdout" | "exec">, project: Project, model: string, chain: string | undefined, name: Name): Promise<Result<string | null, Error>>;
|
|
27
|
+
/**
|
|
28
|
+
* Plans the migration of `folder`, a chain of the project at `root`, that the tables of the model
|
|
29
|
+
* `model` (named as `make model` names it) need. Fails when the model is missing, exports no
|
|
30
|
+
* table, cannot be loaded, or the chain cannot be replayed.
|
|
31
|
+
*/
|
|
32
|
+
export declare function migrationFromModel(ctx: Pick<AnyCommandContext, "exec">, root: string, folder: string, model: string): Promise<Result<ModelMigration, Error>>;
|
|
33
|
+
/**
|
|
34
|
+
* The tables the module at `path` exports, imported by the runtime `sdxc run` uses, from the
|
|
35
|
+
* project root, so the model resolves its imports exactly as the app does.
|
|
36
|
+
*/
|
|
37
|
+
export declare function loadTables(ctx: Pick<AnyCommandContext, "exec">, root: string, path: string): Promise<Result<TableShape[], Error>>;
|
|
38
|
+
/** Every numbered SQL file of the chain at `folder`; a chain with no folder yet is empty. */
|
|
39
|
+
export declare function readChain(root: string, folder: string): Promise<Result<MigrationSource[], Error>>;
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `sdxc make migration --from-model`: loads a model module in the project's own runtime, reads
|
|
3
|
+
* the data-table tables it exports, replays the chain the migration goes into, and plans the
|
|
4
|
+
* statements that bring that chain's schema in step with the model.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { readdir, readFile, stat } from "node:fs/promises";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
import { pathToFileURL } from "node:url";
|
|
12
|
+
import { failure, isFailure, success, wrap } from "@sdxc/result";
|
|
13
|
+
import { lockFileAtOrAbove, packageManager } from "../package-manager.js";
|
|
14
|
+
import { readManifest } from "../upgrade/project.js";
|
|
15
|
+
import { KINDS } from "./kinds.js";
|
|
16
|
+
import { chainFolder } from "./migrations.js";
|
|
17
|
+
import { parseName } from "./names.js";
|
|
18
|
+
import { planMigration } from "./schema-diff.js";
|
|
19
|
+
/** The prefix of the line the loader prints its tables on, apart from anything the model logs. */
|
|
20
|
+
const MODEL_MARKER = "sdxc-model-tables:";
|
|
21
|
+
/**
|
|
22
|
+
* The loader run in the project's runtime. It reports every exported table, and the table of
|
|
23
|
+
* every exported model, once each, as JSON on one marked line; a `bigint` default becomes a number.
|
|
24
|
+
*/
|
|
25
|
+
const LOADER = `
|
|
26
|
+
const KEY = "data-table.tableMetadata";
|
|
27
|
+
const metadataOf = (value) => {
|
|
28
|
+
if (value === null || (typeof value !== "object" && typeof value !== "function")) return undefined;
|
|
29
|
+
const symbol = Object.getOwnPropertySymbols(value).find((s) => s.description === KEY);
|
|
30
|
+
return symbol === undefined ? undefined : value[symbol];
|
|
31
|
+
};
|
|
32
|
+
const module = await import(process.env.SDXC_MODEL);
|
|
33
|
+
const seen = new Set();
|
|
34
|
+
const tables = [];
|
|
35
|
+
for (const value of Object.values(module)) {
|
|
36
|
+
for (const candidate of [value, value?.table]) {
|
|
37
|
+
const metadata = metadataOf(candidate);
|
|
38
|
+
if (metadata === undefined || seen.has(metadata)) continue;
|
|
39
|
+
seen.add(metadata);
|
|
40
|
+
tables.push({ name: metadata.name, primaryKey: metadata.primaryKey, columns: metadata.columnDefinitions });
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const replacer = (key, value) => (typeof value === "bigint" ? Number(value) : value);
|
|
44
|
+
process.stdout.write("\\n${MODEL_MARKER}" + JSON.stringify(tables, replacer) + "\\n");
|
|
45
|
+
`;
|
|
46
|
+
/** A chain file: a leading number, a separator and a name, as `make migration` writes them. */
|
|
47
|
+
const MIGRATION_FILE = /^\d+[-_].+\.sql$/;
|
|
48
|
+
/**
|
|
49
|
+
* The content of the migration `name` that brings `chain` in step with `model`, with every
|
|
50
|
+
* warning printed and kept in the file as a SQL comment, or `null`, after saying so, when the
|
|
51
|
+
* chain already matches the model and nothing should be written.
|
|
52
|
+
*/
|
|
53
|
+
export async function modelMigration(ctx, project, model, chain, name) {
|
|
54
|
+
let folder = chainFolder(project.config.make.migrations, chain);
|
|
55
|
+
if (isFailure(folder))
|
|
56
|
+
return folder;
|
|
57
|
+
let planned = await migrationFromModel(ctx, project.root, folder.data, model);
|
|
58
|
+
if (isFailure(planned))
|
|
59
|
+
return planned;
|
|
60
|
+
let { path, change } = planned.data;
|
|
61
|
+
for (let warning of change.warnings)
|
|
62
|
+
ctx.stdout(`Warning: ${warning}\n`);
|
|
63
|
+
if (change.statements.length === 0) {
|
|
64
|
+
ctx.stdout(`${folder.data} already matches ${path}; nothing to write\n`);
|
|
65
|
+
return success(null);
|
|
66
|
+
}
|
|
67
|
+
let header = [
|
|
68
|
+
`-- ${name.pascal}: brings ${folder.data} in step with ${path}, as sdxc make migration --from-model wrote it.`,
|
|
69
|
+
...change.warnings.map((warning) => `-- Warning: ${warning}`),
|
|
70
|
+
];
|
|
71
|
+
return success(`${header.join("\n")}\n\n${change.statements.join("\n\n")}\n`);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Plans the migration of `folder`, a chain of the project at `root`, that the tables of the model
|
|
75
|
+
* `model` (named as `make model` names it) need. Fails when the model is missing, exports no
|
|
76
|
+
* table, cannot be loaded, or the chain cannot be replayed.
|
|
77
|
+
*/
|
|
78
|
+
export async function migrationFromModel(ctx, root, folder, model) {
|
|
79
|
+
let name = parseName(model);
|
|
80
|
+
if (isFailure(name))
|
|
81
|
+
return name;
|
|
82
|
+
let path = `${KINDS.model.folder}/${name.data.segments.join("/")}${KINDS.model.extension}`;
|
|
83
|
+
let found = await wrap(() => stat(join(root, path)));
|
|
84
|
+
if (isFailure(found)) {
|
|
85
|
+
return failure(new Error(`${path} not found; --from-model takes a model's name as sdxc make model names it`));
|
|
86
|
+
}
|
|
87
|
+
let tables = await loadTables(ctx, root, path);
|
|
88
|
+
if (isFailure(tables))
|
|
89
|
+
return tables;
|
|
90
|
+
let migrations = await readChain(root, folder);
|
|
91
|
+
if (isFailure(migrations))
|
|
92
|
+
return migrations;
|
|
93
|
+
let change = planMigration(migrations.data, tables.data);
|
|
94
|
+
if (isFailure(change))
|
|
95
|
+
return change;
|
|
96
|
+
return success({ path, change: change.data });
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* The tables the module at `path` exports, imported by the runtime `sdxc run` uses, from the
|
|
100
|
+
* project root, so the model resolves its imports exactly as the app does.
|
|
101
|
+
*/
|
|
102
|
+
export async function loadTables(ctx, root, path) {
|
|
103
|
+
let runtime = await projectRuntime(root);
|
|
104
|
+
let command = runtime === "bun" ? ["bun", "-e", LOADER] : ["node", "--input-type=module", "-e", LOADER];
|
|
105
|
+
let ran = await ctx.exec(command, {
|
|
106
|
+
cwd: root,
|
|
107
|
+
stdio: "collect",
|
|
108
|
+
env: { SDXC_MODEL: pathToFileURL(join(root, path)).href },
|
|
109
|
+
});
|
|
110
|
+
if (isFailure(ran)) {
|
|
111
|
+
let printed = "stderr" in ran.error ? ran.error.stderr.trim() : "";
|
|
112
|
+
let detail = printed === "" ? ran.error.message : printed;
|
|
113
|
+
return failure(new Error(`Could not load ${path} with ${runtime}:\n${detail}`, { cause: ran.error }));
|
|
114
|
+
}
|
|
115
|
+
let line = ran.data.stdout.split("\n").find((text) => text.startsWith(MODEL_MARKER));
|
|
116
|
+
if (line === undefined)
|
|
117
|
+
return failure(new Error(`${path} did not report its tables`));
|
|
118
|
+
let parsed = wrap(() => JSON.parse(line.slice(MODEL_MARKER.length)));
|
|
119
|
+
if (isFailure(parsed))
|
|
120
|
+
return parsed;
|
|
121
|
+
let tables = parsed.data
|
|
122
|
+
.map((value) => (isTableShape(value) ? value : null))
|
|
123
|
+
.filter((value) => value !== null);
|
|
124
|
+
if (tables.length === 0) {
|
|
125
|
+
return failure(new Error(`${path} exports no remix/data-table table or model`));
|
|
126
|
+
}
|
|
127
|
+
return success(tables);
|
|
128
|
+
}
|
|
129
|
+
/** Whether a reported value has a table's name, key and columns. */
|
|
130
|
+
function isTableShape(value) {
|
|
131
|
+
if (value === null || typeof value !== "object")
|
|
132
|
+
return false;
|
|
133
|
+
let shape = value;
|
|
134
|
+
return (typeof shape.name === "string" &&
|
|
135
|
+
Array.isArray(shape.primaryKey) &&
|
|
136
|
+
typeof shape.columns === "object" &&
|
|
137
|
+
shape.columns !== null);
|
|
138
|
+
}
|
|
139
|
+
/** `bun` for a project on Bun and `node` otherwise, as `sdxc run` decides. */
|
|
140
|
+
async function projectRuntime(root) {
|
|
141
|
+
let manifest = await readManifest(root);
|
|
142
|
+
let manager = await packageManager({
|
|
143
|
+
declared: isFailure(manifest) ? undefined : manifest.data.manifest.packageManager,
|
|
144
|
+
findLockFile: lockFileAtOrAbove(root),
|
|
145
|
+
});
|
|
146
|
+
return manager.name === "bun" ? "bun" : "node";
|
|
147
|
+
}
|
|
148
|
+
/** Every numbered SQL file of the chain at `folder`; a chain with no folder yet is empty. */
|
|
149
|
+
export async function readChain(root, folder) {
|
|
150
|
+
let entries = await wrap(() => readdir(join(root, folder)));
|
|
151
|
+
if (isFailure(entries)) {
|
|
152
|
+
let code = entries.error.code;
|
|
153
|
+
return code === "ENOENT" ? success([]) : entries;
|
|
154
|
+
}
|
|
155
|
+
return wrap(() => Promise.all(entries.data
|
|
156
|
+
.filter((file) => MIGRATION_FILE.test(file))
|
|
157
|
+
.map(async (file) => ({
|
|
158
|
+
name: file,
|
|
159
|
+
sql: await readFile(join(root, folder, file), "utf8"),
|
|
160
|
+
}))));
|
|
161
|
+
}
|
package/dist/make/generate.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
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
|
-
*
|
|
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
|
+
* Nothing is written while planning, and a file that already exists stops the plan unless the
|
|
6
|
+
* request replaces it.
|
|
5
7
|
*
|
|
6
8
|
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
9
|
* @copyright Sergio Xalambrí 2026
|
|
@@ -14,14 +16,25 @@ import type { Name } from "./names.js";
|
|
|
14
16
|
export interface MakeRequest {
|
|
15
17
|
kind: KindName;
|
|
16
18
|
name: Name;
|
|
17
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* The stub variant a flag selects: `form`, `cron`, `resource` (an MCP resource, or a
|
|
21
|
+
* controller guarded by a resource's abilities) or `resource.form`.
|
|
22
|
+
*/
|
|
18
23
|
variant: string | null;
|
|
19
24
|
/** Whether to write a test beside the file. */
|
|
20
25
|
tests: boolean;
|
|
26
|
+
/** Whether to write a pairing `.spec` under `spec/` for kinds that support one. */
|
|
27
|
+
specs: boolean;
|
|
21
28
|
/** The migration chain, for `make migration`. */
|
|
22
29
|
chain?: string;
|
|
23
30
|
/** The route contract a controller is declared in, a module in `routes/`. */
|
|
24
31
|
routes: string;
|
|
32
|
+
/** The route pattern a controller answers, for the pairing `.spec`'s URL. */
|
|
33
|
+
url?: string;
|
|
34
|
+
/** Replace a file the plan would write that already exists, instead of stopping. */
|
|
35
|
+
replace?: boolean;
|
|
36
|
+
/** Plan the test alone, for a file that exists already. */
|
|
37
|
+
testAlone?: boolean;
|
|
25
38
|
}
|
|
26
39
|
/** A file the run writes, by path relative to the project root. */
|
|
27
40
|
export interface PlannedFile {
|
|
@@ -34,13 +47,20 @@ export interface Plan {
|
|
|
34
47
|
values: Record<string, string>;
|
|
35
48
|
}
|
|
36
49
|
/**
|
|
37
|
-
* Plans `request` in the project at `root`. Every planned path must be free
|
|
38
|
-
* is a failure naming it, before any stub is read.
|
|
50
|
+
* Plans `request` in the project at `root`. Every planned path must be free unless the request
|
|
51
|
+
* replaces files; an existing file is otherwise a failure naming it, before any stub is read.
|
|
39
52
|
*
|
|
40
53
|
* @param packageRoot - The `@sdxc/kit` package, whose `stubs/` holds the built-in stubs.
|
|
41
54
|
* @param overrides - Placeholder values a declaration decided, such as a job's reused group.
|
|
42
55
|
*/
|
|
43
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;
|
|
62
|
+
/** Whether `path`, relative to `root`, exists. */
|
|
63
|
+
export declare function exists(root: string, path: string): Promise<boolean>;
|
|
44
64
|
/** The folder a kind's files go in; a migration's comes from its chain instead. */
|
|
45
65
|
export declare function folderOf(kind: KindName, variant: string | null): string;
|
|
46
66
|
/**
|
package/dist/make/generate.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
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
|
-
*
|
|
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
|
+
* Nothing is written while planning, and a file that already exists stops the plan unless the
|
|
6
|
+
* request replaces it.
|
|
5
7
|
*
|
|
6
8
|
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
9
|
* @copyright Sergio Xalambrí 2026
|
|
@@ -10,13 +12,14 @@ import { readFile, stat } from "node:fs/promises";
|
|
|
10
12
|
import { dirname, join } from "node:path";
|
|
11
13
|
import { failure, isFailure, isSuccess, success, wrap } from "@sdxc/result";
|
|
12
14
|
import { headerTags, resolveAuthor } from "../author.js";
|
|
15
|
+
import { pluralize } from "../scaffold/fields.js";
|
|
13
16
|
import { COMMON_PLACEHOLDERS, KINDS, MCP_RESOURCES_FOLDER } from "./kinds.js";
|
|
14
17
|
import { chainFolder, nextMigrationFile } from "./migrations.js";
|
|
15
18
|
import { camel } from "./names.js";
|
|
16
19
|
import { loadStub, renderStub, stubName } from "./stubs.js";
|
|
17
20
|
/**
|
|
18
|
-
* Plans `request` in the project at `root`. Every planned path must be free
|
|
19
|
-
* is a failure naming it, before any stub is read.
|
|
21
|
+
* Plans `request` in the project at `root`. Every planned path must be free unless the request
|
|
22
|
+
* replaces files; an existing file is otherwise a failure naming it, before any stub is read.
|
|
20
23
|
*
|
|
21
24
|
* @param packageRoot - The `@sdxc/kit` package, whose `stubs/` holds the built-in stubs.
|
|
22
25
|
* @param overrides - Placeholder values a declaration decided, such as a job's reused group.
|
|
@@ -41,44 +44,86 @@ export async function plan(request, root, config, packageRoot, overrides = {}) {
|
|
|
41
44
|
}
|
|
42
45
|
let path = `${folder}/${file}${kind.extension}`;
|
|
43
46
|
let testPath = `${folder}/${file}.test${kind.extension}`;
|
|
44
|
-
let
|
|
45
|
-
let
|
|
47
|
+
let specPath = specPathOf(request);
|
|
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";
|
|
53
|
+
let writesMain = kind.testOnly !== true && request.testAlone !== true;
|
|
54
|
+
let paths = [
|
|
55
|
+
...(writesMain ? [path] : []),
|
|
56
|
+
...(writesTest ? [testPath] : []),
|
|
57
|
+
...(writesSpec ? [specPath] : []),
|
|
58
|
+
];
|
|
46
59
|
for (let candidate of paths) {
|
|
47
|
-
if (
|
|
48
|
-
return failure(new Error(`${candidate} already exists; sdxc make leaves it as it is`));
|
|
60
|
+
if (request.replace !== true && (await exists(root, candidate))) {
|
|
61
|
+
return failure(new Error(`${candidate} already exists; sdxc make leaves it as it is unless --force`));
|
|
49
62
|
}
|
|
50
63
|
}
|
|
51
64
|
let values = await placeholders(request, root, `${folder}/${file}`);
|
|
52
65
|
Object.assign(values, overrides);
|
|
53
66
|
let files = [];
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
67
|
+
if (writesMain) {
|
|
68
|
+
let main = await renderFile(request, values, "main", root, config, packageRoot);
|
|
69
|
+
if (isFailure(main))
|
|
70
|
+
return main;
|
|
71
|
+
files.push({ path, content: main.data });
|
|
72
|
+
}
|
|
58
73
|
if (writesTest) {
|
|
59
|
-
let test = await renderFile(request, values,
|
|
74
|
+
let test = await renderFile(request, values, "test", root, config, packageRoot);
|
|
60
75
|
if (isFailure(test))
|
|
61
76
|
return test;
|
|
62
77
|
files.push({ path: testPath, content: test.data });
|
|
63
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
|
+
}
|
|
64
85
|
return success({ files, values });
|
|
65
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
|
+
}
|
|
99
|
+
/** Whether `path`, relative to `root`, exists. */
|
|
100
|
+
export async function exists(root, path) {
|
|
101
|
+
return isSuccess(await wrap(() => stat(join(root, path))));
|
|
102
|
+
}
|
|
66
103
|
/** The folder a kind's files go in; a migration's comes from its chain instead. */
|
|
67
104
|
export function folderOf(kind, variant) {
|
|
68
105
|
return kind === "mcp" && variant === "resource" ? MCP_RESOURCES_FOLDER : KINDS[kind].folder;
|
|
69
106
|
}
|
|
70
107
|
/**
|
|
71
|
-
* Renders the kind's stub, or its
|
|
72
|
-
* variant without its own
|
|
73
|
-
* contract's stub when one exists (`controller.api.stub`
|
|
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.
|
|
74
113
|
*/
|
|
75
|
-
async function renderFile(request, values,
|
|
76
|
-
let
|
|
77
|
-
|
|
78
|
-
|
|
114
|
+
async function renderFile(request, values, role, root, config, packageRoot) {
|
|
115
|
+
let companion = role !== "main";
|
|
116
|
+
let names = [stubName(request.kind, request.variant, role)];
|
|
117
|
+
let contractual = request.variant === null || request.variant === "resource";
|
|
118
|
+
if (request.kind === "controller" && contractual) {
|
|
119
|
+
let variant = [request.routes, request.variant].filter((part) => part !== null).join(".");
|
|
120
|
+
names.unshift(stubName(request.kind, variant, role));
|
|
121
|
+
}
|
|
122
|
+
if (request.kind === "controller" && request.variant?.startsWith("resource") && companion) {
|
|
123
|
+
names.push(stubName(request.kind, request.routes, role));
|
|
79
124
|
}
|
|
80
|
-
if (request.variant !== null &&
|
|
81
|
-
names.push(stubName(request.kind, null,
|
|
125
|
+
if (request.variant !== null && companion)
|
|
126
|
+
names.push(stubName(request.kind, null, role));
|
|
82
127
|
let stub = await loadStub(names, root, config.make.stubs, packageRoot);
|
|
83
128
|
if (isFailure(stub))
|
|
84
129
|
return stub;
|
|
@@ -109,6 +154,15 @@ async function placeholders(request, root, file) {
|
|
|
109
154
|
command: name.segments.join(" "),
|
|
110
155
|
member: memberAccess(name.segments),
|
|
111
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("/")}`,
|
|
159
|
+
table: pluralize(name.snake),
|
|
160
|
+
attributes: "",
|
|
161
|
+
ability: "",
|
|
162
|
+
access: "",
|
|
163
|
+
pageAccess: "",
|
|
164
|
+
params: "",
|
|
165
|
+
sampleParams: "",
|
|
112
166
|
};
|
|
113
167
|
let values = {};
|
|
114
168
|
for (let key of [...COMMON_PLACEHOLDERS, ...KINDS[request.kind].placeholders]) {
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The access check `make controller --resource` writes into a controller: which of the
|
|
3
|
+
* resource's abilities its method implies, the route pattern that check needs, and the guard
|
|
4
|
+
* statements a resource stub places at the top of each handler.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { Result } from "@sdxc/result";
|
|
10
|
+
import type { Name } from "./names.js";
|
|
11
|
+
/** One of the abilities `make policy` declares for a resource. */
|
|
12
|
+
export type ResourceAction = "create" | "read" | "update" | "delete";
|
|
13
|
+
/** What a guarded controller checks and the placeholder values its stub renders it with. */
|
|
14
|
+
export interface Guard {
|
|
15
|
+
/** The ability's dot-joined path in the catalog, such as `billing.invoice.update`. */
|
|
16
|
+
ability: string;
|
|
17
|
+
/** The resource's key path in the catalog, which `make policy` declares. */
|
|
18
|
+
catalogPath: string[];
|
|
19
|
+
/** The route pattern, which holds `:id` whenever the check needs one row. */
|
|
20
|
+
pattern: string | undefined;
|
|
21
|
+
/** The guard answering a refusal with an empty 404 or 403 response. */
|
|
22
|
+
access: string;
|
|
23
|
+
/** The guard rendering the not-found page for a hidden or missing row. */
|
|
24
|
+
pageAccess: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Plans the guard for a controller named `controller` that `method` routes, checked against the
|
|
28
|
+
* abilities of `resource`. A read, update or delete checks one row, so its route takes
|
|
29
|
+
* `/<controller>/:id` by default and a `pattern` without `:id` is refused. With `model`, the row
|
|
30
|
+
* is the model's, and a missing one is a 404 like a hidden one; without it, the row is its id.
|
|
31
|
+
*
|
|
32
|
+
* @param checked - The ability to check instead of the one `method` implies, such as `update`
|
|
33
|
+
* for an edit form
|
|
34
|
+
*/
|
|
35
|
+
export declare function planGuard(controller: Name, resource: Name, method: string, pattern: string | undefined, model: boolean, checked?: ResourceAction): Result<Guard, Error>;
|