@nifrajs/cli 5.5.1 → 5.6.0
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/CHANGELOG.md +155 -0
- package/dist/cdn-check.d.ts +7 -6
- package/dist/cdn-check.d.ts.map +1 -1
- package/dist/cdn-check.js +13 -1
- package/dist/cdn-check.js.map +1 -1
- package/dist/check-diagnostics.d.ts.map +1 -1
- package/dist/check-diagnostics.js +5 -3
- package/dist/check-diagnostics.js.map +1 -1
- package/dist/check-scan.d.ts.map +1 -1
- package/dist/check-scan.js +29 -2
- package/dist/check-scan.js.map +1 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +14 -5
- package/dist/cli.js.map +1 -1
- package/dist/command-catalog.d.ts +14 -1
- package/dist/command-catalog.d.ts.map +1 -1
- package/dist/command-catalog.js +93 -0
- package/dist/command-catalog.js.map +1 -1
- package/dist/doctor.d.ts +9 -1
- package/dist/doctor.d.ts.map +1 -1
- package/dist/doctor.js +52 -0
- package/dist/doctor.js.map +1 -1
- package/dist/fix-recipes.d.ts.map +1 -1
- package/dist/fix-recipes.js +27 -0
- package/dist/fix-recipes.js.map +1 -1
- package/dist/frontend-guidance.d.ts +1 -1
- package/dist/frontend-guidance.d.ts.map +1 -1
- package/dist/frontend-guidance.js +17 -8
- package/dist/frontend-guidance.js.map +1 -1
- package/dist/introspect.js +1 -1
- package/dist/introspect.js.map +1 -1
- package/dist/load.d.ts +4 -1
- package/dist/load.d.ts.map +1 -1
- package/dist/load.js +8 -0
- package/dist/load.js.map +1 -1
- package/dist/mcp-exec.d.ts.map +1 -1
- package/dist/mcp-exec.js +57 -3
- package/dist/mcp-exec.js.map +1 -1
- package/dist/mcp-http.d.ts +2 -1
- package/dist/mcp-http.d.ts.map +1 -1
- package/dist/mcp-http.js +3 -2
- package/dist/mcp-http.js.map +1 -1
- package/dist/mcp-tool-meta.d.ts +21 -0
- package/dist/mcp-tool-meta.d.ts.map +1 -0
- package/dist/mcp-tool-meta.js +246 -0
- package/dist/mcp-tool-meta.js.map +1 -0
- package/dist/mcp.d.ts +3 -2
- package/dist/mcp.d.ts.map +1 -1
- package/dist/mcp.js +36 -98
- package/dist/mcp.js.map +1 -1
- package/dist/output-schemas.d.ts +8 -0
- package/dist/output-schemas.d.ts.map +1 -1
- package/dist/output-schemas.js +14 -0
- package/dist/output-schemas.js.map +1 -1
- package/dist/ppr-read-fix.d.ts +8 -0
- package/dist/ppr-read-fix.d.ts.map +1 -0
- package/dist/ppr-read-fix.js +356 -0
- package/dist/ppr-read-fix.js.map +1 -0
- package/dist/recipes/5.6.0.d.ts +10 -0
- package/dist/recipes/5.6.0.d.ts.map +1 -0
- package/dist/recipes/5.6.0.js +68 -0
- package/dist/recipes/5.6.0.js.map +1 -0
- package/dist/recipes/index.d.ts.map +1 -1
- package/dist/recipes/index.js +2 -0
- package/dist/recipes/index.js.map +1 -1
- package/dist/review.d.ts.map +1 -1
- package/dist/review.js +7 -2
- package/dist/review.js.map +1 -1
- package/dist/rules/codes.d.ts +8 -3
- package/dist/rules/codes.d.ts.map +1 -1
- package/dist/rules/codes.js +9 -4
- package/dist/rules/codes.js.map +1 -1
- package/dist/rules/explanations.d.ts.map +1 -1
- package/dist/rules/explanations.js +39 -13
- package/dist/rules/explanations.js.map +1 -1
- package/dist/rules/islands.d.ts +91 -0
- package/dist/rules/islands.d.ts.map +1 -1
- package/dist/rules/islands.js +757 -3
- package/dist/rules/islands.js.map +1 -1
- package/dist/rules/ppr.d.ts +16 -0
- package/dist/rules/ppr.d.ts.map +1 -0
- package/dist/rules/ppr.js +114 -0
- package/dist/rules/ppr.js.map +1 -0
- package/dist/rules/swap.d.ts +7 -0
- package/dist/rules/swap.d.ts.map +1 -0
- package/dist/rules/swap.js +338 -0
- package/dist/rules/swap.js.map +1 -0
- package/dist/scaffold-hole.d.ts +105 -0
- package/dist/scaffold-hole.d.ts.map +1 -0
- package/dist/scaffold-hole.js +688 -0
- package/dist/scaffold-hole.js.map +1 -0
- package/dist/scaffold.d.ts +5 -3
- package/dist/scaffold.d.ts.map +1 -1
- package/dist/scaffold.js +56 -46
- package/dist/scaffold.js.map +1 -1
- package/dist/verification-plan.d.ts.map +1 -1
- package/dist/verification-plan.js +12 -1
- package/dist/verification-plan.js.map +1 -1
- package/dist/web-app-options.d.ts +1 -1
- package/dist/web-app-options.d.ts.map +1 -1
- package/dist/web-app-options.js.map +1 -1
- package/docs/examples.json +63 -9
- package/docs/llms-full.txt +2060 -430
- package/docs/types.json +1300 -270
- package/package.json +11 -11
- package/src/cdn-check.ts +21 -7
- package/src/check-diagnostics.ts +5 -3
- package/src/check-scan.ts +34 -3
- package/src/cli.ts +14 -5
- package/src/command-catalog.ts +126 -0
- package/src/doctor.ts +44 -1
- package/src/fix-recipes.ts +28 -0
- package/src/frontend-guidance.ts +20 -8
- package/src/introspect.ts +1 -1
- package/src/load.ts +13 -0
- package/src/mcp-exec.ts +74 -3
- package/src/mcp-http.ts +3 -2
- package/src/mcp-tool-meta.ts +268 -0
- package/src/mcp.ts +35 -99
- package/src/output-schemas.ts +28 -0
- package/src/ppr-read-fix.ts +426 -0
- package/src/recipes/5.6.0.ts +69 -0
- package/src/recipes/index.ts +2 -0
- package/src/review.ts +7 -2
- package/src/rules/codes.ts +9 -4
- package/src/rules/explanations.ts +39 -13
- package/src/rules/islands.ts +875 -3
- package/src/rules/ppr.ts +143 -0
- package/src/rules/swap.ts +402 -0
- package/src/scaffold-hole.ts +880 -0
- package/src/scaffold.ts +60 -49
- package/src/verification-plan.ts +20 -1
- package/src/web-app-options.ts +1 -0
- package/dist/rules/nano.d.ts +0 -6
- package/dist/rules/nano.d.ts.map +0 -1
- package/dist/rules/nano.js +0 -273
- package/dist/rules/nano.js.map +0 -1
- package/src/rules/nano.ts +0 -307
|
@@ -0,0 +1,688 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `nifra scaffold hole <route> <name>`: add a partially prerendered hole to a route whose backend half
|
|
3
|
+
* exports `shellLoader`. The page gets the hole's definition, its entry in `boundaries` and a `<Hole>`
|
|
4
|
+
* where its markup ends; the backend half gets the hole's loader and output schema. Every edit lands
|
|
5
|
+
* at a position the project's TypeScript parser found, and nothing is written unless every edit has
|
|
6
|
+
* one. A Vue or Svelte page gets the code to paste instead.
|
|
7
|
+
*/
|
|
8
|
+
import { readFileSync } from "node:fs";
|
|
9
|
+
import { lstat, mkdir, readFile, writeFile } from "node:fs/promises";
|
|
10
|
+
import { dirname, relative, resolve, sep } from "node:path";
|
|
11
|
+
import { backendFileOf, pageExtension, routePathToFile } from "./scaffold.js";
|
|
12
|
+
/** The hole name rule `@nifrajs/web` enforces: a name becomes a DOM id and a hashed script. */
|
|
13
|
+
export const HOLE_NAME = /^(?=[a-z0-9-]{1,32}$)[a-z](?:-?[a-z0-9])*$/;
|
|
14
|
+
export const MAX_HOLES = 32;
|
|
15
|
+
export const ADAPTER = {
|
|
16
|
+
react: "@nifrajs/web-react/ppr",
|
|
17
|
+
preact: "@nifrajs/web-preact/ppr",
|
|
18
|
+
solid: "@nifrajs/web-solid/ppr",
|
|
19
|
+
};
|
|
20
|
+
/** `cart-count` → `cartCount`. */
|
|
21
|
+
const camel = (name) => name.replace(/-([a-z0-9])/g, (_, c) => c.toUpperCase());
|
|
22
|
+
const pascal = (name) => {
|
|
23
|
+
const value = camel(name);
|
|
24
|
+
return value.charAt(0).toUpperCase() + value.slice(1);
|
|
25
|
+
};
|
|
26
|
+
/** A property key or access for `name`: bare when it is an identifier, quoted when it has a hyphen. */
|
|
27
|
+
const keyOf = (name) => (name.includes("-") ? JSON.stringify(name) : name);
|
|
28
|
+
const accessOf = (name) => name.includes("-") ? `?.[${JSON.stringify(name)}]` : `?.${name}`;
|
|
29
|
+
/** `edits` applied to `source`; edits at one position land in the order they are listed. */
|
|
30
|
+
export function apply(source, edits) {
|
|
31
|
+
let out = source;
|
|
32
|
+
const order = edits.map((edit, index) => ({ edit, index }));
|
|
33
|
+
for (const { edit } of order.sort((a, b) => b.edit.at - a.edit.at || b.index - a.index)) {
|
|
34
|
+
out = out.slice(0, edit.at) + edit.text + out.slice(edit.at + (edit.remove ?? 0));
|
|
35
|
+
}
|
|
36
|
+
return out;
|
|
37
|
+
}
|
|
38
|
+
/** Leading whitespace of the line `pos` is on. */
|
|
39
|
+
export function indentAt(source, pos) {
|
|
40
|
+
const start = source.lastIndexOf("\n", pos - 1) + 1;
|
|
41
|
+
return /^[ \t]*/.exec(source.slice(start))?.[0] ?? "";
|
|
42
|
+
}
|
|
43
|
+
/** Whether only whitespace sits between the start of `pos`'s line and `pos`. */
|
|
44
|
+
function ownLine(source, pos) {
|
|
45
|
+
const start = source.lastIndexOf("\n", pos - 1) + 1;
|
|
46
|
+
return source.slice(start, pos).trim() === "";
|
|
47
|
+
}
|
|
48
|
+
/** Read from `node.modifiers`: TypeScript 7's API has no `getModifiers`. */
|
|
49
|
+
const hasModifier = (node, kind) => node.modifiers?.some((modifier) => modifier.kind === kind) === true;
|
|
50
|
+
const hasExport = (ts, node) => hasModifier(node, ts.SyntaxKind.ExportKeyword);
|
|
51
|
+
/** An exported `const <name> = <initializer>`, its initializer without `as`, `satisfies` or parens. */
|
|
52
|
+
function exportedConst(ts, sf, name) {
|
|
53
|
+
for (const statement of sf.statements) {
|
|
54
|
+
if (!ts.isVariableStatement(statement) || !hasExport(ts, statement))
|
|
55
|
+
continue;
|
|
56
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
57
|
+
if (!ts.isIdentifier(declaration.name) || declaration.name.text !== name)
|
|
58
|
+
continue;
|
|
59
|
+
let value = declaration.initializer;
|
|
60
|
+
while (value !== undefined &&
|
|
61
|
+
(ts.isAsExpression(value) ||
|
|
62
|
+
ts.isSatisfiesExpression(value) ||
|
|
63
|
+
ts.isParenthesizedExpression(value)))
|
|
64
|
+
value = value.expression;
|
|
65
|
+
return { statement, value };
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
function exportsName(ts, sf, name) {
|
|
71
|
+
return (exportedConst(ts, sf, name) !== undefined ||
|
|
72
|
+
sf.statements.some((statement) => ts.isFunctionDeclaration(statement) &&
|
|
73
|
+
hasExport(ts, statement) &&
|
|
74
|
+
statement.name?.text === name));
|
|
75
|
+
}
|
|
76
|
+
const propertyName = (ts, name) => ts.isIdentifier(name) || ts.isStringLiteral(name) || ts.isNoSubstitutionTemplateLiteral(name)
|
|
77
|
+
? name.text
|
|
78
|
+
: undefined;
|
|
79
|
+
/** Every name a module's top level binds, so a new one cannot shadow or clash. */
|
|
80
|
+
export function topLevelNames(ts, sf) {
|
|
81
|
+
const names = new Set();
|
|
82
|
+
const bind = (name) => {
|
|
83
|
+
if (ts.isIdentifier(name))
|
|
84
|
+
names.add(name.text);
|
|
85
|
+
else
|
|
86
|
+
for (const element of name.elements)
|
|
87
|
+
if (!ts.isOmittedExpression(element))
|
|
88
|
+
bind(element.name);
|
|
89
|
+
};
|
|
90
|
+
for (const statement of sf.statements) {
|
|
91
|
+
if (ts.isVariableStatement(statement))
|
|
92
|
+
for (const declaration of statement.declarationList.declarations)
|
|
93
|
+
bind(declaration.name);
|
|
94
|
+
else if ((ts.isFunctionDeclaration(statement) || ts.isClassDeclaration(statement)) &&
|
|
95
|
+
statement.name !== undefined)
|
|
96
|
+
names.add(statement.name.text);
|
|
97
|
+
else if (ts.isImportDeclaration(statement) && statement.importClause !== undefined) {
|
|
98
|
+
const clause = statement.importClause;
|
|
99
|
+
if (clause.name !== undefined)
|
|
100
|
+
names.add(clause.name.text);
|
|
101
|
+
const bindings = clause.namedBindings;
|
|
102
|
+
if (bindings !== undefined && ts.isNamespaceImport(bindings))
|
|
103
|
+
names.add(bindings.name.text);
|
|
104
|
+
if (bindings !== undefined && ts.isNamedImports(bindings))
|
|
105
|
+
for (const element of bindings.elements)
|
|
106
|
+
names.add(element.name.text);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return names;
|
|
110
|
+
}
|
|
111
|
+
/** The edit that makes `need` importable, or none when it already is. */
|
|
112
|
+
function ensureImport(ts, sf, need) {
|
|
113
|
+
const { module, name } = need;
|
|
114
|
+
const typeOnly = need.typeOnly === true;
|
|
115
|
+
const named = need.imported === undefined || need.imported === name ? name : `${need.imported} as ${name}`;
|
|
116
|
+
let last;
|
|
117
|
+
for (const statement of sf.statements) {
|
|
118
|
+
if (!ts.isImportDeclaration(statement))
|
|
119
|
+
continue;
|
|
120
|
+
last = statement;
|
|
121
|
+
if (!ts.isStringLiteral(statement.moduleSpecifier) || statement.moduleSpecifier.text !== module)
|
|
122
|
+
continue;
|
|
123
|
+
const clause = statement.importClause;
|
|
124
|
+
const bindings = clause?.namedBindings;
|
|
125
|
+
if (clause === undefined || bindings === undefined || !ts.isNamedImports(bindings))
|
|
126
|
+
continue;
|
|
127
|
+
if (bindings.elements.some((element) => element.name.text === name))
|
|
128
|
+
return undefined;
|
|
129
|
+
// A type-only declaration takes a type name as it is; a value one needs `type` on it.
|
|
130
|
+
if (!typeOnly && clause.isTypeOnly)
|
|
131
|
+
continue;
|
|
132
|
+
const specifier = typeOnly && !clause.isTypeOnly ? `type ${named}` : named;
|
|
133
|
+
const lastElement = bindings.elements.at(-1);
|
|
134
|
+
return lastElement === undefined
|
|
135
|
+
? { at: bindings.getEnd() - 1, text: specifier }
|
|
136
|
+
: { at: lastElement.getEnd(), text: `, ${specifier}` };
|
|
137
|
+
}
|
|
138
|
+
const line = `import ${typeOnly ? "type " : ""}{ ${named} } from ${JSON.stringify(module)}\n`;
|
|
139
|
+
return last === undefined
|
|
140
|
+
? { at: 0, text: line }
|
|
141
|
+
: { at: last.getEnd(), text: `\n${line.trimEnd()}` };
|
|
142
|
+
}
|
|
143
|
+
/** The TODO loader the scaffold writes. */
|
|
144
|
+
function scaffoldLoader(options) {
|
|
145
|
+
const context = options.shared === true ? "SharedHoleCtx" : "BoundaryRequestCtx";
|
|
146
|
+
const load = options.server === true
|
|
147
|
+
? `load: ({ params }: ${context}) => ({ id: params.id ?? "" }),\noutput: t.object({ id: t.string() }),`
|
|
148
|
+
: `load: (_ctx: ${context}) => ({ text: "TODO" }),\noutput: t.object({ text: t.string() }),`;
|
|
149
|
+
const comment = options.server === true
|
|
150
|
+
? "// Returns the server component's input, which reaches the browser in its record."
|
|
151
|
+
: options.shared === true
|
|
152
|
+
? "// The same for every visitor: no request; built once and kept for `revalidate` seconds."
|
|
153
|
+
: "// Runs for each visitor, under the hole's own deadline.";
|
|
154
|
+
return {
|
|
155
|
+
body: [comment, ...load.split("\n"), ...(options.shared === true ? ["revalidate: 60,"] : [])],
|
|
156
|
+
imports: [
|
|
157
|
+
{ module: "@nifrajs/schema", name: "t" },
|
|
158
|
+
{ module: "@nifrajs/web", name: context, typeOnly: true },
|
|
159
|
+
],
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
/** The backend half's edits: hole `name`'s loader entry, and the imports it uses. */
|
|
163
|
+
export function backendPlan(ts, sf, name, loader) {
|
|
164
|
+
const source = sf.text;
|
|
165
|
+
const edits = [];
|
|
166
|
+
const entry = (indent) => [
|
|
167
|
+
`${indent}${keyOf(name)}: {`,
|
|
168
|
+
...loader.body.map((line) => `${indent} ${line}`),
|
|
169
|
+
`${indent}},`,
|
|
170
|
+
].join("\n");
|
|
171
|
+
const loaders = exportedConst(ts, sf, "boundaryLoaders");
|
|
172
|
+
let keys = [];
|
|
173
|
+
if (loaders === undefined) {
|
|
174
|
+
const satisfies = /import\s+type\s*\{[^}]*\bRoute\b[^}]*\}\s*from\s*["']\.\/\+types\//.test(source)
|
|
175
|
+
? " satisfies Route.BoundaryLoaders"
|
|
176
|
+
: "";
|
|
177
|
+
edits.push({
|
|
178
|
+
at: source.trimEnd().length,
|
|
179
|
+
text: `\n\nexport const boundaryLoaders = {\n${entry(" ")}\n}${satisfies}`,
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
const object = loaders.value;
|
|
184
|
+
if (object === undefined || !ts.isObjectLiteralExpression(object))
|
|
185
|
+
return { edits, keys, reason: "`boundaryLoaders` is not an object literal" };
|
|
186
|
+
keys = object.properties.flatMap((property) => {
|
|
187
|
+
const key = property.name === undefined ? undefined : propertyName(ts, property.name);
|
|
188
|
+
return key === undefined ? [] : [key];
|
|
189
|
+
});
|
|
190
|
+
const close = object.getEnd() - 1;
|
|
191
|
+
const lastProperty = object.properties.at(-1);
|
|
192
|
+
if (lastProperty === undefined) {
|
|
193
|
+
edits.push({
|
|
194
|
+
at: object.getStart(sf),
|
|
195
|
+
remove: object.getWidth(sf),
|
|
196
|
+
text: `{\n${entry(" ")}\n}`,
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
else if (ownLine(source, close)) {
|
|
200
|
+
const indent = indentAt(source, lastProperty.getStart(sf));
|
|
201
|
+
if (!object.properties.hasTrailingComma)
|
|
202
|
+
edits.push({ at: lastProperty.getEnd(), text: "," });
|
|
203
|
+
const lineStart = source.lastIndexOf("\n", close - 1) + 1;
|
|
204
|
+
edits.push({ at: lineStart, text: `${entry(indent)}\n` });
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
return {
|
|
208
|
+
edits,
|
|
209
|
+
keys,
|
|
210
|
+
reason: "`boundaryLoaders` is written on one line; spread it over lines first",
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
for (const need of loader.imports) {
|
|
215
|
+
const edit = ensureImport(ts, sf, need);
|
|
216
|
+
if (edit !== undefined)
|
|
217
|
+
edits.push(edit);
|
|
218
|
+
}
|
|
219
|
+
return { edits, keys };
|
|
220
|
+
}
|
|
221
|
+
/** The component a page module exports as default, as a function node. */
|
|
222
|
+
export function defaultComponent(ts, sf) {
|
|
223
|
+
const local = (name) => {
|
|
224
|
+
for (const statement of sf.statements) {
|
|
225
|
+
if (ts.isFunctionDeclaration(statement) && statement.name?.text === name)
|
|
226
|
+
return statement;
|
|
227
|
+
if (!ts.isVariableStatement(statement))
|
|
228
|
+
continue;
|
|
229
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
230
|
+
const value = declaration.initializer;
|
|
231
|
+
if (ts.isIdentifier(declaration.name) &&
|
|
232
|
+
declaration.name.text === name &&
|
|
233
|
+
value !== undefined &&
|
|
234
|
+
(ts.isArrowFunction(value) || ts.isFunctionExpression(value)))
|
|
235
|
+
return value;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return undefined;
|
|
239
|
+
};
|
|
240
|
+
for (const statement of sf.statements) {
|
|
241
|
+
if (ts.isFunctionDeclaration(statement) && hasModifier(statement, ts.SyntaxKind.DefaultKeyword))
|
|
242
|
+
return statement;
|
|
243
|
+
if (ts.isExportAssignment(statement) && !statement.isExportEquals) {
|
|
244
|
+
const value = statement.expression;
|
|
245
|
+
if (ts.isArrowFunction(value) || ts.isFunctionExpression(value))
|
|
246
|
+
return value;
|
|
247
|
+
if (ts.isIdentifier(value))
|
|
248
|
+
return local(value.text);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return undefined;
|
|
252
|
+
}
|
|
253
|
+
/** The JSX a component returns: its one top-level `return`, or an arrow's expression body. */
|
|
254
|
+
export function returnedJsx(ts, component) {
|
|
255
|
+
let value;
|
|
256
|
+
const body = component.body;
|
|
257
|
+
if (body === undefined)
|
|
258
|
+
return undefined;
|
|
259
|
+
if (ts.isBlock(body)) {
|
|
260
|
+
const returns = body.statements.filter(ts.isReturnStatement);
|
|
261
|
+
if (returns.length !== 1)
|
|
262
|
+
return undefined;
|
|
263
|
+
value = returns[0]?.expression;
|
|
264
|
+
}
|
|
265
|
+
else
|
|
266
|
+
value = body;
|
|
267
|
+
while (value !== undefined && ts.isParenthesizedExpression(value))
|
|
268
|
+
value = value.expression;
|
|
269
|
+
return value !== undefined && (ts.isJsxElement(value) || ts.isJsxFragment(value))
|
|
270
|
+
? value
|
|
271
|
+
: undefined;
|
|
272
|
+
}
|
|
273
|
+
/** Every name a function binds, in its parameters and anywhere in its body. */
|
|
274
|
+
export function localNames(ts, fn, into) {
|
|
275
|
+
const visit = (node) => {
|
|
276
|
+
if ((ts.isVariableDeclaration(node) ||
|
|
277
|
+
ts.isParameter(node) ||
|
|
278
|
+
ts.isBindingElement(node) ||
|
|
279
|
+
ts.isFunctionDeclaration(node) ||
|
|
280
|
+
ts.isClassDeclaration(node)) &&
|
|
281
|
+
node.name !== undefined &&
|
|
282
|
+
ts.isIdentifier(node.name))
|
|
283
|
+
into.add(node.name.text);
|
|
284
|
+
ts.forEachChild(node, visit);
|
|
285
|
+
};
|
|
286
|
+
visit(fn);
|
|
287
|
+
}
|
|
288
|
+
/** A placeholder fallback: without one, a browser without JavaScript shows nothing in the hole. */
|
|
289
|
+
export const fallbackField = (framework) =>
|
|
290
|
+
// Solid: a function, since JSX evaluated at import would be one DOM node every page shares.
|
|
291
|
+
framework === "solid" ? "fallback: () => <p>Loading</p>," : "fallback: <p>Loading</p>,";
|
|
292
|
+
/** The definition the scaffold writes: a TODO render, or the server component's. */
|
|
293
|
+
function scaffoldDefinition(framework, options, component) {
|
|
294
|
+
return {
|
|
295
|
+
fields: [
|
|
296
|
+
`mode: "${options.shared === true ? "shared" : "dynamic"}",`,
|
|
297
|
+
...(options.interactive === true
|
|
298
|
+
? ["interactive: true, // ships its props and hydrates as its own root"]
|
|
299
|
+
: []),
|
|
300
|
+
...(options.timeout === undefined ? [] : [`timeout: ${options.timeout},`]),
|
|
301
|
+
component === undefined
|
|
302
|
+
? "render: (data: { readonly text: string }) => <p>{data.text}</p>,"
|
|
303
|
+
: `render: serverHole(${component.name}),`,
|
|
304
|
+
fallbackField(framework),
|
|
305
|
+
],
|
|
306
|
+
imports: component === undefined
|
|
307
|
+
? []
|
|
308
|
+
: [
|
|
309
|
+
{ module: "@nifrajs/web/server-components/hole", name: "serverHole" },
|
|
310
|
+
{ module: component.specifier, name: component.name },
|
|
311
|
+
],
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
/** The page's edits, for a JSX-family page: the definition, `boundaries`, imports and the `<Hole>`. */
|
|
315
|
+
export function pagePlan(ts, sf, name, framework, definition) {
|
|
316
|
+
const source = sf.text;
|
|
317
|
+
const edits = [];
|
|
318
|
+
// Listed first: the definition can land where the imports end, and goes below them.
|
|
319
|
+
const imports = [];
|
|
320
|
+
const taken = topLevelNames(ts, sf);
|
|
321
|
+
let ident = camel(name);
|
|
322
|
+
const none = [];
|
|
323
|
+
const refuse = (reason) => ({ edits, names: none, ident, reason });
|
|
324
|
+
const boundaries = exportedConst(ts, sf, "boundaries");
|
|
325
|
+
const names = [];
|
|
326
|
+
if (boundaries !== undefined) {
|
|
327
|
+
const list = boundaries.value;
|
|
328
|
+
if (list === undefined || !ts.isArrayLiteralExpression(list))
|
|
329
|
+
return refuse("`boundaries` is not an array literal");
|
|
330
|
+
for (const element of list.elements) {
|
|
331
|
+
// A boundary written inline, or declared in this module, names itself in its `name` field.
|
|
332
|
+
let value = element;
|
|
333
|
+
if (ts.isIdentifier(element)) {
|
|
334
|
+
value = undefined;
|
|
335
|
+
for (const statement of sf.statements)
|
|
336
|
+
if (ts.isVariableStatement(statement))
|
|
337
|
+
for (const declaration of statement.declarationList.declarations)
|
|
338
|
+
if (ts.isIdentifier(declaration.name) && declaration.name.text === element.text)
|
|
339
|
+
value = declaration.initializer;
|
|
340
|
+
}
|
|
341
|
+
while (value !== undefined && (ts.isAsExpression(value) || ts.isSatisfiesExpression(value)))
|
|
342
|
+
value = value.expression;
|
|
343
|
+
if (value === undefined || !ts.isObjectLiteralExpression(value))
|
|
344
|
+
continue;
|
|
345
|
+
for (const property of value.properties)
|
|
346
|
+
if (ts.isPropertyAssignment(property) &&
|
|
347
|
+
propertyName(ts, property.name) === "name" &&
|
|
348
|
+
ts.isStringLiteralLike(property.initializer))
|
|
349
|
+
names.push(property.initializer.text);
|
|
350
|
+
}
|
|
351
|
+
if (list.elements.length >= MAX_HOLES)
|
|
352
|
+
return refuse(`the page already declares ${list.elements.length} boundaries, the most a route may have`);
|
|
353
|
+
}
|
|
354
|
+
const page = defaultComponent(ts, sf);
|
|
355
|
+
if (page === undefined)
|
|
356
|
+
return refuse("the page has no default-exported component");
|
|
357
|
+
const jsx = returnedJsx(ts, page);
|
|
358
|
+
if (jsx === undefined)
|
|
359
|
+
return refuse("the page component does not return one JSX element the hole can go inside");
|
|
360
|
+
// The definition is a module name the component reads, so a name of the component's own shadows it.
|
|
361
|
+
localNames(ts, page, taken);
|
|
362
|
+
if (taken.has(ident))
|
|
363
|
+
ident = `${ident}Hole`;
|
|
364
|
+
if (taken.has(ident))
|
|
365
|
+
return refuse(`\`${ident}\` is already declared in the page`);
|
|
366
|
+
// How the component reads its boundary states.
|
|
367
|
+
const param = page.parameters[0];
|
|
368
|
+
if (param === undefined)
|
|
369
|
+
return refuse("the page component takes no props");
|
|
370
|
+
let states;
|
|
371
|
+
if (ts.isIdentifier(param.name))
|
|
372
|
+
states = `${param.name.text}.boundaries`;
|
|
373
|
+
else if (ts.isObjectBindingPattern(param.name)) {
|
|
374
|
+
const bound = param.name.elements.find((element) => (element.propertyName === undefined
|
|
375
|
+
? ts.isIdentifier(element.name) && element.name.text
|
|
376
|
+
: propertyName(ts, element.propertyName)) === "boundaries");
|
|
377
|
+
if (bound !== undefined && !ts.isIdentifier(bound.name))
|
|
378
|
+
return refuse("the page destructures `boundaries` further");
|
|
379
|
+
if (bound !== undefined && ts.isIdentifier(bound.name))
|
|
380
|
+
states = bound.name.text;
|
|
381
|
+
else {
|
|
382
|
+
states = taken.has("boundaries") ? "" : "boundaries";
|
|
383
|
+
if (states === "")
|
|
384
|
+
return refuse("`boundaries` is already a name in the page");
|
|
385
|
+
if (param.name.elements.some((element) => element.dotDotDotToken !== undefined))
|
|
386
|
+
return refuse("the page's props end in a rest element");
|
|
387
|
+
const lastElement = param.name.elements.at(-1);
|
|
388
|
+
edits.push(lastElement === undefined
|
|
389
|
+
? { at: param.name.getStart(sf) + 1, text: " boundaries " }
|
|
390
|
+
: { at: lastElement.getEnd(), text: ", boundaries" });
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
else
|
|
394
|
+
return refuse("the page component's props are not a name or an object pattern");
|
|
395
|
+
const type = param.type;
|
|
396
|
+
if (type !== undefined && ts.isTypeLiteralNode(type)) {
|
|
397
|
+
const has = type.members.some((member) => member.name !== undefined && propertyName(ts, member.name) === "boundaries");
|
|
398
|
+
if (!has) {
|
|
399
|
+
const lastMember = type.members.at(-1);
|
|
400
|
+
const member = "readonly boundaries?: BoundaryStates";
|
|
401
|
+
if (lastMember === undefined)
|
|
402
|
+
edits.push({ at: type.getStart(sf) + 1, text: ` ${member} ` });
|
|
403
|
+
else if (ownLine(source, type.getEnd() - 1)) {
|
|
404
|
+
const lineStart = source.lastIndexOf("\n", type.getEnd() - 2) + 1;
|
|
405
|
+
edits.push({
|
|
406
|
+
at: lineStart,
|
|
407
|
+
text: `${indentAt(source, lastMember.getStart(sf))}${member}\n`,
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
else {
|
|
411
|
+
const ends = source.slice(lastMember.getStart(sf), lastMember.getEnd()).trimEnd();
|
|
412
|
+
edits.push({
|
|
413
|
+
at: lastMember.getEnd(),
|
|
414
|
+
text: ends.endsWith(";") || ends.endsWith(",") ? ` ${member}` : `; ${member}`,
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
const typeImport = ensureImport(ts, sf, {
|
|
418
|
+
module: "@nifrajs/web",
|
|
419
|
+
name: "BoundaryStates",
|
|
420
|
+
typeOnly: true,
|
|
421
|
+
});
|
|
422
|
+
if (typeImport !== undefined)
|
|
423
|
+
imports.push(typeImport);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
const hole = `<Hole of={${ident}} state={${states}${accessOf(name)}} />`;
|
|
427
|
+
const replaces = definition.replaces;
|
|
428
|
+
if (replaces !== undefined) {
|
|
429
|
+
const at = replaces.getStart(sf);
|
|
430
|
+
edits.push({ at, remove: replaces.getEnd() - at, text: hole });
|
|
431
|
+
}
|
|
432
|
+
else {
|
|
433
|
+
const closing = ts.isJsxElement(jsx) ? jsx.closingElement : jsx.closingFragment;
|
|
434
|
+
const at = closing.getStart(sf);
|
|
435
|
+
edits.push({
|
|
436
|
+
at,
|
|
437
|
+
text: ownLine(source, at) ? ` ${hole}\n${indentAt(source, at)}` : hole,
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
const lines = [`name: ${JSON.stringify(name)},`, ...definition.fields].flatMap((field) => field.split("\n"));
|
|
441
|
+
const text = `const ${ident} = {\n${lines.map((line) => (line === "" ? "" : ` ${line}`)).join("\n")}\n} as const`;
|
|
442
|
+
if (boundaries !== undefined) {
|
|
443
|
+
const list = boundaries.value;
|
|
444
|
+
if (list === undefined || !ts.isArrayLiteralExpression(list))
|
|
445
|
+
return refuse("`boundaries` is not an array literal");
|
|
446
|
+
const lastElement = list.elements.at(-1);
|
|
447
|
+
edits.push(lastElement === undefined
|
|
448
|
+
? { at: list.getStart(sf) + 1, text: ident }
|
|
449
|
+
: { at: lastElement.getEnd(), text: `, ${ident}` });
|
|
450
|
+
edits.push({ at: boundaries.statement.getFullStart(), text: `\n\n${text}` });
|
|
451
|
+
}
|
|
452
|
+
else {
|
|
453
|
+
const anchor = sf.statements.find((statement) => (ts.isFunctionDeclaration(statement) &&
|
|
454
|
+
hasModifier(statement, ts.SyntaxKind.DefaultKeyword)) ||
|
|
455
|
+
ts.isExportAssignment(statement));
|
|
456
|
+
if (anchor === undefined)
|
|
457
|
+
return refuse("the page has no default export to place the hole above");
|
|
458
|
+
edits.push({
|
|
459
|
+
at: anchor.getFullStart(),
|
|
460
|
+
text: `\n\n${text}\nexport const boundaries = [${ident}]`,
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
for (const need of [{ module: ADAPTER[framework] ?? "", name: "Hole" }, ...definition.imports]) {
|
|
464
|
+
const edit = ensureImport(ts, sf, need);
|
|
465
|
+
if (edit !== undefined)
|
|
466
|
+
imports.push(edit);
|
|
467
|
+
}
|
|
468
|
+
return { edits: [...imports, ...edits], names, ident };
|
|
469
|
+
}
|
|
470
|
+
/** The server component a `--server` hole shows: its React view renders on the server only. */
|
|
471
|
+
function componentStub(name) {
|
|
472
|
+
return `import { t } from "@nifrajs/schema"
|
|
473
|
+
import { defineServerComponent } from "@nifrajs/web/server-components"
|
|
474
|
+
|
|
475
|
+
/** Rendered on the server for each visitor; the browser gets its HTML, never this file. */
|
|
476
|
+
export const ${name} = defineServerComponent({
|
|
477
|
+
input: t.object({ id: t.string() }),
|
|
478
|
+
props: t.object({ title: t.string() }),
|
|
479
|
+
// Refuse a visitor who may not see it: throw a Response such as 403.
|
|
480
|
+
authorize() {},
|
|
481
|
+
load: (_ctx, input) => ({ title: \`TODO: \${input.id}\` }),
|
|
482
|
+
view: ({ props }) => <section>{props.title}</section>,
|
|
483
|
+
})
|
|
484
|
+
`;
|
|
485
|
+
}
|
|
486
|
+
function snippetFor(framework, name, ident, options) {
|
|
487
|
+
const fields = [
|
|
488
|
+
`name: ${JSON.stringify(name)},`,
|
|
489
|
+
`mode: "${options.shared === true ? "shared" : "dynamic"}",`,
|
|
490
|
+
...(options.interactive === true ? ["interactive: true,"] : []),
|
|
491
|
+
...(options.timeout === undefined ? [] : [`timeout: ${options.timeout},`]),
|
|
492
|
+
];
|
|
493
|
+
if (framework === "vue")
|
|
494
|
+
return `<script lang="ts">
|
|
495
|
+
// Beside the page's other imports; a hole renders h() output.
|
|
496
|
+
import { Hole } from "@nifrajs/web-vue/ppr"
|
|
497
|
+
import { h } from "vue"
|
|
498
|
+
|
|
499
|
+
const ${ident} = {
|
|
500
|
+
${fields.map((line) => ` ${line}`).join("\n")}
|
|
501
|
+
render: (data: { readonly text: string }) => h("p", data.text),
|
|
502
|
+
fallback: h("p", "Loading"),
|
|
503
|
+
} as const
|
|
504
|
+
export const boundaries = [${ident}] // with any boundaries the page already has
|
|
505
|
+
</script>
|
|
506
|
+
|
|
507
|
+
<!-- In the template, where the hole renders (props from defineProps(["data", "boundaries"])): -->
|
|
508
|
+
<Hole :of="${ident}" :state="props.boundaries${accessOf(name)}" />`;
|
|
509
|
+
return `<script module>
|
|
510
|
+
// Beside the page's other imports; Text is any component that renders the data.
|
|
511
|
+
import Hole from "@nifrajs/web-svelte/hole"
|
|
512
|
+
import { ui } from "@nifrajs/web-svelte/ppr"
|
|
513
|
+
import Text from "../../frontend/Text.svelte"
|
|
514
|
+
|
|
515
|
+
const ${ident} = {
|
|
516
|
+
${fields.map((line) => ` ${line}`).join("\n")}
|
|
517
|
+
render: (data) => ui(Text, { text: data.text }),
|
|
518
|
+
}
|
|
519
|
+
export const boundaries = [${ident}] // with any boundaries the page already has
|
|
520
|
+
</script>
|
|
521
|
+
|
|
522
|
+
<!-- In the markup, where the hole renders (states from let { boundaries: states } = $props()): -->
|
|
523
|
+
<Hole of={${ident}} state={states${accessOf(name)}} />`;
|
|
524
|
+
}
|
|
525
|
+
/** `routes/tea/[id].tsx` → `tea/[id]`, the id `createServerComponents({ holes })` keys by. */
|
|
526
|
+
const routeIdOf = (page) => page.replace(/^routes\//, "").replace(/\.[^./]+$/, "");
|
|
527
|
+
async function exists(path) {
|
|
528
|
+
return (await lstat(path).catch(() => undefined)) !== undefined;
|
|
529
|
+
}
|
|
530
|
+
export function insideRoot(root, file) {
|
|
531
|
+
const base = resolve(root);
|
|
532
|
+
const target = resolve(base, file);
|
|
533
|
+
if (!target.startsWith(`${base}${sep}`))
|
|
534
|
+
throw new Error(`refusing to write outside the app: ${file}`);
|
|
535
|
+
return target;
|
|
536
|
+
}
|
|
537
|
+
/** The page and backend half `route` names: a URL path such as `/tea/:id`, or the page file. */
|
|
538
|
+
export function holeRouteFiles(route, framework) {
|
|
539
|
+
const page = /\.(tsx|jsx|vue|svelte)$/.test(route)
|
|
540
|
+
? route.startsWith("routes/")
|
|
541
|
+
? route
|
|
542
|
+
: `routes/${route.replace(/^\/+/, "")}`
|
|
543
|
+
: routePathToFile(route, pageExtension(framework));
|
|
544
|
+
return { page, backend: backendFileOf(page) };
|
|
545
|
+
}
|
|
546
|
+
/** The project's TypeScript with `files` opened: TypeScript 7 parses only the files its session has. */
|
|
547
|
+
export async function routeTypeScript(root, files) {
|
|
548
|
+
const { loadProjectTypeScript } = await import("./internal/typescript-import.js");
|
|
549
|
+
const base = resolve(root);
|
|
550
|
+
return loadProjectTypeScript(root, undefined, {
|
|
551
|
+
files,
|
|
552
|
+
read: (file) => {
|
|
553
|
+
const path = resolve(base, file);
|
|
554
|
+
if (!path.startsWith(`${base}${sep}`))
|
|
555
|
+
return undefined;
|
|
556
|
+
try {
|
|
557
|
+
return readFileSync(path, "utf8");
|
|
558
|
+
}
|
|
559
|
+
catch {
|
|
560
|
+
return undefined;
|
|
561
|
+
}
|
|
562
|
+
},
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* Add hole `name` to the route at `route` (a URL path such as `/tea/:id`, or its page file). Nothing is
|
|
567
|
+
* written when `write` is false, or when any edit cannot be placed.
|
|
568
|
+
*/
|
|
569
|
+
export async function scaffoldHole(root, route, name, framework, options, ts, write = true) {
|
|
570
|
+
const { page, backend } = holeRouteFiles(route, framework);
|
|
571
|
+
const result = (fields) => ({
|
|
572
|
+
page,
|
|
573
|
+
backend,
|
|
574
|
+
changed: [],
|
|
575
|
+
snippets: [],
|
|
576
|
+
next: [],
|
|
577
|
+
...fields,
|
|
578
|
+
});
|
|
579
|
+
const refuse = (reason) => result({ ok: false, reason });
|
|
580
|
+
if (!HOLE_NAME.test(name))
|
|
581
|
+
return refuse(`"${name}" is not a hole name: 1 to 32 lowercase letters, digits and single hyphens, starting with a letter`);
|
|
582
|
+
if (framework === "vanilla")
|
|
583
|
+
return refuse("partial prerendering needs a framework adapter's Hole; a vanilla page has none");
|
|
584
|
+
if (options.timeout !== undefined &&
|
|
585
|
+
!(Number.isInteger(options.timeout) && options.timeout >= 1 && options.timeout <= 30000))
|
|
586
|
+
return refuse("--timeout is a whole number of milliseconds from 1 to 30000");
|
|
587
|
+
if (options.server === true && (options.shared === true || options.interactive === true))
|
|
588
|
+
return refuse("a hole that shows a server component is dynamic and not interactive");
|
|
589
|
+
// Another adapter's server view is a native component in a file of its own, which the scaffold
|
|
590
|
+
// cannot write for it.
|
|
591
|
+
if (options.server === true && framework !== "react")
|
|
592
|
+
return refuse("--server writes a React server component; for this adapter, define the component with nativeServerView first (see the server components docs), then render it with serverHole(Component)");
|
|
593
|
+
const backendPath = insideRoot(root, backend);
|
|
594
|
+
const pagePath = insideRoot(root, page);
|
|
595
|
+
if (!(await exists(pagePath)))
|
|
596
|
+
return refuse(`no page at ${page}`);
|
|
597
|
+
if (!(await exists(backendPath)))
|
|
598
|
+
return refuse(`${page} is not partially prerendered: it has no backend half. Add ${backend} with \`export const shellLoader\` and \`shellOutput\` first (see the partial prerendering docs)`);
|
|
599
|
+
const backendSource = await readFile(backendPath, "utf8");
|
|
600
|
+
const backendFile = ts.createSourceFile(backend, backendSource, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
|
|
601
|
+
if (!exportsName(ts, backendFile, "shellLoader"))
|
|
602
|
+
return refuse(`${page} is not partially prerendered: ${backend} exports no \`shellLoader\`. Add it, with \`shellOutput\`, first (see the partial prerendering docs)`);
|
|
603
|
+
const back = backendPlan(ts, backendFile, name, scaffoldLoader(options));
|
|
604
|
+
if (back.reason !== undefined)
|
|
605
|
+
return refuse(`${backend}: ${back.reason}`);
|
|
606
|
+
if (back.keys.includes(name))
|
|
607
|
+
return refuse(`${backend} already has a loader for "${name}"`);
|
|
608
|
+
if (back.keys.length >= MAX_HOLES)
|
|
609
|
+
return refuse(`${backend} already has ${back.keys.length} hole loaders, the most a route may have`);
|
|
610
|
+
const component = options.server === true
|
|
611
|
+
? {
|
|
612
|
+
name: pascal(name),
|
|
613
|
+
file: `components/${pascal(name)}.component.server.tsx`,
|
|
614
|
+
}
|
|
615
|
+
: undefined;
|
|
616
|
+
const specifier = component === undefined
|
|
617
|
+
? undefined
|
|
618
|
+
: (() => {
|
|
619
|
+
const rel = relative(dirname(pagePath), resolve(root, component.file))
|
|
620
|
+
.split(sep)
|
|
621
|
+
.join("/");
|
|
622
|
+
return rel.startsWith(".") ? rel : `./${rel}`;
|
|
623
|
+
})();
|
|
624
|
+
const snippets = [];
|
|
625
|
+
const writes = [];
|
|
626
|
+
if (ADAPTER[framework] !== undefined) {
|
|
627
|
+
const pageSource = await readFile(pagePath, "utf8");
|
|
628
|
+
const pageFile = ts.createSourceFile(page, pageSource, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
|
|
629
|
+
const front = pagePlan(ts, pageFile, name, framework, scaffoldDefinition(framework, options, component === undefined || specifier === undefined
|
|
630
|
+
? undefined
|
|
631
|
+
: { name: component.name, specifier }));
|
|
632
|
+
if (front.reason !== undefined)
|
|
633
|
+
return refuse(`${page}: ${front.reason}`);
|
|
634
|
+
if (front.names.includes(name))
|
|
635
|
+
return refuse(`${page} already declares a boundary named "${name}"`);
|
|
636
|
+
writes.push({ file: page, path: pagePath, text: apply(pageSource, front.edits) });
|
|
637
|
+
}
|
|
638
|
+
else {
|
|
639
|
+
snippets.push({ file: page, code: snippetFor(framework, name, camel(name), options) });
|
|
640
|
+
}
|
|
641
|
+
writes.push({ file: backend, path: backendPath, text: apply(backendSource, back.edits) });
|
|
642
|
+
if (component !== undefined) {
|
|
643
|
+
const path = insideRoot(root, component.file);
|
|
644
|
+
if (await exists(path))
|
|
645
|
+
return refuse(`${component.file} already exists`);
|
|
646
|
+
writes.push({ file: component.file, path, text: componentStub(component.name), create: true });
|
|
647
|
+
}
|
|
648
|
+
const next = [];
|
|
649
|
+
if (component !== undefined) {
|
|
650
|
+
next.push(`List ${component.name} for this route in the server entry: createServerComponents({ holes: { ${JSON.stringify(routeIdOf(page))}: { components: [${component.name}] } } }).`, "Call holeComponents() with the adapter's component client in the client entry, before the router mounts.", "Build with the coordinated compiler (buildServerComponents).");
|
|
651
|
+
}
|
|
652
|
+
if (snippets.length > 0)
|
|
653
|
+
next.push(`Place the hole in ${page} from the snippet.`);
|
|
654
|
+
next.push(`Replace the TODO loader in ${backend}, and its output schema with what the hole renders.`);
|
|
655
|
+
if (!write)
|
|
656
|
+
return result({
|
|
657
|
+
ok: true,
|
|
658
|
+
snippets,
|
|
659
|
+
next,
|
|
660
|
+
preview: writes.map((entry) => ({ file: entry.file, text: entry.text })),
|
|
661
|
+
});
|
|
662
|
+
for (const entry of writes) {
|
|
663
|
+
if (entry.create === true) {
|
|
664
|
+
await mkdir(dirname(entry.path), { recursive: true });
|
|
665
|
+
await writeFile(entry.path, entry.text, { flag: "wx" });
|
|
666
|
+
}
|
|
667
|
+
else
|
|
668
|
+
await writeFile(entry.path, entry.text, "utf8");
|
|
669
|
+
}
|
|
670
|
+
// The page's `./+types` module now names the new hole, so `Route.BoundaryLoaders` checks its key.
|
|
671
|
+
const { refreshRouteTypes } = await import("./route-types.js");
|
|
672
|
+
refreshRouteTypes(root, () => { });
|
|
673
|
+
return result({ ok: true, changed: writes.map((entry) => entry.file), snippets, next });
|
|
674
|
+
}
|
|
675
|
+
/** The MCP tool's answer: what was written or would be, and what is left. */
|
|
676
|
+
export function renderHoleScaffold(name, framework, out) {
|
|
677
|
+
const head = `# Scaffold hole \`${name}\` on \`${out.page}\` (${framework})`;
|
|
678
|
+
if (!out.ok)
|
|
679
|
+
return `${head}\n\nNot scaffolded: ${out.reason ?? "unknown reason"}`;
|
|
680
|
+
const status = out.preview === undefined
|
|
681
|
+
? `**Written:** ${out.changed.map((file) => `\`${file}\``).join(", ")}`
|
|
682
|
+
: "**Dry run:** pass `write: true` to apply these files.";
|
|
683
|
+
const previews = (out.preview ?? []).map((entry) => `\n\n\`${entry.file}\`:\n\n\`\`\`tsx\n${entry.text}\`\`\``);
|
|
684
|
+
const snippets = out.snippets.map((snippet) => `\n\nPaste into \`${snippet.file}\`:\n\n\`\`\`${framework}\n${snippet.code}\n\`\`\``);
|
|
685
|
+
const next = out.next.map((step) => `- ${step}`).join("\n");
|
|
686
|
+
return `${head}\n\n${status}${previews.join("")}${snippets.join("")}\n\n**Next:**\n${next}`;
|
|
687
|
+
}
|
|
688
|
+
//# sourceMappingURL=scaffold-hole.js.map
|