fimo 0.8.0 → 0.9.0-experimental.10

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 (218) hide show
  1. package/assets/agent-templates/content-translator/scripts/write-locale-files.ts +66 -0
  2. package/assets/agents-starter/claude/settings.json +19 -0
  3. package/assets/content-templates/hooks-template.eta +4 -2
  4. package/assets/content-templates/types-template.eta +67 -1
  5. package/assets/skills/fimo/SKILL.md +1 -1
  6. package/assets/skills/fimo/references/content.md +86 -15
  7. package/assets/skills/fimo/references/translations.md +75 -0
  8. package/assets/skills/fimo-cli/SKILL.md +6 -2
  9. package/assets/skills/fimo-cli/references/content.md +61 -2
  10. package/assets/skills/fimo-cli/references/integrations/firecrawl.md +20 -4
  11. package/assets/skills/fimo-cli/references/integrations.md +5 -1
  12. package/assets/skills/fimo-cli/references/webhooks.md +162 -0
  13. package/assets/skills/fimo-migration/SKILL.md +24 -2
  14. package/assets/skills/fimo-migration/references/content.md +24 -0
  15. package/assets/skills/fimo-migration/references/manifest.md +46 -0
  16. package/assets/skills/fimo-migration/references/pages.md +234 -0
  17. package/assets/skills/fimo-migration/references/qa.md +24 -2
  18. package/assets/skills/fimo-migration/references/workflow.md +12 -1
  19. package/assets/skills/fimo-migration/references/wrap.md +662 -0
  20. package/assets/skills/fimo-studio/SKILL.md +5 -3
  21. package/assets/skills/fimo-studio/references/cms.md +9 -1
  22. package/assets/skills/fimo-studio/references/visual-editing.md +5 -3
  23. package/assets/skills/fimo-studio/references/webhooks.md +42 -0
  24. package/dist/build/vite/plugins/translations.d.ts +2 -8
  25. package/dist/build/vite/plugins/translations.js +31 -131
  26. package/dist/cli/bundle.json +1 -1
  27. package/dist/cli/index.js +1337 -189
  28. package/dist/runtime/app/FimoProviders.d.ts +1 -11
  29. package/dist/runtime/app/FimoProviders.js +31 -30
  30. package/dist/runtime/astro/index.d.ts +2 -0
  31. package/dist/runtime/astro/index.js +3 -0
  32. package/dist/runtime/astro/server.d.ts +3 -0
  33. package/dist/runtime/astro/server.js +6 -0
  34. package/dist/runtime/content/client/internal.js +1 -0
  35. package/dist/runtime/content/client/types.d.ts +16 -0
  36. package/dist/runtime/core/BuiltWithBadge.d.ts +2 -0
  37. package/dist/runtime/core/BuiltWithBadge.js +68 -0
  38. package/dist/runtime/i18n/FimoLocaleContext.js +1 -0
  39. package/dist/runtime/next/FimoNextProviders.d.ts +24 -0
  40. package/dist/runtime/next/FimoNextProviders.js +20 -0
  41. package/dist/runtime/next/FimoNextScripts.d.ts +10 -0
  42. package/dist/runtime/next/FimoNextScripts.js +19 -0
  43. package/dist/runtime/next/index.d.ts +14 -0
  44. package/dist/runtime/next/index.js +14 -0
  45. package/dist/runtime/next/server.d.ts +2 -0
  46. package/dist/runtime/next/server.js +4 -0
  47. package/dist/runtime/nuxt/composables.d.ts +9 -0
  48. package/dist/runtime/nuxt/composables.js +57 -0
  49. package/dist/runtime/nuxt/index.d.ts +3 -0
  50. package/dist/runtime/nuxt/index.js +6 -0
  51. package/dist/runtime/nuxt/server.d.ts +3 -0
  52. package/dist/runtime/nuxt/server.js +6 -0
  53. package/dist/runtime/primitives/components/RichText.d.ts +5 -1
  54. package/dist/runtime/primitives/components/RichText.js +2 -2
  55. package/dist/runtime/primitives/labels-hook.d.ts +33 -0
  56. package/dist/runtime/primitives/labels-hook.js +41 -0
  57. package/dist/runtime/primitives/labels-store.d.ts +12 -0
  58. package/dist/runtime/primitives/labels-store.js +60 -0
  59. package/dist/runtime/primitives/lib/useFimoLiveValue.d.ts +5 -3
  60. package/dist/runtime/primitives/lib/useFimoLiveValue.js +11 -4
  61. package/dist/runtime/primitives/translations.d.ts +1 -27
  62. package/dist/runtime/primitives/translations.js +7 -85
  63. package/dist/runtime/react/FimoSpaProviders.d.ts +36 -0
  64. package/dist/runtime/react/FimoSpaProviders.js +122 -0
  65. package/dist/runtime/react/index.d.ts +35 -0
  66. package/dist/runtime/react/index.js +40 -0
  67. package/dist/runtime/react/spa-content-cache.d.ts +38 -0
  68. package/dist/runtime/react/spa-content-cache.js +64 -0
  69. package/dist/runtime/remix/FimoRemixProviders.d.ts +16 -0
  70. package/dist/runtime/remix/FimoRemixProviders.js +12 -0
  71. package/dist/runtime/remix/FimoRemixScripts.d.ts +10 -0
  72. package/dist/runtime/remix/FimoRemixScripts.js +19 -0
  73. package/dist/runtime/remix/index.d.ts +12 -0
  74. package/dist/runtime/remix/index.js +11 -0
  75. package/dist/runtime/remix/server.d.ts +2 -0
  76. package/dist/runtime/remix/server.js +2 -0
  77. package/dist/runtime/seo/neutral.d.ts +22 -0
  78. package/dist/runtime/seo/neutral.js +17 -0
  79. package/dist/runtime/shared/display-routes.d.ts +14 -0
  80. package/dist/runtime/shared/display-routes.js +25 -0
  81. package/dist/runtime/shared/fimo-config.server.d.ts +0 -1
  82. package/dist/runtime/shared/fimo-config.server.js +1 -13
  83. package/dist/runtime/shared/fimo-config.types.d.ts +2 -0
  84. package/dist/runtime/shared/i18n-messages.d.ts +29 -0
  85. package/dist/runtime/shared/i18n-messages.js +79 -0
  86. package/dist/runtime/shared/labels-fetch.pure.d.ts +17 -0
  87. package/dist/runtime/shared/labels-fetch.pure.js +71 -0
  88. package/dist/runtime/shared/labels-fetch.server.d.ts +7 -0
  89. package/dist/runtime/shared/labels-fetch.server.js +97 -0
  90. package/dist/runtime/shared/labels-t.pure.d.ts +2 -0
  91. package/dist/runtime/shared/labels-t.pure.js +10 -0
  92. package/dist/runtime/strapi/client.d.ts +30 -0
  93. package/dist/runtime/strapi/client.js +132 -0
  94. package/dist/runtime/strapi/index.d.ts +5 -0
  95. package/dist/runtime/strapi/index.js +6 -0
  96. package/dist/runtime/strapi/reshape.d.ts +50 -0
  97. package/dist/runtime/strapi/reshape.js +121 -0
  98. package/dist/runtime/svelte/index.d.ts +3 -0
  99. package/dist/runtime/svelte/index.js +7 -0
  100. package/dist/runtime/svelte/server.d.ts +3 -0
  101. package/dist/runtime/svelte/server.js +6 -0
  102. package/dist/runtime/vue/index.d.ts +3 -0
  103. package/dist/runtime/vue/index.js +5 -0
  104. package/dist/runtime/vue/server.d.ts +3 -0
  105. package/dist/runtime/vue/server.js +6 -0
  106. package/dist/runtime/wrapped/FimoFileLink.d.ts +17 -0
  107. package/dist/runtime/wrapped/FimoFileLink.js +19 -0
  108. package/dist/runtime/wrapped/FimoMediaImage.d.ts +14 -0
  109. package/dist/runtime/wrapped/FimoMediaImage.js +20 -0
  110. package/dist/runtime/wrapped/FimoMediaVideo.d.ts +14 -0
  111. package/dist/runtime/wrapped/FimoMediaVideo.js +20 -0
  112. package/dist/runtime/wrapped/FimoRichTextBlock.d.ts +26 -0
  113. package/dist/runtime/wrapped/FimoRichTextBlock.js +22 -0
  114. package/dist/runtime/wrapped/FimoText.d.ts +22 -0
  115. package/dist/runtime/wrapped/FimoText.js +20 -0
  116. package/dist/runtime/wrapped/WrappedContentContext.d.ts +28 -0
  117. package/dist/runtime/wrapped/WrappedContentContext.js +30 -0
  118. package/dist/runtime/wrapped/WrappedProviders.d.ts +33 -0
  119. package/dist/runtime/wrapped/WrappedProviders.js +101 -0
  120. package/dist/runtime/wrapped/content-fetch.pure.d.ts +20 -0
  121. package/dist/runtime/wrapped/content-fetch.pure.js +106 -0
  122. package/dist/runtime/wrapped/dom.d.ts +4 -0
  123. package/dist/runtime/wrapped/dom.js +23 -0
  124. package/dist/runtime/wrapped/server.d.ts +80 -0
  125. package/dist/runtime/wrapped/server.js +207 -0
  126. package/dist/scripts/apply-wrap-templates.d.ts +61 -0
  127. package/dist/scripts/apply-wrap-templates.js +137 -0
  128. package/dist/scripts/apply-wrap.d.ts +73 -0
  129. package/dist/scripts/apply-wrap.js +340 -0
  130. package/dist/scripts/cms/discover.d.ts +2 -0
  131. package/dist/scripts/cms/discover.js +17 -0
  132. package/dist/scripts/cms/example-content.d.ts +33 -0
  133. package/dist/scripts/cms/example-content.js +165 -0
  134. package/dist/scripts/cms/strapi/detect.d.ts +2 -0
  135. package/dist/scripts/cms/strapi/detect.js +186 -0
  136. package/dist/scripts/cms/strapi/index.d.ts +3 -0
  137. package/dist/scripts/cms/strapi/index.js +25 -0
  138. package/dist/scripts/cms/strapi/list-source.d.ts +17 -0
  139. package/dist/scripts/cms/strapi/list-source.js +90 -0
  140. package/dist/scripts/cms/strapi/schema-map.d.ts +9 -0
  141. package/dist/scripts/cms/strapi/schema-map.js +289 -0
  142. package/dist/scripts/cms/strapi/shapes.d.ts +25 -0
  143. package/dist/scripts/cms/strapi/shapes.js +48 -0
  144. package/dist/scripts/cms/strapi/touchpoints.d.ts +31 -0
  145. package/dist/scripts/cms/strapi/touchpoints.js +145 -0
  146. package/dist/scripts/cms/strapi/wire-client.d.ts +15 -0
  147. package/dist/scripts/cms/strapi/wire-client.js +107 -0
  148. package/dist/scripts/cms/strapi/wiring.d.ts +17 -0
  149. package/dist/scripts/cms/strapi/wiring.js +46 -0
  150. package/dist/scripts/cms/types.d.ts +145 -0
  151. package/dist/scripts/cms/types.js +11 -0
  152. package/dist/scripts/detect-framework.d.ts +39 -0
  153. package/dist/scripts/detect-framework.js +139 -0
  154. package/dist/scripts/draft-rows.d.ts +19 -0
  155. package/dist/scripts/draft-rows.js +178 -0
  156. package/dist/scripts/draft-schemas.d.ts +27 -0
  157. package/dist/scripts/draft-schemas.js +247 -0
  158. package/dist/scripts/extract-translations.js +16 -11
  159. package/dist/scripts/frameworks/astro/routes.d.ts +3 -0
  160. package/dist/scripts/frameworks/astro/routes.js +44 -0
  161. package/dist/scripts/frameworks/astro/template-style.d.ts +7 -0
  162. package/dist/scripts/frameworks/astro/template-style.js +19 -0
  163. package/dist/scripts/frameworks/discover.d.ts +83 -0
  164. package/dist/scripts/frameworks/discover.js +288 -0
  165. package/dist/scripts/frameworks/discovered-route.d.ts +9 -0
  166. package/dist/scripts/frameworks/discovered-route.js +17 -0
  167. package/dist/scripts/frameworks/next/rewire.d.ts +43 -0
  168. package/dist/scripts/frameworks/next/rewire.js +282 -0
  169. package/dist/scripts/frameworks/next/routes.d.ts +30 -0
  170. package/dist/scripts/frameworks/next/routes.js +176 -0
  171. package/dist/scripts/frameworks/next/wiring.d.ts +33 -0
  172. package/dist/scripts/frameworks/next/wiring.js +186 -0
  173. package/dist/scripts/frameworks/nuxt/routes.d.ts +3 -0
  174. package/dist/scripts/frameworks/nuxt/routes.js +38 -0
  175. package/dist/scripts/frameworks/nuxt/template-style.d.ts +9 -0
  176. package/dist/scripts/frameworks/nuxt/template-style.js +27 -0
  177. package/dist/scripts/frameworks/remix/routes.d.ts +3 -0
  178. package/dist/scripts/frameworks/remix/routes.js +36 -0
  179. package/dist/scripts/frameworks/support.d.ts +75 -0
  180. package/dist/scripts/frameworks/support.js +165 -0
  181. package/dist/scripts/frameworks/sveltekit/routes.d.ts +3 -0
  182. package/dist/scripts/frameworks/sveltekit/routes.js +35 -0
  183. package/dist/scripts/frameworks/sveltekit/template-style.d.ts +9 -0
  184. package/dist/scripts/frameworks/sveltekit/template-style.js +54 -0
  185. package/dist/scripts/frameworks/vite/routes.d.ts +20 -0
  186. package/dist/scripts/frameworks/vite/routes.js +143 -0
  187. package/dist/scripts/frameworks/vite/wiring.d.ts +22 -0
  188. package/dist/scripts/frameworks/vite/wiring.js +144 -0
  189. package/dist/scripts/i18n-namespace.d.ts +27 -0
  190. package/dist/scripts/i18n-namespace.js +155 -0
  191. package/dist/scripts/lib/humanize.d.ts +14 -0
  192. package/dist/scripts/lib/humanize.js +54 -0
  193. package/dist/scripts/lib/validation-report.d.ts +1 -3
  194. package/dist/scripts/lib/validation-report.js +3 -9
  195. package/dist/scripts/render-parity/capture.d.ts +64 -0
  196. package/dist/scripts/render-parity/capture.js +436 -0
  197. package/dist/scripts/render-parity/compare.d.ts +59 -0
  198. package/dist/scripts/render-parity/compare.js +0 -0
  199. package/dist/scripts/render-parity/normalize-html.d.ts +13 -0
  200. package/dist/scripts/render-parity/normalize-html.js +223 -0
  201. package/dist/scripts/render-parity/types.d.ts +41 -0
  202. package/dist/scripts/render-parity/types.js +1 -0
  203. package/dist/scripts/routes/bindings.d.ts +93 -0
  204. package/dist/scripts/routes/bindings.js +117 -0
  205. package/dist/scripts/routes/label-prefixes.d.ts +49 -0
  206. package/dist/scripts/routes/label-prefixes.js +136 -0
  207. package/dist/scripts/routes/module-resolve.d.ts +20 -0
  208. package/dist/scripts/routes/module-resolve.js +78 -0
  209. package/dist/scripts/routes/page-structure.d.ts +72 -0
  210. package/dist/scripts/routes/page-structure.js +234 -0
  211. package/dist/scripts/scan-wrappable.d.ts +200 -0
  212. package/dist/scripts/scan-wrappable.js +1355 -0
  213. package/dist/scripts/validate-route-metadata.js +11 -0
  214. package/dist/scripts/validate-schemas.js +42 -1
  215. package/package.json +88 -4
  216. package/release.json +2 -2
  217. package/templates/react-router/package.json +1 -1
  218. package/templates/react-router/translations/en.json +1 -0
@@ -0,0 +1,282 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { parseSync } from 'oxc-parser';
4
+ import { ensureFimoImport } from '../../apply-wrap.js';
5
+ import { routePathForAppPage } from './routes.js';
6
+ function applyEdits(code, edits) {
7
+ let next = code;
8
+ for (const edit of [...edits].sort((a, b) => b.start - a.start)) {
9
+ next = next.slice(0, edit.start) + edit.replacement + next.slice(edit.end);
10
+ }
11
+ return next;
12
+ }
13
+ function walkAll(node, visit) {
14
+ if (!node || typeof node !== 'object') {
15
+ return;
16
+ }
17
+ if (Array.isArray(node)) {
18
+ for (const child of node) {
19
+ walkAll(child, visit);
20
+ }
21
+ return;
22
+ }
23
+ const record = node;
24
+ if (typeof record.type === 'string') {
25
+ visit(record);
26
+ }
27
+ for (const [key, value] of Object.entries(record)) {
28
+ if (key !== 'type' && value && typeof value === 'object') {
29
+ walkAll(value, visit);
30
+ }
31
+ }
32
+ }
33
+ function defaultExportFunction(program) {
34
+ const exportDefault = (program.body ?? []).find((node) => node.type === 'ExportDefaultDeclaration');
35
+ if (!exportDefault || exportDefault.declaration?.type !== 'FunctionDeclaration') {
36
+ return null;
37
+ }
38
+ const body = exportDefault.declaration.body;
39
+ if (!body || (body.type !== 'FunctionBody' && body.type !== 'BlockStatement')) {
40
+ return null;
41
+ }
42
+ return exportDefault.declaration;
43
+ }
44
+ /** Every reference to `name` (outside `ignoredSpans`) must sit inside the
45
+ * component body — otherwise moving the binding into the component would
46
+ * break module-level consumers (metadata, generateStaticParams, …). */
47
+ function referencesConfinedToFunction(program, name, fn, ignoredSpans) {
48
+ let confined = true;
49
+ walkAll(program, (node) => {
50
+ if (node.type !== 'Identifier' || node.name !== name) {
51
+ return;
52
+ }
53
+ const inIgnored = ignoredSpans.some((span) => node.start >= span.start && node.end <= span.end);
54
+ const inFn = node.start >= fn.body.start && node.end <= fn.body.end;
55
+ if (!inIgnored && !inFn) {
56
+ confined = false;
57
+ }
58
+ });
59
+ return confined;
60
+ }
61
+ function relativeImport(fromFile, toFile) {
62
+ const rel = path.relative(path.dirname(fromFile), toFile).split(path.sep).join('/');
63
+ return rel.startsWith('.') ? rel : `./${rel}`;
64
+ }
65
+ function pageFilesUnder(dir) {
66
+ const out = [];
67
+ const walk = (current) => {
68
+ for (const entry of fs.readdirSync(current, { withFileTypes: true })) {
69
+ if (entry.name === 'node_modules' || entry.name.startsWith('.')) {
70
+ continue;
71
+ }
72
+ const full = path.join(current, entry.name);
73
+ if (entry.isDirectory()) {
74
+ walk(full);
75
+ }
76
+ else if (/\.(tsx|jsx|js)$/.test(entry.name)) {
77
+ out.push(full);
78
+ }
79
+ }
80
+ };
81
+ if (fs.existsSync(dir)) {
82
+ walk(dir);
83
+ }
84
+ return out;
85
+ }
86
+ export function rewireNextCollectionPages(cwd, detection, scan, drafts) {
87
+ const changes = [];
88
+ const warnings = [];
89
+ const pages = [];
90
+ let rewired = 0;
91
+ if (!detection.appDir) {
92
+ return { changes, warnings: ['Collection pages not rewired: pages-router project.'], rewired, pages };
93
+ }
94
+ const appDirAbs = path.join(cwd, detection.appDir);
95
+ const uidBySourceName = new Map(drafts.map((draft) => [draft.sourceName, draft.uid]));
96
+ const draftsBySourceFile = new Map();
97
+ for (const draft of drafts) {
98
+ const bucket = draftsBySourceFile.get(draft.source) ?? [];
99
+ bucket.push(draft.uid);
100
+ draftsBySourceFile.set(draft.source, bucket);
101
+ }
102
+ const bail = (page, reason) => {
103
+ warnings.push(`Page not rewired (${page}): ${reason} — wire it with wrapCollection per the prompt recipe.`);
104
+ };
105
+ const recordPage = (fileAbs, uid) => {
106
+ pages.push({ file: path.relative(cwd, fileAbs), uid, routePath: routePathForAppPage(appDirAbs, fileAbs) });
107
+ };
108
+ // Pages wired on a PREVIOUS run still carry binding evidence (the uid in the
109
+ // call site is final once `adopt-drafts` renamed it) — collect them before
110
+ // rewiring so re-runs keep feeding the page registry.
111
+ for (const pageAbs of pageFilesUnder(appDirAbs)) {
112
+ let code;
113
+ try {
114
+ code = fs.readFileSync(pageAbs, 'utf8');
115
+ }
116
+ catch {
117
+ continue;
118
+ }
119
+ for (const match of code.matchAll(/wrapCollection\(\s*['"]([^'"]+)['"]/g)) {
120
+ recordPage(pageAbs, match[1]);
121
+ }
122
+ }
123
+ // ── In-code array literals: extract to src/data/<name>.json + wire ──
124
+ for (const candidate of scan.collections) {
125
+ const pageAbs = path.join(cwd, candidate.file);
126
+ if (!pageAbs.startsWith(appDirAbs + path.sep)) {
127
+ bail(candidate.file, `in-code collection \`${candidate.name}\` sits outside the app directory`);
128
+ continue;
129
+ }
130
+ const uid = uidBySourceName.get(candidate.name);
131
+ if (!uid) {
132
+ continue;
133
+ }
134
+ const rows = candidate.typedRows;
135
+ if (!rows || rows.length !== candidate.count) {
136
+ bail(candidate.file, `\`${candidate.name}\` is larger than the extraction window`);
137
+ continue;
138
+ }
139
+ const code = fs.readFileSync(pageAbs, 'utf8');
140
+ if (code.includes("'use client'") || code.includes('"use client"')) {
141
+ bail(candidate.file, 'client component (no server fetch)');
142
+ continue;
143
+ }
144
+ let program;
145
+ try {
146
+ program = parseSync(pageAbs, code).program;
147
+ }
148
+ catch {
149
+ bail(candidate.file, 'could not parse');
150
+ continue;
151
+ }
152
+ // `const x = [...]`, `= [...] as const`, `= [...] satisfies T` all count.
153
+ const isArrayInit = (init) => {
154
+ const inner = init?.type === 'TSAsExpression' || init?.type === 'TSSatisfiesExpression' ? init.expression : init;
155
+ return inner?.type === 'ArrayExpression';
156
+ };
157
+ const declaration = (program.body ?? []).find((node) => node.type === 'VariableDeclaration' &&
158
+ node.declarations?.length === 1 &&
159
+ node.declarations[0]?.id?.name === candidate.name &&
160
+ isArrayInit(node.declarations[0]?.init));
161
+ if (!declaration) {
162
+ bail(candidate.file, `top-level \`const ${candidate.name} = [...]\` not found`);
163
+ continue;
164
+ }
165
+ const fn = defaultExportFunction(program);
166
+ if (!fn) {
167
+ bail(candidate.file, 'the default export is not a plain function declaration');
168
+ continue;
169
+ }
170
+ if (!referencesConfinedToFunction(program, candidate.name, fn, [declaration])) {
171
+ bail(candidate.file, `\`${candidate.name}\` is referenced outside the page component`);
172
+ continue;
173
+ }
174
+ const dataDir = path.join(cwd, 'src', 'data');
175
+ const dataFileAbs = path.join(dataDir, `${candidate.name}.json`);
176
+ if (fs.existsSync(dataFileAbs)) {
177
+ bail(candidate.file, `src/data/${candidate.name}.json already exists`);
178
+ continue;
179
+ }
180
+ fs.mkdirSync(dataDir, { recursive: true });
181
+ fs.writeFileSync(dataFileAbs, `${JSON.stringify(rows, null, 2)}\n`);
182
+ const edits = [{ start: declaration.start, end: declaration.end, replacement: '' }];
183
+ if (!fn.async) {
184
+ edits.push({ start: fn.start, end: fn.start, replacement: 'async ' });
185
+ }
186
+ edits.push({
187
+ start: fn.body.start + 1,
188
+ end: fn.body.start + 1,
189
+ replacement: `\n const ${candidate.name} = await wrapCollection('${uid}', ${candidate.name}Source);\n`,
190
+ });
191
+ let next = applyEdits(code, edits);
192
+ next = `import ${candidate.name}Source from '${relativeImport(pageAbs, dataFileAbs)}';\n${next}`;
193
+ next = ensureFimoImport(next, ['wrapCollection'], 'fimo/next/server');
194
+ fs.writeFileSync(pageAbs, next);
195
+ rewired += 1;
196
+ recordPage(pageAbs, uid);
197
+ changes.push(`rewired ${candidate.file}: \`${candidate.name}\` extracted to src/data/${candidate.name}.json and read through wrapCollection('${uid}')`);
198
+ }
199
+ // ── Data-file default imports: wire the importing pages ──
200
+ const dataFileUids = new Map();
201
+ for (const dataFile of scan.dataFiles) {
202
+ const uids = draftsBySourceFile.get(dataFile.file);
203
+ if (uids && uids.length === 1 && /\.json$/i.test(dataFile.file)) {
204
+ dataFileUids.set(path.resolve(cwd, dataFile.file), uids[0]);
205
+ }
206
+ }
207
+ if (dataFileUids.size > 0) {
208
+ for (const pageAbs of pageFilesUnder(appDirAbs)) {
209
+ const code = fs.readFileSync(pageAbs, 'utf8');
210
+ const rel = path.relative(cwd, pageAbs);
211
+ if (!code.includes('src/data/')) {
212
+ continue;
213
+ }
214
+ if (code.includes('wrapCollection(')) {
215
+ continue;
216
+ }
217
+ let program;
218
+ try {
219
+ program = parseSync(pageAbs, code).program;
220
+ }
221
+ catch {
222
+ continue;
223
+ }
224
+ const imports = (program.body ?? []).filter((node) => node.type === 'ImportDeclaration' && typeof node.source?.value === 'string');
225
+ const targets = [];
226
+ for (const declaration of imports) {
227
+ const specifier = (declaration.specifiers ?? []).find((entry) => entry.type === 'ImportDefaultSpecifier');
228
+ if (!specifier || !declaration.source.value.startsWith('.')) {
229
+ continue;
230
+ }
231
+ const resolved = path.resolve(path.dirname(pageAbs), declaration.source.value);
232
+ const uid = dataFileUids.get(resolved);
233
+ if (uid) {
234
+ targets.push({ node: declaration, local: specifier.local.name, uid });
235
+ }
236
+ }
237
+ if (targets.length === 0) {
238
+ continue;
239
+ }
240
+ if (code.includes("'use client'") || code.includes('"use client"')) {
241
+ bail(rel, 'client component (no server fetch)');
242
+ continue;
243
+ }
244
+ const fn = defaultExportFunction(program);
245
+ if (!fn) {
246
+ bail(rel, 'the default export is not a plain function declaration');
247
+ continue;
248
+ }
249
+ const confined = targets.every((target) => referencesConfinedToFunction(program, target.local, fn, [target.node]));
250
+ if (!confined) {
251
+ bail(rel, 'the imported collection is referenced outside the page component');
252
+ continue;
253
+ }
254
+ const edits = [];
255
+ if (!fn.async) {
256
+ edits.push({ start: fn.start, end: fn.start, replacement: 'async ' });
257
+ }
258
+ for (const target of targets) {
259
+ const specifier = (target.node.specifiers ?? []).find((entry) => entry.type === 'ImportDefaultSpecifier');
260
+ edits.push({
261
+ start: specifier.local.start,
262
+ end: specifier.local.end,
263
+ replacement: `${target.local}Source`,
264
+ });
265
+ edits.push({
266
+ start: fn.body.start + 1,
267
+ end: fn.body.start + 1,
268
+ replacement: `\n const ${target.local} = await wrapCollection('${target.uid}', ${target.local}Source);\n`,
269
+ });
270
+ }
271
+ let next = applyEdits(code, edits);
272
+ next = ensureFimoImport(next, ['wrapCollection'], 'fimo/next/server');
273
+ fs.writeFileSync(pageAbs, next);
274
+ rewired += 1;
275
+ for (const target of targets) {
276
+ recordPage(pageAbs, target.uid);
277
+ }
278
+ changes.push(`rewired ${rel}: ${targets.map((target) => `wrapCollection('${target.uid}')`).join(', ')} over the imported source rows`);
279
+ }
280
+ }
281
+ return { changes, warnings, rewired, pages };
282
+ }
@@ -0,0 +1,30 @@
1
+ import { type RouteFileResolution, type RouteLike } from '../../routes/module-resolve.js';
2
+ export interface DiscoveredRoute {
3
+ id: string;
4
+ path: string;
5
+ /** The source file that renders this route (file-based frameworks set it;
6
+ * code-declared routers resolve files separately). */
7
+ file?: string;
8
+ label: string;
9
+ }
10
+ /**
11
+ * Route path for ONE app-router page file, using the same segment
12
+ * normalization as discovery — so a rewired page can be joined back to its
13
+ * registry route. Returns null for non-routable locations (`@slot`, `_lib`).
14
+ */
15
+ export declare function routePathForAppPage(appDir: string, pageFile: string): string | null;
16
+ /**
17
+ * Discover page routes of a Next.js app-router directory (`app/` or
18
+ * `src/app/`). Returns display routes for `fimo-config.json#routes` so the
19
+ * Studio page switcher can enumerate a wrapped site's pages.
20
+ */
21
+ export declare function discoverNextRoutes(appDir: string): DiscoveredRoute[];
22
+ /**
23
+ * Pages-router discovery (`pages/` or `src/pages/`): every module is a route
24
+ * except `_app`/`_document`/`_error` and `api/`. `[param]` segments map to
25
+ * `:param` (the parity capture skips dynamic routes, same as app router).
26
+ */
27
+ export declare function discoverNextPagesRoutes(pagesDir: string): DiscoveredRoute[];
28
+ /** Registry-routed (frameworkRouteFiles): registry route ids → the app-dir
29
+ * page files that render them, joined by routePathForAppPage. */
30
+ export declare function resolveNextRouteFiles(cwd: string, routes: Record<string, RouteLike>): RouteFileResolution[];
@@ -0,0 +1,176 @@
1
+ import { existsSync, readdirSync, statSync } from 'node:fs';
2
+ import { join, relative, sep } from 'node:path';
3
+ import { discoveredRouteLabel } from '../../lib/humanize.js';
4
+ import { walkFiles } from '../../routes/module-resolve.js';
5
+ const PAGE_FILES = new Set(['page.tsx', 'page.jsx', 'page.ts', 'page.js', 'page.mdx']);
6
+ function segmentToPath(segment) {
7
+ // Route groups `(marketing)` don't contribute URL segments. Parallel-route
8
+ // slots `@modal` and private folders `_lib` are not standalone pages.
9
+ if (segment.startsWith('(')) {
10
+ return '';
11
+ }
12
+ if (segment.startsWith('@') || segment.startsWith('_')) {
13
+ return null;
14
+ }
15
+ const catchAll = segment.match(/^\[\.\.\.(.+)\]$/) ?? segment.match(/^\[\[\.\.\.(.+)\]\]$/);
16
+ if (catchAll) {
17
+ return `:${catchAll[1]}*`;
18
+ }
19
+ const param = segment.match(/^\[(.+)\]$/);
20
+ if (param) {
21
+ return `:${param[1]}`;
22
+ }
23
+ return segment;
24
+ }
25
+ /**
26
+ * Route path for ONE app-router page file, using the same segment
27
+ * normalization as discovery — so a rewired page can be joined back to its
28
+ * registry route. Returns null for non-routable locations (`@slot`, `_lib`).
29
+ */
30
+ export function routePathForAppPage(appDir, pageFile) {
31
+ const rel = relative(appDir, pageFile);
32
+ if (rel.startsWith('..')) {
33
+ return null;
34
+ }
35
+ const segments = rel.split(sep).slice(0, -1);
36
+ const parts = [];
37
+ for (const segment of segments) {
38
+ const mapped = segmentToPath(segment);
39
+ if (mapped === null) {
40
+ return null;
41
+ }
42
+ if (mapped.length > 0) {
43
+ parts.push(mapped);
44
+ }
45
+ }
46
+ return `/${parts.join('/')}`.replace(/\/+/g, '/');
47
+ }
48
+ /**
49
+ * Discover page routes of a Next.js app-router directory (`app/` or
50
+ * `src/app/`). Returns display routes for `fimo-config.json#routes` so the
51
+ * Studio page switcher can enumerate a wrapped site's pages.
52
+ */
53
+ export function discoverNextRoutes(appDir) {
54
+ const routes = [];
55
+ const walk = (dir, segments) => {
56
+ let entries;
57
+ try {
58
+ entries = readdirSync(dir);
59
+ }
60
+ catch {
61
+ return;
62
+ }
63
+ if (entries.some((entry) => PAGE_FILES.has(entry))) {
64
+ const parts = segments.filter((s) => s.length > 0);
65
+ const routePath = `/${parts.join('/')}`.replace(/\/+/g, '/');
66
+ routes.push({
67
+ id: routePath === '/' ? 'home' : parts.map((p) => p.replace(/^:/, '')).join('-'),
68
+ path: routePath,
69
+ label: discoveredRouteLabel(routePath),
70
+ });
71
+ }
72
+ for (const entry of entries) {
73
+ const full = join(dir, entry);
74
+ let isDir = false;
75
+ try {
76
+ isDir = statSync(full).isDirectory();
77
+ }
78
+ catch {
79
+ continue;
80
+ }
81
+ if (!isDir) {
82
+ continue;
83
+ }
84
+ const mapped = segmentToPath(entry);
85
+ if (mapped === null) {
86
+ continue;
87
+ }
88
+ walk(full, [...segments, mapped]);
89
+ }
90
+ };
91
+ walk(appDir, []);
92
+ return routes.sort((a, b) => a.path.localeCompare(b.path));
93
+ }
94
+ const PAGES_EXTENSIONS = /\.(tsx|jsx|ts|js)$/;
95
+ const PAGES_SKIP = new Set(['api']);
96
+ /**
97
+ * Pages-router discovery (`pages/` or `src/pages/`): every module is a route
98
+ * except `_app`/`_document`/`_error` and `api/`. `[param]` segments map to
99
+ * `:param` (the parity capture skips dynamic routes, same as app router).
100
+ */
101
+ export function discoverNextPagesRoutes(pagesDir) {
102
+ const routes = [];
103
+ const toSegment = (name) => name
104
+ .replace(/^\[\.\.\.(.+)\]$/, ':$1')
105
+ .replace(/^\[\[\.\.\.(.+)\]\]$/, ':$1')
106
+ .replace(/^\[(.+)\]$/, ':$1');
107
+ const walk = (dir, segments) => {
108
+ let entries;
109
+ try {
110
+ entries = readdirSync(dir);
111
+ }
112
+ catch {
113
+ return;
114
+ }
115
+ for (const entry of entries) {
116
+ const full = join(dir, entry);
117
+ let isDir = false;
118
+ try {
119
+ isDir = statSync(full).isDirectory();
120
+ }
121
+ catch {
122
+ continue;
123
+ }
124
+ if (isDir) {
125
+ // api/ is only special at the pages root.
126
+ if (!(segments.length === 0 && PAGES_SKIP.has(entry))) {
127
+ walk(full, [...segments, toSegment(entry)]);
128
+ }
129
+ continue;
130
+ }
131
+ if (!PAGES_EXTENSIONS.test(entry)) {
132
+ continue;
133
+ }
134
+ const base = entry.replace(PAGES_EXTENSIONS, '');
135
+ if (base.startsWith('_')) {
136
+ continue;
137
+ }
138
+ const parts = base === 'index' ? segments : [...segments, toSegment(base)];
139
+ const routePath = `/${parts.join('/')}`.replace(/\/+/g, '/');
140
+ routes.push({
141
+ id: routePath === '/' ? 'home' : parts.map((part) => part.replace(/^:/, '')).join('-'),
142
+ path: routePath,
143
+ label: discoveredRouteLabel(routePath),
144
+ });
145
+ }
146
+ };
147
+ walk(pagesDir, []);
148
+ routes.sort((a, b) => a.path.localeCompare(b.path));
149
+ return routes;
150
+ }
151
+ /** Registry-routed (frameworkRouteFiles): registry route ids → the app-dir
152
+ * page files that render them, joined by routePathForAppPage. */
153
+ export function resolveNextRouteFiles(cwd, routes) {
154
+ const appDir = [join(cwd, 'app'), join(cwd, 'src', 'app')].find((dir) => existsSync(dir));
155
+ if (!appDir) {
156
+ return [];
157
+ }
158
+ const PAGE_FILE = /^page\.(?:tsx|jsx|ts|js|mdx)$/;
159
+ const pageFiles = [];
160
+ walkFiles(appDir, (name) => PAGE_FILE.test(name), pageFiles);
161
+ const byPath = new Map();
162
+ for (const [routeId, route] of Object.entries(routes)) {
163
+ if (typeof route?.path === 'string') {
164
+ byPath.set(route.path, routeId);
165
+ }
166
+ }
167
+ const resolutions = [];
168
+ for (const file of pageFiles) {
169
+ const routePath = routePathForAppPage(appDir, file);
170
+ const routeId = routePath ? byPath.get(routePath) : undefined;
171
+ if (routeId) {
172
+ resolutions.push({ routeId, file });
173
+ }
174
+ }
175
+ return resolutions;
176
+ }
@@ -0,0 +1,33 @@
1
+ import type { FrameworkDetection } from '../../detect-framework.js';
2
+ /**
3
+ * Next.js-side wrap wiring: everything the wrap migration must touch that is
4
+ * specific to this framework lives HERE, not in the generic flow. Mirror this
5
+ * module when adding a framework (see the fimo-framework-adapter skill).
6
+ */
7
+ export declare function ensureNextWiring(cwd: string): {
8
+ changes: string[];
9
+ warnings: string[];
10
+ };
11
+ export interface RootWiringResult {
12
+ changes: string[];
13
+ warnings: string[];
14
+ /** True when the root wiring is in place (pre-existing or just applied). */
15
+ wired: boolean;
16
+ }
17
+ /**
18
+ * Candidate app-router root-layout files: the direct `<appDir>/layout.*` plus,
19
+ * for i18n apps that place the `<html>` layout under a dynamic segment
20
+ * (next-intl's `app/[locale]/layout.tsx`), any `<appDir>/[segment]/layout.*`.
21
+ * Existing files only, in resolution order.
22
+ */
23
+ export declare function nextRootLayoutFiles(cwd: string, appDir: string): string[];
24
+ /**
25
+ * Wire the app-router root layout deterministically: imports, async default
26
+ * export, loader calls, `<FimoNextScripts />` in head, and `{children}`
27
+ * wrapped in `<FimoNextProviders>`. Only the provably safe shape is touched —
28
+ * one default-exported function whose JSX returns `<html>…<body>` with a
29
+ * `{children}` expression somewhere inside body (create-next-app and its
30
+ * variants). Anything else bails with the reason, and the agent prompt keeps
31
+ * the manual recipe.
32
+ */
33
+ export declare function ensureNextLayoutWiring(cwd: string, detection: Pick<FrameworkDetection, 'appDir'>): RootWiringResult;