@sdxc/kit 0.0.0-pre.1

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.
Files changed (144) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +93 -0
  3. package/config.schema.json +89 -0
  4. package/dist/bin.d.ts +9 -0
  5. package/dist/bin.js +10 -0
  6. package/dist/cli.d.ts +33 -0
  7. package/dist/cli.js +58 -0
  8. package/dist/commands/create.d.ts +20 -0
  9. package/dist/commands/create.js +164 -0
  10. package/dist/commands/init.d.ts +16 -0
  11. package/dist/commands/init.js +27 -0
  12. package/dist/commands/make.d.ts +335 -0
  13. package/dist/commands/make.js +152 -0
  14. package/dist/commands/run.d.ts +23 -0
  15. package/dist/commands/run.js +51 -0
  16. package/dist/commands/ui/add.d.ts +23 -0
  17. package/dist/commands/ui/add.js +50 -0
  18. package/dist/commands/ui/diff.d.ts +20 -0
  19. package/dist/commands/ui/diff.js +28 -0
  20. package/dist/commands/ui/list.d.ts +24 -0
  21. package/dist/commands/ui/list.js +45 -0
  22. package/dist/commands/ui/remove.d.ts +22 -0
  23. package/dist/commands/ui/remove.js +48 -0
  24. package/dist/commands/ui/update.d.ts +23 -0
  25. package/dist/commands/ui/update.js +37 -0
  26. package/dist/commands/upgrade.d.ts +30 -0
  27. package/dist/commands/upgrade.js +184 -0
  28. package/dist/config.d.ts +81 -0
  29. package/dist/config.js +157 -0
  30. package/dist/contract.d.ts +161 -0
  31. package/dist/contract.js +308 -0
  32. package/dist/create/github.d.ts +63 -0
  33. package/dist/create/github.js +172 -0
  34. package/dist/create/target.d.ts +31 -0
  35. package/dist/create/target.js +56 -0
  36. package/dist/create/templates.d.ts +62 -0
  37. package/dist/create/templates.js +228 -0
  38. package/dist/dependencies.d.ts +34 -0
  39. package/dist/dependencies.js +76 -0
  40. package/dist/diff.d.ts +14 -0
  41. package/dist/diff.js +103 -0
  42. package/dist/format.d.ts +21 -0
  43. package/dist/format.js +40 -0
  44. package/dist/init.d.ts +38 -0
  45. package/dist/init.js +92 -0
  46. package/dist/lock.d.ts +74 -0
  47. package/dist/lock.js +214 -0
  48. package/dist/make/declare.d.ts +54 -0
  49. package/dist/make/declare.js +495 -0
  50. package/dist/make/generate.d.ts +56 -0
  51. package/dist/make/generate.js +139 -0
  52. package/dist/make/imports.d.ts +43 -0
  53. package/dist/make/imports.js +104 -0
  54. package/dist/make/index.d.ts +43 -0
  55. package/dist/make/index.js +136 -0
  56. package/dist/make/kinds.d.ts +39 -0
  57. package/dist/make/kinds.js +95 -0
  58. package/dist/make/migrations.d.ts +21 -0
  59. package/dist/make/migrations.js +48 -0
  60. package/dist/make/names.d.ts +31 -0
  61. package/dist/make/names.js +47 -0
  62. package/dist/make/stubs.d.ts +44 -0
  63. package/dist/make/stubs.js +89 -0
  64. package/dist/middleware.d.ts +44 -0
  65. package/dist/middleware.js +49 -0
  66. package/dist/package-manager.d.ts +41 -0
  67. package/dist/package-manager.js +80 -0
  68. package/dist/project.d.ts +19 -0
  69. package/dist/project.js +42 -0
  70. package/dist/releases.d.ts +40 -0
  71. package/dist/releases.js +110 -0
  72. package/dist/testing.d.ts +47 -0
  73. package/dist/testing.js +126 -0
  74. package/dist/ui/commands.d.ts +114 -0
  75. package/dist/ui/commands.js +432 -0
  76. package/dist/ui/imports.d.ts +46 -0
  77. package/dist/ui/imports.js +160 -0
  78. package/dist/ui/layout.d.ts +48 -0
  79. package/dist/ui/layout.js +138 -0
  80. package/dist/ui/registry.d.ts +58 -0
  81. package/dist/ui/registry.js +82 -0
  82. package/dist/upgrade/changelog.d.ts +50 -0
  83. package/dist/upgrade/changelog.js +69 -0
  84. package/dist/upgrade/project.d.ts +62 -0
  85. package/dist/upgrade/project.js +132 -0
  86. package/dist/upgrade/prompt.d.ts +51 -0
  87. package/dist/upgrade/prompt.js +227 -0
  88. package/dist/upgrade/registry.d.ts +33 -0
  89. package/dist/upgrade/registry.js +50 -0
  90. package/lock.schema.json +60 -0
  91. package/package.json +45 -0
  92. package/stubs/command.stub +12 -0
  93. package/stubs/command.test.stub +18 -0
  94. package/stubs/controller.form.stub +33 -0
  95. package/stubs/controller.stub +17 -0
  96. package/stubs/controller.test.stub +15 -0
  97. package/stubs/email.stub +44 -0
  98. package/stubs/email.test.stub +16 -0
  99. package/stubs/factory.stub +10 -0
  100. package/stubs/initializer.stub +7 -0
  101. package/stubs/initializer.test.stub +13 -0
  102. package/stubs/job.cron.stub +12 -0
  103. package/stubs/job.stub +11 -0
  104. package/stubs/job.test.stub +15 -0
  105. package/stubs/mcp.resource.stub +12 -0
  106. package/stubs/mcp.stub +10 -0
  107. package/stubs/mcp.test.stub +15 -0
  108. package/stubs/middleware.stub +11 -0
  109. package/stubs/middleware.test.stub +13 -0
  110. package/stubs/migration.stub +1 -0
  111. package/stubs/model.stub +24 -0
  112. package/stubs/model.test.stub +15 -0
  113. package/stubs/policy.stub +13 -0
  114. package/stubs/policy.test.stub +17 -0
  115. package/stubs/service.stub +13 -0
  116. package/stubs/service.test.stub +14 -0
  117. package/stubs/validator.stub +14 -0
  118. package/stubs/validator.test.stub +22 -0
  119. package/templates/app/README.md +57 -0
  120. package/templates/app/app/commands/about.test.ts +29 -0
  121. package/templates/app/app/commands/about.ts +34 -0
  122. package/templates/app/app/commands/index.ts +15 -0
  123. package/templates/app/app/http/controllers/home.tsx +22 -0
  124. package/templates/app/app/http/controllers/not-found.tsx +25 -0
  125. package/templates/app/app/initializers/assets.ts +11 -0
  126. package/templates/app/app/initializers/cli.ts +35 -0
  127. package/templates/app/app/initializers/dispatcher.ts +18 -0
  128. package/templates/app/app/initializers/logger.ts +10 -0
  129. package/templates/app/app/initializers/mcp.ts +16 -0
  130. package/templates/app/app/initializers/router.tsx +65 -0
  131. package/templates/app/app/jobs/index.ts +10 -0
  132. package/templates/app/app/mcp/resources/index.ts +9 -0
  133. package/templates/app/app/mcp/tools/index.ts +10 -0
  134. package/templates/app/bootstrap/browser.ts +27 -0
  135. package/templates/app/bootstrap/cli.ts +9 -0
  136. package/templates/app/bootstrap/worker.ts +31 -0
  137. package/templates/app/cloudflare.config.ts +28 -0
  138. package/templates/app/config/router-context.d.ts +20 -0
  139. package/templates/app/gitignore +9 -0
  140. package/templates/app/package.json +38 -0
  141. package/templates/app/resources/layouts/document.tsx +58 -0
  142. package/templates/app/routes/web.ts +12 -0
  143. package/templates/app/tsconfig.json +40 -0
  144. package/templates/app/vite.config.ts +40 -0
package/dist/lock.js ADDED
@@ -0,0 +1,214 @@
1
+ /**
2
+ * The CLI's state file: JSON Lines at the configured `lock` path, one entry per line sorted by
3
+ * `kind` then `name`, so every command changes exactly the lines of what it touched. Entries of
4
+ * a kind this CLI does not know are kept byte for byte, so state a newer CLI wrote survives.
5
+ *
6
+ * @author [Sergio Xalambrí](https://sergiodxa.com)
7
+ * @copyright Sergio Xalambrí 2026
8
+ */
9
+ import { mkdir, readFile, rm, stat, writeFile } from "node:fs/promises";
10
+ import { dirname, join } from "node:path";
11
+ import * as s from "@sdxc/json-schema";
12
+ import * as checks from "@sdxc/json-schema/checks";
13
+ import * as JSONL from "@sdxc/jsonl";
14
+ import { failure, isFailure, isSuccess, success, wrap } from "@sdxc/result";
15
+ import { resolveInside } from "./project.js";
16
+ /** Where the lock file lives when the configuration does not move it. */
17
+ export const DEFAULT_LOCK = ".config/sdxc.lock.jsonl";
18
+ /** A component or file name: lowercase segments, so a lock line can never name a path. */
19
+ const NAME = /^[a-z0-9][a-z0-9-]*$/;
20
+ /** A copied file's name relative to `ui.directory`, one segment with no `..`. */
21
+ const FILE_NAME = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
22
+ /** The `sha256-<base64>` form every recorded hash takes. */
23
+ const HASH = /^sha256-[A-Za-z0-9+/]+={0,2}$/;
24
+ /** What every line holds, whatever its kind; the rest of a line belongs to its kind. */
25
+ const ENTRY_KEYS = s.object({ kind: s.string(), name: s.string() }, { unknownKeys: "passthrough" });
26
+ /** A `ui` line: a vendored component, the release it came from and every file written for it. */
27
+ export const UI_LOCK_ENTRY = s.object({
28
+ kind: s.literal("ui").meta({ description: "The command group that owns the entry" }),
29
+ name: s
30
+ .string()
31
+ .pipe(checks.pattern(NAME))
32
+ .meta({ description: "The component, as `@sdxc/ui/<name>` imports it" }),
33
+ release: s.string().meta({ description: "The release the copy came from" }),
34
+ files: s
35
+ .record(s.string().pipe(checks.pattern(FILE_NAME)), s.string().pipe(checks.pattern(HASH)))
36
+ .meta({
37
+ description: "Each file written, relative to `ui.directory`, to the hash of its content",
38
+ }),
39
+ }, { unknownKeys: "error" });
40
+ /**
41
+ * The entry schema as JSON Schema 2020-12, shipped as `lock.schema.json`: every line names its
42
+ * `kind` and `name`, a `ui` line is held to the `ui` entry, and any other kind is accepted.
43
+ */
44
+ export function lockJSONSchema() {
45
+ let ui = s.toJSONSchema(UI_LOCK_ENTRY, { refs: "inline" });
46
+ if (isFailure(ui))
47
+ return ui;
48
+ let { $schema: _, ...uiEntry } = ui.data;
49
+ return success({
50
+ $schema: "https://json-schema.org/draft/2020-12/schema",
51
+ description: "One line of the sdxc lock file, which the CLI writes and the project commits",
52
+ type: "object",
53
+ properties: {
54
+ kind: { type: "string", description: "The command group that owns the entry" },
55
+ name: { type: "string", description: "The entry's identity within its kind" },
56
+ },
57
+ required: ["kind", "name"],
58
+ anyOf: [
59
+ uiEntry,
60
+ {
61
+ properties: { kind: { type: "string", pattern: "^(?!ui$)" } },
62
+ description: "An entry of a kind this release does not define, kept as written",
63
+ },
64
+ ],
65
+ });
66
+ }
67
+ /**
68
+ * The lock file of the project at `root`, empty when there is none yet. A configured location
69
+ * other than the default that is missing while the default exists is a failure naming both, so
70
+ * moving `lock` never starts a second, empty state beside the real one.
71
+ */
72
+ export async function readLock(root, config) {
73
+ let path = config.lock;
74
+ let absolute = resolveInside(root, path);
75
+ if (isFailure(absolute))
76
+ return failure(new Error(`lock: ${absolute.error.message}`));
77
+ let text = await wrap(() => readFile(absolute.data, "utf8"));
78
+ if (isFailure(text)) {
79
+ if (!isMissing(text.error))
80
+ return failure(new Error(`${path}: ${text.error.message}`));
81
+ if (path !== DEFAULT_LOCK && (await isFile(join(root, DEFAULT_LOCK)))) {
82
+ return failure(new Error(`lock is ${path}, which does not exist, but ${DEFAULT_LOCK} does; move ${DEFAULT_LOCK} to ${path} or set lock back to it`));
83
+ }
84
+ return success({ path, lines: [] });
85
+ }
86
+ return parseLock(path, text.data);
87
+ }
88
+ /**
89
+ * The lines of a lock file's `text`. A line that is not JSON, fails its schema, or repeats a
90
+ * `kind` and `name` already seen is a failure naming its line number, and nothing is returned.
91
+ */
92
+ export async function parseLock(path, text) {
93
+ let lines = [];
94
+ let seen = new Map();
95
+ for await (let line of JSONL.lines(chunks(text))) {
96
+ if (isFailure(line))
97
+ return failure(new Error(`${path}: ${line.error.message}`));
98
+ let { text: raw, number } = line.data;
99
+ let where = `${path}:${number}`;
100
+ let value = wrap(() => JSON.parse(raw));
101
+ if (isFailure(value))
102
+ return failure(new Error(`${where}: not valid JSON`));
103
+ let keys = s.parseSafe(ENTRY_KEYS, value.data);
104
+ if (!keys.success)
105
+ return failure(new Error(`${where}: every entry needs a string kind and name`));
106
+ let { kind, name } = keys.value;
107
+ let identity = `${kind}\0${name}`;
108
+ let previous = seen.get(identity);
109
+ if (previous !== undefined) {
110
+ return failure(new Error(`${where}: repeats the ${kind} entry ${name} from line ${previous}; keep one`));
111
+ }
112
+ seen.set(identity, number);
113
+ if (kind !== "ui") {
114
+ lines.push({ kind, name, text: raw });
115
+ continue;
116
+ }
117
+ let entry = s.parseSafe(UI_LOCK_ENTRY, value.data);
118
+ if (!entry.success) {
119
+ let issues = entry.issues.map((issue) => {
120
+ let key = (issue.path ?? []).map(pathSegment).join(".");
121
+ return `${key === "" ? "(entry)" : key}: ${issue.message}`;
122
+ });
123
+ return failure(new Error(`${where}: ${issues.join("; ")}`));
124
+ }
125
+ lines.push({ kind, name, entry: entry.value });
126
+ }
127
+ return success({ path, lines });
128
+ }
129
+ /** Every `ui` entry, in file order. */
130
+ export function uiEntries(lock) {
131
+ return lock.lines.flatMap((line) => ("entry" in line ? [line.entry] : []));
132
+ }
133
+ /** The `ui` entry for `name`, or `undefined` when that component is not copied. */
134
+ export function uiEntry(lock, name) {
135
+ return uiEntries(lock).find((entry) => entry.name === name);
136
+ }
137
+ /** `lock` with `entry` replacing the line of the same name, or inserted where the order puts it. */
138
+ export function setUIEntry(lock, entry) {
139
+ let lines = lock.lines.filter((line) => !(line.kind === "ui" && line.name === entry.name));
140
+ lines.push({ kind: "ui", name: entry.name, entry });
141
+ lines.sort(compareLines);
142
+ return { ...lock, lines };
143
+ }
144
+ /** `lock` without the `ui` line for `name`; every other line is unchanged. */
145
+ export function removeUIEntry(lock, name) {
146
+ return {
147
+ ...lock,
148
+ lines: lock.lines.filter((line) => !(line.kind === "ui" && line.name === name)),
149
+ };
150
+ }
151
+ /**
152
+ * The file's text: one `\n`-terminated line per entry, sorted, known entries with their keys in
153
+ * schema order and unknown ones exactly as read.
154
+ */
155
+ export function renderLock(lock) {
156
+ let text = "";
157
+ for (let line of [...lock.lines].sort(compareLines)) {
158
+ if (!("entry" in line)) {
159
+ text += `${line.text}\n`;
160
+ continue;
161
+ }
162
+ let { kind, name, release, files } = line.entry;
163
+ let ordered = Object.fromEntries(Object.entries(files).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)));
164
+ let rendered = JSONL.stringify([{ kind, name, release, files: ordered }]);
165
+ if (isFailure(rendered))
166
+ return rendered;
167
+ text += rendered.data;
168
+ }
169
+ return success(text);
170
+ }
171
+ /** Writes `lock` at its path in `root`, creating its folders; a lock with no lines is removed. */
172
+ export async function writeLock(root, lock) {
173
+ let absolute = resolveInside(root, lock.path);
174
+ if (isFailure(absolute))
175
+ return absolute;
176
+ let target = absolute.data;
177
+ let text = renderLock(lock);
178
+ if (isFailure(text))
179
+ return text;
180
+ let content = text.data;
181
+ return wrap(async () => {
182
+ if (content === "") {
183
+ await rm(target, { force: true });
184
+ return;
185
+ }
186
+ await mkdir(dirname(target), { recursive: true });
187
+ await writeFile(target, content);
188
+ });
189
+ }
190
+ /** Lines ordered by `kind`, then `name`, by code unit so every machine sorts alike. */
191
+ function compareLines(a, b) {
192
+ let left = `${a.kind}\0${a.name}`;
193
+ let right = `${b.kind}\0${b.name}`;
194
+ return left < right ? -1 : left > right ? 1 : 0;
195
+ }
196
+ /**
197
+ * `text` as the byte source {@link JSONL.lines} reads.
198
+ *
199
+ * @yields The whole text, encoded, as one chunk.
200
+ */
201
+ async function* chunks(text) {
202
+ yield new TextEncoder().encode(text);
203
+ }
204
+ function isMissing(error) {
205
+ return error.code === "ENOENT";
206
+ }
207
+ async function isFile(path) {
208
+ let found = await wrap(() => stat(path));
209
+ return isSuccess(found) && found.data.isFile();
210
+ }
211
+ /** One segment of a Standard Schema issue path, which may be a bare key or a `{ key }` record. */
212
+ function pathSegment(segment) {
213
+ return String(typeof segment === "object" ? segment.key : segment);
214
+ }
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Declares and maps what `make job`, `make controller`, `make mcp` and `make command` generate:
3
+ * the entry in the job map, route contract, MCP or command declaration module, and the mapping
4
+ * in the initializer that serves it, so the new handler answers as soon as the command finishes. Both edits are
5
+ * computed before anything is written; when either cannot be placed, neither is made and the
6
+ * command prints them for the user instead, so a declaration never ships without its mapping.
7
+ *
8
+ * @author [Sergio Xalambrí](https://sergiodxa.com)
9
+ * @copyright Sergio Xalambrí 2026
10
+ */
11
+ import type { Name } from "./names.js";
12
+ /** What to declare and map, as the command line asked for it. */
13
+ export interface DeclarationRequest {
14
+ kind: "job" | "controller" | "mcp" | "command";
15
+ name: Name;
16
+ /** `cron` for a scheduled job, `resource` for an MCP resource, `null` otherwise. */
17
+ variant: string | null;
18
+ /** The schedule of a `cron` job. */
19
+ cron?: string;
20
+ /** A controller's route method: `get`, `post`, `put`, `patch`, `delete` or `form`. */
21
+ method: string;
22
+ /** A controller's route pattern; `/<name>` when omitted. */
23
+ pattern?: string;
24
+ /** The route contract a controller is declared in, a module in `routes/`. */
25
+ routes: string;
26
+ /** The generated handler, relative to the project root, without its extension. */
27
+ handler: string;
28
+ /** The MCP tool name or resource URI, and the title its description is written from. */
29
+ tool: string;
30
+ uri: string;
31
+ title: string;
32
+ }
33
+ /** One file's text before and after an edit, by path relative to the project root. */
34
+ export interface FileEdit {
35
+ path: string;
36
+ before: string;
37
+ after: string;
38
+ }
39
+ /**
40
+ * What `declare` decided. `path` is the declaration's key path in its map, which the handler's
41
+ * stub reads as `{{path}}`. `manual` carries the reason and both edits to make by hand.
42
+ */
43
+ export type Declared = {
44
+ kind: "edits";
45
+ path: string;
46
+ edits: FileEdit[];
47
+ } | {
48
+ kind: "manual";
49
+ path: string;
50
+ reason: string;
51
+ instructions: string;
52
+ };
53
+ /** Plans the declaration and mapping for `request` in the project at `root`. */
54
+ export declare function declare(request: DeclarationRequest, root: string): Promise<Declared>;