@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,421 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The fields `sdxc scaffold` and `sdxc make model` take as `name`, `name:type` or `name?`, with
|
|
3
|
+
* an `:index` or `:unique` modifier, the type a bare name takes by convention, and what each type becomes in the layers that read it as
|
|
4
|
+
* data: the table column, the form and tool schemas, and the samples a test fills it with.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { readFile } from "node:fs/promises";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
import { failure, isFailure, success, wrap } from "@sdxc/result";
|
|
12
|
+
import { column, table } from "remix/data-table";
|
|
13
|
+
import { quote } from "../make/schema.js";
|
|
14
|
+
/** Every modifier, in the order the help lists them. */
|
|
15
|
+
export const FIELD_MODIFIERS = ["index", "unique"];
|
|
16
|
+
/** What a string field's form value must be: something besides spaces. */
|
|
17
|
+
const NOT_BLANK = { expression: 'value.trim() !== ""', message: "Required" };
|
|
18
|
+
/** Every field type `sdxc scaffold` writes pages for, in the order the help lists them. */
|
|
19
|
+
export const FIELD_TYPES = {
|
|
20
|
+
string: {
|
|
21
|
+
column: "c.text()",
|
|
22
|
+
build: (c) => c.text(),
|
|
23
|
+
test: NOT_BLANK,
|
|
24
|
+
convert: null,
|
|
25
|
+
json: "s.string()",
|
|
26
|
+
flag: "string",
|
|
27
|
+
formSample: "Sample text",
|
|
28
|
+
jsonSample: '"Sample text"',
|
|
29
|
+
flagSample: "Sample text",
|
|
30
|
+
changedSample: '"Changed text"',
|
|
31
|
+
invalidSample: " ",
|
|
32
|
+
},
|
|
33
|
+
text: {
|
|
34
|
+
column: "c.text()",
|
|
35
|
+
build: (c) => c.text(),
|
|
36
|
+
test: NOT_BLANK,
|
|
37
|
+
convert: null,
|
|
38
|
+
json: "s.string()",
|
|
39
|
+
flag: "string",
|
|
40
|
+
formSample: "Sample text",
|
|
41
|
+
jsonSample: '"Sample text"',
|
|
42
|
+
flagSample: "Sample text",
|
|
43
|
+
changedSample: '"Changed text"',
|
|
44
|
+
invalidSample: " ",
|
|
45
|
+
},
|
|
46
|
+
integer: {
|
|
47
|
+
column: "c.integer()",
|
|
48
|
+
build: (c) => c.integer(),
|
|
49
|
+
test: { expression: "/^-?\\d+$/.test(value.trim())", message: "Expected a whole number" },
|
|
50
|
+
convert: "Number(value)",
|
|
51
|
+
json: "s.integer()",
|
|
52
|
+
flag: "number",
|
|
53
|
+
formSample: "42",
|
|
54
|
+
jsonSample: "42",
|
|
55
|
+
flagSample: "42",
|
|
56
|
+
changedSample: "7",
|
|
57
|
+
invalidSample: "1.5",
|
|
58
|
+
},
|
|
59
|
+
boolean: {
|
|
60
|
+
column: "c.boolean()",
|
|
61
|
+
build: (c) => c.boolean(),
|
|
62
|
+
test: null,
|
|
63
|
+
convert: null,
|
|
64
|
+
json: "s.boolean()",
|
|
65
|
+
flag: "boolean",
|
|
66
|
+
formSample: "true",
|
|
67
|
+
jsonSample: "true",
|
|
68
|
+
flagSample: "true",
|
|
69
|
+
changedSample: "true",
|
|
70
|
+
invalidSample: "maybe",
|
|
71
|
+
},
|
|
72
|
+
date: {
|
|
73
|
+
column: "c.text()",
|
|
74
|
+
build: (c) => c.text(),
|
|
75
|
+
test: { expression: "/^\\d{4}-\\d{2}-\\d{2}$/.test(value)", message: "Expected a date" },
|
|
76
|
+
convert: null,
|
|
77
|
+
json: "s.string()",
|
|
78
|
+
flag: "string",
|
|
79
|
+
formSample: "2026-01-31",
|
|
80
|
+
jsonSample: '"2026-01-31"',
|
|
81
|
+
flagSample: "2026-01-31",
|
|
82
|
+
changedSample: '"2026-02-28"',
|
|
83
|
+
invalidSample: "soon",
|
|
84
|
+
},
|
|
85
|
+
timestamp: {
|
|
86
|
+
column: "c.integer()",
|
|
87
|
+
build: (c) => c.integer(),
|
|
88
|
+
test: {
|
|
89
|
+
expression: "/^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}(:\\d{2})?$/.test(value) && !Number.isNaN(Date.parse(`${value}Z`))",
|
|
90
|
+
message: "Expected a date and time",
|
|
91
|
+
},
|
|
92
|
+
convert: "Date.parse(`${value}Z`)",
|
|
93
|
+
json: "s.integer()",
|
|
94
|
+
flag: "number",
|
|
95
|
+
formSample: "2026-01-31T09:30",
|
|
96
|
+
jsonSample: "1769851800000",
|
|
97
|
+
flagSample: "1769851800000",
|
|
98
|
+
changedSample: "1772271000000",
|
|
99
|
+
invalidSample: "soon",
|
|
100
|
+
},
|
|
101
|
+
email: {
|
|
102
|
+
column: "c.text()",
|
|
103
|
+
build: (c) => c.text(),
|
|
104
|
+
test: { expression: "/^[^\\s@]+@[^\\s@]+$/.test(value)", message: "Expected an email address" },
|
|
105
|
+
convert: null,
|
|
106
|
+
json: "s.string()",
|
|
107
|
+
flag: "string",
|
|
108
|
+
formSample: "ada@example.com",
|
|
109
|
+
jsonSample: '"ada@example.com"',
|
|
110
|
+
flagSample: "ada@example.com",
|
|
111
|
+
changedSample: '"grace@example.com"',
|
|
112
|
+
invalidSample: "ada",
|
|
113
|
+
},
|
|
114
|
+
url: {
|
|
115
|
+
column: "c.text()",
|
|
116
|
+
build: (c) => c.text(),
|
|
117
|
+
test: { expression: "URL.canParse(value)", message: "Expected a URL" },
|
|
118
|
+
convert: null,
|
|
119
|
+
json: "s.string()",
|
|
120
|
+
flag: "string",
|
|
121
|
+
formSample: "https://example.com/",
|
|
122
|
+
jsonSample: '"https://example.com/"',
|
|
123
|
+
flagSample: "https://example.com/",
|
|
124
|
+
changedSample: '"https://example.org/"',
|
|
125
|
+
invalidSample: "example",
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
/** Every type a field may name, the reference included, in the order the help lists them. */
|
|
129
|
+
export const FIELD_TYPE_NAMES = [
|
|
130
|
+
"string",
|
|
131
|
+
"text",
|
|
132
|
+
"integer",
|
|
133
|
+
"boolean",
|
|
134
|
+
"date",
|
|
135
|
+
"timestamp",
|
|
136
|
+
"email",
|
|
137
|
+
"url",
|
|
138
|
+
"references",
|
|
139
|
+
];
|
|
140
|
+
/** The columns every table has, which a field cannot name. */
|
|
141
|
+
const RESERVED_COLUMNS = new Set(["id", "created_at", "updated_at"]);
|
|
142
|
+
/** Whole names that read as a box to tick: `published`, `archived`… */
|
|
143
|
+
const BOOLEAN_NAMES = new Set([
|
|
144
|
+
"active",
|
|
145
|
+
"admin",
|
|
146
|
+
"approved",
|
|
147
|
+
"archived",
|
|
148
|
+
"completed",
|
|
149
|
+
"confirmed",
|
|
150
|
+
"default",
|
|
151
|
+
"deleted",
|
|
152
|
+
"disabled",
|
|
153
|
+
"done",
|
|
154
|
+
"draft",
|
|
155
|
+
"enabled",
|
|
156
|
+
"featured",
|
|
157
|
+
"hidden",
|
|
158
|
+
"locked",
|
|
159
|
+
"pinned",
|
|
160
|
+
"private",
|
|
161
|
+
"public",
|
|
162
|
+
"published",
|
|
163
|
+
"read",
|
|
164
|
+
"verified",
|
|
165
|
+
"visible",
|
|
166
|
+
]);
|
|
167
|
+
/** The first words that ask a yes-or-no question: `is_public`, `has_paid`, `can_comment`. */
|
|
168
|
+
const BOOLEAN_PREFIXES = new Set(["is", "has", "can", "should", "was", "allow", "allows"]);
|
|
169
|
+
/** Whole names that count something. */
|
|
170
|
+
const INTEGER_NAMES = new Set([
|
|
171
|
+
"age",
|
|
172
|
+
"count",
|
|
173
|
+
"level",
|
|
174
|
+
"position",
|
|
175
|
+
"priority",
|
|
176
|
+
"quantity",
|
|
177
|
+
"rank",
|
|
178
|
+
"rating",
|
|
179
|
+
"score",
|
|
180
|
+
"year",
|
|
181
|
+
]);
|
|
182
|
+
/** The last words of a name that counts something: `view_count`, `price_cents`. */
|
|
183
|
+
const INTEGER_SUFFIXES = new Set(["count", "cents"]);
|
|
184
|
+
/** Whole names, and last words, that hold more than a line of text. */
|
|
185
|
+
const TEXT_NAMES = new Set([
|
|
186
|
+
"bio",
|
|
187
|
+
"body",
|
|
188
|
+
"comment",
|
|
189
|
+
"content",
|
|
190
|
+
"description",
|
|
191
|
+
"details",
|
|
192
|
+
"excerpt",
|
|
193
|
+
"message",
|
|
194
|
+
"notes",
|
|
195
|
+
"summary",
|
|
196
|
+
]);
|
|
197
|
+
/** Whole names that hold a web address beside the `url` suffix. */
|
|
198
|
+
const URL_NAMES = new Set(["homepage", "website"]);
|
|
199
|
+
/** Whole names that hold a calendar day beside the `on` and `date` suffixes. */
|
|
200
|
+
const DATE_NAMES = new Set(["birthday", "birthdate"]);
|
|
201
|
+
/**
|
|
202
|
+
* The type a bare name takes by convention, from its snake_case words. The rules are checked in
|
|
203
|
+
* this order, so `published_at` is a timestamp and `published` a box, and a name no rule
|
|
204
|
+
* matches is a `string`.
|
|
205
|
+
*/
|
|
206
|
+
export function conventionalType(words) {
|
|
207
|
+
let name = words.join("_");
|
|
208
|
+
let first = words[0] ?? "";
|
|
209
|
+
let last = words.at(-1) ?? "";
|
|
210
|
+
let compound = words.length > 1;
|
|
211
|
+
if (compound && last === "id")
|
|
212
|
+
return "references";
|
|
213
|
+
if (compound && last === "at")
|
|
214
|
+
return "timestamp";
|
|
215
|
+
if ((compound && (last === "on" || last === "date")) || DATE_NAMES.has(name))
|
|
216
|
+
return "date";
|
|
217
|
+
if ((compound && BOOLEAN_PREFIXES.has(first)) || BOOLEAN_NAMES.has(name))
|
|
218
|
+
return "boolean";
|
|
219
|
+
if ((compound && INTEGER_SUFFIXES.has(last)) || INTEGER_NAMES.has(name))
|
|
220
|
+
return "integer";
|
|
221
|
+
if (last === "email")
|
|
222
|
+
return "email";
|
|
223
|
+
if (last === "url" || URL_NAMES.has(name))
|
|
224
|
+
return "url";
|
|
225
|
+
if (TEXT_NAMES.has(last))
|
|
226
|
+
return "text";
|
|
227
|
+
return "string";
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Parses `specs` into fields. Each spec is a name with an optional `:type` and an optional
|
|
231
|
+
* `:index` or `:unique`, and a trailing `?` on any part makes the field nullable; a bare name takes {@link conventionalType}, and an
|
|
232
|
+
* explicit type always wins. `author:references` and a bare `author_id` both write `author_id`,
|
|
233
|
+
* pointing at the `authors` table until {@link resolveReferences} reads the model's own table.
|
|
234
|
+
* A reserved, repeated or invalid name, an unknown type, a nullable box and a password column are
|
|
235
|
+
* failures naming the spec.
|
|
236
|
+
*/
|
|
237
|
+
export function parseFields(specs) {
|
|
238
|
+
let fields = [];
|
|
239
|
+
for (let spec of specs) {
|
|
240
|
+
let [rawName = "", ...parts] = spec.split(":");
|
|
241
|
+
let nullable = [rawName, ...parts].some((part) => part.endsWith("?"));
|
|
242
|
+
let name = rawName.replace(/\?$/, "");
|
|
243
|
+
let explicit;
|
|
244
|
+
let index = null;
|
|
245
|
+
for (let part of parts.map((raw) => raw.replace(/\?$/, ""))) {
|
|
246
|
+
if (FIELD_MODIFIERS.includes(part) && index === null) {
|
|
247
|
+
index = part;
|
|
248
|
+
}
|
|
249
|
+
else if (explicit === undefined && !FIELD_MODIFIERS.includes(part)) {
|
|
250
|
+
explicit = part;
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
return failure(new Error(`"${spec}" is not a field; write name, name:type, name:index or name:type:unique`));
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
let words = name
|
|
257
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
258
|
+
.split(/[\s_-]+/)
|
|
259
|
+
.filter((word) => word !== "")
|
|
260
|
+
.map((word) => word.toLowerCase());
|
|
261
|
+
if (explicit === "references" && words.at(-1) !== "id")
|
|
262
|
+
words.push("id");
|
|
263
|
+
let column = words.join("_");
|
|
264
|
+
if (!/^[a-z][a-z0-9_]*$/.test(column)) {
|
|
265
|
+
return failure(new Error(`"${name}" is not a field name; start it with a letter`));
|
|
266
|
+
}
|
|
267
|
+
if (RESERVED_COLUMNS.has(column)) {
|
|
268
|
+
return failure(new Error(`${column} is a column of every table; name it otherwise`));
|
|
269
|
+
}
|
|
270
|
+
if (words.includes("password") && !["hash", "digest"].includes(words.at(-1) ?? "")) {
|
|
271
|
+
return failure(new Error(`${column} would keep a password as typed; store only its hash, in a column such as password_hash`));
|
|
272
|
+
}
|
|
273
|
+
if (fields.some((field) => field.column === column)) {
|
|
274
|
+
return failure(new Error(`${column} is listed twice`));
|
|
275
|
+
}
|
|
276
|
+
let type = explicit === undefined || explicit === "" ? conventionalType(words) : explicit;
|
|
277
|
+
if (!isFieldType(type)) {
|
|
278
|
+
return failure(new Error(`"${type}" in "${spec}" is not a field type; use ${FIELD_TYPE_NAMES.join(", ")}`));
|
|
279
|
+
}
|
|
280
|
+
if (type === "boolean" && nullable) {
|
|
281
|
+
return failure(new Error(`${column} is a box, ticked or not; drop the ? from "${spec}"`));
|
|
282
|
+
}
|
|
283
|
+
let [first = "", ...others] = words;
|
|
284
|
+
let field = {
|
|
285
|
+
column,
|
|
286
|
+
label: capitalize(words.join(" ")),
|
|
287
|
+
flag: words.join("-"),
|
|
288
|
+
key: first + others.map(capitalize).join(""),
|
|
289
|
+
type,
|
|
290
|
+
nullable,
|
|
291
|
+
index,
|
|
292
|
+
};
|
|
293
|
+
if (type === "references") {
|
|
294
|
+
let model = words.slice(0, -1);
|
|
295
|
+
if (model.length === 0) {
|
|
296
|
+
return failure(new Error(`"${spec}" names no model; write author:references`));
|
|
297
|
+
}
|
|
298
|
+
field.reference = {
|
|
299
|
+
model: model.join("-"),
|
|
300
|
+
table: pluralize(model.join("_")),
|
|
301
|
+
key: "text",
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
fields.push(field);
|
|
305
|
+
}
|
|
306
|
+
return success(fields);
|
|
307
|
+
}
|
|
308
|
+
/** Whether `value` names a field type. */
|
|
309
|
+
function isFieldType(value) {
|
|
310
|
+
return FIELD_TYPE_NAMES.includes(value);
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* `fields` with each reference pointing at the table its model in `app/models/` declares, and at
|
|
314
|
+
* that table's `id` type. A reference to a model the project lacks keeps the plural table name,
|
|
315
|
+
* as `sdxc scaffold` names tables, and a text id.
|
|
316
|
+
*/
|
|
317
|
+
export async function resolveReferences(root, fields) {
|
|
318
|
+
let resolved = [];
|
|
319
|
+
for (let field of fields) {
|
|
320
|
+
if (field.reference === undefined) {
|
|
321
|
+
resolved.push(field);
|
|
322
|
+
continue;
|
|
323
|
+
}
|
|
324
|
+
let source = await wrap(() => readFile(join(root, "app/models", `${field.reference?.model}.ts`), "utf8"));
|
|
325
|
+
if (isFailure(source)) {
|
|
326
|
+
resolved.push(field);
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
let table = /\btable\(\s*\{\s*name:\s*["']([^"']+)["']/.exec(source.data)?.[1];
|
|
330
|
+
let key = /\bid:\s*c(?:olumn)?\.(?:integer|bigint)\(/.test(source.data)
|
|
331
|
+
? "integer"
|
|
332
|
+
: "text";
|
|
333
|
+
resolved.push({
|
|
334
|
+
...field,
|
|
335
|
+
reference: { ...field.reference, table: table ?? field.reference.table, key },
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
return resolved;
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* The `remix/data-table` column source of `field` in the table `table`: its type's builder, a
|
|
342
|
+
* reference's foreign key named `<table>_<column>_fkey`, `.unique()` after `:unique`, and
|
|
343
|
+
* `.nullable()` after a `?`.
|
|
344
|
+
*/
|
|
345
|
+
export function columnSource(field, table) {
|
|
346
|
+
let source = field.reference === undefined
|
|
347
|
+
? FIELD_TYPES[field.type].column
|
|
348
|
+
: `c.${field.reference.key}().references(${JSON.stringify(field.reference.table)}, ${JSON.stringify(`${table}_${field.column}_fkey`)})`;
|
|
349
|
+
if (field.index === "unique")
|
|
350
|
+
source = `${source}.unique()`;
|
|
351
|
+
return field.nullable ? `${source}.nullable()` : source;
|
|
352
|
+
}
|
|
353
|
+
/** The column `columnSource` writes, built with `c`, for a table rendered from it. */
|
|
354
|
+
export function buildColumn(field, table, c) {
|
|
355
|
+
let builder = field.reference === undefined
|
|
356
|
+
? FIELD_TYPES[field.type].build(c)
|
|
357
|
+
: c[field.reference.key]().references(field.reference.table, `${table}_${field.column}_fkey`);
|
|
358
|
+
if (field.index === "unique")
|
|
359
|
+
builder = builder.unique();
|
|
360
|
+
return field.nullable ? builder.nullable() : builder;
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* One `CREATE INDEX` per `:index` field of `table`, named `<table>_<column>_index`. The model
|
|
364
|
+
* declares no plain index, so the migration is where it lives; a `:unique` field's index comes
|
|
365
|
+
* from its column instead.
|
|
366
|
+
*/
|
|
367
|
+
export function fieldIndexes(table, fields) {
|
|
368
|
+
return fields
|
|
369
|
+
.filter((field) => field.index === "index")
|
|
370
|
+
.map((field) => `CREATE INDEX ${quote(`${table}_${field.column}_index`)} ON ${quote(table)} (${quote(field.column)});`);
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* The table a model with `fields` declares under `name`, as the model stub writes it: a text `id`
|
|
374
|
+
* key, each field's column, then the `created_at` and `updated_at` its timestamps stamp. Its
|
|
375
|
+
* migration is rendered from this table, so the two always list the same columns.
|
|
376
|
+
*/
|
|
377
|
+
export function fieldsTable(name, fields) {
|
|
378
|
+
let columns = { id: column.text().primaryKey() };
|
|
379
|
+
for (let field of fields)
|
|
380
|
+
columns[field.column] = buildColumn(field, name, column);
|
|
381
|
+
columns.created_at = column.integer();
|
|
382
|
+
columns.updated_at = column.integer();
|
|
383
|
+
return table({ name, columns, timestamps: true });
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* A factory attribute's source for `field`, text numbered by the sequence so two rows a test
|
|
387
|
+
* creates differ. A nullable reference is `null`; a required one is `null` too, which the caller
|
|
388
|
+
* leaves out, since only a test knows which row it should point at.
|
|
389
|
+
*/
|
|
390
|
+
export function factorySource(field) {
|
|
391
|
+
if (field.reference !== undefined)
|
|
392
|
+
return field.nullable ? "null" : null;
|
|
393
|
+
if (field.type === "integer")
|
|
394
|
+
return "({ sequence }) => sequence";
|
|
395
|
+
if (field.type === "boolean")
|
|
396
|
+
return "false";
|
|
397
|
+
if (field.type === "date")
|
|
398
|
+
return '"2026-01-01"';
|
|
399
|
+
if (field.type === "timestamp")
|
|
400
|
+
return "Date.UTC(2026, 0, 1)";
|
|
401
|
+
if (field.type === "email")
|
|
402
|
+
return "({ sequence }) => `person${sequence}@example.com`";
|
|
403
|
+
if (field.type === "url")
|
|
404
|
+
return "({ sequence }) => `https://example.com/${sequence}`";
|
|
405
|
+
return `({ sequence }) => \`${field.label} \${sequence}\``;
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* The plural of a kebab-case `name`, pluralizing its last word by three English rules: a
|
|
409
|
+
* sibilant ending adds `es`, a consonant before `y` becomes `ies`, and anything else adds `s`.
|
|
410
|
+
*/
|
|
411
|
+
export function pluralize(name) {
|
|
412
|
+
if (/(s|x|z|ch|sh)$/.test(name))
|
|
413
|
+
return `${name}es`;
|
|
414
|
+
if (/[^aeiou]y$/.test(name))
|
|
415
|
+
return `${name.slice(0, -1)}ies`;
|
|
416
|
+
return `${name}s`;
|
|
417
|
+
}
|
|
418
|
+
/** `word` with its first letter upper case. */
|
|
419
|
+
function capitalize(word) {
|
|
420
|
+
return word.charAt(0).toUpperCase() + word.slice(1);
|
|
421
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What one `sdxc scaffold` run does: renders every file a resource needs from its stubs, runs
|
|
3
|
+
* each declaration `make` would run in a staged copy of the project so they build on one another,
|
|
4
|
+
* and only then writes. A refusal anywhere in the plan leaves the project as it was.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { Result } from "@sdxc/result";
|
|
10
|
+
import type { MakeContext } from "../make/index.js";
|
|
11
|
+
import type { ScaffoldPiece } from "../make/kinds.js";
|
|
12
|
+
import type { Project } from "../middleware.js";
|
|
13
|
+
/** One scaffold, as the command line gave it. */
|
|
14
|
+
export interface ScaffoldInput {
|
|
15
|
+
name: string;
|
|
16
|
+
/** Each `name`, `name:type` or `name?`, as `parseFields` reads them. */
|
|
17
|
+
fields: readonly string[];
|
|
18
|
+
/** The plural when the English rules get it wrong. */
|
|
19
|
+
plural: string | undefined;
|
|
20
|
+
mcp: boolean;
|
|
21
|
+
command: boolean;
|
|
22
|
+
/** JSON endpoints in the `api` contract, with their OpenAPI operations, instead of pages. */
|
|
23
|
+
api?: boolean;
|
|
24
|
+
/** Whether to write tests; `undefined` takes `make.tests`. */
|
|
25
|
+
test: boolean | undefined;
|
|
26
|
+
/** Whether to write one feature `.spec`; `undefined` takes `make.specs`. */
|
|
27
|
+
spec?: boolean | undefined;
|
|
28
|
+
dryRun: boolean;
|
|
29
|
+
/** The pieces to leave out, each one the project already has or does without. */
|
|
30
|
+
skip?: readonly ScaffoldPiece[];
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Plans and writes the scaffold `input` describes in `project`, printing each file written and
|
|
34
|
+
* edited and each package added; under `dryRun` it prints every file and the diff of every edit
|
|
35
|
+
* instead. A declaration that cannot be placed is printed for the user to make, as `make` does.
|
|
36
|
+
*
|
|
37
|
+
* @param packageRoot - The `@sdxc/kit` package, whose `stubs/` holds the built-in stubs.
|
|
38
|
+
*/
|
|
39
|
+
export declare function scaffold(ctx: MakeContext, project: Project, packageRoot: string, input: ScaffoldInput): Promise<Result<void, Error>>;
|