@nifrajs/cli 5.9.0 → 5.11.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.
Files changed (136) hide show
  1. package/CHANGELOG.md +85 -0
  2. package/dist/check-diagnostics.d.ts +1 -0
  3. package/dist/check-diagnostics.d.ts.map +1 -1
  4. package/dist/check-diagnostics.js +4 -0
  5. package/dist/check-diagnostics.js.map +1 -1
  6. package/dist/check-scan.d.ts +1 -0
  7. package/dist/check-scan.d.ts.map +1 -1
  8. package/dist/check-scan.js +8 -1
  9. package/dist/check-scan.js.map +1 -1
  10. package/dist/check.d.ts.map +1 -1
  11. package/dist/check.js +7 -2
  12. package/dist/check.js.map +1 -1
  13. package/dist/cli.d.ts.map +1 -1
  14. package/dist/cli.js +21 -20
  15. package/dist/cli.js.map +1 -1
  16. package/dist/command-catalog.d.ts +1 -5
  17. package/dist/command-catalog.d.ts.map +1 -1
  18. package/dist/command-catalog.js +2 -21
  19. package/dist/command-catalog.js.map +1 -1
  20. package/dist/doctor.js +2 -2
  21. package/dist/doctor.js.map +1 -1
  22. package/dist/internal/source-facts.d.ts +30 -0
  23. package/dist/internal/source-facts.d.ts.map +1 -1
  24. package/dist/internal/source-facts.js +127 -0
  25. package/dist/internal/source-facts.js.map +1 -1
  26. package/dist/load.d.ts +4 -2
  27. package/dist/load.d.ts.map +1 -1
  28. package/dist/load.js +6 -1
  29. package/dist/load.js.map +1 -1
  30. package/dist/mcp-delegate.d.ts +17 -3
  31. package/dist/mcp-delegate.d.ts.map +1 -1
  32. package/dist/mcp-delegate.js +24 -9
  33. package/dist/mcp-delegate.js.map +1 -1
  34. package/dist/mcp-http.js +1 -1
  35. package/dist/mcp-http.js.map +1 -1
  36. package/dist/mcp-relay.d.ts +37 -0
  37. package/dist/mcp-relay.d.ts.map +1 -0
  38. package/dist/mcp-relay.js +129 -0
  39. package/dist/mcp-relay.js.map +1 -0
  40. package/dist/mcp-tool-meta.d.ts.map +1 -1
  41. package/dist/mcp-tool-meta.js +0 -1
  42. package/dist/mcp-tool-meta.js.map +1 -1
  43. package/dist/mcp.d.ts.map +1 -1
  44. package/dist/mcp.js +55 -1
  45. package/dist/mcp.js.map +1 -1
  46. package/dist/migrate-layout.d.ts.map +1 -1
  47. package/dist/migrate-layout.js +45 -33
  48. package/dist/migrate-layout.js.map +1 -1
  49. package/dist/recipes/5.10.0.d.ts +13 -0
  50. package/dist/recipes/5.10.0.d.ts.map +1 -0
  51. package/dist/recipes/5.10.0.js +45 -0
  52. package/dist/recipes/5.10.0.js.map +1 -0
  53. package/dist/recipes/5.11.0.d.ts +10 -0
  54. package/dist/recipes/5.11.0.d.ts.map +1 -0
  55. package/dist/recipes/5.11.0.js +50 -0
  56. package/dist/recipes/5.11.0.js.map +1 -0
  57. package/dist/recipes/index.d.ts.map +1 -1
  58. package/dist/recipes/index.js +4 -0
  59. package/dist/recipes/index.js.map +1 -1
  60. package/dist/review.d.ts.map +1 -1
  61. package/dist/review.js +3 -0
  62. package/dist/review.js.map +1 -1
  63. package/dist/rules/codes.d.ts +3 -0
  64. package/dist/rules/codes.d.ts.map +1 -1
  65. package/dist/rules/codes.js +3 -0
  66. package/dist/rules/codes.js.map +1 -1
  67. package/dist/rules/explanations.d.ts.map +1 -1
  68. package/dist/rules/explanations.js +15 -0
  69. package/dist/rules/explanations.js.map +1 -1
  70. package/dist/rules/internal-links.d.ts +5 -0
  71. package/dist/rules/internal-links.d.ts.map +1 -0
  72. package/dist/rules/internal-links.js +418 -0
  73. package/dist/rules/internal-links.js.map +1 -0
  74. package/dist/rules/legacy.d.ts.map +1 -1
  75. package/dist/rules/legacy.js +20 -0
  76. package/dist/rules/legacy.js.map +1 -1
  77. package/dist/rules/server-fn-namespace.d.ts +8 -0
  78. package/dist/rules/server-fn-namespace.d.ts.map +1 -0
  79. package/dist/rules/server-fn-namespace.js +43 -0
  80. package/dist/rules/server-fn-namespace.js.map +1 -0
  81. package/dist/rules/source-scan.d.ts +29 -0
  82. package/dist/rules/source-scan.d.ts.map +1 -0
  83. package/dist/rules/source-scan.js +38 -0
  84. package/dist/rules/source-scan.js.map +1 -0
  85. package/dist/rules/text-slot-boolean.d.ts.map +1 -1
  86. package/dist/rules/text-slot-boolean.js +21 -43
  87. package/dist/rules/text-slot-boolean.js.map +1 -1
  88. package/dist/source-rewrites.d.ts +0 -5
  89. package/dist/source-rewrites.d.ts.map +1 -1
  90. package/dist/source-rewrites.js +350 -10
  91. package/dist/source-rewrites.js.map +1 -1
  92. package/dist/typecheck-coverage.d.ts +9 -0
  93. package/dist/typecheck-coverage.d.ts.map +1 -1
  94. package/dist/typecheck-coverage.js +16 -0
  95. package/dist/typecheck-coverage.js.map +1 -1
  96. package/dist/upgrade.d.ts +19 -1
  97. package/dist/upgrade.d.ts.map +1 -1
  98. package/dist/upgrade.js +55 -2
  99. package/dist/upgrade.js.map +1 -1
  100. package/docs/examples.json +21 -9
  101. package/docs/llms-full.txt +800 -262
  102. package/docs/types.json +868 -128
  103. package/package.json +11 -11
  104. package/src/check-diagnostics.ts +5 -0
  105. package/src/check-scan.ts +9 -1
  106. package/src/check.ts +15 -3
  107. package/src/cli.ts +25 -20
  108. package/src/command-catalog.ts +2 -26
  109. package/src/doctor.ts +2 -2
  110. package/src/internal/source-facts.ts +155 -0
  111. package/src/load.ts +15 -3
  112. package/src/mcp-delegate.ts +37 -11
  113. package/src/mcp-http.ts +1 -1
  114. package/src/mcp-relay.ts +155 -0
  115. package/src/mcp-tool-meta.ts +0 -1
  116. package/src/mcp.ts +51 -1
  117. package/src/migrate-layout.ts +61 -29
  118. package/src/recipes/5.10.0.ts +46 -0
  119. package/src/recipes/5.11.0.ts +51 -0
  120. package/src/recipes/index.ts +4 -0
  121. package/src/review.ts +3 -0
  122. package/src/rules/codes.ts +3 -0
  123. package/src/rules/explanations.ts +15 -0
  124. package/src/rules/internal-links.ts +490 -0
  125. package/src/rules/legacy.ts +21 -0
  126. package/src/rules/server-fn-namespace.ts +47 -0
  127. package/src/rules/source-scan.ts +66 -0
  128. package/src/rules/text-slot-boolean.ts +26 -42
  129. package/src/source-rewrites.ts +383 -12
  130. package/src/typecheck-coverage.ts +23 -0
  131. package/src/upgrade.ts +71 -3
  132. package/dist/sync-routes.d.ts +0 -29
  133. package/dist/sync-routes.d.ts.map +0 -1
  134. package/dist/sync-routes.js +0 -56
  135. package/dist/sync-routes.js.map +0 -1
  136. package/src/sync-routes.ts +0 -70
package/src/upgrade.ts CHANGED
@@ -78,13 +78,16 @@ import { getSourceRewrite } from "./source-rewrites.ts"
78
78
 
79
79
  export interface UpgradeOptions {
80
80
  /** Target version, e.g. "1.8.0". Required unless `list` is set. */
81
- readonly version?: string
81
+ readonly version?: string | undefined
82
82
  /** Apply the edits. Without it, the run is a dry-run that only prints the plan. */
83
83
  readonly write?: boolean
84
84
  /** Emit a machine-readable plan/result instead of the human report. */
85
85
  readonly json?: boolean
86
86
  /** Print the available target versions and return. */
87
87
  readonly list?: boolean
88
+ /** The version to upgrade from, in place of the lowest one the project's pins name. After the
89
+ * pins were bumped by hand, the recipes they skipped apply again only from this version. */
90
+ readonly from?: string | undefined
88
91
  /** After `--write`, run `nifra check` and fail the command if it fails, once the install matches
89
92
  * the target; until then, print the install command to run first. Default true. */
90
93
  readonly verify?: boolean
@@ -1888,11 +1891,76 @@ async function uncoveredContracts(cwd: string): Promise<string[]> {
1888
1891
  const uncoveredContractsNote = (routes: readonly string[]): string =>
1889
1892
  `note: contracts.lock.json records no request schema for ${routes.slice(0, 3).join(", ")}${routes.length > 3 ? ` and ${routes.length - 3} more` : ""}. Where this release reads a schema the installed one could not, \`nifra check\` reports it as newly covered: review it, then run \`nifra contracts snapshot\`.`
1890
1893
 
1894
+ const UPGRADE_SWITCHES: readonly string[] = [
1895
+ "--write",
1896
+ "--json",
1897
+ "--list",
1898
+ "--exact",
1899
+ "--allow-downgrade",
1900
+ "--allow-linked-skew",
1901
+ "--no-verify",
1902
+ "--no-hooks",
1903
+ ]
1904
+
1905
+ export interface UpgradeArgs {
1906
+ readonly version: string | undefined
1907
+ readonly from: string | undefined
1908
+ readonly write: boolean
1909
+ readonly json: boolean
1910
+ readonly list: boolean
1911
+ readonly exact: boolean
1912
+ readonly allowDowngrade: boolean
1913
+ readonly allowLinkedSkew: boolean
1914
+ readonly verify: boolean
1915
+ readonly hooks: boolean
1916
+ }
1917
+
1918
+ /** The arguments of `nifra upgrade`. Anything else is refused: an ignored `--from` would plan the
1919
+ * upgrade from the wrong version and still exit 0. */
1920
+ export function parseUpgradeArgs(args: readonly string[]): UpgradeArgs {
1921
+ const on = new Set<string>()
1922
+ let version: string | undefined
1923
+ let from: string | undefined
1924
+ for (let i = 0; i < args.length; i++) {
1925
+ const arg = args[i] ?? ""
1926
+ const equals = arg.startsWith("--") ? arg.indexOf("=") : -1
1927
+ const name = equals === -1 ? arg : arg.slice(0, equals)
1928
+ if (name === "--from") {
1929
+ const value = equals === -1 ? args[++i] : arg.slice(equals + 1)
1930
+ if (value === undefined || value === "" || (equals === -1 && value.startsWith("-")))
1931
+ throw new Error("[nifra] --from needs a version, e.g. --from 5.8.0")
1932
+ from = value
1933
+ } else if (UPGRADE_SWITCHES.includes(arg)) on.add(arg)
1934
+ else if (arg.startsWith("-")) {
1935
+ throw new Error(`[nifra] unknown option: ${arg} - see \`nifra help\``)
1936
+ } else if (version === undefined) version = arg
1937
+ else throw new Error(`[nifra] unexpected argument: ${arg} - see \`nifra help\``)
1938
+ }
1939
+ return {
1940
+ version,
1941
+ from,
1942
+ write: on.has("--write"),
1943
+ json: on.has("--json"),
1944
+ list: on.has("--list"),
1945
+ exact: on.has("--exact"),
1946
+ allowDowngrade: on.has("--allow-downgrade"),
1947
+ allowLinkedSkew: on.has("--allow-linked-skew"),
1948
+ verify: !on.has("--no-verify"),
1949
+ hooks: !on.has("--no-hooks"),
1950
+ }
1951
+ }
1952
+
1891
1953
  export async function runUpgrade(cwd: string, options: UpgradeOptions): Promise<boolean> {
1892
1954
  const cliVersion = options.cliVersion ?? listRecipeVersions().at(-1) ?? "0.0.0"
1893
1955
  const targets = upgradeTargets(cliVersion)
1956
+ if (options.from !== undefined && !RELEASE_VERSION.test(options.from)) {
1957
+ console.error(
1958
+ `[nifra] --from ${JSON.stringify(options.from)} is not a release version, e.g. ${cliVersion}`,
1959
+ )
1960
+ return false
1961
+ }
1894
1962
  if (options.list) {
1895
- const installed = installedGroupVersion(cwd)
1963
+ const installed = options.from ?? installedGroupVersion(cwd)
1896
1964
  const notes = Object.fromEntries(targets.map((v) => [v, getRecipe(v)?.notes ?? []]))
1897
1965
  const ahead = targets.filter((v) => installed === null || compareSemverSpec(v, installed) > 0)
1898
1966
  const unaffected = unaffectedReleases(ahead, groupSurface(cwd))
@@ -1952,7 +2020,7 @@ export async function runUpgrade(cwd: string, options: UpgradeOptions): Promise<
1952
2020
  const write = options.write === true
1953
2021
  const allowDowngrade = options.allowDowngrade === true
1954
2022
  const exact = options.exact === true
1955
- const from = installedGroupVersion(cwd)
2023
+ const from = options.from ?? installedGroupVersion(cwd)
1956
2024
  const recipe = chainRecipes(version, from)
1957
2025
 
1958
2026
  // Fail-closed on rollback: a dry pass writes nothing, so we can refuse before any package.json is touched.
@@ -1,29 +0,0 @@
1
- /**
2
- * `nifra sync-routes` - regenerate the route-search types (`nifra-routes.d.ts`) from the current `routes/`
3
- * tree, so `navigate({ to, search })` is typed against each static route's `searchSchema`. It runs
4
- * `@nifrajs/web`'s `generateRouteSearchTypes` over the discovered routes and writes the `.d.ts` at the
5
- * project root; include it in your tsconfig's `include`. Because it is regenerated from the route files, a
6
- * stale navigate search shape becomes a `tsc` error - run it after adding/renaming a route or changing a
7
- * `searchSchema` (a pure file write, no build). A dynamic route (`:param`/catch-all) is omitted, since its
8
- * pattern is not a concrete `to`; those keep the loose object form.
9
- */
10
- /** The committed route-types file `sync-routes` writes, so `nifra check` can recognise + drift-check it. */
11
- export declare const ROUTE_TYPES_FILE = "nifra-routes.d.ts";
12
- export interface SyncRoutesResult {
13
- /** The written `.d.ts` (relative to the project root). */
14
- readonly file: string;
15
- /** True when the file's content changed and it was rewritten. */
16
- readonly changed: boolean;
17
- /** How many static routes got a typed entry. */
18
- readonly typedRoutes: number;
19
- }
20
- /**
21
- * Generate the route-search types `.d.ts` from `<cwd>/routes` (the nifra convention). Returns
22
- * `null` when there is no `routes/` directory (an API-only project - nothing to type). Never loads
23
- * `backend/framework.ts`: it needs only the route tree, so it works before a build and on a broken
24
- * framework config.
25
- */
26
- export declare function syncRouteTypes(cwd: string): Promise<SyncRoutesResult | null>;
27
- /** CLI entry: write/refresh the route-types file and print a one-line summary + the tsconfig reminder. */
28
- export declare function runSyncRoutes(cwd: string): Promise<boolean>;
29
- //# sourceMappingURL=sync-routes.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"sync-routes.d.ts","sourceRoot":"","sources":["../src/sync-routes.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAKH,4GAA4G;AAC5G,eAAO,MAAM,gBAAgB,sBAAsB,CAAA;AAEnD,MAAM,WAAW,gBAAgB;IAC/B,0DAA0D;IAC1D,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,iEAAiE;IACjE,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAA;IACzB,gDAAgD;IAChD,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAA;CAC7B;AAED;;;;;GAKG;AACH,wBAAsB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,GAAG,IAAI,CAAC,CAkBlF;AAED,0GAA0G;AAC1G,wBAAsB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAiBjE"}
@@ -1,56 +0,0 @@
1
- /**
2
- * `nifra sync-routes` - regenerate the route-search types (`nifra-routes.d.ts`) from the current `routes/`
3
- * tree, so `navigate({ to, search })` is typed against each static route's `searchSchema`. It runs
4
- * `@nifrajs/web`'s `generateRouteSearchTypes` over the discovered routes and writes the `.d.ts` at the
5
- * project root; include it in your tsconfig's `include`. Because it is regenerated from the route files, a
6
- * stale navigate search shape becomes a `tsc` error - run it after adding/renaming a route or changing a
7
- * `searchSchema` (a pure file write, no build). A dynamic route (`:param`/catch-all) is omitted, since its
8
- * pattern is not a concrete `to`; those keep the loose object form.
9
- */
10
- import { existsSync } from "node:fs";
11
- import { join } from "node:path";
12
- /** The committed route-types file `sync-routes` writes, so `nifra check` can recognise + drift-check it. */
13
- export const ROUTE_TYPES_FILE = "nifra-routes.d.ts";
14
- /**
15
- * Generate the route-search types `.d.ts` from `<cwd>/routes` (the nifra convention). Returns
16
- * `null` when there is no `routes/` directory (an API-only project - nothing to type). Never loads
17
- * `backend/framework.ts`: it needs only the route tree, so it works before a build and on a broken
18
- * framework config.
19
- */
20
- export async function syncRouteTypes(cwd) {
21
- const routesDir = join(cwd, "routes");
22
- if (!existsSync(routesDir))
23
- return null;
24
- const { discoverRoutes } = await import("@nifrajs/web/fs");
25
- const { generateRouteSearchTypes } = await import("@nifrajs/web");
26
- const manifest = discoverRoutes(routesDir);
27
- const code = generateRouteSearchTypes(manifest, {
28
- // A route file is relative to routesDir (`reports.tsx`, `users/index.tsx`); the `.d.ts` sits at the
29
- // project root, so the `typeof import(...)` specifier is `./routes/<file без extension>`.
30
- resolve: (file) => `./routes/${file.replace(/\.[^./]+$/, "")}`,
31
- });
32
- const outFile = join(cwd, ROUTE_TYPES_FILE);
33
- const existing = await Bun.file(outFile)
34
- .text()
35
- .catch(() => "");
36
- const changed = existing !== code;
37
- if (changed)
38
- await Bun.write(outFile, code);
39
- return { file: ROUTE_TYPES_FILE, changed, typedRoutes: (code.match(/: SearchOf</g) ?? []).length };
40
- }
41
- /** CLI entry: write/refresh the route-types file and print a one-line summary + the tsconfig reminder. */
42
- export async function runSyncRoutes(cwd) {
43
- const result = await syncRouteTypes(cwd);
44
- if (result === null) {
45
- console.log("nifra sync-routes: no routes/ directory found. Run from your project root.");
46
- return true;
47
- }
48
- const n = result.typedRoutes;
49
- const routes = `${n} typed route${n === 1 ? "" : "s"}`;
50
- console.log(result.changed
51
- ? `✓ wrote ${result.file} (${routes})`
52
- : `• ${result.file} already in sync (${routes})`);
53
- console.log(" Include it in your tsconfig's `include` so `navigate({ to, search })` is typed per route.");
54
- return true;
55
- }
56
- //# sourceMappingURL=sync-routes.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"sync-routes.js","sourceRoot":"","sources":["../src/sync-routes.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AACpC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAEhC,4GAA4G;AAC5G,MAAM,CAAC,MAAM,gBAAgB,GAAG,mBAAmB,CAAA;AAWnD;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAAC,GAAW;IAC9C,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAA;IACrC,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC;QAAE,OAAO,IAAI,CAAA;IACvC,MAAM,EAAE,cAAc,EAAE,GAAG,MAAM,MAAM,CAAC,iBAAiB,CAAC,CAAA;IAC1D,MAAM,EAAE,wBAAwB,EAAE,GAAG,MAAM,MAAM,CAAC,cAAc,CAAC,CAAA;IACjE,MAAM,QAAQ,GAAG,cAAc,CAAC,SAAS,CAAC,CAAA;IAC1C,MAAM,IAAI,GAAG,wBAAwB,CAAC,QAAQ,EAAE;QAC9C,oGAAoG;QACpG,0FAA0F;QAC1F,OAAO,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,YAAY,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE,EAAE,CAAC,EAAE;KAC/D,CAAC,CAAA;IACF,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,EAAE,gBAAgB,CAAC,CAAA;IAC3C,MAAM,QAAQ,GAAG,MAAM,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC;SACrC,IAAI,EAAE;SACN,KAAK,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,CAAA;IAClB,MAAM,OAAO,GAAG,QAAQ,KAAK,IAAI,CAAA;IACjC,IAAI,OAAO;QAAE,MAAM,GAAG,CAAC,KAAK,CAAC,OAAO,EAAE,IAAI,CAAC,CAAA;IAC3C,OAAO,EAAE,IAAI,EAAE,gBAAgB,EAAE,OAAO,EAAE,WAAW,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,cAAc,CAAC,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE,CAAA;AACpG,CAAC;AAED,0GAA0G;AAC1G,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,GAAW;IAC7C,MAAM,MAAM,GAAG,MAAM,cAAc,CAAC,GAAG,CAAC,CAAA;IACxC,IAAI,MAAM,KAAK,IAAI,EAAE,CAAC;QACpB,OAAO,CAAC,GAAG,CAAC,4EAA4E,CAAC,CAAA;QACzF,OAAO,IAAI,CAAA;IACb,CAAC;IACD,MAAM,CAAC,GAAG,MAAM,CAAC,WAAW,CAAA;IAC5B,MAAM,MAAM,GAAG,GAAG,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,CAAA;IACtD,OAAO,CAAC,GAAG,CACT,MAAM,CAAC,OAAO;QACZ,CAAC,CAAC,WAAW,MAAM,CAAC,IAAI,KAAK,MAAM,GAAG;QACtC,CAAC,CAAC,KAAK,MAAM,CAAC,IAAI,qBAAqB,MAAM,GAAG,CACnD,CAAA;IACD,OAAO,CAAC,GAAG,CACT,6FAA6F,CAC9F,CAAA;IACD,OAAO,IAAI,CAAA;AACb,CAAC"}
@@ -1,70 +0,0 @@
1
- /**
2
- * `nifra sync-routes` - regenerate the route-search types (`nifra-routes.d.ts`) from the current `routes/`
3
- * tree, so `navigate({ to, search })` is typed against each static route's `searchSchema`. It runs
4
- * `@nifrajs/web`'s `generateRouteSearchTypes` over the discovered routes and writes the `.d.ts` at the
5
- * project root; include it in your tsconfig's `include`. Because it is regenerated from the route files, a
6
- * stale navigate search shape becomes a `tsc` error - run it after adding/renaming a route or changing a
7
- * `searchSchema` (a pure file write, no build). A dynamic route (`:param`/catch-all) is omitted, since its
8
- * pattern is not a concrete `to`; those keep the loose object form.
9
- */
10
-
11
- import { existsSync } from "node:fs"
12
- import { join } from "node:path"
13
-
14
- /** The committed route-types file `sync-routes` writes, so `nifra check` can recognise + drift-check it. */
15
- export const ROUTE_TYPES_FILE = "nifra-routes.d.ts"
16
-
17
- export interface SyncRoutesResult {
18
- /** The written `.d.ts` (relative to the project root). */
19
- readonly file: string
20
- /** True when the file's content changed and it was rewritten. */
21
- readonly changed: boolean
22
- /** How many static routes got a typed entry. */
23
- readonly typedRoutes: number
24
- }
25
-
26
- /**
27
- * Generate the route-search types `.d.ts` from `<cwd>/routes` (the nifra convention). Returns
28
- * `null` when there is no `routes/` directory (an API-only project - nothing to type). Never loads
29
- * `backend/framework.ts`: it needs only the route tree, so it works before a build and on a broken
30
- * framework config.
31
- */
32
- export async function syncRouteTypes(cwd: string): Promise<SyncRoutesResult | null> {
33
- const routesDir = join(cwd, "routes")
34
- if (!existsSync(routesDir)) return null
35
- const { discoverRoutes } = await import("@nifrajs/web/fs")
36
- const { generateRouteSearchTypes } = await import("@nifrajs/web")
37
- const manifest = discoverRoutes(routesDir)
38
- const code = generateRouteSearchTypes(manifest, {
39
- // A route file is relative to routesDir (`reports.tsx`, `users/index.tsx`); the `.d.ts` sits at the
40
- // project root, so the `typeof import(...)` specifier is `./routes/<file без extension>`.
41
- resolve: (file) => `./routes/${file.replace(/\.[^./]+$/, "")}`,
42
- })
43
- const outFile = join(cwd, ROUTE_TYPES_FILE)
44
- const existing = await Bun.file(outFile)
45
- .text()
46
- .catch(() => "")
47
- const changed = existing !== code
48
- if (changed) await Bun.write(outFile, code)
49
- return { file: ROUTE_TYPES_FILE, changed, typedRoutes: (code.match(/: SearchOf</g) ?? []).length }
50
- }
51
-
52
- /** CLI entry: write/refresh the route-types file and print a one-line summary + the tsconfig reminder. */
53
- export async function runSyncRoutes(cwd: string): Promise<boolean> {
54
- const result = await syncRouteTypes(cwd)
55
- if (result === null) {
56
- console.log("nifra sync-routes: no routes/ directory found. Run from your project root.")
57
- return true
58
- }
59
- const n = result.typedRoutes
60
- const routes = `${n} typed route${n === 1 ? "" : "s"}`
61
- console.log(
62
- result.changed
63
- ? `✓ wrote ${result.file} (${routes})`
64
- : `• ${result.file} already in sync (${routes})`,
65
- )
66
- console.log(
67
- " Include it in your tsconfig's `include` so `navigate({ to, search })` is typed per route.",
68
- )
69
- return true
70
- }