@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,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lists every route of a project as `sdxc routes` prints it: each route contract in `routes/`
|
|
3
|
+
* evaluated to full patterns, joined with what the router initializers in `app/initializers/`
|
|
4
|
+
* map onto it. Reads source only, so it runs without bindings, a build or an install.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { ControllerRef, MiddlewareEntry, Router } from "./routers.js";
|
|
10
|
+
export type { ControllerRef, MiddlewareEntry, Router } from "./routers.js";
|
|
11
|
+
/** One route, and what runs for it; the shape `sdxc routes --json` prints. */
|
|
12
|
+
export interface RouteEntry {
|
|
13
|
+
/** `GET`, `POST`, …, or `ANY` for a bare pattern. */
|
|
14
|
+
method: string;
|
|
15
|
+
pattern: string;
|
|
16
|
+
/** The route contract it is declared in; a contract composed into another reports the outer one. */
|
|
17
|
+
contract: string;
|
|
18
|
+
/** Its key path in the contract, `posts.show`. */
|
|
19
|
+
name: string;
|
|
20
|
+
/** The initializer that maps it, or `null` when no router does. */
|
|
21
|
+
router: string | null;
|
|
22
|
+
/** `null` when no router maps it; its `file` is `null` when the mapping cannot be followed. */
|
|
23
|
+
controller: ControllerRef | null;
|
|
24
|
+
middleware: {
|
|
25
|
+
/** The router's global middleware, which runs first. */
|
|
26
|
+
router: MiddlewareEntry[];
|
|
27
|
+
/** `lazy()`'s second argument, which answers before the controller loads. */
|
|
28
|
+
guards: MiddlewareEntry[];
|
|
29
|
+
/** The controller's middleware, then the action's own. */
|
|
30
|
+
controller: MiddlewareEntry[];
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/** Everything `sdxc routes` prints. */
|
|
34
|
+
export interface RouteListing {
|
|
35
|
+
routes: RouteEntry[];
|
|
36
|
+
/** Every router found, for printing each one's global middleware once. */
|
|
37
|
+
routers: RouterSummary[];
|
|
38
|
+
/** Problems the reader met, each naming its file. */
|
|
39
|
+
diagnostics: string[];
|
|
40
|
+
}
|
|
41
|
+
/** A router as the listing reports it. */
|
|
42
|
+
export interface RouterSummary extends Pick<Router, "file" | "middleware"> {
|
|
43
|
+
/**
|
|
44
|
+
* Each `map()` whose handler loads with the router, as its target and handler, or as its target
|
|
45
|
+
* and the action key path when only that action does, since that code loads on every request.
|
|
46
|
+
*/
|
|
47
|
+
eager: string[];
|
|
48
|
+
}
|
|
49
|
+
/** Lists the routes of the project at `root`. */
|
|
50
|
+
export declare function listRoutes(root: string): Promise<RouteListing>;
|
|
51
|
+
/**
|
|
52
|
+
* The sources under `app/http/controllers/` that no module in `app/` or `bootstrap/` imports
|
|
53
|
+
* statically. A helper shared by controllers, or a handler the router takes as an option, is
|
|
54
|
+
* imported that way, so what is left is reached by a route's `lazy()` import or by nothing.
|
|
55
|
+
*/
|
|
56
|
+
export declare function unimportedControllers(root: string): Promise<string[]>;
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lists every route of a project as `sdxc routes` prints it: each route contract in `routes/`
|
|
3
|
+
* evaluated to full patterns, joined with what the router initializers in `app/initializers/`
|
|
4
|
+
* map onto it. Reads source only, so it runs without bindings, a build or an install.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { readdir } from "node:fs/promises";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
import { isFailure, wrap } from "@sdxc/result";
|
|
12
|
+
import { Project } from "./modules.js";
|
|
13
|
+
import { readRouters } from "./routers.js";
|
|
14
|
+
/** Lists the routes of the project at `root`. */
|
|
15
|
+
export async function listRoutes(root) {
|
|
16
|
+
let project = new Project(root);
|
|
17
|
+
let contracts = await readContracts(project);
|
|
18
|
+
let identities = new Map();
|
|
19
|
+
for (let contract of contracts) {
|
|
20
|
+
for (let [route, path] of leaves(contract.value, [])) {
|
|
21
|
+
if (!identities.has(route))
|
|
22
|
+
identities.set(route, { contract: contract.path, name: path.join(".") });
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
let routers = [];
|
|
26
|
+
for (let file of await sourceFiles(project.root, "app/initializers")) {
|
|
27
|
+
let module = await project.load(file);
|
|
28
|
+
if (module !== null)
|
|
29
|
+
routers.push(...(await readRouters(project, module)));
|
|
30
|
+
}
|
|
31
|
+
let mapped = new Map();
|
|
32
|
+
let order = [];
|
|
33
|
+
for (let router of routers) {
|
|
34
|
+
for (let mapping of router.mappings) {
|
|
35
|
+
let reached = leaves(mapping.target, []);
|
|
36
|
+
if (reached.length === 0) {
|
|
37
|
+
project.diagnostics.push(`${router.file}: ${mapping.text} is not a route sdxc routes can read`);
|
|
38
|
+
}
|
|
39
|
+
for (let [route, path] of reached) {
|
|
40
|
+
let list = mapped.get(route) ?? [];
|
|
41
|
+
list.push({ router, handler: mapping.handler, path });
|
|
42
|
+
mapped.set(route, list);
|
|
43
|
+
let contract = identities.get(route)?.contract;
|
|
44
|
+
if (contract !== undefined && !order.includes(contract))
|
|
45
|
+
order.push(contract);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
let roots = contracts.filter((contract) => contract.root);
|
|
50
|
+
let listed = [
|
|
51
|
+
...order.flatMap((path) => contracts.filter((contract) => contract.path === path)),
|
|
52
|
+
...roots.filter((contract) => !order.includes(contract.path)),
|
|
53
|
+
];
|
|
54
|
+
let routes = [];
|
|
55
|
+
for (let contract of listed) {
|
|
56
|
+
for (let [route] of leaves(contract.value, [])) {
|
|
57
|
+
let identity = identities.get(route);
|
|
58
|
+
if (identity === undefined || identity.contract !== contract.path)
|
|
59
|
+
continue;
|
|
60
|
+
let mappings = mapped.get(route) ?? [];
|
|
61
|
+
if (mappings.length === 0 && !contract.root)
|
|
62
|
+
continue;
|
|
63
|
+
let base = { method: route.method, pattern: route.pattern, ...identity };
|
|
64
|
+
if (mappings.length === 0) {
|
|
65
|
+
routes.push({
|
|
66
|
+
...base,
|
|
67
|
+
router: null,
|
|
68
|
+
controller: null,
|
|
69
|
+
middleware: { router: [], guards: [], controller: [] },
|
|
70
|
+
});
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
for (let mapping of mappings) {
|
|
74
|
+
let handler = await mapping.handler.forAction(mapping.path);
|
|
75
|
+
if (handler.controller.file === null) {
|
|
76
|
+
project.diagnostics.push(`${mapping.router.file}: ${mapping.handler.text} for ${identity.name} could not be followed to a controller`);
|
|
77
|
+
}
|
|
78
|
+
routes.push({
|
|
79
|
+
...base,
|
|
80
|
+
router: mapping.router.file,
|
|
81
|
+
controller: handler.controller,
|
|
82
|
+
middleware: {
|
|
83
|
+
router: mapping.router.middleware,
|
|
84
|
+
guards: handler.guards,
|
|
85
|
+
controller: handler.middleware,
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
routes,
|
|
93
|
+
routers: routers.map((router) => ({
|
|
94
|
+
file: router.file,
|
|
95
|
+
middleware: router.middleware,
|
|
96
|
+
eager: router.mappings.flatMap((mapping) => mapping.handler.eager.map((path) => path.length === 0
|
|
97
|
+
? `${mapping.text}, ${mapping.handler.text}`
|
|
98
|
+
: `${mapping.text} action ${path.join(".")}`)),
|
|
99
|
+
})),
|
|
100
|
+
diagnostics: [...new Set(project.diagnostics)],
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* The sources under `app/http/controllers/` that no module in `app/` or `bootstrap/` imports
|
|
105
|
+
* statically. A helper shared by controllers, or a handler the router takes as an option, is
|
|
106
|
+
* imported that way, so what is left is reached by a route's `lazy()` import or by nothing.
|
|
107
|
+
*/
|
|
108
|
+
export async function unimportedControllers(root) {
|
|
109
|
+
let project = new Project(root);
|
|
110
|
+
let imported = new Set();
|
|
111
|
+
let sources = [...(await sourceFiles(root, "app")), ...(await sourceFiles(root, "bootstrap"))];
|
|
112
|
+
for (let file of sources) {
|
|
113
|
+
let module = await project.load(file);
|
|
114
|
+
if (module === null)
|
|
115
|
+
continue;
|
|
116
|
+
for (let specifier of module.specifiers) {
|
|
117
|
+
let target = await project.resolve(file, specifier);
|
|
118
|
+
if (target !== null && target !== file)
|
|
119
|
+
imported.add(target);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
let controllers = await sourceFiles(root, "app/http/controllers");
|
|
123
|
+
return controllers.filter((file) => !imported.has(file));
|
|
124
|
+
}
|
|
125
|
+
/** Every route contract in `routes/`, in path order. */
|
|
126
|
+
async function readContracts(project) {
|
|
127
|
+
let found = [];
|
|
128
|
+
for (let file of await sourceFiles(project.root, "routes")) {
|
|
129
|
+
let module = await project.load(file);
|
|
130
|
+
if (module === null || module.declarations.defaultExport === null)
|
|
131
|
+
continue;
|
|
132
|
+
let value = await project.exportValue(module, "default");
|
|
133
|
+
if (value.kind === "map")
|
|
134
|
+
found.push({ module, value });
|
|
135
|
+
}
|
|
136
|
+
let imported = new Set();
|
|
137
|
+
for (let { module } of found) {
|
|
138
|
+
for (let specifier of module.specifiers) {
|
|
139
|
+
let file = await project.resolve(module.path, specifier);
|
|
140
|
+
if (file !== null && file !== module.path)
|
|
141
|
+
imported.add(file);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return found.map(({ module, value }) => ({
|
|
145
|
+
path: module.path,
|
|
146
|
+
value,
|
|
147
|
+
root: !imported.has(module.path),
|
|
148
|
+
}));
|
|
149
|
+
}
|
|
150
|
+
/** Every route under `value` with its key path, in declaration order. */
|
|
151
|
+
function leaves(value, path) {
|
|
152
|
+
if (value.kind === "route")
|
|
153
|
+
return [[value, path]];
|
|
154
|
+
if (value.kind !== "map")
|
|
155
|
+
return [];
|
|
156
|
+
return [...value.entries].flatMap(([key, entry]) => leaves(entry, [...path, key]));
|
|
157
|
+
}
|
|
158
|
+
/** The TypeScript and JavaScript sources under a project folder, tests left out, sorted. */
|
|
159
|
+
async function sourceFiles(root, folder) {
|
|
160
|
+
let entries = await wrap(() => readdir(join(root, folder), { recursive: true }));
|
|
161
|
+
if (isFailure(entries))
|
|
162
|
+
return [];
|
|
163
|
+
return entries.data
|
|
164
|
+
.map((entry) => `${folder}/${entry.split("\\").join("/")}`)
|
|
165
|
+
.filter((path) => /\.[cm]?[jt]sx?$/.test(path) && !/\.test\.[jt]sx?$/.test(path) && !path.endsWith(".d.ts"))
|
|
166
|
+
.sort();
|
|
167
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The project's modules as `sdxc routes` reads them: each file scanned once, its imports
|
|
3
|
+
* resolved to project files or packages, and its route declarations evaluated with the
|
|
4
|
+
* `remix/routes` helpers' rules, so a contract's routes come out with full patterns.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { Declarations, Node } from "./parse.js";
|
|
10
|
+
import { Reader } from "./parse.js";
|
|
11
|
+
/** A value a route declaration evaluates to. */
|
|
12
|
+
export type Value = RouteValue | MapValue | StringValue | ArrayValue | HelperValue | OpaqueValue;
|
|
13
|
+
/** One route; its object identity is what a router's mapping and a contract's listing share. */
|
|
14
|
+
export interface RouteValue {
|
|
15
|
+
kind: "route";
|
|
16
|
+
/** `GET`, `POST`, … or `ANY`. */
|
|
17
|
+
method: string;
|
|
18
|
+
pattern: string;
|
|
19
|
+
}
|
|
20
|
+
/** A route map or plain object, its entries in declaration order. */
|
|
21
|
+
export interface MapValue {
|
|
22
|
+
kind: "map";
|
|
23
|
+
entries: Map<string, Value>;
|
|
24
|
+
}
|
|
25
|
+
/** A string, which a route helper reads as a pattern, a method or an option. */
|
|
26
|
+
export interface StringValue {
|
|
27
|
+
kind: "string";
|
|
28
|
+
value: string;
|
|
29
|
+
}
|
|
30
|
+
/** An array, with spread arrays flattened into it. */
|
|
31
|
+
export interface ArrayValue {
|
|
32
|
+
kind: "array";
|
|
33
|
+
items: Value[];
|
|
34
|
+
}
|
|
35
|
+
/** A `remix/routes` helper, by its canonical name (`route`, `get`, `form`, `resources`, …). */
|
|
36
|
+
export interface HelperValue {
|
|
37
|
+
kind: "helper";
|
|
38
|
+
name: string;
|
|
39
|
+
}
|
|
40
|
+
/** A value the reader does not evaluate. */
|
|
41
|
+
export interface OpaqueValue {
|
|
42
|
+
kind: "opaque";
|
|
43
|
+
}
|
|
44
|
+
/** What an identifier in a module refers to. */
|
|
45
|
+
export type Binding = {
|
|
46
|
+
kind: "import";
|
|
47
|
+
specifier: string;
|
|
48
|
+
imported: string;
|
|
49
|
+
file: string | null;
|
|
50
|
+
} | {
|
|
51
|
+
kind: "local";
|
|
52
|
+
node: Node;
|
|
53
|
+
} | {
|
|
54
|
+
kind: "unknown";
|
|
55
|
+
};
|
|
56
|
+
/** One scanned project file. */
|
|
57
|
+
export interface Module {
|
|
58
|
+
/** Project-relative, with its extension. */
|
|
59
|
+
path: string;
|
|
60
|
+
reader: Reader;
|
|
61
|
+
declarations: Declarations;
|
|
62
|
+
/** Local name to the module and export it is imported from. */
|
|
63
|
+
imports: Map<string, {
|
|
64
|
+
specifier: string;
|
|
65
|
+
imported: string;
|
|
66
|
+
}>;
|
|
67
|
+
/** Exported name to the module and export it is re-exported from. */
|
|
68
|
+
reexports: Map<string, {
|
|
69
|
+
specifier: string;
|
|
70
|
+
imported: string;
|
|
71
|
+
}>;
|
|
72
|
+
/** Every static import and re-export specifier, for telling which modules a file depends on. */
|
|
73
|
+
specifiers: string[];
|
|
74
|
+
/** Evaluated top-level values by local name, shared by every reference to them. */
|
|
75
|
+
values: Map<string, Value>;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* The modules of the project at `root`, loaded on demand and kept for the run, so a contract
|
|
79
|
+
* imported by a router and by another contract evaluates to the same route objects.
|
|
80
|
+
*/
|
|
81
|
+
export declare class Project {
|
|
82
|
+
/** The project directory every module path is relative to. */
|
|
83
|
+
readonly root: string;
|
|
84
|
+
/** Problems worth telling the user, each naming the file it concerns. */
|
|
85
|
+
readonly diagnostics: string[];
|
|
86
|
+
private readonly modules;
|
|
87
|
+
/** Reads nothing until a module is asked for. */
|
|
88
|
+
constructor(root: string);
|
|
89
|
+
/** The module at a project-relative path, or `null` when it cannot be read or scanned. */
|
|
90
|
+
load(path: string): Promise<Module | null>;
|
|
91
|
+
/**
|
|
92
|
+
* The project file `specifier` names from `from`, with its extension; `null` for a package.
|
|
93
|
+
* `~/` is the project root, the alias every app's `tsconfig.json` declares.
|
|
94
|
+
*/
|
|
95
|
+
resolve(from: string, specifier: string): Promise<string | null>;
|
|
96
|
+
/** What `name` refers to at the top level of `module`. */
|
|
97
|
+
bind(module: Module, name: string): Promise<Binding>;
|
|
98
|
+
/**
|
|
99
|
+
* The node `module` exports as `name` (`default` included), following re-exports and
|
|
100
|
+
* `export default identifier`; `null` when it cannot be found.
|
|
101
|
+
*/
|
|
102
|
+
exported(module: Module, name: string): Promise<{
|
|
103
|
+
module: Module;
|
|
104
|
+
node: Node;
|
|
105
|
+
} | null>;
|
|
106
|
+
/** The value `module` exports as `name`. */
|
|
107
|
+
exportValue(module: Module, name: string): Promise<Value>;
|
|
108
|
+
/** Evaluates a route declaration expression in `module`. */
|
|
109
|
+
evaluate(node: Node, module: Module): Promise<Value>;
|
|
110
|
+
/** The value of a top-level name: a `remix/routes` helper, an import, or a local `const`. */
|
|
111
|
+
private identifier;
|
|
112
|
+
/** The node another module exports, found through an import or re-export of `from`. */
|
|
113
|
+
private exportedFrom;
|
|
114
|
+
/** Reads and scans one file; a file that does not scan is a diagnostic. */
|
|
115
|
+
private read;
|
|
116
|
+
}
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The project's modules as `sdxc routes` reads them: each file scanned once, its imports
|
|
3
|
+
* resolved to project files or packages, and its route declarations evaluated with the
|
|
4
|
+
* `remix/routes` helpers' rules, so a contract's routes come out with full patterns.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { readFile, stat } from "node:fs/promises";
|
|
10
|
+
import { join, posix } from "node:path";
|
|
11
|
+
import { findModuleReferences, tokenize } from "@sdxc/codemod";
|
|
12
|
+
import { isFailure, isSuccess, wrap } from "@sdxc/result";
|
|
13
|
+
import { applyHelper, helperName } from "./helpers.js";
|
|
14
|
+
import { Reader } from "./parse.js";
|
|
15
|
+
/** The extensions a specifier without one may resolve to, in the order bundlers try them. */
|
|
16
|
+
const EXTENSIONS = [".ts", ".tsx", ".js", ".jsx", ".mts", "/index.ts", "/index.tsx", "/index.js"];
|
|
17
|
+
/** The opaque value, shared since it carries nothing. */
|
|
18
|
+
const OPAQUE = { kind: "opaque" };
|
|
19
|
+
/**
|
|
20
|
+
* The modules of the project at `root`, loaded on demand and kept for the run, so a contract
|
|
21
|
+
* imported by a router and by another contract evaluates to the same route objects.
|
|
22
|
+
*/
|
|
23
|
+
export class Project {
|
|
24
|
+
/** The project directory every module path is relative to. */
|
|
25
|
+
root;
|
|
26
|
+
/** Problems worth telling the user, each naming the file it concerns. */
|
|
27
|
+
diagnostics = [];
|
|
28
|
+
modules = new Map();
|
|
29
|
+
/** Reads nothing until a module is asked for. */
|
|
30
|
+
constructor(root) {
|
|
31
|
+
this.root = root;
|
|
32
|
+
}
|
|
33
|
+
/** The module at a project-relative path, or `null` when it cannot be read or scanned. */
|
|
34
|
+
load(path) {
|
|
35
|
+
let cached = this.modules.get(path);
|
|
36
|
+
if (cached !== undefined)
|
|
37
|
+
return cached;
|
|
38
|
+
let loading = this.read(path);
|
|
39
|
+
this.modules.set(path, loading);
|
|
40
|
+
return loading;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The project file `specifier` names from `from`, with its extension; `null` for a package.
|
|
44
|
+
* `~/` is the project root, the alias every app's `tsconfig.json` declares.
|
|
45
|
+
*/
|
|
46
|
+
async resolve(from, specifier) {
|
|
47
|
+
let base;
|
|
48
|
+
if (specifier.startsWith("~/"))
|
|
49
|
+
base = specifier.slice(2);
|
|
50
|
+
else if (specifier.startsWith("./") || specifier.startsWith("../")) {
|
|
51
|
+
base = posix.normalize(posix.join(posix.dirname(from), specifier));
|
|
52
|
+
}
|
|
53
|
+
else
|
|
54
|
+
return null;
|
|
55
|
+
let candidates = /\.[cm]?[jt]sx?$/.test(base)
|
|
56
|
+
? [base, base.replace(/\.js$/, ".ts"), base.replace(/\.js$/, ".tsx")]
|
|
57
|
+
: EXTENSIONS.map((extension) => `${base}${extension}`);
|
|
58
|
+
for (let candidate of candidates) {
|
|
59
|
+
let found = await wrap(() => stat(join(this.root, candidate)));
|
|
60
|
+
if (isSuccess(found) && found.data.isFile())
|
|
61
|
+
return candidate;
|
|
62
|
+
}
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
/** What `name` refers to at the top level of `module`. */
|
|
66
|
+
async bind(module, name) {
|
|
67
|
+
let imported = module.imports.get(name);
|
|
68
|
+
if (imported !== undefined) {
|
|
69
|
+
return {
|
|
70
|
+
kind: "import",
|
|
71
|
+
...imported,
|
|
72
|
+
file: await this.resolve(module.path, imported.specifier),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
let local = module.declarations.locals.get(name);
|
|
76
|
+
if (local !== undefined)
|
|
77
|
+
return { kind: "local", node: local };
|
|
78
|
+
return { kind: "unknown" };
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* The node `module` exports as `name` (`default` included), following re-exports and
|
|
82
|
+
* `export default identifier`; `null` when it cannot be found.
|
|
83
|
+
*/
|
|
84
|
+
async exported(module, name) {
|
|
85
|
+
if (name === "default" && module.declarations.defaultExport !== null) {
|
|
86
|
+
return { module, node: module.declarations.defaultExport };
|
|
87
|
+
}
|
|
88
|
+
let local = module.declarations.exports.get(name);
|
|
89
|
+
if (local !== undefined) {
|
|
90
|
+
let node = module.declarations.locals.get(local);
|
|
91
|
+
if (node !== undefined)
|
|
92
|
+
return { module, node };
|
|
93
|
+
let imported = module.imports.get(local);
|
|
94
|
+
if (imported !== undefined)
|
|
95
|
+
return this.exportedFrom(module, imported);
|
|
96
|
+
}
|
|
97
|
+
let reexport = module.reexports.get(name);
|
|
98
|
+
if (reexport !== undefined)
|
|
99
|
+
return this.exportedFrom(module, reexport);
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
/** The value `module` exports as `name`. */
|
|
103
|
+
async exportValue(module, name) {
|
|
104
|
+
let key = `export:${name}`;
|
|
105
|
+
let cached = module.values.get(key);
|
|
106
|
+
if (cached !== undefined)
|
|
107
|
+
return cached;
|
|
108
|
+
module.values.set(key, OPAQUE);
|
|
109
|
+
let found = await this.exported(module, name);
|
|
110
|
+
let value = found === null ? OPAQUE : await this.evaluate(found.node, found.module);
|
|
111
|
+
module.values.set(key, value);
|
|
112
|
+
return value;
|
|
113
|
+
}
|
|
114
|
+
/** Evaluates a route declaration expression in `module`. */
|
|
115
|
+
async evaluate(node, module) {
|
|
116
|
+
switch (node.kind) {
|
|
117
|
+
case "string":
|
|
118
|
+
return { kind: "string", value: node.value };
|
|
119
|
+
case "identifier":
|
|
120
|
+
return this.identifier(node.name, module);
|
|
121
|
+
case "member": {
|
|
122
|
+
let object = await this.evaluate(node.object, module);
|
|
123
|
+
return object.kind === "map" ? (object.entries.get(node.property) ?? OPAQUE) : OPAQUE;
|
|
124
|
+
}
|
|
125
|
+
case "call": {
|
|
126
|
+
let callee = await this.evaluate(node.callee, module);
|
|
127
|
+
if (callee.kind !== "helper")
|
|
128
|
+
return OPAQUE;
|
|
129
|
+
let args = [];
|
|
130
|
+
for (let argument of node.args) {
|
|
131
|
+
if (argument.spread)
|
|
132
|
+
return OPAQUE;
|
|
133
|
+
args.push(await this.evaluate(argument.node, module));
|
|
134
|
+
}
|
|
135
|
+
return applyHelper(callee.name, args, (message) => this.diagnostics.push(`${module.path}: ${message}`));
|
|
136
|
+
}
|
|
137
|
+
case "object": {
|
|
138
|
+
let entries = new Map();
|
|
139
|
+
for (let entry of node.entries) {
|
|
140
|
+
if ("spread" in entry) {
|
|
141
|
+
let spread = await this.evaluate(entry.spread, module);
|
|
142
|
+
if (spread.kind === "map")
|
|
143
|
+
for (let [key, value] of spread.entries)
|
|
144
|
+
entries.set(key, value);
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
entries.set(entry.key, await this.evaluate(entry.value, module));
|
|
148
|
+
}
|
|
149
|
+
return { kind: "map", entries };
|
|
150
|
+
}
|
|
151
|
+
case "array": {
|
|
152
|
+
let items = [];
|
|
153
|
+
for (let element of node.elements) {
|
|
154
|
+
let value = await this.evaluate(element.node, module);
|
|
155
|
+
if (element.spread && value.kind === "array")
|
|
156
|
+
items.push(...value.items);
|
|
157
|
+
else if (!element.spread)
|
|
158
|
+
items.push(value);
|
|
159
|
+
}
|
|
160
|
+
return { kind: "array", items };
|
|
161
|
+
}
|
|
162
|
+
default:
|
|
163
|
+
return OPAQUE;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/** The value of a top-level name: a `remix/routes` helper, an import, or a local `const`. */
|
|
167
|
+
async identifier(name, module) {
|
|
168
|
+
let binding = await this.bind(module, name);
|
|
169
|
+
if (binding.kind === "import") {
|
|
170
|
+
let helper = helperName(binding.specifier, binding.imported);
|
|
171
|
+
if (helper !== null)
|
|
172
|
+
return { kind: "helper", name: helper };
|
|
173
|
+
if (binding.file === null)
|
|
174
|
+
return OPAQUE;
|
|
175
|
+
let target = await this.load(binding.file);
|
|
176
|
+
return target === null ? OPAQUE : this.exportValue(target, binding.imported);
|
|
177
|
+
}
|
|
178
|
+
if (binding.kind !== "local")
|
|
179
|
+
return OPAQUE;
|
|
180
|
+
let cached = module.values.get(name);
|
|
181
|
+
if (cached !== undefined)
|
|
182
|
+
return cached;
|
|
183
|
+
module.values.set(name, OPAQUE);
|
|
184
|
+
let value = await this.evaluate(binding.node, module);
|
|
185
|
+
module.values.set(name, value);
|
|
186
|
+
return value;
|
|
187
|
+
}
|
|
188
|
+
/** The node another module exports, found through an import or re-export of `from`. */
|
|
189
|
+
async exportedFrom(from, reference) {
|
|
190
|
+
let file = await this.resolve(from.path, reference.specifier);
|
|
191
|
+
if (file === null)
|
|
192
|
+
return null;
|
|
193
|
+
let target = await this.load(file);
|
|
194
|
+
return target === null ? null : this.exported(target, reference.imported);
|
|
195
|
+
}
|
|
196
|
+
/** Reads and scans one file; a file that does not scan is a diagnostic. */
|
|
197
|
+
async read(path) {
|
|
198
|
+
let source = await wrap(() => readFile(join(this.root, path), "utf8"));
|
|
199
|
+
if (isFailure(source))
|
|
200
|
+
return null;
|
|
201
|
+
let tokens = tokenize(path, source.data);
|
|
202
|
+
let references = findModuleReferences(path, source.data);
|
|
203
|
+
if (isFailure(tokens) || isFailure(references)) {
|
|
204
|
+
let error = isFailure(tokens)
|
|
205
|
+
? tokens.error
|
|
206
|
+
: isFailure(references)
|
|
207
|
+
? references.error
|
|
208
|
+
: null;
|
|
209
|
+
this.diagnostics.push(`${path}: could not be read (${error?.message ?? "unknown error"})`);
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
let reader = new Reader(tokens.data, source.data);
|
|
213
|
+
let imports = new Map();
|
|
214
|
+
let reexports = new Map();
|
|
215
|
+
let specifiers = [];
|
|
216
|
+
for (let reference of references.data) {
|
|
217
|
+
if (reference.kind !== "import" && reference.kind !== "export")
|
|
218
|
+
continue;
|
|
219
|
+
specifiers.push(reference.specifier);
|
|
220
|
+
for (let bound of reference.names) {
|
|
221
|
+
let target = reference.kind === "import" ? imports : reexports;
|
|
222
|
+
target.set(bound.local, { specifier: reference.specifier, imported: bound.imported });
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return {
|
|
226
|
+
path,
|
|
227
|
+
reader,
|
|
228
|
+
declarations: reader.declarations(),
|
|
229
|
+
imports,
|
|
230
|
+
reexports,
|
|
231
|
+
specifiers,
|
|
232
|
+
values: new Map(),
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
}
|