@decocms/blocks 8.10.0 → 8.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/v8/cli/index.d.ts +1 -0
- package/dist/v8/cli/index.d.ts.map +1 -1
- package/dist/v8/cli/index.js +1 -0
- package/dist/v8/cli/index.js.map +1 -1
- package/dist/v8/cli/run.d.ts +2 -0
- package/dist/v8/cli/run.d.ts.map +1 -1
- package/dist/v8/cli/run.js +30 -1
- package/dist/v8/cli/run.js.map +1 -1
- package/dist/v8/cli/skills.d.ts +25 -0
- package/dist/v8/cli/skills.d.ts.map +1 -0
- package/dist/v8/cli/skills.js +70 -0
- package/dist/v8/cli/skills.js.map +1 -0
- package/package.json +3 -2
- package/skills/deco-apps/SKILL.md +82 -0
- package/skills/deco-apps/references/algolia.md +51 -0
- package/skills/deco-apps/references/commerce-types.md +132 -0
- package/skills/deco-apps/references/instrumented-fetch.md +144 -0
- package/skills/deco-apps/references/magento.md +55 -0
- package/skills/deco-apps/references/messaging.md +76 -0
- package/skills/deco-apps/references/shopify.md +69 -0
- package/skills/deco-apps/references/vendored-loaders.md +135 -0
- package/skills/deco-apps/references/vtex-cookies-auth.md +146 -0
- package/skills/deco-apps/references/vtex.md +150 -0
- package/skills/deco-apps/references/wake.md +75 -0
- package/skills/deco-cms-schema/SKILL.md +80 -0
- package/skills/deco-cms-schema/references/annotations.md +147 -0
- package/skills/deco-cms-schema/references/content-hygiene.md +91 -0
- package/skills/deco-cms-schema/references/formats-and-widgets.md +112 -0
- package/skills/deco-cms-schema/references/shapes-that-break-the-form.md +165 -0
- package/skills/deco-cms-schema/references/types-to-fields.md +112 -0
- package/skills/deco-go-live/SKILL.md +127 -0
- package/skills/deco-go-live/references/content-releases.md +90 -0
- package/skills/deco-go-live/references/process.md +106 -0
- package/skills/deco-go-live/references/proxy-headers-checkout.md +139 -0
- package/skills/deco-go-live/references/qa-checklist-template.md +150 -0
- package/skills/deco-go-live/references/qa-inventory.md +120 -0
- package/skills/deco-go-live/references/react-spa.md +183 -0
- package/skills/deco-go-live/references/seo-analytics.md +114 -0
- package/skills/deco-go-live/references/traffic-ramp.md +87 -0
- package/skills/deco-go-live/references/worker-runtime.md +93 -0
- package/skills/deco-go-live/scripts/preflight.sh +221 -0
- package/skills/deco-images-media/SKILL.md +64 -0
- package/skills/deco-images-media/references/asset-upload.md +191 -0
- package/skills/deco-images-media/references/components-per-binding.md +167 -0
- package/skills/deco-images-media/references/providers-and-cdn.md +145 -0
- package/skills/deco-images-media/references/quality-and-lcp.md +93 -0
- package/skills/deco-images-media/references/troubleshooting.md +147 -0
- package/skills/deco-images-media/references/video.md +122 -0
- package/skills/deco-make-editable/SKILL.md +94 -0
- package/skills/deco-make-editable/references/a11y-and-e2e.md +104 -0
- package/skills/deco-make-editable/references/content-shapes.md +198 -0
- package/skills/deco-make-editable/references/from-figma.md +110 -0
- package/skills/deco-make-editable/references/i18n-fields.md +112 -0
- package/skills/deco-make-editable/references/inventory.md +79 -0
- package/skills/deco-make-editable/references/pixel-parity.md +175 -0
- package/skills/deco-make-editable/references/seeding-content.md +119 -0
- package/skills/deco-nextjs-site/SKILL.md +74 -0
- package/skills/deco-nextjs-site/references/caching.md +116 -0
- package/skills/deco-nextjs-site/references/metadata-seo.md +100 -0
- package/skills/deco-nextjs-site/references/proxy-and-drafts.md +177 -0
- package/skills/deco-nextjs-site/references/rendering.md +171 -0
- package/skills/deco-nextjs-site/references/setup.md +190 -0
- package/skills/deco-nextjs-site/references/studio-bridge.md +130 -0
- package/skills/deco-site-authoring/SKILL.md +75 -0
- package/skills/deco-site-authoring/references/lazy-blocks.md +155 -0
- package/skills/deco-site-authoring/references/loaders.md +192 -0
- package/skills/deco-site-authoring/references/pages.md +156 -0
- package/skills/deco-site-authoring/references/review-before-publish.md +95 -0
- package/skills/deco-site-authoring/references/sections.md +158 -0
- package/skills/deco-site-authoring/references/seo.md +116 -0
- package/skills/deco-site-workflow/SKILL.md +92 -0
- package/skills/deco-site-workflow/references/check.md +116 -0
- package/skills/deco-site-workflow/references/cli-reference.md +104 -0
- package/skills/deco-site-workflow/references/content-layout.md +93 -0
- package/skills/deco-site-workflow/references/content-sync.md +146 -0
- package/skills/deco-site-workflow/references/delivery.md +51 -0
- package/skills/deco-site-workflow/references/deploy.md +87 -0
- package/skills/deco-site-workflow/references/schema.md +173 -0
- package/skills/deco-site-workflow/references/serve-and-studio.md +138 -0
- package/skills/deco-storefront-caching/SKILL.md +67 -0
- package/skills/deco-storefront-caching/references/cache-keys.md +129 -0
- package/skills/deco-storefront-caching/references/case-study.md +82 -0
- package/skills/deco-storefront-caching/references/egress-traps.md +153 -0
- package/skills/deco-storefront-caching/references/nextjs.md +60 -0
- package/skills/deco-storefront-caching/references/perf-guardrails-e2e.md +123 -0
- package/skills/deco-storefront-caching/references/verification.md +136 -0
- package/skills/deco-storefront-caching/references/worker-page-cache.md +160 -0
- package/skills/deco-storefront-caching/scripts/html-anatomy.mjs +97 -0
- package/skills/deco-storefront-performance/SKILL.md +81 -0
- package/skills/deco-storefront-performance/references/cls-third-party.md +90 -0
- package/skills/deco-storefront-performance/references/hydration.md +116 -0
- package/skills/deco-storefront-performance/references/loaders-n-plus-1.md +107 -0
- package/skills/deco-storefront-performance/references/navigation-ux.md +130 -0
- package/skills/deco-storefront-performance/references/payload-trim.md +163 -0
- package/skills/deco-storefront-performance/references/render-location.md +152 -0
- package/skills/deco-storefront-performance/references/request-dedupe.md +147 -0
- package/skills/deco-storefront-performance/references/trace-reading.md +79 -0
- package/skills/deco-storefront-performance/references/worker-memory.md +176 -0
- package/skills/deco-tanstack-site/SKILL.md +68 -0
- package/skills/deco-tanstack-site/references/document-and-security.md +155 -0
- package/skills/deco-tanstack-site/references/router-and-navigation.md +137 -0
- package/skills/deco-tanstack-site/references/routes-and-pages.md +183 -0
- package/skills/deco-tanstack-site/references/server-functions.md +123 -0
- package/skills/deco-tanstack-site/references/setup.md +153 -0
- package/skills/deco-tanstack-site/references/worker-entry.md +152 -0
- package/skills/deco-v7-to-v8-migration/SKILL.md +122 -0
- package/skills/deco-v7-to-v8-migration/references/caching-and-privacy.md +10 -0
- package/skills/deco-v7-to-v8-migration/references/content.md +25 -0
- package/skills/deco-v7-to-v8-migration/references/editor-forms.md +27 -0
- package/skills/deco-v7-to-v8-migration/references/faststore.md +40 -0
- package/skills/deco-v7-to-v8-migration/references/framework-code.md +53 -0
- package/skills/deco-v7-to-v8-migration/references/hosted-releases.md +16 -0
- package/skills/deco-v7-to-v8-migration/references/import-map.md +81 -0
- package/skills/deco-v7-to-v8-migration/references/legacy-names.md +33 -0
- package/skills/deco-v7-to-v8-migration/references/manual-steps.md +10 -0
- package/skills/deco-v7-to-v8-migration/references/native-apps.md +43 -0
- package/skills/deco-v7-to-v8-migration/references/nextjs.md +19 -0
- package/skills/deco-v7-to-v8-migration/references/parity.md +66 -0
- package/skills/deco-v7-to-v8-migration/references/rendering.md +13 -0
- package/skills/deco-v7-to-v8-migration/references/running-the-script.md +29 -0
- package/skills/deco-v7-to-v8-migration/references/script-steps.md +13 -0
- package/skills/deco-v7-to-v8-migration/references/server-functions.md +15 -0
- package/skills/deco-v7-to-v8-migration/references/sibling-port.md +11 -0
- package/skills/deco-v7-to-v8-migration/references/site-code.md +15 -0
- package/skills/deco-v7-to-v8-migration/references/telemetry.md +10 -0
- package/skills/deco-v7-to-v8-migration/references/verify.md +20 -0
- package/skills/deco-v7-to-v8-migration/scripts/apps.ts +297 -0
- package/skills/deco-v7-to-v8-migration/scripts/asyncRendering.ts +73 -0
- package/skills/deco-v7-to-v8-migration/scripts/blockMap.ts +584 -0
- package/skills/deco-v7-to-v8-migration/scripts/ciSchemaCheck.ts +145 -0
- package/skills/deco-v7-to-v8-migration/scripts/compat.ts +357 -0
- package/skills/deco-v7-to-v8-migration/scripts/content.ts +121 -0
- package/skills/deco-v7-to-v8-migration/scripts/experiments.ts +81 -0
- package/skills/deco-v7-to-v8-migration/scripts/framework.ts +61 -0
- package/skills/deco-v7-to-v8-migration/scripts/imageCopy.ts +276 -0
- package/skills/deco-v7-to-v8-migration/scripts/imageModule.ts +285 -0
- package/skills/deco-v7-to-v8-migration/scripts/imports.ts +942 -0
- package/skills/deco-v7-to-v8-migration/scripts/invoke.ts +624 -0
- package/skills/deco-v7-to-v8-migration/scripts/legacyNames.ts +45 -0
- package/skills/deco-v7-to-v8-migration/scripts/main.ts +59 -0
- package/skills/deco-v7-to-v8-migration/scripts/matchers.ts +41 -0
- package/skills/deco-v7-to-v8-migration/scripts/migrate.ts +176 -0
- package/skills/deco-v7-to-v8-migration/scripts/redirects.ts +455 -0
- package/skills/deco-v7-to-v8-migration/scripts/report.ts +50 -0
- package/skills/deco-v7-to-v8-migration/scripts/scaffold.ts +1119 -0
- package/skills/deco-v7-to-v8-migration/scripts/secrets.ts +35 -0
- package/skills/deco-v7-to-v8-migration/scripts/sectionLoaders.ts +216 -0
- package/skills/deco-v7-to-v8-migration/scripts/siteSettings.ts +261 -0
- package/skills/deco-v7-to-v8-migration/scripts/stubs.ts +388 -0
- package/skills/deco-v7-to-v8-migration/scripts/tsconfig.json +7 -0
- package/skills/deco-v7-to-v8-migration/scripts/vendor.ts +249 -0
- package/skills/deco-v7-to-v8-migration/scripts/vtexInvoke.ts +240 -0
- package/skills/deco-v7-to-v8-migration/scripts/walk.ts +30 -0
- package/skills/deco-variants/SKILL.md +57 -0
- package/skills/deco-variants/references/caching-variants.md +63 -0
- package/skills/deco-variants/references/custom-matchers.md +83 -0
- package/skills/deco-variants/references/draft-preview.md +59 -0
- package/skills/deco-variants/references/experiments-and-campaigns.md +104 -0
- package/skills/deco-variants/references/multivariate.md +85 -0
- package/skills/deco-variants/references/v7-matchers.md +71 -0
- package/src/v8/cli/index.ts +1 -0
- package/src/v8/cli/run.ts +35 -1
- package/src/v8/cli/skills.ts +91 -0
|
@@ -0,0 +1,624 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/deco/invoke` → server functions (spec: renames-and-migrations › Loaders,
|
|
3
|
+
* actions and invoke). v7's browser code called loaders and actions through
|
|
4
|
+
* an invoke proxy (`invoke.site.actions.x(props)`, `invoke["vtex/loaders/y"](props)`,
|
|
5
|
+
* `invoke("key", props)`), which POSTed to `/deco/invoke/<key>`, and the
|
|
6
|
+
* endpoint called the handler as `handler(props, request)`. The next major has
|
|
7
|
+
* no endpoint, so each key the site calls becomes one server function that
|
|
8
|
+
* calls the same handler the same way, and each call site imports it:
|
|
9
|
+
*
|
|
10
|
+
* - TanStack Start: `src/server/invoke.functions.ts`, `createServerFn` per
|
|
11
|
+
* key; a loader is a GET marked private (`markPrivate`, so the worker never
|
|
12
|
+
* shares it), an action a POST; `invoke.a.b(props)` → `$aB({ data: props })`;
|
|
13
|
+
* - Next.js: `src/server/invoke.actions.ts` (`"use server"`), one async
|
|
14
|
+
* function per key; `invoke.a.b(props)` → `$aB(props)`.
|
|
15
|
+
*
|
|
16
|
+
* A key resolves to its handler module as v7's did: `site/<path>` →
|
|
17
|
+
* `src/<path>`, an app's key → the app's loader or action, vendored into
|
|
18
|
+
* `src/vendor` like the block map's. A key v7's VTEX app answered from its
|
|
19
|
+
* own invoke map (a named export, `vtex/actions/checkout/addItemsToCart`)
|
|
20
|
+
* follows the migration's `vtexInvoke` choice (./vtexInvoke.ts). A key with
|
|
21
|
+
* no handler file becomes `unportedInvoke(key, props)` (src/migration-stubs.ts),
|
|
22
|
+
* which rejects; a key the script can't read (a computed property) is
|
|
23
|
+
* reported and left.
|
|
24
|
+
*
|
|
25
|
+
* Only v7's dispatcher is rewritten: `invoke` from `@decocms/blocks/sdk/invoke`
|
|
26
|
+
* (or `@decocms/tanstack`), or from a site module that fetches `/deco/invoke`
|
|
27
|
+
* (v7 templates' `src/runtime.ts`, and its `Runtime.invoke`). A site-local
|
|
28
|
+
* invoke tree of server functions is site code and is left alone.
|
|
29
|
+
*/
|
|
30
|
+
import fs from "node:fs";
|
|
31
|
+
import path from "node:path";
|
|
32
|
+
import ts from "typescript";
|
|
33
|
+
import { applyEdits, type Edit, importInsertion, listSources, parse } from "./compat";
|
|
34
|
+
import { type Framework, siteFramework } from "./framework";
|
|
35
|
+
import type { Report } from "./report";
|
|
36
|
+
import { chainKey, normalizeKey, propertyChain, type Stub, stubImport } from "./stubs";
|
|
37
|
+
import { firstFile, locateAppModule, relativeSpecifier, vendorModule } from "./vendor";
|
|
38
|
+
import {
|
|
39
|
+
V8_VTEX_OPERATIONS,
|
|
40
|
+
VtexInvokeChoiceNeeded,
|
|
41
|
+
type VtexInvokeMode,
|
|
42
|
+
v8Operation,
|
|
43
|
+
vtexInvokeMapKey,
|
|
44
|
+
writeV8Module,
|
|
45
|
+
} from "./vtexInvoke";
|
|
46
|
+
|
|
47
|
+
const FUNCTIONS_FILE: Record<Framework, string> = {
|
|
48
|
+
tanstack: path.join("src", "server", "invoke.functions.ts"),
|
|
49
|
+
nextjs: path.join("src", "server", "invoke.actions.ts"),
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/** v7 packages whose `invoke` is the `/deco/invoke` proxy. */
|
|
53
|
+
const V7_INVOKE =
|
|
54
|
+
/^@decocms\/(blocks\/sdk\/invoke|blocks\/sdk|tanstack|tanstack\/sdk\/createInvoke)$/;
|
|
55
|
+
|
|
56
|
+
const HAS_DEFAULT = /\bexport\s+default\b|\bexport\s*\{[^}]*\bdefault\b/;
|
|
57
|
+
|
|
58
|
+
interface Root {
|
|
59
|
+
/** `invoke` (a proxy) or `Runtime` (an object whose `.invoke` is one). */
|
|
60
|
+
kind: "invoke" | "runtime";
|
|
61
|
+
/** The import declaration and specifier that bind it. */
|
|
62
|
+
decl: ts.ImportDeclaration;
|
|
63
|
+
element: ts.ImportSpecifier;
|
|
64
|
+
/** The module it came from, for the report. */
|
|
65
|
+
from: string;
|
|
66
|
+
/** A site module (src/runtime.ts) that implements the proxy. */
|
|
67
|
+
file?: string;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** The site file a relative or `~/` import names. */
|
|
71
|
+
function siteModule(root: string, from: string, specifier: string): string | undefined {
|
|
72
|
+
const base = specifier.startsWith("~/")
|
|
73
|
+
? path.join(root, "src", specifier.slice(2))
|
|
74
|
+
: specifier.startsWith(".")
|
|
75
|
+
? path.resolve(path.dirname(from), specifier)
|
|
76
|
+
: undefined;
|
|
77
|
+
if (!base) return undefined;
|
|
78
|
+
const clean = base.replace(/\.(js|tsx?)$/, "");
|
|
79
|
+
return firstFile([
|
|
80
|
+
`${clean}.ts`,
|
|
81
|
+
`${clean}.tsx`,
|
|
82
|
+
path.join(clean, "index.ts"),
|
|
83
|
+
path.join(clean, "index.tsx"),
|
|
84
|
+
]);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** A site module that is v7's invoke proxy: it fetches `/deco/invoke` and defines no server functions. */
|
|
88
|
+
function isProxyModule(file: string): boolean {
|
|
89
|
+
const text = fs.readFileSync(file, "utf8");
|
|
90
|
+
return (
|
|
91
|
+
text.includes("/deco/invoke") &&
|
|
92
|
+
!text.includes("createServerFn") &&
|
|
93
|
+
!/["']use server["']/.test(text)
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function invokeRoots(root: string, file: string, source: ts.SourceFile): Map<string, Root> {
|
|
98
|
+
const roots = new Map<string, Root>();
|
|
99
|
+
for (const s of source.statements) {
|
|
100
|
+
if (!ts.isImportDeclaration(s) || !ts.isStringLiteral(s.moduleSpecifier)) continue;
|
|
101
|
+
if (s.importClause?.isTypeOnly) continue;
|
|
102
|
+
const specifier = s.moduleSpecifier.text;
|
|
103
|
+
const bindings = s.importClause?.namedBindings;
|
|
104
|
+
if (!bindings || !ts.isNamedImports(bindings)) continue;
|
|
105
|
+
let proxyFile: string | undefined;
|
|
106
|
+
if (!V7_INVOKE.test(specifier)) {
|
|
107
|
+
proxyFile = siteModule(root, file, specifier);
|
|
108
|
+
if (!proxyFile || !isProxyModule(proxyFile)) continue;
|
|
109
|
+
}
|
|
110
|
+
for (const element of bindings.elements) {
|
|
111
|
+
if (element.isTypeOnly) continue;
|
|
112
|
+
const imported = (element.propertyName ?? element.name).text;
|
|
113
|
+
const kind =
|
|
114
|
+
imported === "invoke" ? "invoke" : imported === "Runtime" ? "runtime" : undefined;
|
|
115
|
+
if (kind)
|
|
116
|
+
roots.set(element.name.text, { kind, decl: s, element, from: specifier, file: proxyFile });
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return roots;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** One invoke call found in a file. */
|
|
123
|
+
interface Call {
|
|
124
|
+
node: ts.CallExpression;
|
|
125
|
+
key?: string;
|
|
126
|
+
/** The props argument's text, if any. */
|
|
127
|
+
props?: string;
|
|
128
|
+
line: number;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** `site/loaders/gateway/wishlist/add-item` → `$siteLoadersGatewayWishlistAddItem`. */
|
|
132
|
+
function functionName(key: string): string {
|
|
133
|
+
const words = key.split(/[^A-Za-z0-9]+/).filter(Boolean);
|
|
134
|
+
const name = words
|
|
135
|
+
.map((w, i) => (i === 0 ? w[0].toLowerCase() + w.slice(1) : w[0].toUpperCase() + w.slice(1)))
|
|
136
|
+
.join("");
|
|
137
|
+
return `$${name || "invoke"}`;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
interface Handler {
|
|
141
|
+
key: string;
|
|
142
|
+
file: string;
|
|
143
|
+
/** The module's export that handles it, when not the default (v7's VTEX invoke map). */
|
|
144
|
+
exportName?: string;
|
|
145
|
+
name: string;
|
|
146
|
+
/** "GET" for a loader, "POST" for anything else (v7's invoke was always a POST). */
|
|
147
|
+
method: "GET" | "POST";
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** A site file's invoke calls: the keys it names, and the lines of the ones it builds at run time. */
|
|
151
|
+
interface FoundInvokes {
|
|
152
|
+
file: string;
|
|
153
|
+
text: string;
|
|
154
|
+
source: ts.SourceFile;
|
|
155
|
+
roots: Map<string, Root>;
|
|
156
|
+
calls: Call[];
|
|
157
|
+
dynamic: number[];
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Every v7 invoke call under `src/`, read only. */
|
|
161
|
+
function findInvokeCalls(root: string): FoundInvokes[] {
|
|
162
|
+
const sources = listSources(path.join(root, "src"));
|
|
163
|
+
const found: FoundInvokes[] = [];
|
|
164
|
+
|
|
165
|
+
for (const file of sources) {
|
|
166
|
+
const text = fs.readFileSync(file, "utf8");
|
|
167
|
+
if (!/\binvoke\b|\bRuntime\b/.test(text)) continue;
|
|
168
|
+
const source = parse(file, text);
|
|
169
|
+
const roots = invokeRoots(root, file, source);
|
|
170
|
+
if (roots.size === 0) continue;
|
|
171
|
+
const calls: Call[] = [];
|
|
172
|
+
const dynamic: number[] = [];
|
|
173
|
+
const line = (node: ts.Node) =>
|
|
174
|
+
source.getLineAndCharacterOfPosition(node.getStart(source)).line + 1;
|
|
175
|
+
const visit = (node: ts.Node) => {
|
|
176
|
+
if (ts.isCallExpression(node)) {
|
|
177
|
+
const chain = propertyChain(node.expression);
|
|
178
|
+
const rootOf = chain && roots.get(chain.root.text);
|
|
179
|
+
if (chain && rootOf) {
|
|
180
|
+
let segments = chain.segments;
|
|
181
|
+
if (rootOf.kind === "runtime") {
|
|
182
|
+
if (segments[0] !== "invoke") {
|
|
183
|
+
ts.forEachChild(node, visit);
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
segments = segments.slice(1);
|
|
187
|
+
}
|
|
188
|
+
if (segments.length === 0) {
|
|
189
|
+
// invoke("key", props)
|
|
190
|
+
const [key, props] = node.arguments;
|
|
191
|
+
if (key && (ts.isStringLiteral(key) || ts.isNoSubstitutionTemplateLiteral(key))) {
|
|
192
|
+
calls.push({
|
|
193
|
+
node,
|
|
194
|
+
key: normalizeKey(key.text),
|
|
195
|
+
props: props?.getText(source),
|
|
196
|
+
line: line(node),
|
|
197
|
+
});
|
|
198
|
+
} else {
|
|
199
|
+
dynamic.push(line(node));
|
|
200
|
+
}
|
|
201
|
+
} else {
|
|
202
|
+
const key = chainKey(segments);
|
|
203
|
+
if (key)
|
|
204
|
+
calls.push({
|
|
205
|
+
node,
|
|
206
|
+
key,
|
|
207
|
+
props: node.arguments[0]?.getText(source),
|
|
208
|
+
line: line(node),
|
|
209
|
+
});
|
|
210
|
+
else dynamic.push(line(node));
|
|
211
|
+
}
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
ts.forEachChild(node, visit);
|
|
216
|
+
};
|
|
217
|
+
visit(source);
|
|
218
|
+
if (calls.length > 0 || dynamic.length > 0)
|
|
219
|
+
found.push({ file, text, source, roots, calls, dynamic });
|
|
220
|
+
}
|
|
221
|
+
return found;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* The keys of v7's VTEX invoke map (./vtexInvoke.ts) the site calls, sorted,
|
|
226
|
+
* read before the migration writes anything: when there are any, the
|
|
227
|
+
* migration needs `--vtex-invoke`. A site on no v8 binding the script knows
|
|
228
|
+
* keeps its invoke calls as they are, so it needs no choice.
|
|
229
|
+
*/
|
|
230
|
+
export function vtexInvokeMapKeysUsed(root: string): string[] {
|
|
231
|
+
if (!siteFramework(root)) return [];
|
|
232
|
+
const keys = new Set<string>();
|
|
233
|
+
for (const { calls } of findInvokeCalls(root)) {
|
|
234
|
+
for (const { key } of calls) {
|
|
235
|
+
if (
|
|
236
|
+
key !== undefined &&
|
|
237
|
+
/^vtex\/(loaders|actions)\//.test(key) &&
|
|
238
|
+
!locateAppModule(root, key) &&
|
|
239
|
+
vtexInvokeMapKey(root, key)
|
|
240
|
+
) {
|
|
241
|
+
keys.add(key);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return [...keys].sort();
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Rewrites v7's invoke calls under `src/` into server functions. Returns the
|
|
250
|
+
* stubs it used. `vendored` is the block map's vendored modules (shared, so
|
|
251
|
+
* an app loader both use is copied once).
|
|
252
|
+
*/
|
|
253
|
+
export function rewriteInvokes(
|
|
254
|
+
root: string,
|
|
255
|
+
report: Report,
|
|
256
|
+
vendored: Map<string, string>,
|
|
257
|
+
options: { vtexInvoke?: VtexInvokeMode } = {},
|
|
258
|
+
): Set<Stub> {
|
|
259
|
+
const vtexInvoke = options.vtexInvoke;
|
|
260
|
+
const framework = siteFramework(root);
|
|
261
|
+
const stubs = new Set<Stub>();
|
|
262
|
+
const found = findInvokeCalls(root);
|
|
263
|
+
if (found.length === 0) return stubs;
|
|
264
|
+
|
|
265
|
+
const rel = (file: string) => path.relative(root, file).split(path.sep).join("/");
|
|
266
|
+
if (!framework) {
|
|
267
|
+
const where = found.map((f) => rel(f.file));
|
|
268
|
+
report.manual.push({
|
|
269
|
+
step: "invoke",
|
|
270
|
+
subject: `/deco/invoke calls (${where.slice(0, 3).join(", ")}${where.length > 3 ? `, +${where.length - 3} more` : ""})`,
|
|
271
|
+
message:
|
|
272
|
+
"the site runs on no v8 binding the script knows, so they were left: make each key a server function or route handler of your framework that calls its handler with (props, request), and import it at the call site (references/server-functions.md)",
|
|
273
|
+
});
|
|
274
|
+
return stubs;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// Each key's handler: the site's module, or the app's vendored copy.
|
|
278
|
+
const handlers = new Map<string, Handler | null>();
|
|
279
|
+
const names = new Set<string>();
|
|
280
|
+
/** VTEX invoke-map keys: on the v8 client, or why not (`--vtex-invoke v8`); vendored (`keep`). */
|
|
281
|
+
const v8Mapped: string[] = [];
|
|
282
|
+
const v8Unmapped = new Map<string, string>();
|
|
283
|
+
const kept = new Map<string, string>();
|
|
284
|
+
const v8File = path.join(root, "src", "server", "vtex-invoke.server.ts");
|
|
285
|
+
const handlerName = (key: string) => {
|
|
286
|
+
let name = functionName(key);
|
|
287
|
+
for (let n = 2; names.has(name); n++) name = `${functionName(key)}${n}`;
|
|
288
|
+
names.add(name);
|
|
289
|
+
return name;
|
|
290
|
+
};
|
|
291
|
+
const method = (key: string) => (/(^|\/)loaders\//.test(key) ? "GET" : "POST");
|
|
292
|
+
const resolve = (key: string): Handler | null => {
|
|
293
|
+
if (handlers.has(key)) return handlers.get(key)!;
|
|
294
|
+
const mapKey =
|
|
295
|
+
/^vtex\/(loaders|actions)\//.test(key) && !locateAppModule(root, key)
|
|
296
|
+
? vtexInvokeMapKey(root, key)
|
|
297
|
+
: undefined;
|
|
298
|
+
if (mapKey) {
|
|
299
|
+
// migrate() asks for the choice before writing anything (vtexInvokeMapKeysUsed).
|
|
300
|
+
if (!vtexInvoke) throw new VtexInvokeChoiceNeeded([key]);
|
|
301
|
+
let handler: Handler | null = null;
|
|
302
|
+
if (vtexInvoke === "keep") {
|
|
303
|
+
const before = vendored.size;
|
|
304
|
+
const file = vendorModule(root, mapKey.module, vendored, report);
|
|
305
|
+
if (vendored.size > before) {
|
|
306
|
+
report.done.push({
|
|
307
|
+
step: "vendor",
|
|
308
|
+
subject: mapKey.moduleKey,
|
|
309
|
+
message: `copied to ${rel(file)} for v7's VTEX invoke keys (--vtex-invoke keep); rewrite it over the platform's upstream client (/next/upstream-clients)`,
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
kept.set(key, `${rel(file)} ${mapKey.exportName}`);
|
|
313
|
+
handler = {
|
|
314
|
+
key,
|
|
315
|
+
file,
|
|
316
|
+
exportName: mapKey.exportName,
|
|
317
|
+
name: handlerName(key),
|
|
318
|
+
method: method(key),
|
|
319
|
+
};
|
|
320
|
+
} else {
|
|
321
|
+
const v8 = v8Operation(key, framework);
|
|
322
|
+
if ("operation" in v8) {
|
|
323
|
+
v8Mapped.push(key);
|
|
324
|
+
handler = {
|
|
325
|
+
key,
|
|
326
|
+
file: v8File,
|
|
327
|
+
exportName: v8.operation.name,
|
|
328
|
+
name: handlerName(key),
|
|
329
|
+
method: method(key),
|
|
330
|
+
};
|
|
331
|
+
} else {
|
|
332
|
+
v8Unmapped.set(key, v8.reason);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
handlers.set(key, handler);
|
|
336
|
+
return handler;
|
|
337
|
+
}
|
|
338
|
+
let file: string | undefined;
|
|
339
|
+
const [namespace, ...rest] = key.split("/");
|
|
340
|
+
if (namespace === "site" && !rest.includes("..")) {
|
|
341
|
+
const base = path.join(root, "src", ...rest);
|
|
342
|
+
file = firstFile([`${base}.ts`, `${base}.tsx`, path.join(base, "index.ts")]);
|
|
343
|
+
} else if (/^[a-z0-9-]+\/(loaders|actions)\//.test(key)) {
|
|
344
|
+
const located = locateAppModule(root, key);
|
|
345
|
+
// Only a module whose default export is the handler: v7's apps answered other keys from
|
|
346
|
+
// their own invoke maps, which have no file to call.
|
|
347
|
+
if (located && HAS_DEFAULT.test(fs.readFileSync(located.file, "utf8"))) {
|
|
348
|
+
const before = vendored.size;
|
|
349
|
+
file = vendorModule(root, located, vendored, report);
|
|
350
|
+
if (vendored.size > before) {
|
|
351
|
+
report.done.push({
|
|
352
|
+
step: "vendor",
|
|
353
|
+
subject: key,
|
|
354
|
+
message: `copied to ${rel(file)} for its server function; rewrite it over the platform's upstream client (/next/upstream-clients)`,
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
let handler: Handler | null = null;
|
|
360
|
+
if (file && HAS_DEFAULT.test(fs.readFileSync(file, "utf8"))) {
|
|
361
|
+
handler = { key, file, name: handlerName(key), method: method(key) };
|
|
362
|
+
}
|
|
363
|
+
handlers.set(key, handler);
|
|
364
|
+
return handler;
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
let functionsFile = path.join(root, FUNCTIONS_FILE[framework]);
|
|
368
|
+
for (let n = 2; fs.existsSync(functionsFile); n++) {
|
|
369
|
+
functionsFile = path.join(
|
|
370
|
+
root,
|
|
371
|
+
FUNCTIONS_FILE[framework].replace(/\.(functions|actions)\.ts$/, `${n}.$1.ts`),
|
|
372
|
+
);
|
|
373
|
+
}
|
|
374
|
+
const missing = new Map<string, Set<string>>();
|
|
375
|
+
const proxies = new Set<string>();
|
|
376
|
+
let rewritten = 0;
|
|
377
|
+
|
|
378
|
+
for (const { file, text, source, roots, calls, dynamic } of found) {
|
|
379
|
+
const edits: Edit[] = [];
|
|
380
|
+
const imported = new Set<string>();
|
|
381
|
+
const usedStubs = new Set<Stub>();
|
|
382
|
+
const replaced = new Set<ts.Node>();
|
|
383
|
+
for (const call of calls) {
|
|
384
|
+
const handler = resolve(call.key!);
|
|
385
|
+
let replacement: string;
|
|
386
|
+
if (handler) {
|
|
387
|
+
imported.add(handler.name);
|
|
388
|
+
replacement =
|
|
389
|
+
framework === "tanstack"
|
|
390
|
+
? `${handler.name}(${call.props ? `{ data: ${call.props} }` : ""})`
|
|
391
|
+
: `${handler.name}(${call.props ?? ""})`;
|
|
392
|
+
} else {
|
|
393
|
+
const stub = v8Unmapped.has(call.key!) ? "unportedVtexInvoke" : "unportedInvoke";
|
|
394
|
+
usedStubs.add(stub);
|
|
395
|
+
stubs.add(stub);
|
|
396
|
+
replacement = `${stub}(${[JSON.stringify(call.key), call.props].filter(Boolean).join(", ")})`;
|
|
397
|
+
const set = missing.get(call.key!) ?? new Set();
|
|
398
|
+
set.add(`${rel(file)}:${call.line}`);
|
|
399
|
+
missing.set(call.key!, set);
|
|
400
|
+
}
|
|
401
|
+
edits.push({ start: call.node.getStart(source), end: call.node.getEnd(), text: replacement });
|
|
402
|
+
replaced.add(call.node);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// Drop the proxy's import when nothing else in the file uses it.
|
|
406
|
+
for (const [local, rootOf] of roots) {
|
|
407
|
+
let left = 0;
|
|
408
|
+
const count = (node: ts.Node) => {
|
|
409
|
+
if (replaced.has(node)) return;
|
|
410
|
+
if (ts.isIdentifier(node) && node.text === local && node !== rootOf.element.name) {
|
|
411
|
+
const parent = node.parent;
|
|
412
|
+
const isName =
|
|
413
|
+
(ts.isPropertyAccessExpression(parent) && parent.name === node) ||
|
|
414
|
+
(ts.isPropertyAssignment(parent) && parent.name === node);
|
|
415
|
+
if (!isName) left++;
|
|
416
|
+
}
|
|
417
|
+
ts.forEachChild(node, count);
|
|
418
|
+
};
|
|
419
|
+
count(source);
|
|
420
|
+
if (left > 0) continue;
|
|
421
|
+
if (rootOf.file) proxies.add(rootOf.file);
|
|
422
|
+
const bindings = rootOf.decl.importClause!.namedBindings as ts.NamedImports;
|
|
423
|
+
const others = bindings.elements.filter((el) => el !== rootOf.element);
|
|
424
|
+
if (others.length === 0 && !rootOf.decl.importClause!.name) {
|
|
425
|
+
let end = rootOf.decl.getEnd();
|
|
426
|
+
if (text[end] === "\n") end++;
|
|
427
|
+
edits.push({ start: rootOf.decl.getStart(source), end, text: "" });
|
|
428
|
+
} else {
|
|
429
|
+
edits.push({
|
|
430
|
+
start: bindings.getStart(source),
|
|
431
|
+
end: bindings.getEnd(),
|
|
432
|
+
text: others.length > 0 ? `{ ${others.map((el) => el.getText(source)).join(", ")} }` : "",
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
if (imported.size > 0) {
|
|
438
|
+
const at = importInsertion(source);
|
|
439
|
+
const line = `import { ${[...imported].sort().join(", ")} } from "${relativeSpecifier(file, functionsFile)}";`;
|
|
440
|
+
edits.push({ start: at, end: at, text: at === 0 ? `${line}\n` : `\n${line}` });
|
|
441
|
+
}
|
|
442
|
+
if (usedStubs.size > 0) edits.push(stubImport(file, root, [...usedStubs].sort(), source));
|
|
443
|
+
if (edits.length > 0) {
|
|
444
|
+
fs.writeFileSync(file, applyEdits(text, edits));
|
|
445
|
+
rewritten++;
|
|
446
|
+
}
|
|
447
|
+
if (dynamic.length > 0) {
|
|
448
|
+
report.manual.push({
|
|
449
|
+
step: "invoke",
|
|
450
|
+
subject: `${rel(file)}:${dynamic.join(", ")}`,
|
|
451
|
+
message:
|
|
452
|
+
"an invoke call whose key the script can't read (a computed property or key): give it a server function by hand that calls its handler with (props, request), and import it here (references/server-functions.md)",
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
if (v8Mapped.length > 0) {
|
|
458
|
+
const { clientFile } = writeV8Module(root, framework, v8Mapped);
|
|
459
|
+
report.done.push({
|
|
460
|
+
step: "invoke",
|
|
461
|
+
subject: rel(v8File),
|
|
462
|
+
message: `--vtex-invoke v8: v7's VTEX invoke-map keys on the v8 VTEX client, no v7 code: ${v8Mapped.map((k) => `${k} → vtex.${V8_VTEX_OPERATIONS[k].call.replace(/^vtex\./, "").replace(/\(.*$/s, "")}()`).join(", ")}`,
|
|
463
|
+
});
|
|
464
|
+
report.manual.push({
|
|
465
|
+
step: "invoke",
|
|
466
|
+
subject: `${rel(v8File)} (differences from v7)`,
|
|
467
|
+
message: `list them in the PR: ${v8Mapped.map((k) => `${k}: ${V8_VTEX_OPERATIONS[k].differs}`).join("; ")}`,
|
|
468
|
+
});
|
|
469
|
+
if (!clientFile) {
|
|
470
|
+
report.manual.push({
|
|
471
|
+
step: "invoke",
|
|
472
|
+
subject: "src/clients/vtex.ts",
|
|
473
|
+
message:
|
|
474
|
+
"the site saved no deco-vtex app block, so the script wrote no VTEX client: create src/clients/vtex.ts exporting vtexClient(), a createVtexClient from @decocms/apps-vtex with the store's account (/next/upstream-clients)",
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
if (kept.size > 0) {
|
|
479
|
+
report.done.push({
|
|
480
|
+
step: "invoke",
|
|
481
|
+
subject: "VTEX invoke keys (--vtex-invoke keep)",
|
|
482
|
+
message: `v7's VTEX app answered them from its own invoke map; each server function calls v7's handler, vendored: ${[...kept].map(([k, where]) => `${k} → ${where}`).join(", ")}. Run the migration with --vtex-invoke v8 instead to move them to @decocms/apps-vtex's client`,
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
const used = [...handlers.values()].filter((h): h is Handler => h !== null);
|
|
486
|
+
if (used.length > 0) {
|
|
487
|
+
writeFunctions(functionsFile, framework, used);
|
|
488
|
+
report.done.push({
|
|
489
|
+
step: "invoke",
|
|
490
|
+
subject: rel(functionsFile),
|
|
491
|
+
message: `${used.length} server functions, one per key the site invoked (${used.map((h) => h.key).join(", ")}), each calling its handler with (props, request) as /deco/invoke did${framework === "tanstack" ? "; loaders are GETs kept out of the shared cache (markPrivate), actions POSTs" : ""}; ${rewritten} files call them now`,
|
|
492
|
+
});
|
|
493
|
+
report.manual.push({
|
|
494
|
+
step: "invoke",
|
|
495
|
+
subject: rel(functionsFile),
|
|
496
|
+
message:
|
|
497
|
+
"these are public endpoints, as /deco/invoke was: validate each input (a zod schema in inputValidator, or in the action), and set the cookies a handler needs with the framework's own API (v7 forwarded RequestContext.responseHeaders, which v8 doesn't have) (references/server-functions.md)",
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
for (const [key, where] of missing) {
|
|
501
|
+
const list = [...where];
|
|
502
|
+
report.manual.push({
|
|
503
|
+
step: "invoke",
|
|
504
|
+
subject: `${key} (${list.slice(0, 3).join(", ")}${list.length > 3 ? `, +${list.length - 3} more` : ""})`,
|
|
505
|
+
message: v8Unmapped.has(key)
|
|
506
|
+
? `v7's VTEX app answered it from its own invoke map, and the migration ran with --vtex-invoke v8 (no v7 code), but ${v8Unmapped.get(key)}. It's unportedVtexInvoke from src/migration-stubs.ts now, which rejects with the key: write it over the site's VTEX client (src/clients/vtex.ts, /next/upstream-clients) in src/server/vtex-invoke.server.ts and give it a server function, or run the migration with --vtex-invoke keep to vendor v7's handler`
|
|
507
|
+
: key.startsWith("site/")
|
|
508
|
+
? "no handler file answers this key, so v7's invoke failed with \"handler not found\" too; it's unportedInvoke from src/migration-stubs.ts now, which rejects. Remove the call, or write the handler and its server function"
|
|
509
|
+
: "no module of the installed app is this key's handler (v7's app answered it from its own invoke map, or the app isn't installed), so there was nothing to vendor; it's unportedInvoke from src/migration-stubs.ts now, which rejects. Call the platform's client (/next/upstream-clients) from a server function instead",
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
// v7's proxy module, once nothing imports it.
|
|
514
|
+
for (const proxy of proxies) {
|
|
515
|
+
const importers = listSources(path.join(root, "src")).filter((f) => {
|
|
516
|
+
if (f === proxy) return false;
|
|
517
|
+
const t = fs.readFileSync(f, "utf8");
|
|
518
|
+
return [...t.matchAll(/from\s+["']([^"']+)["']|import\(\s*["']([^"']+)["']\s*\)/g)].some(
|
|
519
|
+
(m) => siteModule(root, f, m[1] ?? m[2]) === proxy,
|
|
520
|
+
);
|
|
521
|
+
});
|
|
522
|
+
if (importers.length === 0) {
|
|
523
|
+
fs.rmSync(proxy);
|
|
524
|
+
report.done.push({
|
|
525
|
+
step: "invoke",
|
|
526
|
+
subject: rel(proxy),
|
|
527
|
+
message:
|
|
528
|
+
"v7's invoke proxy (it fetched /deco/invoke, which v8 doesn't serve): deleted, nothing imports it now",
|
|
529
|
+
});
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
return stubs;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
function writeFunctions(file: string, framework: Framework, handlers: Handler[]): void {
|
|
536
|
+
const ident = new Map(handlers.map((h, i) => [h.key, `handler${i}`]));
|
|
537
|
+
const header = [
|
|
538
|
+
"/**",
|
|
539
|
+
" * v7's /deco/invoke calls, one server function per key the site invoked (/next/renames-and-migrations#loaders-actions-and-invoke).",
|
|
540
|
+
" * Generated by the deco-v7-to-v8-migration skill; it's your code now. Each calls its handler as v7's",
|
|
541
|
+
" * invoke endpoint did: with the props (v7 sent {} for none) and the request. These are public",
|
|
542
|
+
" * endpoints, as /deco/invoke was: validate each input before you ship.",
|
|
543
|
+
" */",
|
|
544
|
+
];
|
|
545
|
+
const imports = handlers.map((h) =>
|
|
546
|
+
h.exportName
|
|
547
|
+
? `import { ${h.exportName} as ${ident.get(h.key)} } from "${relativeSpecifier(file, h.file)}";`
|
|
548
|
+
: `import ${ident.get(h.key)} from "${relativeSpecifier(file, h.file)}";`,
|
|
549
|
+
);
|
|
550
|
+
const input = [
|
|
551
|
+
"",
|
|
552
|
+
"/** The props a v7 handler takes. */",
|
|
553
|
+
"// biome-ignore lint/suspicious/noExplicitAny: any handler",
|
|
554
|
+
"type Input<F> = F extends (props: infer P, ...rest: any[]) => unknown ? P : undefined;",
|
|
555
|
+
];
|
|
556
|
+
let lines: string[];
|
|
557
|
+
if (framework === "tanstack") {
|
|
558
|
+
const getOnly = handlers.some((h) => h.method === "GET");
|
|
559
|
+
lines = [
|
|
560
|
+
...header,
|
|
561
|
+
...(getOnly ? ['import { markPrivate } from "@decocms/tanstack/server";'] : []),
|
|
562
|
+
'import { createServerFn } from "@tanstack/react-start";',
|
|
563
|
+
'import { getRequest } from "@tanstack/react-start/server";',
|
|
564
|
+
...imports,
|
|
565
|
+
...input,
|
|
566
|
+
"",
|
|
567
|
+
"/**",
|
|
568
|
+
" * Calls a v7 handler as /deco/invoke did: `handler(props, request)`. Input and result are `any`, as",
|
|
569
|
+
" * v7's invoke was: type each function (a zod inputValidator, a serializable result) as you port it.",
|
|
570
|
+
" */",
|
|
571
|
+
"// biome-ignore lint/suspicious/noExplicitAny: any handler, any result, as v7's invoke",
|
|
572
|
+
"const call = <F extends (...args: any[]) => any>(handler: F, props: Input<F>): Promise<any> =>",
|
|
573
|
+
" Promise.resolve(handler(props, getRequest()));",
|
|
574
|
+
];
|
|
575
|
+
for (const h of handlers) {
|
|
576
|
+
const id = ident.get(h.key)!;
|
|
577
|
+
lines.push(
|
|
578
|
+
"",
|
|
579
|
+
`/** ${h.key}${h.method === "GET" ? " (a loader: per visitor, never shared)" : ""} */`,
|
|
580
|
+
`export const ${h.name} = createServerFn({ method: "${h.method}" })`,
|
|
581
|
+
" // biome-ignore lint/suspicious/noExplicitAny: v7's invoke took any props",
|
|
582
|
+
` .inputValidator((data?: any) => (data ?? {}) as Input<typeof ${id}>)`,
|
|
583
|
+
h.method === "GET"
|
|
584
|
+
? ` .handler(({ data }) => {\n markPrivate();\n return call(${id}, data);\n });`
|
|
585
|
+
: ` .handler(({ data }) => call(${id}, data));`,
|
|
586
|
+
);
|
|
587
|
+
}
|
|
588
|
+
} else {
|
|
589
|
+
lines = [
|
|
590
|
+
'"use server";',
|
|
591
|
+
...header,
|
|
592
|
+
'import { headers } from "next/headers";',
|
|
593
|
+
...imports,
|
|
594
|
+
...input,
|
|
595
|
+
"",
|
|
596
|
+
"/** The request a handler gets: this action's headers (cookies included) on the site's origin. */",
|
|
597
|
+
"async function invokeRequest(): Promise<Request> {",
|
|
598
|
+
" const incoming = await headers();",
|
|
599
|
+
' const host = incoming.get("x-forwarded-host") ?? incoming.get("host") ?? "localhost";',
|
|
600
|
+
' const protocol = incoming.get("x-forwarded-proto") ?? "https";',
|
|
601
|
+
// biome-ignore lint/suspicious/noTemplateCurlyInString: generated source with a template literal
|
|
602
|
+
" return new Request(`${protocol}://${host}/`, { headers: new Headers(incoming) });",
|
|
603
|
+
"}",
|
|
604
|
+
"",
|
|
605
|
+
"/** Calls a v7 handler as /deco/invoke did: `handler(props, request)`. */",
|
|
606
|
+
"// biome-ignore lint/suspicious/noExplicitAny: any handler",
|
|
607
|
+
"async function call<F extends (...args: any[]) => any>(handler: F, props: Input<F> | undefined): Promise<Awaited<ReturnType<F>>> {",
|
|
608
|
+
" return handler(props ?? {}, await invokeRequest());",
|
|
609
|
+
"}",
|
|
610
|
+
];
|
|
611
|
+
for (const h of handlers) {
|
|
612
|
+
const id = ident.get(h.key)!;
|
|
613
|
+
lines.push(
|
|
614
|
+
"",
|
|
615
|
+
`/** ${h.key} */`,
|
|
616
|
+
`export async function ${h.name}(props?: Input<typeof ${id}>) {`,
|
|
617
|
+
` return call(${id}, props);`,
|
|
618
|
+
"}",
|
|
619
|
+
);
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
623
|
+
fs.writeFileSync(file, `${lines.join("\n")}\n`);
|
|
624
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Legacy type names the next major's alias table doesn't carry (spec:
|
|
3
|
+
* studio-compatibility › Well-known types and the alias table). v7 resolved
|
|
4
|
+
* these; the next major resolves only the names the site editor's screens
|
|
5
|
+
* look for, so the migration rewrites the others to one of those (or to the
|
|
6
|
+
* built-in's own name) in the saved content.
|
|
7
|
+
*/
|
|
8
|
+
import fs from "node:fs";
|
|
9
|
+
import path from "node:path";
|
|
10
|
+
import { serializeBlock } from "@decocms/blocks/protocol/keys";
|
|
11
|
+
import type { Report } from "./report";
|
|
12
|
+
import { forEachBlock, readContent } from "./walk";
|
|
13
|
+
|
|
14
|
+
/** Old name → the name it's saved under after the migration. Same props, same behaviour. */
|
|
15
|
+
export const RENAMED_TYPES: Readonly<Record<string, string>> = {
|
|
16
|
+
"website/flags/multivariate/image.ts": "website/flags/multivariate.ts",
|
|
17
|
+
"website/flags/multivariate/message.ts": "website/flags/multivariate.ts",
|
|
18
|
+
"website/flags/multivariate/page.ts": "website/flags/multivariate.ts",
|
|
19
|
+
"$live/flags/multivariate.ts": "website/flags/multivariate.ts",
|
|
20
|
+
"$live/matchers/MatchAlways.ts": "website/matchers/always.ts",
|
|
21
|
+
"website/matchers/date.ts": "date",
|
|
22
|
+
"$live/matchers/MatchDate.ts": "date",
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export function renameLegacyTypes(root: string, report: Report): void {
|
|
26
|
+
const { blocks, files } = readContent(root);
|
|
27
|
+
for (const [entry, block] of Object.entries(blocks)) {
|
|
28
|
+
const renamed = new Set<string>();
|
|
29
|
+
forEachBlock(block, (node) => {
|
|
30
|
+
const to = Object.hasOwn(RENAMED_TYPES, node.__resolveType)
|
|
31
|
+
? RENAMED_TYPES[node.__resolveType]
|
|
32
|
+
: undefined;
|
|
33
|
+
if (to === undefined) return;
|
|
34
|
+
renamed.add(`${node.__resolveType} → ${to}`);
|
|
35
|
+
node.__resolveType = to;
|
|
36
|
+
});
|
|
37
|
+
if (renamed.size === 0) continue;
|
|
38
|
+
fs.writeFileSync(path.join(root, ".deco", "blocks", files[entry]!), serializeBlock(block));
|
|
39
|
+
report.done.push({
|
|
40
|
+
step: "content",
|
|
41
|
+
subject: files[entry]!,
|
|
42
|
+
message: `renamed ${[...renamed].join(", ")}`,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|