@sdxc/kit 0.0.0-stage → 2026.10.16

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 (248) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +92 -2
  3. package/changelog.jsonl +1040 -0
  4. package/config.schema.json +89 -0
  5. package/dist/bin.d.ts +9 -0
  6. package/dist/bin.js +10 -0
  7. package/dist/cli.d.ts +33 -0
  8. package/dist/cli.js +58 -0
  9. package/dist/commands/create.d.ts +20 -0
  10. package/dist/commands/create.js +164 -0
  11. package/dist/commands/init.d.ts +16 -0
  12. package/dist/commands/init.js +27 -0
  13. package/dist/commands/make.d.ts +335 -0
  14. package/dist/commands/make.js +152 -0
  15. package/dist/commands/run.d.ts +23 -0
  16. package/dist/commands/run.js +51 -0
  17. package/dist/commands/ui/add.d.ts +23 -0
  18. package/dist/commands/ui/add.js +50 -0
  19. package/dist/commands/ui/diff.d.ts +20 -0
  20. package/dist/commands/ui/diff.js +28 -0
  21. package/dist/commands/ui/list.d.ts +24 -0
  22. package/dist/commands/ui/list.js +45 -0
  23. package/dist/commands/ui/remove.d.ts +22 -0
  24. package/dist/commands/ui/remove.js +48 -0
  25. package/dist/commands/ui/update.d.ts +23 -0
  26. package/dist/commands/ui/update.js +37 -0
  27. package/dist/commands/upgrade.d.ts +30 -0
  28. package/dist/commands/upgrade.js +184 -0
  29. package/dist/config.d.ts +81 -0
  30. package/dist/config.js +157 -0
  31. package/dist/contract.d.ts +161 -0
  32. package/dist/contract.js +308 -0
  33. package/dist/create/github.d.ts +63 -0
  34. package/dist/create/github.js +172 -0
  35. package/dist/create/target.d.ts +31 -0
  36. package/dist/create/target.js +56 -0
  37. package/dist/create/templates.d.ts +62 -0
  38. package/dist/create/templates.js +228 -0
  39. package/dist/dependencies.d.ts +34 -0
  40. package/dist/dependencies.js +76 -0
  41. package/dist/diff.d.ts +14 -0
  42. package/dist/diff.js +103 -0
  43. package/dist/format.d.ts +21 -0
  44. package/dist/format.js +40 -0
  45. package/dist/init.d.ts +38 -0
  46. package/dist/init.js +92 -0
  47. package/dist/lock.d.ts +74 -0
  48. package/dist/lock.js +214 -0
  49. package/dist/make/declare.d.ts +54 -0
  50. package/dist/make/declare.js +495 -0
  51. package/dist/make/generate.d.ts +62 -0
  52. package/dist/make/generate.js +154 -0
  53. package/dist/make/imports.d.ts +43 -0
  54. package/dist/make/imports.js +105 -0
  55. package/dist/make/index.d.ts +43 -0
  56. package/dist/make/index.js +136 -0
  57. package/dist/make/kinds.d.ts +39 -0
  58. package/dist/make/kinds.js +95 -0
  59. package/dist/make/migrations.d.ts +41 -0
  60. package/dist/make/migrations.js +116 -0
  61. package/dist/make/names.d.ts +31 -0
  62. package/dist/make/names.js +47 -0
  63. package/dist/make/stubs.d.ts +44 -0
  64. package/dist/make/stubs.js +89 -0
  65. package/dist/middleware.d.ts +44 -0
  66. package/dist/middleware.js +49 -0
  67. package/dist/package-manager.d.ts +41 -0
  68. package/dist/package-manager.js +80 -0
  69. package/dist/project.d.ts +19 -0
  70. package/dist/project.js +42 -0
  71. package/dist/releases.d.ts +40 -0
  72. package/dist/releases.js +110 -0
  73. package/dist/testing.d.ts +50 -0
  74. package/dist/testing.js +129 -0
  75. package/dist/ui/commands.d.ts +114 -0
  76. package/dist/ui/commands.js +432 -0
  77. package/dist/ui/imports.d.ts +46 -0
  78. package/dist/ui/imports.js +160 -0
  79. package/dist/ui/layout.d.ts +48 -0
  80. package/dist/ui/layout.js +138 -0
  81. package/dist/ui/registry.d.ts +58 -0
  82. package/dist/ui/registry.js +82 -0
  83. package/dist/upgrade/changelog.d.ts +50 -0
  84. package/dist/upgrade/changelog.js +69 -0
  85. package/dist/upgrade/project.d.ts +62 -0
  86. package/dist/upgrade/project.js +132 -0
  87. package/dist/upgrade/prompt.d.ts +51 -0
  88. package/dist/upgrade/prompt.js +227 -0
  89. package/dist/upgrade/registry.d.ts +33 -0
  90. package/dist/upgrade/registry.js +50 -0
  91. package/lock.schema.json +60 -0
  92. package/package.json +44 -5
  93. package/registry/ui/accordion.json +15 -0
  94. package/registry/ui/alert-dialog.json +20 -0
  95. package/registry/ui/alert.json +15 -0
  96. package/registry/ui/aspect-ratio.json +13 -0
  97. package/registry/ui/attachment.json +15 -0
  98. package/registry/ui/avatar.json +15 -0
  99. package/registry/ui/badge.json +13 -0
  100. package/registry/ui/breadcrumbs.json +15 -0
  101. package/registry/ui/bubble.json +13 -0
  102. package/registry/ui/button.json +16 -0
  103. package/registry/ui/calendar.json +16 -0
  104. package/registry/ui/card.json +18 -0
  105. package/registry/ui/carousel.json +15 -0
  106. package/registry/ui/chart.json +13 -0
  107. package/registry/ui/checkbox-group.json +13 -0
  108. package/registry/ui/checkbox.json +16 -0
  109. package/registry/ui/color-area.json +13 -0
  110. package/registry/ui/color-field.json +20 -0
  111. package/registry/ui/color-picker.json +17 -0
  112. package/registry/ui/color-slider.json +13 -0
  113. package/registry/ui/color-swatch-picker.json +15 -0
  114. package/registry/ui/color-swatch.json +13 -0
  115. package/registry/ui/color-wheel.json +13 -0
  116. package/registry/ui/combobox.json +15 -0
  117. package/registry/ui/command.json +13 -0
  118. package/registry/ui/confirm.json +19 -0
  119. package/registry/ui/context-menu.json +16 -0
  120. package/registry/ui/date-field.json +19 -0
  121. package/registry/ui/date-picker.json +15 -0
  122. package/registry/ui/date-range-picker.json +16 -0
  123. package/registry/ui/description.json +13 -0
  124. package/registry/ui/dialog.json +19 -0
  125. package/registry/ui/disclosure.json +18 -0
  126. package/registry/ui/drawer.json +18 -0
  127. package/registry/ui/drop-indicator.json +13 -0
  128. package/registry/ui/drop-zone.json +13 -0
  129. package/registry/ui/empty.json +15 -0
  130. package/registry/ui/field-error.json +13 -0
  131. package/registry/ui/file-trigger.json +13 -0
  132. package/registry/ui/form.json +21 -0
  133. package/registry/ui/grid-list.json +18 -0
  134. package/registry/ui/group.json +13 -0
  135. package/registry/ui/header.json +13 -0
  136. package/registry/ui/heading-scope.json +20 -0
  137. package/registry/ui/heading.json +15 -0
  138. package/registry/ui/highlight.json +16 -0
  139. package/registry/ui/hover-card.json +13 -0
  140. package/registry/ui/image-placeholder.json +16 -0
  141. package/registry/ui/input.json +13 -0
  142. package/registry/ui/item.json +13 -0
  143. package/registry/ui/keyboard.json +13 -0
  144. package/registry/ui/label.json +13 -0
  145. package/registry/ui/link-button.json +16 -0
  146. package/registry/ui/link.json +13 -0
  147. package/registry/ui/listbox.json +15 -0
  148. package/registry/ui/logo.json +15 -0
  149. package/registry/ui/marker.json +13 -0
  150. package/registry/ui/menu.json +19 -0
  151. package/registry/ui/menubar.json +13 -0
  152. package/registry/ui/message-scroller.json +16 -0
  153. package/registry/ui/message.json +13 -0
  154. package/registry/ui/meter.json +13 -0
  155. package/registry/ui/modal.json +15 -0
  156. package/registry/ui/nav-link.json +13 -0
  157. package/registry/ui/navigation-menu.json +15 -0
  158. package/registry/ui/number-field.json +15 -0
  159. package/registry/ui/otp-field.json +15 -0
  160. package/registry/ui/overlay-arrow.json +13 -0
  161. package/registry/ui/pagination.json +16 -0
  162. package/registry/ui/popover.json +13 -0
  163. package/registry/ui/progress-bar.json +13 -0
  164. package/registry/ui/radio-group.json +13 -0
  165. package/registry/ui/range-calendar.json +16 -0
  166. package/registry/ui/resizable.json +13 -0
  167. package/registry/ui/scroll-area.json +13 -0
  168. package/registry/ui/search-field.json +16 -0
  169. package/registry/ui/section.json +13 -0
  170. package/registry/ui/select.json +13 -0
  171. package/registry/ui/selection-indicator.json +13 -0
  172. package/registry/ui/sentinel-row.json +16 -0
  173. package/registry/ui/separator.json +13 -0
  174. package/registry/ui/shared-element.json +13 -0
  175. package/registry/ui/sheet.json +15 -0
  176. package/registry/ui/shortcut-list.json +18 -0
  177. package/registry/ui/sidebar.json +20 -0
  178. package/registry/ui/skeleton.json +13 -0
  179. package/registry/ui/slider.json +16 -0
  180. package/registry/ui/spinner.json +13 -0
  181. package/registry/ui/switch.json +16 -0
  182. package/registry/ui/table.json +16 -0
  183. package/registry/ui/tabs.json +17 -0
  184. package/registry/ui/tag-group.json +18 -0
  185. package/registry/ui/text-field.json +19 -0
  186. package/registry/ui/text.json +13 -0
  187. package/registry/ui/textarea.json +13 -0
  188. package/registry/ui/time-field.json +19 -0
  189. package/registry/ui/toast.json +16 -0
  190. package/registry/ui/toggle-button.json +17 -0
  191. package/registry/ui/toolbar.json +13 -0
  192. package/registry/ui/tooltip.json +16 -0
  193. package/registry/ui/tree.json +18 -0
  194. package/registry/ui/typeset.json +16 -0
  195. package/registry/ui/youtube-embed.json +16 -0
  196. package/stubs/command.stub +12 -0
  197. package/stubs/command.test.stub +18 -0
  198. package/stubs/controller.form.stub +33 -0
  199. package/stubs/controller.stub +17 -0
  200. package/stubs/controller.test.stub +15 -0
  201. package/stubs/email.stub +44 -0
  202. package/stubs/email.test.stub +16 -0
  203. package/stubs/factory.stub +10 -0
  204. package/stubs/initializer.stub +7 -0
  205. package/stubs/initializer.test.stub +13 -0
  206. package/stubs/job.cron.stub +12 -0
  207. package/stubs/job.stub +11 -0
  208. package/stubs/job.test.stub +15 -0
  209. package/stubs/mcp.resource.stub +12 -0
  210. package/stubs/mcp.stub +10 -0
  211. package/stubs/mcp.test.stub +15 -0
  212. package/stubs/middleware.stub +11 -0
  213. package/stubs/middleware.test.stub +13 -0
  214. package/stubs/migration.stub +1 -0
  215. package/stubs/model.stub +24 -0
  216. package/stubs/model.test.stub +15 -0
  217. package/stubs/policy.stub +13 -0
  218. package/stubs/policy.test.stub +17 -0
  219. package/stubs/service.stub +13 -0
  220. package/stubs/service.test.stub +14 -0
  221. package/stubs/validator.stub +14 -0
  222. package/stubs/validator.test.stub +22 -0
  223. package/templates/app/README.md +57 -0
  224. package/templates/app/app/commands/about.test.ts +29 -0
  225. package/templates/app/app/commands/about.ts +34 -0
  226. package/templates/app/app/commands/index.ts +15 -0
  227. package/templates/app/app/http/controllers/home.tsx +22 -0
  228. package/templates/app/app/http/controllers/not-found.tsx +25 -0
  229. package/templates/app/app/initializers/assets.ts +11 -0
  230. package/templates/app/app/initializers/cli.ts +35 -0
  231. package/templates/app/app/initializers/dispatcher.ts +18 -0
  232. package/templates/app/app/initializers/logger.ts +10 -0
  233. package/templates/app/app/initializers/mcp.ts +16 -0
  234. package/templates/app/app/initializers/router.tsx +65 -0
  235. package/templates/app/app/jobs/index.ts +10 -0
  236. package/templates/app/app/mcp/resources/index.ts +9 -0
  237. package/templates/app/app/mcp/tools/index.ts +10 -0
  238. package/templates/app/bootstrap/browser.ts +27 -0
  239. package/templates/app/bootstrap/cli.ts +9 -0
  240. package/templates/app/bootstrap/worker.ts +31 -0
  241. package/templates/app/cloudflare.config.ts +28 -0
  242. package/templates/app/config/router-context.d.ts +20 -0
  243. package/templates/app/gitignore +9 -0
  244. package/templates/app/package.json +38 -0
  245. package/templates/app/resources/layouts/document.tsx +58 -0
  246. package/templates/app/routes/web.ts +12 -0
  247. package/templates/app/tsconfig.json +40 -0
  248. package/templates/app/vite.config.ts +40 -0
@@ -0,0 +1,116 @@
1
+ /**
2
+ * Migration chains: each configured chain is a folder of numbered SQL files applied in order of
3
+ * their leading number. A new migration sorts after every file in its chain's folder and is
4
+ * spelled like them, so a chain keeps one naming convention whichever tool wrote each file.
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 { failure, isFailure, success, wrap } from "@sdxc/result";
12
+ /** The chain `make migration` writes into without `--chain`. */
13
+ export const DEFAULT_CHAIN = "default";
14
+ /**
15
+ * The folder of `chain` among the configured `chains`. An unknown chain is a failure listing
16
+ * the configured ones.
17
+ */
18
+ export function chainFolder(chains, chain) {
19
+ let name = chain ?? DEFAULT_CHAIN;
20
+ let folder = chains[name];
21
+ if (folder === undefined) {
22
+ let configured = Object.keys(chains).sort().join(", ");
23
+ return failure(new Error(`No migration chain "${name}" in make.migrations; configured: ${configured}`));
24
+ }
25
+ return success(folder);
26
+ }
27
+ /** The convention of an empty chain: `0001_CreatePosts.sql`. */
28
+ export const DEFAULT_CONVENTION = {
29
+ numbering: "sequence",
30
+ width: 4,
31
+ separator: "_",
32
+ case: "pascal",
33
+ };
34
+ /** A 14-digit leading number is a `YYYYMMDDHHmmss` timestamp. */
35
+ const TIMESTAMP_WIDTH = 14;
36
+ /** The shape of each case; a single lowercase word stays undecided, fitting kebab and snake alike. */
37
+ const CASE_PATTERNS = [
38
+ ["kebab", /^[a-z0-9]+(-[a-z0-9]+)+$/],
39
+ ["snake", /^[a-z0-9]+(_[a-z0-9]+)+$/],
40
+ ["pascal", /^[A-Z][A-Za-z0-9]*$/],
41
+ ["camel", /^[a-z][a-z0-9]*[A-Z][A-Za-z0-9]*$/],
42
+ ];
43
+ /**
44
+ * The convention of the SQL files in `files`, read from the newest file first: a chain that
45
+ * changed its spelling keeps its latest one. A name of one lowercase word takes its case from
46
+ * an older file, or from its separator when none tells; an empty chain takes the default.
47
+ */
48
+ export function inferConvention(files) {
49
+ let parsed = files
50
+ .map(parseMigrationFile)
51
+ .filter((file) => file !== null)
52
+ .sort(compareNumbers)
53
+ .reverse();
54
+ let [newest] = parsed;
55
+ if (newest === undefined)
56
+ return DEFAULT_CONVENTION;
57
+ let width = newest.number.length;
58
+ let found = parsed
59
+ .map((file) => CASE_PATTERNS.find(([, pattern]) => pattern.test(file.name))?.[0])
60
+ .find((name) => name !== undefined);
61
+ return {
62
+ numbering: width === TIMESTAMP_WIDTH ? "timestamp" : "sequence",
63
+ width,
64
+ separator: newest.separator,
65
+ case: found ?? (newest.separator === "-" ? "kebab" : "snake"),
66
+ };
67
+ }
68
+ /**
69
+ * The base name, without `.sql`, of the next migration in `folder` named `name`, following the
70
+ * folder's convention. A sequence takes one after the highest number; a timestamp takes `now`,
71
+ * or one past the highest when the clock reads earlier, so the file always applies last.
72
+ */
73
+ export async function nextMigrationFile(root, folder, name, now = new Date()) {
74
+ let entries = await wrap(() => readdir(join(root, folder)));
75
+ if (isFailure(entries)) {
76
+ let code = entries.error.code;
77
+ if (code !== "ENOENT")
78
+ return entries;
79
+ }
80
+ let files = isFailure(entries) ? [] : entries.data;
81
+ let convention = inferConvention(files);
82
+ let highest = files
83
+ .map(parseMigrationFile)
84
+ .reduce((max, file) => (file === null ? max : Math.max(max, Number(file.number))), 0);
85
+ let number = highest + 1;
86
+ if (convention.numbering === "timestamp")
87
+ number = Math.max(Number(timestamp(now)), number);
88
+ let digits = String(number).padStart(convention.width, "0");
89
+ return success(`${digits}${convention.separator}${spell(name, convention.case)}`);
90
+ }
91
+ /** `file` split into its parts, or `null` for anything but a numbered SQL file. */
92
+ function parseMigrationFile(file) {
93
+ let match = /^(\d+)([-_])(.+)\.sql$/.exec(file);
94
+ if (match === null)
95
+ return null;
96
+ let [, number = "", separator, name = ""] = match;
97
+ return { number, separator: separator === "-" ? "-" : "_", name };
98
+ }
99
+ /** Orders files by their leading number, as the migration runners apply them. */
100
+ function compareNumbers(a, b) {
101
+ return Number(a.number) - Number(b.number);
102
+ }
103
+ /** `date` in UTC as `YYYYMMDDHHmmss`. */
104
+ function timestamp(date) {
105
+ return date.toISOString().replace(/\D/g, "").slice(0, TIMESTAMP_WIDTH);
106
+ }
107
+ /** `name` in the given case. */
108
+ function spell(name, spelling) {
109
+ if (spelling === "kebab")
110
+ return name.base;
111
+ if (spelling === "snake")
112
+ return name.snake;
113
+ if (spelling === "camel")
114
+ return name.key;
115
+ return name.pascal;
116
+ }
@@ -0,0 +1,31 @@
1
+ /**
2
+ * How `sdxc make` reads the name argument: a slash is a folder inside the kind's folder, and
3
+ * every segment becomes kebab-case on disk, so `Trials/ExpireOld` and `trials/expire-old`
4
+ * write the same file. The other spellings a stub or a declaration needs derive from it.
5
+ *
6
+ * @author [Sergio Xalambrí](https://sergiodxa.com)
7
+ * @copyright Sergio Xalambrí 2026
8
+ */
9
+ import type { Result } from "@sdxc/result";
10
+ /** A parsed name, in each spelling the generator uses. */
11
+ export interface Name {
12
+ /** Every segment, kebab-case: the folders inside the kind's folder, then the file. */
13
+ segments: string[];
14
+ /** The last segment: the file's base name. */
15
+ base: string;
16
+ /** The last segment in camelCase, the key a declaration uses. */
17
+ key: string;
18
+ /** The last segment in PascalCase. */
19
+ pascal: string;
20
+ /** The last segment in snake_case, as an MCP tool or a table names it. */
21
+ snake: string;
22
+ /** The last segment as words, the first capitalized: `get-post` reads `Get post`. */
23
+ title: string;
24
+ }
25
+ /**
26
+ * Parses `input` into its segments. Each segment must start with a letter once spelled in
27
+ * kebab-case, so every derived key is an identifier; an empty, `.` or `..` segment is refused.
28
+ */
29
+ export declare function parseName(input: string): Result<Name, Error>;
30
+ /** A kebab-case segment in camelCase: `get-post` reads `getPost`. */
31
+ export declare function camel(segment: string): string;
@@ -0,0 +1,47 @@
1
+ /**
2
+ * How `sdxc make` reads the name argument: a slash is a folder inside the kind's folder, and
3
+ * every segment becomes kebab-case on disk, so `Trials/ExpireOld` and `trials/expire-old`
4
+ * write the same file. The other spellings a stub or a declaration needs derive from it.
5
+ *
6
+ * @author [Sergio Xalambrí](https://sergiodxa.com)
7
+ * @copyright Sergio Xalambrí 2026
8
+ */
9
+ import { failure, success } from "@sdxc/result";
10
+ /**
11
+ * Parses `input` into its segments. Each segment must start with a letter once spelled in
12
+ * kebab-case, so every derived key is an identifier; an empty, `.` or `..` segment is refused.
13
+ */
14
+ export function parseName(input) {
15
+ let segments = input.split("/").map(kebab);
16
+ let invalid = segments.find((segment) => !/^[a-z][a-z0-9]*(-[a-z0-9]+)*$/.test(segment));
17
+ if (invalid !== undefined) {
18
+ return failure(new Error(`"${input}" is not a name; use letters, digits and dashes in each segment, starting with a letter`));
19
+ }
20
+ let base = segments[segments.length - 1] ?? "";
21
+ let words = base.split("-");
22
+ return success({
23
+ segments,
24
+ base,
25
+ key: camel(base),
26
+ pascal: words.map(capitalize).join(""),
27
+ snake: words.join("_"),
28
+ title: capitalize(words.join(" ")),
29
+ });
30
+ }
31
+ /** A kebab-case segment in camelCase: `get-post` reads `getPost`. */
32
+ export function camel(segment) {
33
+ let [first = "", ...rest] = segment.split("-");
34
+ return first + rest.map(capitalize).join("");
35
+ }
36
+ /** `text` in kebab-case, splitting camelCase humps, underscores and spaces. */
37
+ function kebab(text) {
38
+ return text
39
+ .replace(/([a-z0-9])([A-Z])/g, "$1-$2")
40
+ .replace(/([A-Z]+)([A-Z][a-z])/g, "$1-$2")
41
+ .replace(/[\s_]+/g, "-")
42
+ .toLowerCase();
43
+ }
44
+ /** `word` with its first letter upper case. */
45
+ function capitalize(word) {
46
+ return word.charAt(0).toUpperCase() + word.slice(1);
47
+ }
@@ -0,0 +1,44 @@
1
+ /**
2
+ * The stubs `sdxc make` writes files from. A project overrides any of them by keeping a file
3
+ * of the same name in `make.stubs`; every other stub comes from the `@sdxc/kit` package. A stub is
4
+ * the file's text with `{{placeholder}}`s, and one the kind does not define is an error.
5
+ *
6
+ * @author [Sergio Xalambrí](https://sergiodxa.com)
7
+ * @copyright Sergio Xalambrí 2026
8
+ */
9
+ import type { Result } from "@sdxc/result";
10
+ import type { KindName } from "./kinds.js";
11
+ /** A stub's text and where it was read from, for messages that name it. */
12
+ export interface Stub {
13
+ name: string;
14
+ text: string;
15
+ /** Relative to the project root for a project stub, `@sdxc/kit/stubs/<name>` for a built-in one. */
16
+ source: string;
17
+ }
18
+ /** What `sdxc make stubs` did with each stub, by path relative to the project root. */
19
+ export interface CopiedStubs {
20
+ written: string[];
21
+ kept: string[];
22
+ }
23
+ /** The stub file name for `kind`, its `variant` and whether it is the test stub. */
24
+ export declare function stubName(kind: KindName, variant: string | null, test: boolean): string;
25
+ /** The folder of built-in stubs in the `@sdxc/kit` package at `packageRoot`. */
26
+ export declare function builtinStubsDir(packageRoot: string): string;
27
+ /**
28
+ * The first of `names` that exists, looking in the project's `stubsDir` before the built-in
29
+ * folder for each name in turn; a failure naming every one when none exists.
30
+ *
31
+ * @param root - The project root.
32
+ * @param stubsDir - The configured `make.stubs`, relative to the root.
33
+ */
34
+ export declare function loadStub(names: readonly string[], root: string, stubsDir: string, packageRoot: string): Promise<Result<Stub, Error>>;
35
+ /**
36
+ * `stub`'s text with every placeholder replaced by its value. A placeholder without a value is
37
+ * a failure naming the stub, the placeholder and the ones the kind accepts.
38
+ */
39
+ export declare function renderStub(stub: Stub, values: Readonly<Record<string, string>>): Result<string, Error>;
40
+ /**
41
+ * Copies the built-in stubs of `kinds` (all of them when empty) into the project's `stubsDir`,
42
+ * creating it. A stub already there is kept as it is, so a project's edits survive.
43
+ */
44
+ export declare function copyStubs(kinds: readonly KindName[], root: string, stubsDir: string, packageRoot: string): Promise<Result<CopiedStubs, Error>>;
@@ -0,0 +1,89 @@
1
+ /**
2
+ * The stubs `sdxc make` writes files from. A project overrides any of them by keeping a file
3
+ * of the same name in `make.stubs`; every other stub comes from the `@sdxc/kit` package. A stub is
4
+ * the file's text with `{{placeholder}}`s, and one the kind does not define is an error.
5
+ *
6
+ * @author [Sergio Xalambrí](https://sergiodxa.com)
7
+ * @copyright Sergio Xalambrí 2026
8
+ */
9
+ import { mkdir, readdir, readFile, stat, writeFile } from "node:fs/promises";
10
+ import { join } from "node:path";
11
+ import { failure, isFailure, isSuccess, success, wrap } from "@sdxc/result";
12
+ /** A placeholder as stubs spell it: `{{name}}`, spaces inside the braces allowed. */
13
+ const PLACEHOLDER = /\{\{\s*([A-Za-z]+)\s*\}\}/g;
14
+ /** The stub file name for `kind`, its `variant` and whether it is the test stub. */
15
+ export function stubName(kind, variant, test) {
16
+ return [kind, variant, test ? "test" : null, "stub"].filter((part) => part !== null).join(".");
17
+ }
18
+ /** The folder of built-in stubs in the `@sdxc/kit` package at `packageRoot`. */
19
+ export function builtinStubsDir(packageRoot) {
20
+ return join(packageRoot, "stubs");
21
+ }
22
+ /**
23
+ * The first of `names` that exists, looking in the project's `stubsDir` before the built-in
24
+ * folder for each name in turn; a failure naming every one when none exists.
25
+ *
26
+ * @param root - The project root.
27
+ * @param stubsDir - The configured `make.stubs`, relative to the root.
28
+ */
29
+ export async function loadStub(names, root, stubsDir, packageRoot) {
30
+ for (let name of names) {
31
+ let candidates = [
32
+ [join(root, stubsDir, name), `${stubsDir}/${name}`],
33
+ [join(builtinStubsDir(packageRoot), name), `@sdxc/kit/stubs/${name}`],
34
+ ];
35
+ for (let [path, source] of candidates) {
36
+ let text = await wrap(() => readFile(path, "utf8"));
37
+ if (isSuccess(text))
38
+ return success({ name, text: text.data, source });
39
+ }
40
+ }
41
+ return failure(new Error(`No stub ${names.join(" or ")} in ${stubsDir} or @sdxc/kit`));
42
+ }
43
+ /**
44
+ * `stub`'s text with every placeholder replaced by its value. A placeholder without a value is
45
+ * a failure naming the stub, the placeholder and the ones the kind accepts.
46
+ */
47
+ export function renderStub(stub, values) {
48
+ let unknown = [...stub.text.matchAll(PLACEHOLDER)]
49
+ .map((match) => match[1] ?? "")
50
+ .filter((name) => !Object.hasOwn(values, name));
51
+ if (unknown.length > 0) {
52
+ let accepted = Object.keys(values)
53
+ .map((name) => `{{${name}}}`)
54
+ .join(", ");
55
+ return failure(new Error(`${stub.source} uses {{${unknown[0]}}}, which this kind does not define; it accepts ${accepted}`));
56
+ }
57
+ return success(stub.text.replace(PLACEHOLDER, (_, name) => values[name] ?? ""));
58
+ }
59
+ /**
60
+ * Copies the built-in stubs of `kinds` (all of them when empty) into the project's `stubsDir`,
61
+ * creating it. A stub already there is kept as it is, so a project's edits survive.
62
+ */
63
+ export async function copyStubs(kinds, root, stubsDir, packageRoot) {
64
+ let builtin = await wrap(() => readdir(builtinStubsDir(packageRoot)));
65
+ if (isFailure(builtin))
66
+ return builtin;
67
+ let selected = builtin.data
68
+ .filter((name) => name.endsWith(".stub"))
69
+ .filter((name) => kinds.length === 0 || kinds.some((kind) => name.startsWith(`${kind}.`)))
70
+ .sort();
71
+ let outcome = { written: [], kept: [] };
72
+ let copied = await wrap(async () => {
73
+ await mkdir(join(root, stubsDir), { recursive: true });
74
+ for (let name of selected) {
75
+ let target = join(root, stubsDir, name);
76
+ let relative = `${stubsDir}/${name}`;
77
+ if (isSuccess(await wrap(() => stat(target)))) {
78
+ outcome.kept.push(relative);
79
+ continue;
80
+ }
81
+ let text = await readFile(join(builtinStubsDir(packageRoot), name), "utf8");
82
+ await writeFile(target, text, { flag: "wx" });
83
+ outcome.written.push(relative);
84
+ }
85
+ });
86
+ if (isFailure(copied))
87
+ return copied;
88
+ return success(outcome);
89
+ }
@@ -0,0 +1,44 @@
1
+ /**
2
+ * What `sdxc` commands share, published onto the context by middleware: the project they work
3
+ * in, with its loaded configuration, and the `@sdxc/kit` package their templates, stubs, registry and
4
+ * changelog come from, which a test replaces with a fixture package.
5
+ *
6
+ * @author [Sergio Xalambrí](https://sergiodxa.com)
7
+ * @copyright Sergio Xalambrí 2026
8
+ */
9
+ import type { CommandMiddleware } from "@sdxc/cli";
10
+ import type { Config } from "./config.js";
11
+ /** The project a command works in. */
12
+ export interface Project {
13
+ /** The nearest directory at or above the working directory holding a `package.json`. */
14
+ root: string;
15
+ /** The project's configuration, with every default filled in. */
16
+ config: Config;
17
+ }
18
+ /** The project `project()` found, read as `ctx.project`. */
19
+ export declare const CurrentProject: {
20
+ defaultValue?: Project;
21
+ };
22
+ /** The directory of the `@sdxc/kit` package a command reads from, read as `ctx.packageRoot`. */
23
+ export declare const PackageRoot: {
24
+ defaultValue?: string;
25
+ };
26
+ /**
27
+ * Finds the project root and loads its configuration before the command runs. No project, or a
28
+ * configuration that does not parse, ends the run with that message and exit code 1.
29
+ */
30
+ export declare function project(): CommandMiddleware<{
31
+ key: typeof CurrentProject;
32
+ value: Project;
33
+ property: "project";
34
+ }>;
35
+ /**
36
+ * Publishes the `@sdxc/kit` package directory. `packageRoot(directory)` installs that one;
37
+ * `packageRoot()` keeps the one an earlier middleware installed, or installs the package this
38
+ * module runs from, so a command lists it in its own chain and a test's CLI overrides it.
39
+ */
40
+ export declare function packageRoot(directory?: string): CommandMiddleware<{
41
+ key: typeof PackageRoot;
42
+ value: string;
43
+ property: "packageRoot";
44
+ }>;
@@ -0,0 +1,49 @@
1
+ /**
2
+ * What `sdxc` commands share, published onto the context by middleware: the project they work
3
+ * in, with its loaded configuration, and the `@sdxc/kit` package their templates, stubs, registry and
4
+ * changelog come from, which a test replaces with a fixture package.
5
+ *
6
+ * @author [Sergio Xalambrí](https://sergiodxa.com)
7
+ * @copyright Sergio Xalambrí 2026
8
+ */
9
+ import { isFailure } from "@sdxc/result";
10
+ import { createContextKey } from "remix/router";
11
+ import { loadConfig } from "./config.js";
12
+ import { defaultPackageRoot } from "./create/templates.js";
13
+ import { findProjectRoot } from "./project.js";
14
+ /** The project `project()` found, read as `ctx.project`. */
15
+ export const CurrentProject = createContextKey();
16
+ /** The directory of the `@sdxc/kit` package a command reads from, read as `ctx.packageRoot`. */
17
+ export const PackageRoot = createContextKey();
18
+ /**
19
+ * Finds the project root and loads its configuration before the command runs. No project, or a
20
+ * configuration that does not parse, ends the run with that message and exit code 1.
21
+ */
22
+ export function project() {
23
+ return async (ctx, next) => {
24
+ let root = await findProjectRoot(ctx.cwd);
25
+ if (isFailure(root))
26
+ return root;
27
+ let loaded = await loadConfig(root.data);
28
+ if (isFailure(loaded))
29
+ return loaded;
30
+ ctx.set(CurrentProject, { root: root.data, config: loaded.data.config }, { property: "project" });
31
+ await next();
32
+ };
33
+ }
34
+ /**
35
+ * Publishes the `@sdxc/kit` package directory. `packageRoot(directory)` installs that one;
36
+ * `packageRoot()` keeps the one an earlier middleware installed, or installs the package this
37
+ * module runs from, so a command lists it in its own chain and a test's CLI overrides it.
38
+ */
39
+ export function packageRoot(directory) {
40
+ return async (ctx, next) => {
41
+ if (directory !== undefined) {
42
+ ctx.set(PackageRoot, directory, { property: "packageRoot" });
43
+ }
44
+ else if (!ctx.has(PackageRoot)) {
45
+ ctx.set(PackageRoot, defaultPackageRoot(), { property: "packageRoot" });
46
+ }
47
+ await next();
48
+ };
49
+ }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * The package manager a project installs with, and the commands it spells. A `packageManager`
3
+ * field decides it, then a lock file, then the package manager that ran the CLI, so `sdxc create`
4
+ * and `sdxc upgrade` print and run the same commands for the same project.
5
+ *
6
+ * @author [Sergio Xalambrí](https://sergiodxa.com)
7
+ * @copyright Sergio Xalambrí 2026
8
+ */
9
+ /** A package manager, with how a project installs, runs an installed binary and runs a script. */
10
+ export interface PackageManager {
11
+ name: "bun" | "npm" | "pnpm" | "yarn";
12
+ install: string[];
13
+ /** The command prefix that downloads a package and runs its binary, the package spec appended. */
14
+ dlx: string[];
15
+ /** The command prefix that runs a `package.json` script. */
16
+ run: string[];
17
+ }
18
+ /** What a project says about its package manager. */
19
+ export interface PackageManagerSources {
20
+ /** The `packageManager` field of `package.json`, such as `pnpm@9.0.0`. */
21
+ declared: string | undefined;
22
+ /** The first of `names`, in order, that the project holds as a lock file, or `null`. */
23
+ findLockFile(names: readonly string[]): string | null | Promise<string | null>;
24
+ /** The `npm_config_user_agent` the CLI was started with, such as `bun/1.3.0`. */
25
+ userAgent?: string | undefined;
26
+ }
27
+ /**
28
+ * The package manager `sources` name: the `packageManager` field, then the first lock file in
29
+ * lookup order, then the user agent, and npm when none of them names one it knows.
30
+ */
31
+ export declare function packageManager(sources: PackageManagerSources): Promise<PackageManager>;
32
+ /**
33
+ * Finds lock files in `root`, then in each directory above it, so a package inside a workspace
34
+ * takes the workspace's lock file and the nearest directory holding one decides.
35
+ */
36
+ export declare function lockFileAtOrAbove(root: string): PackageManagerSources["findLockFile"];
37
+ /**
38
+ * The command that starts `script`, as a person types it: `npm run dev`, or `bun dev` for the
39
+ * package managers that run a script by its name alone.
40
+ */
41
+ export declare function scriptCommand(manager: PackageManager, script: string): string;
@@ -0,0 +1,80 @@
1
+ /**
2
+ * The package manager a project installs with, and the commands it spells. A `packageManager`
3
+ * field decides it, then a lock file, then the package manager that ran the CLI, so `sdxc create`
4
+ * and `sdxc upgrade` print and run the same commands for the same project.
5
+ *
6
+ * @author [Sergio Xalambrí](https://sergiodxa.com)
7
+ * @copyright Sergio Xalambrí 2026
8
+ */
9
+ import { stat } from "node:fs/promises";
10
+ import { dirname, join } from "node:path";
11
+ import { isSuccess, wrap } from "@sdxc/result";
12
+ /** The package managers by name, with the commands each one spells. */
13
+ const PACKAGE_MANAGERS = {
14
+ bun: { name: "bun", install: ["bun", "install"], dlx: ["bunx"], run: ["bun", "run"] },
15
+ npm: { name: "npm", install: ["npm", "install"], dlx: ["npx", "--yes"], run: ["npm", "run"] },
16
+ pnpm: {
17
+ name: "pnpm",
18
+ install: ["pnpm", "install"],
19
+ dlx: ["pnpm", "dlx"],
20
+ run: ["pnpm", "run"],
21
+ },
22
+ yarn: {
23
+ name: "yarn",
24
+ install: ["yarn", "install"],
25
+ dlx: ["yarn", "dlx"],
26
+ run: ["yarn", "run"],
27
+ },
28
+ };
29
+ /** Lock files by the package manager that writes them, in lookup order. */
30
+ const LOCK_FILES = [
31
+ ["bun.lock", "bun"],
32
+ ["bun.lockb", "bun"],
33
+ ["pnpm-lock.yaml", "pnpm"],
34
+ ["yarn.lock", "yarn"],
35
+ ["package-lock.json", "npm"],
36
+ ];
37
+ /**
38
+ * The package manager `sources` name: the `packageManager` field, then the first lock file in
39
+ * lookup order, then the user agent, and npm when none of them names one it knows.
40
+ */
41
+ export async function packageManager(sources) {
42
+ let declared = known(sources.declared?.split("@")[0]);
43
+ if (declared !== null)
44
+ return PACKAGE_MANAGERS[declared];
45
+ let lockFile = await sources.findLockFile(LOCK_FILES.map(([file]) => file));
46
+ let locked = LOCK_FILES.find(([file]) => file === lockFile);
47
+ if (locked !== undefined)
48
+ return PACKAGE_MANAGERS[locked[1]];
49
+ return PACKAGE_MANAGERS[known(sources.userAgent?.split("/")[0]) ?? "npm"];
50
+ }
51
+ /**
52
+ * Finds lock files in `root`, then in each directory above it, so a package inside a workspace
53
+ * takes the workspace's lock file and the nearest directory holding one decides.
54
+ */
55
+ export function lockFileAtOrAbove(root) {
56
+ return async (names) => {
57
+ let directory = root;
58
+ while (true) {
59
+ for (let name of names) {
60
+ if (isSuccess(await wrap(() => stat(join(directory, name)))))
61
+ return name;
62
+ }
63
+ let parent = dirname(directory);
64
+ if (parent === directory)
65
+ return null;
66
+ directory = parent;
67
+ }
68
+ };
69
+ }
70
+ /**
71
+ * The command that starts `script`, as a person types it: `npm run dev`, or `bun dev` for the
72
+ * package managers that run a script by its name alone.
73
+ */
74
+ export function scriptCommand(manager, script) {
75
+ return manager.name === "npm" ? `npm run ${script}` : `${manager.name} ${script}`;
76
+ }
77
+ /** `name` when it is a package manager this module knows, `null` otherwise. */
78
+ function known(name) {
79
+ return name === "bun" || name === "npm" || name === "pnpm" || name === "yarn" ? name : null;
80
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * The project every `sdxc` command works in: the nearest directory at or above the current one
3
+ * that holds a `package.json`. Configuration and every configured path resolve against it, and a
4
+ * path that would leave it is refused, so a command only ever writes inside the project.
5
+ *
6
+ * @author [Sergio Xalambrí](https://sergiodxa.com)
7
+ * @copyright Sergio Xalambrí 2026
8
+ */
9
+ import type { Result } from "@sdxc/result";
10
+ /**
11
+ * The nearest directory at or above `cwd` holding a `package.json`, as an absolute path. Reaching
12
+ * the filesystem root without one is a failure naming where the search started.
13
+ */
14
+ export declare function findProjectRoot(cwd: string): Promise<Result<string, Error>>;
15
+ /**
16
+ * `path`, relative to `root`, as an absolute path that stays inside `root`. An absolute path, or
17
+ * one whose `..` segments climb out of the project, is a failure.
18
+ */
19
+ export declare function resolveInside(root: string, path: string): Result<string, Error>;
@@ -0,0 +1,42 @@
1
+ /**
2
+ * The project every `sdxc` command works in: the nearest directory at or above the current one
3
+ * that holds a `package.json`. Configuration and every configured path resolve against it, and a
4
+ * path that would leave it is refused, so a command only ever writes inside the project.
5
+ *
6
+ * @author [Sergio Xalambrí](https://sergiodxa.com)
7
+ * @copyright Sergio Xalambrí 2026
8
+ */
9
+ import { stat } from "node:fs/promises";
10
+ import { dirname, isAbsolute, join, relative, resolve } from "node:path";
11
+ import { failure, isSuccess, success, wrap } from "@sdxc/result";
12
+ /**
13
+ * The nearest directory at or above `cwd` holding a `package.json`, as an absolute path. Reaching
14
+ * the filesystem root without one is a failure naming where the search started.
15
+ */
16
+ export async function findProjectRoot(cwd) {
17
+ let directory = resolve(cwd);
18
+ while (true) {
19
+ let manifest = await wrap(() => stat(join(directory, "package.json")));
20
+ if (isSuccess(manifest) && manifest.data.isFile())
21
+ return success(directory);
22
+ let parent = dirname(directory);
23
+ if (parent === directory) {
24
+ return failure(new Error(`No package.json at or above ${resolve(cwd)}; run sdxc inside a project`));
25
+ }
26
+ directory = parent;
27
+ }
28
+ }
29
+ /**
30
+ * `path`, relative to `root`, as an absolute path that stays inside `root`. An absolute path, or
31
+ * one whose `..` segments climb out of the project, is a failure.
32
+ */
33
+ export function resolveInside(root, path) {
34
+ if (isAbsolute(path))
35
+ return failure(new Error(`${path} must be relative to the project root`));
36
+ let resolved = resolve(root, path);
37
+ let fromRoot = relative(root, resolved);
38
+ if (fromRoot.startsWith("..") || isAbsolute(fromRoot)) {
39
+ return failure(new Error(`${path} points outside the project root`));
40
+ }
41
+ return success(resolved);
42
+ }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * The kit's one notion of a release: `@sdxc/*` versions are UTC dates and a daily release
3
+ * republishes only what changed, so a package at release D is its newest version on or before D,
4
+ * read from the npm registry. `create`, `make` and `upgrade` all pin through here.
5
+ *
6
+ * @author [Sergio Xalambrí](https://sergiodxa.com)
7
+ * @copyright Sergio Xalambrí 2026
8
+ */
9
+ import type { Result } from "@sdxc/result";
10
+ /** Where npm serves a package's registry document, `<REGISTRY>/<name>`. */
11
+ export declare const REGISTRY = "https://registry.npmjs.org";
12
+ /** The scope every package the kit pins is published under. */
13
+ export declare const SCOPE = "@sdxc/";
14
+ /** What the registry knows about one package. */
15
+ export interface Packument {
16
+ name: string;
17
+ /** The version the `latest` dist-tag points at, `null` when it has none. */
18
+ latest: string | null;
19
+ /** Every published version with the peers it declares. */
20
+ versions: Map<string, Record<string, string>>;
21
+ }
22
+ /**
23
+ * The registry document for `name`. A package npm does not have is a failure naming it, as is
24
+ * an unreachable registry or a document without versions.
25
+ */
26
+ export declare function fetchPackument(name: string, signal?: AbortSignal): Promise<Result<Packument, Error>>;
27
+ /** The newest dated version of `packument` on or before the release `release`, or `null`. */
28
+ export declare function versionAtRelease(packument: Packument, release: string): string | null;
29
+ /**
30
+ * The exact version of each of `names` at `release`, or at its `latest` dist-tag when `release`
31
+ * is `null`. A package with no version at that release is a failure naming both.
32
+ */
33
+ export declare function resolveRelease(names: Iterable<string>, release: string | null, signal?: AbortSignal): Promise<Result<Map<string, string>, Error>>;
34
+ /**
35
+ * The release a project is on: the newest date among the exact `@sdxc/*` pins its dependency
36
+ * sections list, or `null` when it pins none to a dated version.
37
+ */
38
+ export declare function projectRelease(sections: ReadonlyArray<Readonly<Record<string, string>> | undefined>): string | null;
39
+ /** The npm package a module specifier comes from, `@sdxc/auth/issuer` from `@sdxc/auth`, or `null`. */
40
+ export declare function scopedPackageOf(specifier: string): string | null;