@nifrajs/cli 5.9.0 → 5.10.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 (104) hide show
  1. package/CHANGELOG.md +57 -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 +12 -8
  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/internal/source-facts.d.ts +30 -0
  21. package/dist/internal/source-facts.d.ts.map +1 -1
  22. package/dist/internal/source-facts.js +127 -0
  23. package/dist/internal/source-facts.js.map +1 -1
  24. package/dist/load.d.ts +2 -0
  25. package/dist/load.d.ts.map +1 -1
  26. package/dist/load.js +5 -0
  27. package/dist/load.js.map +1 -1
  28. package/dist/mcp-http.js +1 -1
  29. package/dist/mcp-http.js.map +1 -1
  30. package/dist/mcp-tool-meta.d.ts.map +1 -1
  31. package/dist/mcp-tool-meta.js +0 -1
  32. package/dist/mcp-tool-meta.js.map +1 -1
  33. package/dist/recipes/5.10.0.d.ts +12 -0
  34. package/dist/recipes/5.10.0.d.ts.map +1 -0
  35. package/dist/recipes/5.10.0.js +44 -0
  36. package/dist/recipes/5.10.0.js.map +1 -0
  37. package/dist/recipes/index.d.ts.map +1 -1
  38. package/dist/recipes/index.js +2 -0
  39. package/dist/recipes/index.js.map +1 -1
  40. package/dist/review.d.ts.map +1 -1
  41. package/dist/review.js +3 -0
  42. package/dist/review.js.map +1 -1
  43. package/dist/rules/codes.d.ts +3 -0
  44. package/dist/rules/codes.d.ts.map +1 -1
  45. package/dist/rules/codes.js +3 -0
  46. package/dist/rules/codes.js.map +1 -1
  47. package/dist/rules/explanations.d.ts.map +1 -1
  48. package/dist/rules/explanations.js +15 -0
  49. package/dist/rules/explanations.js.map +1 -1
  50. package/dist/rules/internal-links.d.ts +5 -0
  51. package/dist/rules/internal-links.d.ts.map +1 -0
  52. package/dist/rules/internal-links.js +418 -0
  53. package/dist/rules/internal-links.js.map +1 -0
  54. package/dist/rules/legacy.d.ts.map +1 -1
  55. package/dist/rules/legacy.js +20 -0
  56. package/dist/rules/legacy.js.map +1 -1
  57. package/dist/rules/server-fn-namespace.d.ts +8 -0
  58. package/dist/rules/server-fn-namespace.d.ts.map +1 -0
  59. package/dist/rules/server-fn-namespace.js +43 -0
  60. package/dist/rules/server-fn-namespace.js.map +1 -0
  61. package/dist/rules/source-scan.d.ts +29 -0
  62. package/dist/rules/source-scan.d.ts.map +1 -0
  63. package/dist/rules/source-scan.js +38 -0
  64. package/dist/rules/source-scan.js.map +1 -0
  65. package/dist/rules/text-slot-boolean.d.ts.map +1 -1
  66. package/dist/rules/text-slot-boolean.js +21 -43
  67. package/dist/rules/text-slot-boolean.js.map +1 -1
  68. package/dist/source-rewrites.d.ts.map +1 -1
  69. package/dist/source-rewrites.js +118 -3
  70. package/dist/source-rewrites.js.map +1 -1
  71. package/dist/typecheck-coverage.d.ts +9 -0
  72. package/dist/typecheck-coverage.d.ts.map +1 -1
  73. package/dist/typecheck-coverage.js +16 -0
  74. package/dist/typecheck-coverage.js.map +1 -1
  75. package/docs/examples.json +19 -7
  76. package/docs/llms-full.txt +555 -200
  77. package/docs/types.json +560 -115
  78. package/package.json +11 -11
  79. package/src/check-diagnostics.ts +5 -0
  80. package/src/check-scan.ts +9 -1
  81. package/src/check.ts +15 -3
  82. package/src/cli.ts +16 -8
  83. package/src/command-catalog.ts +2 -26
  84. package/src/internal/source-facts.ts +155 -0
  85. package/src/load.ts +9 -0
  86. package/src/mcp-http.ts +1 -1
  87. package/src/mcp-tool-meta.ts +0 -1
  88. package/src/recipes/5.10.0.ts +45 -0
  89. package/src/recipes/index.ts +2 -0
  90. package/src/review.ts +3 -0
  91. package/src/rules/codes.ts +3 -0
  92. package/src/rules/explanations.ts +15 -0
  93. package/src/rules/internal-links.ts +490 -0
  94. package/src/rules/legacy.ts +21 -0
  95. package/src/rules/server-fn-namespace.ts +47 -0
  96. package/src/rules/source-scan.ts +66 -0
  97. package/src/rules/text-slot-boolean.ts +26 -42
  98. package/src/source-rewrites.ts +133 -4
  99. package/src/typecheck-coverage.ts +23 -0
  100. package/dist/sync-routes.d.ts +0 -29
  101. package/dist/sync-routes.d.ts.map +0 -1
  102. package/dist/sync-routes.js +0 -56
  103. package/dist/sync-routes.js.map +0 -1
  104. package/src/sync-routes.ts +0 -70
@@ -1,10 +1,8 @@
1
- import { type Diagnostic, diagnostic } from "../diagnostics.ts"
2
- import { createSourceFacts } from "../internal/source-facts.ts"
3
- import { commentBlockExpectation } from "./comment-markers.ts"
1
+ import { diagnostic } from "../diagnostics.ts"
4
2
  import type { CheckRule } from "./index.ts"
5
- import { loadRuleTypeScript } from "./typescript.ts"
3
+ import { scanSources } from "./source-scan.ts"
6
4
 
7
- const TEXT_SLOT_BOOLEAN_EXPECTATION = ["NF-C055", "text-slot-boolean"]
5
+ const TEXT_SLOT_BOOLEAN_EXPECTATION = ["NF-C055", "text-slot-boolean"] as const
8
6
  const ZOD_IMPORT = /from\s+["']zod(?:\/v4)?(?:\/mini)?["']/
9
7
  const VALUE_IN = { query: "query", headers: "header", cookies: "cookie" } as const
10
8
 
@@ -16,41 +14,27 @@ const VALUE_IN = { query: "query", headers: "header", cookies: "cookie" } as con
16
14
  export const textSlotBooleanRule: CheckRule = {
17
15
  code: "NF-C055",
18
16
  title: "zod coerce.boolean() reads request text",
19
- async scan(ctx) {
20
- const loaded = await loadRuleTypeScript(ctx)
21
- if (loaded.diagnostics !== undefined) return loaded.diagnostics
22
- const ts = loaded.compiler
23
- if (ts === undefined) return []
24
- const facts = createSourceFacts(ts)
25
- const findings: Diagnostic[] = []
26
- for (const file of ctx.project.source.files) {
27
- if (!/\.[cm]?tsx?$/.test(file)) continue
28
- const text = ctx.project.source.read(file)
29
- if (text === undefined || !text.includes("coerce") || !ZOD_IMPORT.test(text)) continue
30
- const tree = facts.parse(file, text)
31
- if (tree === undefined) continue
32
- const lines = text.split("\n")
33
- for (const found of facts.textSlotBooleanCoercions(tree)) {
34
- const line = tree.getLineAndCharacterOfPosition(found.position).line + 1
35
- if (commentBlockExpectation(lines, line, TEXT_SLOT_BOOLEAN_EXPECTATION) !== undefined)
36
- continue
37
- findings.push(
38
- diagnostic({
39
- code: "NF-C055",
40
- severity: "warn",
41
- file,
42
- line,
43
- message: `\`coerce.boolean()\` in the \`${found.slot}\` schema of ${found.method} ${found.path} reads every non-empty value as true, "false" and "0" included: a ${VALUE_IN[found.slot]} value is always text. Use \`z.stringbool()\`, which reads "false", "0", "no" and "off" as false and refuses text it does not know.`,
44
- evidence: [
45
- "zod's coerce.boolean() is Boolean(value): any non-empty string is true",
46
- `${found.slot} values arrive as text, so "false" never reaches the schema as false`,
47
- "a use kept on purpose: `// nifra-expect NF-C055: <reason>` above the call",
48
- ],
49
- verify: "nifra check",
50
- }),
51
- )
52
- }
53
- }
54
- return findings
55
- },
17
+ scan: (ctx) =>
18
+ scanSources(ctx, {
19
+ expectation: TEXT_SLOT_BOOLEAN_EXPECTATION,
20
+ mayMatch: (text) => text.includes("coerce") && ZOD_IMPORT.test(text),
21
+ find: (facts, tree) =>
22
+ facts.textSlotBooleanCoercions(tree).map((found) => ({
23
+ position: found.position,
24
+ report: (line) =>
25
+ diagnostic({
26
+ code: "NF-C055",
27
+ severity: "warn",
28
+ file: tree.fileName,
29
+ line,
30
+ message: `\`coerce.boolean()\` in the \`${found.slot}\` schema of ${found.method} ${found.path} reads every non-empty value as true, "false" and "0" included: a ${VALUE_IN[found.slot]} value is always text. Use \`z.stringbool()\`, which reads "false", "0", "no" and "off" as false and refuses text it does not know.`,
31
+ evidence: [
32
+ "zod's coerce.boolean() is Boolean(value): any non-empty string is true",
33
+ `${found.slot} values arrive as text, so "false" never reaches the schema as false`,
34
+ "a use kept on purpose: `// nifra-expect NF-C055: <reason>` above the call",
35
+ ],
36
+ verify: "nifra check",
37
+ }),
38
+ })),
39
+ }),
56
40
  }
@@ -73,13 +73,19 @@ function propertyName(ts: typeof TS, name: TS.PropertyName): string | undefined
73
73
  * `serveStatic()`; and `signals` on `@nifrajs/node`'s and `@nifrajs/deno`'s `serve()` becomes
74
74
  * `gracefulSignals`.
75
75
  */
76
- function rewriteServeStatic(ts: typeof TS, file: string, text: string): RewriteResult {
76
+ /** Parse `text` as `file`, with the line lookup a rewrite lists its manual sites by. */
77
+ function parseSource(ts: typeof TS, file: string, text: string) {
77
78
  const tree = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true, scriptKindOf(ts, file))
79
+ const lineOf = (node: TS.Node): number =>
80
+ tree.getLineAndCharacterOfPosition(node.getStart(tree)).line + 1
81
+ return { tree, lineOf }
82
+ }
83
+
84
+ function rewriteServeStatic(ts: typeof TS, file: string, text: string): RewriteResult {
85
+ const { tree, lineOf } = parseSource(ts, file, text)
78
86
  const edits: Edit[] = []
79
87
  const manual: ManualSite[] = []
80
88
  let count = 0
81
- const lineOf = (node: TS.Node): number =>
82
- tree.getLineAndCharacterOfPosition(node.getStart(tree)).line + 1
83
89
  const source = (node: TS.Node): string => node.getText(tree)
84
90
  const imports = new Map<string, ModuleImport>()
85
91
  // Every import of @nifrajs/web, with the specifiers of names 5.9 moved out of it.
@@ -446,6 +452,126 @@ function rewriteServeStatic(ts: typeof TS, file: string, text: string): RewriteR
446
452
  return { text: applyEdits(text, edits), count, manual }
447
453
  }
448
454
 
455
+ const WEB_APP = "createWebApp"
456
+
457
+ /** `createWebApp({ experimental: { ppr } })` becomes `createWebApp({ ppr })`: 5.10 moved the option. */
458
+ function rewritePprOption(ts: typeof TS, file: string, text: string): RewriteResult {
459
+ const { tree, lineOf } = parseSource(ts, file, text)
460
+ const edits: Edit[] = []
461
+ const manual: ManualSite[] = []
462
+ let count = 0
463
+ // The names createWebApp is imported under, and the names @nifrajs/web is imported as a namespace.
464
+ const locals = new Set<string>()
465
+ const namespaces = new Set<string>()
466
+ for (const statement of tree.statements) {
467
+ if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier))
468
+ continue
469
+ if (statement.moduleSpecifier.text !== WEB) continue
470
+ const bindings = statement.importClause?.namedBindings
471
+ if (bindings === undefined) continue
472
+ if (ts.isNamespaceImport(bindings)) namespaces.add(bindings.name.text)
473
+ else {
474
+ for (const element of bindings.elements) {
475
+ if ((element.propertyName ?? element.name).text === WEB_APP) locals.add(element.name.text)
476
+ }
477
+ }
478
+ }
479
+ const isCreateWebApp = (callee: TS.Expression): boolean =>
480
+ (ts.isIdentifier(callee) && locals.has(callee.text)) ||
481
+ (ts.isPropertyAccessExpression(callee) &&
482
+ ts.isIdentifier(callee.expression) &&
483
+ namespaces.has(callee.expression.text) &&
484
+ callee.name.text === WEB_APP)
485
+ const nameOf = (property: TS.ObjectLiteralElementLike): string | undefined =>
486
+ ts.isShorthandPropertyAssignment(property)
487
+ ? property.name.text
488
+ : ts.isPropertyAssignment(property)
489
+ ? propertyName(ts, property.name)
490
+ : undefined
491
+ const property = (
492
+ object: TS.ObjectLiteralExpression,
493
+ name: string,
494
+ ): TS.ObjectLiteralElementLike | undefined => object.properties.find((p) => nameOf(p) === name)
495
+ const handled = new Set<TS.ObjectLiteralExpression>()
496
+
497
+ const move = (options: TS.ObjectLiteralExpression): void => {
498
+ const experimental = property(options, "experimental")
499
+ if (experimental === undefined) return
500
+ const spread = options.properties.some((p) => ts.isSpreadAssignment(p))
501
+ const inner = ts.isPropertyAssignment(experimental) ? experimental.initializer : undefined
502
+ const moved = inner !== undefined && ts.isObjectLiteralExpression(inner) ? inner : undefined
503
+ const only = moved?.properties.length === 1 ? moved.properties[0] : undefined
504
+ if (
505
+ moved === undefined ||
506
+ only === undefined ||
507
+ nameOf(only) !== "ppr" ||
508
+ property(options, "ppr") !== undefined ||
509
+ spread
510
+ ) {
511
+ manual.push({
512
+ line: lineOf(experimental),
513
+ reason:
514
+ "experimental.ppr is now ppr: pass it beside the other createWebApp options and remove `experimental`",
515
+ })
516
+ return
517
+ }
518
+ edits.push({
519
+ start: experimental.getStart(tree),
520
+ end: experimental.end,
521
+ text: ts.isPropertyAssignment(only) ? `ppr: ${only.initializer.getText(tree)}` : "ppr",
522
+ })
523
+ count++
524
+ }
525
+
526
+ const visit = (node: TS.Node): void => {
527
+ if (ts.isCallExpression(node) && isCreateWebApp(node.expression)) {
528
+ const options = node.arguments[0]
529
+ if (options !== undefined && ts.isObjectLiteralExpression(options)) {
530
+ handled.add(options)
531
+ move(options)
532
+ }
533
+ }
534
+ ts.forEachChild(node, visit)
535
+ }
536
+ visit(tree)
537
+
538
+ // Options built apart from the call, or passed through a name, are the user's to move.
539
+ const sweep = (node: TS.Node): void => {
540
+ if (ts.isObjectLiteralExpression(node) && !handled.has(node)) {
541
+ const experimental = property(node, "experimental")
542
+ const inner =
543
+ experimental !== undefined && ts.isPropertyAssignment(experimental)
544
+ ? experimental.initializer
545
+ : undefined
546
+ if (
547
+ inner !== undefined &&
548
+ ts.isObjectLiteralExpression(inner) &&
549
+ property(inner, "ppr") !== undefined
550
+ ) {
551
+ manual.push({
552
+ line: lineOf(node),
553
+ reason:
554
+ "experimental.ppr is now ppr: pass it beside the other createWebApp options and remove `experimental`",
555
+ })
556
+ }
557
+ }
558
+ ts.forEachChild(node, sweep)
559
+ }
560
+ sweep(tree)
561
+ return {
562
+ text: applyEdits(text, edits),
563
+ count,
564
+ manual: manual.toSorted((a, b) => a.line - b.line),
565
+ }
566
+ }
567
+
568
+ const PPR_OPTION: SourceRewrite = {
569
+ id: "ppr-option",
570
+ description: "createWebApp({ experimental: { ppr } }) → createWebApp({ ppr })",
571
+ mentions: (text) => /\bexperimental\b/.test(text) && /\bcreateWebApp\b/.test(text),
572
+ rewrite: rewritePprOption,
573
+ }
574
+
449
575
  const SERVE_STATIC: SourceRewrite = {
450
576
  id: "serve-static",
451
577
  description:
@@ -456,7 +582,10 @@ const SERVE_STATIC: SourceRewrite = {
456
582
  rewrite: rewriteServeStatic,
457
583
  }
458
584
 
459
- const REWRITES: Readonly<Record<string, SourceRewrite>> = { [SERVE_STATIC.id]: SERVE_STATIC }
585
+ const REWRITES: Readonly<Record<string, SourceRewrite>> = {
586
+ [SERVE_STATIC.id]: SERVE_STATIC,
587
+ [PPR_OPTION.id]: PPR_OPTION,
588
+ }
460
589
 
461
590
  export function getSourceRewrite(id: string): SourceRewrite | undefined {
462
591
  return REWRITES[id]
@@ -6,6 +6,7 @@
6
6
  */
7
7
  import { existsSync, readFileSync, realpathSync } from "node:fs"
8
8
  import { dirname, isAbsolute, join, relative, resolve } from "node:path"
9
+ import { ROUTE_TABLE_FILE } from "@nifrajs/web/route-types"
9
10
  import { stripComments } from "./check-scan.ts"
10
11
 
11
12
  const ZONE_FOLDER = new Set(["routes", "frontend", "backend", "shared"])
@@ -140,6 +141,28 @@ const excludedBy = (patterns: readonly string[], file: string): boolean =>
140
141
  (pattern) => new Bun.Glob(pattern).match(file) || new Bun.Glob(`${pattern}/**`).match(file),
141
142
  )
142
143
 
144
+ // A named import of a navigation API from `@nifrajs/web` or an adapter: what the route table types.
145
+ const NAVIGATION_IMPORT =
146
+ /\bimport\s+(?:type\s+)?\{[^}]*\b(?:useNavigate|Link|NavLink|Navigate|redirect|href|rawHref)\b[^}]*\}\s*from\s*["']@nifrajs\/web(?:-[a-z]+)?(?:\/(?:router|client))?["']/
147
+
148
+ /**
149
+ * `.nifra/types/routes.d.ts` (project-relative) when a source file uses a navigation API, the table
150
+ * exists, and no program holds it. A program that imports any route's `./+types` module loads the table
151
+ * through that module's reference; one that does not, and does not name it, never sees it. Its `href`
152
+ * calls have no route keys to check against. An app that uses none of these APIs is left alone.
153
+ */
154
+ export function routeTableOutsideProgram(
155
+ cwd: string,
156
+ programFiles: readonly string[],
157
+ sources: ReadonlyArray<{ readonly content: string }>,
158
+ ): string | undefined {
159
+ const table = join(cwd, ROUTE_TABLE_FILE)
160
+ if (!existsSync(table) || !sources.some(({ content }) => NAVIGATION_IMPORT.test(content)))
161
+ return undefined
162
+ const wanted = new Set([canonical(table), canonical(real(table))])
163
+ return programFiles.some((file) => wanted.has(canonical(file))) ? undefined : ROUTE_TABLE_FILE
164
+ }
165
+
143
166
  /**
144
167
  * The TypeScript files under a zone folder that `programFiles` does not contain, project-relative and
145
168
  * in input order. Declaration files and tests are left out: a project may typecheck tests with a
@@ -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
- }