@sdxc/kit 2026.10.16 → 2026.10.20
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 +391 -20
- package/changelog.jsonl +1160 -1035
- package/config.schema.json +47 -1
- package/dist/author.d.ts +36 -0
- package/dist/author.js +107 -0
- package/dist/cli.js +12 -0
- package/dist/commands/create.js +9 -4
- 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 +72 -6
- package/dist/commands/make.js +127 -19
- 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 +18 -2
- package/dist/commands/run.js +35 -12
- package/dist/commands/scaffold.d.ts +29 -0
- package/dist/commands/scaffold.js +33 -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 +18 -0
- package/dist/config.js +28 -1
- package/dist/contract.d.ts +97 -3
- package/dist/contract.js +360 -26
- package/dist/create/templates.d.ts +8 -1
- package/dist/create/templates.js +26 -2
- package/dist/dependencies.d.ts +4 -1
- package/dist/dependencies.js +13 -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 +311 -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 +68 -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/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/init.d.ts +5 -3
- package/dist/init.js +15 -7
- 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 +32 -4
- package/dist/make/declare.js +405 -27
- 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 +16 -5
- package/dist/make/generate.js +43 -14
- 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 +47 -4
- package/dist/make/index.js +287 -25
- package/dist/make/kinds.d.ts +31 -3
- package/dist/make/kinds.js +82 -9
- 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 +7 -5
- package/dist/make/stubs.js +16 -5
- package/dist/make/test.d.ts +43 -0
- package/dist/make/test.js +149 -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 +37 -0
- package/dist/scaffold/index.js +613 -0
- package/dist/scaffold/render.d.ts +51 -0
- package/dist/scaffold/render.js +332 -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/testing.d.ts +12 -2
- package/dist/testing.js +22 -3
- 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/stubs/command.stub +4 -0
- package/stubs/command.test.stub +3 -0
- package/stubs/controller.api.resource.stub +23 -0
- package/stubs/controller.api.stub +21 -0
- package/stubs/controller.api.test.stub +18 -0
- package/stubs/controller.form.stub +40 -10
- package/stubs/controller.form.test.stub +61 -0
- package/stubs/controller.resource.form.stub +67 -0
- package/stubs/controller.resource.stub +28 -0
- package/stubs/controller.stub +15 -8
- package/stubs/controller.test.stub +3 -0
- package/stubs/email.stub +15 -38
- package/stubs/email.test.stub +12 -7
- package/stubs/factory.stub +4 -1
- package/stubs/initializer.stub +2 -0
- package/stubs/initializer.test.stub +3 -0
- package/stubs/job.cron.stub +4 -0
- package/stubs/job.stub +4 -0
- package/stubs/job.test.stub +3 -0
- package/stubs/mcp.resource.stub +3 -0
- package/stubs/mcp.stub +3 -0
- package/stubs/mcp.test.stub +3 -0
- package/stubs/middleware.stub +6 -1
- package/stubs/middleware.test.stub +3 -0
- package/stubs/model.stub +12 -3
- package/stubs/model.test.stub +3 -0
- package/stubs/policy.test.stub +36 -7
- 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 +2 -0
- package/stubs/scaffold.model.stub +35 -0
- package/stubs/scaffold.model.test.stub +25 -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/stubs/service.stub +4 -0
- package/stubs/service.test.stub +3 -0
- package/stubs/validator.stub +3 -0
- package/stubs/validator.test.stub +3 -0
- package/templates/app/.env.example +16 -0
- package/templates/app/AGENTS.md +163 -0
- package/templates/app/CLAUDE.md +1 -0
- package/templates/app/README.md +79 -37
- package/templates/app/app/commands/about.test.ts +1 -1
- 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/console.ts +6 -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 +46 -0
- package/templates/app/app/commands/db/migrate.ts +88 -0
- 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 +72 -1
- package/templates/app/app/commands/typegen.test.ts +17 -0
- package/templates/app/app/commands/typegen.ts +16 -0
- package/templates/app/app/emails/index.ts +10 -0
- package/templates/app/app/http/controllers/api/health.tsx +15 -0
- package/templates/app/app/http/controllers/api/not-found.tsx +16 -0
- package/templates/app/app/http/controllers/api/openapi.tsx +17 -0
- package/templates/app/app/http/controllers/home.tsx +21 -13
- package/templates/app/app/http/controllers/mcp.tsx +16 -0
- package/templates/app/app/http/controllers/not-found.tsx +23 -18
- package/templates/app/app/http/controllers/robots.test.ts +18 -0
- package/templates/app/app/http/controllers/robots.ts +24 -0
- package/templates/app/app/http/controllers/sitemap.test.ts +28 -0
- package/templates/app/app/http/controllers/sitemap.ts +25 -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 +19 -0
- package/templates/app/app/http/middleware/page.test.ts +43 -0
- package/templates/app/app/http/middleware/page.ts +51 -0
- package/templates/app/app/http/middleware/server-timing.test.ts +15 -0
- package/templates/app/app/http/middleware/server-timing.ts +42 -0
- package/templates/app/app/initializers/assets.ts +1 -1
- package/templates/app/app/initializers/captcha.ts +11 -0
- package/templates/app/app/initializers/cli.ts +68 -2
- package/templates/app/app/initializers/database.test.ts +18 -0
- package/templates/app/app/initializers/database.ts +18 -0
- package/templates/app/app/initializers/dispatcher.ts +26 -0
- package/templates/app/app/initializers/honeypot.ts +11 -0
- package/templates/app/app/initializers/i18n.test.ts +16 -0
- package/templates/app/app/initializers/i18n.ts +28 -0
- package/templates/app/app/initializers/local-database.test.ts +33 -0
- package/templates/app/app/initializers/local-database.ts +53 -0
- package/templates/app/app/initializers/mail-transport.test.ts +27 -0
- package/templates/app/app/initializers/mail-transport.ts +26 -0
- package/templates/app/app/initializers/mail.test.ts +27 -0
- package/templates/app/app/initializers/mail.ts +19 -0
- package/templates/app/app/initializers/mcp.ts +1 -1
- package/templates/app/app/initializers/openapi.ts +21 -0
- package/templates/app/app/initializers/router.test.ts +68 -0
- package/templates/app/app/initializers/router.tsx +81 -25
- package/templates/app/app/initializers/seo.ts +15 -0
- package/templates/app/app/initializers/session.test.ts +34 -0
- package/templates/app/app/initializers/session.ts +29 -0
- package/templates/app/app/initializers/spam.ts +10 -0
- 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/models/index.ts +10 -0
- 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 +92 -0
- package/templates/app/app/services/migrations.ts +117 -0
- package/templates/app/app/test/cloudflare-workers.ts +25 -0
- package/templates/app/app/test/local-database.ts +67 -0
- package/templates/app/app/test/session.ts +28 -0
- package/templates/app/bootstrap/cli.ts +1 -1
- package/templates/app/bootstrap/console.ts +13 -0
- package/templates/app/bootstrap/worker.ts +4 -4
- package/templates/app/cloudflare.config.ts +30 -2
- package/templates/app/config/constants.ts +19 -0
- package/templates/app/config/problems.ts +24 -0
- package/templates/app/config/security-policy.ts +34 -0
- package/templates/app/config/security-txt.ts +28 -0
- package/templates/app/database/migrations/.gitkeep +0 -0
- package/templates/app/database/seed.ts +12 -0
- package/templates/app/package.json +37 -6
- package/templates/app/resources/layouts/document.tsx +28 -7
- package/templates/app/resources/locales/en.json +10 -0
- package/templates/app/routes/api.ts +29 -0
- package/templates/app/routes/web.ts +6 -4
- package/templates/app/vite.config.ts +4 -0
- package/stubs/policy.stub +0 -13
- package/templates/app/config/router-context.d.ts +0 -20
|
@@ -0,0 +1,30 @@
|
|
|
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 type { Result } from "@sdxc/result";
|
|
10
|
+
import type { Name } from "./names.js";
|
|
11
|
+
/** What a migration's name says it does, and to which table. */
|
|
12
|
+
export interface MigrationIntent {
|
|
13
|
+
action: "create" | "add" | "remove";
|
|
14
|
+
/** The table's snake_case name: the words after `create`, `to` or `from`. */
|
|
15
|
+
table: string;
|
|
16
|
+
}
|
|
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 declare function migrationIntent(name: Name): MigrationIntent | null;
|
|
23
|
+
/**
|
|
24
|
+
* The content of the migration `name` that `specs` describe, applied to the replayed chain at
|
|
25
|
+
* `folder` before it is returned, so a table that already exists, a missing table or a column
|
|
26
|
+
* SQLite cannot drop fails here. A chain that does not replay is left unchecked.
|
|
27
|
+
*
|
|
28
|
+
* @param root - The project root, whose `app/models/` resolves each reference's table.
|
|
29
|
+
*/
|
|
30
|
+
export declare function fieldMigration(root: string, folder: string, name: Name, specs: readonly string[]): Promise<Result<string, Error>>;
|
|
@@ -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 and, when tests are on, its test beside it, each rendered from its stub
|
|
4
|
-
*
|
|
3
|
+
* layout gives it and, when tests are on, its test beside it, each rendered from its stub; a
|
|
4
|
+
* 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,7 +16,10 @@ 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;
|
|
@@ -22,6 +27,10 @@ export interface MakeRequest {
|
|
|
22
27
|
chain?: string;
|
|
23
28
|
/** The route contract a controller is declared in, a module in `routes/`. */
|
|
24
29
|
routes: string;
|
|
30
|
+
/** Replace a file the plan would write that already exists, instead of stopping. */
|
|
31
|
+
replace?: boolean;
|
|
32
|
+
/** Plan the test alone, for a file that exists already. */
|
|
33
|
+
testAlone?: boolean;
|
|
25
34
|
}
|
|
26
35
|
/** A file the run writes, by path relative to the project root. */
|
|
27
36
|
export interface PlannedFile {
|
|
@@ -34,13 +43,15 @@ export interface Plan {
|
|
|
34
43
|
values: Record<string, string>;
|
|
35
44
|
}
|
|
36
45
|
/**
|
|
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.
|
|
46
|
+
* Plans `request` in the project at `root`. Every planned path must be free unless the request
|
|
47
|
+
* replaces files; an existing file is otherwise a failure naming it, before any stub is read.
|
|
39
48
|
*
|
|
40
49
|
* @param packageRoot - The `@sdxc/kit` package, whose `stubs/` holds the built-in stubs.
|
|
41
50
|
* @param overrides - Placeholder values a declaration decided, such as a job's reused group.
|
|
42
51
|
*/
|
|
43
52
|
export declare function plan(request: MakeRequest, root: string, config: Config, packageRoot: string, overrides?: Readonly<Record<string, string>>): Promise<Result<Plan, Error>>;
|
|
53
|
+
/** Whether `path`, relative to `root`, exists. */
|
|
54
|
+
export declare function exists(root: string, path: string): Promise<boolean>;
|
|
44
55
|
/** The folder a kind's files go in; a migration's comes from its chain instead. */
|
|
45
56
|
export declare function folderOf(kind: KindName, variant: string | null): string;
|
|
46
57
|
/**
|
package/dist/make/generate.js
CHANGED
|
@@ -1,21 +1,25 @@
|
|
|
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 and, when tests are on, its test beside it, each rendered from its stub
|
|
4
|
-
*
|
|
3
|
+
* layout gives it and, when tests are on, its test beside it, each rendered from its stub; a
|
|
4
|
+
* 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
|
|
8
10
|
*/
|
|
9
11
|
import { readFile, stat } from "node:fs/promises";
|
|
10
|
-
import { join } from "node:path";
|
|
12
|
+
import { dirname, join } from "node:path";
|
|
11
13
|
import { failure, isFailure, isSuccess, success, wrap } from "@sdxc/result";
|
|
14
|
+
import { headerTags, resolveAuthor } from "../author.js";
|
|
15
|
+
import { pluralize } from "../scaffold/fields.js";
|
|
12
16
|
import { COMMON_PLACEHOLDERS, KINDS, MCP_RESOURCES_FOLDER } from "./kinds.js";
|
|
13
17
|
import { chainFolder, nextMigrationFile } from "./migrations.js";
|
|
14
18
|
import { camel } from "./names.js";
|
|
15
19
|
import { loadStub, renderStub, stubName } from "./stubs.js";
|
|
16
20
|
/**
|
|
17
|
-
* Plans `request` in the project at `root`. Every planned path must be free
|
|
18
|
-
* 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.
|
|
19
23
|
*
|
|
20
24
|
* @param packageRoot - The `@sdxc/kit` package, whose `stubs/` holds the built-in stubs.
|
|
21
25
|
* @param overrides - Placeholder values a declaration decided, such as a job's reused group.
|
|
@@ -40,20 +44,23 @@ export async function plan(request, root, config, packageRoot, overrides = {}) {
|
|
|
40
44
|
}
|
|
41
45
|
let path = `${folder}/${file}${kind.extension}`;
|
|
42
46
|
let testPath = `${folder}/${file}.test${kind.extension}`;
|
|
43
|
-
let writesTest = kind.tests && request.tests;
|
|
44
|
-
let
|
|
47
|
+
let writesTest = kind.tests && (request.tests || request.testAlone === true);
|
|
48
|
+
let writesMain = kind.testOnly !== true && request.testAlone !== true;
|
|
49
|
+
let paths = [...(writesMain ? [path] : []), ...(writesTest ? [testPath] : [])];
|
|
45
50
|
for (let candidate of paths) {
|
|
46
|
-
if (
|
|
47
|
-
return failure(new Error(`${candidate} already exists; sdxc make leaves it as it is`));
|
|
51
|
+
if (request.replace !== true && (await exists(root, candidate))) {
|
|
52
|
+
return failure(new Error(`${candidate} already exists; sdxc make leaves it as it is unless --force`));
|
|
48
53
|
}
|
|
49
54
|
}
|
|
50
55
|
let values = await placeholders(request, root, `${folder}/${file}`);
|
|
51
56
|
Object.assign(values, overrides);
|
|
52
57
|
let files = [];
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
58
|
+
if (writesMain) {
|
|
59
|
+
let main = await renderFile(request, values, false, root, config, packageRoot);
|
|
60
|
+
if (isFailure(main))
|
|
61
|
+
return main;
|
|
62
|
+
files.push({ path, content: main.data });
|
|
63
|
+
}
|
|
57
64
|
if (writesTest) {
|
|
58
65
|
let test = await renderFile(request, values, true, root, config, packageRoot);
|
|
59
66
|
if (isFailure(test))
|
|
@@ -62,16 +69,30 @@ export async function plan(request, root, config, packageRoot, overrides = {}) {
|
|
|
62
69
|
}
|
|
63
70
|
return success({ files, values });
|
|
64
71
|
}
|
|
72
|
+
/** Whether `path`, relative to `root`, exists. */
|
|
73
|
+
export async function exists(root, path) {
|
|
74
|
+
return isSuccess(await wrap(() => stat(join(root, path))));
|
|
75
|
+
}
|
|
65
76
|
/** The folder a kind's files go in; a migration's comes from its chain instead. */
|
|
66
77
|
export function folderOf(kind, variant) {
|
|
67
78
|
return kind === "mcp" && variant === "resource" ? MCP_RESOURCES_FOLDER : KINDS[kind].folder;
|
|
68
79
|
}
|
|
69
80
|
/**
|
|
70
81
|
* Renders the kind's stub, or its test stub, with `values`. A variant's own stub wins; a
|
|
71
|
-
* variant without its own test stub uses the kind's.
|
|
82
|
+
* variant without its own test stub uses the kind's. A controller with no variant takes its
|
|
83
|
+
* contract's stub when one exists (`controller.api.stub` for `routes/api`), else the kind's, and
|
|
84
|
+
* a guarded one its contract's `resource` stub (`controller.api.resource.stub`) the same way.
|
|
72
85
|
*/
|
|
73
86
|
async function renderFile(request, values, test, root, config, packageRoot) {
|
|
74
87
|
let names = [stubName(request.kind, request.variant, test)];
|
|
88
|
+
let contractual = request.variant === null || request.variant === "resource";
|
|
89
|
+
if (request.kind === "controller" && contractual) {
|
|
90
|
+
let variant = [request.routes, request.variant].filter((part) => part !== null).join(".");
|
|
91
|
+
names.unshift(stubName(request.kind, variant, test));
|
|
92
|
+
}
|
|
93
|
+
if (request.kind === "controller" && request.variant?.startsWith("resource") && test) {
|
|
94
|
+
names.push(stubName(request.kind, request.routes, true));
|
|
95
|
+
}
|
|
75
96
|
if (request.variant !== null && test)
|
|
76
97
|
names.push(stubName(request.kind, null, true));
|
|
77
98
|
let stub = await loadStub(names, root, config.make.stubs, packageRoot);
|
|
@@ -92,6 +113,7 @@ async function placeholders(request, root, file) {
|
|
|
92
113
|
snake: name.snake,
|
|
93
114
|
title: name.title,
|
|
94
115
|
specifier: `~/${file}`,
|
|
116
|
+
header: headerTags(await resolveAuthor(dirname(join(root, file)))),
|
|
95
117
|
};
|
|
96
118
|
let declarationPath = name.segments.map(camel).join(".");
|
|
97
119
|
let extra = {
|
|
@@ -103,6 +125,13 @@ async function placeholders(request, root, file) {
|
|
|
103
125
|
command: name.segments.join(" "),
|
|
104
126
|
member: memberAccess(name.segments),
|
|
105
127
|
words: name.segments.map((segment) => literal(segment)).join(", "),
|
|
128
|
+
table: pluralize(name.snake),
|
|
129
|
+
attributes: "",
|
|
130
|
+
ability: "",
|
|
131
|
+
access: "",
|
|
132
|
+
pageAccess: "",
|
|
133
|
+
params: "",
|
|
134
|
+
sampleParams: "",
|
|
106
135
|
};
|
|
107
136
|
let values = {};
|
|
108
137
|
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>;
|