@sdxc/kit 2026.10.25 → 2026.10.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -15
- package/changelog.jsonl +40 -5
- package/dist/architecture/graph.d.ts +16 -3
- package/dist/architecture/graph.js +37 -19
- package/dist/architecture/layers.d.ts +10 -3
- package/dist/architecture/layers.js +38 -9
- package/dist/commands/destroy.d.ts +1 -1
- package/dist/commands/make.d.ts +5 -5
- package/dist/commands/make.js +11 -8
- package/dist/commands/scaffold.d.ts +1 -1
- package/dist/contract.d.ts +3 -3
- package/dist/doctor/checks.d.ts +2 -2
- package/dist/doctor/checks.js +14 -2
- package/dist/doctor/constant-names.d.ts +17 -0
- package/dist/doctor/constant-names.js +106 -0
- package/dist/doctor/dependencies.d.ts +3 -3
- package/dist/doctor/dependencies.js +3 -3
- package/dist/doctor/fan-out.d.ts +3 -2
- package/dist/doctor/fan-out.js +4 -3
- package/dist/doctor/fetch-param.d.ts +15 -0
- package/dist/doctor/fetch-param.js +37 -0
- package/dist/doctor/files.d.ts +2 -0
- package/dist/doctor/files.js +4 -0
- package/dist/doctor/local-const.d.ts +15 -0
- package/dist/doctor/local-const.js +54 -0
- package/dist/doctor/logger.d.ts +15 -0
- package/dist/doctor/logger.js +41 -0
- package/dist/doctor/orphans.d.ts +3 -3
- package/dist/doctor/orphans.js +33 -7
- package/dist/doctor/process-env.d.ts +15 -0
- package/dist/doctor/process-env.js +39 -0
- package/dist/doctor/project.js +17 -13
- package/dist/doctor/result-throws.d.ts +5 -4
- package/dist/doctor/result-throws.js +6 -5
- package/dist/doctor/source-rule.d.ts +35 -0
- package/dist/doctor/source-rule.js +52 -0
- package/dist/doctor/stack-imports.d.ts +15 -0
- package/dist/doctor/stack-imports.js +60 -0
- package/dist/doctor/syntax.d.ts +31 -0
- package/dist/doctor/syntax.js +56 -0
- package/dist/make/binding.d.ts +13 -8
- package/dist/make/binding.js +96 -64
- package/dist/make/from-model.js +2 -7
- package/dist/make/index.js +2 -7
- package/dist/package-manager.d.ts +6 -0
- package/dist/package-manager.js +25 -2
- package/dist/scaffold/index.js +2 -7
- package/dist/ui/commands.js +2 -5
- package/dist/ui/registry.d.ts +2 -2
- package/dist/upgrade/changelog.d.ts +2 -0
- package/dist/upgrade/changelog.js +2 -0
- package/dist/upgrade/project.d.ts +2 -2
- package/dist/upgrade/prompt.d.ts +3 -2
- package/dist/upgrade/prompt.js +43 -2
- package/package.json +3 -3
- package/stubs/scaffold.test-mcp.stub +5 -1
- package/templates/app/AGENTS.md +3 -1
- package/templates/app/app/commands/db/fresh.ts +20 -13
- package/templates/app/app/commands/db/migrate.ts +42 -32
- package/templates/app/app/http/controllers/api/openapi.test.ts +29 -0
- package/templates/app/app/http/controllers/api/openapi.tsx +8 -2
- package/templates/app/app/http/controllers/mcp.test.ts +16 -0
- package/templates/app/app/http/middleware/access.ts +6 -3
- package/templates/app/app/http/middleware/page.ts +3 -4
- package/templates/app/app/initializers/database.test.ts +6 -1
- package/templates/app/app/initializers/openapi.ts +5 -4
- package/templates/app/app/initializers/router.tsx +3 -4
- package/templates/app/app/test/cloudflare-workers.test.ts +27 -0
- package/templates/app/app/test/cloudflare-workers.ts +17 -4
- package/templates/app/app/test/database.ts +33 -0
- package/templates/app/app/test/mcp.ts +96 -0
- package/templates/app/resources/layouts/document.tsx +2 -3
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `local-const` check: `const` declares module-level constants only, and every binding inside
|
|
3
|
+
* a function or block is `let`. A reader then knows a `const` is shared by the whole module and
|
|
4
|
+
* named for it, and a local binding reads the same whether or not it is reassigned.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { sourceCheck } from "./source-rule.js";
|
|
10
|
+
import { lineAt, parseModule, walk } from "./syntax.js";
|
|
11
|
+
/** Every `const` below the module's top level, tests included; ambient declarations are left out. */
|
|
12
|
+
export const LOCAL_CONST_RULE = {
|
|
13
|
+
name: "local-const",
|
|
14
|
+
tests: true,
|
|
15
|
+
find: findLocalConsts,
|
|
16
|
+
passed: "Every const is a module-level constant",
|
|
17
|
+
noun: ["const inside a function or block", "consts inside a function or block"],
|
|
18
|
+
fix: "Declare it with let, or move a true constant to the module's top level in UPPER_SNAKE_CASE",
|
|
19
|
+
};
|
|
20
|
+
/** Warns on each `const` declared inside a function or block. */
|
|
21
|
+
export const localConstCheck = sourceCheck(LOCAL_CONST_RULE);
|
|
22
|
+
/** Each `const` declaration nested in a function, class or block of `source`. */
|
|
23
|
+
export function findLocalConsts(file, source) {
|
|
24
|
+
let parsed = parseModule(file, source);
|
|
25
|
+
if (parsed === null)
|
|
26
|
+
return [];
|
|
27
|
+
let findings = [];
|
|
28
|
+
walk(parsed.program, (node, ancestors) => {
|
|
29
|
+
if (node.type !== "VariableDeclaration" || node.kind !== "const")
|
|
30
|
+
return;
|
|
31
|
+
if (node.declare === true || ancestors.some(isAmbient) || ancestors.every(isTopLevel))
|
|
32
|
+
return;
|
|
33
|
+
findings.push({
|
|
34
|
+
line: lineAt(source, node.start),
|
|
35
|
+
message: "const inside a function or block",
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
return findings;
|
|
39
|
+
}
|
|
40
|
+
/** Nodes that keep a declaration at module level: a namespace's members are module members too. */
|
|
41
|
+
const MODULE_LEVEL = new Set([
|
|
42
|
+
"Program",
|
|
43
|
+
"ExportNamedDeclaration",
|
|
44
|
+
"TSModuleDeclaration",
|
|
45
|
+
"TSModuleBlock",
|
|
46
|
+
]);
|
|
47
|
+
/** A node that keeps a declaration at module level. */
|
|
48
|
+
function isTopLevel(node) {
|
|
49
|
+
return MODULE_LEVEL.has(node.type);
|
|
50
|
+
}
|
|
51
|
+
/** A `declare` module or namespace, whose members describe types rather than run. */
|
|
52
|
+
function isAmbient(node) {
|
|
53
|
+
return node.type === "TSModuleDeclaration" && node.declare === true;
|
|
54
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `logger` check: the app builds its loggers in `app/initializers/`, and the router and job
|
|
3
|
+
* dispatcher attach that one instance to each invocation, so every line carries the request's
|
|
4
|
+
* trace and a handler logs through `ctx.log` or `currentLog()`.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { SourceFinding, SourceRule } from "./source-rule.js";
|
|
10
|
+
/** Every `createLogger()` call in app code outside `app/initializers/`. */
|
|
11
|
+
export declare const LOGGER_RULE: SourceRule;
|
|
12
|
+
/** Warns on each `createLogger()` call outside `app/initializers/`. */
|
|
13
|
+
export declare const loggerCheck: import("./check.js").Check;
|
|
14
|
+
/** Each `createLogger()` call in `source`, unless `file` is an initializer. */
|
|
15
|
+
export declare function findLoggerInstances(file: string, source: string): SourceFinding[];
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `logger` check: the app builds its loggers in `app/initializers/`, and the router and job
|
|
3
|
+
* dispatcher attach that one instance to each invocation, so every line carries the request's
|
|
4
|
+
* trace and a handler logs through `ctx.log` or `currentLog()`.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { sourceCheck } from "./source-rule.js";
|
|
10
|
+
import { isNode, lineAt, parseModule, walk } from "./syntax.js";
|
|
11
|
+
/** The folder that owns the app's single instances. */
|
|
12
|
+
const INITIALIZERS = "app/initializers/";
|
|
13
|
+
/** Every `createLogger()` call in app code outside `app/initializers/`. */
|
|
14
|
+
export const LOGGER_RULE = {
|
|
15
|
+
name: "logger",
|
|
16
|
+
tests: false,
|
|
17
|
+
find: findLoggerInstances,
|
|
18
|
+
passed: "Loggers are built in app/initializers",
|
|
19
|
+
noun: ["logger built outside app/initializers", "loggers built outside app/initializers"],
|
|
20
|
+
fix: "Log through ctx.log, or currentLog() from @sdxc/logger, and build the logger in app/initializers/logger.ts",
|
|
21
|
+
};
|
|
22
|
+
/** Warns on each `createLogger()` call outside `app/initializers/`. */
|
|
23
|
+
export const loggerCheck = sourceCheck(LOGGER_RULE);
|
|
24
|
+
/** Each `createLogger()` call in `source`, unless `file` is an initializer. */
|
|
25
|
+
export function findLoggerInstances(file, source) {
|
|
26
|
+
if (file.startsWith(INITIALIZERS) || file.includes(`/${INITIALIZERS}`))
|
|
27
|
+
return [];
|
|
28
|
+
let parsed = parseModule(file, source);
|
|
29
|
+
if (parsed === null)
|
|
30
|
+
return [];
|
|
31
|
+
let findings = [];
|
|
32
|
+
walk(parsed.program, (node) => {
|
|
33
|
+
if (node.type !== "CallExpression")
|
|
34
|
+
return;
|
|
35
|
+
let callee = node.callee;
|
|
36
|
+
if (!isNode(callee) || callee.type !== "Identifier" || callee.name !== "createLogger")
|
|
37
|
+
return;
|
|
38
|
+
findings.push({ line: lineAt(source, node.start), message: "calls createLogger()" });
|
|
39
|
+
});
|
|
40
|
+
return findings;
|
|
41
|
+
}
|
package/dist/doctor/orphans.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The `orphans` check: every app-source module is imported by another
|
|
3
|
-
* loads from outside the graph (bootstrap,
|
|
4
|
-
* agent leftovers show up as unreferenced files.
|
|
2
|
+
* The `orphans` check: every app-source module is imported by another module or a test, named
|
|
3
|
+
* by a root config file, or is an entry the layout loads from outside the graph (bootstrap,
|
|
4
|
+
* routes, initializers, declarations). Dead agent leftovers show up as unreferenced files.
|
|
5
5
|
*
|
|
6
6
|
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
7
|
* @copyright Sergio Xalambrí 2026
|
package/dist/doctor/orphans.js
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The `orphans` check: every app-source module is imported by another
|
|
3
|
-
* loads from outside the graph (bootstrap,
|
|
4
|
-
* agent leftovers show up as unreferenced files.
|
|
2
|
+
* The `orphans` check: every app-source module is imported by another module or a test, named
|
|
3
|
+
* by a root config file, or is an entry the layout loads from outside the graph (bootstrap,
|
|
4
|
+
* routes, initializers, declarations). Dead agent leftovers show up as unreferenced files.
|
|
5
5
|
*
|
|
6
6
|
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
7
|
* @copyright Sergio Xalambrí 2026
|
|
8
8
|
*/
|
|
9
|
+
import { readdir, readFile } from "node:fs/promises";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
import { isFailure, wrap } from "@sdxc/result";
|
|
9
12
|
import { projectGraph } from "../architecture/graph.js";
|
|
13
|
+
import { isDeclaration } from "../architecture/layers.js";
|
|
10
14
|
import { passed, skipped } from "./check.js";
|
|
11
15
|
/** Prefixes that may have no importers inside the project graph. */
|
|
12
16
|
const ENTRY_PREFIXES = [
|
|
@@ -26,9 +30,10 @@ export const orphansCheck = {
|
|
|
26
30
|
let graph = await projectGraph(context.root);
|
|
27
31
|
if (graph.modules.length === 0)
|
|
28
32
|
return skipped("No app source to find orphans");
|
|
29
|
-
let imported = new Set(graph.edges.map((edge) => edge.to));
|
|
33
|
+
let imported = new Set([...graph.edges, ...graph.testEdges].map((edge) => edge.to));
|
|
34
|
+
let configured = await rootConfigText(context.root);
|
|
30
35
|
let findings = graph.modules
|
|
31
|
-
.filter((module) => !imported.has(module) && !isEntry(module))
|
|
36
|
+
.filter((module) => !imported.has(module) && !isEntry(module) && !configured.includes(module))
|
|
32
37
|
.sort()
|
|
33
38
|
.map((module) => `${module}: nothing in the project imports it`);
|
|
34
39
|
if (findings.length === 0) {
|
|
@@ -44,7 +49,28 @@ export const orphansCheck = {
|
|
|
44
49
|
};
|
|
45
50
|
},
|
|
46
51
|
};
|
|
47
|
-
/**
|
|
52
|
+
/** A tool's config file at the project root, such as `vite.config.ts`. */
|
|
53
|
+
const ROOT_CONFIG = /\.config\.[cm]?[jt]s$/;
|
|
54
|
+
/**
|
|
55
|
+
* Whether `path` is an entry the layout loads without a same-project importer, or a declaration
|
|
56
|
+
* module `sdxc make` imports into its initializer with the first handler it maps.
|
|
57
|
+
*/
|
|
48
58
|
function isEntry(path) {
|
|
49
|
-
return ENTRY_PREFIXES.some((prefix) => path.startsWith(prefix));
|
|
59
|
+
return ENTRY_PREFIXES.some((prefix) => path.startsWith(prefix)) || isDeclaration(path);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The text of every tool config at the project root, joined, so a module a config names by
|
|
63
|
+
* path, as `vite.config.ts` names a test stand-in for a runtime module, counts as used.
|
|
64
|
+
*/
|
|
65
|
+
async function rootConfigText(root) {
|
|
66
|
+
let entries = await wrap(() => readdir(root));
|
|
67
|
+
if (isFailure(entries))
|
|
68
|
+
return "";
|
|
69
|
+
let texts = [];
|
|
70
|
+
for (let name of entries.data.filter((entry) => ROOT_CONFIG.test(entry))) {
|
|
71
|
+
let text = await wrap(() => readFile(join(root, name), "utf8"));
|
|
72
|
+
if (!isFailure(text))
|
|
73
|
+
texts.push(text.data);
|
|
74
|
+
}
|
|
75
|
+
return texts.join("\n");
|
|
50
76
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `process-env` check: app code reads its bindings and secrets from `env` in
|
|
3
|
+
* `cloudflare:workers`, which `cloudflare.config.ts` declares and typegen types. `process.env`
|
|
4
|
+
* holds none of them in a deployed Worker.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { SourceFinding, SourceRule } from "./source-rule.js";
|
|
10
|
+
/** Every `process.env` read in app code; tests set up their environment as they need. */
|
|
11
|
+
export declare const PROCESS_ENV_RULE: SourceRule;
|
|
12
|
+
/** Warns on each `process.env` read in app code. */
|
|
13
|
+
export declare const processEnvCheck: import("./check.js").Check;
|
|
14
|
+
/** Each `process.env` member access in `source`. */
|
|
15
|
+
export declare function findProcessEnv(file: string, source: string): SourceFinding[];
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `process-env` check: app code reads its bindings and secrets from `env` in
|
|
3
|
+
* `cloudflare:workers`, which `cloudflare.config.ts` declares and typegen types. `process.env`
|
|
4
|
+
* holds none of them in a deployed Worker.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { sourceCheck } from "./source-rule.js";
|
|
10
|
+
import { isNode, lineAt, parseModule, walk } from "./syntax.js";
|
|
11
|
+
/** Every `process.env` read in app code; tests set up their environment as they need. */
|
|
12
|
+
export const PROCESS_ENV_RULE = {
|
|
13
|
+
name: "process-env",
|
|
14
|
+
tests: false,
|
|
15
|
+
find: findProcessEnv,
|
|
16
|
+
passed: "App code reads env from cloudflare:workers",
|
|
17
|
+
noun: ["process.env read", "process.env reads"],
|
|
18
|
+
fix: 'Read the binding from env, imported from "cloudflare:workers"',
|
|
19
|
+
};
|
|
20
|
+
/** Warns on each `process.env` read in app code. */
|
|
21
|
+
export const processEnvCheck = sourceCheck(PROCESS_ENV_RULE);
|
|
22
|
+
/** Each `process.env` member access in `source`. */
|
|
23
|
+
export function findProcessEnv(file, source) {
|
|
24
|
+
let parsed = parseModule(file, source);
|
|
25
|
+
if (parsed === null)
|
|
26
|
+
return [];
|
|
27
|
+
let findings = [];
|
|
28
|
+
walk(parsed.program, (node) => {
|
|
29
|
+
if (node.type !== "MemberExpression")
|
|
30
|
+
return;
|
|
31
|
+
let { object, property } = node;
|
|
32
|
+
if (!isNode(object) || object.type !== "Identifier" || object.name !== "process")
|
|
33
|
+
return;
|
|
34
|
+
if (!isNode(property) || property.name !== "env")
|
|
35
|
+
return;
|
|
36
|
+
findings.push({ line: lineAt(source, node.start), message: "reads process.env" });
|
|
37
|
+
});
|
|
38
|
+
return findings;
|
|
39
|
+
}
|
package/dist/doctor/project.js
CHANGED
|
@@ -63,19 +63,23 @@ const context = Object.freeze({
|
|
|
63
63
|
}
|
|
64
64
|
},
|
|
65
65
|
async files(folder) {
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
66
|
+
const found = [];
|
|
67
|
+
const visit = async (dir) => {
|
|
68
|
+
let entries;
|
|
69
|
+
try {
|
|
70
|
+
entries = await readdir(dir, { withFileTypes: true });
|
|
71
|
+
} catch {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
for (const entry of entries) {
|
|
75
|
+
if (entry.name === "node_modules" || entry.name.startsWith(".")) continue;
|
|
76
|
+
const full = join(dir, entry.name);
|
|
77
|
+
if (entry.isDirectory()) await visit(full);
|
|
78
|
+
else if (entry.isFile()) found.push(relative(root, full).split(sep).join("/"));
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
await visit(inside(folder));
|
|
82
|
+
return found.sort();
|
|
79
83
|
},
|
|
80
84
|
});
|
|
81
85
|
const NAME = new RegExp(input.namePattern);
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The `result-throws` check: app source
|
|
3
|
-
*
|
|
2
|
+
* The `result-throws` check: app source returns `@sdxc/result` failures so handlers stay typed.
|
|
3
|
+
* Entry points in `bootstrap/` answer the runtime that calls them, whose callbacks report a
|
|
4
|
+
* failure by throwing, so they are left out.
|
|
4
5
|
*
|
|
5
6
|
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
6
7
|
* @copyright Sergio Xalambrí 2026
|
|
7
8
|
*/
|
|
8
9
|
import type { Check } from "./check.js";
|
|
9
10
|
/**
|
|
10
|
-
* Warns on each `throw` statement in app source
|
|
11
|
-
* {@link isAppSource}.
|
|
11
|
+
* Warns on each `throw` statement in app source outside `bootstrap/`. Tests and declaration
|
|
12
|
+
* files are left out by {@link isAppSource}.
|
|
12
13
|
*/
|
|
13
14
|
export declare const resultThrowsCheck: Check;
|
|
14
15
|
/**
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The `result-throws` check: app source
|
|
3
|
-
*
|
|
2
|
+
* The `result-throws` check: app source returns `@sdxc/result` failures so handlers stay typed.
|
|
3
|
+
* Entry points in `bootstrap/` answer the runtime that calls them, whose callbacks report a
|
|
4
|
+
* failure by throwing, so they are left out.
|
|
4
5
|
*
|
|
5
6
|
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
6
7
|
* @copyright Sergio Xalambrí 2026
|
|
@@ -13,15 +14,15 @@ import { PROJECT_FOLDERS } from "../stats.js";
|
|
|
13
14
|
import { passed, skipped } from "./check.js";
|
|
14
15
|
import { isAppSource, listFiles } from "./files.js";
|
|
15
16
|
/**
|
|
16
|
-
* Warns on each `throw` statement in app source
|
|
17
|
-
* {@link isAppSource}.
|
|
17
|
+
* Warns on each `throw` statement in app source outside `bootstrap/`. Tests and declaration
|
|
18
|
+
* files are left out by {@link isAppSource}.
|
|
18
19
|
*/
|
|
19
20
|
export const resultThrowsCheck = {
|
|
20
21
|
name: "result-throws",
|
|
21
22
|
async run(context) {
|
|
22
23
|
let findings = [];
|
|
23
24
|
let files = 0;
|
|
24
|
-
for (let folder of PROJECT_FOLDERS) {
|
|
25
|
+
for (let folder of PROJECT_FOLDERS.filter((name) => name !== "bootstrap")) {
|
|
25
26
|
for (let file of (await listFiles(context.root, folder)).filter(isAppSource)) {
|
|
26
27
|
files += 1;
|
|
27
28
|
let source = await wrap(() => readFile(join(context.root, file), "utf8"));
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A source rule: one convention the project's modules follow, found per file by `find`, and the
|
|
3
|
+
* `sdxc doctor` check that reads every module in the app layout's folders for it. A rule is plain
|
|
4
|
+
* data, so a monorepo check can run the same `find` over folders outside one app.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { Check } from "./check.js";
|
|
10
|
+
/** One place a module breaks a rule. */
|
|
11
|
+
export interface SourceFinding {
|
|
12
|
+
/** The 1-based line it starts on. */
|
|
13
|
+
line: number;
|
|
14
|
+
/** What breaks the rule, short enough to follow `file:line:` in the report. */
|
|
15
|
+
message: string;
|
|
16
|
+
}
|
|
17
|
+
/** A convention every module of the project follows. */
|
|
18
|
+
export interface SourceRule {
|
|
19
|
+
/** The check's name in the report and for `--only` and `--skip`. */
|
|
20
|
+
name: string;
|
|
21
|
+
/** Whether tests follow the rule too; app code always does. */
|
|
22
|
+
tests: boolean;
|
|
23
|
+
/** Each place `source` breaks the rule; a module oxc cannot parse yields none. */
|
|
24
|
+
find(file: string, source: string): SourceFinding[];
|
|
25
|
+
/** The report's message when no module breaks the rule. */
|
|
26
|
+
passed: string;
|
|
27
|
+
/** The noun for one finding and for many, as in "Found 2 local consts". */
|
|
28
|
+
noun: [one: string, many: string];
|
|
29
|
+
/** The edit that clears a finding. */
|
|
30
|
+
fix: string;
|
|
31
|
+
}
|
|
32
|
+
/** Whether `rule` reads `file`: a module, and app code unless the rule covers tests. */
|
|
33
|
+
export declare function appliesTo(rule: SourceRule, file: string): boolean;
|
|
34
|
+
/** The `sdxc doctor` check that warns on every finding of `rule` across the app layout's folders. */
|
|
35
|
+
export declare function sourceCheck(rule: SourceRule): Check;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A source rule: one convention the project's modules follow, found per file by `find`, and the
|
|
3
|
+
* `sdxc doctor` check that reads every module in the app layout's folders for it. A rule is plain
|
|
4
|
+
* data, so a monorepo check can run the same `find` over folders outside one app.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { readFile } from "node:fs/promises";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
import { isFailure, wrap } from "@sdxc/result";
|
|
12
|
+
import { PROJECT_FOLDERS } from "../stats.js";
|
|
13
|
+
import { passed, skipped } from "./check.js";
|
|
14
|
+
import { isAppSource, isModule, listFiles } from "./files.js";
|
|
15
|
+
/** Whether `rule` reads `file`: a module, and app code unless the rule covers tests. */
|
|
16
|
+
export function appliesTo(rule, file) {
|
|
17
|
+
return rule.tests ? isModule(file) : isAppSource(file);
|
|
18
|
+
}
|
|
19
|
+
/** The `sdxc doctor` check that warns on every finding of `rule` across the app layout's folders. */
|
|
20
|
+
export function sourceCheck(rule) {
|
|
21
|
+
return {
|
|
22
|
+
name: rule.name,
|
|
23
|
+
async run(context) {
|
|
24
|
+
let findings = [];
|
|
25
|
+
let files = 0;
|
|
26
|
+
for (let folder of PROJECT_FOLDERS) {
|
|
27
|
+
for (let file of await listFiles(context.root, folder)) {
|
|
28
|
+
if (!appliesTo(rule, file))
|
|
29
|
+
continue;
|
|
30
|
+
files += 1;
|
|
31
|
+
let source = await wrap(() => readFile(join(context.root, file), "utf8"));
|
|
32
|
+
if (isFailure(source))
|
|
33
|
+
continue;
|
|
34
|
+
for (let found of rule.find(file, source.data)) {
|
|
35
|
+
findings.push(`${file}:${found.line}: ${found.message}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (files === 0)
|
|
40
|
+
return skipped("No app source to read");
|
|
41
|
+
if (findings.length === 0)
|
|
42
|
+
return passed(rule.passed);
|
|
43
|
+
let [one, many] = rule.noun;
|
|
44
|
+
return {
|
|
45
|
+
status: "warn",
|
|
46
|
+
message: `Found ${findings.length} ${findings.length === 1 ? one : many}`,
|
|
47
|
+
findings,
|
|
48
|
+
fix: rule.fix,
|
|
49
|
+
};
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `stack-imports` check: the project imports the library its stack settled on for each
|
|
3
|
+
* concern, so validation, UI and markup each have one way to be written: `remix/data-schema`
|
|
4
|
+
* for schemas, `remix/component` JSX for UI and server HTML, and `css()` mixins for styles.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { SourceFinding, SourceRule } from "./source-rule.js";
|
|
10
|
+
/** Every import, re-export or dynamic `import()` of a replaced library, tests included. */
|
|
11
|
+
export declare const STACK_IMPORTS_RULE: SourceRule;
|
|
12
|
+
/** Warns on each import of a library the stack replaces. */
|
|
13
|
+
export declare const stackImportsCheck: import("./check.js").Check;
|
|
14
|
+
/** Each place `source` loads a replaced library, with the library to use instead. */
|
|
15
|
+
export declare function findStackImports(file: string, source: string): SourceFinding[];
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `stack-imports` check: the project imports the library its stack settled on for each
|
|
3
|
+
* concern, so validation, UI and markup each have one way to be written: `remix/data-schema`
|
|
4
|
+
* for schemas, `remix/component` JSX for UI and server HTML, and `css()` mixins for styles.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { sourceCheck } from "./source-rule.js";
|
|
10
|
+
import { isNode, lineAt, parseModule, walk } from "./syntax.js";
|
|
11
|
+
/** Each replaced library, matched on the import's specifier. */
|
|
12
|
+
const REPLACEMENTS = [
|
|
13
|
+
{ specifier: /^zod(?:\/|$)/, use: "remix/data-schema" },
|
|
14
|
+
{
|
|
15
|
+
specifier: /^(?:react|react-dom|react-router|@remix-run\/react|preact)(?:\/|$)/,
|
|
16
|
+
use: "remix/component",
|
|
17
|
+
},
|
|
18
|
+
{ specifier: /^(?:tailwindcss|@tailwindcss\/[^/]+)(?:\/|$)/, use: "css() mixins" },
|
|
19
|
+
{ specifier: /^remix\/html-template(?:\/|$)/, use: "remix/component JSX rendered on the server" },
|
|
20
|
+
];
|
|
21
|
+
/** Nodes whose `source` names the module they load. */
|
|
22
|
+
const LOADS = new Set([
|
|
23
|
+
"ImportDeclaration",
|
|
24
|
+
"ImportExpression",
|
|
25
|
+
"ExportNamedDeclaration",
|
|
26
|
+
"ExportAllDeclaration",
|
|
27
|
+
]);
|
|
28
|
+
/** Every import, re-export or dynamic `import()` of a replaced library, tests included. */
|
|
29
|
+
export const STACK_IMPORTS_RULE = {
|
|
30
|
+
name: "stack-imports",
|
|
31
|
+
tests: true,
|
|
32
|
+
find: findStackImports,
|
|
33
|
+
passed: "Every import uses the stack's library for its concern",
|
|
34
|
+
noun: ["import of a replaced library", "imports of replaced libraries"],
|
|
35
|
+
fix: "Import the library named in each finding instead",
|
|
36
|
+
};
|
|
37
|
+
/** Warns on each import of a library the stack replaces. */
|
|
38
|
+
export const stackImportsCheck = sourceCheck(STACK_IMPORTS_RULE);
|
|
39
|
+
/** Each place `source` loads a replaced library, with the library to use instead. */
|
|
40
|
+
export function findStackImports(file, source) {
|
|
41
|
+
let parsed = parseModule(file, source);
|
|
42
|
+
if (parsed === null)
|
|
43
|
+
return [];
|
|
44
|
+
let findings = [];
|
|
45
|
+
walk(parsed.program, (node) => {
|
|
46
|
+
if (!LOADS.has(node.type) || !isNode(node.source))
|
|
47
|
+
return;
|
|
48
|
+
let specifier = node.source.value;
|
|
49
|
+
if (typeof specifier !== "string")
|
|
50
|
+
return;
|
|
51
|
+
let replacement = REPLACEMENTS.find((entry) => entry.specifier.test(specifier));
|
|
52
|
+
if (replacement === undefined)
|
|
53
|
+
return;
|
|
54
|
+
findings.push({
|
|
55
|
+
line: lineAt(source, node.start),
|
|
56
|
+
message: `imports ${specifier}; use ${replacement.use}`,
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
return findings;
|
|
60
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The parsed view of a module the source-rule checks read: its oxc AST, its comments, and a walk
|
|
3
|
+
* that hands every node its ancestors, so a rule decides on a node and where it sits in one pass.
|
|
4
|
+
*
|
|
5
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
6
|
+
* @copyright Sergio Xalambrí 2026
|
|
7
|
+
*/
|
|
8
|
+
import type { Comment } from "oxc-parser";
|
|
9
|
+
/** An oxc AST node, read by `type` and by whichever fields a rule needs. */
|
|
10
|
+
export interface SyntaxNode {
|
|
11
|
+
type: string;
|
|
12
|
+
start: number;
|
|
13
|
+
end: number;
|
|
14
|
+
[field: string]: unknown;
|
|
15
|
+
}
|
|
16
|
+
/** A module's AST and comments. */
|
|
17
|
+
export interface ParsedModule {
|
|
18
|
+
program: SyntaxNode;
|
|
19
|
+
comments: Comment[];
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The module oxc parses from `source`, or `null` when it cannot, so the type checker rather than
|
|
23
|
+
* doctor names the syntax error.
|
|
24
|
+
*/
|
|
25
|
+
export declare function parseModule(file: string, source: string): ParsedModule | null;
|
|
26
|
+
/** Calls `enter` on every node under `root`, depth first, with its ancestors outermost first. */
|
|
27
|
+
export declare function walk(root: SyntaxNode, enter: (node: SyntaxNode, ancestors: readonly SyntaxNode[]) => void): void;
|
|
28
|
+
/** Whether `value` is an AST node rather than a scalar or a plain field object. */
|
|
29
|
+
export declare function isNode(value: unknown): value is SyntaxNode;
|
|
30
|
+
/** The 1-based line holding character `index` of `source`. */
|
|
31
|
+
export declare function lineAt(source: string, index: number): number;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The parsed view of a module the source-rule checks read: its oxc AST, its comments, and a walk
|
|
3
|
+
* that hands every node its ancestors, so a rule decides on a node and where it sits in one pass.
|
|
4
|
+
*
|
|
5
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
6
|
+
* @copyright Sergio Xalambrí 2026
|
|
7
|
+
*/
|
|
8
|
+
import { parseSync } from "oxc-parser";
|
|
9
|
+
/**
|
|
10
|
+
* The module oxc parses from `source`, or `null` when it cannot, so the type checker rather than
|
|
11
|
+
* doctor names the syntax error.
|
|
12
|
+
*/
|
|
13
|
+
export function parseModule(file, source) {
|
|
14
|
+
let parsed = parseSync(file, source);
|
|
15
|
+
if (parsed.errors.length > 0)
|
|
16
|
+
return null;
|
|
17
|
+
return { program: parsed.program, comments: parsed.comments };
|
|
18
|
+
}
|
|
19
|
+
/** Calls `enter` on every node under `root`, depth first, with its ancestors outermost first. */
|
|
20
|
+
export function walk(root, enter) {
|
|
21
|
+
let ancestors = [];
|
|
22
|
+
let visit = (node) => {
|
|
23
|
+
enter(node, ancestors);
|
|
24
|
+
ancestors.push(node);
|
|
25
|
+
for (let child of children(node))
|
|
26
|
+
visit(child);
|
|
27
|
+
ancestors.pop();
|
|
28
|
+
};
|
|
29
|
+
visit(root);
|
|
30
|
+
}
|
|
31
|
+
/** The nodes held directly by `node`'s fields, in source order. */
|
|
32
|
+
function children(node) {
|
|
33
|
+
let found = [];
|
|
34
|
+
for (let [key, value] of Object.entries(node)) {
|
|
35
|
+
if (key === "type")
|
|
36
|
+
continue;
|
|
37
|
+
for (let item of Array.isArray(value) ? value : [value]) {
|
|
38
|
+
if (isNode(item))
|
|
39
|
+
found.push(item);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return found;
|
|
43
|
+
}
|
|
44
|
+
/** Whether `value` is an AST node rather than a scalar or a plain field object. */
|
|
45
|
+
export function isNode(value) {
|
|
46
|
+
return (typeof value === "object" && value !== null && typeof Reflect.get(value, "type") === "string");
|
|
47
|
+
}
|
|
48
|
+
/** The 1-based line holding character `index` of `source`. */
|
|
49
|
+
export function lineAt(source, index) {
|
|
50
|
+
let line = 1;
|
|
51
|
+
for (let at = 0; at < index && at < source.length; at++) {
|
|
52
|
+
if (source.charCodeAt(at) === 10)
|
|
53
|
+
line += 1;
|
|
54
|
+
}
|
|
55
|
+
return line;
|
|
56
|
+
}
|
package/dist/make/binding.d.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `sdxc make secret`, `make binding` and `make do`: edit `cloudflare.config.ts`, append
|
|
3
|
-
* `.env.example` for secrets, re-export a Durable Object from `bootstrap/worker.ts`, and
|
|
4
|
-
* `sdxc run typegen
|
|
3
|
+
* `.env.example` for secrets, re-export a Durable Object from `bootstrap/worker.ts`, and note
|
|
4
|
+
* `sdxc run typegen`, answering what changed for the project's `format` like every other kind.
|
|
5
5
|
*
|
|
6
6
|
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
7
|
* @copyright Sergio Xalambrí 2026
|
|
8
8
|
*/
|
|
9
9
|
import type { AnyCommandContext } from "@sdxc/cli";
|
|
10
10
|
import type { Result } from "@sdxc/result";
|
|
11
|
+
import type { Made } from "./index.js";
|
|
11
12
|
/** Binding kinds `make binding` accepts; `secret` also appends `.env.example`. */
|
|
12
13
|
export declare const BINDING_KINDS: readonly ["secret", "kv", "d1", "r2", "queue", "rateLimit", "sendEmail", "durableObject"];
|
|
13
14
|
/** One of {@link BINDING_KINDS}. */
|
|
@@ -16,27 +17,31 @@ export type BindingKind = (typeof BINDING_KINDS)[number];
|
|
|
16
17
|
export type DoRole = "model" | "service";
|
|
17
18
|
/** Context these makers need to print. */
|
|
18
19
|
export type BindingContext = Pick<AnyCommandContext, "stdout">;
|
|
19
|
-
/**
|
|
20
|
+
/**
|
|
21
|
+
* Plans and optionally writes a `bindings.<kind>()` entry for `NAME`; `--force` replaces an
|
|
22
|
+
* existing declaration of `NAME` where it stands.
|
|
23
|
+
*/
|
|
20
24
|
export declare function makeBinding(ctx: BindingContext, root: string, input: {
|
|
21
25
|
kind: BindingKind;
|
|
22
26
|
name: string;
|
|
23
27
|
dryRun: boolean;
|
|
24
28
|
force: boolean;
|
|
25
|
-
}): Promise<Result<
|
|
29
|
+
}): Promise<Result<Made, Error>>;
|
|
26
30
|
/** `make secret NAME` — shorthand for `make binding secret NAME`. */
|
|
27
31
|
export declare function makeSecret(ctx: BindingContext, root: string, input: {
|
|
28
32
|
name: string;
|
|
29
33
|
dryRun: boolean;
|
|
30
34
|
force: boolean;
|
|
31
|
-
}): Promise<Result<
|
|
35
|
+
}): Promise<Result<Made, Error>>;
|
|
32
36
|
/**
|
|
33
37
|
* Writes a Durable Object class stub, re-exports it from `bootstrap/worker.ts`, and declares
|
|
34
|
-
* the binding plus `exports.durableObject` in `cloudflare.config.ts`.
|
|
38
|
+
* the binding plus `exports.durableObject` in `cloudflare.config.ts`. A name ending in `do`,
|
|
39
|
+
* in any case, gets the `DO` suffix once: `feed-do` and `FeedDO` both write `FeedDO`.
|
|
35
40
|
*/
|
|
36
|
-
export declare function makeDo(ctx: BindingContext, root: string,
|
|
41
|
+
export declare function makeDo(ctx: BindingContext, root: string, input: {
|
|
37
42
|
name: string;
|
|
38
43
|
role: DoRole;
|
|
39
44
|
binding?: string;
|
|
40
45
|
dryRun: boolean;
|
|
41
46
|
force: boolean;
|
|
42
|
-
}): Promise<Result<
|
|
47
|
+
}): Promise<Result<Made, Error>>;
|