@nifrajs/cli 5.17.0 → 5.18.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/CHANGELOG.md +67 -0
- package/dist/check-diagnostics.d.ts +7 -0
- package/dist/check-diagnostics.d.ts.map +1 -1
- package/dist/check-diagnostics.js +3 -0
- package/dist/check-diagnostics.js.map +1 -1
- package/dist/check-scan-sql.d.ts +95 -0
- package/dist/check-scan-sql.d.ts.map +1 -0
- package/dist/check-scan-sql.js +1190 -0
- package/dist/check-scan-sql.js.map +1 -0
- package/dist/check-scan.d.ts +5 -93
- package/dist/check-scan.d.ts.map +1 -1
- package/dist/check-scan.js +20 -1207
- package/dist/check-scan.js.map +1 -1
- package/dist/check.d.ts.map +1 -1
- package/dist/check.js +9 -0
- package/dist/check.js.map +1 -1
- package/dist/cli.d.ts +2 -6
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +249 -80
- package/dist/cli.js.map +1 -1
- package/dist/command-catalog.d.ts +3 -0
- package/dist/command-catalog.d.ts.map +1 -1
- package/dist/command-catalog.js +18 -4
- package/dist/command-catalog.js.map +1 -1
- package/dist/frontend-guidance.js +1 -1
- package/dist/frontend-guidance.js.map +1 -1
- package/dist/internal/a11y-element.d.ts +33 -0
- package/dist/internal/a11y-element.d.ts.map +1 -0
- package/dist/internal/a11y-element.js +124 -0
- package/dist/internal/a11y-element.js.map +1 -0
- package/dist/internal/a11y-jsx.d.ts +12 -0
- package/dist/internal/a11y-jsx.d.ts.map +1 -0
- package/dist/internal/a11y-jsx.js +42 -0
- package/dist/internal/a11y-jsx.js.map +1 -0
- package/dist/internal/a11y-template.d.ts +11 -0
- package/dist/internal/a11y-template.d.ts.map +1 -0
- package/dist/internal/a11y-template.js +56 -0
- package/dist/internal/a11y-template.js.map +1 -0
- package/dist/internal/aria-data.d.ts +23 -0
- package/dist/internal/aria-data.d.ts.map +1 -0
- package/dist/internal/aria-data.js +220 -0
- package/dist/internal/aria-data.js.map +1 -0
- package/dist/internal/edit-distance.d.ts +3 -0
- package/dist/internal/edit-distance.d.ts.map +1 -0
- package/dist/internal/edit-distance.js +12 -0
- package/dist/internal/edit-distance.js.map +1 -0
- package/dist/internal/imported-as.d.ts +10 -0
- package/dist/internal/imported-as.d.ts.map +1 -0
- package/dist/internal/imported-as.js +23 -0
- package/dist/internal/imported-as.js.map +1 -0
- package/dist/internal/source-facts.d.ts +3 -0
- package/dist/internal/source-facts.d.ts.map +1 -1
- package/dist/internal/source-facts.js +2 -0
- package/dist/internal/source-facts.js.map +1 -1
- package/dist/internal/template-markup.d.ts +58 -0
- package/dist/internal/template-markup.d.ts.map +1 -0
- package/dist/internal/template-markup.js +221 -0
- package/dist/internal/template-markup.js.map +1 -0
- package/dist/internal/typescript-import.d.ts.map +1 -1
- package/dist/internal/typescript-import.js +11 -0
- package/dist/internal/typescript-import.js.map +1 -1
- package/dist/mcp-exec.d.ts.map +1 -1
- package/dist/mcp-exec.js +37 -23
- package/dist/mcp-exec.js.map +1 -1
- package/dist/mcp-http.js +1 -1
- package/dist/mcp-io.d.ts.map +1 -1
- package/dist/mcp-io.js +5 -3
- package/dist/mcp-io.js.map +1 -1
- package/dist/mcp-root.js +1 -1
- package/dist/mcp-root.js.map +1 -1
- package/dist/mcp-ws.d.ts.map +1 -1
- package/dist/mcp-ws.js +3 -2
- package/dist/mcp-ws.js.map +1 -1
- package/dist/migrate-layout-config.d.ts +63 -0
- package/dist/migrate-layout-config.d.ts.map +1 -0
- package/dist/migrate-layout-config.js +507 -0
- package/dist/migrate-layout-config.js.map +1 -0
- package/dist/migrate-layout-split.d.ts +46 -0
- package/dist/migrate-layout-split.d.ts.map +1 -0
- package/dist/migrate-layout-split.js +687 -0
- package/dist/migrate-layout-split.js.map +1 -0
- package/dist/migrate-layout.d.ts +4 -24
- package/dist/migrate-layout.d.ts.map +1 -1
- package/dist/migrate-layout.js +8 -1183
- package/dist/migrate-layout.js.map +1 -1
- package/dist/output-schemas.d.ts.map +1 -1
- package/dist/output-schemas.js +78 -10
- package/dist/output-schemas.js.map +1 -1
- package/dist/recipes/5.18.0.d.ts +9 -0
- package/dist/recipes/5.18.0.d.ts.map +1 -0
- package/dist/recipes/5.18.0.js +56 -0
- package/dist/recipes/5.18.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 +6 -0
- package/dist/review.js.map +1 -1
- package/dist/rules/a11y.d.ts +5 -0
- package/dist/rules/a11y.d.ts.map +1 -0
- package/dist/rules/a11y.js +78 -0
- package/dist/rules/a11y.js.map +1 -0
- package/dist/rules/codes.d.ts +6 -0
- package/dist/rules/codes.d.ts.map +1 -1
- package/dist/rules/codes.js +6 -0
- package/dist/rules/codes.js.map +1 -1
- package/dist/rules/comment-markers.js +1 -1
- package/dist/rules/comment-markers.js.map +1 -1
- package/dist/rules/explanations.d.ts.map +1 -1
- package/dist/rules/explanations.js +30 -0
- package/dist/rules/explanations.js.map +1 -1
- package/dist/rules/host-exposure.d.ts +23 -0
- package/dist/rules/host-exposure.d.ts.map +1 -0
- package/dist/rules/host-exposure.js +380 -0
- package/dist/rules/host-exposure.js.map +1 -0
- package/dist/rules/internal-links.d.ts.map +1 -1
- package/dist/rules/internal-links.js +16 -141
- package/dist/rules/internal-links.js.map +1 -1
- package/dist/rules/islands.d.ts +54 -0
- package/dist/rules/islands.d.ts.map +1 -1
- package/dist/rules/islands.js +577 -80
- package/dist/rules/islands.js.map +1 -1
- package/dist/rules/security.d.ts +1 -0
- package/dist/rules/security.d.ts.map +1 -1
- package/dist/rules/security.js +34 -7
- package/dist/rules/security.js.map +1 -1
- package/dist/runtime-differences.js +3 -3
- package/dist/runtime-differences.js.map +1 -1
- package/dist/scaffold.d.ts +1 -1
- package/dist/scaffold.d.ts.map +1 -1
- package/dist/scaffold.js +2 -2
- package/dist/scaffold.js.map +1 -1
- package/dist/source-rewrites.d.ts.map +1 -1
- package/dist/source-rewrites.js +2 -19
- package/dist/source-rewrites.js.map +1 -1
- package/docs/examples.json +4 -4
- package/docs/llms-full.txt +422 -42
- package/docs/types.json +466 -56
- package/package.json +12 -12
- package/src/check-diagnostics.ts +11 -0
- package/src/check-scan-sql.ts +1746 -0
- package/src/check-scan.ts +34 -1754
- package/src/check.ts +10 -1
- package/src/cli.ts +258 -98
- package/src/command-catalog.ts +21 -4
- package/src/frontend-guidance.ts +1 -1
- package/src/internal/a11y-element.ts +227 -0
- package/src/internal/a11y-jsx.ts +54 -0
- package/src/internal/a11y-template.ts +74 -0
- package/src/internal/aria-data.ts +242 -0
- package/src/internal/edit-distance.ts +16 -0
- package/src/internal/imported-as.ts +30 -0
- package/src/internal/source-facts.ts +4 -0
- package/src/internal/template-markup.ts +270 -0
- package/src/internal/typescript-import.ts +15 -0
- package/src/mcp-exec.ts +43 -22
- package/src/mcp-http.ts +1 -1
- package/src/mcp-io.ts +5 -3
- package/src/mcp-root.ts +1 -1
- package/src/mcp-ws.ts +3 -2
- package/src/migrate-layout-config.ts +615 -0
- package/src/migrate-layout-split.ts +870 -0
- package/src/migrate-layout.ts +37 -1468
- package/src/output-schemas.ts +85 -17
- package/src/recipes/5.18.0.ts +63 -0
- package/src/recipes/index.ts +2 -0
- package/src/review.ts +6 -0
- package/src/rules/a11y.ts +119 -0
- package/src/rules/codes.ts +6 -0
- package/src/rules/comment-markers.ts +2 -2
- package/src/rules/explanations.ts +30 -0
- package/src/rules/host-exposure.ts +531 -0
- package/src/rules/internal-links.ts +21 -154
- package/src/rules/islands.ts +698 -86
- package/src/rules/security.ts +48 -7
- package/src/runtime-differences.ts +3 -3
- package/src/scaffold.ts +3 -3
- package/src/source-rewrites.ts +2 -15
package/dist/migrate-layout.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync,
|
|
1
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync, } from "node:fs";
|
|
2
2
|
import { builtinModules } from "node:module";
|
|
3
|
-
import { dirname, extname, isAbsolute, join, posix, relative, resolve
|
|
4
|
-
import { BACKEND_ROUTE_EXPORTS } from "@nifrajs/web/route-manifest";
|
|
5
|
-
import { parseZoneConfig } from "@nifrajs/web/zones";
|
|
3
|
+
import { dirname, extname, isAbsolute, join, posix, relative, resolve } from "node:path";
|
|
6
4
|
import { BACKEND_APP_FILE, CONFIG_FILE, FRAMEWORK_FILE, retiredRootFiles } from "./app-files.js";
|
|
7
5
|
import { codeUnitOrder } from "./internal/code-unit-order.js";
|
|
8
6
|
import { configPathReader } from "./internal/config-paths.js";
|
|
@@ -15,9 +13,8 @@ import { importTypeScript } from "./internal/typescript-import.js";
|
|
|
15
13
|
import { planOutputSchemas } from "./output-schemas.js";
|
|
16
14
|
import { missingOutputSchemas } from "./rules/data-guard.js";
|
|
17
15
|
import { apiPrefixFix } from "./rules/pages.js";
|
|
18
|
-
const BACKEND_EXPORTS = BACKEND_ROUTE_EXPORTS;
|
|
19
16
|
const ROUTE_FILE = /\.(?:tsx|jsx|svelte|vue|mdx)$/;
|
|
20
|
-
const SOURCE_FILE = /\.(?:[cm]?[jt]sx?|svelte|vue|mdx)$/;
|
|
17
|
+
export const SOURCE_FILE = /\.(?:[cm]?[jt]sx?|svelte|vue|mdx)$/;
|
|
21
18
|
const RESOLVE_EXTENSIONS = [
|
|
22
19
|
".ts",
|
|
23
20
|
".tsx",
|
|
@@ -38,7 +35,7 @@ const RETIRED_SPECIFIERS = {
|
|
|
38
35
|
const TOOLING_FILE = /(?:\.(?:json|jsonc|ya?ml|toml|sh|bash|zsh|ps1)|(?:^|\/)(?:Dockerfile|Makefile|Procfile))$/;
|
|
39
36
|
const SELF_LOCATION = /\bimport\.meta\.(?:dirname|dir|filename|url)\b|\b__(?:dirname|filename)\b/;
|
|
40
37
|
const SKIP_DIRS = new Set(["node_modules", "dist", "build", "coverage", ".git", ".vite", "public"]);
|
|
41
|
-
const ZONE_DIRS = new Set(["frontend", "backend", "shared", "routes", "public"]);
|
|
38
|
+
export const ZONE_DIRS = new Set(["frontend", "backend", "shared", "routes", "public"]);
|
|
42
39
|
const FOLDER_SIDES = ["frontend", "backend", "shared"];
|
|
43
40
|
const DATA_FILE = /\.(?:json|jsonc|json5|ndjson|ya?ml|toml|csv|tsv|txt|xml|sql)$/;
|
|
44
41
|
const MOCK_CALLS = new Set([
|
|
@@ -88,684 +85,9 @@ function listFiles(root) {
|
|
|
88
85
|
walk(root);
|
|
89
86
|
return out.sort();
|
|
90
87
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
else
|
|
95
|
-
for (const element of name.elements)
|
|
96
|
-
if (!ts.isOmittedExpression(element))
|
|
97
|
-
bindingNames(ts, element.name, into);
|
|
98
|
-
}
|
|
99
|
-
const hasModifier = (ts, node, kind) => (ts.canHaveModifiers(node) ? (ts.getModifiers(node) ?? []) : []).some((m) => m.kind === kind);
|
|
100
|
-
function jsxBindings(ts, source) {
|
|
101
|
-
const comments = (ts.getLeadingCommentRanges(source, 0) ?? [])
|
|
102
|
-
.map(({ pos, end }) => source.slice(pos, end))
|
|
103
|
-
.join("\n");
|
|
104
|
-
if (/@jsxImportSource\s/.test(comments))
|
|
105
|
-
return { element: [], fragment: [] };
|
|
106
|
-
const root = (pragma) => new RegExp(`@${pragma}\\s+([A-Za-z_$][\\w$]*)`).exec(comments)?.[1];
|
|
107
|
-
const element = root("jsx") ?? "React";
|
|
108
|
-
return { element: [element], fragment: [root("jsxFrag") ?? element] };
|
|
109
|
-
}
|
|
110
|
-
function references(ts, node, jsx) {
|
|
111
|
-
const value = new Set();
|
|
112
|
-
const type = new Set();
|
|
113
|
-
const visit = (current, inType) => {
|
|
114
|
-
if (ts.isJsxOpeningElement(current) || ts.isJsxSelfClosingElement(current))
|
|
115
|
-
for (const name of jsx.element)
|
|
116
|
-
value.add(name);
|
|
117
|
-
else if (ts.isJsxFragment(current))
|
|
118
|
-
for (const name of jsx.fragment)
|
|
119
|
-
value.add(name);
|
|
120
|
-
const typed = inType ||
|
|
121
|
-
ts.isTypeNode(current) ||
|
|
122
|
-
(ts.isHeritageClause(current) && current.token === ts.SyntaxKind.ImplementsKeyword);
|
|
123
|
-
if (ts.isIdentifier(current)) {
|
|
124
|
-
const parent = current.parent;
|
|
125
|
-
const isName = parent !== undefined &&
|
|
126
|
-
((ts.isPropertyAccessExpression(parent) && parent.name === current) ||
|
|
127
|
-
(ts.isPropertyAssignment(parent) && parent.name === current) ||
|
|
128
|
-
(ts.isPropertySignature(parent) && parent.name === current) ||
|
|
129
|
-
(ts.isPropertyDeclaration(parent) && parent.name === current) ||
|
|
130
|
-
(ts.isMethodDeclaration(parent) && parent.name === current) ||
|
|
131
|
-
(ts.isMethodSignature(parent) && parent.name === current) ||
|
|
132
|
-
(ts.isQualifiedName(parent) && parent.right === current) ||
|
|
133
|
-
(ts.isJsxAttribute(parent) && parent.name === current) ||
|
|
134
|
-
(ts.isBindingElement(parent) && parent.propertyName === current) ||
|
|
135
|
-
ts.isImportSpecifier(parent) ||
|
|
136
|
-
ts.isExportSpecifier(parent) ||
|
|
137
|
-
ts.isImportClause(parent) ||
|
|
138
|
-
ts.isNamespaceImport(parent));
|
|
139
|
-
if (!isName)
|
|
140
|
-
(typed ? type : value).add(current.text);
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
if (ts.isShorthandPropertyAssignment(current))
|
|
144
|
-
value.add(current.name.text);
|
|
145
|
-
ts.forEachChild(current, (child) => visit(child, typed));
|
|
146
|
-
};
|
|
147
|
-
visit(node, false);
|
|
148
|
-
return { value, type };
|
|
149
|
-
}
|
|
150
|
-
function describeStatements(ts, sf) {
|
|
151
|
-
const jsx = jsxBindings(ts, sf.text);
|
|
152
|
-
return sf.statements.map((node) => {
|
|
153
|
-
const declares = new Set();
|
|
154
|
-
const exports = new Map();
|
|
155
|
-
const listed = [];
|
|
156
|
-
const exported = hasModifier(ts, node, ts.SyntaxKind.ExportKeyword);
|
|
157
|
-
const isDefault = hasModifier(ts, node, ts.SyntaxKind.DefaultKeyword);
|
|
158
|
-
const addExport = (local, as) => {
|
|
159
|
-
exports.set(local, [...(exports.get(local) ?? []), as]);
|
|
160
|
-
};
|
|
161
|
-
if (ts.isImportDeclaration(node)) {
|
|
162
|
-
const clause = node.importClause;
|
|
163
|
-
if (clause?.name)
|
|
164
|
-
declares.add(clause.name.text);
|
|
165
|
-
const bindings = clause?.namedBindings;
|
|
166
|
-
if (bindings && ts.isNamespaceImport(bindings))
|
|
167
|
-
declares.add(bindings.name.text);
|
|
168
|
-
if (bindings && ts.isNamedImports(bindings))
|
|
169
|
-
for (const el of bindings.elements)
|
|
170
|
-
declares.add(el.name.text);
|
|
171
|
-
}
|
|
172
|
-
else if (ts.isVariableStatement(node)) {
|
|
173
|
-
for (const decl of node.declarationList.declarations) {
|
|
174
|
-
const names = new Set();
|
|
175
|
-
bindingNames(ts, decl.name, names);
|
|
176
|
-
for (const name of names) {
|
|
177
|
-
declares.add(name);
|
|
178
|
-
if (exported)
|
|
179
|
-
addExport(name, name);
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
else if (ts.isFunctionDeclaration(node) ||
|
|
184
|
-
ts.isClassDeclaration(node) ||
|
|
185
|
-
ts.isInterfaceDeclaration(node) ||
|
|
186
|
-
ts.isTypeAliasDeclaration(node) ||
|
|
187
|
-
ts.isEnumDeclaration(node)) {
|
|
188
|
-
const name = node.name?.text;
|
|
189
|
-
if (name !== undefined)
|
|
190
|
-
declares.add(name);
|
|
191
|
-
if (exported)
|
|
192
|
-
addExport(name ?? "default", isDefault ? "default" : (name ?? "default"));
|
|
193
|
-
}
|
|
194
|
-
else if (ts.isExportAssignment(node)) {
|
|
195
|
-
addExport("default", "default");
|
|
196
|
-
}
|
|
197
|
-
else if (ts.isExportDeclaration(node) &&
|
|
198
|
-
node.exportClause &&
|
|
199
|
-
ts.isNamedExports(node.exportClause)) {
|
|
200
|
-
for (const el of node.exportClause.elements) {
|
|
201
|
-
const local = (el.propertyName ?? el.name).text;
|
|
202
|
-
addExport(node.moduleSpecifier ? `\0${el.name.text}` : local, el.name.text);
|
|
203
|
-
if (node.moduleSpecifier === undefined)
|
|
204
|
-
listed.push({ exported: el.name.text, local, type: node.isTypeOnly || el.isTypeOnly });
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
const refs = ts.isImportDeclaration(node)
|
|
208
|
-
? { value: new Set(), type: new Set() }
|
|
209
|
-
: references(ts, node, jsx);
|
|
210
|
-
for (const name of declares) {
|
|
211
|
-
refs.value.delete(name);
|
|
212
|
-
refs.type.delete(name);
|
|
213
|
-
}
|
|
214
|
-
return {
|
|
215
|
-
node,
|
|
216
|
-
text: node.getFullText(sf),
|
|
217
|
-
declares,
|
|
218
|
-
exports,
|
|
219
|
-
valueRefs: refs.value,
|
|
220
|
-
typeRefs: refs.type,
|
|
221
|
-
listed,
|
|
222
|
-
};
|
|
223
|
-
});
|
|
224
|
-
}
|
|
225
|
-
function filterImport(ts, sf, node, uses, fresh) {
|
|
226
|
-
const clause = node.importClause;
|
|
227
|
-
const spec = node.moduleSpecifier.getText(sf);
|
|
228
|
-
const attributes = node.attributes ? ` ${node.attributes.getText(sf)}` : "";
|
|
229
|
-
if (clause === undefined)
|
|
230
|
-
return node.getFullText(sf);
|
|
231
|
-
const heads = [];
|
|
232
|
-
const named = [];
|
|
233
|
-
let total = 0;
|
|
234
|
-
const add = (into, name, text, bare) => {
|
|
235
|
-
total++;
|
|
236
|
-
if (uses.value.has(name) || uses.type.has(name))
|
|
237
|
-
into.push({ text, bare, typeOnly: !uses.value.has(name) });
|
|
238
|
-
};
|
|
239
|
-
if (clause.name)
|
|
240
|
-
add(heads, clause.name.text, clause.name.text, clause.name.text);
|
|
241
|
-
const bindings = clause.namedBindings;
|
|
242
|
-
if (bindings && ts.isNamespaceImport(bindings)) {
|
|
243
|
-
const text = `* as ${bindings.name.text}`;
|
|
244
|
-
add(heads, bindings.name.text, text, text);
|
|
245
|
-
}
|
|
246
|
-
if (bindings && ts.isNamedImports(bindings)) {
|
|
247
|
-
for (const el of bindings.elements) {
|
|
248
|
-
const renamed = el.propertyName ? `${el.propertyName.getText(sf)} as ` : "";
|
|
249
|
-
add(named, el.name.text, el.getText(sf), `${renamed}${el.name.text}`);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
const kept = [...heads, ...named];
|
|
253
|
-
if (kept.length === 0)
|
|
254
|
-
return undefined;
|
|
255
|
-
const leading = node.getFullText(sf).slice(0, node.getStart(sf) - node.getFullStart());
|
|
256
|
-
const statement = (typeOnly, body) => `import ${typeOnly ? "type " : ""}${body} from ${spec}${attributes}${terminatorOf(sf, node)}`;
|
|
257
|
-
const list = (parts, text) => parts.length === 0 ? [] : [`{ ${parts.map(text).join(", ")} }`];
|
|
258
|
-
if (!clause.isTypeOnly &&
|
|
259
|
-
node.attributes === undefined &&
|
|
260
|
-
(fresh || kept.length < total) &&
|
|
261
|
-
kept.every((part) => part.typeOnly)) {
|
|
262
|
-
const bodies = [...heads.map((part) => part.bare), ...list(named, (part) => part.bare)];
|
|
263
|
-
return `${leading}${bodies.map((body) => statement(true, body)).join("\n")}`;
|
|
264
|
-
}
|
|
265
|
-
const body = [...heads.map((part) => part.text), ...list(named, (part) => part.text)];
|
|
266
|
-
return `${leading}${statement(clause.isTypeOnly, body.join(", "))}`;
|
|
267
|
-
}
|
|
268
|
-
const terminatorOf = (sf, node) => node.getText(sf).endsWith(";") ? ";" : "";
|
|
269
|
-
function filterExportList(ts, sf, node, keep) {
|
|
270
|
-
const clause = node.exportClause;
|
|
271
|
-
if (!clause || !ts.isNamedExports(clause))
|
|
272
|
-
return node.getFullText(sf);
|
|
273
|
-
const kept = clause.elements.filter((el) => keep(el.name.text)).map((el) => el.getText(sf));
|
|
274
|
-
if (kept.length === 0)
|
|
275
|
-
return undefined;
|
|
276
|
-
const from = node.moduleSpecifier ? ` from ${node.moduleSpecifier.getText(sf)}` : "";
|
|
277
|
-
const leading = node.getFullText(sf).slice(0, node.getStart(sf) - node.getFullStart());
|
|
278
|
-
return `${leading}export ${node.isTypeOnly ? "type " : ""}{ ${kept.join(", ")} }${from}${terminatorOf(sf, node)}`;
|
|
279
|
-
}
|
|
280
|
-
const joinStatements = (texts) => {
|
|
281
|
-
const body = texts.join("").replace(/^\s*\n/, "");
|
|
282
|
-
return body.endsWith("\n") ? body : `${body}\n`;
|
|
283
|
-
};
|
|
284
|
-
const PURE_OBJECT_CALLS = new Set(["keys", "values", "entries", "freeze", "fromEntries"]);
|
|
285
|
-
function dataBlocker(ts, node, isLocal, reads) {
|
|
286
|
-
const data = (n) => {
|
|
287
|
-
if (ts.isStringLiteral(n) ||
|
|
288
|
-
ts.isNumericLiteral(n) ||
|
|
289
|
-
ts.isBigIntLiteral(n) ||
|
|
290
|
-
ts.isNoSubstitutionTemplateLiteral(n) ||
|
|
291
|
-
n.kind === ts.SyntaxKind.TrueKeyword ||
|
|
292
|
-
n.kind === ts.SyntaxKind.FalseKeyword ||
|
|
293
|
-
n.kind === ts.SyntaxKind.NullKeyword)
|
|
294
|
-
return undefined;
|
|
295
|
-
if (ts.isIdentifier(n)) {
|
|
296
|
-
if (isLocal(n.text)) {
|
|
297
|
-
reads.add(n.text);
|
|
298
|
-
return undefined;
|
|
299
|
-
}
|
|
300
|
-
return n.text === "undefined" ? undefined : n.text;
|
|
301
|
-
}
|
|
302
|
-
const every = (nodes) => {
|
|
303
|
-
for (const child of nodes) {
|
|
304
|
-
const blocker = data(child);
|
|
305
|
-
if (blocker !== undefined)
|
|
306
|
-
return blocker;
|
|
307
|
-
}
|
|
308
|
-
return undefined;
|
|
309
|
-
};
|
|
310
|
-
if (ts.isTemplateExpression(n))
|
|
311
|
-
return every(n.templateSpans.map((span) => span.expression));
|
|
312
|
-
if (ts.isPrefixUnaryExpression(n))
|
|
313
|
-
return n.operator === ts.SyntaxKind.PlusPlusToken ||
|
|
314
|
-
n.operator === ts.SyntaxKind.MinusMinusToken
|
|
315
|
-
? ""
|
|
316
|
-
: data(n.operand);
|
|
317
|
-
if (ts.isBinaryExpression(n)) {
|
|
318
|
-
const operator = n.operatorToken.kind;
|
|
319
|
-
const assigns = operator >= ts.SyntaxKind.FirstAssignment && operator <= ts.SyntaxKind.LastAssignment;
|
|
320
|
-
if (assigns ||
|
|
321
|
-
operator === ts.SyntaxKind.CommaToken ||
|
|
322
|
-
operator === ts.SyntaxKind.InKeyword ||
|
|
323
|
-
operator === ts.SyntaxKind.InstanceOfKeyword)
|
|
324
|
-
return "";
|
|
325
|
-
return every([n.left, n.right]);
|
|
326
|
-
}
|
|
327
|
-
if (ts.isConditionalExpression(n))
|
|
328
|
-
return every([n.condition, n.whenTrue, n.whenFalse]);
|
|
329
|
-
if (ts.isArrayLiteralExpression(n))
|
|
330
|
-
return every(n.elements.flatMap((element) => ts.isOmittedExpression(element)
|
|
331
|
-
? []
|
|
332
|
-
: [ts.isSpreadElement(element) ? element.expression : element]));
|
|
333
|
-
if (ts.isObjectLiteralExpression(n)) {
|
|
334
|
-
for (const property of n.properties) {
|
|
335
|
-
const blocker = ts.isPropertyAssignment(property)
|
|
336
|
-
? ((ts.isComputedPropertyName(property.name)
|
|
337
|
-
? data(property.name.expression)
|
|
338
|
-
: undefined) ?? data(property.initializer))
|
|
339
|
-
: ts.isShorthandPropertyAssignment(property)
|
|
340
|
-
? property.objectAssignmentInitializer === undefined
|
|
341
|
-
? data(property.name)
|
|
342
|
-
: ""
|
|
343
|
-
: ts.isSpreadAssignment(property)
|
|
344
|
-
? data(property.expression)
|
|
345
|
-
: "";
|
|
346
|
-
if (blocker !== undefined)
|
|
347
|
-
return blocker;
|
|
348
|
-
}
|
|
349
|
-
return undefined;
|
|
350
|
-
}
|
|
351
|
-
if (ts.isParenthesizedExpression(n) ||
|
|
352
|
-
ts.isAsExpression(n) ||
|
|
353
|
-
ts.isSatisfiesExpression(n) ||
|
|
354
|
-
ts.isNonNullExpression(n) ||
|
|
355
|
-
ts.isTypeAssertionExpression(n))
|
|
356
|
-
return data(n.expression);
|
|
357
|
-
if (ts.isPropertyAccessExpression(n))
|
|
358
|
-
return ts.isPrivateIdentifier(n.name) ? "" : data(n.expression);
|
|
359
|
-
if (ts.isElementAccessExpression(n))
|
|
360
|
-
return every([n.expression, n.argumentExpression]);
|
|
361
|
-
if (ts.isCallExpression(n)) {
|
|
362
|
-
const callee = n.expression;
|
|
363
|
-
const objectRead = ts.isPropertyAccessExpression(callee) &&
|
|
364
|
-
ts.isIdentifier(callee.expression) &&
|
|
365
|
-
callee.expression.text === "Object" &&
|
|
366
|
-
!isLocal("Object") &&
|
|
367
|
-
PURE_OBJECT_CALLS.has(callee.name.text) &&
|
|
368
|
-
n.questionDotToken === undefined &&
|
|
369
|
-
n.typeArguments === undefined;
|
|
370
|
-
return objectRead ? every(n.arguments) : "";
|
|
371
|
-
}
|
|
372
|
-
return "";
|
|
373
|
-
};
|
|
374
|
-
return data(node);
|
|
375
|
-
}
|
|
376
|
-
export function splitRouteSource(ts, source, fileName, backendSpecifier, options = {}) {
|
|
377
|
-
const kind = fileName.endsWith("x") ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
|
|
378
|
-
const sf = ts.createSourceFile(fileName, source, ts.ScriptTarget.Latest, true, kind);
|
|
379
|
-
const statements = describeStatements(ts, sf);
|
|
380
|
-
const issues = [];
|
|
381
|
-
const exportsOf = (info) => [...info.exports.values()].flat();
|
|
382
|
-
const isBackendStatement = (info) => exportsOf(info).some((name) => BACKEND_EXPORTS.has(name));
|
|
383
|
-
const moved = statements.flatMap(exportsOf).filter((name) => BACKEND_EXPORTS.has(name));
|
|
384
|
-
if (moved.length === 0)
|
|
385
|
-
return { frontend: source, backend: undefined, moved: [], issues: [] };
|
|
386
|
-
const declaredBy = new Map();
|
|
387
|
-
const importedBy = new Map();
|
|
388
|
-
for (const info of statements) {
|
|
389
|
-
const into = ts.isImportDeclaration(info.node) ? importedBy : declaredBy;
|
|
390
|
-
for (const name of info.declares)
|
|
391
|
-
into.set(name, info);
|
|
392
|
-
}
|
|
393
|
-
const isTypeDeclaration = (info) => ts.isInterfaceDeclaration(info.node) || ts.isTypeAliasDeclaration(info.node);
|
|
394
|
-
const readsFor = (info, backend) => {
|
|
395
|
-
if (info.listed.length === 0)
|
|
396
|
-
return { value: info.valueRefs, type: info.typeRefs };
|
|
397
|
-
const mine = info.listed.filter((entry) => BACKEND_EXPORTS.has(entry.exported) === backend);
|
|
398
|
-
return {
|
|
399
|
-
value: new Set(mine.filter((entry) => !entry.type).map((entry) => entry.local)),
|
|
400
|
-
type: new Set(mine.filter((entry) => entry.type).map((entry) => entry.local)),
|
|
401
|
-
};
|
|
402
|
-
};
|
|
403
|
-
const movedLocals = new Set();
|
|
404
|
-
for (const info of statements) {
|
|
405
|
-
for (const [local, names] of info.exports)
|
|
406
|
-
if (names.some((name) => BACKEND_EXPORTS.has(name)))
|
|
407
|
-
movedLocals.add(local);
|
|
408
|
-
}
|
|
409
|
-
const partition = (shared) => {
|
|
410
|
-
const local = (name) => {
|
|
411
|
-
const target = declaredBy.get(name);
|
|
412
|
-
return target === undefined || shared.has(target) ? undefined : target;
|
|
413
|
-
};
|
|
414
|
-
const backendSet = new Set();
|
|
415
|
-
const queue = statements.filter(isBackendStatement);
|
|
416
|
-
for (const info of queue)
|
|
417
|
-
backendSet.add(info);
|
|
418
|
-
while (queue.length > 0) {
|
|
419
|
-
const info = queue.pop();
|
|
420
|
-
const reads = readsFor(info, true);
|
|
421
|
-
for (const name of [...reads.value, ...reads.type]) {
|
|
422
|
-
const target = local(name);
|
|
423
|
-
if (target !== undefined && !backendSet.has(target)) {
|
|
424
|
-
backendSet.add(target);
|
|
425
|
-
queue.push(target);
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
const frontendKeep = new Set();
|
|
430
|
-
const seed = statements.filter((info) => !ts.isImportDeclaration(info.node) &&
|
|
431
|
-
!shared.has(info) &&
|
|
432
|
-
(!backendSet.has(info) ||
|
|
433
|
-
exportsOf(info).some((name) => !BACKEND_EXPORTS.has(name)) ||
|
|
434
|
-
(info.declares.size === 0 && info.exports.size === 0)));
|
|
435
|
-
const fqueue = [...seed];
|
|
436
|
-
for (const info of fqueue)
|
|
437
|
-
frontendKeep.add(info);
|
|
438
|
-
while (fqueue.length > 0) {
|
|
439
|
-
const info = fqueue.pop();
|
|
440
|
-
const reads = readsFor(info, false);
|
|
441
|
-
for (const name of reads.value) {
|
|
442
|
-
if (movedLocals.has(name) && !info.declares.has(name))
|
|
443
|
-
continue;
|
|
444
|
-
const target = local(name);
|
|
445
|
-
if (target !== undefined && !frontendKeep.has(target) && !isBackendStatement(target)) {
|
|
446
|
-
frontendKeep.add(target);
|
|
447
|
-
fqueue.push(target);
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
for (const name of reads.type) {
|
|
451
|
-
if (movedLocals.has(name))
|
|
452
|
-
continue;
|
|
453
|
-
const target = local(name);
|
|
454
|
-
if (target !== undefined && !frontendKeep.has(target) && !isBackendStatement(target)) {
|
|
455
|
-
frontendKeep.add(target);
|
|
456
|
-
fqueue.push(target);
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
return { backendSet, frontendKeep };
|
|
461
|
-
};
|
|
462
|
-
let { backendSet, frontendKeep } = partition(new Set());
|
|
463
|
-
const copies = () => [...frontendKeep].filter((info) => backendSet.has(info) &&
|
|
464
|
-
!isBackendStatement(info) &&
|
|
465
|
-
info.declares.size > 0 &&
|
|
466
|
-
!isTypeDeclaration(info));
|
|
467
|
-
const shared = new Set();
|
|
468
|
-
const sharedTypes = new Set();
|
|
469
|
-
const sharedImports = new Set();
|
|
470
|
-
const unshared = new Map();
|
|
471
|
-
if (options.sharedFile !== undefined) {
|
|
472
|
-
const gather = (info, closure) => {
|
|
473
|
-
if (closure.values.has(info) || shared.has(info))
|
|
474
|
-
return undefined;
|
|
475
|
-
const node = info.node;
|
|
476
|
-
const reads = new Set();
|
|
477
|
-
let blocker = "";
|
|
478
|
-
if (!isBackendStatement(info) &&
|
|
479
|
-
ts.isVariableStatement(node) &&
|
|
480
|
-
(node.declarationList.flags & ts.NodeFlags.Const) !== 0 &&
|
|
481
|
-
!hasModifier(ts, node, ts.SyntaxKind.DeclareKeyword)) {
|
|
482
|
-
blocker = undefined;
|
|
483
|
-
for (const declaration of node.declarationList.declarations) {
|
|
484
|
-
blocker =
|
|
485
|
-
ts.isIdentifier(declaration.name) && declaration.initializer !== undefined
|
|
486
|
-
? dataBlocker(ts, declaration.initializer, (name) => declaredBy.has(name), reads)
|
|
487
|
-
: "";
|
|
488
|
-
if (blocker !== undefined)
|
|
489
|
-
break;
|
|
490
|
-
}
|
|
491
|
-
}
|
|
492
|
-
if (blocker !== undefined)
|
|
493
|
-
return blocker !== "" && importedBy.has(blocker)
|
|
494
|
-
? `it reads the import "${blocker}"`
|
|
495
|
-
: "it is not constant data";
|
|
496
|
-
closure.values.add(info);
|
|
497
|
-
for (const name of reads) {
|
|
498
|
-
const target = declaredBy.get(name);
|
|
499
|
-
const why = target === undefined ? undefined : gather(target, closure);
|
|
500
|
-
if (why !== undefined)
|
|
501
|
-
return `it reads "${name}", which ${why.slice(3)}`;
|
|
502
|
-
}
|
|
503
|
-
for (const name of info.typeRefs) {
|
|
504
|
-
const why = gatherType(name, closure);
|
|
505
|
-
if (why !== undefined)
|
|
506
|
-
return why;
|
|
507
|
-
}
|
|
508
|
-
return undefined;
|
|
509
|
-
};
|
|
510
|
-
const gatherType = (name, closure) => {
|
|
511
|
-
const local = declaredBy.get(name);
|
|
512
|
-
if (local === undefined) {
|
|
513
|
-
if (importedBy.has(name))
|
|
514
|
-
closure.imports.add(name);
|
|
515
|
-
return undefined;
|
|
516
|
-
}
|
|
517
|
-
if (!isTypeDeclaration(local)) {
|
|
518
|
-
const why = gather(local, closure);
|
|
519
|
-
return why === undefined ? undefined : `its type reads "${name}", which ${why.slice(3)}`;
|
|
520
|
-
}
|
|
521
|
-
if (closure.types.has(local) || sharedTypes.has(local))
|
|
522
|
-
return undefined;
|
|
523
|
-
closure.types.add(local);
|
|
524
|
-
for (const ref of [...local.typeRefs, ...local.valueRefs]) {
|
|
525
|
-
const why = gatherType(ref, closure);
|
|
526
|
-
if (why !== undefined)
|
|
527
|
-
return why;
|
|
528
|
-
}
|
|
529
|
-
return undefined;
|
|
530
|
-
};
|
|
531
|
-
for (const info of copies()) {
|
|
532
|
-
const closure = { values: new Set(), types: new Set(), imports: new Set() };
|
|
533
|
-
const why = gather(info, closure);
|
|
534
|
-
if (why !== undefined) {
|
|
535
|
-
unshared.set(info, why);
|
|
536
|
-
continue;
|
|
537
|
-
}
|
|
538
|
-
for (const value of closure.values)
|
|
539
|
-
shared.add(value);
|
|
540
|
-
for (const type of closure.types)
|
|
541
|
-
sharedTypes.add(type);
|
|
542
|
-
for (const name of closure.imports)
|
|
543
|
-
sharedImports.add(name);
|
|
544
|
-
}
|
|
545
|
-
if (shared.size > 0)
|
|
546
|
-
({ backendSet, frontendKeep } = partition(shared));
|
|
547
|
-
}
|
|
548
|
-
const typeImports = new Set();
|
|
549
|
-
for (const info of frontendKeep) {
|
|
550
|
-
if (isBackendStatement(info))
|
|
551
|
-
continue;
|
|
552
|
-
const reads = readsFor(info, false);
|
|
553
|
-
for (const name of movedLocals) {
|
|
554
|
-
if (info.declares.has(name))
|
|
555
|
-
continue;
|
|
556
|
-
if (reads.value.has(name))
|
|
557
|
-
issues.push(`the frontend uses "${name}" at runtime, but it runs on the server only`);
|
|
558
|
-
if (reads.type.has(name) || reads.value.has(name))
|
|
559
|
-
typeImports.add(name);
|
|
560
|
-
}
|
|
561
|
-
}
|
|
562
|
-
for (const info of copies()) {
|
|
563
|
-
const why = unshared.get(info);
|
|
564
|
-
issues.push(`"${[...info.declares].join(", ")}" is needed by both halves and was copied into each${why === undefined ? "" : `, since ${why}`}; consider moving it to shared/`);
|
|
565
|
-
}
|
|
566
|
-
const uses = (infos, backend) => {
|
|
567
|
-
const value = new Set();
|
|
568
|
-
const type = new Set();
|
|
569
|
-
for (const info of infos) {
|
|
570
|
-
const reads = readsFor(info, backend);
|
|
571
|
-
for (const name of reads.value)
|
|
572
|
-
value.add(name);
|
|
573
|
-
for (const name of reads.type)
|
|
574
|
-
type.add(name);
|
|
575
|
-
}
|
|
576
|
-
return { value, type };
|
|
577
|
-
};
|
|
578
|
-
const backendUses = uses(backendSet, true);
|
|
579
|
-
const frontendUses = uses(frontendKeep, false);
|
|
580
|
-
const semi = statements.some((info) => terminatorOf(sf, info.node) !== "") ? ";" : "";
|
|
581
|
-
const firstImport = statements.find((info) => ts.isImportDeclaration(info.node))?.node;
|
|
582
|
-
const quote = firstImport !== undefined && ts.isImportDeclaration(firstImport)
|
|
583
|
-
? (firstImport.moduleSpecifier.getText(sf)[0] ?? '"')
|
|
584
|
-
: '"';
|
|
585
|
-
const sharedNames = [...shared].flatMap((info) => [...info.declares]);
|
|
586
|
-
const sharedSpecifier = (() => {
|
|
587
|
-
const file = options.sharedFile ?? "";
|
|
588
|
-
const relative = statements.flatMap((info) => ts.isImportDeclaration(info.node) &&
|
|
589
|
-
ts.isStringLiteral(info.node.moduleSpecifier) &&
|
|
590
|
-
info.node.moduleSpecifier.text.startsWith(".")
|
|
591
|
-
? [info.node.moduleSpecifier.text]
|
|
592
|
-
: []);
|
|
593
|
-
const extension = relative
|
|
594
|
-
.map((specifier) => /\.(ts|tsx|js|jsx)$/.exec(specifier)?.[1])
|
|
595
|
-
.find((found) => found !== undefined);
|
|
596
|
-
const suffix = extension === undefined ? "" : extension.startsWith("t") ? ".ts" : ".js";
|
|
597
|
-
return `./${file.replace(/\.ts$/, "")}${suffix}`;
|
|
598
|
-
})();
|
|
599
|
-
const sharedImport = (half) => {
|
|
600
|
-
const names = sharedNames
|
|
601
|
-
.filter((name) => half.value.has(name) || half.type.has(name))
|
|
602
|
-
.sort(codeUnitOrder);
|
|
603
|
-
if (names.length === 0)
|
|
604
|
-
return [];
|
|
605
|
-
const typeOnly = names.every((name) => !half.value.has(name));
|
|
606
|
-
return [
|
|
607
|
-
`\nimport ${typeOnly ? "type " : ""}{ ${names.join(", ")} } from ${quote}${sharedSpecifier}${quote}${semi}`,
|
|
608
|
-
];
|
|
609
|
-
};
|
|
610
|
-
const reexported = [...shared].flatMap((info) => [...info.exports].flatMap(([local, names]) => names.map((name) => (name === local ? local : `${local} as ${name}`))));
|
|
611
|
-
const backendTexts = [];
|
|
612
|
-
const frontendTexts = [];
|
|
613
|
-
let afterImports = 0;
|
|
614
|
-
let backendAfterImports = 0;
|
|
615
|
-
for (const info of statements) {
|
|
616
|
-
const node = info.node;
|
|
617
|
-
if (ts.isImportDeclaration(node)) {
|
|
618
|
-
const back = filterImport(ts, sf, node, backendUses, true);
|
|
619
|
-
if (back !== undefined)
|
|
620
|
-
backendTexts.push(node.importClause ? back : "");
|
|
621
|
-
const front = filterImport(ts, sf, node, frontendUses, false);
|
|
622
|
-
if (front !== undefined)
|
|
623
|
-
frontendTexts.push(front);
|
|
624
|
-
afterImports = frontendTexts.length;
|
|
625
|
-
backendAfterImports = backendTexts.length;
|
|
626
|
-
continue;
|
|
627
|
-
}
|
|
628
|
-
if (isBackendStatement(info)) {
|
|
629
|
-
if (ts.isExportDeclaration(node) &&
|
|
630
|
-
exportsOf(info).some((name) => !BACKEND_EXPORTS.has(name))) {
|
|
631
|
-
backendTexts.push(filterExportList(ts, sf, node, (name) => BACKEND_EXPORTS.has(name)) ?? "");
|
|
632
|
-
frontendTexts.push(filterExportList(ts, sf, node, (name) => !BACKEND_EXPORTS.has(name)) ?? "");
|
|
633
|
-
continue;
|
|
634
|
-
}
|
|
635
|
-
if (exportsOf(info).some((name) => !BACKEND_EXPORTS.has(name))) {
|
|
636
|
-
issues.push(`one statement exports both "${exportsOf(info).join('", "')}"; split it by hand`);
|
|
637
|
-
}
|
|
638
|
-
backendTexts.push(info.text);
|
|
639
|
-
continue;
|
|
640
|
-
}
|
|
641
|
-
if (backendSet.has(info))
|
|
642
|
-
backendTexts.push(stripExport(ts, sf, info));
|
|
643
|
-
if (frontendKeep.has(info))
|
|
644
|
-
frontendTexts.push(info.text);
|
|
645
|
-
}
|
|
646
|
-
const frontendImports = sharedImport(frontendUses);
|
|
647
|
-
if (reexported.length > 0)
|
|
648
|
-
frontendImports.push(`\nexport { ${reexported.join(", ")} } from ${quote}${sharedSpecifier}${quote}${semi}`);
|
|
649
|
-
if (typeImports.size > 0)
|
|
650
|
-
frontendImports.push(`\nimport type { ${[...typeImports].sort().join(", ")} } from ${quote}${backendSpecifier}${quote}${semi}`);
|
|
651
|
-
frontendTexts.splice(afterImports, 0, ...frontendImports);
|
|
652
|
-
backendTexts.splice(backendAfterImports, 0, ...sharedImport(backendUses));
|
|
653
|
-
const pragmas = (ts.getLeadingCommentRanges(source, 0) ?? [])
|
|
654
|
-
.map((range) => source.slice(range.pos, range.end))
|
|
655
|
-
.filter((comment) => /@jsx/.test(comment));
|
|
656
|
-
const lift = (text) => pragmas.reduce((out, pragma) => out.replace(new RegExp(`${escapeRegExp(pragma)}[ \\t]*\\n?`), ""), text);
|
|
657
|
-
const frontend = lift(joinStatements(frontendTexts));
|
|
658
|
-
let sharedText;
|
|
659
|
-
if (shared.size > 0) {
|
|
660
|
-
const texts = [];
|
|
661
|
-
const typeUses = { value: new Set(), type: sharedImports };
|
|
662
|
-
for (const info of statements) {
|
|
663
|
-
if (ts.isImportDeclaration(info.node)) {
|
|
664
|
-
const kept = filterImport(ts, sf, info.node, typeUses, true);
|
|
665
|
-
if (kept !== undefined && info.node.importClause !== undefined)
|
|
666
|
-
texts.push(kept);
|
|
667
|
-
}
|
|
668
|
-
else if (shared.has(info) || sharedTypes.has(info)) {
|
|
669
|
-
texts.push(withExport(ts, sf, info));
|
|
670
|
-
}
|
|
671
|
-
}
|
|
672
|
-
sharedText = lift(joinStatements(texts));
|
|
673
|
-
}
|
|
674
|
-
const result = {
|
|
675
|
-
frontend: pragmas.length > 0 ? `${pragmas.join("\n")}\n${frontend}` : frontend,
|
|
676
|
-
backend: lift(joinStatements(backendTexts)),
|
|
677
|
-
moved: [...new Set(moved)].sort(),
|
|
678
|
-
issues,
|
|
679
|
-
};
|
|
680
|
-
return sharedText === undefined ? result : { ...result, shared: sharedText };
|
|
681
|
-
}
|
|
682
|
-
function withExport(ts, sf, info) {
|
|
683
|
-
if (hasModifier(ts, info.node, ts.SyntaxKind.ExportKeyword))
|
|
684
|
-
return info.text;
|
|
685
|
-
const at = info.node.getStart(sf) - info.node.getFullStart();
|
|
686
|
-
return `${info.text.slice(0, at)}export ${info.text.slice(at)}`;
|
|
687
|
-
}
|
|
688
|
-
function stripExport(ts, sf, info) {
|
|
689
|
-
if (info.exports.size === 0)
|
|
690
|
-
return info.text;
|
|
691
|
-
const node = info.node;
|
|
692
|
-
const exportKeyword = (ts.canHaveModifiers(node) ? (ts.getModifiers(node) ?? []) : []).find((m) => m.kind === ts.SyntaxKind.ExportKeyword);
|
|
693
|
-
if (exportKeyword === undefined)
|
|
694
|
-
return info.text;
|
|
695
|
-
const start = exportKeyword.getStart(sf) - node.getFullStart();
|
|
696
|
-
const end = exportKeyword.getEnd() - node.getFullStart();
|
|
697
|
-
return info.text.slice(0, start) + info.text.slice(end).replace(/^\s+/, "");
|
|
698
|
-
}
|
|
699
|
-
export function middlewareSource(ts, source, fileName) {
|
|
700
|
-
const sf = ts.createSourceFile(fileName, source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
|
|
701
|
-
let out = source;
|
|
702
|
-
for (const node of [...sf.statements].reverse()) {
|
|
703
|
-
if (ts.isExportAssignment(node) && !node.isExportEquals) {
|
|
704
|
-
out = `${out.slice(0, node.getStart(sf))}export const middleware = ${node.expression.getText(sf)}${terminatorOf(sf, node)}${out.slice(node.getEnd())}`;
|
|
705
|
-
}
|
|
706
|
-
else if (ts.isFunctionDeclaration(node) &&
|
|
707
|
-
hasModifier(ts, node, ts.SyntaxKind.DefaultKeyword)) {
|
|
708
|
-
const modifiers = (ts.getModifiers(node) ?? []).filter((m) => m.kind === ts.SyntaxKind.ExportKeyword || m.kind === ts.SyntaxKind.DefaultKeyword);
|
|
709
|
-
const first = modifiers[0];
|
|
710
|
-
const last = modifiers[modifiers.length - 1];
|
|
711
|
-
if (first === undefined || last === undefined)
|
|
712
|
-
continue;
|
|
713
|
-
if (node.name === undefined) {
|
|
714
|
-
const text = out
|
|
715
|
-
.slice(last.getEnd(), node.getEnd())
|
|
716
|
-
.replace(/^(\s*(?:async\s+)?function\s*\*?)\s*/, "$1 middleware");
|
|
717
|
-
out = `${out.slice(0, first.getStart(sf))}export${text}${out.slice(node.getEnd())}`;
|
|
718
|
-
}
|
|
719
|
-
else {
|
|
720
|
-
out = `${out.slice(0, first.getStart(sf))}${out.slice(last.getEnd()).replace(/^\s+/, "")}`;
|
|
721
|
-
const semi = sf.statements.some((s) => terminatorOf(sf, s) !== "") ? ";" : "";
|
|
722
|
-
out = `${out.trimEnd()}\nexport { ${node.name.text} as middleware }${semi}\n`;
|
|
723
|
-
}
|
|
724
|
-
}
|
|
725
|
-
}
|
|
726
|
-
return out;
|
|
727
|
-
}
|
|
728
|
-
function mergeModules(ts, first, second) {
|
|
729
|
-
if (first.trim() === "")
|
|
730
|
-
return second;
|
|
731
|
-
const split = (text) => {
|
|
732
|
-
const sf = ts.createSourceFile("merge.ts", text, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
|
|
733
|
-
const imports = [];
|
|
734
|
-
const body = [];
|
|
735
|
-
for (const node of sf.statements) {
|
|
736
|
-
;
|
|
737
|
-
(ts.isImportDeclaration(node) ? imports : body).push(node.getFullText(sf).replace(/^\s*\n/, ""));
|
|
738
|
-
}
|
|
739
|
-
return { imports, body };
|
|
740
|
-
};
|
|
741
|
-
const a = split(first);
|
|
742
|
-
const b = split(second);
|
|
743
|
-
const imports = [...new Set([...a.imports, ...b.imports].map((text) => text.trim()))];
|
|
744
|
-
const body = [...a.body, ...b.body].map((text) => text.trim()).join("\n\n");
|
|
745
|
-
return `${imports.join("\n")}${imports.length > 0 ? "\n\n" : ""}${body}\n`;
|
|
746
|
-
}
|
|
747
|
-
function scriptBlocks(source) {
|
|
748
|
-
const blocks = [];
|
|
749
|
-
const pattern = /<script\b([^>]*)>([\s\S]*?)<\/script(?=[\s/>])[^>]*>/gi;
|
|
750
|
-
const visible = source.replace(/<!--[\s\S]*?-->/g, (comment) => comment.replace(/[^\n]/g, " "));
|
|
751
|
-
for (const match of visible.matchAll(pattern)) {
|
|
752
|
-
const attrs = match[1] ?? "";
|
|
753
|
-
const start = (match.index ?? 0) + match[0].indexOf(">") + 1;
|
|
754
|
-
blocks.push({
|
|
755
|
-
start,
|
|
756
|
-
end: start + (match[2] ?? "").length,
|
|
757
|
-
module: /\bmodule\b|context=["']module["']/.test(attrs),
|
|
758
|
-
setup: /\bsetup\b/.test(attrs),
|
|
759
|
-
});
|
|
760
|
-
}
|
|
761
|
-
return blocks;
|
|
762
|
-
}
|
|
763
|
-
function applyEdits(text, edits) {
|
|
764
|
-
let out = text;
|
|
765
|
-
for (const edit of edits.sort((a, b) => b.start - a.start))
|
|
766
|
-
out = out.slice(0, edit.start) + edit.text + out.slice(edit.end);
|
|
767
|
-
return out;
|
|
768
|
-
}
|
|
88
|
+
export { applyEdits, mergeModules, middlewareSource, scriptBlocks, splitRouteSource, } from "./migrate-layout-split.js";
|
|
89
|
+
import { appZones, declareApiKind, declareZones, followTsconfigs, ignoreNifraState, pruneVacatedDirs, rewritePackagePaths, staleContentGlobs, zoneDeclarations, } from "./migrate-layout-config.js";
|
|
90
|
+
import { applyEdits, hasModifier, mergeModules, middlewareSource, scriptBlocks, splitRouteSource, terminatorOf, } from "./migrate-layout-split.js";
|
|
769
91
|
function mockedSpecifier(ts, node) {
|
|
770
92
|
const callee = node.expression;
|
|
771
93
|
if (!ts.isPropertyAccessExpression(callee) || !ts.isIdentifier(callee.expression))
|
|
@@ -1607,7 +929,7 @@ const packageName = (specifier) => {
|
|
|
1607
929
|
return specifier.startsWith("@") ? parts.slice(0, 2).join("/") : (parts[0] ?? "");
|
|
1608
930
|
};
|
|
1609
931
|
const isRootScript = (file, rootMoves) => !file.includes("/") && SOURCE_FILE.test(file) && rootMoves[file] === undefined;
|
|
1610
|
-
const isTestFile = (file) => /(?:^|\/)(?:test|tests|__tests__)\//.test(file) || /\.(?:test|spec)\.[cm]?[jt]sx?$/.test(file);
|
|
932
|
+
export const isTestFile = (file) => /(?:^|\/)(?:test|tests|__tests__)\//.test(file) || /\.(?:test|spec)\.[cm]?[jt]sx?$/.test(file);
|
|
1611
933
|
const webConfigFile = (root, rootMoves) => rootMoves["framework.ts"] !== undefined
|
|
1612
934
|
? "framework.ts"
|
|
1613
935
|
: existsSync(join(root, FRAMEWORK_FILE))
|
|
@@ -2541,501 +1863,4 @@ export async function splitRouteFile(appRoot, file, options = {}) {
|
|
|
2541
1863
|
writeFileSync(join(root, sharedFile), result.shared);
|
|
2542
1864
|
return [file, backendFile, sharedFile];
|
|
2543
1865
|
}
|
|
2544
|
-
function appZones(root) {
|
|
2545
|
-
const manifest = join(root, "package.json");
|
|
2546
|
-
if (!existsSync(manifest))
|
|
2547
|
-
return {};
|
|
2548
|
-
let parsed;
|
|
2549
|
-
try {
|
|
2550
|
-
parsed = JSON.parse(readFileSync(manifest, "utf8"));
|
|
2551
|
-
}
|
|
2552
|
-
catch {
|
|
2553
|
-
return {};
|
|
2554
|
-
}
|
|
2555
|
-
const nifra = typeof parsed === "object" && parsed !== null ? Reflect.get(parsed, "nifra") : undefined;
|
|
2556
|
-
const zones = typeof nifra === "object" && nifra !== null ? Reflect.get(nifra, "zones") : undefined;
|
|
2557
|
-
return zones === undefined ? {} : parseZoneConfig(zones, manifest);
|
|
2558
|
-
}
|
|
2559
|
-
const matchers = new Map();
|
|
2560
|
-
function matches(pattern, file) {
|
|
2561
|
-
let match = matchers.get(pattern);
|
|
2562
|
-
if (match === undefined) {
|
|
2563
|
-
let spec = pattern
|
|
2564
|
-
.replaceAll("\\", "/")
|
|
2565
|
-
.replace(/^\.\//, "")
|
|
2566
|
-
.replace(/(?<!\/)\/+$/, "");
|
|
2567
|
-
if (spec === "" || spec === ".")
|
|
2568
|
-
match = () => true;
|
|
2569
|
-
else if (!/[*?]/.test(spec)) {
|
|
2570
|
-
const folder = `${spec}/`;
|
|
2571
|
-
match = (path) => path === spec || path.startsWith(folder);
|
|
2572
|
-
}
|
|
2573
|
-
else {
|
|
2574
|
-
if (spec.endsWith("**"))
|
|
2575
|
-
spec = `${spec}/*`;
|
|
2576
|
-
else if (!/[*?.][^/]*$/.test(spec.slice(spec.lastIndexOf("/") + 1)))
|
|
2577
|
-
spec = `${spec}/**/*`;
|
|
2578
|
-
const glob = new Bun.Glob(spec);
|
|
2579
|
-
match = (path) => glob.match(path);
|
|
2580
|
-
}
|
|
2581
|
-
matchers.set(pattern, match);
|
|
2582
|
-
}
|
|
2583
|
-
return match(file);
|
|
2584
|
-
}
|
|
2585
|
-
const TSCONFIG = /^tsconfig(?:\.[\w-]+)?\.json$/;
|
|
2586
|
-
const TS_FILE = /\.(?:[cm]?ts|tsx)$/;
|
|
2587
|
-
const inProgram = (lists, file) => lists.files.includes(file) ||
|
|
2588
|
-
(lists.include.some((pattern) => matches(pattern, file)) &&
|
|
2589
|
-
!lists.exclude.some((pattern) => matches(pattern, file)));
|
|
2590
|
-
function twinEntry(pattern, to) {
|
|
2591
|
-
const slash = to.indexOf("/");
|
|
2592
|
-
if (pattern === undefined || slash === -1)
|
|
2593
|
-
return to;
|
|
2594
|
-
const bare = pattern.replace(/^\.\//, "");
|
|
2595
|
-
const twin = `${to.slice(0, slash)}/${bare}`;
|
|
2596
|
-
if (!matches(twin, to))
|
|
2597
|
-
return to;
|
|
2598
|
-
return bare === pattern ? twin : `./${twin}`;
|
|
2599
|
-
}
|
|
2600
|
-
function followTsconfigs(ts, root, moves, layout) {
|
|
2601
|
-
const files = new Map();
|
|
2602
|
-
const issues = [];
|
|
2603
|
-
const typescript = layout.filter(({ to }) => TS_FILE.test(to));
|
|
2604
|
-
const names = readdirSync(root)
|
|
2605
|
-
.filter((name) => TSCONFIG.test(name) && isFile(join(root, name)))
|
|
2606
|
-
.sort(codeUnitOrder);
|
|
2607
|
-
const settled = new Map();
|
|
2608
|
-
const pending = [...names];
|
|
2609
|
-
for (let progress = true; progress && pending.length > 0;) {
|
|
2610
|
-
progress = false;
|
|
2611
|
-
for (const name of [...pending]) {
|
|
2612
|
-
const text = readFileSync(join(root, name), "utf8");
|
|
2613
|
-
const source = ts.parseJsonText(name, text);
|
|
2614
|
-
const top = source.statements[0]?.expression;
|
|
2615
|
-
if (top === undefined || !ts.isObjectLiteralExpression(top)) {
|
|
2616
|
-
pending.splice(pending.indexOf(name), 1);
|
|
2617
|
-
progress = true;
|
|
2618
|
-
if (moves.size > 0)
|
|
2619
|
-
issues.push({
|
|
2620
|
-
file: name,
|
|
2621
|
-
reason: "is not a JSON object; keep the moved files in its include and exclude by hand",
|
|
2622
|
-
});
|
|
2623
|
-
continue;
|
|
2624
|
-
}
|
|
2625
|
-
const base = jsonProperty(ts, top, "extends")?.initializer;
|
|
2626
|
-
let parent;
|
|
2627
|
-
if (base !== undefined && ts.isStringLiteral(base) && base.text.startsWith(".")) {
|
|
2628
|
-
const path = posix.normalize(base.text);
|
|
2629
|
-
const file = path.endsWith(".json") ? path : `${path}.json`;
|
|
2630
|
-
if (names.includes(file) && file !== name)
|
|
2631
|
-
parent = file;
|
|
2632
|
-
}
|
|
2633
|
-
if (parent !== undefined && pending.includes(parent))
|
|
2634
|
-
continue;
|
|
2635
|
-
pending.splice(pending.indexOf(name), 1);
|
|
2636
|
-
progress = true;
|
|
2637
|
-
const inherited = parent === undefined ? undefined : settled.get(parent);
|
|
2638
|
-
const extendsOther = base !== undefined && inherited === undefined;
|
|
2639
|
-
const edits = [];
|
|
2640
|
-
const own = new Map();
|
|
2641
|
-
let malformed;
|
|
2642
|
-
for (const key of ["include", "exclude", "files"]) {
|
|
2643
|
-
const array = jsonProperty(ts, top, key)?.initializer;
|
|
2644
|
-
if (array === undefined)
|
|
2645
|
-
continue;
|
|
2646
|
-
if (!ts.isArrayLiteralExpression(array)) {
|
|
2647
|
-
malformed = key;
|
|
2648
|
-
continue;
|
|
2649
|
-
}
|
|
2650
|
-
const lists = { before: [], after: [] };
|
|
2651
|
-
for (const element of array.elements) {
|
|
2652
|
-
if (!ts.isStringLiteral(element))
|
|
2653
|
-
continue;
|
|
2654
|
-
const to = moves.get(posix.normalize(element.text));
|
|
2655
|
-
lists.before.push(element.text);
|
|
2656
|
-
if (to === undefined) {
|
|
2657
|
-
lists.after.push(element.text);
|
|
2658
|
-
continue;
|
|
2659
|
-
}
|
|
2660
|
-
const spelled = element.text.startsWith("./") ? `./${to}` : to;
|
|
2661
|
-
lists.after.push(spelled);
|
|
2662
|
-
edits.push({
|
|
2663
|
-
start: element.getStart(source) + 1,
|
|
2664
|
-
end: element.getEnd() - 1,
|
|
2665
|
-
text: spelled,
|
|
2666
|
-
});
|
|
2667
|
-
}
|
|
2668
|
-
own.set(key, lists);
|
|
2669
|
-
}
|
|
2670
|
-
let updated = applyEdits(text, edits);
|
|
2671
|
-
const changes = [];
|
|
2672
|
-
if (edits.length > 0)
|
|
2673
|
-
changes.push(`${edits.length} file path(s) follow their moved files`);
|
|
2674
|
-
const list = (key, when) => {
|
|
2675
|
-
const mine = own.get(key);
|
|
2676
|
-
if (mine !== undefined)
|
|
2677
|
-
return mine[when];
|
|
2678
|
-
if (inherited !== undefined)
|
|
2679
|
-
return [...inherited[when][key]];
|
|
2680
|
-
if (extendsOther)
|
|
2681
|
-
return undefined;
|
|
2682
|
-
return key === "exclude" ? [] : own.has("files") ? [] : ["**/*"];
|
|
2683
|
-
};
|
|
2684
|
-
const filesOf = (when) => (own.get("files")?.[when] ?? []).map((entry) => posix.normalize(entry));
|
|
2685
|
-
const beforeInclude = list("include", "before");
|
|
2686
|
-
const beforeExclude = list("exclude", "before");
|
|
2687
|
-
const afterInclude = list("include", "after");
|
|
2688
|
-
const afterExclude = list("exclude", "after");
|
|
2689
|
-
if (malformed !== undefined ||
|
|
2690
|
-
beforeInclude === undefined ||
|
|
2691
|
-
beforeExclude === undefined ||
|
|
2692
|
-
afterInclude === undefined ||
|
|
2693
|
-
afterExclude === undefined) {
|
|
2694
|
-
if (malformed !== undefined && moves.size > 0)
|
|
2695
|
-
issues.push({
|
|
2696
|
-
file: name,
|
|
2697
|
-
reason: `"${malformed}" is not an array; keep the moved files in its program by hand`,
|
|
2698
|
-
});
|
|
2699
|
-
if (edits.length > 0)
|
|
2700
|
-
files.set(name, { text: updated, changes });
|
|
2701
|
-
continue;
|
|
2702
|
-
}
|
|
2703
|
-
const before = {
|
|
2704
|
-
include: beforeInclude,
|
|
2705
|
-
exclude: beforeExclude,
|
|
2706
|
-
files: filesOf("before"),
|
|
2707
|
-
};
|
|
2708
|
-
const after = { include: afterInclude, exclude: afterExclude, files: filesOf("after") };
|
|
2709
|
-
const added = { include: [], exclude: [] };
|
|
2710
|
-
let stranded;
|
|
2711
|
-
let strandedCount = 0;
|
|
2712
|
-
const quiet = new Map();
|
|
2713
|
-
const takesInOnlyChecked = (folder) => {
|
|
2714
|
-
let safe = quiet.get(folder);
|
|
2715
|
-
if (safe === undefined) {
|
|
2716
|
-
const wider = { ...after, include: [...after.include, folder] };
|
|
2717
|
-
safe = typescript.every((file) => !matches(folder, file.to) ||
|
|
2718
|
-
inProgram(before, file.from) ||
|
|
2719
|
-
!inProgram(wider, file.to));
|
|
2720
|
-
quiet.set(folder, safe);
|
|
2721
|
-
}
|
|
2722
|
-
return safe;
|
|
2723
|
-
};
|
|
2724
|
-
for (const { from, to } of typescript) {
|
|
2725
|
-
if (!inProgram(before, from) || inProgram(after, to))
|
|
2726
|
-
continue;
|
|
2727
|
-
if (after.include.some((pattern) => matches(pattern, to))) {
|
|
2728
|
-
stranded ??= { from, to, key: "exclude" };
|
|
2729
|
-
strandedCount++;
|
|
2730
|
-
continue;
|
|
2731
|
-
}
|
|
2732
|
-
const zone = to.includes("/") ? to.slice(0, to.indexOf("/")) : undefined;
|
|
2733
|
-
const entry = zone !== undefined && takesInOnlyChecked(zone)
|
|
2734
|
-
? zone
|
|
2735
|
-
: twinEntry(before.include.find((pattern) => matches(pattern, from)), to);
|
|
2736
|
-
added.include.push(entry);
|
|
2737
|
-
after.include.push(entry);
|
|
2738
|
-
}
|
|
2739
|
-
for (const { from, to } of typescript) {
|
|
2740
|
-
if (inProgram(before, from) || !inProgram(after, to))
|
|
2741
|
-
continue;
|
|
2742
|
-
let entry = twinEntry(before.exclude.find((pattern) => matches(pattern, from)), to);
|
|
2743
|
-
if (entry !== to &&
|
|
2744
|
-
typescript.some((other) => matches(entry, other.to) && inProgram(before, other.from)))
|
|
2745
|
-
entry = to;
|
|
2746
|
-
added.exclude.push(entry);
|
|
2747
|
-
after.exclude.push(entry);
|
|
2748
|
-
}
|
|
2749
|
-
for (const key of ["include", "exclude"]) {
|
|
2750
|
-
const entries = [...new Set(added[key])].sort(codeUnitOrder);
|
|
2751
|
-
if (entries.length === 0)
|
|
2752
|
-
continue;
|
|
2753
|
-
const reparsed = ts.parseJsonText(name, updated);
|
|
2754
|
-
const object = reparsed.statements[0]?.expression;
|
|
2755
|
-
const array = object !== undefined && ts.isObjectLiteralExpression(object)
|
|
2756
|
-
? jsonProperty(ts, object, key)?.initializer
|
|
2757
|
-
: undefined;
|
|
2758
|
-
if (array === undefined || !ts.isArrayLiteralExpression(array)) {
|
|
2759
|
-
stranded ??= { from: "", to: entries[0] ?? "", key };
|
|
2760
|
-
strandedCount += entries.length;
|
|
2761
|
-
continue;
|
|
2762
|
-
}
|
|
2763
|
-
updated = appendToArray(reparsed, updated, array, entries);
|
|
2764
|
-
changes.push(`${key} += ${entries.join(", ")}`);
|
|
2765
|
-
}
|
|
2766
|
-
if (stranded !== undefined)
|
|
2767
|
-
issues.push({
|
|
2768
|
-
file: name,
|
|
2769
|
-
reason: `cannot keep ${strandedCount} moved file(s) in or out of its program as before (first: "${stranded.to}"); its "${stranded.key}" is inherited or would hide them, so set it by hand`,
|
|
2770
|
-
});
|
|
2771
|
-
settled.set(name, { before, after });
|
|
2772
|
-
if (changes.length > 0)
|
|
2773
|
-
files.set(name, { text: updated, changes });
|
|
2774
|
-
}
|
|
2775
|
-
}
|
|
2776
|
-
return { files, issues };
|
|
2777
|
-
}
|
|
2778
|
-
function appendToArray(source, text, array, entries) {
|
|
2779
|
-
const quoted = entries.map((entry) => JSON.stringify(entry));
|
|
2780
|
-
const last = array.elements[array.elements.length - 1];
|
|
2781
|
-
if (last === undefined) {
|
|
2782
|
-
const at = array.getStart(source) + 1;
|
|
2783
|
-
return `${text.slice(0, at)}${quoted.join(", ")}${text.slice(at)}`;
|
|
2784
|
-
}
|
|
2785
|
-
let at = last.end;
|
|
2786
|
-
const trailing = /^\s*,/.exec(text.slice(at));
|
|
2787
|
-
if (trailing !== null)
|
|
2788
|
-
at += trailing[0].length;
|
|
2789
|
-
if (text.slice(array.getStart(source), last.getStart(source)).includes("\n")) {
|
|
2790
|
-
const lineStart = text.lastIndexOf("\n", last.getStart(source)) + 1;
|
|
2791
|
-
const indent = text.slice(lineStart, last.getStart(source)).match(/^\s*/)?.[0] ?? "";
|
|
2792
|
-
const newline = text.indexOf("\n", at);
|
|
2793
|
-
const eol = newline === -1 ? text.length : newline;
|
|
2794
|
-
if (/^\s*(?:\/\/.*|\/\*.*\*\/\s*)?$/.test(text.slice(at, eol))) {
|
|
2795
|
-
const head = trailing !== null
|
|
2796
|
-
? text.slice(0, eol)
|
|
2797
|
-
: `${text.slice(0, last.end)},${text.slice(last.end, eol)}`;
|
|
2798
|
-
const lines = quoted.join(`,\n${indent}`);
|
|
2799
|
-
return `${head}\n${indent}${lines}${trailing !== null ? "," : ""}${text.slice(eol)}`;
|
|
2800
|
-
}
|
|
2801
|
-
}
|
|
2802
|
-
const insert = trailing !== null ? ` ${quoted.join(", ")},` : `, ${quoted.join(", ")}`;
|
|
2803
|
-
return `${text.slice(0, at)}${insert}${text.slice(at)}`;
|
|
2804
|
-
}
|
|
2805
|
-
const jsonProperty = (ts, object, key) => object.properties.find((property) => ts.isPropertyAssignment(property) &&
|
|
2806
|
-
ts.isStringLiteral(property.name) &&
|
|
2807
|
-
property.name.text === key);
|
|
2808
|
-
function insertProperty(source, text, object, entry) {
|
|
2809
|
-
const open = object.getStart(source);
|
|
2810
|
-
const last = object.properties[object.properties.length - 1];
|
|
2811
|
-
if (last === undefined)
|
|
2812
|
-
return `${text.slice(0, open)}{ ${entry} }${text.slice(object.end)}`;
|
|
2813
|
-
const start = last.getStart(source);
|
|
2814
|
-
if (!text.slice(open, start).includes("\n")) {
|
|
2815
|
-
return `${text.slice(0, last.end)}, ${entry}${text.slice(last.end)}`;
|
|
2816
|
-
}
|
|
2817
|
-
const indent = /^[ \t]*/.exec(text.slice(text.lastIndexOf("\n", start) + 1, start))?.[0] ?? "";
|
|
2818
|
-
return `${text.slice(0, last.end)},\n${indent}${entry}${text.slice(last.end)}`;
|
|
2819
|
-
}
|
|
2820
|
-
function zoneDeclarations(inPlace, files, text, configured) {
|
|
2821
|
-
const sides = new Map();
|
|
2822
|
-
const packages = new Set();
|
|
2823
|
-
for (const file of files) {
|
|
2824
|
-
const top = file.split("/")[0] ?? "";
|
|
2825
|
-
if (!file.includes("/") || ZONE_DIRS.has(top))
|
|
2826
|
-
continue;
|
|
2827
|
-
if (posix.basename(file) === "package.json") {
|
|
2828
|
-
for (let dir = posix.dirname(file); dir !== "."; dir = posix.dirname(dir))
|
|
2829
|
-
packages.add(dir);
|
|
2830
|
-
continue;
|
|
2831
|
-
}
|
|
2832
|
-
if (isTestFile(file) || file.endsWith(".d.ts") || configured(file))
|
|
2833
|
-
continue;
|
|
2834
|
-
const suffix = /\.(frontend|backend|shared|fn)\.[cm]?[jt]sx?$/.exec(file)?.[1];
|
|
2835
|
-
const side = inPlace.get(file) ??
|
|
2836
|
-
(suffix === "fn" ? "backend" : suffix) ??
|
|
2837
|
-
(SOURCE_FILE.test(file) && /from\s+["'](?:node:|bun:)/.test(text(file) ?? "")
|
|
2838
|
-
? "backend"
|
|
2839
|
-
: undefined);
|
|
2840
|
-
if (side === undefined)
|
|
2841
|
-
continue;
|
|
2842
|
-
for (let dir = posix.dirname(file); dir !== "."; dir = posix.dirname(dir))
|
|
2843
|
-
sides.set(dir, (sides.get(dir) ?? new Set()).add(side));
|
|
2844
|
-
}
|
|
2845
|
-
const declared = new Map();
|
|
2846
|
-
for (const [file, side] of [...inPlace].sort(([a], [b]) => codeUnitOrder(a, b))) {
|
|
2847
|
-
const parts = file.split("/");
|
|
2848
|
-
let path = file;
|
|
2849
|
-
for (let depth = 1; depth < parts.length; depth++) {
|
|
2850
|
-
const dir = parts.slice(0, depth).join("/");
|
|
2851
|
-
const found = sides.get(dir);
|
|
2852
|
-
if (!packages.has(dir) && found?.size === 1 && found.has(side)) {
|
|
2853
|
-
path = dir;
|
|
2854
|
-
break;
|
|
2855
|
-
}
|
|
2856
|
-
}
|
|
2857
|
-
declared.set(path, side);
|
|
2858
|
-
}
|
|
2859
|
-
return declared;
|
|
2860
|
-
}
|
|
2861
|
-
function declareZones(ts, root, paths) {
|
|
2862
|
-
const listed = (side) => [...paths].flatMap(([path, of]) => (of === side ? [path] : []));
|
|
2863
|
-
const setting = `{ ${FOLDER_SIDES.filter((side) => listed(side).length > 0)
|
|
2864
|
-
.map((side) => `"${side}": ${JSON.stringify(listed(side))}`)
|
|
2865
|
-
.join(", ")} }`;
|
|
2866
|
-
const path = join(root, "package.json");
|
|
2867
|
-
if (!existsSync(path))
|
|
2868
|
-
return { issue: `there is none in the app; declare "nifra": { "zones": ${setting} } in it` };
|
|
2869
|
-
let text = readFileSync(path, "utf8");
|
|
2870
|
-
const find = (keys) => {
|
|
2871
|
-
const source = ts.parseJsonText(path, text);
|
|
2872
|
-
let node = source.statements[0]?.expression;
|
|
2873
|
-
for (const key of keys)
|
|
2874
|
-
node =
|
|
2875
|
-
node !== undefined && ts.isObjectLiteralExpression(node)
|
|
2876
|
-
? jsonProperty(ts, node, key)?.initializer
|
|
2877
|
-
: undefined;
|
|
2878
|
-
return { source, node };
|
|
2879
|
-
};
|
|
2880
|
-
const top = find([]);
|
|
2881
|
-
if (top.node === undefined || !ts.isObjectLiteralExpression(top.node))
|
|
2882
|
-
return { issue: `is not a JSON object; add "nifra": { "zones": ${setting} } to it by hand` };
|
|
2883
|
-
const nifra = find(["nifra"]);
|
|
2884
|
-
if (nifra.node === undefined)
|
|
2885
|
-
return { text: insertProperty(top.source, text, top.node, `"nifra": { "zones": ${setting} }`) };
|
|
2886
|
-
if (!ts.isObjectLiteralExpression(nifra.node))
|
|
2887
|
-
return { issue: `"nifra" is not an object; add "zones": ${setting} to it by hand` };
|
|
2888
|
-
const zones = find(["nifra", "zones"]);
|
|
2889
|
-
if (zones.node === undefined)
|
|
2890
|
-
return { text: insertProperty(nifra.source, text, nifra.node, `"zones": ${setting}`) };
|
|
2891
|
-
for (const side of FOLDER_SIDES) {
|
|
2892
|
-
const add = listed(side);
|
|
2893
|
-
if (add.length === 0)
|
|
2894
|
-
continue;
|
|
2895
|
-
const { source, node } = find(["nifra", "zones"]);
|
|
2896
|
-
if (node === undefined || !ts.isObjectLiteralExpression(node))
|
|
2897
|
-
return { issue: `"nifra.zones" is not an object; add ${setting} to it by hand` };
|
|
2898
|
-
const array = jsonProperty(ts, node, side)?.initializer;
|
|
2899
|
-
if (array === undefined)
|
|
2900
|
-
text = insertProperty(source, text, node, `"${side}": ${JSON.stringify(add)}`);
|
|
2901
|
-
else if (ts.isArrayLiteralExpression(array))
|
|
2902
|
-
text = appendToArray(source, text, array, add);
|
|
2903
|
-
else
|
|
2904
|
-
return {
|
|
2905
|
-
issue: `"nifra.zones.${side}" is not an array; add ${JSON.stringify(add)} to it by hand`,
|
|
2906
|
-
};
|
|
2907
|
-
}
|
|
2908
|
-
return { text };
|
|
2909
|
-
}
|
|
2910
|
-
function declareApiKind(ts, root) {
|
|
2911
|
-
const path = join(root, "package.json");
|
|
2912
|
-
if (!existsSync(path)) {
|
|
2913
|
-
return {
|
|
2914
|
-
issue: 'there is none in the app; declare "nifra": { "kind": "api" } in the package.json that governs it',
|
|
2915
|
-
};
|
|
2916
|
-
}
|
|
2917
|
-
const text = readFileSync(path, "utf8");
|
|
2918
|
-
const source = ts.parseJsonText(path, text);
|
|
2919
|
-
const top = source.statements[0]?.expression;
|
|
2920
|
-
if (top === undefined || !ts.isObjectLiteralExpression(top))
|
|
2921
|
-
return { issue: 'is not a JSON object; add "nifra": { "kind": "api" } to it by hand' };
|
|
2922
|
-
const nifra = jsonProperty(ts, top, "nifra");
|
|
2923
|
-
if (nifra === undefined) {
|
|
2924
|
-
return { text: insertProperty(source, text, top, '"nifra": { "kind": "api" }') };
|
|
2925
|
-
}
|
|
2926
|
-
if (!ts.isObjectLiteralExpression(nifra.initializer))
|
|
2927
|
-
return { issue: '"nifra" is not an object; add "kind": "api" to it by hand' };
|
|
2928
|
-
return { text: insertProperty(source, text, nifra.initializer, '"kind": "api"') };
|
|
2929
|
-
}
|
|
2930
|
-
const PACKAGE_PATH_FIELDS = new Set([
|
|
2931
|
-
"main",
|
|
2932
|
-
"module",
|
|
2933
|
-
"types",
|
|
2934
|
-
"typings",
|
|
2935
|
-
"browser",
|
|
2936
|
-
"source",
|
|
2937
|
-
"bin",
|
|
2938
|
-
"exports",
|
|
2939
|
-
"imports",
|
|
2940
|
-
"files",
|
|
2941
|
-
"scripts",
|
|
2942
|
-
]);
|
|
2943
|
-
function rewritePackagePaths(ts, text, moves) {
|
|
2944
|
-
const source = ts.parseJsonText("package.json", text);
|
|
2945
|
-
const top = source.statements[0]?.expression;
|
|
2946
|
-
if (top === undefined || !ts.isObjectLiteralExpression(top) || moves.size === 0)
|
|
2947
|
-
return { text, count: 0 };
|
|
2948
|
-
const ordered = [...moves].sort(([a], [b]) => b.length - a.length);
|
|
2949
|
-
const pattern = new RegExp(`(?<![\\w.@/-])(\\./)?(${ordered.map(([from]) => escapeRegExp(from)).join("|")})(?![\\w/-]|\\.\\w)`, "g");
|
|
2950
|
-
const edits = [];
|
|
2951
|
-
let count = 0;
|
|
2952
|
-
const visit = (node, field) => {
|
|
2953
|
-
if (ts.isStringLiteral(node)) {
|
|
2954
|
-
if (field !== "scripts" && node.text.includes("*"))
|
|
2955
|
-
return;
|
|
2956
|
-
const value = node.text.replace(pattern, (_match, dot, from) => {
|
|
2957
|
-
count += 1;
|
|
2958
|
-
return `${dot ?? ""}${moves.get(from) ?? from}`;
|
|
2959
|
-
});
|
|
2960
|
-
if (value !== node.text)
|
|
2961
|
-
edits.push({
|
|
2962
|
-
start: node.getStart(source),
|
|
2963
|
-
end: node.getEnd(),
|
|
2964
|
-
text: JSON.stringify(value),
|
|
2965
|
-
});
|
|
2966
|
-
return;
|
|
2967
|
-
}
|
|
2968
|
-
if (ts.isArrayLiteralExpression(node)) {
|
|
2969
|
-
for (const element of node.elements)
|
|
2970
|
-
visit(element, field);
|
|
2971
|
-
return;
|
|
2972
|
-
}
|
|
2973
|
-
if (!ts.isObjectLiteralExpression(node))
|
|
2974
|
-
return;
|
|
2975
|
-
for (const property of node.properties) {
|
|
2976
|
-
if (!ts.isPropertyAssignment(property))
|
|
2977
|
-
continue;
|
|
2978
|
-
if (ts.isStringLiteral(property.name) && property.name.text.startsWith("//"))
|
|
2979
|
-
continue;
|
|
2980
|
-
visit(property.initializer, field);
|
|
2981
|
-
}
|
|
2982
|
-
};
|
|
2983
|
-
for (const property of top.properties) {
|
|
2984
|
-
if (!ts.isPropertyAssignment(property) || !ts.isStringLiteral(property.name))
|
|
2985
|
-
continue;
|
|
2986
|
-
if (PACKAGE_PATH_FIELDS.has(property.name.text))
|
|
2987
|
-
visit(property.initializer, property.name.text);
|
|
2988
|
-
}
|
|
2989
|
-
return { text: applyEdits(text, edits), count };
|
|
2990
|
-
}
|
|
2991
|
-
function staleContentGlobs(ts, text, moves) {
|
|
2992
|
-
const globs = [];
|
|
2993
|
-
const source = ts.createSourceFile("tailwind.config.ts", text, ts.ScriptTarget.Latest, true);
|
|
2994
|
-
const visit = (node) => {
|
|
2995
|
-
if (ts.isStringLiteralLike(node) && /[*/]/.test(node.text) && !node.text.startsWith("!"))
|
|
2996
|
-
globs.push(node.text.replace(/^\.\//, ""));
|
|
2997
|
-
ts.forEachChild(node, visit);
|
|
2998
|
-
};
|
|
2999
|
-
visit(source);
|
|
3000
|
-
const matchers = globs.map((glob) => ({ glob, matcher: new Bun.Glob(glob) }));
|
|
3001
|
-
const lost = moves.filter(({ from, to }) => matchers.some(({ matcher }) => matcher.match(from)) &&
|
|
3002
|
-
!matchers.some(({ matcher }) => matcher.match(to)));
|
|
3003
|
-
const first = lost[0];
|
|
3004
|
-
if (first === undefined)
|
|
3005
|
-
return undefined;
|
|
3006
|
-
const folders = [...new Set(lost.map(({ to }) => to.split("/")[0] ?? to))].sort(codeUnitOrder);
|
|
3007
|
-
return `its content globs matched ${lost.length} file(s) that move, but none of their new paths (such as "${first.from}" -> "${first.to}"), so Tailwind would stop seeing their classes. Add globs for ${folders.map((folder) => `"./${folder}/**/*"`).join(", ")} (with the extensions you use)`;
|
|
3008
|
-
}
|
|
3009
|
-
function ignoreNifraState(root) {
|
|
3010
|
-
let exitCode = null;
|
|
3011
|
-
try {
|
|
3012
|
-
exitCode = Bun.spawnSync(["git", "check-ignore", "-q", ".nifra/types"], {
|
|
3013
|
-
cwd: root,
|
|
3014
|
-
stdout: "ignore",
|
|
3015
|
-
stderr: "ignore",
|
|
3016
|
-
}).exitCode;
|
|
3017
|
-
}
|
|
3018
|
-
catch {
|
|
3019
|
-
}
|
|
3020
|
-
if (exitCode === 0)
|
|
3021
|
-
return undefined;
|
|
3022
|
-
const path = join(root, ".gitignore");
|
|
3023
|
-
const current = existsSync(path) ? readFileSync(path, "utf8") : "";
|
|
3024
|
-
if (exitCode !== 1 && /^\/?\.nifra(?:\/|\*|\/\*\*)?\s*$/m.test(current))
|
|
3025
|
-
return undefined;
|
|
3026
|
-
const separator = current === "" || current.endsWith("\n") ? "" : "\n";
|
|
3027
|
-
return `${current}${separator}.nifra/\n`;
|
|
3028
|
-
}
|
|
3029
|
-
function pruneVacatedDirs(root, vacated) {
|
|
3030
|
-
const dirs = new Set();
|
|
3031
|
-
for (const file of vacated)
|
|
3032
|
-
dirs.add(dirname(join(root, file)));
|
|
3033
|
-
for (const start of [...dirs].sort((a, b) => b.length - a.length)) {
|
|
3034
|
-
for (let dir = start; dir !== root && dir.startsWith(`${root}${sep}`); dir = dirname(dir)) {
|
|
3035
|
-
if (!existsSync(dir) || readdirSync(dir).length > 0)
|
|
3036
|
-
break;
|
|
3037
|
-
rmdirSync(dir);
|
|
3038
|
-
}
|
|
3039
|
-
}
|
|
3040
|
-
}
|
|
3041
1866
|
//# sourceMappingURL=migrate-layout.js.map
|