@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,613 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What one `sdxc scaffold` run does: renders every file a resource needs from its stubs, runs
|
|
3
|
+
* each declaration `make` would run in a staged copy of the project so they build on one another,
|
|
4
|
+
* and only then writes. A refusal anywhere in the plan leaves the project as it was.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { cp, mkdir, mkdtemp, readdir, readFile, rm, stat, writeFile } from "node:fs/promises";
|
|
10
|
+
import { tmpdir } from "node:os";
|
|
11
|
+
import { dirname, join } from "node:path";
|
|
12
|
+
import { failure, isFailure, isSuccess, success, wrap } from "@sdxc/result";
|
|
13
|
+
import { headerTags, resolveAuthor } from "../author.js";
|
|
14
|
+
import { missingDependencies } from "../dependencies.js";
|
|
15
|
+
import { unifiedDiff } from "../diff.js";
|
|
16
|
+
import { formatFiles } from "../format.js";
|
|
17
|
+
import { declare, declaresAbilities } from "../make/declare.js";
|
|
18
|
+
import { SCAFFOLD_PIECES } from "../make/kinds.js";
|
|
19
|
+
import { chainFolder, nextMigrationFile } from "../make/migrations.js";
|
|
20
|
+
import { parseName } from "../make/names.js";
|
|
21
|
+
import { loadStub, renderStub } from "../make/stubs.js";
|
|
22
|
+
import { lockFileAtOrAbove, packageManager } from "../package-manager.js";
|
|
23
|
+
import { readManifest } from "../upgrade/project.js";
|
|
24
|
+
import { parseFields, pluralize } from "./fields.js";
|
|
25
|
+
import { apiOperationNames, apiOperations, flagProperties, jsonProperties, localeBlock, placeholders, } from "./render.js";
|
|
26
|
+
/** The endpoints `--api` writes, in the order the contract declares them. */
|
|
27
|
+
const API_ACTIONS = ["index", "show", "create", "update", "destroy"];
|
|
28
|
+
/** The route contract `--api` declares its endpoints in, whose module holds their operations. */
|
|
29
|
+
const API_CONTRACT = "api";
|
|
30
|
+
/** The module that builds the OpenAPI document from the contract's operations. */
|
|
31
|
+
const OPENAPI_INITIALIZER = "app/initializers/openapi.ts";
|
|
32
|
+
/** What the project folders the declarations read are copied from, relative to the root. */
|
|
33
|
+
const STAGED = ["app", "routes", "tsconfig.json"];
|
|
34
|
+
/** The copy a scaffold adds to, one file per language. */
|
|
35
|
+
const LOCALES = "resources/locales";
|
|
36
|
+
/**
|
|
37
|
+
* Plans and writes the scaffold `input` describes in `project`, printing each file written and
|
|
38
|
+
* edited and each package added; under `dryRun` it prints every file and the diff of every edit
|
|
39
|
+
* instead. A declaration that cannot be placed is printed for the user to make, as `make` does.
|
|
40
|
+
*
|
|
41
|
+
* @param packageRoot - The `@sdxc/kit` package, whose `stubs/` holds the built-in stubs.
|
|
42
|
+
*/
|
|
43
|
+
export async function scaffold(ctx, project, packageRoot, input) {
|
|
44
|
+
let { root, config } = project;
|
|
45
|
+
let resource = parseResource(input);
|
|
46
|
+
if (isFailure(resource))
|
|
47
|
+
return resource;
|
|
48
|
+
let tests = input.test ?? config.make.tests;
|
|
49
|
+
let skip = new Set(input.skip ?? []);
|
|
50
|
+
let missing = await skippedButNeeded(root, resource.data, input, skip, tests);
|
|
51
|
+
if (isFailure(missing))
|
|
52
|
+
return missing;
|
|
53
|
+
let pieces = piecesOf(resource.data, input, tests, skip);
|
|
54
|
+
let folder = chainFolder(config.make.migrations, undefined);
|
|
55
|
+
if (isFailure(folder))
|
|
56
|
+
return folder;
|
|
57
|
+
let migrationName = parseName(`create-${resource.data.plural.base}`);
|
|
58
|
+
if (isFailure(migrationName))
|
|
59
|
+
return migrationName;
|
|
60
|
+
let migration = await nextMigrationFile(root, folder.data, migrationName.data);
|
|
61
|
+
if (isFailure(migration))
|
|
62
|
+
return migration;
|
|
63
|
+
if (!skip.has("migration")) {
|
|
64
|
+
pieces.push({ stub: "scaffold.migration.stub", path: `${folder.data}/${migration.data}.sql` });
|
|
65
|
+
}
|
|
66
|
+
let planned = [];
|
|
67
|
+
let header = headerTags(await resolveAuthor(root));
|
|
68
|
+
let values = placeholders(resource.data, header);
|
|
69
|
+
for (let piece of pieces) {
|
|
70
|
+
let exists = isSuccess(await wrap(() => stat(join(root, piece.path))));
|
|
71
|
+
if (exists && piece.shared === true)
|
|
72
|
+
continue;
|
|
73
|
+
if (exists) {
|
|
74
|
+
return failure(new Error(`${piece.path} already exists; sdxc scaffold leaves it as it is`));
|
|
75
|
+
}
|
|
76
|
+
let stub = await loadStub([piece.stub], root, config.make.stubs, packageRoot);
|
|
77
|
+
if (isFailure(stub))
|
|
78
|
+
return stub;
|
|
79
|
+
let content = renderStub(stub.data, values);
|
|
80
|
+
if (isFailure(content))
|
|
81
|
+
return content;
|
|
82
|
+
planned.push({ path: piece.path, content: content.data });
|
|
83
|
+
}
|
|
84
|
+
let requests = declarationsOf(resource.data, input, config.make.routes, skip);
|
|
85
|
+
if (isFailure(requests))
|
|
86
|
+
return requests;
|
|
87
|
+
let staged = await stageDeclarations(root, requests.data, planned);
|
|
88
|
+
if (isFailure(staged))
|
|
89
|
+
return staged;
|
|
90
|
+
let edits = [...staged.data.edits];
|
|
91
|
+
let withPages = !skip.has("pages") && input.api !== true;
|
|
92
|
+
let locales = withPages ? await localeEdits(root, resource.data) : success([]);
|
|
93
|
+
if (isFailure(locales))
|
|
94
|
+
return locales;
|
|
95
|
+
edits.push(...locales.data);
|
|
96
|
+
let dependencies = await missingDependencies(root, [
|
|
97
|
+
...planned,
|
|
98
|
+
...edits.map((edit) => ({ path: edit.path, content: edit.after })),
|
|
99
|
+
]);
|
|
100
|
+
if (isFailure(dependencies))
|
|
101
|
+
return dependencies;
|
|
102
|
+
if (dependencies.data !== null)
|
|
103
|
+
edits.push(dependencies.data);
|
|
104
|
+
if (input.api === true && !skip.has("pages")) {
|
|
105
|
+
let documented = await operationEdits(root, resource.data, edits);
|
|
106
|
+
if (isFailure(documented))
|
|
107
|
+
return documented;
|
|
108
|
+
edits.splice(0, edits.length, ...documented.data.edits);
|
|
109
|
+
staged.data.manual.push(...documented.data.manual);
|
|
110
|
+
}
|
|
111
|
+
let skippedNote = skip.size === 0
|
|
112
|
+
? []
|
|
113
|
+
: [
|
|
114
|
+
`Wrote no ${SCAFFOLD_PIECES.filter((piece) => skip.has(piece)).join(", ")}, as --skip asked\n`,
|
|
115
|
+
];
|
|
116
|
+
let notes = staged.data.manual.map((manual) => `Declare and map ${manual.path} by hand, since ${manual.reason}:\n${manual.instructions}\n`);
|
|
117
|
+
if (input.dryRun) {
|
|
118
|
+
for (let file of planned)
|
|
119
|
+
ctx.stdout(`Would write ${file.path}:\n\n${file.content}\n`);
|
|
120
|
+
for (let edit of edits)
|
|
121
|
+
ctx.stdout(unifiedDiff(edit.path, edit.before, edit.after));
|
|
122
|
+
for (let note of [...notes, ...skippedNote])
|
|
123
|
+
ctx.stdout(note);
|
|
124
|
+
return success(undefined);
|
|
125
|
+
}
|
|
126
|
+
let written = await wrap(async () => {
|
|
127
|
+
for (let file of planned) {
|
|
128
|
+
let target = join(root, file.path);
|
|
129
|
+
await mkdir(dirname(target), { recursive: true });
|
|
130
|
+
await writeFile(target, file.content, { flag: "wx" });
|
|
131
|
+
}
|
|
132
|
+
for (let edit of edits)
|
|
133
|
+
await writeFile(join(root, edit.path), edit.after);
|
|
134
|
+
});
|
|
135
|
+
if (isFailure(written))
|
|
136
|
+
return written;
|
|
137
|
+
for (let file of planned)
|
|
138
|
+
ctx.stdout(`Wrote ${file.path}\n`);
|
|
139
|
+
for (let edit of edits) {
|
|
140
|
+
if (edit !== dependencies.data)
|
|
141
|
+
ctx.stdout(`Edited ${edit.path}\n`);
|
|
142
|
+
}
|
|
143
|
+
if (dependencies.data !== null) {
|
|
144
|
+
let read = await readManifest(root);
|
|
145
|
+
let manager = await packageManager({
|
|
146
|
+
declared: isFailure(read) ? undefined : read.data.manifest.packageManager,
|
|
147
|
+
findLockFile: lockFileAtOrAbove(root),
|
|
148
|
+
});
|
|
149
|
+
let added = dependencies.data.added.map(({ name, version }) => `${name}@${version}`);
|
|
150
|
+
ctx.stdout(`Added ${added.join(", ")} to package.json; run ${manager.install.join(" ")}\n`);
|
|
151
|
+
}
|
|
152
|
+
let changed = [...planned.map((file) => file.path), ...edits.map((edit) => edit.path)];
|
|
153
|
+
let formatted = await formatFiles(ctx, config.format, changed, root);
|
|
154
|
+
for (let note of [...notes, ...skippedNote])
|
|
155
|
+
ctx.stdout(note);
|
|
156
|
+
return formatted;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* The resource `input` names: one name without folders, its plural from `--plural` or the
|
|
160
|
+
* English rules, and at least one field a form must fill. A reference is refused, since every
|
|
161
|
+
* generated test would need a row of the referenced table to point at.
|
|
162
|
+
*/
|
|
163
|
+
function parseResource(input) {
|
|
164
|
+
let singular = parseName(input.name);
|
|
165
|
+
if (isFailure(singular))
|
|
166
|
+
return singular;
|
|
167
|
+
if (singular.data.segments.length > 1) {
|
|
168
|
+
return failure(new Error(`A scaffold name cannot hold folders: ${input.name}`));
|
|
169
|
+
}
|
|
170
|
+
let plural = parseName(input.plural ?? pluralize(singular.data.base));
|
|
171
|
+
if (isFailure(plural))
|
|
172
|
+
return plural;
|
|
173
|
+
if (plural.data.segments.length > 1) {
|
|
174
|
+
return failure(new Error(`A plural cannot hold folders: ${input.plural ?? ""}`));
|
|
175
|
+
}
|
|
176
|
+
let fields = parseFields(input.fields);
|
|
177
|
+
if (isFailure(fields))
|
|
178
|
+
return fields;
|
|
179
|
+
if (fields.data.length === 0) {
|
|
180
|
+
return failure(new Error("Give the scaffold at least one field, such as title"));
|
|
181
|
+
}
|
|
182
|
+
let reference = fields.data.find((field) => field.type === "references");
|
|
183
|
+
if (reference !== undefined) {
|
|
184
|
+
return failure(new Error(`${reference.column} is a reference, which sdxc scaffold writes no pages for yet; write ${reference.column}:string, or add it with sdxc make model`));
|
|
185
|
+
}
|
|
186
|
+
if (fields.data.every((field) => field.nullable)) {
|
|
187
|
+
return failure(new Error("Give the scaffold at least one field without ?, which a form must fill"));
|
|
188
|
+
}
|
|
189
|
+
return success({ singular: singular.data, plural: plural.data, fields: fields.data });
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Refuses a skip that leaves the scaffold reading a file the project lacks: the model every other
|
|
193
|
+
* piece reads, the validator the pages submit through, the factory the tests seed rows with, and
|
|
194
|
+
* the abilities the pages and tools check.
|
|
195
|
+
*/
|
|
196
|
+
async function skippedButNeeded(root, resource, input, skip, tests) {
|
|
197
|
+
let name = resource.singular.base;
|
|
198
|
+
let has = async (path) => isSuccess(await wrap(() => stat(join(root, path))));
|
|
199
|
+
let endpoints = !skip.has("pages");
|
|
200
|
+
let pages = endpoints && input.api !== true;
|
|
201
|
+
let readers = endpoints || input.mcp || input.command;
|
|
202
|
+
let needs = [
|
|
203
|
+
["model", readers, `app/models/${name}.ts`, "the pages, tools and commands read"],
|
|
204
|
+
["validator", pages, `app/http/validators/${name}.ts`, "the pages submit through"],
|
|
205
|
+
["factory", tests && readers, `database/factories/${name}.ts`, "the tests seed rows with"],
|
|
206
|
+
];
|
|
207
|
+
for (let [piece, needed, path, reader] of needs) {
|
|
208
|
+
if (!skip.has(piece) || !needed || (await has(path)))
|
|
209
|
+
continue;
|
|
210
|
+
return failure(new Error(`--skip ${piece} needs ${path}, which ${reader}; drop ${piece} from --skip`));
|
|
211
|
+
}
|
|
212
|
+
if (skip.has("policy") && (endpoints || input.mcp)) {
|
|
213
|
+
let key = resource.singular.key;
|
|
214
|
+
if (!(await declaresAbilities(root, [key]))) {
|
|
215
|
+
return failure(new Error(`--skip policy needs the ${key} abilities in app/policies/abilities.ts, which the pages and tools check; run sdxc make policy ${name} or drop policy from --skip`));
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return success(undefined);
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Every file the scaffold writes but its migration, each test right after the file it covers,
|
|
222
|
+
* leaving out the pieces in `skip`.
|
|
223
|
+
*/
|
|
224
|
+
function piecesOf(resource, input, tests, skip) {
|
|
225
|
+
let { singular, plural } = resource;
|
|
226
|
+
let name = singular.base;
|
|
227
|
+
let pieces = [];
|
|
228
|
+
let add = (stub, path, tested) => {
|
|
229
|
+
pieces.push({ stub: `scaffold.${stub}.stub`, path });
|
|
230
|
+
if (tested && tests) {
|
|
231
|
+
let test = path.replace(/(\.tsx?)$/, ".test$1");
|
|
232
|
+
pieces.push({ stub: `scaffold.${stub}.test.stub`, path: test });
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
if (!skip.has("model"))
|
|
236
|
+
add("model", `app/models/${name}.ts`, true);
|
|
237
|
+
if (!skip.has("factory"))
|
|
238
|
+
add("factory", `database/factories/${name}.ts`, false);
|
|
239
|
+
if (!skip.has("validator") && input.api !== true) {
|
|
240
|
+
add("validator", `app/http/validators/${name}.ts`, true);
|
|
241
|
+
}
|
|
242
|
+
if (tests && !skip.has("policy")) {
|
|
243
|
+
pieces.push({ stub: "policy.test.stub", path: `app/policies/${name}.test.ts` });
|
|
244
|
+
}
|
|
245
|
+
if (!skip.has("pages") && input.api === true) {
|
|
246
|
+
for (let action of API_ACTIONS) {
|
|
247
|
+
add(`api.${action}`, `app/http/controllers/api/${plural.base}/${action}.tsx`, true);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
else if (!skip.has("pages")) {
|
|
251
|
+
add("form", `resources/views/${plural.base}/form.tsx`, false);
|
|
252
|
+
for (let action of ["index", "new", "show", "edit", "destroy"]) {
|
|
253
|
+
add(`controller.${action}`, `app/http/controllers/${plural.base}/${action}.tsx`, true);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
if (input.mcp) {
|
|
257
|
+
for (let action of ["list", "get", "create", "update", "delete"]) {
|
|
258
|
+
add(`mcp.${action}`, `app/mcp/tools/${plural.base}/${action}.ts`, true);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
if (input.command) {
|
|
262
|
+
for (let action of ["list", "show", "create", "delete"]) {
|
|
263
|
+
add(`command.${action}`, `app/commands/${plural.base}/${action}.ts`, true);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
if (tests) {
|
|
267
|
+
pieces.push({
|
|
268
|
+
stub: "scaffold.test-database.stub",
|
|
269
|
+
path: "app/test/database.ts",
|
|
270
|
+
shared: true,
|
|
271
|
+
});
|
|
272
|
+
if (input.mcp) {
|
|
273
|
+
pieces.push({ stub: "scaffold.test-mcp.stub", path: "app/test/mcp.ts", shared: true });
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
return pieces;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Every declaration the scaffold makes, in order: the model in its registry, its abilities in the
|
|
280
|
+
* catalog and their grants in the policy, the five pages in the route contract `routes`, then the
|
|
281
|
+
* tools and the commands when asked for.
|
|
282
|
+
*/
|
|
283
|
+
function declarationsOf(resource, input, routes, skip) {
|
|
284
|
+
let { singular, plural, fields } = resource;
|
|
285
|
+
let words = singular.title.toLowerCase();
|
|
286
|
+
let pluralWords = plural.title.toLowerCase();
|
|
287
|
+
let base = { variant: null, method: "get", routes, tool: "", uri: "" };
|
|
288
|
+
let requests = [];
|
|
289
|
+
if (!skip.has("model")) {
|
|
290
|
+
requests.push({
|
|
291
|
+
...base,
|
|
292
|
+
kind: "model",
|
|
293
|
+
name: singular,
|
|
294
|
+
handler: `app/models/${singular.base}`,
|
|
295
|
+
title: singular.title,
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
if (!skip.has("policy")) {
|
|
299
|
+
requests.push({
|
|
300
|
+
...base,
|
|
301
|
+
kind: "policy",
|
|
302
|
+
name: singular,
|
|
303
|
+
handler: `app/policies/${singular.base}`,
|
|
304
|
+
title: singular.title,
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
let pages = [
|
|
308
|
+
["index", "get", `/${plural.base}`, undefined],
|
|
309
|
+
["new", "form", `/${plural.base}/new`, "false"],
|
|
310
|
+
["show", "get", `/${plural.base}/:id`, undefined],
|
|
311
|
+
["edit", "form", `/${plural.base}/:id/edit`, "false"],
|
|
312
|
+
["destroy", "post", `/${plural.base}/:id/destroy`, undefined],
|
|
313
|
+
];
|
|
314
|
+
let endpoints = [
|
|
315
|
+
["index", "get", `/api/${plural.base}`],
|
|
316
|
+
["show", "get", `/api/${plural.base}/:id`],
|
|
317
|
+
["create", "post", `/api/${plural.base}`],
|
|
318
|
+
["update", "patch", `/api/${plural.base}/:id`],
|
|
319
|
+
["destroy", "delete", `/api/${plural.base}/:id`],
|
|
320
|
+
];
|
|
321
|
+
for (let [action, method, pattern] of skip.has("pages") || input.api !== true ? [] : endpoints) {
|
|
322
|
+
let name = parseName(`api/${plural.base}/${action}`);
|
|
323
|
+
if (isFailure(name))
|
|
324
|
+
return name;
|
|
325
|
+
requests.push({
|
|
326
|
+
...base,
|
|
327
|
+
kind: "controller",
|
|
328
|
+
name: name.data,
|
|
329
|
+
method,
|
|
330
|
+
pattern,
|
|
331
|
+
routes: API_CONTRACT,
|
|
332
|
+
handler: `app/http/controllers/api/${plural.base}/${action}`,
|
|
333
|
+
title: `${plural.title} ${action}`,
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
let pagesWritten = !skip.has("pages") && input.api !== true;
|
|
337
|
+
for (let [action, method, pattern, sitemap] of pagesWritten ? pages : []) {
|
|
338
|
+
let name = parseName(`${plural.base}/${action}`);
|
|
339
|
+
if (isFailure(name))
|
|
340
|
+
return name;
|
|
341
|
+
requests.push({
|
|
342
|
+
...base,
|
|
343
|
+
kind: "controller",
|
|
344
|
+
name: name.data,
|
|
345
|
+
method,
|
|
346
|
+
pattern,
|
|
347
|
+
handler: `app/http/controllers/${plural.base}/${action}`,
|
|
348
|
+
title: `${plural.title} ${action}`,
|
|
349
|
+
...(sitemap === undefined ? {} : { sitemap }),
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
if (input.mcp) {
|
|
353
|
+
let readOnly = "annotations: { readOnlyHint: true },";
|
|
354
|
+
let tools = [
|
|
355
|
+
[
|
|
356
|
+
"list",
|
|
357
|
+
`list_${plural.snake}`,
|
|
358
|
+
`List ${pluralWords}, newest first; get_${singular.snake} reads one in full.`,
|
|
359
|
+
(s) => [`input: ${s}.object({ limit: ${s}.defaulted(${s}.integer(), 20) }),`, readOnly],
|
|
360
|
+
],
|
|
361
|
+
[
|
|
362
|
+
"get",
|
|
363
|
+
`get_${singular.snake}`,
|
|
364
|
+
`Read one ${words} by its id, every field included.`,
|
|
365
|
+
(s) => [`input: ${s}.object({ id: ${s}.string() }),`, readOnly],
|
|
366
|
+
],
|
|
367
|
+
[
|
|
368
|
+
"create",
|
|
369
|
+
`create_${singular.snake}`,
|
|
370
|
+
`Create a ${words} from every field and answer it with its id.`,
|
|
371
|
+
(s) => [`input: ${s}.object({`, ...jsonProperties(fields, s, false), "}),"],
|
|
372
|
+
],
|
|
373
|
+
[
|
|
374
|
+
"update",
|
|
375
|
+
`update_${singular.snake}`,
|
|
376
|
+
`Change the fields named on the ${words} with the id; the others keep their values.`,
|
|
377
|
+
(s) => [
|
|
378
|
+
`input: ${s}.object({`,
|
|
379
|
+
`\tid: ${s}.string(),`,
|
|
380
|
+
...jsonProperties(fields, s, true),
|
|
381
|
+
"}),",
|
|
382
|
+
],
|
|
383
|
+
],
|
|
384
|
+
[
|
|
385
|
+
"delete",
|
|
386
|
+
`delete_${singular.snake}`,
|
|
387
|
+
`Delete the ${words} with the id.`,
|
|
388
|
+
(s) => [`input: ${s}.object({ id: ${s}.string() }),`],
|
|
389
|
+
],
|
|
390
|
+
];
|
|
391
|
+
for (let [action, tool, description, input] of tools) {
|
|
392
|
+
let name = parseName(`${plural.base}/${action}`);
|
|
393
|
+
if (isFailure(name))
|
|
394
|
+
return name;
|
|
395
|
+
requests.push({
|
|
396
|
+
...base,
|
|
397
|
+
kind: "mcp",
|
|
398
|
+
name: name.data,
|
|
399
|
+
handler: `app/mcp/tools/${plural.base}/${action}`,
|
|
400
|
+
tool,
|
|
401
|
+
title: description,
|
|
402
|
+
options: {
|
|
403
|
+
imports: ["s"],
|
|
404
|
+
lines: (locals) => [
|
|
405
|
+
`description: ${JSON.stringify(description)},`,
|
|
406
|
+
...input(locals.s ?? "s"),
|
|
407
|
+
],
|
|
408
|
+
},
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
if (input.command) {
|
|
413
|
+
let id = (locals) => [
|
|
414
|
+
`arguments: { id: ${locals.argument ?? "argument"}(${locals.s ?? "s"}.string(), { description: ${JSON.stringify(`The ${words}'s id`)} }) },`,
|
|
415
|
+
];
|
|
416
|
+
let commands = [
|
|
417
|
+
["list", `List every ${words}, newest first`, undefined],
|
|
418
|
+
["show", `Print one ${words} as JSON`, { imports: ["argument", "s"], lines: id }],
|
|
419
|
+
[
|
|
420
|
+
"create",
|
|
421
|
+
`Create a ${words} and print its id`,
|
|
422
|
+
{
|
|
423
|
+
imports: ["flag"],
|
|
424
|
+
lines: (locals) => ["flags: {", ...flagProperties(fields, locals.flag ?? "flag"), "},"],
|
|
425
|
+
},
|
|
426
|
+
],
|
|
427
|
+
["delete", `Delete one ${words}`, { imports: ["argument", "s"], lines: id }],
|
|
428
|
+
];
|
|
429
|
+
for (let [action, description, options] of commands) {
|
|
430
|
+
let name = parseName(`${plural.base}/${action}`);
|
|
431
|
+
if (isFailure(name))
|
|
432
|
+
return name;
|
|
433
|
+
requests.push({
|
|
434
|
+
...base,
|
|
435
|
+
kind: "command",
|
|
436
|
+
name: name.data,
|
|
437
|
+
handler: `app/commands/${plural.base}/${action}`,
|
|
438
|
+
title: description,
|
|
439
|
+
...(options === undefined ? {} : { options }),
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
return success(requests);
|
|
444
|
+
}
|
|
445
|
+
/**
|
|
446
|
+
* Runs `requests` in order against a temporary copy of the folders they read, with the files the
|
|
447
|
+
* scaffold writes already in it, writing each one's edits into the copy so the next starts from
|
|
448
|
+
* them, and answers each changed file's original text against its final one. The project itself
|
|
449
|
+
* is only read.
|
|
450
|
+
*/
|
|
451
|
+
async function stageDeclarations(root, requests, planned) {
|
|
452
|
+
let stage = await wrap(() => mkdtemp(join(tmpdir(), "sdxc-scaffold-")));
|
|
453
|
+
if (isFailure(stage))
|
|
454
|
+
return stage;
|
|
455
|
+
let result = await wrap(async () => {
|
|
456
|
+
for (let entry of STAGED) {
|
|
457
|
+
let source = join(root, entry);
|
|
458
|
+
if (isFailure(await wrap(() => stat(source))))
|
|
459
|
+
continue;
|
|
460
|
+
await cp(source, join(stage.data, entry), { recursive: true });
|
|
461
|
+
}
|
|
462
|
+
for (let file of planned) {
|
|
463
|
+
let target = join(stage.data, file.path);
|
|
464
|
+
await mkdir(dirname(target), { recursive: true });
|
|
465
|
+
await writeFile(target, file.content);
|
|
466
|
+
}
|
|
467
|
+
let originals = new Map();
|
|
468
|
+
let manual = [];
|
|
469
|
+
for (let request of requests) {
|
|
470
|
+
let declared = await declare(request, stage.data);
|
|
471
|
+
if (declared.kind === "manual") {
|
|
472
|
+
let { path, reason, instructions } = declared;
|
|
473
|
+
manual.push({ path, reason, instructions });
|
|
474
|
+
continue;
|
|
475
|
+
}
|
|
476
|
+
for (let edit of declared.edits) {
|
|
477
|
+
if (!originals.has(edit.path))
|
|
478
|
+
originals.set(edit.path, edit.before);
|
|
479
|
+
await writeFile(join(stage.data, edit.path), edit.after);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
let edits = [];
|
|
483
|
+
for (let [path, before] of originals) {
|
|
484
|
+
edits.push({ path, before, after: await readFile(join(stage.data, path), "utf8") });
|
|
485
|
+
}
|
|
486
|
+
return { edits, manual };
|
|
487
|
+
});
|
|
488
|
+
await rm(stage.data, { recursive: true, force: true });
|
|
489
|
+
return result;
|
|
490
|
+
}
|
|
491
|
+
/**
|
|
492
|
+
* `edits` with the resource's OpenAPI operations appended to `routes/api.ts`, after the routes
|
|
493
|
+
* the staged declarations added, and added to the document `app/initializers/openapi.ts` builds.
|
|
494
|
+
* A file laid out otherwise, such as one with no `@sdxc/json-schema` import, gets the code
|
|
495
|
+
* printed for the user to place instead.
|
|
496
|
+
*/
|
|
497
|
+
async function operationEdits(root, resource, edits) {
|
|
498
|
+
let result = [...edits];
|
|
499
|
+
let manual = [];
|
|
500
|
+
/** The file's text as the edits so far leave it, recording the edit that changes it next. */
|
|
501
|
+
let current = async (path) => {
|
|
502
|
+
let edit = result.find((entry) => entry.path === path);
|
|
503
|
+
if (edit !== undefined)
|
|
504
|
+
return edit.after;
|
|
505
|
+
let read = await wrap(() => readFile(join(root, path), "utf8"));
|
|
506
|
+
return isFailure(read) ? null : read.data;
|
|
507
|
+
};
|
|
508
|
+
let write = async (path, after) => {
|
|
509
|
+
let index = result.findIndex((entry) => entry.path === path);
|
|
510
|
+
if (index !== -1) {
|
|
511
|
+
result[index] = { ...result[index], after };
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
let before = await wrap(() => readFile(join(root, path), "utf8"));
|
|
515
|
+
if (isFailure(before))
|
|
516
|
+
return;
|
|
517
|
+
result.push({ path, before: before.data, after });
|
|
518
|
+
};
|
|
519
|
+
let contract = `routes/${API_CONTRACT}.ts`;
|
|
520
|
+
let names = apiOperationNames(resource);
|
|
521
|
+
let source = await current(contract);
|
|
522
|
+
let locals = source === null ? null : operationLocals(source);
|
|
523
|
+
let code = apiOperations(resource, locals ?? { s: "s", defineOperation: "defineOperation", routes: "ROUTES" });
|
|
524
|
+
if (source === null || locals === null) {
|
|
525
|
+
manual.push({
|
|
526
|
+
path: contract,
|
|
527
|
+
reason: `${contract} does not import @sdxc/json-schema, defineOperation and a default export the operations can use`,
|
|
528
|
+
instructions: `At the end of ${contract}:
|
|
529
|
+
${code}`,
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
else {
|
|
533
|
+
await write(contract, `${source.trimEnd()}
|
|
534
|
+
|
|
535
|
+
${code}
|
|
536
|
+
`);
|
|
537
|
+
}
|
|
538
|
+
let initializer = await current(OPENAPI_INITIALIZER);
|
|
539
|
+
let added = initializer === null ? null : addOperations(initializer, names);
|
|
540
|
+
if (added === null) {
|
|
541
|
+
manual.push({
|
|
542
|
+
path: OPENAPI_INITIALIZER,
|
|
543
|
+
reason: `${OPENAPI_INITIALIZER} has no createDocument(…).add(…) call importing from ~/routes/${API_CONTRACT}`,
|
|
544
|
+
instructions: `Import ${names.join(", ")} from "~/routes/${API_CONTRACT}" and pass them to the document's .add(…)`,
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
else {
|
|
548
|
+
await write(OPENAPI_INITIALIZER, added);
|
|
549
|
+
}
|
|
550
|
+
return success({ edits: result, manual });
|
|
551
|
+
}
|
|
552
|
+
/**
|
|
553
|
+
* The names `source` imports `@sdxc/json-schema`'s namespace, `defineOperation` and its own
|
|
554
|
+
* default export under, or `null` when it lacks one of them.
|
|
555
|
+
*/
|
|
556
|
+
function operationLocals(source) {
|
|
557
|
+
let s = /import \* as (\w+) from "@sdxc\/json-schema";/.exec(source)?.[1];
|
|
558
|
+
let imported = /import \{([^}]*)\} from "@sdxc\/openapi";/.exec(source)?.[1] ?? "";
|
|
559
|
+
let defineOperation = /\bdefineOperation(?:\s+as\s+(\w+))?\b/.exec(imported);
|
|
560
|
+
let routes = /export default (\w+);/.exec(source)?.[1];
|
|
561
|
+
if (s === undefined || defineOperation === null || routes === undefined)
|
|
562
|
+
return null;
|
|
563
|
+
return { s, defineOperation: defineOperation[1] ?? "defineOperation", routes };
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* `source` with `names` imported from the API contract and passed to its document's `.add(…)`,
|
|
567
|
+
* or `null` when it has no named import from `~/routes/api` and no `.add(` call to extend.
|
|
568
|
+
*/
|
|
569
|
+
function addOperations(source, names) {
|
|
570
|
+
let specifier = `~/routes/${API_CONTRACT}`;
|
|
571
|
+
let importPattern = new RegExp(`import \\{([^}]*)\\} from "${specifier.replaceAll("/", "\\/")}";`);
|
|
572
|
+
let imported = importPattern.exec(source);
|
|
573
|
+
let add = /\.add\(([^)]*)\)/.exec(source);
|
|
574
|
+
if (imported === null || add === null)
|
|
575
|
+
return null;
|
|
576
|
+
let existing = (imported[1] ?? "")
|
|
577
|
+
.split(",")
|
|
578
|
+
.map((name) => name.trim())
|
|
579
|
+
.filter((name) => name !== "");
|
|
580
|
+
let importLine = `import { ${[...existing, ...names].join(", ")} } from "${specifier}";`;
|
|
581
|
+
let args = (add[1] ?? "")
|
|
582
|
+
.split(",")
|
|
583
|
+
.map((arg) => arg.trim())
|
|
584
|
+
.filter((arg) => arg !== "");
|
|
585
|
+
let withImport = source.replace(imported[0], importLine);
|
|
586
|
+
return withImport.replace(/\.add\(([^)]*)\)/, `.add(${[...args, ...names].join(", ")})`);
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* The edit of every `resources/locales/*.json` that adds the resource's copy under its plural
|
|
590
|
+
* key. A file that already has that key, or does not parse, stops the scaffold.
|
|
591
|
+
*/
|
|
592
|
+
async function localeEdits(root, resource) {
|
|
593
|
+
let names = await wrap(() => readdir(join(root, LOCALES)));
|
|
594
|
+
if (isFailure(names))
|
|
595
|
+
return success([]);
|
|
596
|
+
let key = resource.plural.key;
|
|
597
|
+
let edits = [];
|
|
598
|
+
for (let name of names.data.filter((entry) => entry.endsWith(".json")).sort()) {
|
|
599
|
+
let path = `${LOCALES}/${name}`;
|
|
600
|
+
let before = await wrap(() => readFile(join(root, path), "utf8"));
|
|
601
|
+
if (isFailure(before))
|
|
602
|
+
return before;
|
|
603
|
+
let parsed = wrap(() => JSON.parse(before.data));
|
|
604
|
+
if (isFailure(parsed))
|
|
605
|
+
return failure(new Error(`${path}: ${parsed.error.message}`));
|
|
606
|
+
if (Object.hasOwn(parsed.data, key)) {
|
|
607
|
+
return failure(new Error(`${path} already has "${key}"; sdxc scaffold leaves it as it is`));
|
|
608
|
+
}
|
|
609
|
+
let after = { ...parsed.data, [key]: localeBlock(resource) };
|
|
610
|
+
edits.push({ path, before: before.data, after: `${JSON.stringify(after, null, "\t")}\n` });
|
|
611
|
+
}
|
|
612
|
+
return success(edits);
|
|
613
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The placeholder values `sdxc scaffold` renders its stubs with: the resource's names in every
|
|
3
|
+
* spelling a stub needs, and the blocks written field by field, such as the model's columns or
|
|
4
|
+
* the form's controls, so a stub stays plain text with no loop in it.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { Name } from "../make/names.js";
|
|
10
|
+
import type { Field } from "./fields.js";
|
|
11
|
+
/** The resource a scaffold writes: its singular and plural names, and its fields. */
|
|
12
|
+
export interface Resource {
|
|
13
|
+
/** The model's name: `post`. */
|
|
14
|
+
singular: Name;
|
|
15
|
+
/** The table's, the routes' and the folders' name: `posts`. */
|
|
16
|
+
plural: Name;
|
|
17
|
+
fields: Field[];
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Every placeholder the scaffold's stubs may use. `header` is the module JSDoc's author tags;
|
|
21
|
+
* the rest derive from `resource`, the copy keys from its plural key (`posts.index.title`).
|
|
22
|
+
*/
|
|
23
|
+
export declare function placeholders(resource: Resource, header: string): Record<string, string>;
|
|
24
|
+
/** The local names `routes/api.ts` imports what the operations are written with under. */
|
|
25
|
+
export interface OperationLocals {
|
|
26
|
+
/** `@sdxc/json-schema`'s namespace. */
|
|
27
|
+
s: string;
|
|
28
|
+
defineOperation: string;
|
|
29
|
+
/** The contract's default export. */
|
|
30
|
+
routes: string;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The OpenAPI operations `scaffold --api` appends to `routes/api.ts`: the schema of a row as the
|
|
34
|
+
* API answers it, then one operation per endpoint, typing each body the endpoint parses and
|
|
35
|
+
* each response it sends, so `/api/openapi.json` documents exactly what the handlers do.
|
|
36
|
+
*/
|
|
37
|
+
export declare function apiOperations(resource: Resource, locals: OperationLocals): string;
|
|
38
|
+
/** The operation names `apiOperations` exports, in the order it writes them. */
|
|
39
|
+
export declare function apiOperationNames(resource: Resource): string[];
|
|
40
|
+
/**
|
|
41
|
+
* The copy every `resources/locales/*.json` gains under the plural key: each page's title and
|
|
42
|
+
* buttons, a label per field, and the words a ticked and an unticked box read as.
|
|
43
|
+
*/
|
|
44
|
+
export declare function localeBlock(resource: Resource): Record<string, unknown>;
|
|
45
|
+
/**
|
|
46
|
+
* The tool input's properties, one line each, every one optional when `optional` says so; a
|
|
47
|
+
* nullable field also takes `null`.
|
|
48
|
+
*/
|
|
49
|
+
export declare function jsonProperties(fields: readonly Field[], s: string, optional: boolean): string[];
|
|
50
|
+
/** A command flag per field, one line each, written with the contract's `flag` local. */
|
|
51
|
+
export declare function flagProperties(fields: readonly Field[], flag: string): string[];
|