@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,59 @@
1
+ /** The migration's command line: `bun scripts/main.ts` (or `npx tsx scripts/main.ts`). */
2
+ import { parseArgs } from "node:util";
3
+ import { migrate } from "./migrate";
4
+ import { formatReport } from "./report";
5
+ import { VTEX_INVOKE_MODES, VtexInvokeChoiceNeeded, type VtexInvokeMode } from "./vtexInvoke";
6
+
7
+ const USAGE = `Usage: bun scripts/main.ts [--root <dir>] [--decofile <file>] [--vtex-invoke keep|v8]
8
+
9
+ Migrates a v7 Deco site to the next major, in place, and prints what is left
10
+ to do. Run it on a clean working tree (see /next/renames-and-migrations).
11
+
12
+ --root <dir> the app root, with the site's package.json (default: .)
13
+ --decofile <file> content to split into .deco/blocks when the site has none
14
+ (the JSON served at <site>/.decofile)
15
+ --vtex-invoke <mode> the VTEX invoke keys v7's VTEX app answered from its own
16
+ invoke map (invoke.vtex.actions.checkout.addItemsToCart);
17
+ needed only when the site calls them, and it has no default:
18
+ keep vendor v7's handler, so the call works as it did
19
+ v8 no v7 code: @decocms/apps-vtex's client where it has the
20
+ operation, else a stub that rejects and a report line`;
21
+
22
+ async function main(argv: string[]): Promise<number> {
23
+ const { values } = parseArgs({
24
+ args: argv,
25
+ options: {
26
+ root: { type: "string", default: "." },
27
+ decofile: { type: "string" },
28
+ "vtex-invoke": { type: "string" },
29
+ help: { type: "boolean", short: "h" },
30
+ },
31
+ });
32
+ if (values.help) {
33
+ console.log(USAGE);
34
+ return 0;
35
+ }
36
+ const vtexInvoke = values["vtex-invoke"];
37
+ if (vtexInvoke !== undefined && !VTEX_INVOKE_MODES.includes(vtexInvoke as VtexInvokeMode)) {
38
+ throw new Error(`--vtex-invoke takes keep or v8, not "${vtexInvoke}"`);
39
+ }
40
+ const report = await migrate({
41
+ root: values.root!,
42
+ decofile: values.decofile,
43
+ vtexInvoke: vtexInvoke as VtexInvokeMode | undefined,
44
+ });
45
+ console.log(formatReport(report));
46
+ return 0;
47
+ }
48
+
49
+ main(process.argv.slice(2)).then(
50
+ (code) => {
51
+ process.exitCode = code;
52
+ },
53
+ (error) => {
54
+ console.error(error instanceof Error ? error.message : error);
55
+ // A choice the migration needs explains itself; the usage is for a wrong command line.
56
+ if (!(error instanceof VtexInvokeChoiceNeeded)) console.error(USAGE);
57
+ process.exitCode = 1;
58
+ },
59
+ );
@@ -0,0 +1,41 @@
1
+ /**
2
+ * v7's website matchers in the block map. `@decocms/compat-v7/matchers` has
3
+ * each one with v7's results, taking the request explicitly; the binding's
4
+ * compat subpath gives that request (`matcherContext()`, with one A/B
5
+ * decision list per request). The block map registers each matcher the
6
+ * content uses under its v7 name. v7's random matcher isn't one of them, so
7
+ * the binding is the only writer of the visitor's `deco_segment`: its tests
8
+ * move by hand into the saved block `experiments`, as do date windows into
9
+ * `campaigns` (the content step reports both, ./experiments.ts).
10
+ */
11
+ import type { Framework } from "./framework";
12
+
13
+ /** The matchers @decocms/compat-v7/matchers ports, with their props type; `request: false` reads none. */
14
+ const MATCHERS: Record<string, { props: string; request: boolean }> = {
15
+ cookie: { props: "CookieProps", request: true },
16
+ device: { props: "DeviceProps", request: true },
17
+ environment: { props: "never", request: false },
18
+ host: { props: "HostProps", request: true },
19
+ location: { props: "LocationProps", request: true },
20
+ multi: { props: "MultiProps", request: false },
21
+ pathname: { props: "PathnameProps", request: true },
22
+ queryString: { props: "QueryStringProps", request: true },
23
+ };
24
+
25
+ /** The matcher a v7 type name (`website/matchers/device.ts`, with or without `.ts`) names, if ported. */
26
+ export function v7Matcher(type: string): string | undefined {
27
+ const name = /^website\/matchers\/([A-Za-z]+)(?:\.ts)?$/.exec(type)?.[1];
28
+ return name && Object.hasOwn(MATCHERS, name) ? name : undefined;
29
+ }
30
+
31
+ /** The block map's function for one matcher. */
32
+ export function matcherEntry(matcher: string, framework: Framework): string {
33
+ const { props, request } = MATCHERS[matcher];
34
+ if (!request) return `(props: matchers.${props}) => matchers.${matcher}(props)`;
35
+ return framework === "tanstack"
36
+ ? `(props: matchers.${props}) => matchers.${matcher}(props, matcherInput())`
37
+ : `async (props: matchers.${props}) => matchers.${matcher}(props, await matcherContext())`;
38
+ }
39
+
40
+ export const V7_MATCHER_HINT =
41
+ "a v7 matcher: @decocms/compat-v7/matchers has v7's (device, cookie, host, pathname, queryString, location, multi), taking the request; the script registers them on a TanStack Start or Next.js site, through matcherContext() from @decocms/compat-v7/tanstack/server or /nextjs/server. Elsewhere register it under this name with the request your framework gives (/next/matchers-and-variants#matchers); v7's environment matcher read the process environment, which v8 never does: write one in the site that knows where it runs. A random matcher belongs in the saved block experiments (type split-traffic), under its saved-block name, read by the experiment rule ({ name }) (/next/matchers-and-variants#run-an-a-b-test)";
@@ -0,0 +1,180 @@
1
+ /**
2
+ * `migrate`: moves a v7 site to the next major in one pass (spec:
3
+ * renames-and-migrations › Migrating from v7). The steps, in order:
4
+ *
5
+ * 1. content: saved blocks in `.deco/blocks`, v7 generated files removed,
6
+ * v7 async-rendering wrappers (Lazy, Deferred) unwrapped to their sections,
7
+ * legacy type names outside the alias table renamed, v7's random
8
+ * matchers and date rules holding their own dates reported (moved by
9
+ * hand into the saved blocks `experiments` and `campaigns`), every v7
10
+ * redirect block folded into the `Redirects` block (type `redirects`);
11
+ * then site settings folded into the `CMS` block (`.deco/blocks/CMS.json`);
12
+ * 2. secrets: each v7 secret listed, to move to the deploy platform's
13
+ * secrets (content holds none);
14
+ * 3. block map: the site's v7 section-loader registration turned into an
15
+ * export, then `.deco/index.ts` with aliases under the v7 names (v7's
16
+ * matchers from `@decocms/compat-v7/matchers`, `loadSections` running the
17
+ * section loaders), after vendoring the app loaders and actions the
18
+ * content calls, and the view registry `src/views.ts`;
19
+ * 4. invoke: each `/deco/invoke` key the site calls → one server function
20
+ * (`src/server/invoke.functions.ts` or `invoke.actions.ts`), imported at
21
+ * each call site; `ctx.invoke`, app state and `useSection` → the stubs in
22
+ * `src/migration-stubs.ts`;
23
+ * 5. scaffold: `src/cms.ts`, the page loader and the catch-all route for the
24
+ * binding, replacing v7's page routes; on TanStack Start, a report line
25
+ * pointing at the example worker entry to copy, with what v7's
26
+ * `createDecoWorkerEntry` configured (the script writes no worker entry);
27
+ * 6. imports: the codemod over `src/` (v7 helpers to `@decocms/compat-v7`,
28
+ * `ctx.device` from the loader's request, `src/imageUrl.ts` on TanStack);
29
+ * then every `client.list(…)` call and `.deco/blocks` scan reported
30
+ * (`client.entries()` or one `resolve(name)` instead; ./contentReads.ts);
31
+ * 7. scripts: `deco schema && deco content` before dev and build, and
32
+ * `deco schema --check` in the site's CI after the install step.
33
+ *
34
+ * Before the block map, v7's app blocks (`deco-vtex`, …) become the client
35
+ * code that configures each platform's client (`src/clients/<app>.ts`).
36
+ *
37
+ * It changes files in place, so run it on a clean working tree and review the
38
+ * diff. What it can't do is returned in the report.
39
+ */
40
+ import fs from "node:fs";
41
+ import path from "node:path";
42
+ import { writeAppClients } from "./apps";
43
+ import { unwrapAsyncRendering } from "./asyncRendering";
44
+ import { writeBlockMap } from "./blockMap";
45
+ import { addSchemaCheck } from "./ciSchemaCheck";
46
+ import { listSources, rewriteDeviceReads } from "./compat";
47
+ import { moveContent } from "./content";
48
+ import { reportContentReads } from "./contentReads";
49
+ import { reportTestsAndDates } from "./experiments";
50
+ import { siteFramework, V8_RANGE } from "./framework";
51
+ import { rewriteImports } from "./imports";
52
+ import { rewriteInvokes, vtexInvokeMapKeysUsed } from "./invoke";
53
+ import { renameLegacyTypes } from "./legacyNames";
54
+ import { foldRedirects } from "./redirects";
55
+ import { createReport, type Report } from "./report";
56
+ import { reportWorkerEntry, writeScaffold } from "./scaffold";
57
+ import { listSecrets } from "./secrets";
58
+ import { wireSectionLoaders } from "./sectionLoaders";
59
+ import { foldSiteSettings } from "./siteSettings";
60
+ import { rewriteLoaderContext, rewritePartialImports, writeStubs } from "./stubs";
61
+ import { VtexInvokeChoiceNeeded, type VtexInvokeMode } from "./vtexInvoke";
62
+
63
+ interface MigrateOptions {
64
+ /** The app root: the folder with the site's package.json. */
65
+ root: string;
66
+ /** A decofile to split into `.deco/blocks` when the site has none, relative to the root. */
67
+ decofile?: string;
68
+ /**
69
+ * What the VTEX invoke keys v7's VTEX app answered from its own invoke map
70
+ * become: `"keep"` vendors v7's handler, `"v8"` moves them to
71
+ * `@decocms/apps-vtex`'s client with no v7 code (./vtexInvoke.ts). No
72
+ * default: a site that calls such keys without a choice gets
73
+ * `VtexInvokeChoiceNeeded` before anything is written.
74
+ */
75
+ vtexInvoke?: VtexInvokeMode;
76
+ }
77
+
78
+ /** The scripts /next/cli#run-it-before-dev-and-build asks for. */
79
+ const SCRIPTS: Record<string, string> = {
80
+ predev: "deco schema && deco content",
81
+ prebuild: "deco schema && deco content && deco check",
82
+ };
83
+
84
+ function addScripts(root: string, report: Report): void {
85
+ const file = path.join(root, "package.json");
86
+ const manifest = JSON.parse(fs.readFileSync(file, "utf8"));
87
+ manifest.scripts ??= {};
88
+ const added: string[] = [];
89
+ for (const [name, command] of Object.entries(SCRIPTS)) {
90
+ const current = manifest.scripts[name];
91
+ if (current === undefined) {
92
+ manifest.scripts[name] = command;
93
+ added.push(name);
94
+ } else if (!current.includes(command)) {
95
+ report.manual.push({
96
+ step: "scripts",
97
+ subject: `package.json ${name}`,
98
+ message: `run ${command} in it (/next/cli#run-it-before-dev-and-build)`,
99
+ });
100
+ }
101
+ }
102
+ if (added.length > 0) {
103
+ fs.writeFileSync(file, `${JSON.stringify(manifest, null, 2)}\n`);
104
+ report.done.push({
105
+ step: "scripts",
106
+ subject: "package.json",
107
+ message: `added ${added.join(" and ")}`,
108
+ });
109
+ }
110
+ const framework = siteFramework(root);
111
+ const packages = [`@decocms/blocks ${V8_RANGE}`];
112
+ if (framework) {
113
+ packages.push(
114
+ `@decocms/${framework} ${V8_RANGE} (the v8 binding, released with blocks at one version)`,
115
+ );
116
+ }
117
+ const written = siteImports(root);
118
+ if (written.has("@decocms/compat-v7")) {
119
+ packages.push(`@decocms/compat-v7 ${V8_RANGE} (v7's helpers, released with them)`);
120
+ }
121
+ for (const client of ["@decocms/apps-vtex", "@decocms/apps-shopify"]) {
122
+ if (written.has(client)) packages.push(`${client} ${V8_RANGE}`);
123
+ }
124
+ report.manual.push({
125
+ step: "scripts",
126
+ subject: "package.json",
127
+ message: `depend on ${packages.join(", ")}; drop the v7 codegen from build, and @decocms/apps-commerce, @decocms/blocks-admin and @decocms/blocks-cli once nothing imports them`,
128
+ });
129
+ }
130
+
131
+ /** The `@decocms/*` packages the site's code and block map import after the migration. */
132
+ function siteImports(root: string): Set<string> {
133
+ const found = new Set<string>();
134
+ const files = [...listSources(path.join(root, "src")), path.join(root, ".deco", "index.ts")];
135
+ for (const file of files) {
136
+ if (!fs.existsSync(file)) continue;
137
+ for (const [, name] of fs.readFileSync(file, "utf8").matchAll(/["'](@decocms\/[^/"']+)/g)) {
138
+ found.add(name);
139
+ }
140
+ }
141
+ return found;
142
+ }
143
+
144
+ export async function migrate(options: MigrateOptions): Promise<Report> {
145
+ const root = path.resolve(options.root);
146
+ if (!fs.existsSync(path.join(root, "package.json"))) {
147
+ throw new Error(`no package.json in ${root}; pass the app root with --root`);
148
+ }
149
+ if (!options.vtexInvoke) {
150
+ const keys = vtexInvokeMapKeysUsed(root);
151
+ if (keys.length > 0) throw new VtexInvokeChoiceNeeded(keys);
152
+ }
153
+ const report = createReport();
154
+ moveContent(root, report, { decofile: options.decofile });
155
+ unwrapAsyncRendering(root, report);
156
+ renameLegacyTypes(root, report);
157
+ foldRedirects(root, report);
158
+ reportTestsAndDates(root, report);
159
+ const apps = writeAppClients(root, report);
160
+ foldSiteSettings(root, report);
161
+ listSecrets(root, report);
162
+ const wiring = wireSectionLoaders(root, report);
163
+ const { vendored } = writeBlockMap(root, report, wiring, apps);
164
+ const stubs = new Set([
165
+ ...rewriteInvokes(root, report, vendored, { vtexInvoke: options.vtexInvoke }),
166
+ ...rewriteLoaderContext(root, report),
167
+ ...rewritePartialImports(root, report),
168
+ ]);
169
+ writeStubs(root, stubs, report);
170
+ rewriteDeviceReads(root, report);
171
+ writeScaffold(root, report);
172
+ if (siteFramework(root) === "tanstack") {
173
+ reportWorkerEntry(root, report);
174
+ }
175
+ rewriteImports(root, report, vendored);
176
+ reportContentReads(root, report);
177
+ addScripts(root, report);
178
+ addSchemaCheck(root, report);
179
+ return report;
180
+ }