@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
package/dist/make/kinds.js
CHANGED
|
@@ -14,18 +14,36 @@
|
|
|
14
14
|
export const COMMON_PLACEHOLDERS = ["name", "key", "Name", "snake", "title", "specifier", "header"];
|
|
15
15
|
/**
|
|
16
16
|
* Every kind, in the order `sdxc make --help` lists them. `path` is the declaration's key path
|
|
17
|
-
* in its map (`trials.expire`), `routes` the route contract's specifier, `tool` the MCP tool
|
|
17
|
+
* in its map (`trials.expire`, or a policy's ability group `billing.invoice`), `routes` the route contract's specifier, `tool` the MCP tool
|
|
18
18
|
* name, `uri` an MCP resource's pattern, `model` the factory's model specifier. A command's
|
|
19
19
|
* `command` is the words typed to run it (`users grant`), `member` the property access that
|
|
20
|
-
* reaches its declaration (`.users.grant`), and `words` those words as string literals.
|
|
20
|
+
* reaches its declaration (`.users.grant`), and `words` those words as string literals. A
|
|
21
|
+
* model's `columns` are the column lines of the fields `make model` was given and `table` the
|
|
22
|
+
* plural its table is named; a factory's `attributes` are a value per field, empty without fields.
|
|
23
|
+
* A `--resource` controller's `ability` is the one it checks, `access` the guard answering a
|
|
24
|
+
* refusal with an empty response and `pageAccess` the one rendering the not-found page. A form's
|
|
25
|
+
* `params` is what its handlers pass to `href()`, `ctx.params` when its route has params, and
|
|
26
|
+
* `sampleParams` the values its test passes instead. An email's `subject`, `layoutTitle`, `body` and
|
|
27
|
+
* `preview` are its template's members written from its fields, and `sample`, `subjectSample` and
|
|
28
|
+
* `textSample` what its test renders and expects.
|
|
21
29
|
*/
|
|
22
30
|
export const KINDS = {
|
|
23
31
|
controller: {
|
|
24
32
|
folder: "app/http/controllers",
|
|
25
33
|
extension: ".tsx",
|
|
26
34
|
tests: true,
|
|
35
|
+
specs: true,
|
|
27
36
|
nested: true,
|
|
28
|
-
placeholders: [
|
|
37
|
+
placeholders: [
|
|
38
|
+
"path",
|
|
39
|
+
"routes",
|
|
40
|
+
"ability",
|
|
41
|
+
"access",
|
|
42
|
+
"pageAccess",
|
|
43
|
+
"params",
|
|
44
|
+
"sampleParams",
|
|
45
|
+
"url",
|
|
46
|
+
],
|
|
29
47
|
},
|
|
30
48
|
middleware: {
|
|
31
49
|
folder: "app/http/middleware",
|
|
@@ -42,7 +60,13 @@ export const KINDS = {
|
|
|
42
60
|
placeholders: [],
|
|
43
61
|
},
|
|
44
62
|
job: { folder: "app/jobs", extension: ".ts", tests: true, nested: true, placeholders: ["path"] },
|
|
45
|
-
model: {
|
|
63
|
+
model: {
|
|
64
|
+
folder: "app/models",
|
|
65
|
+
extension: ".ts",
|
|
66
|
+
tests: true,
|
|
67
|
+
nested: true,
|
|
68
|
+
placeholders: ["columns", "table"],
|
|
69
|
+
},
|
|
46
70
|
migration: {
|
|
47
71
|
folder: "database/migrations",
|
|
48
72
|
extension: ".sql",
|
|
@@ -55,7 +79,7 @@ export const KINDS = {
|
|
|
55
79
|
extension: ".ts",
|
|
56
80
|
tests: false,
|
|
57
81
|
nested: true,
|
|
58
|
-
placeholders: ["model"],
|
|
82
|
+
placeholders: ["model", "attributes"],
|
|
59
83
|
},
|
|
60
84
|
service: {
|
|
61
85
|
folder: "app/services",
|
|
@@ -64,7 +88,14 @@ export const KINDS = {
|
|
|
64
88
|
nested: true,
|
|
65
89
|
placeholders: [],
|
|
66
90
|
},
|
|
67
|
-
policy: {
|
|
91
|
+
policy: {
|
|
92
|
+
folder: "app/policies",
|
|
93
|
+
extension: ".ts",
|
|
94
|
+
tests: true,
|
|
95
|
+
testOnly: true,
|
|
96
|
+
nested: true,
|
|
97
|
+
placeholders: ["path"],
|
|
98
|
+
},
|
|
68
99
|
initializer: {
|
|
69
100
|
folder: "app/initializers",
|
|
70
101
|
extension: ".ts",
|
|
@@ -72,11 +103,27 @@ export const KINDS = {
|
|
|
72
103
|
nested: true,
|
|
73
104
|
placeholders: [],
|
|
74
105
|
},
|
|
75
|
-
email: {
|
|
106
|
+
email: {
|
|
107
|
+
folder: "app/emails",
|
|
108
|
+
extension: ".tsx",
|
|
109
|
+
tests: true,
|
|
110
|
+
nested: true,
|
|
111
|
+
placeholders: [
|
|
112
|
+
"path",
|
|
113
|
+
"subject",
|
|
114
|
+
"layoutTitle",
|
|
115
|
+
"body",
|
|
116
|
+
"preview",
|
|
117
|
+
"sample",
|
|
118
|
+
"subjectSample",
|
|
119
|
+
"textSample",
|
|
120
|
+
],
|
|
121
|
+
},
|
|
76
122
|
mcp: {
|
|
77
123
|
folder: "app/mcp/tools",
|
|
78
124
|
extension: ".ts",
|
|
79
125
|
tests: true,
|
|
126
|
+
specs: true,
|
|
80
127
|
nested: true,
|
|
81
128
|
placeholders: ["path", "tool", "uri"],
|
|
82
129
|
},
|
|
@@ -84,10 +131,47 @@ export const KINDS = {
|
|
|
84
131
|
folder: "app/commands",
|
|
85
132
|
extension: ".ts",
|
|
86
133
|
tests: true,
|
|
134
|
+
specs: true,
|
|
87
135
|
nested: true,
|
|
88
|
-
placeholders: ["command", "member", "words"],
|
|
136
|
+
placeholders: ["command", "member", "words", "args"],
|
|
89
137
|
},
|
|
90
138
|
};
|
|
139
|
+
/** What `make model` writes beside the model unless `--skip` names it. */
|
|
140
|
+
export const MODEL_COMPANIONS = ["migration", "factory"];
|
|
141
|
+
/** Every action `--actions` accepts in either contract, and `all`. */
|
|
142
|
+
export const ACTION_NAMES = [
|
|
143
|
+
"all",
|
|
144
|
+
"index",
|
|
145
|
+
"new",
|
|
146
|
+
"show",
|
|
147
|
+
"edit",
|
|
148
|
+
"create",
|
|
149
|
+
"update",
|
|
150
|
+
"destroy",
|
|
151
|
+
];
|
|
152
|
+
/** The pieces `--skip` can leave out of a scaffold. */
|
|
153
|
+
export const SCAFFOLD_PIECES = [
|
|
154
|
+
"model",
|
|
155
|
+
"migration",
|
|
156
|
+
"factory",
|
|
157
|
+
"validator",
|
|
158
|
+
"policy",
|
|
159
|
+
"pages",
|
|
160
|
+
];
|
|
161
|
+
/** The kinds `destroy` undoes; a migration may have run and a policy is edits alone, so neither is. */
|
|
162
|
+
export const DESTROY_KINDS = [
|
|
163
|
+
"controller",
|
|
164
|
+
"middleware",
|
|
165
|
+
"validator",
|
|
166
|
+
"job",
|
|
167
|
+
"model",
|
|
168
|
+
"factory",
|
|
169
|
+
"service",
|
|
170
|
+
"initializer",
|
|
171
|
+
"email",
|
|
172
|
+
"mcp",
|
|
173
|
+
"command",
|
|
174
|
+
];
|
|
91
175
|
/** Where `make mcp --resource` writes, beside the tools. */
|
|
92
176
|
export const MCP_RESOURCES_FOLDER = "app/mcp/resources";
|
|
93
177
|
/** Whether `value` names a kind. */
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mounts what `make middleware --mount <target>` generates: imports the middleware into a router
|
|
3
|
+
* initializer's `createRouter({ middleware: [...] })` or a `createMiddleware(…)` chain module and
|
|
4
|
+
* calls it last there. The targets are read from the project, each named after its file.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { Result } from "@sdxc/result";
|
|
10
|
+
import type { Declared } from "./declare.js";
|
|
11
|
+
import type { Name } from "./names.js";
|
|
12
|
+
/** What to mount and where, as the command line asked for it. */
|
|
13
|
+
export interface MountRequest {
|
|
14
|
+
name: Name;
|
|
15
|
+
/** The target `--mount` named: a router initializer's or a chain module's name. */
|
|
16
|
+
target: string;
|
|
17
|
+
/** The generated middleware, relative to the project root, without its extension. */
|
|
18
|
+
handler: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Plans the import and the call that mount the middleware on `request.target`. A target the
|
|
22
|
+
* project lacks is a usage failure listing the ones it has; an edit that cannot be placed comes
|
|
23
|
+
* back as `manual`, with the import and the call to add by hand.
|
|
24
|
+
*/
|
|
25
|
+
export declare function mount(request: MountRequest, root: string): Promise<Result<Declared, Error>>;
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mounts what `make middleware --mount <target>` generates: imports the middleware into a router
|
|
3
|
+
* initializer's `createRouter({ middleware: [...] })` or a `createMiddleware(…)` chain module and
|
|
4
|
+
* calls it last there. The targets are read from the project, each named after its file.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { readdir, readFile } from "node:fs/promises";
|
|
10
|
+
import { join, posix } from "node:path";
|
|
11
|
+
import { usage } from "@sdxc/cli";
|
|
12
|
+
import { addElement, findModuleReferences } from "@sdxc/codemod";
|
|
13
|
+
import { isFailure, isSuccess, success, wrap } from "@sdxc/result";
|
|
14
|
+
import { literal } from "./generate.js";
|
|
15
|
+
import { ensureImport, resolvesTo, specifierFor } from "./imports.js";
|
|
16
|
+
/** Where router initializers live; each one that calls `createRouter` is a target. */
|
|
17
|
+
const INITIALIZERS = "app/initializers";
|
|
18
|
+
/** Where chain modules live; each one default-exporting `createMiddleware(…)` is a target. */
|
|
19
|
+
const CHAINS = "app/http/middleware";
|
|
20
|
+
/**
|
|
21
|
+
* Plans the import and the call that mount the middleware on `request.target`. A target the
|
|
22
|
+
* project lacks is a usage failure listing the ones it has; an edit that cannot be placed comes
|
|
23
|
+
* back as `manual`, with the import and the call to add by hand.
|
|
24
|
+
*/
|
|
25
|
+
export async function mount(request, root) {
|
|
26
|
+
let targets = await findTargets(root);
|
|
27
|
+
let target = targets.find((candidate) => candidate.name === request.target);
|
|
28
|
+
if (target === undefined) {
|
|
29
|
+
let names = targets.map((candidate) => candidate.name).join(", ");
|
|
30
|
+
return usage(`No middleware chain ${literal(request.target)} to mount on; this project has: ${names === "" ? "none" : names}`);
|
|
31
|
+
}
|
|
32
|
+
let tsconfig = await wrap(() => readFile(join(root, "tsconfig.json"), "utf8"));
|
|
33
|
+
let aliased = isSuccess(tsconfig) && tsconfig.data.includes('"~/app/*"');
|
|
34
|
+
let examples = target.references
|
|
35
|
+
.filter((reference) => reference.kind === "import")
|
|
36
|
+
.map((reference) => reference.specifier);
|
|
37
|
+
let specifier = specifierFor(target.path, request.handler, examples, aliased);
|
|
38
|
+
let local = request.name.key;
|
|
39
|
+
let call = `${local}()`;
|
|
40
|
+
let chain = target.kind === "router"
|
|
41
|
+
? `${target.callee}({ middleware: [ …, ${call} ] })`
|
|
42
|
+
: `${target.callee}(…, ${call})`;
|
|
43
|
+
let manual = (reason) => ({
|
|
44
|
+
kind: "manual",
|
|
45
|
+
path: target.name,
|
|
46
|
+
reason,
|
|
47
|
+
instructions: [
|
|
48
|
+
`In ${target.path}, import it and call it last in the chain:`,
|
|
49
|
+
` import ${local} from ${literal(specifier)};`,
|
|
50
|
+
` ${chain}`,
|
|
51
|
+
].join("\n"),
|
|
52
|
+
});
|
|
53
|
+
let bound = target.references.some((reference) => reference.kind === "import" && reference.names.some((name) => name.local === local));
|
|
54
|
+
if (bound)
|
|
55
|
+
return success(manual(`${target.path} already binds ${local}`));
|
|
56
|
+
let imported = ensureImport(target.path, target.source, { kind: "default" }, (candidate) => resolvesTo(candidate, target.path, request.handler), specifier, local);
|
|
57
|
+
if (isFailure(imported))
|
|
58
|
+
return success(manual(imported.error.message));
|
|
59
|
+
let added = addElement(target.path, imported.data.source, {
|
|
60
|
+
call: target.callee,
|
|
61
|
+
...(target.kind === "router" ? { path: ["middleware"] } : {}),
|
|
62
|
+
value: `${imported.data.local}()`,
|
|
63
|
+
});
|
|
64
|
+
if (isFailure(added))
|
|
65
|
+
return success(manual(added.error.message));
|
|
66
|
+
return success({
|
|
67
|
+
kind: "edits",
|
|
68
|
+
path: target.name,
|
|
69
|
+
edits: [{ path: target.path, before: target.source, after: added.data.source }],
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Every target in the project: the router initializers in file order, then the chain modules in
|
|
74
|
+
* path order. A chain named like a router initializer is never reached, since names resolve to
|
|
75
|
+
* the first target that has them.
|
|
76
|
+
*/
|
|
77
|
+
async function findTargets(root) {
|
|
78
|
+
let targets = [];
|
|
79
|
+
for (let path of await sources(root, INITIALIZERS, false)) {
|
|
80
|
+
let target = await readTarget(root, path, "createRouter");
|
|
81
|
+
if (target === null || !new RegExp(`\\b${escape(target.callee)}\\s*\\(`).test(target.source))
|
|
82
|
+
continue;
|
|
83
|
+
targets.push({ ...target, name: withoutExtension(path, INITIALIZERS), kind: "router" });
|
|
84
|
+
}
|
|
85
|
+
for (let path of await sources(root, CHAINS, true)) {
|
|
86
|
+
let target = await readTarget(root, path, "createMiddleware");
|
|
87
|
+
if (target === null)
|
|
88
|
+
continue;
|
|
89
|
+
let exported = new RegExp(`export\\s+default\\s+${escape(target.callee)}\\s*\\(`);
|
|
90
|
+
if (!exported.test(target.source))
|
|
91
|
+
continue;
|
|
92
|
+
targets.push({ ...target, name: withoutExtension(path, CHAINS), kind: "chain" });
|
|
93
|
+
}
|
|
94
|
+
return targets;
|
|
95
|
+
}
|
|
96
|
+
/** The TypeScript modules under `folder`, sorted, leaving out tests and declaration files. */
|
|
97
|
+
async function sources(root, folder, recursive) {
|
|
98
|
+
let entries = await wrap(() => readdir(join(root, folder), { recursive }));
|
|
99
|
+
if (isFailure(entries))
|
|
100
|
+
return [];
|
|
101
|
+
return entries.data
|
|
102
|
+
.map((entry) => entry.split("\\").join("/"))
|
|
103
|
+
.filter((entry) => /\.tsx?$/.test(entry) && !/\.(?:test\.tsx?|d\.ts)$/.test(entry))
|
|
104
|
+
.sort()
|
|
105
|
+
.map((entry) => posix.join(folder, entry));
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* The file at `path` with the local name it imports `factory` by, as a value; `null` when it
|
|
109
|
+
* cannot be read or scanned, or imports no such factory.
|
|
110
|
+
*/
|
|
111
|
+
async function readTarget(root, path, factory) {
|
|
112
|
+
let source = await wrap(() => readFile(join(root, path), "utf8"));
|
|
113
|
+
if (isFailure(source))
|
|
114
|
+
return null;
|
|
115
|
+
let references = findModuleReferences(path, source.data);
|
|
116
|
+
if (isFailure(references))
|
|
117
|
+
return null;
|
|
118
|
+
for (let reference of references.data) {
|
|
119
|
+
if (reference.kind !== "import" || reference.typeOnly)
|
|
120
|
+
continue;
|
|
121
|
+
let bound = reference.names.find((name) => name.imported === factory && !name.typeOnly);
|
|
122
|
+
if (bound !== undefined) {
|
|
123
|
+
return { path, source: source.data, references: references.data, callee: bound.local };
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
/** A module's name inside `folder`: its path there, without the extension. */
|
|
129
|
+
function withoutExtension(path, folder) {
|
|
130
|
+
return posix.relative(folder, path).replace(/\.tsx?$/, "");
|
|
131
|
+
}
|
|
132
|
+
/** `text` with every regular-expression metacharacter escaped. */
|
|
133
|
+
function escape(text) {
|
|
134
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
135
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The migration that brings a chain's schema in step with a model's tables: the chain is replayed
|
|
3
|
+
* into an in-memory SQLite, each table is read back and compared with its data-table definition,
|
|
4
|
+
* and the cheapest statements SQLite and D1 apply without losing rows are chosen and verified.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { DatabaseSync } from "node:sqlite";
|
|
10
|
+
import type { Result } from "@sdxc/result";
|
|
11
|
+
import type { TableShape } from "./schema.js";
|
|
12
|
+
/** One migration file of a chain: its file name, which carries its number, and its SQL. */
|
|
13
|
+
export interface MigrationSource {
|
|
14
|
+
name: string;
|
|
15
|
+
sql: string;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* What the migration does: its statements in order, each ending in `;`, and what a reviewer must
|
|
19
|
+
* know before applying them. No statements means the chain already matches the model.
|
|
20
|
+
*/
|
|
21
|
+
export interface SchemaChange {
|
|
22
|
+
statements: string[];
|
|
23
|
+
warnings: string[];
|
|
24
|
+
}
|
|
25
|
+
/** An error {@link replayChain} stepped past because its caller accepted it. */
|
|
26
|
+
export interface AcceptedFailure {
|
|
27
|
+
file: string;
|
|
28
|
+
/** SQLite's message for the statement that failed. */
|
|
29
|
+
message: string;
|
|
30
|
+
}
|
|
31
|
+
/** How {@link replayChain} treats a statement that errors. */
|
|
32
|
+
export interface ReplayOptions {
|
|
33
|
+
/**
|
|
34
|
+
* Whether the replay steps past `message`, raised by a statement of `file`, and goes on with
|
|
35
|
+
* the next one. Without it, every error stops the replay.
|
|
36
|
+
*/
|
|
37
|
+
accepts?(file: string, message: string): boolean;
|
|
38
|
+
}
|
|
39
|
+
/** A replayed chain: the database it built, which the caller closes, and the errors it accepted. */
|
|
40
|
+
export interface ReplayedChain {
|
|
41
|
+
db: DatabaseSync;
|
|
42
|
+
accepted: AcceptedFailure[];
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Plans the migration that makes the schema `migrations` build match `tables`, then applies it to
|
|
46
|
+
* that schema and compares again, so a plan that errors or leaves a difference fails here rather
|
|
47
|
+
* than in production.
|
|
48
|
+
*/
|
|
49
|
+
export declare function planMigration(migrations: readonly MigrationSource[], tables: readonly TableShape[]): Result<SchemaChange, Error>;
|
|
50
|
+
/**
|
|
51
|
+
* A fresh in-memory database with `migrations` applied in order of their leading number, with
|
|
52
|
+
* double-quoted string literals disabled so a copy reading a missing column errors instead of
|
|
53
|
+
* writing the column's name into every row. An error the caller accepts is stepped past.
|
|
54
|
+
*
|
|
55
|
+
* @returns The database, with every accepted error in replay order; the first other error stops
|
|
56
|
+
* the replay with a failure naming its file.
|
|
57
|
+
*/
|
|
58
|
+
export declare function replayChain(migrations: readonly MigrationSource[], options?: ReplayOptions): Result<ReplayedChain, Error>;
|