Skip to content

πŸͺ’ Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat(web): pnpm structure:move β€” a move with every importer rewritten (LFE-14804) (#15820)

Nikita Kabardind37d69c2ed50
diff --git a/web/.dependency-cruiser.js b/web/.dependency-cruiser.js
index d79eb2ff11..d7fee0f618 100644
--- a/web/.dependency-cruiser.js
+++ b/web/.dependency-cruiser.js
@@ -30,12 +30,19 @@ module.exports = {
{
name: "rfc08-features-via-index",
comment:
- "Rule 8: features import other features only through their index.ts.",
+ "Rule 8: features import other features only through a surface β€” " +
+ "`<feature>` from client code, `<feature>/server` from server code. " +
+ "A feature has two, because it is a full-stack slice: the root index " +
+ "must stay client-safe, so it can never re-export server/.",
severity: "warn",
from: { path: "^(src/(?:ee/)?features/[^/]+)/" },
to: {
path: "^src/(?:ee/)?features/[^/]+/",
- pathNot: ["^$1/", "^src/(?:ee/)?features/[^/]+/index\\.tsx?$"],
+ pathNot: [
+ "^$1/",
+ "^src/(?:ee/)?features/[^/]+/index\\.tsx?$",
+ "^src/(?:ee/)?features/[^/]+/server/index\\.tsx?$",
+ ],
},
},
{
diff --git a/web/AGENTS.md b/web/AGENTS.md
index 478d3d9ed3..44e2928051 100644
--- a/web/AGENTS.md
+++ b/web/AGENTS.md
@@ -232,6 +232,9 @@ Sentry instrumentation skill first and decide whether it should capture at all
- Agent browser install to the default user-level Playwright cache: `pnpm run playwright:install`
- Build: `pnpm --filter web run build`
- Structure-RFC violation counts: `pnpm --filter web run structure:stats`
+- Move files/folders with every importer rewritten:
+ `pnpm --filter web run structure:move <from...> <to-dir>` β€” never hand-edit
+ import specifiers for a move, and never `mv` a source file without it
(see `web/scripts/structure/README.md`)
## Playbooks
diff --git a/web/package.json b/web/package.json
index 518229cf08..aec921e0e1 100644
--- a/web/package.json
+++ b/web/package.json
@@ -27,6 +27,7 @@
"build-trace": "dotenv -e ../.env -- next internal trace .next/trace-turbopack",
"storybook": "dotenv -e ../.env -- storybook dev -p 6006",
"structure:stats": "node scripts/structure/stats.mjs",
+ "structure:move": "node scripts/structure/move.mjs",
"build-storybook": "dotenv -e ../.env -- storybook build",
"sfdc:backfill": "dotenv -e ../.env -- tsx src/ee/features/sfdc-sync/scripts/backfillSfdcLeads.ts"
},
diff --git a/web/scripts/structure/README.md b/web/scripts/structure/README.md
index 30c52c4347..51eb3d4e46 100644
--- a/web/scripts/structure/README.md
+++ b/web/scripts/structure/README.md
@@ -1,4 +1,17 @@
-# structure:stats β€” the project-structure RFC dashboard
+# structure:\* β€” the project-structure RFC panel
+
+`structure:stats` says what is wrong and ranks what to fix next;
+`structure:move` does the mechanical half of the fix. The loop they exist for is
+many small, boring PRs, each visibly dropping the count:
+
+1. `pnpm structure:stats --next --scope <area>` β†’ take item #1.
+2. Mechanical part via `pnpm structure:move`; judgment part (splits, renames,
+ authoring an `index.ts`) by hand.
+3. Re-run stats. The PR body is the item headline plus the before/after counts
+ from `--diff` ("rule 6: 104 β†’ 63"). One item per PR; no baseline
+ regeneration unless it is the point of the PR.
+
+# structure:stats β€” the RFC dashboard
Counts violations of the [web project-structure RFC](https://linear.app/clickhouse/document/langfuse-web-project-code-structure-rfc-ecbc304915d6)
(meta LFE-14748) per rule, so migration progress is one visible number.
@@ -28,31 +41,131 @@ rescores. Leverage = violations cleared Γ— rule weight (`RULE_WEIGHTS` in
`next.mjs` β€” runtime hazards outrank naming nits). The intended loop:
`--next --scope <area>` β†’ fix item 1 as its own PR β†’ re-run.
+## structure:move β€” a move with the imports carried along
+
+```sh
+pnpm structure:move <from...> <to-dir> # move files and/or folders, batched
+pnpm structure:move <from> <to-file> # rename (one source, file target)
+pnpm structure:move --dry-run src/hooks/useFoo.ts src/features/bar/hooks
+```
+
+A target ending in a source extension is a rename rather than a move into a
+directory β€” `fns/tree-building.ts fns/treeBuilding.ts` β€” and the siblings follow
+the stem (`tree-building.clienttest.ts` β†’ `treeBuilding.clienttest.ts`).
+Renaming the _export_ inside the file is a content edit and stays manual, so a
+naming fix is two steps: the rename here, the symbol by hand. Directory renames
+are not in the surface: a directory source with a file-shaped target is
+rejected, because `git mv` would happily produce a directory called `foo.ts`.
+Move the contents instead.
+
+Case-only renames (`BreakdownToolTip.tsx` β†’ `BreakdownTooltip.tsx`) work, and
+they are the whole naming sweep's bread and butter. They need two special
+moves: macOS reports the destination as already existing, so the conflict check
+lets a case-only pair through β€” but only once `stat` says the two paths are the
+same inode, so on a case-sensitive filesystem a genuinely different file at that
+path still blocks β€” and `git mv -f` performs it; and TypeScript would
+see no rename at all under a case-insensitive host, so a batch containing one
+forces case-sensitive comparison β€” otherwise every importer keeps the old
+spelling and only breaks on Linux CI.
+
+Flags: `--dry-run` (print the plan and every rewrite, change nothing),
+`--no-siblings`, `--no-verify` (skip the closing `tsc` + `--diff`), `--no-color`.
+
+The rewrites come from TypeScript's own
+`LanguageService.getEditsForFileRename` over `web/tsconfig.json` β€” the exact
+primitive VS Code's "move file" uses β€” so `@/src/...` aliases, extension-less
+specifiers, index resolution and literal dynamic `import()` are the compiler's
+problem, not ours. Booting the service costs ~5–15s and every move after that
+is instant, which is why the CLI is batch-shaped.
+
+- **Batch moves need a live layout.** The host is mutable: each rename bumps the
+ affected script versions and the project version, so move #2 computes its
+ edits against the tree move #1 produced. Freeze those versions and the second
+ move's spans are offsets into stale text β€” it shreds any importer that both
+ moves touch, silently. That is the whole reason this is a script and not a
+ `for` loop around a fresh program.
+- **Colocated siblings come along.** `X.tsx` brings `X.clienttest.tsx`,
+ `X.stories.tsx`, `X.fixtures.ts` (and `.servertest`/`.test`/`.spec`/`.module`),
+ both flat next to it and from its `__tests__/` β€” where they land in a
+ `__tests__/` at the destination. A facet segment before the tag counts
+ (`X.media.clienttest.tsx`), the same shape rule 18 reads, and the nearest
+ subject owns the file β€” `X.bar.clienttest.tsx` stays with `X.bar.tsx` when that
+ exists. `--no-siblings` opts out. The tag list is closed on purpose, so
+ `index.ts` never drags `index.tsx` along.
+- **Move β‰  edit (rule 15).** Rewrites land in importers. A moved file may only
+ change where an alias self-reference (`@/src/<old path>/sibling`) has to
+ follow the subtree it is part of; those are listed separately. A rewrite that
+ would point a moved file at something left behind aborts the whole batch β€”
+ move that sibling too, or do the move by hand.
+- **History is preserved**: `git mv`, so `log --follow` and `blame -C` keep
+ working. Importer rewrites go through prettier (a longer specifier can push a
+ line past the print width) and are left **unstaged** β€” `git mv` stages the
+ renames by nature, but staging edited importers would fold any unrelated work
+ in them into this move's index entry. A rewrite target that already has
+ uncommitted changes is called out before anything is written.
+- **Nothing is destructive.** No `reset`, no `stash`, no `checkout --`, and no
+ write that can land on top of an existing file. Failures print the way back β€”
+ the inverse `structure:move`, not a reset β€” including a batch that dies
+ halfway, which prints the inverse of whatever completed.
+- **Idempotent**: everything already at the destination is a no-op, exit 0.
+- **Blind spot, surfaced not solved**: modules named by string (`vi.mock` paths,
+ worker URLs, route strings) are invisible to the compiler, so `tsc` stays
+ green while they dangle. Each run greps the repo for the old path and prints
+ every surviving hit β€” fix those by hand. Not theoretical: the
+ `AdvancedJsonViewer` calibration move left three `vi.mock()` paths behind and
+ six tests failed under a green typecheck.
+
+Splitting a file and directory renames are not part of the surface
+(follow-up LFE-14806).
+
## Rule β†’ mechanism
-| Rule | What | Counted by |
-| ------ | --------------------------------------------------- | ------------------------------------------------------------------------ |
-| 1–4 | component/hook/fn/store/context file shape + naming | census (TS parse) |
-| 5 | kind folders closed list | census (dir walk) |
-| 6 | single-feature files live in the feature | graph (used-in inversion) |
-| 7 | no importing another component's internals | graph + `.dependency-cruiser.js` |
-| 8 | cross-feature imports via feature `index.ts` | graph + `.dependency-cruiser.js` |
-| 9 | `index.ts` only at feature roots, re-exports only | census |
-| 10 | no client β†’ `server/` (types excepted) | graph + `.dependency-cruiser.js` |
-| 11 | no runtime import cycles | graph + `.dependency-cruiser.js` |
-| 12 | `src/pages` files import only a Page component | graph + `.dependency-cruiser.js` |
-| 13 | `components/ui` frozen | census (file count, baseline ratchets adds) |
-| 14, 15 | design-system purity; git-mv moves | review / process β€” not counted |
-| 16 | ESLint ignores at file level only | census (line-level disables) |
-| 17 | baseline only shrinks | this baseline + `--diff` |
-| 18 | fn/hook tests colocated flat | census |
-| 19 | only tests import `__tests__` | graph + `.dependency-cruiser.js` |
-| 20 | no unused exports | graph (file-level orphans; symbol-level needs a knip config β€” follow-up) |
+| Rule | What | Counted by |
+| ------ | --------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------ |
+| 1–4 | component/hook/fn/store/context file shape + naming; a `fns/` module folder groups one engine | census (TS parse) |
+| 5 | kind folders closed list (+ `constants`, `types`; `docs` anywhere) | census (dir walk) |
+| 6 | single-feature files live in the feature | graph (used-in inversion) |
+| 7 | no importing another component's internals | graph + `.dependency-cruiser.js` |
+| 8 | cross-feature imports via a feature surface (`index.ts`, `server/index.ts`) | graph + `.dependency-cruiser.js` |
+| 9 | `index.ts` at a feature root and its `server/` root | census |
+| 10 | no client β†’ `server/` (types excepted) | graph + `.dependency-cruiser.js` |
+| 11 | no runtime import cycles | graph + `.dependency-cruiser.js` |
+| 12 | `src/pages` files import only a Page component | graph + `.dependency-cruiser.js` |
+| 13 | `components/ui` frozen | census (file count, baseline ratchets adds) |
+| 14, 15 | design-system purity; git-mv moves | review / process β€” not counted |
+| 16 | ESLint ignores at file level only | census (line-level disables) |
+| 17 | baseline only shrinks | this baseline + `--diff` |
+| 18 | fn/hook tests colocated flat | census |
+| 19 | only tests import `__tests__` | graph + `.dependency-cruiser.js` |
+| 20 | no unused exports | graph (file-level orphans; symbol-level needs a knip config β€” follow-up) |
`.dependency-cruiser.js` carries the import rules as CI-ready warnings; the
detectors here are the exact reference implementation (the config's regex
approximations under-count some nested-component cases β€” see its header).
+## RFC amendments the detectors now encode
+
+Found by migrating `features/traces` and approved in-flight; the Linear RFC is
+the source of truth and carries the prose (handed over via the LFE-14804
+mailbox).
+
+- **`constants/` and `types/` are kind folders.** A constant is not a function,
+ so it does not belong in `fns/`, and a per-feature `config/` or `shared/`
+ folder is how predictability dies. `types/` holds one type per file, named
+ after it β€” a `types.ts` inside `fns/` is wrong twice.
+- **`docs/` is allowed at any level** and is not a kind folder: it holds prose,
+ nothing imports it. A README beside a component is prose loose in a code
+ folder.
+- **A `fns/` module folder groups one engine.** `fns/searchJson/` holding
+ `matchNode.ts`, `buildIndex.ts` β€” the grouping lives in the folder name so
+ each file still has one export. It may not grow kind folders of its own; the
+ moment it wants `components/` it is a feature, not a module.
+- **A feature has two surfaces**, because it is a full-stack slice: `index.ts`
+ at the root (client-safe) and `server/index.ts`. If one index re-exported
+ `server/`, every client importer would transitively evaluate Prisma and
+ ClickHouse β€” nothing crashes when that happens, which is exactly why it has
+ to be structural.
+
## Calibration notes (as of the reworked traces feature, #15784)
- Component boundary (rules 7/9) = any PascalCase directory; its public entry
diff --git a/web/scripts/structure/detectors.mjs b/web/scripts/structure/detectors.mjs
index 806137b225..08f577c10a 100644
--- a/web/scripts/structure/detectors.mjs
+++ b/web/scripts/structure/detectors.mjs
@@ -21,7 +21,18 @@ const KIND_DIRS = new Set([
"stores",
"fns",
"server",
+ "constants",
+ "types",
]);
+const KINDS_LIST = [...KIND_DIRS].join("|");
+
+/** A feature's server-side public surface (RFC rule 9, amended). */
+const FEATURE_SERVER_INDEX =
+ /^src\/(?:ee\/)?features\/[^/]+\/server\/index\.tsx?$/;
+
+// `docs/` holds prose, not code, and is allowed at any level: it is where a
+// README goes so it is not loose in a code folder.
+const DOC_DIRS = new Set(["docs"]);
/** @type {(p: string) => boolean} */
export const isTestish = (p) =>
@@ -84,7 +95,8 @@ export function rule7(modules) {
return out;
}
-// Rule 8 β€” features import other features only through their index.ts.
+// Rule 8 β€” features import other features only through a surface: the root
+// index from client code, server/index.ts from server code (rule 9, amended).
/** @param {Module[]} modules @returns {Violation[]} */
export function rule8(modules) {
const out = [];
@@ -96,6 +108,9 @@ export function rule8(modules) {
if (!to || to === from) continue;
if (dep.resolved === `${to}index.ts` || dep.resolved === `${to}index.tsx`)
continue;
+ // the same exception .dependency-cruiser.js carries, so the census and
+ // CI agree on the server surface
+ if (FEATURE_SERVER_INDEX.test(dep.resolved)) continue;
out.push(v(`${mod.source} -> ${dep.resolved}`, mod.source, dep.resolved));
}
}
@@ -434,7 +449,8 @@ export function rule4(files, exportsOf) {
issues.push("dump file");
if (values.length > 1)
issues.push(
- `${values.length} exports: ${values.map((e) => e.name).join(", ")}`,
+ `${values.length} exports: ${values.map((e) => e.name).join(", ")}` +
+ ` (one per file, or a module folder here)`,
);
if (issues.length) out.push(v(`${f}: ${issues.join("; ")}`, f));
}
@@ -455,7 +471,19 @@ export function rule5(dirs) {
const name = base(d);
const parent = d.slice(0, -(name.length + 1));
const parentName = base(parent);
- if (name === "__tests__") continue;
+ if (name === "__tests__" || DOC_DIRS.has(name)) continue;
+ // A module folder inside fns/ groups one engine's modules (RFC rule 4). It
+ // may not grow kind folders of its own β€” that would make it a feature β€” so
+ // a reserved kind name here is not a module folder and stays in scope.
+ if (parentName === "fns" && !PASCAL.test(name) && !KIND_DIRS.has(name))
+ continue;
+ if (KIND_DIRS.has(parentName) && parentName !== "components") {
+ const grandparent = base(parent.slice(0, -(parentName.length + 1)));
+ if (grandparent === "fns") {
+ out.push(v(`${d}: kind folder inside a fns/ module folder`, d));
+ continue;
+ }
+ }
if (KIND_DIRS.has(name)) {
const featRootDir = /^src\/(?:ee\/)?features\/[^/]+$/.test(parent);
if (!featRootDir && !PASCAL.test(parentName))
@@ -469,12 +497,7 @@ export function rule5(dirs) {
if (parentName !== "components")
out.push(v(`${d}: component folder outside components/`, d));
} else {
- out.push(
- v(
- `${d}: '${name}' is not a kind folder (components|hooks|contexts|stores|fns|server)`,
- d,
- ),
- );
+ out.push(v(`${d}: '${name}' is not a kind folder (${KINDS_LIST})`, d));
}
}
return out;
@@ -489,8 +512,13 @@ export function rule9(files, exportsOf) {
/^src\/(?:ee\/)?(features|components|hooks|contexts|stores|fns|utils|constants|lib)\//;
for (const f of files) {
if (!/(^|\/)index\.[jt]sx?$/.test(f) || !SCOPE.test(f)) continue;
- if (/(^|\/)server\//.test(f)) continue; // server/ internals: unspecified by the RFC
- if (/^src\/(?:ee\/)?features\/[^/]+\/index\.tsx?$/.test(f)) {
+ // A feature has two surfaces: the root index (client-safe) and
+ // server/index.ts (server-side). Anything deeper in server/ is unspecified.
+ if (/(^|\/)server\//.test(f) && !FEATURE_SERVER_INDEX.test(f)) continue;
+ if (
+ /^src\/(?:ee\/)?features\/[^/]+\/index\.tsx?$/.test(f) ||
+ FEATURE_SERVER_INDEX.test(f)
+ ) {
const ex = exportsOf(f);
if (ex.some((e) => e.kind === "star")) out.push(v(`${f}: export *`, f));
if (ex.some((e) => e.kind === "value"))
diff --git a/web/scripts/structure/move.mjs b/web/scripts/structure/move.mjs
new file mode 100644
index 0000000000..045cb6fe7a
--- /dev/null
+++ b/web/scripts/structure/move.mjs
@@ -0,0 +1,846 @@
+#!/usr/bin/env node
+// structure:move β€” move files/folders and rewrite every importer, the way an
+// IDE does: TypeScript's own LanguageService.getEditsForFileRename over
+// web/tsconfig.json, so `@/src/...` aliases, extension-less specifiers, index
+// resolution and literal dynamic import() are all handled by the compiler.
+//
+// pnpm structure:move <from...> <to-dir> move into a directory
+// pnpm structure:move <from> <to-file> rename (one source, file target)
+//
+// Flags: --dry-run --no-siblings --no-verify --no-color
+//
+// Booting the language service costs ~10-20s and every move after that is
+// instant, which is why the CLI is batch-shaped.
+//
+// Invariants:
+// - move β‰  edit (RFC rule 15): moved files stay byte-identical; rewrites
+// land in importers only. A move that would edit a moved file aborts.
+// - history preserved: renames go through `git mv`.
+// - never destructive: on failure the revert command is printed, not run.
+import { execFileSync } from "node:child_process";
+import {
+ existsSync,
+ mkdirSync,
+ readdirSync,
+ renameSync,
+ rmdirSync,
+ statSync,
+ writeFileSync,
+} from "node:fs";
+import { createRequire } from "node:module";
+import { dirname, relative, resolve } from "node:path";
+import { fileURLToPath } from "node:url";
+
+const require = createRequire(import.meta.url);
+/** @type {typeof import("typescript")} */
+const ts = require("typescript");
+
+/** @typedef {import("typescript").TextChange} TextChange */
+/** @typedef {{ from: string, to: string, isDir: boolean, sibling: boolean }} Move */
+
+const webRoot = resolve(dirname(fileURLToPath(import.meta.url)), "../..");
+const invokedFrom = process.cwd();
+
+// Source we may rewrite, and the extensions that mark a rename target. The
+// edit set also offers tsconfig entries and generated `.next/types` decls.
+const SOURCE_EXT = [".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs"];
+
+// A colocated sibling is `<stem>.<facet?>.<tag>.<ext>` next to `<stem>.<ext>`;
+// the tag list is closed so that `index.ts` never drags `index.tsx` along.
+const SIBLING_TAGS = [
+ "clienttest",
+ "servertest",
+ "test",
+ "spec",
+ "stories",
+ "fixtures",
+ "module",
+];
+
+// --- args -------------------------------------------------------------------
+const args = process.argv.slice(2);
+/** @type {(name: string) => boolean} */
+const flag = (name) => args.includes(`--${name}`);
+const positional = args.filter((a) => !a.startsWith("--"));
+const dryRun = flag("dry-run");
+const withSiblings = !flag("no-siblings");
+const verify = !flag("no-verify");
+
+const useColor =
+ process.stdout.isTTY && !process.env.NO_COLOR && !flag("no-color");
+/** @type {(code: string) => (s: string) => string} */
+const paint = (code) => (s) => (useColor ? `\x1b[${code}m${s}\x1b[0m` : `${s}`);
+const green = paint("32");
+const red = paint("31");
+const dim = paint("2");
+const bold = paint("1");
+
+if (positional.length < 2) {
+ console.error(
+ [
+ "usage: pnpm structure:move <from...> <to-dir> move into a directory",
+ " pnpm structure:move <from> <to-file> rename in place",
+ "",
+ " <from> files and/or folders (web-relative, e.g. src/hooks/useFoo.ts)",
+ " <to-dir> destination directory; created if missing",
+ " <to-file> a target ending in a source extension renames instead:",
+ " src/f/fns/tree-building.ts src/f/fns/treeBuilding.ts",
+ "",
+ " --dry-run show the plan and the importer rewrites, change nothing",
+ " --no-siblings do not carry <name>.{clienttest,stories,fixtures,...} along",
+ " (flat next to the file, and under its __tests__/)",
+ " --no-verify skip the closing tsc + structure:stats --diff",
+ ].join("\n"),
+ );
+ process.exit(1);
+}
+
+// --- paths -------------------------------------------------------------------
+/** Web-relative, forward-slashed. Accepts web-relative, repo-relative or absolute input. */
+/** @type {(p: string) => string} */
+function toWebRel(p) {
+ const rel = relative(webRoot, resolve(invokedFrom, p)).replace(/\\/g, "/");
+ if (rel.startsWith("..")) {
+ console.error(`${red("outside web/:")} ${p}`);
+ process.exit(1);
+ }
+ // `web/src/...` pasted while already inside web/ β€” there is no web/web
+ if (rel.startsWith("web/") && !existsSync(abs("web"))) return rel.slice(4);
+ return rel;
+}
+/** @type {(rel: string) => string} */
+const abs = (rel) => `${webRoot}/${rel}`;
+/** @type {(rel: string) => boolean} */
+const isDirOnDisk = (rel) =>
+ existsSync(abs(rel)) && statSync(abs(rel)).isDirectory();
+/** "" for a file sitting directly in web/ β€” lastIndexOf alone would truncate it */
+/** @type {(p: string) => string} */
+const parentDir = (p) =>
+ p.includes("/") ? p.slice(0, p.lastIndexOf("/")) : "";
+/** @type {(p: string) => string} */
+const baseName = (p) => p.slice(p.lastIndexOf("/") + 1);
+/** Same trap as parentDir: an extension-less name must keep all of itself */
+/** @type {(p: string) => string} */
+const stemOf = (p) => (p.includes(".") ? p.slice(0, p.lastIndexOf(".")) : p);
+/** Web-relative join: a "" dir must not produce a leading slash, which git reads as absolute */
+/** @type {(...parts: string[]) => string} */
+const join = (...parts) => parts.filter(Boolean).join("/");
+
+/** @type {(absDir: string) => string[]} */
+function walkFiles(absDir) {
+ /** @type {string[]} */
+ const out = [];
+ for (const e of readdirSync(absDir, { withFileTypes: true })) {
+ const p = `${absDir}/${e.name}`;
+ if (e.isDirectory()) out.push(...walkFiles(p));
+ else out.push(p);
+ }
+ return out;
+}
+
+// --- plan --------------------------------------------------------------------
+// One source plus a target that names a file is a rename; anything else is a
+// move into a directory. `getEditsForFileRename` does not care which it is.
+const lastArg = toWebRel(
+ /** @type {string} */ (positional[positional.length - 1]),
+);
+const fromArgs = positional.slice(0, -1).map(toWebRel);
+const renameTo =
+ fromArgs.length === 1 &&
+ !isDirOnDisk(lastArg) &&
+ SOURCE_EXT.some((e) => lastArg.endsWith(e))
+ ? lastArg
+ : null;
+const toDir = renameTo ? parentDir(renameTo) : lastArg;
+
+if (!renameTo && !isDirOnDisk(toDir) && existsSync(abs(toDir))) {
+ console.error(`${red("destination is a file:")} ${toDir}`);
+ process.exit(1);
+}
+// A rename target names a file, so a directory source would just produce a
+// directory called `foo.ts` β€” git performs that happily. Directory renames are
+// not in the surface (README): move the contents instead.
+if (renameTo && isDirOnDisk(/** @type {string} */ (fromArgs[0]))) {
+ console.error(
+ `${red("directory rename:")} ${fromArgs[0]} is a directory, and ${renameTo} names a file β€” move the directory's contents to their new home instead`,
+ );
+ process.exit(1);
+}
+
+/** @type {Map<string, Move>} keyed by `from` */
+const plan = new Map();
+/** @type {(from: string, to: string, sibling: boolean) => void} */
+function addMove(from, to, sibling) {
+ if (plan.has(from)) return;
+ plan.set(from, { from, to, isDir: isDirOnDisk(from), sibling });
+}
+/**
+ * `<stem>.<facet?>.<tag>.<ext>`: a facet segment before the tag is normal here
+ * (`PrettyJsonView.media.clienttest.tsx`) and rule 18 reads them the same way.
+ * The nearest subject owns the file, so `Foo.bar.clienttest.tsx` belongs to
+ * `Foo.bar.tsx` when that exists, not to `Foo.tsx`.
+ */
+/** @type {(dir: string, stem: string, entry: string) => boolean} */
+function isSiblingOf(dir, stem, entry) {
+ if (!entry.startsWith(`${stem}.`)) return false;
+ const segments = entry.slice(stem.length + 1).split(".");
+ segments.pop(); // the extension
+ if (!segments.some((s) => SIBLING_TAGS.includes(s))) return false;
+ let subject = stem;
+ for (const segment of segments) {
+ if (SIBLING_TAGS.includes(segment)) break;
+ subject += `.${segment}`;
+ if (SOURCE_EXT.some((e) => existsSync(abs(join(dir, subject + e)))))
+ return false;
+ }
+ return true;
+}
+
+for (const from of fromArgs) {
+ const to = renameTo ?? join(toDir, baseName(from));
+ if (!existsSync(abs(from)) && !existsSync(abs(to))) {
+ console.error(`${red("not found:")} ${from}`);
+ process.exit(1);
+ }
+ addMove(from, to, false);
+ if (!withSiblings || isDirOnDisk(from) || !existsSync(abs(from))) continue;
+ const dir = parentDir(from);
+ const base = baseName(from);
+ const stem = stemOf(base);
+ // a rename carries its siblings' stems along: Foo.clienttest.tsx -> Bar.clienttest.tsx
+ const newBase = baseName(to);
+ const newStem = stemOf(newBase);
+ /** @type {(entry: string) => string} */
+ const renamed = (entry) => newStem + entry.slice(stem.length);
+ for (const entry of readdirSync(abs(dir)))
+ if (isSiblingOf(dir, stem, entry))
+ addMove(join(dir, entry), join(toDir, renamed(entry)), true);
+ if (isDirOnDisk(join(dir, "__tests__")))
+ // the subject a `__tests__/` entry belongs to still lives in `dir`
+ for (const entry of readdirSync(abs(join(dir, "__tests__"))))
+ if (isSiblingOf(dir, stem, entry))
+ addMove(
+ join(dir, "__tests__", entry),
+ join(toDir, "__tests__", renamed(entry)),
+ true,
+ );
+}
+
+/** @type {(m: Move) => boolean} */
+const isCaseOnly = (m) =>
+ m.from !== m.to && m.from.toLowerCase() === m.to.toLowerCase();
+/**
+ * A case-insensitive filesystem reports a case-only rename's destination as
+ * existing because it IS the source β€” same inode. On a case-sensitive one it is
+ * a second, unrelated file, and overwriting it would be destructive, so only
+ * the same-inode case may skip the conflict check.
+ */
+/** @type {(m: Move) => boolean} */
+const isSelfCaseRename = (m) => {
+ if (!isCaseOnly(m)) return false;
+ try {
+ const from = statSync(abs(m.from));
+ const to = statSync(abs(m.to));
+ return from.ino === to.ino && from.dev === to.dev;
+ } catch {
+ return false;
+ }
+};
+
+/** @type {(msg: string) => never} */
+function bail(msg) {
+ console.error(msg);
+ process.exit(1);
+}
+/** @type {Map<string, string>} destination -> the source that claimed it */
+const claimed = new Map();
+for (const m of plan.values()) {
+ // two sources with one basename would both target the same path; git mv
+ // refuses the second, and a rename fallback would silently clobber the first
+ const already = claimed.get(m.to);
+ if (already)
+ bail(
+ `${red("two sources, one destination:")} ${already} and ${m.from} both want ${m.to}`,
+ );
+ claimed.set(m.to, m.from);
+ if (m.isDir && (toDir === m.from || toDir.startsWith(`${m.from}/`)))
+ bail(
+ `${red("destination is inside the source:")} ${toDir} is under ${m.from}`,
+ );
+ for (const other of plan.values())
+ if (other !== m && other.isDir && m.from.startsWith(`${other.from}/`))
+ bail(
+ `${red("nested source:")} ${m.from} is already inside ${other.from}`,
+ );
+}
+
+// --- idempotence + conflicts --------------------------------------------------
+/** @type {Move[]} */
+const pending = [];
+/** @type {Move[]} */
+const done = [];
+for (const m of plan.values()) {
+ if (!existsSync(abs(m.from))) {
+ if (existsSync(abs(m.to))) done.push(m);
+ else {
+ console.error(`${red("not found:")} ${m.from}`);
+ process.exit(1);
+ }
+ continue;
+ }
+ // a case-only rename looks like an existing destination on a
+ // case-insensitive filesystem, but it is exactly the naming sweep's job
+ if (existsSync(abs(m.to)) && !isSelfCaseRename(m)) {
+ console.error(
+ `${red("destination exists:")} ${m.to} (source ${m.from} is still there too)`,
+ );
+ process.exit(1);
+ }
+ pending.push(m);
+}
+
+const hasCaseOnly = pending.some(isCaseOnly);
+
+if (!pending.length) {
+ console.log(
+ `${green("already applied")} β€” ${done.length} path${done.length === 1 ? "" : "s"} already at ${toDir}`,
+ );
+ process.exit(0);
+}
+if (done.length)
+ console.log(
+ dim(`${done.length} of ${plan.size} already at ${toDir} β€” moving the rest`),
+ );
+
+console.log(
+ bold(renameTo ? `structure:move β†’ ${renameTo}` : `structure:move β†’ ${toDir}`),
+);
+for (const m of pending) {
+ const destDir = parentDir(m.to);
+ const note = !m.sibling
+ ? ""
+ : dim(` (sibling${destDir === toDir ? "" : ` β†’ ${destDir}/`})`);
+ console.log(` ${m.isDir ? "dir " : "file"} ${m.from}${note}`);
+}
+console.log();
+
+// --- language service over a mutable in-memory layout ------------------------
+const t0 = Date.now();
+const parsed = ts.getParsedCommandLineOfConfigFile(
+ `${webRoot}/tsconfig.json`,
+ {},
+ {
+ useCaseSensitiveFileNames: ts.sys.useCaseSensitiveFileNames,
+ readDirectory: ts.sys.readDirectory,
+ fileExists: ts.sys.fileExists,
+ readFile: ts.sys.readFile,
+ getCurrentDirectory: () => webRoot,
+ onUnRecoverableConfigFileDiagnostic: (dg) => {
+ console.error(ts.flattenDiagnosticMessageText(dg.messageText, "\n"));
+ process.exit(1);
+ },
+ },
+);
+if (!parsed) {
+ console.error("could not read web/tsconfig.json");
+ process.exit(1);
+}
+
+/** @type {(p: string) => string} */
+const norm = (p) => p.replace(/\\/g, "/");
+/** @type {Map<string, string>} in-memory content (edited or relocated) */
+const overlay = new Map();
+/** @type {Set<string>} paths the in-memory layout no longer has */
+const removed = new Set();
+/** @type {Map<string, number>} */
+const versions = new Map();
+/** @type {Set<string>} */
+const scriptFileNames = new Set(parsed.fileNames.map(norm));
+let projectVersion = 0;
+
+/** @type {(p: string) => string | undefined} */
+const currentText = (p) =>
+ overlay.has(p)
+ ? overlay.get(p)
+ : removed.has(p)
+ ? undefined
+ : ts.sys.readFile(p);
+/** @type {(p: string, text: string) => void} */
+const setText = (p, text) => {
+ overlay.set(p, text);
+ removed.delete(p);
+ versions.set(p, (versions.get(p) ?? 0) + 1);
+ projectVersion++;
+};
+/** @type {(p: string) => void} */
+const dropFile = (p) => {
+ overlay.delete(p);
+ removed.add(p);
+ versions.set(p, (versions.get(p) ?? 0) + 1);
+ projectVersion++;
+};
+
+/** @type {import("typescript").LanguageServiceHost} */
+const lsHost = {
+ getScriptFileNames: () => [...scriptFileNames],
+ getScriptVersion: (f) => String(versions.get(norm(f)) ?? 0),
+ getScriptSnapshot: (f) => {
+ const text = currentText(norm(f));
+ return text === undefined ? undefined : ts.ScriptSnapshot.fromString(text);
+ },
+ getProjectVersion: () => String(projectVersion),
+ getCurrentDirectory: () => webRoot,
+ getCompilationSettings: () => parsed.options,
+ getDefaultLibFileName: (o) => ts.getDefaultLibFilePath(o),
+ // a case-only rename is only a rename under case-sensitive comparison
+ useCaseSensitiveFileNames: () =>
+ hasCaseOnly || ts.sys.useCaseSensitiveFileNames,
+ fileExists: (f) => {
+ const p = norm(f);
+ if (removed.has(p)) return false;
+ return overlay.has(p) || ts.sys.fileExists(p);
+ },
+ readFile: (f, encoding) => {
+ const p = norm(f);
+ if (removed.has(p)) return undefined;
+ return overlay.has(p) ? overlay.get(p) : ts.sys.readFile(p, encoding);
+ },
+ directoryExists: (d) => {
+ const p = norm(d);
+ if (ts.sys.directoryExists(p)) return true;
+ for (const f of overlay.keys()) if (f.startsWith(`${p}/`)) return true;
+ return false;
+ },
+ getDirectories: (d) => ts.sys.getDirectories(norm(d)),
+ readDirectory: (path, extensions, exclude, include, depth) => {
+ const dir = norm(path);
+ const found = ts.sys
+ .readDirectory(dir, extensions, exclude, include, depth)
+ .map(norm)
+ .filter((p) => !removed.has(p));
+ for (const p of overlay.keys())
+ if (
+ p.startsWith(`${dir}/`) &&
+ !found.includes(p) &&
+ (!extensions || extensions.some((e) => p.endsWith(e)))
+ )
+ found.push(p);
+ return found;
+ },
+ realpath: ts.sys.realpath,
+};
+
+const ls = ts.createLanguageService(lsHost, ts.createDocumentRegistry());
+// force the first program build so the boot cost is paid (and visible) once
+ls.getProgram();
+console.log(
+ dim(`language service ready (${((Date.now() - t0) / 1000).toFixed(1)}s)`),
+);
+
+// --- compute the rewrites ----------------------------------------------------
+const formatOptions = ts.getDefaultFormatCodeSettings("\n");
+/** @type {import("typescript").UserPreferences} */
+const preferences = { quotePreference: "double" };
+
+/** @type {Map<string, string>} moved-file new abs path -> its content before the batch */
+const movedOriginal = new Map();
+/** @type {Map<string, string>} old abs path -> new abs path, for every moved FILE */
+const fileRenames = new Map();
+/** @type {Map<string, string>} the inverse, for messages */
+const oldPaths = new Map();
+for (const m of pending) {
+ const sources = m.isDir
+ ? walkFiles(abs(m.from)).map(norm)
+ : [norm(abs(m.from))];
+ for (const src of sources) {
+ const dest = norm(abs(m.to)) + src.slice(norm(abs(m.from)).length);
+ fileRenames.set(src, dest);
+ oldPaths.set(dest, src);
+ const text = ts.sys.readFile(src);
+ if (text !== undefined) movedOriginal.set(dest, text);
+ }
+}
+/** @type {(dest: string) => string} */
+const oldPathOf = (dest) => oldPaths.get(dest) ?? dest;
+
+/** @type {(text: string, changes: readonly TextChange[]) => string} */
+function applyChanges(text, changes) {
+ let out = text;
+ for (const c of [...changes].sort((a, b) => b.span.start - a.span.start))
+ out =
+ out.slice(0, c.span.start) +
+ c.newText +
+ out.slice(c.span.start + c.span.length);
+ return out;
+}
+
+/** @type {(p: string) => boolean} */
+const isSource = (p) =>
+ p.startsWith(`${webRoot}/`) &&
+ !p.startsWith(`${webRoot}/.next`) &&
+ !p.includes("/node_modules/") &&
+ SOURCE_EXT.some((e) => p.endsWith(e));
+
+/** @type {Set<string>} abs paths of importers we rewrote */
+const touched = new Set();
+for (const m of pending) {
+ const oldAbs = norm(abs(m.from));
+ const newAbs = norm(abs(m.to));
+ for (const fc of ls.getEditsForFileRename(
+ oldAbs,
+ newAbs,
+ formatOptions,
+ preferences,
+ )) {
+ const target = norm(fc.fileName);
+ const before = currentText(target);
+ if (before === undefined || !isSource(target)) continue;
+ setText(target, applyChanges(before, fc.textChanges));
+ // a moved file already relocated by an earlier move in this batch is named
+ // by its new path here β€” either way it is not an importer
+ if (!fileRenames.has(target) && !movedOriginal.has(target))
+ touched.add(target);
+ }
+ // reflect the new layout so the next move in the batch sees it
+ for (const [src, dest] of fileRenames)
+ if (src === oldAbs || src.startsWith(`${oldAbs}/`)) {
+ const text = currentText(src);
+ if (text !== undefined) setText(dest, text);
+ dropFile(src);
+ if (scriptFileNames.delete(src)) scriptFileNames.add(dest);
+ }
+}
+
+// --- postcondition: move β‰  edit ---------------------------------------------
+// A moved file may only change where the subtree it moved with is spelled
+// differently from its new home (self-references written as `@/src/...`).
+// Anything else β€” a link to something left behind, a non-specifier edit β€” is
+// the compiler happily doing the wrong thing for us, so it aborts.
+/** @type {(fileName: string, text: string) => { specifiers: string[], blanked: string }} */
+function moduleSpecifiers(fileName, text) {
+ const sf = ts.createSourceFile(
+ fileName,
+ text,
+ ts.ScriptTarget.Latest,
+ true,
+ fileName.endsWith("x") ? ts.ScriptKind.TSX : ts.ScriptKind.TS,
+ );
+ /** @type {import("typescript").StringLiteralLike[]} */
+ const nodes = [];
+ /** @type {(n: import("typescript").Node) => void} */
+ const visit = (n) => {
+ if (
+ (ts.isImportDeclaration(n) || ts.isExportDeclaration(n)) &&
+ n.moduleSpecifier &&
+ ts.isStringLiteralLike(n.moduleSpecifier)
+ )
+ nodes.push(n.moduleSpecifier);
+ else if (
+ ts.isImportTypeNode(n) &&
+ ts.isLiteralTypeNode(n.argument) &&
+ ts.isStringLiteralLike(n.argument.literal)
+ )
+ nodes.push(n.argument.literal);
+ else if (
+ ts.isCallExpression(n) &&
+ (n.expression.kind === ts.SyntaxKind.ImportKeyword ||
+ (ts.isIdentifier(n.expression) && n.expression.text === "require")) &&
+ n.arguments[0] &&
+ ts.isStringLiteralLike(n.arguments[0])
+ )
+ nodes.push(n.arguments[0]);
+ ts.forEachChild(n, visit);
+ };
+ visit(sf);
+ nodes.sort((a, b) => a.getStart(sf) - b.getStart(sf));
+ let blanked = "";
+ let cursor = 0;
+ for (const node of nodes) {
+ blanked += text.slice(cursor, node.getStart(sf)) + "\0";
+ cursor = node.getEnd();
+ }
+ return {
+ specifiers: nodes.map((n) => n.text),
+ blanked: blanked + text.slice(cursor),
+ };
+}
+
+/** @type {import("typescript").ModuleResolutionHost} */
+const overlayResolutionHost = {
+ fileExists: (f) => lsHost.fileExists(f),
+ readFile: (f) => lsHost.readFile(f),
+ directoryExists: (d) =>
+ lsHost.directoryExists
+ ? lsHost.directoryExists(d)
+ : ts.sys.directoryExists(d),
+ getCurrentDirectory: () => webRoot,
+ getDirectories: (d) =>
+ lsHost.getDirectories ? lsHost.getDirectories(d) : [],
+ realpath: ts.sys.realpath,
+ useCaseSensitiveFileNames: true,
+};
+const destinations = new Set(fileRenames.values());
+
+/** @type {Map<string, string[]>} moved file -> "<old> β†’ <new>" self-reference rewrites */
+const followed = new Map();
+/** @type {Map<string, string[]>} moved file -> rewrites that point outside the move */
+const stranded = new Map();
+for (const [dest, original] of movedOriginal) {
+ const now = currentText(dest) ?? "";
+ if (now === original) continue;
+ const before = moduleSpecifiers(dest, original);
+ const after = moduleSpecifiers(dest, now);
+ if (
+ before.blanked !== after.blanked ||
+ before.specifiers.length !== after.specifiers.length
+ ) {
+ stranded.set(dest, ["(edit outside a module specifier)"]);
+ continue;
+ }
+ for (const [i, spec] of after.specifiers.entries()) {
+ if (spec === before.specifiers[i]) continue;
+ const resolved = ts.resolveModuleName(
+ spec,
+ dest,
+ parsed.options,
+ overlayResolutionHost,
+ ).resolvedModule?.resolvedFileName;
+ const bucket =
+ resolved && destinations.has(norm(resolved)) ? followed : stranded;
+ bucket.set(dest, [
+ ...(bucket.get(dest) ?? []),
+ `${before.specifiers[i]} β†’ ${spec}`,
+ ]);
+ }
+}
+
+if (stranded.size) {
+ console.error(
+ red(
+ "aborted: this move would edit the moved files (RFC rule 15: move β‰  edit).",
+ ),
+ );
+ for (const [dest, rewrites] of stranded) {
+ console.error(`\n ${relative(webRoot, oldPathOf(dest))}`);
+ for (const r of rewrites) console.error(` ${r}`);
+ }
+ console.error(
+ "\nThose point at something left behind. Move it too, or do this one by hand.",
+ );
+ process.exit(1);
+}
+// --- report ------------------------------------------------------------------
+const importerFiles = [...touched].sort();
+console.log(
+ `${importerFiles.length} importer${importerFiles.length === 1 ? "" : "s"} to rewrite`,
+);
+for (const f of importerFiles) {
+ console.log(
+ dryRun ? ` ${relative(webRoot, f)}` : dim(` ${relative(webRoot, f)}`),
+ );
+ if (!dryRun) continue;
+ const was = moduleSpecifiers(f, ts.sys.readFile(f) ?? "").specifiers;
+ for (const [i, spec] of moduleSpecifiers(
+ f,
+ currentText(f) ?? "",
+ ).specifiers.entries())
+ if (was[i] !== spec)
+ console.log(` ${red(was[i] ?? "")} β†’ ${green(spec)}`);
+}
+if (followed.size) {
+ const n = [...followed.values()].reduce((a, r) => a + r.length, 0);
+ console.log(
+ `${followed.size} moved file${followed.size === 1 ? "" : "s"} respelling ${n} alias self-reference${n === 1 ? "" : "s"} into the subtree ${dim("(they encode the old path)")}`,
+ );
+ if (dryRun)
+ for (const [dest, rewrites] of followed) {
+ console.log(` ${relative(webRoot, oldPathOf(dest))}`);
+ for (const r of rewrites) console.log(dim(` ${r}`));
+ }
+}
+
+/** @type {(cmd: string, cmdArgs: string[]) => string} */
+const git = (cmd, cmdArgs) =>
+ execFileSync("git", [cmd, ...cmdArgs], { cwd: webRoot, encoding: "utf8" });
+
+// Rewriting a file that already has uncommitted work in it also runs prettier
+// over that work β€” worth saying out loud before it happens.
+if (importerFiles.length) {
+ const rels = importerFiles.map((f) => relative(webRoot, f));
+ const dirty = git("status", ["--porcelain", "--", ...rels])
+ .split("\n")
+ .filter(Boolean);
+ if (dirty.length) {
+ console.log(
+ red(
+ `${dirty.length} file${dirty.length === 1 ? "" : "s"} to rewrite already ha${dirty.length === 1 ? "s" : "ve"} uncommitted changes:`,
+ ),
+ );
+ for (const line of dirty) console.log(` ${line}`);
+ }
+}
+
+if (dryRun) {
+ console.log();
+ console.log(dim("dry run β€” nothing written. Drop --dry-run to apply."));
+ process.exit(0);
+}
+
+// --- apply -------------------------------------------------------------------
+// The way back out of any state this can reach is the inverse move, so print
+// that rather than a reset β€” including when the apply itself dies halfway.
+/** @type {(moves: Move[]) => void} */
+function printRevert(moves) {
+ if (!moves.length) return;
+ console.log();
+ console.log(dim("revert:"));
+ // a renamed file's inverse is a rename, not a move into its old directory β€”
+ // that would just report the destination as already occupied
+ /** @type {Map<string, string[]>} original parent dir -> new paths that came from it */
+ const byParent = new Map();
+ for (const m of moves) {
+ if (baseName(m.from) !== baseName(m.to)) {
+ console.log(dim(` pnpm structure:move ${m.to} ${m.from}`));
+ continue;
+ }
+ const parent = parentDir(m.from) || ".";
+ byParent.set(parent, [...(byParent.get(parent) ?? []), m.to]);
+ }
+ for (const [parent, tos] of byParent)
+ console.log(dim(` pnpm structure:move ${tos.join(" ")} ${parent}`));
+}
+
+/** @type {Move[]} */
+const appliedMoves = [];
+try {
+ for (const m of pending) {
+ mkdirSync(abs(parentDir(m.to)), { recursive: true });
+ try {
+ git("mv", isCaseOnly(m) ? ["-f", m.from, m.to] : [m.from, m.to]);
+ } catch (err) {
+ // git mv also refuses an untracked source; a plain rename is equivalent
+ // there, but only ever onto a destination that does not exist β€” rename(2)
+ // would overwrite one silently.
+ if (existsSync(abs(m.to)) && !isSelfCaseRename(m)) throw err;
+ renameSync(abs(m.from), abs(m.to));
+ git("add", ["--", m.to]);
+ }
+ appliedMoves.push(m);
+ }
+ // git does not track directories, so a move that empties one leaves it on
+ // disk. rmdir only ever succeeds on an empty one, so this cannot lose work.
+ for (const m of pending) {
+ let dir = parentDir(m.from);
+ while (dir.startsWith("src/") && dir !== "src") {
+ try {
+ rmdirSync(abs(dir));
+ } catch {
+ break;
+ }
+ dir = parentDir(dir);
+ }
+ }
+ const rewritten = [...new Set([...importerFiles, ...followed.keys()])];
+ for (const f of rewritten) writeFileSync(f, currentText(f) ?? "", "utf8");
+ if (rewritten.length)
+ // a specifier gets longer or shorter, so prettier may want to re-wrap the line
+ execFileSync(
+ "pnpm",
+ [
+ "exec",
+ "prettier",
+ "--write",
+ "--log-level",
+ "warn",
+ ...rewritten.map((f) => relative(webRoot, f)),
+ ],
+ { cwd: webRoot, stdio: "inherit" },
+ );
+ // A moved file's respelled self-reference IS staged: `git mv` already put its
+ // pre-edit bytes in the index, so leaving it out would stage a rename whose
+ // content imports a path this same move deleted. Importer rewrites stay
+ // unstaged β€” those files can carry unrelated work that is not ours to commit.
+ if (followed.size)
+ git("add", [
+ "--",
+ ...[...followed.keys()].map((f) => relative(webRoot, f)),
+ ]);
+} catch (err) {
+ console.error(
+ red(`\napply failed: ${err instanceof Error ? err.message : String(err)}`),
+ );
+ printRevert(appliedMoves);
+ process.exit(1);
+}
+console.log();
+console.log(
+ `${green("moved")} ${pending.length} path${pending.length === 1 ? "" : "s"} β†’ ${toDir}, rewrote ${importerFiles.length} importer${importerFiles.length === 1 ? "" : "s"}`,
+);
+
+// --- the blind spot + the way back -------------------------------------------
+// Modules named by string (vi.mock, worker URLs, route strings) are invisible
+// to the compiler, so tsc stays green while they dangle. We can't rewrite them
+// safely, but we can refuse to let them pass unseen.
+const HIT_LIMIT = 30;
+/** @type {string[]} */
+const dangling = [];
+for (const m of pending) {
+ const needle = m.isDir ? m.from : stemOf(m.from);
+ try {
+ const out = execFileSync(
+ "git",
+ [
+ "grep",
+ "-nF",
+ needle,
+ "--",
+ ":(exclude)web/.structure-baseline.json",
+ ":(exclude)*.lock",
+ ":(exclude)pnpm-lock.yaml",
+ ],
+ { cwd: resolve(webRoot, ".."), encoding: "utf8" },
+ );
+ dangling.push(...out.trimEnd().split("\n"));
+ } catch {
+ // git grep exits 1 with no matches
+ }
+}
+console.log();
+if (dangling.length) {
+ console.log(
+ red(
+ `${dangling.length} reference${dangling.length === 1 ? "" : "s"} to the old path survive as strings β€” tsc cannot see these:`,
+ ),
+ );
+ for (const line of dangling.slice(0, HIT_LIMIT)) console.log(` ${line}`);
+ if (dangling.length > HIT_LIMIT)
+ console.log(dim(` … ${dangling.length - HIT_LIMIT} more`));
+ console.log(dim("Fix them by hand (vi.mock paths, worker URLs, docs)."));
+} else {
+ console.log(dim("no string references to the old path survive."));
+}
+
+printRevert(pending);
+
+// --- verify ------------------------------------------------------------------
+if (!verify) process.exit(0);
+console.log();
+console.log(bold("tsc --noEmit"));
+try {
+ execFileSync("pnpm", ["run", "typecheck"], {
+ cwd: webRoot,
+ stdio: "inherit",
+ });
+} catch {
+ console.error(
+ red(
+ "\ntypecheck failed β€” the move is on disk; revert with the command above.",
+ ),
+ );
+ process.exit(1);
+}
+console.log(bold("\nstructure:stats --diff"));
+execFileSync("node", ["scripts/structure/stats.mjs", "--diff"], {
+ cwd: webRoot,
+ stdio: "inherit",
+});
diff --git a/web/scripts/structure/stats.mjs b/web/scripts/structure/stats.mjs
index 7929b82f24..5446beb054 100644
--- a/web/scripts/structure/stats.mjs
+++ b/web/scripts/structure/stats.mjs
@@ -129,7 +129,7 @@ const RULES = [
{
id: 8,
mech: "graph",
- title: "Cross-feature imports only via the feature's index.ts",
+ title: "Cross-feature imports only via a feature surface (index, server)",
get: () => d.rule8(modules),
},
{