@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.
- package/LICENSE.md +21 -0
- package/README.md +93 -0
- package/config.schema.json +89 -0
- package/dist/bin.d.ts +9 -0
- package/dist/bin.js +10 -0
- package/dist/cli.d.ts +33 -0
- package/dist/cli.js +58 -0
- package/dist/commands/create.d.ts +20 -0
- package/dist/commands/create.js +164 -0
- package/dist/commands/init.d.ts +16 -0
- package/dist/commands/init.js +27 -0
- package/dist/commands/make.d.ts +335 -0
- package/dist/commands/make.js +152 -0
- package/dist/commands/run.d.ts +23 -0
- package/dist/commands/run.js +51 -0
- package/dist/commands/ui/add.d.ts +23 -0
- package/dist/commands/ui/add.js +50 -0
- package/dist/commands/ui/diff.d.ts +20 -0
- package/dist/commands/ui/diff.js +28 -0
- package/dist/commands/ui/list.d.ts +24 -0
- package/dist/commands/ui/list.js +45 -0
- package/dist/commands/ui/remove.d.ts +22 -0
- package/dist/commands/ui/remove.js +48 -0
- package/dist/commands/ui/update.d.ts +23 -0
- package/dist/commands/ui/update.js +37 -0
- package/dist/commands/upgrade.d.ts +30 -0
- package/dist/commands/upgrade.js +184 -0
- package/dist/config.d.ts +81 -0
- package/dist/config.js +157 -0
- package/dist/contract.d.ts +161 -0
- package/dist/contract.js +308 -0
- package/dist/create/github.d.ts +63 -0
- package/dist/create/github.js +172 -0
- package/dist/create/target.d.ts +31 -0
- package/dist/create/target.js +56 -0
- package/dist/create/templates.d.ts +62 -0
- package/dist/create/templates.js +228 -0
- package/dist/dependencies.d.ts +34 -0
- package/dist/dependencies.js +76 -0
- package/dist/diff.d.ts +14 -0
- package/dist/diff.js +103 -0
- package/dist/format.d.ts +21 -0
- package/dist/format.js +40 -0
- package/dist/init.d.ts +38 -0
- package/dist/init.js +92 -0
- package/dist/lock.d.ts +74 -0
- package/dist/lock.js +214 -0
- package/dist/make/declare.d.ts +54 -0
- package/dist/make/declare.js +495 -0
- package/dist/make/generate.d.ts +56 -0
- package/dist/make/generate.js +139 -0
- package/dist/make/imports.d.ts +43 -0
- package/dist/make/imports.js +104 -0
- package/dist/make/index.d.ts +43 -0
- package/dist/make/index.js +136 -0
- package/dist/make/kinds.d.ts +39 -0
- package/dist/make/kinds.js +95 -0
- package/dist/make/migrations.d.ts +21 -0
- package/dist/make/migrations.js +48 -0
- package/dist/make/names.d.ts +31 -0
- package/dist/make/names.js +47 -0
- package/dist/make/stubs.d.ts +44 -0
- package/dist/make/stubs.js +89 -0
- package/dist/middleware.d.ts +44 -0
- package/dist/middleware.js +49 -0
- package/dist/package-manager.d.ts +41 -0
- package/dist/package-manager.js +80 -0
- package/dist/project.d.ts +19 -0
- package/dist/project.js +42 -0
- package/dist/releases.d.ts +40 -0
- package/dist/releases.js +110 -0
- package/dist/testing.d.ts +47 -0
- package/dist/testing.js +126 -0
- package/dist/ui/commands.d.ts +114 -0
- package/dist/ui/commands.js +432 -0
- package/dist/ui/imports.d.ts +46 -0
- package/dist/ui/imports.js +160 -0
- package/dist/ui/layout.d.ts +48 -0
- package/dist/ui/layout.js +138 -0
- package/dist/ui/registry.d.ts +58 -0
- package/dist/ui/registry.js +82 -0
- package/dist/upgrade/changelog.d.ts +50 -0
- package/dist/upgrade/changelog.js +69 -0
- package/dist/upgrade/project.d.ts +62 -0
- package/dist/upgrade/project.js +132 -0
- package/dist/upgrade/prompt.d.ts +51 -0
- package/dist/upgrade/prompt.js +227 -0
- package/dist/upgrade/registry.d.ts +33 -0
- package/dist/upgrade/registry.js +50 -0
- package/lock.schema.json +60 -0
- package/package.json +45 -0
- package/stubs/command.stub +12 -0
- package/stubs/command.test.stub +18 -0
- package/stubs/controller.form.stub +33 -0
- package/stubs/controller.stub +17 -0
- package/stubs/controller.test.stub +15 -0
- package/stubs/email.stub +44 -0
- package/stubs/email.test.stub +16 -0
- package/stubs/factory.stub +10 -0
- package/stubs/initializer.stub +7 -0
- package/stubs/initializer.test.stub +13 -0
- package/stubs/job.cron.stub +12 -0
- package/stubs/job.stub +11 -0
- package/stubs/job.test.stub +15 -0
- package/stubs/mcp.resource.stub +12 -0
- package/stubs/mcp.stub +10 -0
- package/stubs/mcp.test.stub +15 -0
- package/stubs/middleware.stub +11 -0
- package/stubs/middleware.test.stub +13 -0
- package/stubs/migration.stub +1 -0
- package/stubs/model.stub +24 -0
- package/stubs/model.test.stub +15 -0
- package/stubs/policy.stub +13 -0
- package/stubs/policy.test.stub +17 -0
- package/stubs/service.stub +13 -0
- package/stubs/service.test.stub +14 -0
- package/stubs/validator.stub +14 -0
- package/stubs/validator.test.stub +22 -0
- package/templates/app/README.md +57 -0
- package/templates/app/app/commands/about.test.ts +29 -0
- package/templates/app/app/commands/about.ts +34 -0
- package/templates/app/app/commands/index.ts +15 -0
- package/templates/app/app/http/controllers/home.tsx +22 -0
- package/templates/app/app/http/controllers/not-found.tsx +25 -0
- package/templates/app/app/initializers/assets.ts +11 -0
- package/templates/app/app/initializers/cli.ts +35 -0
- package/templates/app/app/initializers/dispatcher.ts +18 -0
- package/templates/app/app/initializers/logger.ts +10 -0
- package/templates/app/app/initializers/mcp.ts +16 -0
- package/templates/app/app/initializers/router.tsx +65 -0
- package/templates/app/app/jobs/index.ts +10 -0
- package/templates/app/app/mcp/resources/index.ts +9 -0
- package/templates/app/app/mcp/tools/index.ts +10 -0
- package/templates/app/bootstrap/browser.ts +27 -0
- package/templates/app/bootstrap/cli.ts +9 -0
- package/templates/app/bootstrap/worker.ts +31 -0
- package/templates/app/cloudflare.config.ts +28 -0
- package/templates/app/config/router-context.d.ts +20 -0
- package/templates/app/gitignore +9 -0
- package/templates/app/package.json +38 -0
- package/templates/app/resources/layouts/document.tsx +58 -0
- package/templates/app/routes/web.ts +12 -0
- package/templates/app/tsconfig.json +40 -0
- package/templates/app/vite.config.ts +40 -0
|
@@ -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>;
|
package/dist/project.js
ADDED
|
@@ -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;
|
package/dist/releases.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
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 * as s from "@sdxc/json-schema";
|
|
10
|
+
import { failure, isFailure, success, wrap } from "@sdxc/result";
|
|
11
|
+
import { compare } from "@sdxc/semver";
|
|
12
|
+
/** Where npm serves a package's registry document, `<REGISTRY>/<name>`. */
|
|
13
|
+
export const REGISTRY = "https://registry.npmjs.org";
|
|
14
|
+
/** The scope every package the kit pins is published under. */
|
|
15
|
+
export const SCOPE = "@sdxc/";
|
|
16
|
+
/** A dated release version, `2026.10.14`; the bootstrap placeholder and prereleases are not one. */
|
|
17
|
+
const DATED_VERSION = /^\d{4}\.\d{1,2}\.\d{1,2}$/;
|
|
18
|
+
/** An exact pin as a manifest may spell it, with an optional range operator before a dated version. */
|
|
19
|
+
const DATED_PIN = /^[\^~=]?v?(\d{4}\.\d{1,2}\.\d{1,2})$/;
|
|
20
|
+
/** The fields of a registry document the kit reads. */
|
|
21
|
+
const PACKUMENT_SCHEMA = s.object({
|
|
22
|
+
"dist-tags": s.optional(s.record(s.string(), s.string())),
|
|
23
|
+
versions: s.optional(s.record(s.string(), s.object({ peerDependencies: s.optional(s.record(s.string(), s.string())) }))),
|
|
24
|
+
});
|
|
25
|
+
/**
|
|
26
|
+
* The registry document for `name`. A package npm does not have is a failure naming it, as is
|
|
27
|
+
* an unreachable registry or a document without versions.
|
|
28
|
+
*/
|
|
29
|
+
export async function fetchPackument(name, signal) {
|
|
30
|
+
let url = `${REGISTRY}/${name.replace("/", "%2f")}`;
|
|
31
|
+
let response = await wrap(() => fetch(url, { headers: { accept: "application/json" }, signal }));
|
|
32
|
+
if (isFailure(response)) {
|
|
33
|
+
return failure(new Error(`Could not reach the npm registry: ${response.error.message}`));
|
|
34
|
+
}
|
|
35
|
+
if (response.data.status === 404)
|
|
36
|
+
return failure(new Error(`${name} is not published on npm`));
|
|
37
|
+
if (!response.data.ok) {
|
|
38
|
+
return failure(new Error(`The npm registry answered ${response.data.status} for ${url}`));
|
|
39
|
+
}
|
|
40
|
+
let body = await wrap(() => response.data.json());
|
|
41
|
+
if (isFailure(body))
|
|
42
|
+
return failure(new Error(`The npm registry sent invalid JSON for ${url}`));
|
|
43
|
+
let checked = s.parseSafe(PACKUMENT_SCHEMA, body.data);
|
|
44
|
+
if (!checked.success)
|
|
45
|
+
return failure(new Error(`The npm registry sent no versions for ${url}`));
|
|
46
|
+
let versions = new Map();
|
|
47
|
+
for (let [version, manifest] of Object.entries(checked.value.versions ?? {})) {
|
|
48
|
+
versions.set(version, manifest.peerDependencies ?? {});
|
|
49
|
+
}
|
|
50
|
+
return success({ name, latest: checked.value["dist-tags"]?.latest ?? null, versions });
|
|
51
|
+
}
|
|
52
|
+
/** The newest dated version of `packument` on or before the release `release`, or `null`. */
|
|
53
|
+
export function versionAtRelease(packument, release) {
|
|
54
|
+
let found = null;
|
|
55
|
+
for (let version of packument.versions.keys()) {
|
|
56
|
+
if (!DATED_VERSION.test(version) || compare(version, release) > 0)
|
|
57
|
+
continue;
|
|
58
|
+
if (found === null || compare(version, found) > 0)
|
|
59
|
+
found = version;
|
|
60
|
+
}
|
|
61
|
+
return found;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* The exact version of each of `names` at `release`, or at its `latest` dist-tag when `release`
|
|
65
|
+
* is `null`. A package with no version at that release is a failure naming both.
|
|
66
|
+
*/
|
|
67
|
+
export async function resolveRelease(names, release, signal) {
|
|
68
|
+
let sorted = [...new Set(names)].sort();
|
|
69
|
+
let fetched = await Promise.all(sorted.map((name) => fetchPackument(name, signal)));
|
|
70
|
+
let versions = new Map();
|
|
71
|
+
for (let packument of fetched) {
|
|
72
|
+
if (isFailure(packument))
|
|
73
|
+
return packument;
|
|
74
|
+
let { name } = packument.data;
|
|
75
|
+
let version = release === null ? packument.data.latest : versionAtRelease(packument.data, release);
|
|
76
|
+
if (version === null) {
|
|
77
|
+
return failure(new Error(release === null
|
|
78
|
+
? `${name} has no latest version on npm`
|
|
79
|
+
: `${name} has no release on or before ${release}`));
|
|
80
|
+
}
|
|
81
|
+
versions.set(name, version);
|
|
82
|
+
}
|
|
83
|
+
return success(versions);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* The release a project is on: the newest date among the exact `@sdxc/*` pins its dependency
|
|
87
|
+
* sections list, or `null` when it pins none to a dated version.
|
|
88
|
+
*/
|
|
89
|
+
export function projectRelease(sections) {
|
|
90
|
+
let newest = null;
|
|
91
|
+
for (let section of sections) {
|
|
92
|
+
for (let [name, range] of Object.entries(section ?? {})) {
|
|
93
|
+
if (!name.startsWith(SCOPE))
|
|
94
|
+
continue;
|
|
95
|
+
let version = DATED_PIN.exec(range.trim())?.[1];
|
|
96
|
+
if (version === undefined)
|
|
97
|
+
continue;
|
|
98
|
+
if (newest === null || compare(version, newest) > 0)
|
|
99
|
+
newest = version;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return newest;
|
|
103
|
+
}
|
|
104
|
+
/** The npm package a module specifier comes from, `@sdxc/auth/issuer` from `@sdxc/auth`, or `null`. */
|
|
105
|
+
export function scopedPackageOf(specifier) {
|
|
106
|
+
if (!specifier.startsWith(SCOPE))
|
|
107
|
+
return null;
|
|
108
|
+
let name = specifier.split("/").slice(0, 2).join("/");
|
|
109
|
+
return name.length > SCOPE.length ? name : null;
|
|
110
|
+
}
|