@sdxc/kit 2026.10.17 → 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 +390 -20
- package/changelog.jsonl +1160 -1092
- package/config.schema.json +47 -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 +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 +16 -0
- package/dist/commands/run.js +32 -11
- 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.js +3 -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/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 +16 -5
- package/dist/make/generate.js +38 -15
- 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 -29
- package/dist/make/kinds.d.ts +29 -2
- package/dist/make/kinds.js +79 -7
- 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/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/controller.api.resource.stub +23 -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/email.stub +9 -14
- package/stubs/email.test.stub +9 -9
- package/stubs/factory.stub +1 -1
- 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 +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/templates/app/AGENTS.md +163 -0
- package/templates/app/CLAUDE.md +1 -0
- package/templates/app/README.md +28 -13
- 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/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 +28 -2
- package/templates/app/app/initializers/dispatcher.ts +9 -1
- 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 +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/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 +2 -1
- package/stubs/policy.stub +0 -16
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `sdxc make controller <name> --actions`: one controller per action of a resource, each
|
|
3
|
+
* declared under `<name>` with the method and route a resource's pages or JSON endpoints take,
|
|
4
|
+
* and, with `--resource`, guarded by the ability the action needs.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { Result } from "@sdxc/result";
|
|
10
|
+
import type { Project } from "../middleware.js";
|
|
11
|
+
import type { MakeContext, MakeInput } from "./index.js";
|
|
12
|
+
/** What `--actions` was given beside the controller's own flags. */
|
|
13
|
+
export interface ActionsInput extends Omit<MakeInput, "kind" | "method" | "path" | "ability"> {
|
|
14
|
+
actions: readonly string[];
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Makes one controller per action in `input.actions` under `input.name`, in the order the
|
|
18
|
+
* contract lists them. Every action's file is checked free before the first is written, so a
|
|
19
|
+
* taken name stops the run with nothing written.
|
|
20
|
+
*
|
|
21
|
+
* @param packageRoot - The `@sdxc/kit` package, whose `stubs/` holds the built-in stubs.
|
|
22
|
+
*/
|
|
23
|
+
export declare function makeActions(ctx: MakeContext, project: Project, packageRoot: string, input: ActionsInput): Promise<Result<void, Error>>;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `sdxc make controller <name> --actions`: one controller per action of a resource, each
|
|
3
|
+
* declared under `<name>` with the method and route a resource's pages or JSON endpoints take,
|
|
4
|
+
* and, with `--resource`, guarded by the ability the action needs.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { failure, isFailure } from "@sdxc/result";
|
|
10
|
+
import { exists } from "./generate.js";
|
|
11
|
+
import { parseName } from "./names.js";
|
|
12
|
+
import { finish, makeUnformatted } from "./index.js";
|
|
13
|
+
/** The pages of a resource, as `sdxc scaffold` routes them. */
|
|
14
|
+
const PAGE_ACTIONS = {
|
|
15
|
+
index: { method: "get", suffix: "", ability: null },
|
|
16
|
+
new: { method: "form", suffix: "/new", ability: "create" },
|
|
17
|
+
show: { method: "get", suffix: "/:id", ability: "read" },
|
|
18
|
+
edit: { method: "form", suffix: "/:id/edit", ability: "update" },
|
|
19
|
+
destroy: { method: "post", suffix: "/:id/destroy", ability: "delete" },
|
|
20
|
+
};
|
|
21
|
+
/** The JSON endpoints of a resource, one per method. */
|
|
22
|
+
const API_ACTIONS = {
|
|
23
|
+
index: { method: "get", suffix: "", ability: null },
|
|
24
|
+
show: { method: "get", suffix: "/:id", ability: "read" },
|
|
25
|
+
create: { method: "post", suffix: "", ability: "create" },
|
|
26
|
+
update: { method: "patch", suffix: "/:id", ability: "update" },
|
|
27
|
+
destroy: { method: "delete", suffix: "/:id", ability: "delete" },
|
|
28
|
+
};
|
|
29
|
+
/** The contract whose controllers answer JSON, so its actions are the endpoints. */
|
|
30
|
+
const API_CONTRACT = "api";
|
|
31
|
+
/**
|
|
32
|
+
* Makes one controller per action in `input.actions` under `input.name`, in the order the
|
|
33
|
+
* contract lists them. Every action's file is checked free before the first is written, so a
|
|
34
|
+
* taken name stops the run with nothing written.
|
|
35
|
+
*
|
|
36
|
+
* @param packageRoot - The `@sdxc/kit` package, whose `stubs/` holds the built-in stubs.
|
|
37
|
+
*/
|
|
38
|
+
export async function makeActions(ctx, project, packageRoot, input) {
|
|
39
|
+
let routes = input.routes ?? project.config.make.routes;
|
|
40
|
+
let table = routes === API_CONTRACT ? API_ACTIONS : PAGE_ACTIONS;
|
|
41
|
+
let wanted = input.actions.includes("all") ? Object.keys(table) : [...new Set(input.actions)];
|
|
42
|
+
let unknown = wanted.filter((action) => !(action in table));
|
|
43
|
+
if (unknown.length > 0) {
|
|
44
|
+
let contract = routes === API_CONTRACT ? "the api contract" : `routes/${routes}`;
|
|
45
|
+
return failure(new Error(`${unknown.join(", ")} ${unknown.length === 1 ? "is not an action" : "are not actions"} of ${contract}; use ${Object.keys(table).join(", ")} or all`));
|
|
46
|
+
}
|
|
47
|
+
let base = parseName(input.name);
|
|
48
|
+
if (isFailure(base))
|
|
49
|
+
return base;
|
|
50
|
+
let folder = base.data.segments.join("/");
|
|
51
|
+
let ordered = Object.keys(table).filter((action) => wanted.includes(action));
|
|
52
|
+
for (let action of ordered) {
|
|
53
|
+
let path = `app/http/controllers/${folder}/${action}.tsx`;
|
|
54
|
+
if (input.force !== true && (await exists(project.root, path))) {
|
|
55
|
+
return failure(new Error(`${path} already exists; sdxc make leaves it as it is unless --force`));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
let changed = [];
|
|
59
|
+
let notes = [];
|
|
60
|
+
for (let action of ordered) {
|
|
61
|
+
let route = table[action];
|
|
62
|
+
let guarded = input.guardedBy !== undefined && route.ability !== null;
|
|
63
|
+
let made = await makeUnformatted(ctx, project, packageRoot, {
|
|
64
|
+
...input,
|
|
65
|
+
kind: "controller",
|
|
66
|
+
name: `${folder}/${action}`,
|
|
67
|
+
method: route.method,
|
|
68
|
+
path: `/${folder}${route.suffix}`,
|
|
69
|
+
routes,
|
|
70
|
+
guardedBy: guarded ? input.guardedBy : undefined,
|
|
71
|
+
ability: guarded ? (route.ability ?? undefined) : undefined,
|
|
72
|
+
});
|
|
73
|
+
if (isFailure(made))
|
|
74
|
+
return made;
|
|
75
|
+
changed.push(...made.data.changed.filter((path) => !changed.includes(path)));
|
|
76
|
+
notes.push(...made.data.notes.filter((note) => !notes.includes(note)));
|
|
77
|
+
}
|
|
78
|
+
if (input.guardedBy !== undefined && ordered.includes("index")) {
|
|
79
|
+
notes.push(`${folder}/index lists rows, so it checks no single one; show each row a visitor may read with ctx.access.can(abilities.<resource>.read, { <resource>: row })\n`);
|
|
80
|
+
}
|
|
81
|
+
return finish(ctx, project, { changed, notes });
|
|
82
|
+
}
|
package/dist/make/declare.d.ts
CHANGED
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
* Declares and maps what `make job`, `make controller`, `make mcp` and `make command` generate:
|
|
3
3
|
* the entry in the job map, route contract, MCP or command declaration module, and the mapping
|
|
4
4
|
* in the initializer that serves it, plus a page's sitemap decision; `make model` registers the
|
|
5
|
-
* model in `app/models/index.ts`
|
|
6
|
-
*
|
|
5
|
+
* model in `app/models/index.ts` and `make policy` a resource's abilities and grants the same
|
|
6
|
+
* way. Every edit is computed before anything is written; when one cannot be placed, none is
|
|
7
|
+
* made and the command prints them for the user instead.
|
|
7
8
|
*
|
|
8
9
|
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
9
10
|
* @copyright Sergio Xalambrí 2026
|
|
@@ -11,7 +12,7 @@
|
|
|
11
12
|
import type { Name } from "./names.js";
|
|
12
13
|
/** What to declare and map, as the command line asked for it. */
|
|
13
14
|
export interface DeclarationRequest {
|
|
14
|
-
kind: "job" | "controller" | "mcp" | "command" | "model";
|
|
15
|
+
kind: "job" | "controller" | "mcp" | "command" | "model" | "policy" | "email";
|
|
15
16
|
name: Name;
|
|
16
17
|
/** `cron` for a scheduled job, `resource` for an MCP resource, `null` otherwise. */
|
|
17
18
|
variant: string | null;
|
|
@@ -29,6 +30,20 @@ export interface DeclarationRequest {
|
|
|
29
30
|
tool: string;
|
|
30
31
|
uri: string;
|
|
31
32
|
title: string;
|
|
33
|
+
/** A page's sitemap decision in place of the one its pattern implies. */
|
|
34
|
+
sitemap?: string;
|
|
35
|
+
/** A tool's or a command's options in place of the empty ones `make` declares; an email's props. */
|
|
36
|
+
options?: DeclarationOptions;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* The options of a tool or a command whose input the caller knows: the object literal's lines,
|
|
40
|
+
* written with the local name each of `imports` is bound to in the declarations module, which
|
|
41
|
+
* gains the import when it lacks it. A tool's lines replace its description and input too.
|
|
42
|
+
*/
|
|
43
|
+
export interface DeclarationOptions {
|
|
44
|
+
/** The schema builder `s`, and a command's `argument` and `flag`. */
|
|
45
|
+
imports: readonly ("s" | "argument" | "flag")[];
|
|
46
|
+
lines: (locals: Readonly<Record<string, string>>) => string[];
|
|
32
47
|
}
|
|
33
48
|
/** One file's text before and after an edit, by path relative to the project root. */
|
|
34
49
|
export interface FileEdit {
|
|
@@ -52,3 +67,16 @@ export type Declared = {
|
|
|
52
67
|
};
|
|
53
68
|
/** Plans the declaration and mapping for `request` in the project at `root`. */
|
|
54
69
|
export declare function declare(request: DeclarationRequest, root: string): Promise<Declared>;
|
|
70
|
+
/**
|
|
71
|
+
* Whether `app/policies/abilities.ts` declares a group at `path` in its `abilities({ … })`, as
|
|
72
|
+
* `make policy` does for a resource. An unreadable catalog, or one without the call, declares none.
|
|
73
|
+
*/
|
|
74
|
+
export declare function declaresAbilities(root: string, path: readonly string[]): Promise<boolean>;
|
|
75
|
+
/**
|
|
76
|
+
* The row type a resource's model exports, `PostRow` from `app/models/post.ts`, or `null` when
|
|
77
|
+
* the resource has no model or its model exports no such type.
|
|
78
|
+
*/
|
|
79
|
+
export declare function findRow(root: string, name: Name): Promise<{
|
|
80
|
+
path: string;
|
|
81
|
+
type: string;
|
|
82
|
+
} | null>;
|
package/dist/make/declare.js
CHANGED
|
@@ -2,18 +2,19 @@
|
|
|
2
2
|
* Declares and maps what `make job`, `make controller`, `make mcp` and `make command` generate:
|
|
3
3
|
* the entry in the job map, route contract, MCP or command declaration module, and the mapping
|
|
4
4
|
* in the initializer that serves it, plus a page's sitemap decision; `make model` registers the
|
|
5
|
-
* model in `app/models/index.ts`
|
|
6
|
-
*
|
|
5
|
+
* model in `app/models/index.ts` and `make policy` a resource's abilities and grants the same
|
|
6
|
+
* way. Every edit is computed before anything is written; when one cannot be placed, none is
|
|
7
|
+
* made and the command prints them for the user instead.
|
|
7
8
|
*
|
|
8
9
|
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
9
10
|
* @copyright Sergio Xalambrí 2026
|
|
10
11
|
*/
|
|
11
12
|
import { readdir, readFile } from "node:fs/promises";
|
|
12
13
|
import { dirname, join, posix } from "node:path";
|
|
13
|
-
import { addProperty, findModuleReferences, insertAfterLast } from "@sdxc/codemod";
|
|
14
|
+
import { addElement, addProperty, findModuleReferences, insertAfterLast } from "@sdxc/codemod";
|
|
14
15
|
import { failure, isFailure, isSuccess, success, wrap } from "@sdxc/result";
|
|
15
16
|
import { literal, memberAccess } from "./generate.js";
|
|
16
|
-
import { ensureImport, resolvesTo, specifierFor } from "./imports.js";
|
|
17
|
+
import { ensureImport, ensureTypeImport, resolvesTo, specifierFor } from "./imports.js";
|
|
17
18
|
import { camel } from "./names.js";
|
|
18
19
|
/** The `remix/routes` helper each `--method` declares with; `form` adds a page and an action. */
|
|
19
20
|
const ROUTE_HELPERS = {
|
|
@@ -31,6 +32,7 @@ const INSTANCE_NAMES = {
|
|
|
31
32
|
createRouter: "router",
|
|
32
33
|
createHandler: "mcp",
|
|
33
34
|
createCLI: "cli",
|
|
35
|
+
createMail: "mail",
|
|
34
36
|
};
|
|
35
37
|
/** Plans the declaration and mapping for `request` in the project at `root`. */
|
|
36
38
|
export async function declare(request, root) {
|
|
@@ -42,6 +44,10 @@ export async function declare(request, root) {
|
|
|
42
44
|
return declareCommand(request, root);
|
|
43
45
|
if (request.kind === "model")
|
|
44
46
|
return declareModel(request, root);
|
|
47
|
+
if (request.kind === "policy")
|
|
48
|
+
return declarePolicy(request, root);
|
|
49
|
+
if (request.kind === "email")
|
|
50
|
+
return declareEmail(request, root);
|
|
45
51
|
return declareMcp(request, root);
|
|
46
52
|
}
|
|
47
53
|
/** Declares a job in `app/jobs/index.ts` and maps it on the job dispatcher. */
|
|
@@ -122,7 +128,7 @@ async function declareController(request, root) {
|
|
|
122
128
|
let lazyLocal = "lazy";
|
|
123
129
|
let statement = (lazy) => `${instance}.map(${local}.${path.join(".")}, ${lazy}(() => import(${literal(handler)})));`;
|
|
124
130
|
let sitemap = PAGE_HELPERS.has(helper) ? await findSitemap(root, module) : null;
|
|
125
|
-
let decision = sitemapDecision(helper, pattern);
|
|
131
|
+
let decision = request.sitemap ?? sitemapDecision(helper, pattern);
|
|
126
132
|
let manual = (reason) => ({
|
|
127
133
|
kind: "manual",
|
|
128
134
|
path: path.join("."),
|
|
@@ -267,6 +273,8 @@ async function declareMcp(request, root) {
|
|
|
267
273
|
return manual(schema.error.message);
|
|
268
274
|
source = schema.data.source;
|
|
269
275
|
body[1] = `input: ${schema.data.local}.object({}),`;
|
|
276
|
+
if (request.options !== undefined)
|
|
277
|
+
body = request.options.lines({ s: schema.data.local });
|
|
270
278
|
}
|
|
271
279
|
let added = addProperty(declarations, source, {
|
|
272
280
|
call: group,
|
|
@@ -296,6 +304,73 @@ async function declareMcp(request, root) {
|
|
|
296
304
|
],
|
|
297
305
|
};
|
|
298
306
|
}
|
|
307
|
+
/**
|
|
308
|
+
* Declares an email in `app/emails/index.ts`, with the props its fields describe, and maps its
|
|
309
|
+
* template on the email registry through `lazy()`, imported from the package that provides
|
|
310
|
+
* `createMail`.
|
|
311
|
+
*/
|
|
312
|
+
async function declareEmail(request, root) {
|
|
313
|
+
let module = "app/emails/index";
|
|
314
|
+
let declarations = `${module}.ts`;
|
|
315
|
+
let { name } = request;
|
|
316
|
+
let initializer = await findInitializer(root, module, "createMail");
|
|
317
|
+
let instance = initializer === null ? "mail" : instanceOf(initializer, "createMail");
|
|
318
|
+
let local = initializer === null ? "emails" : (defaultLocal(initializer, module) ?? "emails");
|
|
319
|
+
let path = [...name.segments.slice(0, -1).map(camel), name.key];
|
|
320
|
+
let lines = (s) => request.options?.lines({ s }) ?? [`props: ${s}.object({}),`];
|
|
321
|
+
let valueWith = (callee, s) => `${callee}({\n${lines(s)
|
|
322
|
+
.map((line) => `\t${line}`)
|
|
323
|
+
.join("\n")}\n})`;
|
|
324
|
+
let handler = initializer === null ? `~/${request.handler}` : handlerSpecifier(initializer, request.handler);
|
|
325
|
+
let statement = (lazy) => `${instance}.map(${local}.${path.join(".")}, ${lazy}(() => import(${literal(handler)})));`;
|
|
326
|
+
let manual = (reason) => ({
|
|
327
|
+
kind: "manual",
|
|
328
|
+
path: path.join("."),
|
|
329
|
+
reason,
|
|
330
|
+
instructions: instructions(`${declarations}, in emails({ … })${groupNote(path)}`, `${path.at(-1)}: ${valueWith("email", "s")},`, initializer?.path ?? "the email registry's initializer", statement("lazy")),
|
|
331
|
+
});
|
|
332
|
+
if (initializer === null)
|
|
333
|
+
return manual("no initializer in app/initializers/ creates the email registry");
|
|
334
|
+
let file = await readSource(root, declarations);
|
|
335
|
+
if (isFailure(file))
|
|
336
|
+
return manual(`${declarations} could not be read`);
|
|
337
|
+
let mailSpecifier = file.data.references.find((reference) => reference.kind === "import" && reference.names.some((bound) => bound.imported === "emails"))?.specifier;
|
|
338
|
+
if (mailSpecifier === undefined)
|
|
339
|
+
return manual(`${declarations} does not import emails()`);
|
|
340
|
+
let builder = ensureImport(declarations, file.data.source, { kind: "named", name: "email" }, (specifier) => specifier === mailSpecifier, mailSpecifier, "email");
|
|
341
|
+
if (isFailure(builder))
|
|
342
|
+
return manual(builder.error.message);
|
|
343
|
+
let schema = ensureImport(declarations, builder.data.source, { kind: "namespace" }, (specifier) => specifier === "remix/data-schema", "remix/data-schema", "s");
|
|
344
|
+
if (isFailure(schema))
|
|
345
|
+
return manual(schema.error.message);
|
|
346
|
+
let added = addProperty(declarations, schema.data.source, {
|
|
347
|
+
call: "emails",
|
|
348
|
+
path: path.slice(0, -1),
|
|
349
|
+
key: name.key,
|
|
350
|
+
value: valueWith(builder.data.local, schema.data.local),
|
|
351
|
+
});
|
|
352
|
+
if (isFailure(added))
|
|
353
|
+
return manual(added.error.message);
|
|
354
|
+
let mailSource = initializer.references.find((reference) => reference.kind === "import" &&
|
|
355
|
+
reference.names.some((bound) => bound.imported === "createMail"))?.specifier;
|
|
356
|
+
if (mailSource === undefined)
|
|
357
|
+
return manual(`${initializer.path} does not import createMail`);
|
|
358
|
+
let lazy = ensureImport(initializer.path, initializer.source, { kind: "named", name: "lazy" }, (specifier) => specifier === mailSource, mailSource, "lazy");
|
|
359
|
+
if (isFailure(lazy))
|
|
360
|
+
return manual(lazy.error.message);
|
|
361
|
+
let withLazy = { ...initializer, source: lazy.data.source };
|
|
362
|
+
let mapped = mapAfter(withLazy, module, local, statement(lazy.data.local), `${instance}.map`, "createMail");
|
|
363
|
+
if (isFailure(mapped))
|
|
364
|
+
return manual(mapped.error.message);
|
|
365
|
+
return {
|
|
366
|
+
kind: "edits",
|
|
367
|
+
path: path.join("."),
|
|
368
|
+
edits: [
|
|
369
|
+
{ path: declarations, before: file.data.source, after: added.data.source },
|
|
370
|
+
{ path: initializer.path, before: initializer.source, after: mapped.data },
|
|
371
|
+
],
|
|
372
|
+
};
|
|
373
|
+
}
|
|
299
374
|
/**
|
|
300
375
|
* Declares a command in `app/commands/index.ts` and maps it on the command line. Each folder of
|
|
301
376
|
* the name is a `commands({ … })` group, created when missing, and the command's key is the
|
|
@@ -350,13 +425,28 @@ async function declareCommand(request, root) {
|
|
|
350
425
|
return manual(`\`${groups.slice(0, depth + 1).join(".")}\` is a command, not a group`);
|
|
351
426
|
}
|
|
352
427
|
}
|
|
353
|
-
let
|
|
428
|
+
let value = `${builder.data.local}({ description: ${description} })`;
|
|
429
|
+
if (request.options !== undefined) {
|
|
430
|
+
let locals = {};
|
|
431
|
+
for (let wanted of request.options.imports) {
|
|
432
|
+
let imported = wanted === "s"
|
|
433
|
+
? ensureImport(declarations, source, { kind: "namespace" }, (specifier) => specifier === "remix/data-schema", "remix/data-schema", "s")
|
|
434
|
+
: ensureImport(declarations, source, { kind: "named", name: wanted }, (specifier) => specifier === group.specifier, group.specifier, wanted);
|
|
435
|
+
if (isFailure(imported))
|
|
436
|
+
return manual(imported.error.message);
|
|
437
|
+
source = imported.data.source;
|
|
438
|
+
locals[wanted] = imported.data.local;
|
|
439
|
+
}
|
|
440
|
+
let lines = [`description: ${description},`, ...request.options.lines(locals)];
|
|
441
|
+
value = `${builder.data.local}({\n${indentTab(lines.join("\n"))}\n})`;
|
|
442
|
+
}
|
|
443
|
+
let entry = `${keyText(leaf)}: ${value}`;
|
|
354
444
|
let added = depth === groups.length
|
|
355
445
|
? addProperty(declarations, source, {
|
|
356
446
|
call: groupLocal,
|
|
357
447
|
path: groups,
|
|
358
448
|
key: leaf,
|
|
359
|
-
value
|
|
449
|
+
value,
|
|
360
450
|
})
|
|
361
451
|
: addProperty(declarations, source, {
|
|
362
452
|
call: groupLocal,
|
|
@@ -425,6 +515,192 @@ async function declareModel(request, root) {
|
|
|
425
515
|
edits: [{ path: declarations, before: file.data.source, after: added.data.source }],
|
|
426
516
|
};
|
|
427
517
|
}
|
|
518
|
+
/** The abilities of a resource `make policy` grants to `member`; its `read` goes to `everyone`. */
|
|
519
|
+
const WRITE_ACTIONS = ["create", "update", "delete"];
|
|
520
|
+
/**
|
|
521
|
+
* Declares a resource's `create`, `read`, `update` and `delete` abilities in
|
|
522
|
+
* `app/policies/abilities.ts` and grants them in `app/policies/policy.ts`, reads to `everyone` and
|
|
523
|
+
* writes to `member`. A refused read answers as not found, so a hidden row looks like a missing one.
|
|
524
|
+
*/
|
|
525
|
+
async function declarePolicy(request, root) {
|
|
526
|
+
let catalog = "app/policies/abilities.ts";
|
|
527
|
+
let policyFile = "app/policies/policy.ts";
|
|
528
|
+
let { name } = request;
|
|
529
|
+
let groups = name.segments.slice(0, -1).map(camel);
|
|
530
|
+
let path = [...groups, name.key];
|
|
531
|
+
let ability = path.join(".");
|
|
532
|
+
let id = name.segments.join("-");
|
|
533
|
+
let row = await findRow(root, name);
|
|
534
|
+
let rowType = row?.type ?? "{ id: string }";
|
|
535
|
+
let group = (abilityLocal, contextLocal, type) => {
|
|
536
|
+
let context = `${contextLocal}<{ ${name.key}: ${type} }>(${literal(name.key)})`;
|
|
537
|
+
return [
|
|
538
|
+
"{",
|
|
539
|
+
` create: ${abilityLocal}(),`,
|
|
540
|
+
` read: ${abilityLocal}({ context: ${context}, deniedAs: "notFound" }),`,
|
|
541
|
+
` update: ${abilityLocal}({ context: ${context} }),`,
|
|
542
|
+
` delete: ${abilityLocal}({ context: ${context} }),`,
|
|
543
|
+
"}",
|
|
544
|
+
].join("\n");
|
|
545
|
+
};
|
|
546
|
+
let reads = (allow) => `${allow}(${literal(`${ability}.read`)}, { id: ${literal(`${id}-read`)} })`;
|
|
547
|
+
let writes = (allow) => {
|
|
548
|
+
let names = WRITE_ACTIONS.map((action) => literal(`${ability}.${action}`)).join(", ");
|
|
549
|
+
return `${allow}([${names}], { id: ${literal(`${id}-write`)} })`;
|
|
550
|
+
};
|
|
551
|
+
let manual = (reason) => ({
|
|
552
|
+
kind: "manual",
|
|
553
|
+
path: ability,
|
|
554
|
+
reason,
|
|
555
|
+
instructions: [
|
|
556
|
+
...(row === null
|
|
557
|
+
? []
|
|
558
|
+
: [`In ${catalog}:`, indent(`import type { ${row.type} } from "~/${row.path}";`)]),
|
|
559
|
+
`In ${catalog}, in abilities({ … })${groupNote(path)}:`,
|
|
560
|
+
indent(`${name.key}: ${group("ability", "context", rowType)},`),
|
|
561
|
+
`In ${policyFile}, in definePolicy(…)'s everyone:`,
|
|
562
|
+
indent(`${reads("allow")},`),
|
|
563
|
+
`In ${policyFile}, in definePolicy(…)'s roles.member:`,
|
|
564
|
+
indent(`${writes("allow")},`),
|
|
565
|
+
].join("\n"),
|
|
566
|
+
});
|
|
567
|
+
let file = await readSource(root, catalog);
|
|
568
|
+
if (isFailure(file))
|
|
569
|
+
return manual(`${catalog} could not be read`);
|
|
570
|
+
let catalogImport = importOf(file.data, "abilities");
|
|
571
|
+
if (catalogImport === null)
|
|
572
|
+
return manual(`${catalog} does not import abilities()`);
|
|
573
|
+
let source = file.data.source;
|
|
574
|
+
let locals = {};
|
|
575
|
+
for (let helper of ["ability", "context"]) {
|
|
576
|
+
let imported = ensureImport(catalog, source, { kind: "named", name: helper }, (specifier) => specifier === catalogImport.specifier, catalogImport.specifier, helper);
|
|
577
|
+
if (isFailure(imported))
|
|
578
|
+
return manual(imported.error.message);
|
|
579
|
+
source = imported.data.source;
|
|
580
|
+
locals[helper] = imported.data.local;
|
|
581
|
+
}
|
|
582
|
+
let type = rowType;
|
|
583
|
+
if (row !== null) {
|
|
584
|
+
let aliased = await declaresAlias(root);
|
|
585
|
+
let specifier = specifierFor(catalog, row.path, staticSpecifiers(file.data), aliased);
|
|
586
|
+
let imported = ensureTypeImport(catalog, source, row.type, specifier);
|
|
587
|
+
if (isFailure(imported))
|
|
588
|
+
return manual(imported.error.message);
|
|
589
|
+
source = imported.data.source;
|
|
590
|
+
type = imported.data.local;
|
|
591
|
+
}
|
|
592
|
+
let declared = addProperty(catalog, source, {
|
|
593
|
+
call: catalogImport.local,
|
|
594
|
+
path: groups,
|
|
595
|
+
key: name.key,
|
|
596
|
+
value: group(locals.ability ?? "ability", locals.context ?? "context", type),
|
|
597
|
+
});
|
|
598
|
+
if (isFailure(declared))
|
|
599
|
+
return manual(declared.error.message);
|
|
600
|
+
let policy = await readSource(root, policyFile);
|
|
601
|
+
if (isFailure(policy))
|
|
602
|
+
return manual(`${policyFile} could not be read`);
|
|
603
|
+
let defined = importOf(policy.data, "definePolicy");
|
|
604
|
+
if (defined === null)
|
|
605
|
+
return manual(`${policyFile} does not import definePolicy()`);
|
|
606
|
+
let allow = ensureImport(policyFile, policy.data.source, { kind: "named", name: "allow" }, (specifier) => specifier === defined.specifier, defined.specifier, "allow");
|
|
607
|
+
if (isFailure(allow))
|
|
608
|
+
return manual(allow.error.message);
|
|
609
|
+
let granted = grant(policyFile, allow.data.source, defined.local, [["everyone"]], reads(allow.data.local));
|
|
610
|
+
if (isFailure(granted))
|
|
611
|
+
return manual(granted.error.message);
|
|
612
|
+
granted = grant(policyFile, granted.data, defined.local, [
|
|
613
|
+
["roles", "member"],
|
|
614
|
+
["roles", "member", "grants"],
|
|
615
|
+
], writes(allow.data.local));
|
|
616
|
+
if (isFailure(granted))
|
|
617
|
+
return manual(granted.error.message);
|
|
618
|
+
return {
|
|
619
|
+
kind: "edits",
|
|
620
|
+
path: ability,
|
|
621
|
+
edits: [
|
|
622
|
+
{ path: catalog, before: file.data.source, after: declared.data.source },
|
|
623
|
+
{ path: policyFile, before: policy.data.source, after: granted.data },
|
|
624
|
+
],
|
|
625
|
+
};
|
|
626
|
+
}
|
|
627
|
+
/**
|
|
628
|
+
* Whether `app/policies/abilities.ts` declares a group at `path` in its `abilities({ … })`, as
|
|
629
|
+
* `make policy` does for a resource. An unreadable catalog, or one without the call, declares none.
|
|
630
|
+
*/
|
|
631
|
+
export async function declaresAbilities(root, path) {
|
|
632
|
+
let catalog = "app/policies/abilities.ts";
|
|
633
|
+
let file = await readSource(root, catalog);
|
|
634
|
+
if (isFailure(file))
|
|
635
|
+
return false;
|
|
636
|
+
let call = importOf(file.data, "abilities");
|
|
637
|
+
if (call === null)
|
|
638
|
+
return false;
|
|
639
|
+
let [key = "", ...rest] = [...path].reverse();
|
|
640
|
+
let probe = addProperty(catalog, file.data.source, {
|
|
641
|
+
call: call.local,
|
|
642
|
+
path: rest.reverse(),
|
|
643
|
+
key,
|
|
644
|
+
value: "{}",
|
|
645
|
+
});
|
|
646
|
+
return isFailure(probe) && probe.error.code === "duplicate-key";
|
|
647
|
+
}
|
|
648
|
+
/**
|
|
649
|
+
* `source` with `value` appended to the first of `lists` the policy's options hold, each a key
|
|
650
|
+
* path in `definePolicy`'s second argument; with none of them, the first is created holding it.
|
|
651
|
+
*/
|
|
652
|
+
function grant(filename, source, call, lists, value) {
|
|
653
|
+
for (let path of lists) {
|
|
654
|
+
let added = addElement(filename, source, { call, argument: 1, path: [...path], value });
|
|
655
|
+
if (isSuccess(added))
|
|
656
|
+
return success(added.data.source);
|
|
657
|
+
if (added.error.code !== "not-found")
|
|
658
|
+
return added;
|
|
659
|
+
}
|
|
660
|
+
let [first = []] = lists;
|
|
661
|
+
let created = addProperty(filename, source, {
|
|
662
|
+
call,
|
|
663
|
+
argument: 1,
|
|
664
|
+
path: first.slice(0, -1),
|
|
665
|
+
key: first.at(-1) ?? "",
|
|
666
|
+
value: `[${value}]`,
|
|
667
|
+
});
|
|
668
|
+
if (isFailure(created))
|
|
669
|
+
return created;
|
|
670
|
+
return success(created.data.source);
|
|
671
|
+
}
|
|
672
|
+
/**
|
|
673
|
+
* The row type a resource's model exports, `PostRow` from `app/models/post.ts`, or `null` when
|
|
674
|
+
* the resource has no model or its model exports no such type.
|
|
675
|
+
*/
|
|
676
|
+
export async function findRow(root, name) {
|
|
677
|
+
let path = `app/models/${name.segments.join("/")}`;
|
|
678
|
+
let type = `${name.pascal}Row`;
|
|
679
|
+
for (let extension of [".ts", ".tsx"]) {
|
|
680
|
+
let source = await wrap(() => readFile(join(root, `${path}${extension}`), "utf8"));
|
|
681
|
+
if (isFailure(source))
|
|
682
|
+
continue;
|
|
683
|
+
let exported = new RegExp(`export\\s+(?:type|interface)\\s+${escape(type)}\\b`);
|
|
684
|
+
return exported.test(source.data) ? { path, type } : null;
|
|
685
|
+
}
|
|
686
|
+
return null;
|
|
687
|
+
}
|
|
688
|
+
/** The local name and specifier `file` imports the named export `name` under, or `null`. */
|
|
689
|
+
function importOf(file, name) {
|
|
690
|
+
for (let reference of file.references) {
|
|
691
|
+
if (reference.kind !== "import")
|
|
692
|
+
continue;
|
|
693
|
+
let bound = reference.names.find((each) => each.imported === name && !each.typeOnly);
|
|
694
|
+
if (bound !== undefined)
|
|
695
|
+
return { local: bound.local, specifier: reference.specifier };
|
|
696
|
+
}
|
|
697
|
+
return null;
|
|
698
|
+
}
|
|
699
|
+
/** Whether the project's `tsconfig.json` declares the `~/app/*` alias. */
|
|
700
|
+
async function declaresAlias(root) {
|
|
701
|
+
let tsconfig = await wrap(() => readFile(join(root, "tsconfig.json"), "utf8"));
|
|
702
|
+
return !isFailure(tsconfig) && tsconfig.data.includes('"~/app/*"');
|
|
703
|
+
}
|
|
428
704
|
/**
|
|
429
705
|
* A `commands({ … })` group holding `entry`, wrapped in one more group for each of `levels`,
|
|
430
706
|
* outermost first, for the folders of a command name that have no group yet.
|
|
@@ -485,8 +761,7 @@ async function findInitializer(root, module, factory, importing = "preferred") {
|
|
|
485
761
|
if (isSuccess(file))
|
|
486
762
|
files.push(file.data);
|
|
487
763
|
}
|
|
488
|
-
let
|
|
489
|
-
let aliased = !isFailure(tsconfig) && tsconfig.data.includes('"~/app/*"');
|
|
764
|
+
let aliased = await declaresAlias(root);
|
|
490
765
|
for (let file of files)
|
|
491
766
|
file.aliased = aliased;
|
|
492
767
|
let calling = factory === null ? null : new RegExp(`\\b${factory}\\(`);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `sdxc make email <name> field ...`: the fields become the email's props, typed by the
|
|
3
|
+
* conventions `make model` uses, and each one is declared in the props schema, rendered in the
|
|
4
|
+
* template's body, and given a sample its `preview()` and its test share.
|
|
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 `make email` writes from its fields. */
|
|
12
|
+
export interface EmailPlan {
|
|
13
|
+
/** The lines inside `email({ … })`, given the local the schema builder is imported as. */
|
|
14
|
+
declaration: (s: string) => string[];
|
|
15
|
+
/** The stub placeholders the template and its test read. */
|
|
16
|
+
placeholders: Record<string, string>;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Plans the email `name` with the props `specs` describe, or `heading` and `body` without any.
|
|
20
|
+
* A reference, an index and a unique field describe a table rather than an email, so each is
|
|
21
|
+
* refused naming the spec.
|
|
22
|
+
*/
|
|
23
|
+
export declare function planEmail(specs: readonly string[], name: Name): Result<EmailPlan, Error>;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `sdxc make email <name> field ...`: the fields become the email's props, typed by the
|
|
3
|
+
* conventions `make model` uses, and each one is declared in the props schema, rendered in the
|
|
4
|
+
* template's body, and given a sample its `preview()` and its test share.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { failure, isFailure, success } from "@sdxc/result";
|
|
10
|
+
import { parseFields } from "../scaffold/fields.js";
|
|
11
|
+
import { literal } from "./generate.js";
|
|
12
|
+
/** The fields an email given none is built from: a heading that is its subject, and a body. */
|
|
13
|
+
const DEFAULT_FIELDS = ["heading", "body:text"];
|
|
14
|
+
/** The `remix/data-schema` builder each field type's prop is declared with. */
|
|
15
|
+
const SCHEMAS = {
|
|
16
|
+
string: "string()",
|
|
17
|
+
text: "string()",
|
|
18
|
+
integer: "number()",
|
|
19
|
+
boolean: "boolean()",
|
|
20
|
+
date: "string()",
|
|
21
|
+
timestamp: "number()",
|
|
22
|
+
email: "string()",
|
|
23
|
+
url: "string()",
|
|
24
|
+
};
|
|
25
|
+
/** The types whose value is text, the first of which is the email's subject and heading. */
|
|
26
|
+
const TEXT_TYPES = new Set(["string", "text"]);
|
|
27
|
+
/**
|
|
28
|
+
* Plans the email `name` with the props `specs` describe, or `heading` and `body` without any.
|
|
29
|
+
* A reference, an index and a unique field describe a table rather than an email, so each is
|
|
30
|
+
* refused naming the spec.
|
|
31
|
+
*/
|
|
32
|
+
export function planEmail(specs, name) {
|
|
33
|
+
let parsed = parseFields(specs.length === 0 ? DEFAULT_FIELDS : specs);
|
|
34
|
+
if (isFailure(parsed))
|
|
35
|
+
return parsed;
|
|
36
|
+
for (let [position, field] of parsed.data.entries()) {
|
|
37
|
+
let spec = specs[position] ?? field.column;
|
|
38
|
+
if (field.type === "references") {
|
|
39
|
+
return failure(new Error(`"${spec}" references a table; an email's props hold values`));
|
|
40
|
+
}
|
|
41
|
+
if (field.index !== null) {
|
|
42
|
+
return failure(new Error(`"${spec}" indexes a column; an email's props have none`));
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
let fields = parsed.data;
|
|
46
|
+
let heading = fields.find((field) => TEXT_TYPES.has(field.type) && !field.nullable) ?? null;
|
|
47
|
+
let samples = new Map(fields.map((field) => [field.key, sample(field)]));
|
|
48
|
+
let preview = `{ ${fields.map((field) => `${field.key}: ${samples.get(field.key)}`).join(", ")} }`;
|
|
49
|
+
let shown = fields.find((field) => field !== heading && TEXT_TYPES.has(field.type)) ?? heading;
|
|
50
|
+
let subjectSample = heading === null ? literal(name.title) : (samples.get(heading.key) ?? "");
|
|
51
|
+
return success({
|
|
52
|
+
declaration: (s) => [
|
|
53
|
+
`props: ${s}.object({`,
|
|
54
|
+
...fields.map((field) => `\t${field.key}: ${schema(field, s)},`),
|
|
55
|
+
"}),",
|
|
56
|
+
],
|
|
57
|
+
placeholders: {
|
|
58
|
+
subject: heading === null
|
|
59
|
+
? `() => ${literal(name.title)}`
|
|
60
|
+
: `({ ${heading.key} }) => ${heading.key}`,
|
|
61
|
+
layoutTitle: heading === null ? literal(name.title) : `{handle.props.${heading.key}}`,
|
|
62
|
+
body: [...(heading === null ? [] : [heading]), ...fields.filter((field) => field !== heading)]
|
|
63
|
+
.map((field) => `\t\t\t${element(field, field === heading)}`)
|
|
64
|
+
.join("\n"),
|
|
65
|
+
preview,
|
|
66
|
+
sample: preview,
|
|
67
|
+
subjectSample,
|
|
68
|
+
textSample: shown === null ? subjectSample : (samples.get(shown.key) ?? subjectSample),
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
/** The prop's schema, optional when its spec ended in `?`. */
|
|
73
|
+
function schema(field, s) {
|
|
74
|
+
let builder = `${s}.${SCHEMAS[field.type]}`;
|
|
75
|
+
return field.nullable ? `${s}.optional(${builder})` : builder;
|
|
76
|
+
}
|
|
77
|
+
/** A value of the prop's type, distinct per text field so a test can tell them apart. */
|
|
78
|
+
function sample(field) {
|
|
79
|
+
if (field.type === "integer")
|
|
80
|
+
return "42";
|
|
81
|
+
if (field.type === "timestamp")
|
|
82
|
+
return "1769851800000";
|
|
83
|
+
if (field.type === "boolean")
|
|
84
|
+
return "true";
|
|
85
|
+
if (field.type === "date")
|
|
86
|
+
return literal("2026-01-31");
|
|
87
|
+
if (field.type === "email")
|
|
88
|
+
return literal("ada@example.com");
|
|
89
|
+
if (field.type === "url")
|
|
90
|
+
return literal("https://example.com/");
|
|
91
|
+
return literal(`Sample ${field.label.toLowerCase()}`);
|
|
92
|
+
}
|
|
93
|
+
/** The JSX one prop renders as, guarded when the prop is optional. */
|
|
94
|
+
function element(field, heading) {
|
|
95
|
+
let value = `handle.props.${field.key}`;
|
|
96
|
+
if (field.type === "boolean")
|
|
97
|
+
return `{${value} ? <Email.Text>${field.label}</Email.Text> : null}`;
|
|
98
|
+
let shown = shape(field, value, heading);
|
|
99
|
+
return field.nullable ? `{${value} === undefined ? null : ${shown}}` : shown;
|
|
100
|
+
}
|
|
101
|
+
/** The element a present prop renders as, by its type. */
|
|
102
|
+
function shape(field, value, heading) {
|
|
103
|
+
if (heading)
|
|
104
|
+
return `<Email.Heading>{${value}}</Email.Heading>`;
|
|
105
|
+
if (field.type === "url")
|
|
106
|
+
return `<Email.Button href={${value}}>${field.label}</Email.Button>`;
|
|
107
|
+
if (field.type === "email") {
|
|
108
|
+
return `<Email.Text>\n\t\t\t\t<Email.Link href={\`mailto:\${${value}}\`}>{${value}}</Email.Link>\n\t\t\t</Email.Text>`;
|
|
109
|
+
}
|
|
110
|
+
return `<Email.Text>{${value}}</Email.Text>`;
|
|
111
|
+
}
|