@decocms/blocks 8.10.0 → 8.12.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 (210) hide show
  1. package/dist/index.d.ts +1 -1
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js.map +1 -1
  4. package/dist/protocol/conformance/blockMetaVectors.json +80 -0
  5. package/dist/protocol/index.d.ts +5 -2
  6. package/dist/protocol/index.d.ts.map +1 -1
  7. package/dist/protocol/index.js +5 -2
  8. package/dist/protocol/index.js.map +1 -1
  9. package/dist/v8/blockMeta.d.ts +33 -4
  10. package/dist/v8/blockMeta.d.ts.map +1 -1
  11. package/dist/v8/blockMeta.js +70 -13
  12. package/dist/v8/blockMeta.js.map +1 -1
  13. package/dist/v8/cli/index.d.ts +1 -0
  14. package/dist/v8/cli/index.d.ts.map +1 -1
  15. package/dist/v8/cli/index.js +1 -0
  16. package/dist/v8/cli/index.js.map +1 -1
  17. package/dist/v8/cli/run.d.ts +2 -0
  18. package/dist/v8/cli/run.d.ts.map +1 -1
  19. package/dist/v8/cli/run.js +30 -1
  20. package/dist/v8/cli/run.js.map +1 -1
  21. package/dist/v8/cli/skills.d.ts +25 -0
  22. package/dist/v8/cli/skills.d.ts.map +1 -0
  23. package/dist/v8/cli/skills.js +70 -0
  24. package/dist/v8/cli/skills.js.map +1 -0
  25. package/dist/v8/client.d.ts +6 -4
  26. package/dist/v8/client.d.ts.map +1 -1
  27. package/dist/v8/client.js +26 -70
  28. package/dist/v8/client.js.map +1 -1
  29. package/dist/v8/cms.d.ts.map +1 -1
  30. package/dist/v8/cms.js +1 -2
  31. package/dist/v8/cms.js.map +1 -1
  32. package/dist/v8/contentIndex.d.ts +18 -7
  33. package/dist/v8/contentIndex.d.ts.map +1 -1
  34. package/dist/v8/contentIndex.js +46 -17
  35. package/dist/v8/contentIndex.js.map +1 -1
  36. package/dist/v8/draftChanges.d.ts.map +1 -1
  37. package/dist/v8/draftChanges.js +4 -1
  38. package/dist/v8/draftChanges.js.map +1 -1
  39. package/dist/v8/index.d.ts +1 -1
  40. package/dist/v8/index.d.ts.map +1 -1
  41. package/dist/v8/remoteLoader.d.ts +4 -2
  42. package/dist/v8/remoteLoader.d.ts.map +1 -1
  43. package/dist/v8/remoteLoader.js +13 -3
  44. package/dist/v8/remoteLoader.js.map +1 -1
  45. package/dist/v8/types.d.ts +47 -7
  46. package/dist/v8/types.d.ts.map +1 -1
  47. package/package.json +3 -2
  48. package/skills/deco-apps/SKILL.md +82 -0
  49. package/skills/deco-apps/references/algolia.md +51 -0
  50. package/skills/deco-apps/references/commerce-types.md +132 -0
  51. package/skills/deco-apps/references/instrumented-fetch.md +144 -0
  52. package/skills/deco-apps/references/magento.md +55 -0
  53. package/skills/deco-apps/references/messaging.md +76 -0
  54. package/skills/deco-apps/references/shopify.md +69 -0
  55. package/skills/deco-apps/references/vendored-loaders.md +135 -0
  56. package/skills/deco-apps/references/vtex-cookies-auth.md +146 -0
  57. package/skills/deco-apps/references/vtex.md +150 -0
  58. package/skills/deco-apps/references/wake.md +75 -0
  59. package/skills/deco-cms-schema/SKILL.md +80 -0
  60. package/skills/deco-cms-schema/references/annotations.md +147 -0
  61. package/skills/deco-cms-schema/references/content-hygiene.md +91 -0
  62. package/skills/deco-cms-schema/references/formats-and-widgets.md +112 -0
  63. package/skills/deco-cms-schema/references/shapes-that-break-the-form.md +165 -0
  64. package/skills/deco-cms-schema/references/types-to-fields.md +112 -0
  65. package/skills/deco-go-live/SKILL.md +127 -0
  66. package/skills/deco-go-live/references/content-releases.md +90 -0
  67. package/skills/deco-go-live/references/process.md +106 -0
  68. package/skills/deco-go-live/references/proxy-headers-checkout.md +139 -0
  69. package/skills/deco-go-live/references/qa-checklist-template.md +150 -0
  70. package/skills/deco-go-live/references/qa-inventory.md +120 -0
  71. package/skills/deco-go-live/references/react-spa.md +183 -0
  72. package/skills/deco-go-live/references/seo-analytics.md +114 -0
  73. package/skills/deco-go-live/references/traffic-ramp.md +87 -0
  74. package/skills/deco-go-live/references/worker-runtime.md +93 -0
  75. package/skills/deco-go-live/scripts/preflight.sh +221 -0
  76. package/skills/deco-images-media/SKILL.md +64 -0
  77. package/skills/deco-images-media/references/asset-upload.md +191 -0
  78. package/skills/deco-images-media/references/components-per-binding.md +167 -0
  79. package/skills/deco-images-media/references/providers-and-cdn.md +145 -0
  80. package/skills/deco-images-media/references/quality-and-lcp.md +93 -0
  81. package/skills/deco-images-media/references/troubleshooting.md +147 -0
  82. package/skills/deco-images-media/references/video.md +122 -0
  83. package/skills/deco-make-editable/SKILL.md +94 -0
  84. package/skills/deco-make-editable/references/a11y-and-e2e.md +104 -0
  85. package/skills/deco-make-editable/references/content-shapes.md +203 -0
  86. package/skills/deco-make-editable/references/from-figma.md +110 -0
  87. package/skills/deco-make-editable/references/i18n-fields.md +112 -0
  88. package/skills/deco-make-editable/references/inventory.md +79 -0
  89. package/skills/deco-make-editable/references/pixel-parity.md +175 -0
  90. package/skills/deco-make-editable/references/seeding-content.md +119 -0
  91. package/skills/deco-nextjs-site/SKILL.md +74 -0
  92. package/skills/deco-nextjs-site/references/caching.md +116 -0
  93. package/skills/deco-nextjs-site/references/metadata-seo.md +100 -0
  94. package/skills/deco-nextjs-site/references/proxy-and-drafts.md +177 -0
  95. package/skills/deco-nextjs-site/references/rendering.md +172 -0
  96. package/skills/deco-nextjs-site/references/setup.md +190 -0
  97. package/skills/deco-nextjs-site/references/studio-bridge.md +130 -0
  98. package/skills/deco-site-authoring/SKILL.md +75 -0
  99. package/skills/deco-site-authoring/references/lazy-blocks.md +155 -0
  100. package/skills/deco-site-authoring/references/loaders.md +192 -0
  101. package/skills/deco-site-authoring/references/pages.md +158 -0
  102. package/skills/deco-site-authoring/references/review-before-publish.md +95 -0
  103. package/skills/deco-site-authoring/references/sections.md +158 -0
  104. package/skills/deco-site-authoring/references/seo.md +116 -0
  105. package/skills/deco-site-workflow/SKILL.md +101 -0
  106. package/skills/deco-site-workflow/references/check.md +116 -0
  107. package/skills/deco-site-workflow/references/cli-reference.md +106 -0
  108. package/skills/deco-site-workflow/references/content-layout.md +93 -0
  109. package/skills/deco-site-workflow/references/content-sync.md +146 -0
  110. package/skills/deco-site-workflow/references/delivery.md +51 -0
  111. package/skills/deco-site-workflow/references/deploy.md +87 -0
  112. package/skills/deco-site-workflow/references/schema.md +173 -0
  113. package/skills/deco-site-workflow/references/serve-and-studio.md +138 -0
  114. package/skills/deco-storefront-caching/SKILL.md +67 -0
  115. package/skills/deco-storefront-caching/references/cache-keys.md +129 -0
  116. package/skills/deco-storefront-caching/references/case-study.md +82 -0
  117. package/skills/deco-storefront-caching/references/egress-traps.md +153 -0
  118. package/skills/deco-storefront-caching/references/nextjs.md +60 -0
  119. package/skills/deco-storefront-caching/references/perf-guardrails-e2e.md +123 -0
  120. package/skills/deco-storefront-caching/references/verification.md +136 -0
  121. package/skills/deco-storefront-caching/references/worker-page-cache.md +160 -0
  122. package/skills/deco-storefront-caching/scripts/html-anatomy.mjs +97 -0
  123. package/skills/deco-storefront-performance/SKILL.md +81 -0
  124. package/skills/deco-storefront-performance/references/cls-third-party.md +90 -0
  125. package/skills/deco-storefront-performance/references/hydration.md +116 -0
  126. package/skills/deco-storefront-performance/references/loaders-n-plus-1.md +107 -0
  127. package/skills/deco-storefront-performance/references/navigation-ux.md +130 -0
  128. package/skills/deco-storefront-performance/references/payload-trim.md +163 -0
  129. package/skills/deco-storefront-performance/references/render-location.md +152 -0
  130. package/skills/deco-storefront-performance/references/request-dedupe.md +147 -0
  131. package/skills/deco-storefront-performance/references/trace-reading.md +79 -0
  132. package/skills/deco-storefront-performance/references/worker-memory.md +176 -0
  133. package/skills/deco-tanstack-site/SKILL.md +68 -0
  134. package/skills/deco-tanstack-site/references/document-and-security.md +155 -0
  135. package/skills/deco-tanstack-site/references/router-and-navigation.md +137 -0
  136. package/skills/deco-tanstack-site/references/routes-and-pages.md +183 -0
  137. package/skills/deco-tanstack-site/references/server-functions.md +123 -0
  138. package/skills/deco-tanstack-site/references/setup.md +153 -0
  139. package/skills/deco-tanstack-site/references/worker-entry.md +152 -0
  140. package/skills/deco-v7-to-v8-migration/SKILL.md +123 -0
  141. package/skills/deco-v7-to-v8-migration/references/caching-and-privacy.md +10 -0
  142. package/skills/deco-v7-to-v8-migration/references/content.md +25 -0
  143. package/skills/deco-v7-to-v8-migration/references/editor-forms.md +27 -0
  144. package/skills/deco-v7-to-v8-migration/references/faststore.md +40 -0
  145. package/skills/deco-v7-to-v8-migration/references/framework-code.md +53 -0
  146. package/skills/deco-v7-to-v8-migration/references/hosted-releases.md +16 -0
  147. package/skills/deco-v7-to-v8-migration/references/import-map.md +81 -0
  148. package/skills/deco-v7-to-v8-migration/references/legacy-names.md +33 -0
  149. package/skills/deco-v7-to-v8-migration/references/manual-steps.md +10 -0
  150. package/skills/deco-v7-to-v8-migration/references/native-apps.md +43 -0
  151. package/skills/deco-v7-to-v8-migration/references/nextjs.md +19 -0
  152. package/skills/deco-v7-to-v8-migration/references/parity.md +66 -0
  153. package/skills/deco-v7-to-v8-migration/references/rendering.md +13 -0
  154. package/skills/deco-v7-to-v8-migration/references/running-the-script.md +29 -0
  155. package/skills/deco-v7-to-v8-migration/references/script-steps.md +13 -0
  156. package/skills/deco-v7-to-v8-migration/references/server-functions.md +15 -0
  157. package/skills/deco-v7-to-v8-migration/references/sibling-port.md +11 -0
  158. package/skills/deco-v7-to-v8-migration/references/site-code.md +15 -0
  159. package/skills/deco-v7-to-v8-migration/references/telemetry.md +10 -0
  160. package/skills/deco-v7-to-v8-migration/references/verify.md +23 -0
  161. package/skills/deco-v7-to-v8-migration/scripts/apps.ts +297 -0
  162. package/skills/deco-v7-to-v8-migration/scripts/asyncRendering.ts +73 -0
  163. package/skills/deco-v7-to-v8-migration/scripts/blockMap.ts +584 -0
  164. package/skills/deco-v7-to-v8-migration/scripts/checkContentReads.ts +32 -0
  165. package/skills/deco-v7-to-v8-migration/scripts/ciSchemaCheck.ts +145 -0
  166. package/skills/deco-v7-to-v8-migration/scripts/compat.ts +357 -0
  167. package/skills/deco-v7-to-v8-migration/scripts/content.ts +121 -0
  168. package/skills/deco-v7-to-v8-migration/scripts/contentReads.ts +157 -0
  169. package/skills/deco-v7-to-v8-migration/scripts/experiments.ts +81 -0
  170. package/skills/deco-v7-to-v8-migration/scripts/framework.ts +61 -0
  171. package/skills/deco-v7-to-v8-migration/scripts/imageCopy.ts +276 -0
  172. package/skills/deco-v7-to-v8-migration/scripts/imageModule.ts +285 -0
  173. package/skills/deco-v7-to-v8-migration/scripts/imports.ts +942 -0
  174. package/skills/deco-v7-to-v8-migration/scripts/invoke.ts +624 -0
  175. package/skills/deco-v7-to-v8-migration/scripts/legacyNames.ts +45 -0
  176. package/skills/deco-v7-to-v8-migration/scripts/main.ts +59 -0
  177. package/skills/deco-v7-to-v8-migration/scripts/matchers.ts +41 -0
  178. package/skills/deco-v7-to-v8-migration/scripts/migrate.ts +180 -0
  179. package/skills/deco-v7-to-v8-migration/scripts/redirects.ts +455 -0
  180. package/skills/deco-v7-to-v8-migration/scripts/report.ts +50 -0
  181. package/skills/deco-v7-to-v8-migration/scripts/scaffold.ts +1119 -0
  182. package/skills/deco-v7-to-v8-migration/scripts/secrets.ts +35 -0
  183. package/skills/deco-v7-to-v8-migration/scripts/sectionLoaders.ts +216 -0
  184. package/skills/deco-v7-to-v8-migration/scripts/siteSettings.ts +261 -0
  185. package/skills/deco-v7-to-v8-migration/scripts/stubs.ts +388 -0
  186. package/skills/deco-v7-to-v8-migration/scripts/tsconfig.json +7 -0
  187. package/skills/deco-v7-to-v8-migration/scripts/vendor.ts +249 -0
  188. package/skills/deco-v7-to-v8-migration/scripts/vtexInvoke.ts +240 -0
  189. package/skills/deco-v7-to-v8-migration/scripts/walk.ts +30 -0
  190. package/skills/deco-variants/SKILL.md +57 -0
  191. package/skills/deco-variants/references/caching-variants.md +63 -0
  192. package/skills/deco-variants/references/custom-matchers.md +83 -0
  193. package/skills/deco-variants/references/draft-preview.md +59 -0
  194. package/skills/deco-variants/references/experiments-and-campaigns.md +104 -0
  195. package/skills/deco-variants/references/multivariate.md +85 -0
  196. package/skills/deco-variants/references/v7-matchers.md +71 -0
  197. package/src/index.ts +1 -1
  198. package/src/protocol/conformance/blockMetaVectors.json +80 -0
  199. package/src/protocol/index.ts +8 -1
  200. package/src/v8/blockMeta.ts +75 -11
  201. package/src/v8/cli/index.ts +1 -0
  202. package/src/v8/cli/run.ts +35 -1
  203. package/src/v8/cli/skills.ts +91 -0
  204. package/src/v8/client.ts +32 -77
  205. package/src/v8/cms.ts +1 -2
  206. package/src/v8/contentIndex.ts +49 -19
  207. package/src/v8/draftChanges.ts +5 -1
  208. package/src/v8/index.ts +1 -1
  209. package/src/v8/remoteLoader.ts +13 -3
  210. package/src/v8/types.ts +48 -7
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Secrets (spec: renames-and-migrations › Secrets). v7 saved each credential
3
+ * as a `website/loaders/secret.ts` block, encrypted under `DECO_CRYPTO_KEY`.
4
+ * The next major keeps no secrets in content: they live in the deploy
5
+ * platform's secrets, and the site's code reads them and passes them as
6
+ * params. This step decrypts nothing and writes nothing: it lists each v7
7
+ * secret, with the variable v7 read it from when it names one, so the
8
+ * developer moves its value to the deploy platform and deletes the block
9
+ * (`deco check` points out any left).
10
+ */
11
+ import type { Report } from "./report";
12
+ import { forEachBlock, readContent } from "./walk";
13
+
14
+ const DOCS = "/next/renames-and-migrations#secrets";
15
+
16
+ /** v7's secret type names, with and without the extension: the secrets step's, never the block map's. */
17
+ export const V7_SECRET_TYPES = new Set(["website/loaders/secret.ts", "website/loaders/secret"]);
18
+
19
+ /** Every v7 secret in `.deco/blocks`, as a manual step: move its value to the deploy platform. */
20
+ export function listSecrets(root: string, report: Report): void {
21
+ const { blocks, files } = readContent(root);
22
+ for (const [entry, block] of Object.entries(blocks)) {
23
+ forEachBlock(block, (node) => {
24
+ if (!V7_SECRET_TYPES.has(node.__resolveType)) return;
25
+ const name = typeof node.name === "string" && node.name.length > 0 ? node.name : undefined;
26
+ const where = node === block ? "a v7 secret" : `a v7 secret inside "${entry}"`;
27
+ const variable = name ? `, such as ${name} (the variable v7 read it from)` : "";
28
+ report.manual.push({
29
+ step: "secrets",
30
+ subject: files[entry],
31
+ message: `${where}: secrets live in your deploy platform now, not in content. Put its value in the deploy platform's secrets${variable}, read it in the site's code and pass it as a param where the content used it, then delete it from content (${DOCS})`,
32
+ });
33
+ });
34
+ }
35
+ }
@@ -0,0 +1,216 @@
1
+ /**
2
+ * Section loaders. v7 ran a section's loader only when the site registered
3
+ * it, with `registerSectionLoaders({ "site/sections/…": loader })` from
4
+ * `@decocms/blocks/cms`, and ran them over the page's sections after it
5
+ * resolved. `@decocms/compat-v7/sections` has that runner with v7's rules,
6
+ * taking the loaders as an argument; so this step turns the registration
7
+ * into an export of the same map:
8
+ *
9
+ * registerSectionLoaders({ … });
10
+ * → export const sectionLoaders: RunSectionLoadersOptions["loaders"] = { … };
11
+ *
12
+ * and `registerNonCriticalSections([…])` into `export const
13
+ * nonCriticalSections`. The block map then runs them (`loadSections` in
14
+ * `.deco/index.ts`), together with every other section's own `loader`,
15
+ * which v7 never ran: an approved difference (PO 2026-10-15) the block map
16
+ * step reports section by section. The mixins (`withDevice`, `compose`, …) move to
17
+ * `@decocms/compat-v7/sections` in the imports step.
18
+ */
19
+ import fs from "node:fs";
20
+ import path from "node:path";
21
+ import ts from "typescript";
22
+ import { applyEdits, type Edit, importInsertion, listSources, parse } from "./compat";
23
+ import type { Report } from "./report";
24
+
25
+ const V7_CMS = /^@decocms\/blocks\/cms(\/client)?$/;
26
+
27
+ /** v7's registration call → the export it becomes. */
28
+ const CONVERSIONS: Record<string, { name: string; type: string }> = {
29
+ registerSectionLoaders: { name: "sectionLoaders", type: 'RunSectionLoadersOptions["loaders"]' },
30
+ registerNonCriticalSections: { name: "nonCriticalSections", type: "readonly string[]" },
31
+ };
32
+
33
+ export interface SectionLoaderWiring {
34
+ /** The module that now exports `sectionLoaders`, and the section names it registers (null: not all literal). */
35
+ loaders?: { file: string; keys: Set<string> | null };
36
+ /** The module that now exports `nonCriticalSections`. */
37
+ nonCritical?: { file: string };
38
+ }
39
+
40
+ interface Call {
41
+ file: string;
42
+ /** The v7 function's name in `CONVERSIONS`. */
43
+ v7: string;
44
+ statement: ts.ExpressionStatement;
45
+ call: ts.CallExpression;
46
+ source: ts.SourceFile;
47
+ /** The import specifiers that bring it in. */
48
+ imports: ts.ImportSpecifier[];
49
+ }
50
+
51
+ /** The local names each v7 registration function is imported under, from `@decocms/blocks/cms`. */
52
+ function v7Imports(source: ts.SourceFile): Map<string, { v7: string; el: ts.ImportSpecifier }> {
53
+ const out = new Map<string, { v7: string; el: ts.ImportSpecifier }>();
54
+ for (const s of source.statements) {
55
+ if (!ts.isImportDeclaration(s) || !ts.isStringLiteral(s.moduleSpecifier)) continue;
56
+ if (!V7_CMS.test(s.moduleSpecifier.text)) continue;
57
+ const b = s.importClause?.namedBindings;
58
+ if (!b || !ts.isNamedImports(b)) continue;
59
+ for (const el of b.elements) {
60
+ const v7 = (el.propertyName ?? el.name).text;
61
+ if (Object.hasOwn(CONVERSIONS, v7)) out.set(el.name.text, { v7, el });
62
+ }
63
+ }
64
+ return out;
65
+ }
66
+
67
+ function findCalls(file: string, source: ts.SourceFile, nested: number[]): Call[] {
68
+ const locals = v7Imports(source);
69
+ if (locals.size === 0) return [];
70
+ const calls: Call[] = [];
71
+ const visit = (node: ts.Node) => {
72
+ if (
73
+ ts.isCallExpression(node) &&
74
+ ts.isIdentifier(node.expression) &&
75
+ locals.has(node.expression.text)
76
+ ) {
77
+ const { v7, el } = locals.get(node.expression.text)!;
78
+ const statement = node.parent;
79
+ if (
80
+ ts.isExpressionStatement(statement) &&
81
+ statement.parent === source &&
82
+ node.arguments.length === 1
83
+ ) {
84
+ calls.push({ file, v7, statement, call: node, source, imports: [el] });
85
+ } else {
86
+ nested.push(source.getLineAndCharacterOfPosition(node.getStart(source)).line + 1);
87
+ }
88
+ }
89
+ ts.forEachChild(node, visit);
90
+ };
91
+ visit(source);
92
+ return calls;
93
+ }
94
+
95
+ /** The literal keys of a loader map, or null when it has a spread, a computed key or isn't a literal. */
96
+ function literalKeys(arg: ts.Expression): Set<string> | null {
97
+ if (!ts.isObjectLiteralExpression(arg)) return null;
98
+ const keys = new Set<string>();
99
+ for (const p of arg.properties) {
100
+ if (ts.isSpreadAssignment(p) || !p.name) return null;
101
+ if (
102
+ ts.isStringLiteral(p.name) ||
103
+ ts.isIdentifier(p.name) ||
104
+ ts.isNoSubstitutionTemplateLiteral(p.name)
105
+ )
106
+ keys.add(p.name.text);
107
+ else return null;
108
+ }
109
+ return keys;
110
+ }
111
+
112
+ /** The edit that drops `els` from their import declaration, or the whole declaration when nothing is left. */
113
+ function dropImports(source: ts.SourceFile, els: ts.ImportSpecifier[]): Edit[] {
114
+ const edits: Edit[] = [];
115
+ const byDecl = new Map<ts.ImportDeclaration, ts.ImportSpecifier[]>();
116
+ for (const el of els) {
117
+ const decl = el.parent.parent.parent as ts.ImportDeclaration;
118
+ byDecl.set(decl, [...(byDecl.get(decl) ?? []), el]);
119
+ }
120
+ for (const [decl, drop] of byDecl) {
121
+ const named = decl.importClause!.namedBindings as ts.NamedImports;
122
+ const keep = named.elements.filter((el) => !drop.includes(el));
123
+ if (keep.length === 0 && !decl.importClause?.name) {
124
+ const text = source.getFullText();
125
+ let end = decl.getEnd();
126
+ if (text[end] === "\n") end++;
127
+ edits.push({ start: decl.getStart(source), end, text: "" });
128
+ } else {
129
+ edits.push({
130
+ start: named.getStart(source),
131
+ end: named.getEnd(),
132
+ text: keep.length === 0 ? "{}" : `{ ${keep.map((el) => el.getText(source)).join(", ")} }`,
133
+ });
134
+ }
135
+ }
136
+ return edits;
137
+ }
138
+
139
+ /**
140
+ * Turns the site's v7 section-loader registrations into exports the block
141
+ * map imports. One call per function site-wide is converted; more, or a call
142
+ * not at a module's top level, is reported.
143
+ */
144
+ export function wireSectionLoaders(root: string, report: Report): SectionLoaderWiring {
145
+ const all: Call[] = [];
146
+ const nestedAt: string[] = [];
147
+ for (const file of listSources(path.join(root, "src"))) {
148
+ const text = fs.readFileSync(file, "utf8");
149
+ if (!/\bregister(SectionLoaders|NonCriticalSections)\b/.test(text)) continue;
150
+ const nested: number[] = [];
151
+ all.push(...findCalls(file, parse(file, text), nested));
152
+ const rel = path.relative(root, file).split(path.sep).join("/");
153
+ nestedAt.push(...nested.map((line) => `${rel}:${line}`));
154
+ }
155
+ for (const where of nestedAt) {
156
+ report.manual.push({
157
+ step: "imports",
158
+ subject: where,
159
+ message:
160
+ "a v7 section-loader registration the script couldn't convert (not a top-level call with one argument): export the map instead and run it with runSectionLoaders from @decocms/compat-v7/sections",
161
+ });
162
+ }
163
+
164
+ const wiring: SectionLoaderWiring = {};
165
+ const byFile = new Map<string, Call[]>();
166
+ for (const v7 of Object.keys(CONVERSIONS)) {
167
+ const calls = all.filter((c) => c.v7 === v7);
168
+ if (calls.length === 0) continue;
169
+ if (calls.length > 1) {
170
+ report.manual.push({
171
+ step: "imports",
172
+ subject: v7,
173
+ message: `called ${calls.length} times (${calls.map((c) => path.relative(root, c.file)).join(", ")}): merge them into one exported ${CONVERSIONS[v7].name} for the block map's loadSections`,
174
+ });
175
+ continue;
176
+ }
177
+ const [call] = calls;
178
+ byFile.set(call.file, [...(byFile.get(call.file) ?? []), call]);
179
+ if (v7 === "registerSectionLoaders") {
180
+ wiring.loaders = { file: call.file, keys: literalKeys(call.call.arguments[0]) };
181
+ } else {
182
+ wiring.nonCritical = { file: call.file };
183
+ }
184
+ }
185
+
186
+ for (const [file, calls] of byFile) {
187
+ const { source } = calls[0];
188
+ const edits: Edit[] = calls.map((c) => {
189
+ const { name, type } = CONVERSIONS[c.v7];
190
+ return {
191
+ start: c.statement.getStart(source),
192
+ end: c.statement.getEnd(),
193
+ text: `export const ${name}: ${type} = ${c.call.arguments[0].getText(source)};`,
194
+ };
195
+ });
196
+ edits.push(
197
+ ...dropImports(
198
+ source,
199
+ calls.flatMap((c) => c.imports),
200
+ ),
201
+ );
202
+ if (calls.some((c) => c.v7 === "registerSectionLoaders")) {
203
+ const at = importInsertion(source);
204
+ const line = 'import type { RunSectionLoadersOptions } from "@decocms/compat-v7/sections";';
205
+ edits.push({ start: at, end: at, text: at === 0 ? `${line}\n` : `\n${line}` });
206
+ }
207
+ fs.writeFileSync(file, applyEdits(source.getFullText(), edits));
208
+ const rel = path.relative(root, file).split(path.sep).join("/");
209
+ report.done.push({
210
+ step: "imports",
211
+ subject: rel,
212
+ message: `${calls.map((c) => `${c.v7}(…) is now export const ${CONVERSIONS[c.v7].name}`).join("; ")}, run by loadSections in .deco/index.ts`,
213
+ });
214
+ }
215
+ return wiring;
216
+ }
@@ -0,0 +1,261 @@
1
+ /**
2
+ * Site settings (spec: renames-and-migrations › Site settings): the next
3
+ * major keeps site-wide settings in one saved block, `CMS`, of the built-in
4
+ * type `cms-settings`. This step folds what it finds into
5
+ * `.deco/blocks/CMS.json`:
6
+ *
7
+ * - a literal `collectorAddress` on v7's `OneDollarStats` component in
8
+ * `src/` → `analytics.collector`;
9
+ * - a prerelease `Telemetry` / `Analytics` saved block (type `telemetry` /
10
+ * `analytics`) → its section, field for field, variants included; the old
11
+ * file is deleted.
12
+ *
13
+ * What's already in `CMS.json` is kept; nothing is written when there's
14
+ * nothing to fold, and a second run changes nothing. Settings v7 read from
15
+ * the hosting environment are listed in the report. Preview hosts aren't
16
+ * settings any more: Studio writes them into each release's metadata, so
17
+ * the v7 Site block's `previewHosts` and `DECO_ALLOWED_PREVIEW_HOSTS` are
18
+ * left alone.
19
+ */
20
+ import fs from "node:fs";
21
+ import path from "node:path";
22
+ import { LEGACY_ALIASES } from "@decocms/blocks/cli";
23
+ import { blockFileName, serializeBlock } from "@decocms/blocks/protocol/keys";
24
+ import type { Report } from "./report";
25
+ import { isPlainObject, type JsonObject, readContent } from "./walk";
26
+
27
+ const SETTINGS_BLOCK = "CMS";
28
+ const SETTINGS_TYPE = "cms-settings";
29
+ /** Prerelease well-known blocks, by name, and the built-in type each had. */
30
+ const PRERELEASE: { name: string; type: "telemetry" | "analytics" }[] = [
31
+ { name: "Telemetry", type: "telemetry" },
32
+ { name: "Analytics", type: "analytics" },
33
+ ];
34
+ const SOURCE = /\.(tsx?|jsx?|mts|cts)$/;
35
+
36
+ type Section = "telemetry" | "analytics";
37
+
38
+ function isMultivariate(type: unknown): boolean {
39
+ return (
40
+ type === "multivariate" ||
41
+ (typeof type === "string" &&
42
+ Object.hasOwn(LEGACY_ALIASES, type) &&
43
+ LEGACY_ALIASES[type] === "multivariate")
44
+ );
45
+ }
46
+
47
+ /**
48
+ * A prerelease `telemetry`/`analytics` block as its section: its fields
49
+ * without `__resolveType`. A `multivariate` stays one, with each variant's
50
+ * value turned into a section the same way; anything else (a reference to
51
+ * another saved block) is kept as is.
52
+ */
53
+ function asSection(node: unknown, type: string): unknown {
54
+ if (!isPlainObject(node)) return node;
55
+ if (node.__resolveType === type) {
56
+ const { __resolveType: _, ...fields } = node;
57
+ return fields;
58
+ }
59
+ if (isMultivariate(node.__resolveType) && Array.isArray(node.variants)) {
60
+ return {
61
+ ...node,
62
+ variants: node.variants.map((variant) => {
63
+ if (!isPlainObject(variant)) return variant;
64
+ const value = variant.value;
65
+ if (isPlainObject(value) && value.__resolveType === "lazy") {
66
+ return { ...variant, value: { ...value, value: asSection(value.value, type) } };
67
+ }
68
+ return { ...variant, value: asSection(value, type) };
69
+ }),
70
+ };
71
+ }
72
+ return node;
73
+ }
74
+
75
+ /** Whether a prerelease block is (or picks between) the given built-in. */
76
+ function isPrereleaseBlock(node: unknown, type: string): boolean {
77
+ if (!isPlainObject(node)) return false;
78
+ if (node.__resolveType === type) return true;
79
+ if (!isMultivariate(node.__resolveType) || !Array.isArray(node.variants)) return false;
80
+ return node.variants.some((variant) => {
81
+ if (!isPlainObject(variant)) return false;
82
+ const value = variant.value;
83
+ const inner = isPlainObject(value) && value.__resolveType === "lazy" ? value.value : value;
84
+ return isPlainObject(inner) && inner.__resolveType === type;
85
+ });
86
+ }
87
+
88
+ function listSources(dir: string): string[] {
89
+ if (!fs.existsSync(dir)) return [];
90
+ return fs
91
+ .readdirSync(dir, { recursive: true, withFileTypes: true })
92
+ .filter((d) => d.isFile() && SOURCE.test(d.name) && !d.name.endsWith(".d.ts"))
93
+ .map((d) => path.join(d.parentPath, d.name))
94
+ .filter((f) => !f.split(path.sep).includes("node_modules"));
95
+ }
96
+
97
+ /** Literal `collectorAddress` values on `<OneDollarStats …>` elements in `src/`. */
98
+ function oneDollarCollectors(root: string): { values: Set<string>; dynamic: string[] } {
99
+ const values = new Set<string>();
100
+ const dynamic: string[] = [];
101
+ for (const file of listSources(path.join(root, "src"))) {
102
+ const text = fs.readFileSync(file, "utf8");
103
+ if (!text.includes("OneDollarStats")) continue;
104
+ for (const element of text.matchAll(/<OneDollarStats\b([^>]*)\/?>/g)) {
105
+ const attributes = element[1];
106
+ if (!/\bcollectorAddress\s*=/.test(attributes)) continue;
107
+ const literal =
108
+ /\bcollectorAddress\s*=\s*(?:"([^"]*)"|'([^']*)'|\{\s*"([^"]*)"\s*\}|\{\s*'([^']*)'\s*\}|\{\s*`([^`$]*)`\s*\})/.exec(
109
+ attributes,
110
+ );
111
+ const value = literal?.slice(1).find((v) => v !== undefined);
112
+ if (value) values.add(value);
113
+ else dynamic.push(path.relative(root, file).split(path.sep).join("/"));
114
+ }
115
+ }
116
+ return { values, dynamic };
117
+ }
118
+
119
+ /**
120
+ * Sets `value` at `section` (and `field`, when given) of `target` unless
121
+ * something is already there. Returns whether it changed anything.
122
+ */
123
+ function fold(target: JsonObject, section: Section, value: unknown, field?: string): boolean {
124
+ if (field === undefined) {
125
+ if (target[section] === undefined) {
126
+ target[section] = value;
127
+ return true;
128
+ }
129
+ const existing = target[section];
130
+ // Both plain sections: fold field by field; a section with variants is kept as it is.
131
+ if (!isPlainObject(existing) || "__resolveType" in existing) return false;
132
+ if (!isPlainObject(value) || "__resolveType" in value) return false;
133
+ let changed = false;
134
+ for (const [key, v] of Object.entries(value))
135
+ changed = fold(target, section, v, key) || changed;
136
+ return changed;
137
+ }
138
+ const existing = target[section];
139
+ if (existing === undefined) {
140
+ target[section] = { [field]: value };
141
+ return true;
142
+ }
143
+ if (!isPlainObject(existing) || "__resolveType" in existing) return false;
144
+ if (existing[field] !== undefined) return false;
145
+ existing[field] = value;
146
+ return true;
147
+ }
148
+
149
+ export function foldSiteSettings(root: string, report: Report): void {
150
+ const blocksDir = path.join(root, ".deco", "blocks");
151
+ const { blocks, files } = readContent(root);
152
+
153
+ const current = blocks[SETTINGS_BLOCK];
154
+ if (current !== undefined && current.__resolveType !== SETTINGS_TYPE) {
155
+ report.manual.push({
156
+ step: "settings",
157
+ subject: `.deco/blocks/${files[SETTINGS_BLOCK]}`,
158
+ message: `"${SETTINGS_BLOCK}" is the name of the CMS settings block (type "${SETTINGS_TYPE}"); rename this "${String(current.__resolveType)}" block, then run the migration again to fold your settings (/next/renames-and-migrations#site-settings)`,
159
+ });
160
+ return;
161
+ }
162
+ const settings: JsonObject = current
163
+ ? (JSON.parse(JSON.stringify(current)) as JsonObject)
164
+ : { __resolveType: SETTINGS_TYPE };
165
+ const done: string[] = [];
166
+ const remove: string[] = [];
167
+
168
+ // A literal collectorAddress on OneDollarStats.
169
+ const collectors = oneDollarCollectors(root);
170
+ if (collectors.values.size === 1) {
171
+ const [collector] = collectors.values;
172
+ if (fold(settings, "analytics", collector, "collector")) {
173
+ done.push("analytics.collector from OneDollarStats' collectorAddress");
174
+ }
175
+ } else if (collectors.values.size > 1) {
176
+ report.manual.push({
177
+ step: "settings",
178
+ subject: "OneDollarStats collectorAddress",
179
+ message: `several collectors (${[...collectors.values].join(", ")}): put the one to keep in CMS.json's analytics.collector (/next/analytics)`,
180
+ });
181
+ }
182
+ for (const file of collectors.dynamic) {
183
+ report.manual.push({
184
+ step: "settings",
185
+ subject: file,
186
+ message:
187
+ "OneDollarStats' collectorAddress isn't a literal: put its value in CMS.json's analytics.collector (/next/analytics)",
188
+ });
189
+ }
190
+
191
+ // Prerelease Telemetry.json / Analytics.json.
192
+ for (const { name, type } of PRERELEASE) {
193
+ const block = blocks[name];
194
+ if (!isPrereleaseBlock(block, type)) continue;
195
+ const section = asSection(block, type);
196
+ const changed = fold(settings, type, section);
197
+ if (!changed && settings[type] !== undefined && !isSubset(section, settings[type])) {
198
+ report.manual.push({
199
+ step: "settings",
200
+ subject: `.deco/blocks/${files[name]}`,
201
+ message: `CMS.json already has a ${type} section that differs; merge this block into it by hand, then delete it`,
202
+ });
203
+ continue;
204
+ }
205
+ remove.push(files[name]);
206
+ done.push(`the ${type} section from ${files[name]} (deleted)`);
207
+ }
208
+
209
+ // Leftover blocks of the folded built-ins fail deco check.
210
+ for (const [name, block] of Object.entries(blocks)) {
211
+ if (remove.includes(files[name])) continue;
212
+ const type = block.__resolveType;
213
+ if (type === "telemetry" || type === "analytics") {
214
+ report.manual.push({
215
+ step: "settings",
216
+ subject: `.deco/blocks/${files[name]}`,
217
+ message: `the "${type}" built-in is gone: fold this block into the ${type} section of CMS.json (/next/renames-and-migrations#site-settings)`,
218
+ });
219
+ }
220
+ }
221
+
222
+ if (done.length > 0) {
223
+ fs.mkdirSync(blocksDir, { recursive: true });
224
+ const file = files[SETTINGS_BLOCK] ?? blockFileName(SETTINGS_BLOCK);
225
+ const next = serializeBlock(settings);
226
+ const full = path.join(blocksDir, file);
227
+ if (!fs.existsSync(full) || fs.readFileSync(full, "utf8") !== next) {
228
+ fs.writeFileSync(full, next);
229
+ }
230
+ for (const old of remove) fs.rmSync(path.join(blocksDir, old), { force: true });
231
+ report.done.push({
232
+ step: "settings",
233
+ subject: `.deco/blocks/${file}`,
234
+ message: `folded ${done.join("; ")}`,
235
+ });
236
+ }
237
+
238
+ // Settings v7 read from the hosting environment never reach the repository.
239
+ report.manual.push(
240
+ {
241
+ step: "settings",
242
+ subject: "DECO_OTEL_* sampling",
243
+ message:
244
+ "if your hosting sets sampling variables, move them to the telemetry section's errorSampleRate and traceSampleRate, within the limits in code (/next/telemetry#sampling)",
245
+ },
246
+ {
247
+ step: "settings",
248
+ subject: "DECO_ANALYTICS_ENABLED, ONEDOLLAR_ENABLED, ONEDOLLAR_COLLECTOR",
249
+ message:
250
+ "if your hosting sets them, ONEDOLLAR_ENABLED becomes the analytics section's enabled and ONEDOLLAR_COLLECTOR its collector; the Deco Analytics tag renders with decoAnalyticsKey, or with decoAnalyticsFirstParty on a host that routes /_dq/* to the collector (/next/analytics)",
251
+ },
252
+ );
253
+ }
254
+
255
+ /** Whether every field of `part` is already in `whole` with the same value. */
256
+ function isSubset(part: unknown, whole: unknown): boolean {
257
+ if (!isPlainObject(part) || !isPlainObject(whole)) {
258
+ return JSON.stringify(part) === JSON.stringify(whole);
259
+ }
260
+ return Object.entries(part).every(([key, value]) => isSubset(value, whole[key]));
261
+ }