@nifrajs/cli 5.12.1 → 5.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +72 -0
- package/README.md +3 -1
- package/dist/agent-files.js +0 -74
- package/dist/agent-files.js.map +1 -1
- package/dist/app-files.js +0 -19
- package/dist/app-files.js.map +1 -1
- package/dist/assure-hydration.js +0 -23
- package/dist/assure-hydration.js.map +1 -1
- package/dist/assure.js +0 -14
- package/dist/assure.js.map +1 -1
- package/dist/capabilities-tool.js +0 -51
- package/dist/capabilities-tool.js.map +1 -1
- package/dist/cdn-check.js +0 -2
- package/dist/cdn-check.js.map +1 -1
- package/dist/check-diagnostics.js +0 -29
- package/dist/check-diagnostics.js.map +1 -1
- package/dist/check-scan.js +10 -621
- package/dist/check-scan.js.map +1 -1
- package/dist/check.js +0 -86
- package/dist/check.js.map +1 -1
- package/dist/cli.d.ts +9 -2
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +139 -351
- package/dist/cli.js.map +1 -1
- package/dist/client-module.js +0 -13
- package/dist/client-module.js.map +1 -1
- package/dist/command-catalog.d.ts +29 -1
- package/dist/command-catalog.d.ts.map +1 -1
- package/dist/command-catalog.js +100 -23
- package/dist/command-catalog.js.map +1 -1
- package/dist/component-check.js +1 -7
- package/dist/component-check.js.map +1 -1
- package/dist/config-target.js +0 -13
- package/dist/config-target.js.map +1 -1
- package/dist/contract-proof.js +0 -1
- package/dist/contract-proof.js.map +1 -1
- package/dist/contracts.js +0 -36
- package/dist/contracts.js.map +1 -1
- package/dist/db-audit.js +0 -15
- package/dist/db-audit.js.map +1 -1
- package/dist/db-child.js +0 -20
- package/dist/db-child.js.map +1 -1
- package/dist/db-config.js +0 -23
- package/dist/db-config.js.map +1 -1
- package/dist/db-tool.js +0 -24
- package/dist/db-tool.js.map +1 -1
- package/dist/deployment-adapters.d.ts +12 -0
- package/dist/deployment-adapters.d.ts.map +1 -0
- package/dist/deployment-adapters.js +86 -0
- package/dist/deployment-adapters.js.map +1 -0
- package/dist/dev-bun-config.d.ts +4 -3
- package/dist/dev-bun-config.d.ts.map +1 -1
- package/dist/dev-bun-config.js +9 -101
- package/dist/dev-bun-config.js.map +1 -1
- package/dist/dev-feed-tool.js +0 -32
- package/dist/dev-feed-tool.js.map +1 -1
- package/dist/dev-server-client.js +0 -16
- package/dist/dev-server-client.js.map +1 -1
- package/dist/diagnostics.js +0 -10
- package/dist/diagnostics.js.map +1 -1
- package/dist/diff-tool.js +0 -23
- package/dist/diff-tool.js.map +1 -1
- package/dist/docs-search.js +0 -35
- package/dist/docs-search.js.map +1 -1
- package/dist/doctor.js +3 -161
- package/dist/doctor.js.map +1 -1
- package/dist/env-file.js +0 -66
- package/dist/env-file.js.map +1 -1
- package/dist/examples.js +0 -16
- package/dist/examples.js.map +1 -1
- package/dist/fix-recipes.js +0 -20
- package/dist/fix-recipes.js.map +1 -1
- package/dist/frontend-guidance.js +0 -36
- package/dist/frontend-guidance.js.map +1 -1
- package/dist/hand-built-manifest.js +0 -17
- package/dist/hand-built-manifest.js.map +1 -1
- package/dist/hooks-report.js +0 -4
- package/dist/hooks-report.js.map +1 -1
- package/dist/i18n-check.js +0 -30
- package/dist/i18n-check.js.map +1 -1
- package/dist/init-agents.d.ts +5 -0
- package/dist/init-agents.d.ts.map +1 -1
- package/dist/init-agents.js +2 -134
- package/dist/init-agents.js.map +1 -1
- package/dist/internal/code-unit-order.js +0 -2
- package/dist/internal/code-unit-order.js.map +1 -1
- package/dist/internal/config-paths.js +0 -12
- package/dist/internal/config-paths.js.map +1 -1
- package/dist/internal/control-character.js +0 -1
- package/dist/internal/control-character.js.map +1 -1
- package/dist/internal/escape-regexp.d.ts +1 -2
- package/dist/internal/escape-regexp.d.ts.map +1 -1
- package/dist/internal/escape-regexp.js +1 -4
- package/dist/internal/escape-regexp.js.map +1 -1
- package/dist/internal/git-ignored.js +0 -11
- package/dist/internal/git-ignored.js.map +1 -1
- package/dist/internal/installed-package.js +0 -2
- package/dist/internal/installed-package.js.map +1 -1
- package/dist/internal/line-at.js +0 -1
- package/dist/internal/line-at.js.map +1 -1
- package/dist/internal/nifra-project.js +0 -31
- package/dist/internal/nifra-project.js.map +1 -1
- package/dist/internal/posix-path.js +0 -1
- package/dist/internal/posix-path.js.map +1 -1
- package/dist/internal/project-specifiers.js +0 -34
- package/dist/internal/project-specifiers.js.map +1 -1
- package/dist/internal/realpath-or-self.js +0 -1
- package/dist/internal/realpath-or-self.js.map +1 -1
- package/dist/internal/record.d.ts +1 -1
- package/dist/internal/record.d.ts.map +1 -1
- package/dist/internal/record.js +1 -4
- package/dist/internal/record.js.map +1 -1
- package/dist/internal/reserved-segment-codemod.js +0 -44
- package/dist/internal/reserved-segment-codemod.js.map +1 -1
- package/dist/internal/script-kind.js +0 -5
- package/dist/internal/script-kind.js.map +1 -1
- package/dist/internal/source-facts.js +3 -37
- package/dist/internal/source-facts.js.map +1 -1
- package/dist/internal/typescript-import.js +0 -49
- package/dist/internal/typescript-import.js.map +1 -1
- package/dist/internal/workspace-packages.js +0 -24
- package/dist/internal/workspace-packages.js.map +1 -1
- package/dist/introspect.js +1 -89
- package/dist/introspect.js.map +1 -1
- package/dist/learn.js +0 -16
- package/dist/learn.js.map +1 -1
- package/dist/levels-tool.js +0 -34
- package/dist/levels-tool.js.map +1 -1
- package/dist/load.d.ts +12 -3
- package/dist/load.d.ts.map +1 -1
- package/dist/load.js +19 -75
- package/dist/load.js.map +1 -1
- package/dist/local-cli.js +0 -23
- package/dist/local-cli.js.map +1 -1
- package/dist/lock-links.js +0 -20
- package/dist/lock-links.js.map +1 -1
- package/dist/manifest-tool.js +0 -5
- package/dist/manifest-tool.js.map +1 -1
- package/dist/mcp-app-tool.js +0 -9
- package/dist/mcp-app-tool.js.map +1 -1
- package/dist/mcp-context.js +0 -30
- package/dist/mcp-context.js.map +1 -1
- package/dist/mcp-delegate.js +0 -45
- package/dist/mcp-delegate.js.map +1 -1
- package/dist/mcp-docs-tools.js +0 -13
- package/dist/mcp-docs-tools.js.map +1 -1
- package/dist/mcp-exec.js +0 -71
- package/dist/mcp-exec.js.map +1 -1
- package/dist/mcp-http.js +1 -38
- package/dist/mcp-http.js.map +1 -1
- package/dist/mcp-io.d.ts +5 -1
- package/dist/mcp-io.d.ts.map +1 -1
- package/dist/mcp-io.js +3 -68
- package/dist/mcp-io.js.map +1 -1
- package/dist/mcp-isolate.js +0 -28
- package/dist/mcp-isolate.js.map +1 -1
- package/dist/mcp-path.js +0 -8
- package/dist/mcp-path.js.map +1 -1
- package/dist/mcp-project-child.js +0 -19
- package/dist/mcp-project-child.js.map +1 -1
- package/dist/mcp-protocol.js +0 -5
- package/dist/mcp-protocol.js.map +1 -1
- package/dist/mcp-reflect.js +0 -11
- package/dist/mcp-reflect.js.map +1 -1
- package/dist/mcp-relay.d.ts +4 -0
- package/dist/mcp-relay.d.ts.map +1 -1
- package/dist/mcp-relay.js +9 -20
- package/dist/mcp-relay.js.map +1 -1
- package/dist/mcp-render.js +1 -42
- package/dist/mcp-render.js.map +1 -1
- package/dist/mcp-root.d.ts.map +1 -1
- package/dist/mcp-root.js +18 -90
- package/dist/mcp-root.js.map +1 -1
- package/dist/mcp-run.js +1 -38
- package/dist/mcp-run.js.map +1 -1
- package/dist/mcp-tool-meta.js +3 -8
- package/dist/mcp-tool-meta.js.map +1 -1
- package/dist/mcp-ws.js +0 -16
- package/dist/mcp-ws.js.map +1 -1
- package/dist/mcp.js +0 -86
- package/dist/mcp.js.map +1 -1
- package/dist/migrate-layout.js +0 -313
- package/dist/migrate-layout.js.map +1 -1
- package/dist/openapi-tool.js +0 -18
- package/dist/openapi-tool.js.map +1 -1
- package/dist/openapi-types.js +0 -22
- package/dist/openapi-types.js.map +1 -1
- package/dist/output-schemas.js +0 -67
- package/dist/output-schemas.js.map +1 -1
- package/dist/pipeline-guard.js +0 -58
- package/dist/pipeline-guard.js.map +1 -1
- package/dist/pipeline-report.js +0 -71
- package/dist/pipeline-report.js.map +1 -1
- package/dist/port.d.ts +6 -3
- package/dist/port.d.ts.map +1 -1
- package/dist/port.js +16 -92
- package/dist/port.js.map +1 -1
- package/dist/ppr-read-fix.js +0 -29
- package/dist/ppr-read-fix.js.map +1 -1
- package/dist/project-facts.js +0 -5
- package/dist/project-facts.js.map +1 -1
- package/dist/project-path.js +0 -7
- package/dist/project-path.js.map +1 -1
- package/dist/project-site.js +0 -2
- package/dist/project-site.js.map +1 -1
- package/dist/recipes/1.8.0.js +0 -9
- package/dist/recipes/1.8.0.js.map +1 -1
- package/dist/recipes/2.0.0.js +0 -6
- package/dist/recipes/2.0.0.js.map +1 -1
- package/dist/recipes/3.0.0.js +0 -7
- package/dist/recipes/3.0.0.js.map +1 -1
- package/dist/recipes/4.0.0.js +0 -6
- package/dist/recipes/4.0.0.js.map +1 -1
- package/dist/recipes/5.0.0.js +0 -8
- package/dist/recipes/5.0.0.js.map +1 -1
- package/dist/recipes/5.1.0.js +0 -6
- package/dist/recipes/5.1.0.js.map +1 -1
- package/dist/recipes/5.10.0.js +0 -10
- package/dist/recipes/5.10.0.js.map +1 -1
- package/dist/recipes/5.11.0.js +0 -7
- package/dist/recipes/5.11.0.js.map +1 -1
- package/dist/recipes/5.12.0.js +0 -6
- package/dist/recipes/5.12.0.js.map +1 -1
- package/dist/recipes/5.13.0.d.ts +9 -0
- package/dist/recipes/5.13.0.d.ts.map +1 -0
- package/dist/recipes/5.13.0.js +34 -0
- package/dist/recipes/5.13.0.js.map +1 -0
- package/dist/recipes/5.14.0.d.ts +10 -0
- package/dist/recipes/5.14.0.d.ts.map +1 -0
- package/dist/recipes/5.14.0.js +26 -0
- package/dist/recipes/5.14.0.js.map +1 -0
- package/dist/recipes/5.2.0.js +0 -7
- package/dist/recipes/5.2.0.js.map +1 -1
- package/dist/recipes/5.3.0.js +0 -8
- package/dist/recipes/5.3.0.js.map +1 -1
- package/dist/recipes/5.4.0.js +0 -8
- package/dist/recipes/5.4.0.js.map +1 -1
- package/dist/recipes/5.5.0.js +0 -6
- package/dist/recipes/5.5.0.js.map +1 -1
- package/dist/recipes/5.6.0.js +0 -7
- package/dist/recipes/5.6.0.js.map +1 -1
- package/dist/recipes/5.7.0.js +0 -9
- package/dist/recipes/5.7.0.js.map +1 -1
- package/dist/recipes/5.8.0.js +0 -6
- package/dist/recipes/5.8.0.js.map +1 -1
- package/dist/recipes/5.9.0.js +0 -9
- package/dist/recipes/5.9.0.js.map +1 -1
- package/dist/recipes/index.d.ts.map +1 -1
- package/dist/recipes/index.js +4 -9
- package/dist/recipes/index.js.map +1 -1
- package/dist/release-verification.js +0 -14
- package/dist/release-verification.js.map +1 -1
- package/dist/replay.js +0 -3
- package/dist/replay.js.map +1 -1
- package/dist/review.js +0 -21
- package/dist/review.js.map +1 -1
- package/dist/route-types.js +0 -17
- package/dist/route-types.js.map +1 -1
- package/dist/rule-explain.js +0 -5
- package/dist/rule-explain.js.map +1 -1
- package/dist/rules/codes.js +0 -2
- package/dist/rules/codes.js.map +1 -1
- package/dist/rules/comment-markers.js +0 -25
- package/dist/rules/comment-markers.js.map +1 -1
- package/dist/rules/contract-visibility.js +0 -27
- package/dist/rules/contract-visibility.js.map +1 -1
- package/dist/rules/data-guard.js +1 -52
- package/dist/rules/data-guard.js.map +1 -1
- package/dist/rules/internal-links.js +0 -36
- package/dist/rules/internal-links.js.map +1 -1
- package/dist/rules/islands.js +2 -65
- package/dist/rules/islands.js.map +1 -1
- package/dist/rules/layout.js +0 -1
- package/dist/rules/layout.js.map +1 -1
- package/dist/rules/legacy.js +0 -31
- package/dist/rules/legacy.js.map +1 -1
- package/dist/rules/merge-auth.js +0 -9
- package/dist/rules/merge-auth.js.map +1 -1
- package/dist/rules/pages.js +0 -95
- package/dist/rules/pages.js.map +1 -1
- package/dist/rules/ppr.js +0 -8
- package/dist/rules/ppr.js.map +1 -1
- package/dist/rules/route-statement.js +0 -13
- package/dist/rules/route-statement.js.map +1 -1
- package/dist/rules/routes.js +1 -67
- package/dist/rules/routes.js.map +1 -1
- package/dist/rules/secrets.js +0 -15
- package/dist/rules/secrets.js.map +1 -1
- package/dist/rules/security.js +0 -113
- package/dist/rules/security.js.map +1 -1
- package/dist/rules/server-fn-namespace.js +0 -5
- package/dist/rules/server-fn-namespace.js.map +1 -1
- package/dist/rules/source-scan.js +0 -5
- package/dist/rules/source-scan.js.map +1 -1
- package/dist/rules/swap.js +0 -21
- package/dist/rules/swap.js.map +1 -1
- package/dist/rules/text-slot-boolean.js +0 -5
- package/dist/rules/text-slot-boolean.js.map +1 -1
- package/dist/rules/typescript.js +0 -5
- package/dist/rules/typescript.js.map +1 -1
- package/dist/rules/undeclared-response.js +0 -8
- package/dist/rules/undeclared-response.js.map +1 -1
- package/dist/rules/zones.js +0 -45
- package/dist/rules/zones.js.map +1 -1
- package/dist/runtime-differences.js +0 -20
- package/dist/runtime-differences.js.map +1 -1
- package/dist/scaffold-hole.js +1 -45
- package/dist/scaffold-hole.js.map +1 -1
- package/dist/scaffold.js +3 -60
- package/dist/scaffold.js.map +1 -1
- package/dist/sdk.js +1 -13
- package/dist/sdk.js.map +1 -1
- package/dist/search-terms.js +0 -12
- package/dist/search-terms.js.map +1 -1
- package/dist/shell-files.d.ts +44 -0
- package/dist/shell-files.d.ts.map +1 -0
- package/dist/shell-files.js +186 -0
- package/dist/shell-files.js.map +1 -0
- package/dist/shell-init.d.ts +33 -0
- package/dist/shell-init.d.ts.map +1 -0
- package/dist/shell-init.js +84 -0
- package/dist/shell-init.js.map +1 -0
- package/dist/smoke.js +0 -4
- package/dist/smoke.js.map +1 -1
- package/dist/source-rewrites.js +0 -43
- package/dist/source-rewrites.js.map +1 -1
- package/dist/stylex-migrate.js +0 -29
- package/dist/stylex-migrate.js.map +1 -1
- package/dist/symptom-search.js +0 -3
- package/dist/symptom-search.js.map +1 -1
- package/dist/sync-manifest.js +3 -33
- package/dist/sync-manifest.js.map +1 -1
- package/dist/test-tool.js +0 -10
- package/dist/test-tool.js.map +1 -1
- package/dist/typecheck-coverage.js +0 -36
- package/dist/typecheck-coverage.js.map +1 -1
- package/dist/types-search.js +0 -35
- package/dist/types-search.js.map +1 -1
- package/dist/upgrade.js +0 -214
- package/dist/upgrade.js.map +1 -1
- package/dist/verification-plan.js +0 -19
- package/dist/verification-plan.js.map +1 -1
- package/dist/verification.js +0 -23
- package/dist/verification.js.map +1 -1
- package/dist/version-drift.js +0 -8
- package/dist/version-drift.js.map +1 -1
- package/dist/web-app-options.js +0 -16
- package/dist/web-app-options.js.map +1 -1
- package/dist/work-graph.js +0 -21
- package/dist/work-graph.js.map +1 -1
- package/dist/workspace-link.js +0 -31
- package/dist/workspace-link.js.map +1 -1
- package/docs/examples.json +97 -1
- package/docs/llms-full.txt +1347 -59
- package/docs/types.json +756 -37
- package/package.json +12 -11
- package/src/assure-hydration.ts +3 -3
- package/src/cli.ts +189 -39
- package/src/command-catalog.ts +144 -7
- package/src/deployment-adapters.ts +107 -0
- package/src/dev-bun-config.ts +21 -2
- package/src/init-agents.ts +1 -1
- package/src/internal/escape-regexp.ts +1 -4
- package/src/internal/record.ts +1 -3
- package/src/internal/typescript-import.ts +1 -1
- package/src/load.ts +36 -3
- package/src/mcp-http.ts +1 -1
- package/src/mcp-io.ts +8 -5
- package/src/mcp-relay.ts +19 -7
- package/src/mcp-render.ts +1 -1
- package/src/mcp-root.ts +21 -3
- package/src/mcp-tool-meta.ts +3 -3
- package/src/port.ts +25 -8
- package/src/recipes/5.13.0.ts +41 -0
- package/src/recipes/5.14.0.ts +34 -0
- package/src/recipes/index.ts +4 -0
- package/src/rules/routes.ts +2 -2
- package/src/shell-files.ts +225 -0
- package/src/shell-init.ts +139 -0
package/dist/migrate-layout.js
CHANGED
|
@@ -1,30 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `nifra migrate layout` - move an app onto the frontend/backend split.
|
|
3
|
-
*
|
|
4
|
-
* What it does, in order:
|
|
5
|
-
* 1. splits every route file: the exports only the server reads (`loader`, `action`, ...) and the code
|
|
6
|
-
* only they use move to the route's `x.backend.ts`; the frontend file keeps the rest, plus an
|
|
7
|
-
* `import type` for any moved name it still mentions in a type;
|
|
8
|
-
* 2. folds each `_middleware.ts` into its directory's `_layout.backend.ts` as `middleware`;
|
|
9
|
-
* 3. moves `backend.ts` to `backend/app.ts` and `framework.ts` to `backend/framework.ts`;
|
|
10
|
-
* 4. moves each `x.server.ts` under `backend/` without the retired suffix, and zones every other module
|
|
11
|
-
* by who imports it at runtime: only route frontends and the `clientModule` -> `frontend/`, only
|
|
12
|
-
* the server side -> `backend/`, both -> `shared/`. Imports through tsconfig `paths`/`baseUrl` and
|
|
13
|
-
* package.json `imports` count as the bundler reads them;
|
|
14
|
-
* 5. rewrites every import in the app to the new locations (test-runner mocks included), an alias
|
|
15
|
-
* through the same alias when one of its targets covers the new place and as a relative path
|
|
16
|
-
* otherwise, each checked to resolve to its target after the moves; the config's `clientModule`
|
|
17
|
-
* and `componentPipeline.entries` paths; `@nifrajs/web/server-only` to
|
|
18
|
-
* `@nifrajs/web/backend-only`; and `ServerOnly` to `BackendOnly`;
|
|
19
|
-
* 6. adds the zone folders moved TypeScript lands in to tsconfig `include`, and `.nifra/` to
|
|
20
|
-
* `.gitignore`.
|
|
21
|
-
*
|
|
22
|
-
* Data files (JSON, YAML, CSV, ...) and folders the app zones in package.json stay where they are.
|
|
23
|
-
*
|
|
24
|
-
* It is a dry run unless `write` is set, and it reports what it could not decide (a frontend that uses
|
|
25
|
-
* a moved name at runtime, a helper both halves need, a loader with no output schema, an import no
|
|
26
|
-
* specifier reaches after the moves, a specifier that names nothing) instead of guessing.
|
|
27
|
-
*/
|
|
28
1
|
import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmdirSync, rmSync, statSync, writeFileSync, } from "node:fs";
|
|
29
2
|
import { builtinModules } from "node:module";
|
|
30
3
|
import { dirname, extname, isAbsolute, join, posix, relative, resolve, sep } from "node:path";
|
|
@@ -42,7 +15,6 @@ import { importTypeScript } from "./internal/typescript-import.js";
|
|
|
42
15
|
import { planOutputSchemas } from "./output-schemas.js";
|
|
43
16
|
import { missingOutputSchemas } from "./rules/data-guard.js";
|
|
44
17
|
import { apiPrefixFix } from "./rules/pages.js";
|
|
45
|
-
/** Exports only the server or the build reads: `@nifrajs/web`'s route-pair contract. */
|
|
46
18
|
const BACKEND_EXPORTS = BACKEND_ROUTE_EXPORTS;
|
|
47
19
|
const ROUTE_FILE = /\.(?:tsx|jsx|svelte|vue|mdx)$/;
|
|
48
20
|
const SOURCE_FILE = /\.(?:[cm]?[jt]sx?|svelte|vue|mdx)$/;
|
|
@@ -63,16 +35,12 @@ const LEGACY_SERVER = /\.server(\.(?:[cm]?[jt]sx?|svelte|vue|mdx))$/;
|
|
|
63
35
|
const RETIRED_SPECIFIERS = {
|
|
64
36
|
"@nifrajs/web/server-only": "@nifrajs/web/backend-only",
|
|
65
37
|
};
|
|
66
|
-
/** Scripts and config that name app files by path: scanned for paths that moved. Only the app's own
|
|
67
|
-
* package.json is rewritten, in the fields `rewritePackagePaths` reads. */
|
|
68
38
|
const TOOLING_FILE = /(?:\.(?:json|jsonc|ya?ml|toml|sh|bash|zsh|ps1)|(?:^|\/)(?:Dockerfile|Makefile|Procfile))$/;
|
|
69
39
|
const SELF_LOCATION = /\bimport\.meta\.(?:dirname|dir|filename|url)\b|\b__(?:dirname|filename)\b/;
|
|
70
40
|
const SKIP_DIRS = new Set(["node_modules", "dist", "build", "coverage", ".git", ".vite", "public"]);
|
|
71
41
|
const ZONE_DIRS = new Set(["frontend", "backend", "shared", "routes", "public"]);
|
|
72
42
|
const FOLDER_SIDES = ["frontend", "backend", "shared"];
|
|
73
|
-
/** Data other tools may read by path too; reported, never moved. Images and fonts still move. */
|
|
74
43
|
const DATA_FILE = /\.(?:json|jsonc|json5|ndjson|ya?ml|toml|csv|tsv|txt|xml|sql)$/;
|
|
75
|
-
/** Test-runner calls that name a module by path, as an import does. */
|
|
76
44
|
const MOCK_CALLS = new Set([
|
|
77
45
|
"mock.module",
|
|
78
46
|
"vi.mock",
|
|
@@ -86,7 +54,6 @@ const MOCK_CALLS = new Set([
|
|
|
86
54
|
"jest.unstable_mockModule",
|
|
87
55
|
]);
|
|
88
56
|
const TAILWIND_CONFIG = /^tailwind\.config\.[cm]?[jt]s$/;
|
|
89
|
-
/** What a `x.test.ts` beside `x.<ext>` tests. */
|
|
90
57
|
const SUBJECT_EXTENSIONS = [
|
|
91
58
|
".ts",
|
|
92
59
|
".tsx",
|
|
@@ -99,15 +66,12 @@ const SUBJECT_EXTENSIONS = [
|
|
|
99
66
|
".svelte",
|
|
100
67
|
".vue",
|
|
101
68
|
];
|
|
102
|
-
/** A specifier without its `?query` or `#hash`; a leading `#` names a package.json import. Cut by
|
|
103
|
-
* index, since `/[?#].*$/` retries from every `#` and is quadratic on a long run of them. */
|
|
104
69
|
const withoutQuery = (specifier) => {
|
|
105
70
|
const query = specifier.indexOf("?", 1);
|
|
106
71
|
const hash = specifier.indexOf("#", 1);
|
|
107
72
|
const cut = query === -1 ? hash : hash === -1 ? query : Math.min(query, hash);
|
|
108
73
|
return cut === -1 ? specifier : specifier.slice(0, cut);
|
|
109
74
|
};
|
|
110
|
-
/** Every first-party file under `root`, app-relative and posix. */
|
|
111
75
|
function listFiles(root) {
|
|
112
76
|
const out = [];
|
|
113
77
|
const walk = (dir) => {
|
|
@@ -133,11 +97,6 @@ function bindingNames(ts, name, into) {
|
|
|
133
97
|
bindingNames(ts, element.name, into);
|
|
134
98
|
}
|
|
135
99
|
const hasModifier = (ts, node, kind) => (ts.canHaveModifiers(node) ? (ts.getModifiers(node) ?? []) : []).some((m) => m.kind === kind);
|
|
136
|
-
/**
|
|
137
|
-
* The bindings classic JSX compiles to calls of, which no identifier in the source names: the roots
|
|
138
|
-
* of the `@jsx`/`@jsxFrag` pragmas, else TypeScript's default `React`. A file that names its own
|
|
139
|
-
* `@jsxImportSource` imports the runtime itself.
|
|
140
|
-
*/
|
|
141
100
|
function jsxBindings(ts, source) {
|
|
142
101
|
const comments = (ts.getLeadingCommentRanges(source, 0) ?? [])
|
|
143
102
|
.map(({ pos, end }) => source.slice(pos, end))
|
|
@@ -160,7 +119,6 @@ function references(ts, node, jsx) {
|
|
|
160
119
|
value.add(name);
|
|
161
120
|
const typed = inType ||
|
|
162
121
|
ts.isTypeNode(current) ||
|
|
163
|
-
// nifra-expect NF-S002: `token` is the heritage clause's syntax kind, not a credential.
|
|
164
122
|
(ts.isHeritageClause(current) && current.token === ts.SyntaxKind.ImplementsKeyword);
|
|
165
123
|
if (ts.isIdentifier(current)) {
|
|
166
124
|
const parent = current.parent;
|
|
@@ -264,11 +222,6 @@ function describeStatements(ts, sf) {
|
|
|
264
222
|
};
|
|
265
223
|
});
|
|
266
224
|
}
|
|
267
|
-
/**
|
|
268
|
-
* Rebuild an import keeping only the bindings `uses` names; `undefined` when none remain. Bindings
|
|
269
|
-
* read only in types become `import type`, so no bundler loads the module for them: always in a
|
|
270
|
-
* `fresh` module, and in the page only where the split narrowed the import.
|
|
271
|
-
*/
|
|
272
225
|
function filterImport(ts, sf, node, uses, fresh) {
|
|
273
226
|
const clause = node.importClause;
|
|
274
227
|
const spec = node.moduleSpecifier.getText(sf);
|
|
@@ -306,16 +259,13 @@ function filterImport(ts, sf, node, uses, fresh) {
|
|
|
306
259
|
node.attributes === undefined &&
|
|
307
260
|
(fresh || kept.length < total) &&
|
|
308
261
|
kept.every((part) => part.typeOnly)) {
|
|
309
|
-
// `import type` takes one default, namespace or list, so each goes in its own statement.
|
|
310
262
|
const bodies = [...heads.map((part) => part.bare), ...list(named, (part) => part.bare)];
|
|
311
263
|
return `${leading}${bodies.map((body) => statement(true, body)).join("\n")}`;
|
|
312
264
|
}
|
|
313
265
|
const body = [...heads.map((part) => part.text), ...list(named, (part) => part.text)];
|
|
314
266
|
return `${leading}${statement(clause.isTypeOnly, body.join(", "))}`;
|
|
315
267
|
}
|
|
316
|
-
/** The `;` a statement was written with, so a rebuilt one keeps the file's style. */
|
|
317
268
|
const terminatorOf = (sf, node) => node.getText(sf).endsWith(";") ? ";" : "";
|
|
318
|
-
/** Rebuild `export { a, b as c }` keeping only the exported names `keep` accepts. */
|
|
319
269
|
function filterExportList(ts, sf, node, keep) {
|
|
320
270
|
const clause = node.exportClause;
|
|
321
271
|
if (!clause || !ts.isNamedExports(clause))
|
|
@@ -332,11 +282,6 @@ const joinStatements = (texts) => {
|
|
|
332
282
|
return body.endsWith("\n") ? body : `${body}\n`;
|
|
333
283
|
};
|
|
334
284
|
const PURE_OBJECT_CALLS = new Set(["keys", "values", "entries", "freeze", "fromEntries"]);
|
|
335
|
-
/**
|
|
336
|
-
* Whether `node` builds constant data: literals, arrays and objects of it, operators over it,
|
|
337
|
-
* `Object.keys`-style reads, and the file's own top-level names, which go into `reads`. `undefined`
|
|
338
|
-
* when it is data; else the import it reads, or `""` for anything else (a call, a function, a global).
|
|
339
|
-
*/
|
|
340
285
|
function dataBlocker(ts, node, isLocal, reads) {
|
|
341
286
|
const data = (n) => {
|
|
342
287
|
if (ts.isStringLiteral(n) ||
|
|
@@ -428,10 +373,6 @@ function dataBlocker(ts, node, isLocal, reads) {
|
|
|
428
373
|
};
|
|
429
374
|
return data(node);
|
|
430
375
|
}
|
|
431
|
-
/**
|
|
432
|
-
* Split one route module's source into its frontend and backend halves. `backendSpecifier` is how the
|
|
433
|
-
* frontend refers to the backend half in an `import type` (`./index.backend.ts`).
|
|
434
|
-
*/
|
|
435
376
|
export function splitRouteSource(ts, source, fileName, backendSpecifier, options = {}) {
|
|
436
377
|
const kind = fileName.endsWith("x") ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
|
|
437
378
|
const sf = ts.createSourceFile(fileName, source, ts.ScriptTarget.Latest, true, kind);
|
|
@@ -450,7 +391,6 @@ export function splitRouteSource(ts, source, fileName, backendSpecifier, options
|
|
|
450
391
|
into.set(name, info);
|
|
451
392
|
}
|
|
452
393
|
const isTypeDeclaration = (info) => ts.isInterfaceDeclaration(info.node) || ts.isTypeAliasDeclaration(info.node);
|
|
453
|
-
/** What `info` reads for one half: an export list reads only the locals it exports there. */
|
|
454
394
|
const readsFor = (info, backend) => {
|
|
455
395
|
if (info.listed.length === 0)
|
|
456
396
|
return { value: info.valueRefs, type: info.typeRefs };
|
|
@@ -466,13 +406,11 @@ export function splitRouteSource(ts, source, fileName, backendSpecifier, options
|
|
|
466
406
|
if (names.some((name) => BACKEND_EXPORTS.has(name)))
|
|
467
407
|
movedLocals.add(local);
|
|
468
408
|
}
|
|
469
|
-
/** Each half's statements. What `shared` holds, both import, so neither half keeps a copy. */
|
|
470
409
|
const partition = (shared) => {
|
|
471
410
|
const local = (name) => {
|
|
472
411
|
const target = declaredBy.get(name);
|
|
473
412
|
return target === undefined || shared.has(target) ? undefined : target;
|
|
474
413
|
};
|
|
475
|
-
// The backend half: its export statements plus every top-level declaration they reach.
|
|
476
414
|
const backendSet = new Set();
|
|
477
415
|
const queue = statements.filter(isBackendStatement);
|
|
478
416
|
for (const info of queue)
|
|
@@ -488,14 +426,11 @@ export function splitRouteSource(ts, source, fileName, backendSpecifier, options
|
|
|
488
426
|
}
|
|
489
427
|
}
|
|
490
428
|
}
|
|
491
|
-
// The frontend half: everything that is not a backend export statement, minus declarations that
|
|
492
|
-
// only the backend half needs. An export list that also names backend exports keeps the rest.
|
|
493
429
|
const frontendKeep = new Set();
|
|
494
430
|
const seed = statements.filter((info) => !ts.isImportDeclaration(info.node) &&
|
|
495
431
|
!shared.has(info) &&
|
|
496
432
|
(!backendSet.has(info) ||
|
|
497
433
|
exportsOf(info).some((name) => !BACKEND_EXPORTS.has(name)) ||
|
|
498
|
-
// a statement with no declaration (a side effect) stays where it ran
|
|
499
434
|
(info.declares.size === 0 && info.exports.size === 0)));
|
|
500
435
|
const fqueue = [...seed];
|
|
501
436
|
for (const info of fqueue)
|
|
@@ -529,14 +464,11 @@ export function splitRouteSource(ts, source, fileName, backendSpecifier, options
|
|
|
529
464
|
!isBackendStatement(info) &&
|
|
530
465
|
info.declares.size > 0 &&
|
|
531
466
|
!isTypeDeclaration(info));
|
|
532
|
-
// Constant data both halves read moves to a module beside the route, with the declarations it
|
|
533
|
-
// needs: one definition, which neither half's bundle can drift from.
|
|
534
467
|
const shared = new Set();
|
|
535
468
|
const sharedTypes = new Set();
|
|
536
469
|
const sharedImports = new Set();
|
|
537
470
|
const unshared = new Map();
|
|
538
471
|
if (options.sharedFile !== undefined) {
|
|
539
|
-
/** Why `info` cannot move to the shared module, or `undefined` once it and what it needs are in `closure`. */
|
|
540
472
|
const gather = (info, closure) => {
|
|
541
473
|
if (closure.values.has(info) || shared.has(info))
|
|
542
474
|
return undefined;
|
|
@@ -613,8 +545,6 @@ export function splitRouteSource(ts, source, fileName, backendSpecifier, options
|
|
|
613
545
|
if (shared.size > 0)
|
|
614
546
|
({ backendSet, frontendKeep } = partition(shared));
|
|
615
547
|
}
|
|
616
|
-
// A moved name the frontend still mentions: in a type it becomes `import type`; at runtime it is an
|
|
617
|
-
// error the author has to resolve.
|
|
618
548
|
const typeImports = new Set();
|
|
619
549
|
for (const info of frontendKeep) {
|
|
620
550
|
if (isBackendStatement(info))
|
|
@@ -648,7 +578,6 @@ export function splitRouteSource(ts, source, fileName, backendSpecifier, options
|
|
|
648
578
|
const backendUses = uses(backendSet, true);
|
|
649
579
|
const frontendUses = uses(frontendKeep, false);
|
|
650
580
|
const semi = statements.some((info) => terminatorOf(sf, info.node) !== "") ? ";" : "";
|
|
651
|
-
// The imports the split writes quote the way the file's own imports do.
|
|
652
581
|
const firstImport = statements.find((info) => ts.isImportDeclaration(info.node))?.node;
|
|
653
582
|
const quote = firstImport !== undefined && ts.isImportDeclaration(firstImport)
|
|
654
583
|
? (firstImport.moduleSpecifier.getText(sf)[0] ?? '"')
|
|
@@ -656,7 +585,6 @@ export function splitRouteSource(ts, source, fileName, backendSpecifier, options
|
|
|
656
585
|
const sharedNames = [...shared].flatMap((info) => [...info.declares]);
|
|
657
586
|
const sharedSpecifier = (() => {
|
|
658
587
|
const file = options.sharedFile ?? "";
|
|
659
|
-
// The halves name the module the way the route names its other relative imports.
|
|
660
588
|
const relative = statements.flatMap((info) => ts.isImportDeclaration(info.node) &&
|
|
661
589
|
ts.isStringLiteral(info.node.moduleSpecifier) &&
|
|
662
590
|
info.node.moduleSpecifier.text.startsWith(".")
|
|
@@ -679,7 +607,6 @@ export function splitRouteSource(ts, source, fileName, backendSpecifier, options
|
|
|
679
607
|
`\nimport ${typeOnly ? "type " : ""}{ ${names.join(", ")} } from ${quote}${sharedSpecifier}${quote}${semi}`,
|
|
680
608
|
];
|
|
681
609
|
};
|
|
682
|
-
// The page still exports what it exported, now from the shared module.
|
|
683
610
|
const reexported = [...shared].flatMap((info) => [...info.exports].flatMap(([local, names]) => names.map((name) => (name === local ? local : `${local} as ${name}`))));
|
|
684
611
|
const backendTexts = [];
|
|
685
612
|
const frontendTexts = [];
|
|
@@ -716,7 +643,6 @@ export function splitRouteSource(ts, source, fileName, backendSpecifier, options
|
|
|
716
643
|
if (frontendKeep.has(info))
|
|
717
644
|
frontendTexts.push(info.text);
|
|
718
645
|
}
|
|
719
|
-
// After the other imports: the nearest relative path sorts last.
|
|
720
646
|
const frontendImports = sharedImport(frontendUses);
|
|
721
647
|
if (reexported.length > 0)
|
|
722
648
|
frontendImports.push(`\nexport { ${reexported.join(", ")} } from ${quote}${sharedSpecifier}${quote}${semi}`);
|
|
@@ -724,7 +650,6 @@ export function splitRouteSource(ts, source, fileName, backendSpecifier, options
|
|
|
724
650
|
frontendImports.push(`\nimport type { ${[...typeImports].sort().join(", ")} } from ${quote}${backendSpecifier}${quote}${semi}`);
|
|
725
651
|
frontendTexts.splice(afterImports, 0, ...frontendImports);
|
|
726
652
|
backendTexts.splice(backendAfterImports, 0, ...sharedImport(backendUses));
|
|
727
|
-
// A JSX pragma counts only in the file's leading comments, and the backend half has no JSX.
|
|
728
653
|
const pragmas = (ts.getLeadingCommentRanges(source, 0) ?? [])
|
|
729
654
|
.map((range) => source.slice(range.pos, range.end))
|
|
730
655
|
.filter((comment) => /@jsx/.test(comment));
|
|
@@ -754,14 +679,12 @@ export function splitRouteSource(ts, source, fileName, backendSpecifier, options
|
|
|
754
679
|
};
|
|
755
680
|
return sharedText === undefined ? result : { ...result, shared: sharedText };
|
|
756
681
|
}
|
|
757
|
-
/** A declaration the shared module holds is exported, so either half can import it. */
|
|
758
682
|
function withExport(ts, sf, info) {
|
|
759
683
|
if (hasModifier(ts, info.node, ts.SyntaxKind.ExportKeyword))
|
|
760
684
|
return info.text;
|
|
761
685
|
const at = info.node.getStart(sf) - info.node.getFullStart();
|
|
762
686
|
return `${info.text.slice(0, at)}export ${info.text.slice(at)}`;
|
|
763
687
|
}
|
|
764
|
-
/** A helper the backend half copies keeps its declaration; an `export` on it would add a backend export. */
|
|
765
688
|
function stripExport(ts, sf, info) {
|
|
766
689
|
if (info.exports.size === 0)
|
|
767
690
|
return info.text;
|
|
@@ -773,11 +696,9 @@ function stripExport(ts, sf, info) {
|
|
|
773
696
|
const end = exportKeyword.getEnd() - node.getFullStart();
|
|
774
697
|
return info.text.slice(0, start) + info.text.slice(end).replace(/^\s+/, "");
|
|
775
698
|
}
|
|
776
|
-
/** `_middleware.ts`'s default export, as the `middleware` export of a `_layout.backend.ts`. */
|
|
777
699
|
export function middlewareSource(ts, source, fileName) {
|
|
778
700
|
const sf = ts.createSourceFile(fileName, source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
|
|
779
701
|
let out = source;
|
|
780
|
-
// Edit from the end so earlier offsets stay valid.
|
|
781
702
|
for (const node of [...sf.statements].reverse()) {
|
|
782
703
|
if (ts.isExportAssignment(node) && !node.isExportEquals) {
|
|
783
704
|
out = `${out.slice(0, node.getStart(sf))}export const middleware = ${node.expression.getText(sf)}${terminatorOf(sf, node)}${out.slice(node.getEnd())}`;
|
|
@@ -804,7 +725,6 @@ export function middlewareSource(ts, source, fileName) {
|
|
|
804
725
|
}
|
|
805
726
|
return out;
|
|
806
727
|
}
|
|
807
|
-
/** Merge two backend modules: imports first (identical ones once), then the rest of each in order. */
|
|
808
728
|
function mergeModules(ts, first, second) {
|
|
809
729
|
if (first.trim() === "")
|
|
810
730
|
return second;
|
|
@@ -824,15 +744,9 @@ function mergeModules(ts, first, second) {
|
|
|
824
744
|
const body = [...a.body, ...b.body].map((text) => text.trim()).join("\n\n");
|
|
825
745
|
return `${imports.join("\n")}${imports.length > 0 ? "\n\n" : ""}${body}\n`;
|
|
826
746
|
}
|
|
827
|
-
// ---------------------------------------------------------------------------------------------------
|
|
828
|
-
// SFC script blocks
|
|
829
|
-
// ---------------------------------------------------------------------------------------------------
|
|
830
|
-
/** The `<script>` blocks of a Svelte/Vue file: their content range and whether it is the module script. */
|
|
831
747
|
function scriptBlocks(source) {
|
|
832
748
|
const blocks = [];
|
|
833
|
-
// A closing tag ends at any space, slash or `>` after its name, as the compilers read it.
|
|
834
749
|
const pattern = /<script\b([^>]*)>([\s\S]*?)<\/script(?=[\s/>])[^>]*>/gi;
|
|
835
|
-
// A `<script>` written inside an HTML comment is prose, not a block; blank comments out, keeping offsets.
|
|
836
750
|
const visible = source.replace(/<!--[\s\S]*?-->/g, (comment) => comment.replace(/[^\n]/g, " "));
|
|
837
751
|
for (const match of visible.matchAll(pattern)) {
|
|
838
752
|
const attrs = match[1] ?? "";
|
|
@@ -846,14 +760,12 @@ function scriptBlocks(source) {
|
|
|
846
760
|
}
|
|
847
761
|
return blocks;
|
|
848
762
|
}
|
|
849
|
-
/** `text` with every edit made, last offset first so the ones before it still point where they did. */
|
|
850
763
|
function applyEdits(text, edits) {
|
|
851
764
|
let out = text;
|
|
852
765
|
for (const edit of edits.sort((a, b) => b.start - a.start))
|
|
853
766
|
out = out.slice(0, edit.start) + edit.text + out.slice(edit.end);
|
|
854
767
|
return out;
|
|
855
768
|
}
|
|
856
|
-
/** The path argument of `mock.module("./x", ...)` or a vitest/jest twin, when it is a literal. */
|
|
857
769
|
function mockedSpecifier(ts, node) {
|
|
858
770
|
const callee = node.expression;
|
|
859
771
|
if (!ts.isPropertyAccessExpression(callee) || !ts.isIdentifier(callee.expression))
|
|
@@ -912,7 +824,6 @@ function importsOf(ts, file, text) {
|
|
|
912
824
|
const isRequire = ts.isIdentifier(node.expression) && node.expression.text === "require";
|
|
913
825
|
if ((isImport || isRequire) && arg !== undefined && ts.isStringLiteralLike(arg))
|
|
914
826
|
refs.push({ specifier: arg.text, typeOnly: false });
|
|
915
|
-
// A test's mock names the module without loading it into the app: no runtime edge.
|
|
916
827
|
const mocked = mockedSpecifier(ts, node);
|
|
917
828
|
if (mocked !== undefined)
|
|
918
829
|
refs.push({ specifier: mocked.text, typeOnly: true });
|
|
@@ -928,7 +839,6 @@ function importsOf(ts, file, text) {
|
|
|
928
839
|
visit(sf);
|
|
929
840
|
return refs;
|
|
930
841
|
}
|
|
931
|
-
/** The `import.meta.glob` calls of a JS/TS module. */
|
|
932
842
|
function globCalls(ts, file, text) {
|
|
933
843
|
if (!/\.[cm]?[jt]sx?$/.test(file) || !text.includes("import.meta.glob"))
|
|
934
844
|
return [];
|
|
@@ -939,7 +849,6 @@ function globCalls(ts, file, text) {
|
|
|
939
849
|
ts.isPropertyAccessExpression(node.expression) &&
|
|
940
850
|
node.expression.name.text === "glob" &&
|
|
941
851
|
ts.isMetaProperty(node.expression.expression) &&
|
|
942
|
-
// nifra-expect NF-S002: keywordToken is a TypeScript SyntaxKind enum identifying import.meta, never a credential or request value.
|
|
943
852
|
node.expression.expression.keywordToken === ts.SyntaxKind.ImportKeyword) {
|
|
944
853
|
const arg = node.arguments[0];
|
|
945
854
|
const elements = arg === undefined ? [] : ts.isArrayLiteralExpression(arg) ? [...arg.elements] : [arg];
|
|
@@ -962,8 +871,6 @@ function globCalls(ts, file, text) {
|
|
|
962
871
|
visit(sf);
|
|
963
872
|
return calls;
|
|
964
873
|
}
|
|
965
|
-
/** A glob pattern written in `file` as an app-relative glob, by Vite's rules: `./` and `../` from the
|
|
966
|
-
* file's folder, `/` from the app root. `undefined` for any other shape, such as an alias. */
|
|
967
874
|
function appGlob(file, pattern) {
|
|
968
875
|
if (pattern.startsWith("/"))
|
|
969
876
|
return pattern.slice(1);
|
|
@@ -972,8 +879,6 @@ function appGlob(file, pattern) {
|
|
|
972
879
|
const at = posix.normalize(posix.join(posix.dirname(file), pattern));
|
|
973
880
|
return at.startsWith("../") ? undefined : at;
|
|
974
881
|
}
|
|
975
|
-
/** The files among `files` a glob call in `file` loads, a `!` pattern excluding; `undefined` when a
|
|
976
|
-
* pattern cannot be read from the app. */
|
|
977
882
|
function globTargets(file, patterns, files) {
|
|
978
883
|
const include = [];
|
|
979
884
|
const exclude = [];
|
|
@@ -987,11 +892,6 @@ function globTargets(file, patterns, files) {
|
|
|
987
892
|
include.some((glob) => glob.match(candidate)) &&
|
|
988
893
|
!exclude.some((glob) => glob.match(candidate)));
|
|
989
894
|
}
|
|
990
|
-
/**
|
|
991
|
-
* `pattern`, written in `from` (now `to`), re-pointed at the folder its matches `before` moved to: the
|
|
992
|
-
* part before the first wildcard is the folder, and every match must keep its path below it.
|
|
993
|
-
* `undefined` when the matches did not move as one folder.
|
|
994
|
-
*/
|
|
995
895
|
function repointGlob(from, to, pattern, before, newPath) {
|
|
996
896
|
const negated = pattern.startsWith("!");
|
|
997
897
|
const written = negated ? pattern.slice(1) : pattern;
|
|
@@ -1023,7 +923,6 @@ function repointGlob(from, to, pattern, before, newPath) {
|
|
|
1023
923
|
: `${folder === "" ? "." : folder.startsWith("..") ? folder : `./${folder}`}/${rest}`;
|
|
1024
924
|
return negated ? `!${next}` : next;
|
|
1025
925
|
}
|
|
1026
|
-
/** Resolve a relative specifier against the file map, the way a bundler would. */
|
|
1027
926
|
function resolveIn(files, from, specifier) {
|
|
1028
927
|
if (!specifier.startsWith("."))
|
|
1029
928
|
return undefined;
|
|
@@ -1034,7 +933,6 @@ function resolveIn(files, from, specifier) {
|
|
|
1034
933
|
...RESOLVE_EXTENSIONS.map((ext) => base + ext),
|
|
1035
934
|
...RESOLVE_EXTENSIONS.map((ext) => `${base}/index${ext}`),
|
|
1036
935
|
];
|
|
1037
|
-
// `./x.js` written for a `./x.ts` source.
|
|
1038
936
|
const js = /\.(?:[cm]?js|jsx)$/.exec(base);
|
|
1039
937
|
if (js !== null) {
|
|
1040
938
|
const stem = base.slice(0, -js[0].length);
|
|
@@ -1042,7 +940,6 @@ function resolveIn(files, from, specifier) {
|
|
|
1042
940
|
}
|
|
1043
941
|
return candidates.find((candidate) => files.has(candidate));
|
|
1044
942
|
}
|
|
1045
|
-
/** Where `text` spells, inside quotes, an import specifier that resolves among `files`: [start, end). */
|
|
1046
943
|
function liveSpecifierSpans(ts, file, text, files) {
|
|
1047
944
|
const spans = [];
|
|
1048
945
|
for (const { specifier } of importsOf(ts, file, text)) {
|
|
@@ -1056,10 +953,6 @@ function liveSpecifierSpans(ts, file, text, files) {
|
|
|
1056
953
|
}
|
|
1057
954
|
return spans;
|
|
1058
955
|
}
|
|
1059
|
-
/**
|
|
1060
|
-
* The comment spans of a JS/TS module: the leading and trailing trivia of each token. Empty when the
|
|
1061
|
-
* compiler cannot walk the tokens, so every mention in the file stays an issue.
|
|
1062
|
-
*/
|
|
1063
956
|
function triviaCommentRanges(ts, file, text) {
|
|
1064
957
|
const spans = new Map();
|
|
1065
958
|
const add = (ranges) => {
|
|
@@ -1070,7 +963,6 @@ function triviaCommentRanges(ts, file, text) {
|
|
|
1070
963
|
const kind = /x$/.test(file) ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
|
|
1071
964
|
const source = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true, kind);
|
|
1072
965
|
const visit = (node) => {
|
|
1073
|
-
// A JSDoc block is itself one of the comments its token's trivia holds; JSX text is not trivia.
|
|
1074
966
|
if (node.kind >= ts.SyntaxKind.FirstJSDocNode && node.kind <= ts.SyntaxKind.LastJSDocNode)
|
|
1075
967
|
return;
|
|
1076
968
|
if (ts.isJsxText(node))
|
|
@@ -1092,10 +984,6 @@ function triviaCommentRanges(ts, file, text) {
|
|
|
1092
984
|
return [...spans];
|
|
1093
985
|
}
|
|
1094
986
|
const OWN_DIR = new Set(["import.meta.dir", "import.meta.dirname", "__dirname"]);
|
|
1095
|
-
/**
|
|
1096
|
-
* The path `relative` names from the folder of `from`, named from the folder of `to`; `undefined` when
|
|
1097
|
-
* neither the file nor the target moved, so a file that stays keeps its spelling.
|
|
1098
|
-
*/
|
|
1099
987
|
function rebaseRelative(from, to, relative, locate) {
|
|
1100
988
|
const before = posix.normalize(posix.join(posix.dirname(from), relative));
|
|
1101
989
|
const target = locate(before);
|
|
@@ -1104,14 +992,8 @@ function rebaseRelative(from, to, relative, locate) {
|
|
|
1104
992
|
const next = posix.relative(posix.dirname(to), target) || ".";
|
|
1105
993
|
return relative.endsWith("/") && next !== "." ? `${next}/` : next;
|
|
1106
994
|
}
|
|
1107
|
-
/** A shell script's own folder: `$(dirname "$0")`, `$(dirname "${BASH_SOURCE[0]}")`, `${0%/*}`. */
|
|
1108
995
|
const SHELL_OWN_DIR = /\$\(\s*dirname\s+("?)\$(?:0|\{BASH_SOURCE(?:\[0\])?\}|BASH_SOURCE)\1\s*\)|`dirname\s+("?)\$0\2`|\$\{(?:0|BASH_SOURCE(?:\[0\])?)%\/\*\}/g;
|
|
1109
996
|
const SHELL_FILE = /\.(?:sh|bash|zsh)$/;
|
|
1110
|
-
/**
|
|
1111
|
-
* Re-point the paths a shell script moving from `from` to `to` builds from its own folder
|
|
1112
|
-
* (`cd "$(dirname "$0")/.."`), as `rebaseOwnPaths` does for a module. Returns the new text and the
|
|
1113
|
-
* first use of the script's folder with no literal path after it, which the migration cannot follow.
|
|
1114
|
-
*/
|
|
1115
997
|
function rebaseShellPaths(from, to, text, locate) {
|
|
1116
998
|
let unfollowed;
|
|
1117
999
|
const edits = [];
|
|
@@ -1131,10 +1013,6 @@ function rebaseShellPaths(from, to, text, locate) {
|
|
|
1131
1013
|
next = next.slice(0, edit.start) + edit.text + next.slice(edit.end);
|
|
1132
1014
|
return { text: next, unfollowed };
|
|
1133
1015
|
}
|
|
1134
|
-
/**
|
|
1135
|
-
* Top-level `const dir = import.meta.dir` (or `.dirname`, `__dirname`) bindings, by name. A name the
|
|
1136
|
-
* file declares anywhere else (a parameter, an inner `let`) is left out: a use could be that binding.
|
|
1137
|
-
*/
|
|
1138
1016
|
function ownDirAliases(ts, sf) {
|
|
1139
1017
|
const aliases = new Map();
|
|
1140
1018
|
for (const statement of sf.statements) {
|
|
@@ -1171,13 +1049,6 @@ function ownDirAliases(ts, sf) {
|
|
|
1171
1049
|
aliases.delete(name);
|
|
1172
1050
|
return aliases;
|
|
1173
1051
|
}
|
|
1174
|
-
/**
|
|
1175
|
-
* Re-point the paths a module moving from `from` to `to` builds from its own location, so each still
|
|
1176
|
-
* reaches what it did: `join(import.meta.dir, "..", "x")` (or `resolve`), `import.meta.dir + "/../x"`,
|
|
1177
|
-
* `` `${__dirname}/../x` `` and `new URL("../x", import.meta.url)`, each with literal segments. `locate`
|
|
1178
|
-
* maps an app-relative target to where it ends up. Returns the new text and the first use of the
|
|
1179
|
-
* module's location it could not follow.
|
|
1180
|
-
*/
|
|
1181
1052
|
function rebaseOwnPaths(ts, from, to, text, locate) {
|
|
1182
1053
|
const sf = ts.createSourceFile(to, text, ts.ScriptTarget.Latest, true, to.endsWith("x") ? ts.ScriptKind.TSX : ts.ScriptKind.TS);
|
|
1183
1054
|
const edits = [];
|
|
@@ -1251,8 +1122,6 @@ function rebaseOwnPaths(ts, from, to, text, locate) {
|
|
|
1251
1122
|
ts.forEachChild(node, visit);
|
|
1252
1123
|
};
|
|
1253
1124
|
visit(sf);
|
|
1254
|
-
// An alias whose every use was re-pointed is followed too; one left in a call the migration cannot
|
|
1255
|
-
// read keeps its `import.meta.dir` unfollowed.
|
|
1256
1125
|
for (const [name, declaration] of aliases) {
|
|
1257
1126
|
const isFollowed = (node) => followed.some(([start, end]) => start <= node.getStart(sf) && node.getEnd() <= end);
|
|
1258
1127
|
const uses = [];
|
|
@@ -1268,8 +1137,6 @@ function rebaseOwnPaths(ts, from, to, text, locate) {
|
|
|
1268
1137
|
const unfollowed = [...text.matchAll(new RegExp(SELF_LOCATION.source, "g"))].find((match) => !followed.some(([start, end]) => start <= match.index && match.index < end))?.[0];
|
|
1269
1138
|
return { text: applyEdits(text, edits), unfollowed };
|
|
1270
1139
|
}
|
|
1271
|
-
/** Rewrite one relative specifier from `from`'s new location to `target`'s new location, keeping the
|
|
1272
|
-
* author's style: extension kept when written, dropped when not, `/index` collapsed when it was. */
|
|
1273
1140
|
function respecify(original, oldTarget, newFrom, newTarget) {
|
|
1274
1141
|
const query = original.slice(withoutQuery(original).length);
|
|
1275
1142
|
const clean = original.slice(0, original.length - query.length);
|
|
@@ -1291,7 +1158,6 @@ function respecify(original, oldTarget, newFrom, newTarget) {
|
|
|
1291
1158
|
rel = `./${rel}`;
|
|
1292
1159
|
return rel + query;
|
|
1293
1160
|
}
|
|
1294
|
-
/** A relative specifier whose target does not move, rewritten for an importer that does. */
|
|
1295
1161
|
function rebase(specifier, from, newFrom) {
|
|
1296
1162
|
if (!specifier.startsWith(".") || from === newFrom)
|
|
1297
1163
|
return undefined;
|
|
@@ -1299,7 +1165,6 @@ function rebase(specifier, from, newFrom) {
|
|
|
1299
1165
|
const rel = posix.relative(posix.dirname(newFrom), target);
|
|
1300
1166
|
return rel.startsWith(".") ? rel : `./${rel}`;
|
|
1301
1167
|
}
|
|
1302
|
-
/** `specifier` re-pointed from a split route to its backend half, in the author's extension style. */
|
|
1303
1168
|
function backendSpecifier(specifier, route, backend, from) {
|
|
1304
1169
|
const query = specifier.slice(withoutQuery(specifier).length);
|
|
1305
1170
|
const written = extname(specifier.slice(0, specifier.length - query.length));
|
|
@@ -1312,11 +1177,6 @@ function backendSpecifier(specifier, route, backend, from) {
|
|
|
1312
1177
|
const rel = posix.relative(posix.dirname(from), target);
|
|
1313
1178
|
return (rel.startsWith(".") ? rel : `./${rel}`) + query;
|
|
1314
1179
|
}
|
|
1315
|
-
/**
|
|
1316
|
-
* Point an importer's named imports of a split route's moved exports (`loader`, `action`, ...) at the
|
|
1317
|
-
* backend half, splitting a statement that also names what stayed. What cannot be rewritten (a
|
|
1318
|
-
* namespace or dynamic import that reads a moved name, `export *`) is returned as an issue.
|
|
1319
|
-
*/
|
|
1320
1180
|
function redirectMovedImports(ts, file, text, splitOf) {
|
|
1321
1181
|
const redirects = [];
|
|
1322
1182
|
const sf = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true, file.endsWith("x") ? ts.ScriptKind.TSX : ts.ScriptKind.TS);
|
|
@@ -1404,11 +1264,6 @@ function redirectMovedImports(ts, file, text, splitOf) {
|
|
|
1404
1264
|
visit(sf);
|
|
1405
1265
|
return { text: applyEdits(text, edits), issues, redirects };
|
|
1406
1266
|
}
|
|
1407
|
-
/**
|
|
1408
|
-
* The exports code reads from what `import()` loads: off the awaited module (`(await import(x)).a`),
|
|
1409
|
-
* through a destructuring, or through a variable used only as `mod.a`. `undefined` when the module
|
|
1410
|
-
* goes anywhere else, so its reads cannot be listed.
|
|
1411
|
-
*/
|
|
1412
1267
|
function dynamicReads(ts, sf, call) {
|
|
1413
1268
|
let node = call;
|
|
1414
1269
|
if (!ts.isAwaitExpression(node.parent))
|
|
@@ -1454,7 +1309,6 @@ function dynamicReads(ts, sf, call) {
|
|
|
1454
1309
|
visit(sf);
|
|
1455
1310
|
return escapes ? undefined : [...names].sort(codeUnitOrder);
|
|
1456
1311
|
}
|
|
1457
|
-
/** A `workspaces` glob as a matcher over package-relative folder paths. */
|
|
1458
1312
|
function workspacePattern(glob) {
|
|
1459
1313
|
const source = glob
|
|
1460
1314
|
.replace(/^\.\//, "")
|
|
@@ -1466,11 +1320,6 @@ function workspacePattern(glob) {
|
|
|
1466
1320
|
.join("/");
|
|
1467
1321
|
return new RegExp(`^${source}$`);
|
|
1468
1322
|
}
|
|
1469
|
-
/**
|
|
1470
|
-
* The workspace an app belongs to: the nearest folder above it whose package.json declares
|
|
1471
|
-
* `workspaces` that include the app, and the package folders those globs (less any `!` exclusion)
|
|
1472
|
-
* name, other than the app and any folder that holds it. `undefined` outside one.
|
|
1473
|
-
*/
|
|
1474
1323
|
function workspaceSiblings(root) {
|
|
1475
1324
|
for (let dir = dirname(root), depth = 0; depth < 8; dir = dirname(dir), depth += 1) {
|
|
1476
1325
|
let declared;
|
|
@@ -1493,7 +1342,6 @@ function workspaceSiblings(root) {
|
|
|
1493
1342
|
.map((glob) => workspacePattern(glob.slice(1)));
|
|
1494
1343
|
const member = (rel) => include.some((pattern) => pattern.test(rel)) &&
|
|
1495
1344
|
!exclude.some((pattern) => pattern.test(rel));
|
|
1496
|
-
// The nearest declaration decides, as package managers read it.
|
|
1497
1345
|
if (!member(toPosix(relative(dir, root))))
|
|
1498
1346
|
return undefined;
|
|
1499
1347
|
const packages = [];
|
|
@@ -1523,16 +1371,8 @@ function workspaceSiblings(root) {
|
|
|
1523
1371
|
}
|
|
1524
1372
|
return undefined;
|
|
1525
1373
|
}
|
|
1526
|
-
/**
|
|
1527
|
-
* Where code in a workspace sibling names an app file that moves: a relative import into the app, or
|
|
1528
|
-
* the file's path in a string or command (`bun -e 'import("./src/x.ts")'` with the app as its working
|
|
1529
|
-
* directory). Only files that mention the app's folder or package name are read further, and a path
|
|
1530
|
-
* the sibling's own package holds is its own file. Every line that names a path is listed, each marked
|
|
1531
|
-
* when it lies only in a comment. Report-only: the migration never edits a sibling.
|
|
1532
|
-
*/
|
|
1533
1374
|
function siblingReferences(ts, root, pkg, file, text, moves, known, ownName) {
|
|
1534
1375
|
const abs = join(pkg, file);
|
|
1535
|
-
// A process started in the app (`cwd: \`${repo}apps/web\``) reads its paths from the app root.
|
|
1536
1376
|
const runsInApp = new RegExp(`\\bcwd\\s*:[^,\\n]*\\b${escapeRegExp(posix.basename(toPosix(root)))}\\b`).test(text);
|
|
1537
1377
|
const comments = /\.[cm]?[jt]sx?$/.test(file) ? triviaCommentRanges(ts, file, text) : [];
|
|
1538
1378
|
const found = new Map();
|
|
@@ -1567,8 +1407,6 @@ function siblingReferences(ts, root, pkg, file, text, moves, known, ownName) {
|
|
|
1567
1407
|
: undefined;
|
|
1568
1408
|
const pattern = new RegExp(`(?<![\\w.-])(?:\\./)?(?:${escapeRegExp(from)}(?![\\w])${stem === undefined ? "" : `|${escapeRegExp(stem)}(?=["'\`])`})`, "g");
|
|
1569
1409
|
for (const match of text.matchAll(pattern)) {
|
|
1570
|
-
// `packages/x/src/a.ts` names another package's `src/a.ts`; only a prefix that reaches the
|
|
1571
|
-
// app counts, and a path the sibling holds itself is its own.
|
|
1572
1410
|
const prefix = /(?:[\w.@-]+\/)+$/.exec(text.slice(0, match.index))?.[0] ?? "";
|
|
1573
1411
|
const bases = prefix === "" ? [] : [dirname(abs), pkg, dirname(pkg), dirname(dirname(pkg))];
|
|
1574
1412
|
if (prefix !== "" && !bases.some((base) => resolve(base, prefix) === root))
|
|
@@ -1587,8 +1425,6 @@ function siblingReferences(ts, root, pkg, file, text, moves, known, ownName) {
|
|
|
1587
1425
|
commentLines: [...commentLines].filter((line) => !lines.has(line)).sort(byNumber),
|
|
1588
1426
|
}));
|
|
1589
1427
|
}
|
|
1590
|
-
/** The package folder an app without its own package.json sits in (`--dir src`), at most two levels
|
|
1591
|
-
* up and never a workspace root, so the scan beside it stays inside one package. */
|
|
1592
1428
|
function enclosingPackage(root) {
|
|
1593
1429
|
if (existsSync(join(root, "package.json")))
|
|
1594
1430
|
return undefined;
|
|
@@ -1644,7 +1480,6 @@ function rewriteImports(ts, file, text, rewrite) {
|
|
|
1644
1480
|
visit(sf);
|
|
1645
1481
|
};
|
|
1646
1482
|
const edits = [];
|
|
1647
|
-
// A match's group 2 is the specifier; group 1 is everything before it.
|
|
1648
1483
|
const rewriteMatches = (pattern) => {
|
|
1649
1484
|
for (const match of text.matchAll(pattern)) {
|
|
1650
1485
|
const next = rewrite(match[2] ?? "");
|
|
@@ -1669,7 +1504,6 @@ function rewriteImports(ts, file, text, rewrite) {
|
|
|
1669
1504
|
}
|
|
1670
1505
|
return applyEdits(text, edits);
|
|
1671
1506
|
}
|
|
1672
|
-
/** `ServerOnly` imported from `@nifrajs/web` becomes `BackendOnly`, along with every use of it. */
|
|
1673
1507
|
function renameRetiredTypes(ts, file, text) {
|
|
1674
1508
|
if (!text.includes("ServerOnly"))
|
|
1675
1509
|
return text;
|
|
@@ -1717,16 +1551,7 @@ function renameRetiredTypes(ts, file, text) {
|
|
|
1717
1551
|
}
|
|
1718
1552
|
return applyEdits(text, edits);
|
|
1719
1553
|
}
|
|
1720
|
-
// ---------------------------------------------------------------------------------------------------
|
|
1721
|
-
// The migration
|
|
1722
|
-
// ---------------------------------------------------------------------------------------------------
|
|
1723
|
-
/** Root files that stay where they are: the CLI's config, and scripts no build loads. */
|
|
1724
|
-
/** The config files the loader reads, old layout included; a path field may live in any of them. */
|
|
1725
1554
|
const CONFIG_FILES = ["nifra.config.ts", "framework.ts", "backend/framework.ts"];
|
|
1726
|
-
/**
|
|
1727
|
-
* The module paths a config exports, read statically: `clientModule` and the component pipeline's
|
|
1728
|
-
* `entries`. The browser loads `clientModule` and `entries.client`; `ssr` and `rsc` run on the server.
|
|
1729
|
-
*/
|
|
1730
1555
|
function configEntries(ts, file, text) {
|
|
1731
1556
|
const sf = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
|
|
1732
1557
|
const bare = (node) => {
|
|
@@ -1777,14 +1602,12 @@ function configEntries(ts, file, text) {
|
|
|
1777
1602
|
}
|
|
1778
1603
|
return out;
|
|
1779
1604
|
}
|
|
1780
|
-
/** The package an import names: `@scope/pkg` or `pkg`, without a subpath. */
|
|
1781
1605
|
const packageName = (specifier) => {
|
|
1782
1606
|
const parts = specifier.split("/");
|
|
1783
1607
|
return specifier.startsWith("@") ? parts.slice(0, 2).join("/") : (parts[0] ?? "");
|
|
1784
1608
|
};
|
|
1785
1609
|
const isRootScript = (file, rootMoves) => !file.includes("/") && SOURCE_FILE.test(file) && rootMoves[file] === undefined;
|
|
1786
1610
|
const isTestFile = (file) => /(?:^|\/)(?:test|tests|__tests__)\//.test(file) || /\.(?:test|spec)\.[cm]?[jt]sx?$/.test(file);
|
|
1787
|
-
/** The web config an app has, old root `framework.ts` included, or `undefined`. */
|
|
1788
1611
|
const webConfigFile = (root, rootMoves) => rootMoves["framework.ts"] !== undefined
|
|
1789
1612
|
? "framework.ts"
|
|
1790
1613
|
: existsSync(join(root, FRAMEWORK_FILE))
|
|
@@ -1792,10 +1615,6 @@ const webConfigFile = (root, rootMoves) => rootMoves["framework.ts"] !== undefin
|
|
|
1792
1615
|
: hasWebConfig(root, ".")
|
|
1793
1616
|
? CONFIG_FILE
|
|
1794
1617
|
: undefined;
|
|
1795
|
-
/**
|
|
1796
|
-
* `"api"` when the app declares `"nifra": { "kind": "api" }`, or has no web config, no `routes/` and a
|
|
1797
|
-
* backend; `"web"` when it has `routes/`. Web config without `routes/` could be either, so it throws.
|
|
1798
|
-
*/
|
|
1799
1618
|
function layoutMode(root, rootMoves) {
|
|
1800
1619
|
const declared = createKindProbe(root)(root);
|
|
1801
1620
|
if (declared.invalid !== undefined)
|
|
@@ -1819,7 +1638,6 @@ function layoutMode(root, rootMoves) {
|
|
|
1819
1638
|
}
|
|
1820
1639
|
return "api";
|
|
1821
1640
|
}
|
|
1822
|
-
/** An app one folder down (`src/routes/`): the `--dir` that migrates it, as a sentence to append. */
|
|
1823
1641
|
function nestedAppHint(root) {
|
|
1824
1642
|
const found = readdirSync(root, { withFileTypes: true })
|
|
1825
1643
|
.filter((entry) => entry.isDirectory() &&
|
|
@@ -1838,10 +1656,8 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
1838
1656
|
if (ts === undefined)
|
|
1839
1657
|
throw new Error("[nifra] `nifra migrate layout` needs TypeScript: bun add -d typescript");
|
|
1840
1658
|
const root = resolve(appRoot);
|
|
1841
|
-
// Only a root file that exports what the old layout read moves; any other backend.ts is a module.
|
|
1842
1659
|
const rootMoves = Object.fromEntries(retiredRootFiles(root));
|
|
1843
1660
|
const mode = layoutMode(root, rootMoves);
|
|
1844
|
-
// An API-only app has no browser: nothing to split, and every file is server code where it is.
|
|
1845
1661
|
const api = mode === "api";
|
|
1846
1662
|
const zones = appZones(root);
|
|
1847
1663
|
const configured = [...(zones.backend ?? []), ...(zones.frontend ?? []), ...(zones.shared ?? [])];
|
|
@@ -1854,11 +1670,8 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
1854
1670
|
}
|
|
1855
1671
|
const issues = [];
|
|
1856
1672
|
const splits = [];
|
|
1857
|
-
/** The app after the migration: new path -> text. */
|
|
1858
1673
|
const next = new Map(original);
|
|
1859
|
-
/** Old path -> new path, for every file that moves. */
|
|
1860
1674
|
const moveMap = new Map();
|
|
1861
|
-
// 1-2. Route halves and middleware.
|
|
1862
1675
|
const backendHalves = new Map();
|
|
1863
1676
|
for (const [file, text] of original) {
|
|
1864
1677
|
if (api || !file.startsWith("routes/") || file.includes(".backend."))
|
|
@@ -1875,7 +1688,6 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
1875
1688
|
const backendFile = file.replace(ROUTE_FILE, ".backend.ts");
|
|
1876
1689
|
const specifier = `./${posix.basename(backendFile)}`;
|
|
1877
1690
|
const sharedFile = file.replace(ROUTE_FILE, ".shared.ts");
|
|
1878
|
-
// A taken name leaves data both halves read copied into each, as reported.
|
|
1879
1691
|
const splitOptions = next.has(sharedFile) || existsSync(join(root, sharedFile))
|
|
1880
1692
|
? {}
|
|
1881
1693
|
: { sharedFile: posix.basename(sharedFile) };
|
|
@@ -1920,19 +1732,15 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
1920
1732
|
const existing = next.get(file);
|
|
1921
1733
|
next.set(file, existing === undefined ? text : mergeModules(ts, existing, text));
|
|
1922
1734
|
}
|
|
1923
|
-
// 3-4. Zoning by runtime reachability, on the post-split graph.
|
|
1924
1735
|
for (const [from, to] of Object.entries(rootMoves))
|
|
1925
1736
|
if (next.has(from))
|
|
1926
1737
|
moveMap.set(from, to);
|
|
1927
|
-
// Assets (images, fonts) resolve too, so they move with the modules that import them.
|
|
1928
1738
|
const known = new Set([...next.keys(), ...everyFile]);
|
|
1929
1739
|
const knownPaths = new Set([...known].map((file) => join(root, file)));
|
|
1930
|
-
// Route halves the split creates resolve before they are written.
|
|
1931
1740
|
const resolver = createSpecifierResolver({
|
|
1932
1741
|
moduleExists: (path) => knownPaths.has(path) || isFile(path),
|
|
1933
1742
|
});
|
|
1934
1743
|
const realRoot = realpathOrSelf(root);
|
|
1935
|
-
/** An app file as the migration names it, from a path the resolver found (real, or as planned). */
|
|
1936
1744
|
const appFile = (path) => {
|
|
1937
1745
|
if (path === undefined)
|
|
1938
1746
|
return undefined;
|
|
@@ -1943,8 +1751,6 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
1943
1751
|
}
|
|
1944
1752
|
return undefined;
|
|
1945
1753
|
};
|
|
1946
|
-
/** A non-relative specifier the app's tsconfig `paths`/`baseUrl` or package.json `imports` maps to
|
|
1947
|
-
* one of its files, as the bundler reads it. */
|
|
1948
1754
|
const viaAlias = (file, specifier) => {
|
|
1949
1755
|
if (specifier.startsWith(".") || isAbsolute(specifier))
|
|
1950
1756
|
return undefined;
|
|
@@ -1962,8 +1768,6 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
1962
1768
|
}
|
|
1963
1769
|
})();
|
|
1964
1770
|
const installed = new Map();
|
|
1965
|
-
/** Whether a bare specifier names something outside the app that exists: a built-in, a URL or a
|
|
1966
|
-
* scheme the bundler owns, the app's own package name, or a package installed at or above it. */
|
|
1967
1771
|
const external = (specifier) => {
|
|
1968
1772
|
if (/^[a-z][a-z\d+.-]*:/i.test(specifier) || specifier === "bun")
|
|
1969
1773
|
return true;
|
|
@@ -1987,13 +1791,10 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
1987
1791
|
}
|
|
1988
1792
|
return found;
|
|
1989
1793
|
};
|
|
1990
|
-
// A test (or any module) that imports `loader` from a page would import a name the page no longer
|
|
1991
|
-
// exports: it reads it from the backend half instead.
|
|
1992
1794
|
const splitRoutes = new Map();
|
|
1993
1795
|
for (const split of splits)
|
|
1994
1796
|
if (next.has(split.file))
|
|
1995
1797
|
splitRoutes.set(split.file, { ...split, moved: new Set(split.moved) });
|
|
1996
|
-
/** `base` is where `file` is relative to: the app root, or the package folder around it. */
|
|
1997
1798
|
const splitTarget = (base, file, resolve) => (specifier) => {
|
|
1998
1799
|
const route = resolve(specifier);
|
|
1999
1800
|
const split = route === undefined ? undefined : splitRoutes.get(route);
|
|
@@ -2045,7 +1846,6 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
2045
1846
|
});
|
|
2046
1847
|
}
|
|
2047
1848
|
}
|
|
2048
|
-
// What a glob loads is imported too: the files it matches go where the module that loads them goes.
|
|
2049
1849
|
for (const call of globCalls(ts, file, text)) {
|
|
2050
1850
|
const targets = call.literal
|
|
2051
1851
|
? globTargets(file, call.patterns.map((pattern) => pattern.text), known)
|
|
@@ -2062,12 +1862,9 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
2062
1862
|
}
|
|
2063
1863
|
edges.set(file, refs);
|
|
2064
1864
|
}
|
|
2065
|
-
// The config's module paths are entries too: the browser loads `clientModule`.
|
|
2066
1865
|
const entries = CONFIG_FILES.filter((file) => next.has(file)).flatMap((file) => configEntries(ts, file, next.get(file) ?? "").map((entry) => ({
|
|
2067
1866
|
...entry,
|
|
2068
1867
|
file,
|
|
2069
|
-
// A string is read from the app root, whichever config file holds it; a joined path from the
|
|
2070
|
-
// config's own folder.
|
|
2071
1868
|
target: entry.base === "app"
|
|
2072
1869
|
? resolveAny("nifra.config.ts", entry.path)
|
|
2073
1870
|
: resolveIn(known, file, entry.path.startsWith("../") ? entry.path : `./${entry.path}`),
|
|
@@ -2080,8 +1877,6 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
2080
1877
|
...files.filter((f) => f.startsWith("routes/") && ROUTE_FILE.test(f) && !f.includes(".backend.") && !isTestFile(f)),
|
|
2081
1878
|
...browserEntries,
|
|
2082
1879
|
];
|
|
2083
|
-
// A route page the server imports - a generated `server-manifest.ts` imports them all - is rendered
|
|
2084
|
-
// there, not run as server code, so the server's walk stops at it: what only pages use stays frontend.
|
|
2085
1880
|
const pages = new Set(frontendRoots.filter((f) => !browserEntries.has(f)));
|
|
2086
1881
|
const reach = (roots, stopAt) => {
|
|
2087
1882
|
const seen = new Set();
|
|
@@ -2111,10 +1906,7 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
2111
1906
|
];
|
|
2112
1907
|
const browser = reach(frontendRoots);
|
|
2113
1908
|
const server = reach(backendRoots, pages);
|
|
2114
|
-
// What the app's own server modules reach, without the root scripts: a root file in here is a module
|
|
2115
|
-
// the app imports, not an entry, so it moves into a zone.
|
|
2116
1909
|
const appServer = reach(backendRoots.filter((f) => f.includes("/") || rootMoves[f] !== undefined || serverEntries.has(f)), pages);
|
|
2117
|
-
// A `.server` module ran empty in the browser, so only server code used it: it moves under backend/.
|
|
2118
1910
|
for (const file of api ? [] : files) {
|
|
2119
1911
|
if (!LEGACY_SERVER.test(file) || isTestFile(file))
|
|
2120
1912
|
continue;
|
|
@@ -2130,7 +1922,6 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
2130
1922
|
});
|
|
2131
1923
|
}
|
|
2132
1924
|
}
|
|
2133
|
-
/** With `zones`, the files that stay where they are and the side package.json gives them. */
|
|
2134
1925
|
const inPlace = new Map();
|
|
2135
1926
|
const zoneOf = (file) => browser.has(file) && server.has(file) ? "shared" : browser.has(file) ? "frontend" : "backend";
|
|
2136
1927
|
for (const file of api ? [] : files) {
|
|
@@ -2164,9 +1955,6 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
2164
1955
|
}
|
|
2165
1956
|
moveMap.set(file, `${zone}/${file}`);
|
|
2166
1957
|
}
|
|
2167
|
-
// A folder whose reached files all move into one zone takes its other files along: a file nothing
|
|
2168
|
-
// imports at runtime (a component not used yet, an asset) has no zone of its own, and leaving it
|
|
2169
|
-
// behind splits the folder for every tool that lists it. Server built-ins never go to the browser.
|
|
2170
1958
|
const zonesUnder = new Map();
|
|
2171
1959
|
for (const [from, to] of moveMap) {
|
|
2172
1960
|
if (isTestFile(from))
|
|
@@ -2180,7 +1968,6 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
2180
1968
|
continue;
|
|
2181
1969
|
if (ZONE_DIRS.has(file.split("/")[0] ?? "") || inConfiguredFolder(file))
|
|
2182
1970
|
continue;
|
|
2183
|
-
// A test and its snapshot follow their subject below.
|
|
2184
1971
|
if (isTestFile(file) ||
|
|
2185
1972
|
file.endsWith(".snap") ||
|
|
2186
1973
|
file.endsWith(".d.ts") ||
|
|
@@ -2188,7 +1975,6 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
2188
1975
|
continue;
|
|
2189
1976
|
let zones;
|
|
2190
1977
|
for (let dir = posix.dirname(file); dir !== "." && zones === undefined; dir = posix.dirname(dir)) {
|
|
2191
|
-
// A nested package is its own install, never part of the app's folder.
|
|
2192
1978
|
if (existsSync(join(root, dir, "package.json")))
|
|
2193
1979
|
break;
|
|
2194
1980
|
zones = zonesUnder.get(dir);
|
|
@@ -2224,8 +2010,6 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
2224
2010
|
moveMap.delete(from);
|
|
2225
2011
|
}
|
|
2226
2012
|
const newPath = (file) => moveMap.get(file) ?? file;
|
|
2227
|
-
// A test beside its subject (`x.test.ts` by `x.ts`, or in a `__tests__/` under its folder) moves
|
|
2228
|
-
// with it, snapshots included, once the subject's place is settled; a test never decides a zone.
|
|
2229
2013
|
const present = new Set(files);
|
|
2230
2014
|
for (const test of files) {
|
|
2231
2015
|
const stem = /^(.+?)\.(?:test|spec)\.[cm]?[jt]sx?$/.exec(posix.basename(test))?.[1];
|
|
@@ -2270,7 +2054,6 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
2270
2054
|
...moveMap,
|
|
2271
2055
|
...[...inPlace].map(([file, side]) => [file, side]),
|
|
2272
2056
|
]) {
|
|
2273
|
-
// A test is never bundled, so its own server imports decide nothing.
|
|
2274
2057
|
if (isTestFile(from))
|
|
2275
2058
|
continue;
|
|
2276
2059
|
if ((to === "shared" || to.startsWith("shared/")) &&
|
|
@@ -2281,10 +2064,8 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
2281
2064
|
});
|
|
2282
2065
|
}
|
|
2283
2066
|
}
|
|
2284
|
-
// 5. Rewrite imports to the new locations.
|
|
2285
2067
|
const final = new Map();
|
|
2286
2068
|
const rewritten = [];
|
|
2287
|
-
// The app as the migration leaves it, where every rewritten specifier must reach its target.
|
|
2288
2069
|
const planned = new Set([...known].filter((file) => next.has(file) || !original.has(file)).map(newPath));
|
|
2289
2070
|
const plannedResolver = createSpecifierResolver({
|
|
2290
2071
|
moduleExists: (path) => {
|
|
@@ -2294,7 +2075,6 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
2294
2075
|
return isFile(path);
|
|
2295
2076
|
},
|
|
2296
2077
|
});
|
|
2297
|
-
/** Whether `specifier`, written in `from` after the moves, loads `target` there. */
|
|
2298
2078
|
const reaches = (from, specifier, target) => specifier.startsWith(".")
|
|
2299
2079
|
? resolveIn(planned, from, specifier) === target
|
|
2300
2080
|
: appFile(plannedResolver(join(root, from), withoutQuery(specifier))) === target;
|
|
@@ -2308,11 +2088,6 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
2308
2088
|
reason: `"${specifier}" names "${target}", which moves to "${newPath(target)}", and no specifier the migration can write from "${from}" reaches it there; point it there by hand`,
|
|
2309
2089
|
});
|
|
2310
2090
|
};
|
|
2311
|
-
/**
|
|
2312
|
-
* How `from` names `target` after the moves, for the alias `specifier` that `file` writes: as written
|
|
2313
|
-
* while it still reaches it, through the same alias when one of its targets covers the new place,
|
|
2314
|
-
* else as a relative path. `undefined` leaves it as written.
|
|
2315
|
-
*/
|
|
2316
2091
|
const aliasTo = (file, specifier, target, from = newPath(file)) => {
|
|
2317
2092
|
const to = newPath(target);
|
|
2318
2093
|
if (to === target && from === file)
|
|
@@ -2335,7 +2110,6 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
2335
2110
|
if (retired !== undefined)
|
|
2336
2111
|
return retired;
|
|
2337
2112
|
const target = resolveAny(file, specifier);
|
|
2338
|
-
// Outside the app (or a skipped dir): the target stays put, so only a moved importer re-bases it.
|
|
2339
2113
|
if (target === undefined)
|
|
2340
2114
|
return rebase(specifier, file, newPath(file));
|
|
2341
2115
|
if (!specifier.startsWith("."))
|
|
@@ -2353,9 +2127,6 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
2353
2127
|
(redirectedFiles.has(file) || (updated !== text && original.get(file) === text)))
|
|
2354
2128
|
rewritten.push(file);
|
|
2355
2129
|
}
|
|
2356
|
-
// A glob names its files by pattern, which no import rewrite reads: each one must still load, from
|
|
2357
|
-
// the module's new folder, the files it loaded before. A pattern whose matches moved together
|
|
2358
|
-
// into one folder is re-pointed at it; any other change is reported.
|
|
2359
2130
|
for (const file of next.keys()) {
|
|
2360
2131
|
const at = newPath(file);
|
|
2361
2132
|
const text = final.get(at);
|
|
@@ -2396,8 +2167,6 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
2396
2167
|
if (!moveMap.has(file) && !rewritten.includes(file))
|
|
2397
2168
|
rewritten.push(file);
|
|
2398
2169
|
}
|
|
2399
|
-
// A config string names its module from the app root, so it follows the module, never the config
|
|
2400
|
-
// file; a path joined onto the config's folder is re-pointed with the module's own paths below.
|
|
2401
2170
|
for (const configFile of CONFIG_FILES) {
|
|
2402
2171
|
const at = newPath(configFile);
|
|
2403
2172
|
const text = final.get(at);
|
|
@@ -2425,10 +2194,7 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
2425
2194
|
const moves = [...moveMap]
|
|
2426
2195
|
.map(([from, to]) => ({ from, to }))
|
|
2427
2196
|
.sort((a, b) => codeUnitOrder(a.from, b.from));
|
|
2428
|
-
// A moved module's paths from its own location now start from its new folder: re-point the ones it
|
|
2429
|
-
// spells out, and report the first one it cannot follow.
|
|
2430
2197
|
const unfollowed = new Map();
|
|
2431
|
-
/** Moved shell scripts whose own-folder paths were re-pointed: new path -> text. */
|
|
2432
2198
|
const scriptWrites = new Map();
|
|
2433
2199
|
for (const { from, to } of moves) {
|
|
2434
2200
|
if (SHELL_FILE.test(to) && !original.has(from)) {
|
|
@@ -2452,8 +2218,6 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
2452
2218
|
if (result.unfollowed !== undefined)
|
|
2453
2219
|
unfollowed.set(to, result.unfollowed);
|
|
2454
2220
|
}
|
|
2455
|
-
// A module that stays still names files from its own folder (a config's `clientModule` joined onto
|
|
2456
|
-
// `import.meta.dir`): each that moved is re-pointed.
|
|
2457
2221
|
const landed = new Set(moveMap.values());
|
|
2458
2222
|
for (const [file, text] of final) {
|
|
2459
2223
|
if (landed.has(file) || !/\.[cm]?[jt]sx?$/.test(file) || !SELF_LOCATION.test(text))
|
|
@@ -2465,11 +2229,7 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
2465
2229
|
if (original.has(file) && !rewritten.includes(file))
|
|
2466
2230
|
rewritten.push(file);
|
|
2467
2231
|
}
|
|
2468
|
-
// A path in a string or comment is not an import, so nothing above rewrote it: a build script that
|
|
2469
|
-
// writes `data/x.json` would keep writing to the old place, and so would a deploy script or
|
|
2470
|
-
// `package.json` beside the code.
|
|
2471
2232
|
const settled = new Set([...known].map(newPath));
|
|
2472
|
-
// A new backend half starts where its route was, so it keeps that route's place in each program.
|
|
2473
2233
|
const splitFrom = new Map(splits
|
|
2474
2234
|
.filter((split) => !original.has(split.backend))
|
|
2475
2235
|
.map((split) => [split.backend, split.file]));
|
|
@@ -2477,7 +2237,6 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
2477
2237
|
.filter((file) => next.has(file) || !original.has(file))
|
|
2478
2238
|
.map((file) => ({ from: splitFrom.get(file) ?? file, to: newPath(file) }));
|
|
2479
2239
|
const tsconfigs = followTsconfigs(ts, root, moveMap, layout);
|
|
2480
|
-
// The app's own package.json names files from the app root: its scripts and entry fields follow.
|
|
2481
2240
|
const manifestPath = join(root, "package.json");
|
|
2482
2241
|
const manifest = existsSync(manifestPath)
|
|
2483
2242
|
? rewritePackagePaths(ts, readFileSync(manifestPath, "utf8"), moveMap)
|
|
@@ -2494,21 +2253,17 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
2494
2253
|
readFileSync(join(root, file), "utf8"),
|
|
2495
2254
|
]);
|
|
2496
2255
|
}
|
|
2497
|
-
// A mention only in comments is worth updating, not a decision: it never changes what runs.
|
|
2498
2256
|
const commentFollowUps = [];
|
|
2499
2257
|
for (const [file, text] of scanned) {
|
|
2500
2258
|
let live;
|
|
2501
2259
|
let comments;
|
|
2502
2260
|
for (const [from, to] of moveMap) {
|
|
2503
|
-
// `shared/lib/x.ts` contains `lib/x.ts`; that is the new path, not a stale one.
|
|
2504
2261
|
const newPrefix = to.endsWith(from) ? to.slice(0, to.length - from.length) : undefined;
|
|
2505
2262
|
const stale = [
|
|
2506
2263
|
...text.matchAll(new RegExp(`(?<![\\w.-])${escapeRegExp(from)}(?![\\w])`, "g")),
|
|
2507
2264
|
].filter((match) => {
|
|
2508
2265
|
if (newPrefix !== undefined && text.slice(0, match.index).endsWith(newPrefix))
|
|
2509
2266
|
return false;
|
|
2510
|
-
// An import that resolves after the moves is current, even when it still reads like the old
|
|
2511
|
-
// path: `./x.json` from a module that moved together with `x.json`.
|
|
2512
2267
|
live ??= liveSpecifierSpans(ts, file, text, settled);
|
|
2513
2268
|
const end = match.index + from.length;
|
|
2514
2269
|
return !live.some(([start, stop]) => start <= match.index && end <= stop);
|
|
@@ -2523,8 +2278,6 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
2523
2278
|
issues.push({ file, reason: `mentions "${from}" by path; it moved to "${to}"` });
|
|
2524
2279
|
}
|
|
2525
2280
|
}
|
|
2526
|
-
// Code beside an app one folder down (a package's test/ when the app is its src/) is not the
|
|
2527
|
-
// migration's to edit, so each import there that stops resolving to what it named is reported.
|
|
2528
2281
|
const pkg = enclosingPackage(root);
|
|
2529
2282
|
if (pkg !== undefined) {
|
|
2530
2283
|
const prefix = toPosix(relative(pkg, root));
|
|
@@ -2556,8 +2309,6 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
2556
2309
|
issues.push({ file: at, reason });
|
|
2557
2310
|
}
|
|
2558
2311
|
}
|
|
2559
|
-
// A workspace sibling is not the migration's to edit either: each file that names a moved app file,
|
|
2560
|
-
// by import or by path, is reported with the line and both paths.
|
|
2561
2312
|
const appName = posix.basename(toPosix(root));
|
|
2562
2313
|
const mentionsApp = new RegExp(`(?:^|[/'"\`])${escapeRegExp(appName)}(?:[/'"\`]|$)`, "m");
|
|
2563
2314
|
for (const sibling of moveMap.size === 0 ? [] : (workspaceSiblings(root) ?? [])) {
|
|
@@ -2586,8 +2337,6 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
2586
2337
|
}
|
|
2587
2338
|
}
|
|
2588
2339
|
}
|
|
2589
|
-
// Paths a module finds at run time are not imports either: one built from the module's own location
|
|
2590
|
-
// now starts from its new folder, and a folder listed at run time loses whatever moved out of it.
|
|
2591
2340
|
const leaving = new Map();
|
|
2592
2341
|
for (const move of moves) {
|
|
2593
2342
|
const own = unfollowed.get(move.to);
|
|
@@ -2616,9 +2365,6 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
2616
2365
|
reason: `moves to "${first.to}"${others} but ${left} in ${dir}/; code or tooling that lists ${dir}/ no longer finds what moved - move the folder together by hand`,
|
|
2617
2366
|
});
|
|
2618
2367
|
}
|
|
2619
|
-
// The server refuses data a loader or action returns without an output schema. Each half the split
|
|
2620
|
-
// wrote gets a draft from its return type, read from the layout as planned so the dry run shows
|
|
2621
|
-
// what --write adds; only the author knows which fields the browser may see, so it is a draft.
|
|
2622
2368
|
const halves = [...new Set(splits.map((split) => split.backend))].filter((file) => final.has(file));
|
|
2623
2369
|
const unwritten = new Map();
|
|
2624
2370
|
for (const [file, text] of final)
|
|
@@ -2628,7 +2374,6 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
2628
2374
|
? undefined
|
|
2629
2375
|
: await planOutputSchemas(root, halves, (file) => final.get(file), {
|
|
2630
2376
|
overlay: unwritten,
|
|
2631
|
-
// What NF-C030 would flag: a schema for data the function returns, not for `return null`.
|
|
2632
2377
|
draft: (file, schema) => missingOutputSchemas(final.get(file) ?? "").some((found) => found.schema === schema),
|
|
2633
2378
|
});
|
|
2634
2379
|
for (const [file, text] of schemaPlan?.edits ?? [])
|
|
@@ -2641,8 +2386,6 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
2641
2386
|
for (const { name, schema } of missingOutputSchemas(text)) {
|
|
2642
2387
|
const why = schemaPlan?.entries.find((entry) => entry.file === file && entry.schema === schema && entry.reason !== undefined)?.reason;
|
|
2643
2388
|
const missing = `exports ${name === "action" ? "an" : "a"} ${name} but no ${schema}, so data it returns fails the request`;
|
|
2644
|
-
// A half this run did not write keeps the schema it had: drafting one is the next command, not a
|
|
2645
|
-
// decision the migration left open.
|
|
2646
2389
|
if (why === undefined && (schemaPlan === undefined || schemaPlan.warnings.length === 0)) {
|
|
2647
2390
|
followUps.push({
|
|
2648
2391
|
file,
|
|
@@ -2657,7 +2400,6 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
2657
2400
|
});
|
|
2658
2401
|
}
|
|
2659
2402
|
}
|
|
2660
|
-
// Tooling that lists folders or globs, not files: the import rewrite above never sees it.
|
|
2661
2403
|
const tooling = [];
|
|
2662
2404
|
const toolingWrites = new Map();
|
|
2663
2405
|
for (const [file, { text, changes }] of tsconfigs.files) {
|
|
@@ -2678,7 +2420,6 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
2678
2420
|
toolingWrites.set(".gitignore", gitignore);
|
|
2679
2421
|
tooling.push({ file: ".gitignore", change: "ignore .nifra/ (dev and build state)" });
|
|
2680
2422
|
}
|
|
2681
|
-
// A root backend.ts was served at /api by default until 5.0, which mounts it only where apiPrefix says.
|
|
2682
2423
|
if (!api && rootMoves["backend.ts"] !== undefined) {
|
|
2683
2424
|
const mount = apiPrefixFix({
|
|
2684
2425
|
framework: final.get(FRAMEWORK_FILE),
|
|
@@ -2707,7 +2448,6 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
2707
2448
|
tooling.push({ file: "package.json", change: `"nifra.zones" lists "${path}" as ${side}` });
|
|
2708
2449
|
}
|
|
2709
2450
|
}
|
|
2710
|
-
// `nifra check` reads the declaration statically; without it, a later routes/ or config would zone the app.
|
|
2711
2451
|
const kind = api && createKindProbe(root)(root).kind === undefined ? declareApiKind(ts, root) : {};
|
|
2712
2452
|
if (kind.issue !== undefined)
|
|
2713
2453
|
issues.push({ file: "package.json", reason: kind.issue });
|
|
@@ -2736,7 +2476,6 @@ async function planLayoutMigration(appRoot, options = {}) {
|
|
|
2736
2476
|
};
|
|
2737
2477
|
return { root, original, final, toolingWrites, scriptWrites, moves, report };
|
|
2738
2478
|
}
|
|
2739
|
-
/** Apply a planned migration in the same order the command historically used. */
|
|
2740
2479
|
function applyLayoutMigration(plan) {
|
|
2741
2480
|
const { root, original, final, toolingWrites, scriptWrites, moves } = plan;
|
|
2742
2481
|
for (const [file, text] of toolingWrites)
|
|
@@ -2778,11 +2517,6 @@ export async function migrateLayout(appRoot, options = {}) {
|
|
|
2778
2517
|
: entry),
|
|
2779
2518
|
};
|
|
2780
2519
|
}
|
|
2781
|
-
/**
|
|
2782
|
-
* Split one route file in place: its server-only exports, and the code only they use, move into its
|
|
2783
|
-
* `x.backend.ts`, created or merged into. Returns the files written, app-relative; nothing for a file
|
|
2784
|
-
* that exports nothing for the server. Refuses, naming why, a split that would need a decision.
|
|
2785
|
-
*/
|
|
2786
2520
|
export async function splitRouteFile(appRoot, file, options = {}) {
|
|
2787
2521
|
if (!/\.[jt]sx$/.test(file))
|
|
2788
2522
|
return [];
|
|
@@ -2807,7 +2541,6 @@ export async function splitRouteFile(appRoot, file, options = {}) {
|
|
|
2807
2541
|
writeFileSync(join(root, sharedFile), result.shared);
|
|
2808
2542
|
return [file, backendFile, sharedFile];
|
|
2809
2543
|
}
|
|
2810
|
-
/** The app's `"nifra": { "zones": ... }`, so folders it already zones are left where they are. */
|
|
2811
2544
|
function appZones(root) {
|
|
2812
2545
|
const manifest = join(root, "package.json");
|
|
2813
2546
|
if (!existsSync(manifest))
|
|
@@ -2824,7 +2557,6 @@ function appZones(root) {
|
|
|
2824
2557
|
return zones === undefined ? {} : parseZoneConfig(zones, manifest);
|
|
2825
2558
|
}
|
|
2826
2559
|
const matchers = new Map();
|
|
2827
|
-
/** Whether a tsconfig `include` or `exclude` entry takes in `file` (app-relative), the way tsc reads it. */
|
|
2828
2560
|
function matches(pattern, file) {
|
|
2829
2561
|
let match = matchers.get(pattern);
|
|
2830
2562
|
if (match === undefined) {
|
|
@@ -2855,7 +2587,6 @@ const TS_FILE = /\.(?:[cm]?ts|tsx)$/;
|
|
|
2855
2587
|
const inProgram = (lists, file) => lists.files.includes(file) ||
|
|
2856
2588
|
(lists.include.some((pattern) => matches(pattern, file)) &&
|
|
2857
2589
|
!lists.exclude.some((pattern) => matches(pattern, file)));
|
|
2858
|
-
/** `pattern` under the zone folder `to` landed in, when that reaches `to`; else `to` itself. */
|
|
2859
2590
|
function twinEntry(pattern, to) {
|
|
2860
2591
|
const slash = to.indexOf("/");
|
|
2861
2592
|
if (pattern === undefined || slash === -1)
|
|
@@ -2866,13 +2597,6 @@ function twinEntry(pattern, to) {
|
|
|
2866
2597
|
return to;
|
|
2867
2598
|
return bare === pattern ? twin : `./${twin}`;
|
|
2868
2599
|
}
|
|
2869
|
-
/**
|
|
2870
|
-
* Keep every TypeScript file in, or out of, each app-root `tsconfig*.json` program as it was. An entry
|
|
2871
|
-
* naming a moved file follows it; a folder or glob that took a moved file in gains its twin under the
|
|
2872
|
-
* zone folder (`src` -> `frontend/src`), and one that kept a file out does the same in `exclude`. So a
|
|
2873
|
-
* test run under its own config's types, or a folder left out on purpose, stays out after the move,
|
|
2874
|
-
* and code the config checked stays checked. An `include` or `exclude` a config inherits is not edited.
|
|
2875
|
-
*/
|
|
2876
2600
|
function followTsconfigs(ts, root, moves, layout) {
|
|
2877
2601
|
const files = new Map();
|
|
2878
2602
|
const issues = [];
|
|
@@ -2880,7 +2604,6 @@ function followTsconfigs(ts, root, moves, layout) {
|
|
|
2880
2604
|
const names = readdirSync(root)
|
|
2881
2605
|
.filter((name) => TSCONFIG.test(name) && isFile(join(root, name)))
|
|
2882
2606
|
.sort(codeUnitOrder);
|
|
2883
|
-
// Each config's lists before and after the move, for the configs that extend it.
|
|
2884
2607
|
const settled = new Map();
|
|
2885
2608
|
const pending = [...names];
|
|
2886
2609
|
for (let progress = true; progress && pending.length > 0;) {
|
|
@@ -2956,7 +2679,6 @@ function followTsconfigs(ts, root, moves, layout) {
|
|
|
2956
2679
|
return [...inherited[when][key]];
|
|
2957
2680
|
if (extendsOther)
|
|
2958
2681
|
return undefined;
|
|
2959
|
-
// tsc's default: everything below the config, unless `files` lists the program.
|
|
2960
2682
|
return key === "exclude" ? [] : own.has("files") ? [] : ["**/*"];
|
|
2961
2683
|
};
|
|
2962
2684
|
const filesOf = (when) => (own.get("files")?.[when] ?? []).map((entry) => posix.normalize(entry));
|
|
@@ -2988,8 +2710,6 @@ function followTsconfigs(ts, root, moves, layout) {
|
|
|
2988
2710
|
let stranded;
|
|
2989
2711
|
let strandedCount = 0;
|
|
2990
2712
|
const quiet = new Map();
|
|
2991
|
-
// The whole zone folder is the entry that keeps covering code added there later, but only when
|
|
2992
|
-
// it takes in no file the config left out.
|
|
2993
2713
|
const takesInOnlyChecked = (folder) => {
|
|
2994
2714
|
let safe = quiet.get(folder);
|
|
2995
2715
|
if (safe === undefined) {
|
|
@@ -3005,7 +2725,6 @@ function followTsconfigs(ts, root, moves, layout) {
|
|
|
3005
2725
|
if (!inProgram(before, from) || inProgram(after, to))
|
|
3006
2726
|
continue;
|
|
3007
2727
|
if (after.include.some((pattern) => matches(pattern, to))) {
|
|
3008
|
-
// Its new place sits under an `exclude` that did not hold the old one.
|
|
3009
2728
|
stranded ??= { from, to, key: "exclude" };
|
|
3010
2729
|
strandedCount++;
|
|
3011
2730
|
continue;
|
|
@@ -3021,7 +2740,6 @@ function followTsconfigs(ts, root, moves, layout) {
|
|
|
3021
2740
|
if (inProgram(before, from) || !inProgram(after, to))
|
|
3022
2741
|
continue;
|
|
3023
2742
|
let entry = twinEntry(before.exclude.find((pattern) => matches(pattern, from)), to);
|
|
3024
|
-
// A twin that would also drop a file the config checked is too wide: name the file.
|
|
3025
2743
|
if (entry !== to &&
|
|
3026
2744
|
typescript.some((other) => matches(entry, other.to) && inProgram(before, other.from)))
|
|
3027
2745
|
entry = to;
|
|
@@ -3057,7 +2775,6 @@ function followTsconfigs(ts, root, moves, layout) {
|
|
|
3057
2775
|
}
|
|
3058
2776
|
return { files, issues };
|
|
3059
2777
|
}
|
|
3060
|
-
/** `text` with `entries` appended to `array`: one per line in a multi-line array, else inline. */
|
|
3061
2778
|
function appendToArray(source, text, array, entries) {
|
|
3062
2779
|
const quoted = entries.map((entry) => JSON.stringify(entry));
|
|
3063
2780
|
const last = array.elements[array.elements.length - 1];
|
|
@@ -3069,8 +2786,6 @@ function appendToArray(source, text, array, entries) {
|
|
|
3069
2786
|
const trailing = /^\s*,/.exec(text.slice(at));
|
|
3070
2787
|
if (trailing !== null)
|
|
3071
2788
|
at += trailing[0].length;
|
|
3072
|
-
// A multi-line array gets one entry per line at the last element's indent, below any comment that
|
|
3073
|
-
// closes the last element's line; anything else gets them inline.
|
|
3074
2789
|
if (text.slice(array.getStart(source), last.getStart(source)).includes("\n")) {
|
|
3075
2790
|
const lineStart = text.lastIndexOf("\n", last.getStart(source)) + 1;
|
|
3076
2791
|
const indent = text.slice(lineStart, last.getStart(source)).match(/^\s*/)?.[0] ?? "";
|
|
@@ -3090,7 +2805,6 @@ function appendToArray(source, text, array, entries) {
|
|
|
3090
2805
|
const jsonProperty = (ts, object, key) => object.properties.find((property) => ts.isPropertyAssignment(property) &&
|
|
3091
2806
|
ts.isStringLiteral(property.name) &&
|
|
3092
2807
|
property.name.text === key);
|
|
3093
|
-
/** `text` with `entry` as the last property of `object`, on its own line when the object spans lines. */
|
|
3094
2808
|
function insertProperty(source, text, object, entry) {
|
|
3095
2809
|
const open = object.getStart(source);
|
|
3096
2810
|
const last = object.properties[object.properties.length - 1];
|
|
@@ -3103,11 +2817,6 @@ function insertProperty(source, text, object, entry) {
|
|
|
3103
2817
|
const indent = /^[ \t]*/.exec(text.slice(text.lastIndexOf("\n", start) + 1, start))?.[0] ?? "";
|
|
3104
2818
|
return `${text.slice(0, last.end)},\n${indent}${entry}${text.slice(last.end)}`;
|
|
3105
2819
|
}
|
|
3106
|
-
/**
|
|
3107
|
-
* The `nifra.zones` entries that keep `inPlace` files where they are: for each, the widest folder
|
|
3108
|
-
* above it whose zoned files all take its side (a nested package stops the climb), else the file
|
|
3109
|
-
* itself. A file another side's suffix or a server built-in claims counts against a folder.
|
|
3110
|
-
*/
|
|
3111
2820
|
function zoneDeclarations(inPlace, files, text, configured) {
|
|
3112
2821
|
const sides = new Map();
|
|
3113
2822
|
const packages = new Set();
|
|
@@ -3120,7 +2829,6 @@ function zoneDeclarations(inPlace, files, text, configured) {
|
|
|
3120
2829
|
packages.add(dir);
|
|
3121
2830
|
continue;
|
|
3122
2831
|
}
|
|
3123
|
-
// A configured path is more specific than any folder above it, so it never counts against one.
|
|
3124
2832
|
if (isTestFile(file) || file.endsWith(".d.ts") || configured(file))
|
|
3125
2833
|
continue;
|
|
3126
2834
|
const suffix = /\.(frontend|backend|shared|fn)\.[cm]?[jt]sx?$/.exec(file)?.[1];
|
|
@@ -3150,7 +2858,6 @@ function zoneDeclarations(inPlace, files, text, configured) {
|
|
|
3150
2858
|
}
|
|
3151
2859
|
return declared;
|
|
3152
2860
|
}
|
|
3153
|
-
/** The app's package.json with `paths` added to `"nifra": { "zones": ... }`, editing only where they go. */
|
|
3154
2861
|
function declareZones(ts, root, paths) {
|
|
3155
2862
|
const listed = (side) => [...paths].flatMap(([path, of]) => (of === side ? [path] : []));
|
|
3156
2863
|
const setting = `{ ${FOLDER_SIDES.filter((side) => listed(side).length > 0)
|
|
@@ -3160,7 +2867,6 @@ function declareZones(ts, root, paths) {
|
|
|
3160
2867
|
if (!existsSync(path))
|
|
3161
2868
|
return { issue: `there is none in the app; declare "nifra": { "zones": ${setting} } in it` };
|
|
3162
2869
|
let text = readFileSync(path, "utf8");
|
|
3163
|
-
// Parsed again for each edit: an edit moves every offset after it.
|
|
3164
2870
|
const find = (keys) => {
|
|
3165
2871
|
const source = ts.parseJsonText(path, text);
|
|
3166
2872
|
let node = source.statements[0]?.expression;
|
|
@@ -3201,7 +2907,6 @@ function declareZones(ts, root, paths) {
|
|
|
3201
2907
|
}
|
|
3202
2908
|
return { text };
|
|
3203
2909
|
}
|
|
3204
|
-
/** The app's package.json with `"nifra": { "kind": "api" }` added, editing only where it goes. */
|
|
3205
2910
|
function declareApiKind(ts, root) {
|
|
3206
2911
|
const path = join(root, "package.json");
|
|
3207
2912
|
if (!existsSync(path)) {
|
|
@@ -3222,7 +2927,6 @@ function declareApiKind(ts, root) {
|
|
|
3222
2927
|
return { issue: '"nifra" is not an object; add "kind": "api" to it by hand' };
|
|
3223
2928
|
return { text: insertProperty(source, text, nifra.initializer, '"kind": "api"') };
|
|
3224
2929
|
}
|
|
3225
|
-
/** package.json fields whose string values name files from the package folder. */
|
|
3226
2930
|
const PACKAGE_PATH_FIELDS = new Set([
|
|
3227
2931
|
"main",
|
|
3228
2932
|
"module",
|
|
@@ -3236,17 +2940,11 @@ const PACKAGE_PATH_FIELDS = new Set([
|
|
|
3236
2940
|
"files",
|
|
3237
2941
|
"scripts",
|
|
3238
2942
|
]);
|
|
3239
|
-
/**
|
|
3240
|
-
* `text` (a package.json at the app root) with each moved file it names by its exact path, in the
|
|
3241
|
-
* fields above, named at its new path; `./` kept as written. A value with a `*` outside `scripts` is a
|
|
3242
|
-
* pattern over a folder, which no single move settles, so it stays and the path scan reports it.
|
|
3243
|
-
*/
|
|
3244
2943
|
function rewritePackagePaths(ts, text, moves) {
|
|
3245
2944
|
const source = ts.parseJsonText("package.json", text);
|
|
3246
2945
|
const top = source.statements[0]?.expression;
|
|
3247
2946
|
if (top === undefined || !ts.isObjectLiteralExpression(top) || moves.size === 0)
|
|
3248
2947
|
return { text, count: 0 };
|
|
3249
|
-
// Longest first, so `lib/a.test.ts` is not read as `lib/a.ts` with a tail.
|
|
3250
2948
|
const ordered = [...moves].sort(([a], [b]) => b.length - a.length);
|
|
3251
2949
|
const pattern = new RegExp(`(?<![\\w.@/-])(\\./)?(${ordered.map(([from]) => escapeRegExp(from)).join("|")})(?![\\w/-]|\\.\\w)`, "g");
|
|
3252
2950
|
const edits = [];
|
|
@@ -3277,7 +2975,6 @@ function rewritePackagePaths(ts, text, moves) {
|
|
|
3277
2975
|
for (const property of node.properties) {
|
|
3278
2976
|
if (!ts.isPropertyAssignment(property))
|
|
3279
2977
|
continue;
|
|
3280
|
-
// `"//x"` keys are comments by npm convention.
|
|
3281
2978
|
if (ts.isStringLiteral(property.name) && property.name.text.startsWith("//"))
|
|
3282
2979
|
continue;
|
|
3283
2980
|
visit(property.initializer, field);
|
|
@@ -3291,7 +2988,6 @@ function rewritePackagePaths(ts, text, moves) {
|
|
|
3291
2988
|
}
|
|
3292
2989
|
return { text: applyEdits(text, edits), count };
|
|
3293
2990
|
}
|
|
3294
|
-
/** Tailwind `content` globs that matched a moved file at its old path and match nothing at its new one. */
|
|
3295
2991
|
function staleContentGlobs(ts, text, moves) {
|
|
3296
2992
|
const globs = [];
|
|
3297
2993
|
const source = ts.createSourceFile("tailwind.config.ts", text, ts.ScriptTarget.Latest, true);
|
|
@@ -3310,10 +3006,6 @@ function staleContentGlobs(ts, text, moves) {
|
|
|
3310
3006
|
const folders = [...new Set(lost.map(({ to }) => to.split("/")[0] ?? to))].sort(codeUnitOrder);
|
|
3311
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)`;
|
|
3312
3008
|
}
|
|
3313
|
-
/**
|
|
3314
|
-
* `.nifra/` holds generated types, dev state and the dev server's launch token; none of it belongs in
|
|
3315
|
-
* git. The new `.gitignore` text when it is not ignored yet, else `undefined`.
|
|
3316
|
-
*/
|
|
3317
3009
|
function ignoreNifraState(root) {
|
|
3318
3010
|
let exitCode = null;
|
|
3319
3011
|
try {
|
|
@@ -3324,25 +3016,20 @@ function ignoreNifraState(root) {
|
|
|
3324
3016
|
}).exitCode;
|
|
3325
3017
|
}
|
|
3326
3018
|
catch {
|
|
3327
|
-
// No git installed: the same as no checkout.
|
|
3328
3019
|
}
|
|
3329
3020
|
if (exitCode === 0)
|
|
3330
3021
|
return undefined;
|
|
3331
3022
|
const path = join(root, ".gitignore");
|
|
3332
3023
|
const current = existsSync(path) ? readFileSync(path, "utf8") : "";
|
|
3333
|
-
// Outside a git checkout `check-ignore` cannot answer; the app's own file decides.
|
|
3334
3024
|
if (exitCode !== 1 && /^\/?\.nifra(?:\/|\*|\/\*\*)?\s*$/m.test(current))
|
|
3335
3025
|
return undefined;
|
|
3336
3026
|
const separator = current === "" || current.endsWith("\n") ? "" : "\n";
|
|
3337
3027
|
return `${current}${separator}.nifra/\n`;
|
|
3338
3028
|
}
|
|
3339
|
-
/** Remove each directory the run emptied, then each parent that leaves empty. A directory that was
|
|
3340
|
-
* empty before the run is the app's own, not the migration's to remove. */
|
|
3341
3029
|
function pruneVacatedDirs(root, vacated) {
|
|
3342
3030
|
const dirs = new Set();
|
|
3343
3031
|
for (const file of vacated)
|
|
3344
3032
|
dirs.add(dirname(join(root, file)));
|
|
3345
|
-
// Deepest first, so a parent is judged after its emptied children are gone.
|
|
3346
3033
|
for (const start of [...dirs].sort((a, b) => b.length - a.length)) {
|
|
3347
3034
|
for (let dir = start; dir !== root && dir.startsWith(`${root}${sep}`); dir = dirname(dir)) {
|
|
3348
3035
|
if (!existsSync(dir) || readdirSync(dir).length > 0)
|