@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.
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/protocol/conformance/blockMetaVectors.json +80 -0
- package/dist/protocol/index.d.ts +5 -2
- package/dist/protocol/index.d.ts.map +1 -1
- package/dist/protocol/index.js +5 -2
- package/dist/protocol/index.js.map +1 -1
- package/dist/v8/blockMeta.d.ts +33 -4
- package/dist/v8/blockMeta.d.ts.map +1 -1
- package/dist/v8/blockMeta.js +70 -13
- package/dist/v8/blockMeta.js.map +1 -1
- 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/dist/v8/client.d.ts +6 -4
- package/dist/v8/client.d.ts.map +1 -1
- package/dist/v8/client.js +26 -70
- package/dist/v8/client.js.map +1 -1
- package/dist/v8/cms.d.ts.map +1 -1
- package/dist/v8/cms.js +1 -2
- package/dist/v8/cms.js.map +1 -1
- package/dist/v8/contentIndex.d.ts +18 -7
- package/dist/v8/contentIndex.d.ts.map +1 -1
- package/dist/v8/contentIndex.js +46 -17
- package/dist/v8/contentIndex.js.map +1 -1
- package/dist/v8/draftChanges.d.ts.map +1 -1
- package/dist/v8/draftChanges.js +4 -1
- package/dist/v8/draftChanges.js.map +1 -1
- package/dist/v8/index.d.ts +1 -1
- package/dist/v8/index.d.ts.map +1 -1
- package/dist/v8/remoteLoader.d.ts +4 -2
- package/dist/v8/remoteLoader.d.ts.map +1 -1
- package/dist/v8/remoteLoader.js +13 -3
- package/dist/v8/remoteLoader.js.map +1 -1
- package/dist/v8/types.d.ts +47 -7
- package/dist/v8/types.d.ts.map +1 -1
- 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 +203 -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 +172 -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 +158 -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 +101 -0
- package/skills/deco-site-workflow/references/check.md +116 -0
- package/skills/deco-site-workflow/references/cli-reference.md +106 -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 +123 -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 +23 -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/checkContentReads.ts +32 -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/contentReads.ts +157 -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 +180 -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/index.ts +1 -1
- package/src/protocol/conformance/blockMetaVectors.json +80 -0
- package/src/protocol/index.ts +8 -1
- package/src/v8/blockMeta.ts +75 -11
- package/src/v8/cli/index.ts +1 -0
- package/src/v8/cli/run.ts +35 -1
- package/src/v8/cli/skills.ts +91 -0
- package/src/v8/client.ts +32 -77
- package/src/v8/cms.ts +1 -2
- package/src/v8/contentIndex.ts +49 -19
- package/src/v8/draftChanges.ts +5 -1
- package/src/v8/index.ts +1 -1
- package/src/v8/remoteLoader.ts +13 -3
- package/src/v8/types.ts +48 -7
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `deco schema --check` in the site's CI: the step fails a pull request whose
|
|
3
|
+
* `.deco/schema.gen.json` differs from what `deco schema` writes (Studio reads
|
|
4
|
+
* that file for the editor forms, so a stale one shows stale forms). It runs
|
|
5
|
+
* right after the install step, before a build's `prebuild` regenerates the
|
|
6
|
+
* file. Workflows are line-edited (no YAML rewrite), so comments and layout
|
|
7
|
+
* stay as they were.
|
|
8
|
+
*/
|
|
9
|
+
import fs from "node:fs";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
import type { Report } from "./report";
|
|
12
|
+
|
|
13
|
+
const CHECK = "deco schema --check";
|
|
14
|
+
const INSTALL =
|
|
15
|
+
/^(?:(?:bun|pnpm|yarn)\s+install|npm\s+(?:ci|install|i)|yarn(?:\s+--\S+)*\s*$)(?:\s|$)/;
|
|
16
|
+
const STEP_NAME = "Check that .deco/schema.gen.json is up to date";
|
|
17
|
+
|
|
18
|
+
/** The folder with the site's git repository: the root itself, or the nearest parent with `.git`. */
|
|
19
|
+
function repoRoot(root: string): string {
|
|
20
|
+
for (let dir = root; ; dir = path.dirname(dir)) {
|
|
21
|
+
if (fs.existsSync(path.join(dir, ".git"))) return dir;
|
|
22
|
+
if (path.dirname(dir) === dir) return root;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** How the site runs a dependency's bin, from its lockfile. */
|
|
27
|
+
function runner(root: string, repo: string): string {
|
|
28
|
+
const has = (name: string) =>
|
|
29
|
+
fs.existsSync(path.join(root, name)) || fs.existsSync(path.join(repo, name));
|
|
30
|
+
if (has("bun.lock") || has("bun.lockb")) return "bunx";
|
|
31
|
+
if (has("pnpm-lock.yaml")) return "pnpm exec";
|
|
32
|
+
if (has("yarn.lock")) return "yarn";
|
|
33
|
+
return "npx";
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const indentOf = (line: string) => line.length - line.trimStart().length;
|
|
37
|
+
|
|
38
|
+
/** The `run:` command of the step on or under `line`, when it's a one-line command. */
|
|
39
|
+
function runCommand(line: string): string | undefined {
|
|
40
|
+
const m = /^\s*(?:-\s+)?run:\s*(.+?)\s*$/.exec(line);
|
|
41
|
+
return m && !/^[|>]/.test(m[1]) ? m[1].replace(/^["']|["']$/g, "") : undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface Insert {
|
|
45
|
+
/** Line index the new step goes before. */
|
|
46
|
+
at: number;
|
|
47
|
+
text: string[];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Where the check goes in one workflow: after the first install step, with
|
|
52
|
+
* that step's indentation and working directory. Undefined when the workflow
|
|
53
|
+
* has no one-line install step.
|
|
54
|
+
*/
|
|
55
|
+
function planInsert(
|
|
56
|
+
lines: string[],
|
|
57
|
+
command: string,
|
|
58
|
+
workingDirectory: string,
|
|
59
|
+
): Insert | undefined {
|
|
60
|
+
for (let i = 0; i < lines.length; i++) {
|
|
61
|
+
const run = runCommand(lines[i]);
|
|
62
|
+
if (!run || !INSTALL.test(run) || /\s(?:-g|--global)\b/.test(run)) continue;
|
|
63
|
+
// The step's own `- ` line: this one, or the closest above with less indentation.
|
|
64
|
+
let start = i;
|
|
65
|
+
while (start >= 0 && !/^\s*-\s/.test(lines[start])) start--;
|
|
66
|
+
if (start < 0) continue;
|
|
67
|
+
const dash = indentOf(lines[start]);
|
|
68
|
+
let end = start + 1;
|
|
69
|
+
while (end < lines.length) {
|
|
70
|
+
const line = lines[end];
|
|
71
|
+
if (line.trim() !== "" && !line.trimStart().startsWith("#") && indentOf(line) <= dash) break;
|
|
72
|
+
end++;
|
|
73
|
+
}
|
|
74
|
+
while (end > start + 1 && lines[end - 1].trim() === "") end--;
|
|
75
|
+
const body = " ".repeat(dash + 2);
|
|
76
|
+
const dir = lines
|
|
77
|
+
.slice(start, end)
|
|
78
|
+
.map((l) => /^\s*(?:-\s+)?working-directory:\s*(.+?)\s*$/.exec(l)?.[1])
|
|
79
|
+
.find(Boolean);
|
|
80
|
+
const text = [`${" ".repeat(dash)}- name: ${STEP_NAME}`];
|
|
81
|
+
if (dir) text.push(`${body}working-directory: ${dir}`);
|
|
82
|
+
// A job's `defaults.run.working-directory` already applies to the new step.
|
|
83
|
+
else if (workingDirectory && !lines.some((l) => /^\s*working-directory:/.test(l)))
|
|
84
|
+
text.push(`${body}working-directory: ${workingDirectory}`);
|
|
85
|
+
text.push(`${body}run: ${command}`);
|
|
86
|
+
return { at: end, text };
|
|
87
|
+
}
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Add `deco schema --check` to the site's CI workflows (the pull-request ones when there are any). */
|
|
92
|
+
export function addSchemaCheck(root: string, report: Report): void {
|
|
93
|
+
const repo = repoRoot(root);
|
|
94
|
+
const dir = path.join(repo, ".github", "workflows");
|
|
95
|
+
const files = fs.existsSync(dir)
|
|
96
|
+
? fs
|
|
97
|
+
.readdirSync(dir)
|
|
98
|
+
.filter((f) => /\.ya?ml$/.test(f))
|
|
99
|
+
.sort()
|
|
100
|
+
.map((f) => path.join(dir, f))
|
|
101
|
+
: [];
|
|
102
|
+
const workflows = files
|
|
103
|
+
.map((file) => ({ file, text: fs.readFileSync(file, "utf8") }))
|
|
104
|
+
// v7's content regeneration jobs are deleted by hand (references/manual-steps.md).
|
|
105
|
+
.filter(({ text }) => !/generate:blocks|regen-blocks/.test(text));
|
|
106
|
+
const rel = (file: string) => path.relative(repo, file).split(path.sep).join("/");
|
|
107
|
+
const command = `${runner(root, repo)} ${CHECK}`;
|
|
108
|
+
const manual = (message: string) =>
|
|
109
|
+
report.manual.push({ step: "scripts", subject: "CI", message });
|
|
110
|
+
|
|
111
|
+
const already = workflows.filter(({ text }) => text.includes(CHECK));
|
|
112
|
+
if (already.length > 0) {
|
|
113
|
+
report.done.push({
|
|
114
|
+
step: "scripts",
|
|
115
|
+
subject: "CI",
|
|
116
|
+
message: `${already.map((w) => rel(w.file)).join(", ")} already run${already.length === 1 ? "s" : ""} ${CHECK}`,
|
|
117
|
+
});
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const onPullRequests = workflows.filter(({ text }) => /\bpull_request(?:_target)?\b/.test(text));
|
|
121
|
+
const targets = onPullRequests.length > 0 ? onPullRequests : workflows;
|
|
122
|
+
const siteDir = path.relative(repo, root).split(path.sep).join("/");
|
|
123
|
+
const added: string[] = [];
|
|
124
|
+
for (const { file, text } of targets) {
|
|
125
|
+
const lines = text.split("\n");
|
|
126
|
+
const insert = planInsert(lines, command, siteDir);
|
|
127
|
+
if (!insert) continue;
|
|
128
|
+
lines.splice(insert.at, 0, ...insert.text);
|
|
129
|
+
fs.writeFileSync(file, lines.join("\n"));
|
|
130
|
+
added.push(rel(file));
|
|
131
|
+
}
|
|
132
|
+
if (added.length > 0) {
|
|
133
|
+
report.done.push({
|
|
134
|
+
step: "scripts",
|
|
135
|
+
subject: "CI",
|
|
136
|
+
message: `${added.join(", ")}: \`${command}\` after the install step, so a pull request whose .deco/schema.gen.json differs from what deco schema writes fails (commit the regenerated file to fix it)`,
|
|
137
|
+
});
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
manual(
|
|
141
|
+
targets.length === 0
|
|
142
|
+
? `no CI workflow found in ${rel(dir)}: run \`${command}\` in the site's CI after installing dependencies, so a stale .deco/schema.gen.json fails the pull request`
|
|
143
|
+
: `no one-line install step in ${targets.map((w) => rel(w.file)).join(", ")}: add \`${command}\` after the dependencies are installed, so a stale .deco/schema.gen.json fails the pull request`,
|
|
144
|
+
);
|
|
145
|
+
}
|
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@decocms/compat-v7`: the one home of v7's pure helpers on v8 (the product
|
|
3
|
+
* owner's call). The codemod points v7 imports of those helpers at it, with
|
|
4
|
+
* v7's output and no v7 runtime:
|
|
5
|
+
*
|
|
6
|
+
* - `@decocms/apps-commerce/<subpath>` → `@decocms/compat-v7/commerce/<subpath>`
|
|
7
|
+
* (v7's layout under one prefix; the app runtime files have no port);
|
|
8
|
+
* - v7 helper names (`clx`, `detectDevice`, `useScript`, the widget and
|
|
9
|
+
* section types, the section-loader mixins) → the compat subpath that
|
|
10
|
+
* exports them (`COMPAT_MOVES`), and `useDevice`/`DeviceProvider` → the
|
|
11
|
+
* site's binding subpath (`/tanstack` or `/nextjs`);
|
|
12
|
+
* - a v7 section loader's `ctx.device` → `detectDevice(req.headers.get("user-agent") ?? "")`
|
|
13
|
+
* (`rewriteDeviceReads`): v8 has no `ctx`, and reading it throws.
|
|
14
|
+
*/
|
|
15
|
+
import fs from "node:fs";
|
|
16
|
+
import path from "node:path";
|
|
17
|
+
import ts from "typescript";
|
|
18
|
+
import type { Framework } from "./framework";
|
|
19
|
+
import type { Report } from "./report";
|
|
20
|
+
|
|
21
|
+
/** Every entry point of `@decocms/compat-v7` (its package.json `exports`; a test keeps them equal). */
|
|
22
|
+
export const COMPAT_V7_ENTRIES = [
|
|
23
|
+
"@decocms/compat-v7",
|
|
24
|
+
"@decocms/compat-v7/types",
|
|
25
|
+
"@decocms/compat-v7/types/widgets",
|
|
26
|
+
"@decocms/compat-v7/device",
|
|
27
|
+
"@decocms/compat-v7/script",
|
|
28
|
+
"@decocms/compat-v7/clx",
|
|
29
|
+
"@decocms/compat-v7/matchers",
|
|
30
|
+
"@decocms/compat-v7/sections",
|
|
31
|
+
"@decocms/compat-v7/commerce",
|
|
32
|
+
"@decocms/compat-v7/commerce/types",
|
|
33
|
+
"@decocms/compat-v7/commerce/types/cart",
|
|
34
|
+
"@decocms/compat-v7/commerce/sdk/analytics",
|
|
35
|
+
"@decocms/compat-v7/commerce/sdk/formatPrice",
|
|
36
|
+
"@decocms/compat-v7/commerce/sdk/url",
|
|
37
|
+
"@decocms/compat-v7/commerce/sdk/useOffer",
|
|
38
|
+
"@decocms/compat-v7/commerce/sdk/useVariantPossibilities",
|
|
39
|
+
"@decocms/compat-v7/commerce/utils/canonical",
|
|
40
|
+
"@decocms/compat-v7/commerce/utils/constants",
|
|
41
|
+
"@decocms/compat-v7/commerce/utils/filters",
|
|
42
|
+
"@decocms/compat-v7/commerce/utils/productToAnalyticsItem",
|
|
43
|
+
"@decocms/compat-v7/commerce/utils/stateByZip",
|
|
44
|
+
"@decocms/compat-v7/tanstack",
|
|
45
|
+
"@decocms/compat-v7/tanstack/server",
|
|
46
|
+
"@decocms/compat-v7/tanstack/worker",
|
|
47
|
+
"@decocms/compat-v7/nextjs",
|
|
48
|
+
"@decocms/compat-v7/nextjs/server",
|
|
49
|
+
"@decocms/compat-v7/nextjs/proxy",
|
|
50
|
+
];
|
|
51
|
+
|
|
52
|
+
/** The compat entry a v7 `@decocms/apps-commerce/<subpath>` import moves to, if it was ported. */
|
|
53
|
+
export function compatCommerceSpecifier(specifier: string): string | undefined {
|
|
54
|
+
// 7.x's package, or the 6.x path a 7.x site sometimes still imports (the same files).
|
|
55
|
+
const sub = /^@decocms\/(?:apps-commerce|apps\/commerce)\/(.+)$/.exec(specifier)?.[1];
|
|
56
|
+
if (!sub) return undefined;
|
|
57
|
+
const target = `@decocms/compat-v7/commerce/${sub.replace(/\.tsx?$/, "")}`;
|
|
58
|
+
return COMPAT_V7_ENTRIES.includes(target) ? target : undefined;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const DEVICE = ["Device", "detectDevice", "isMobileUA", "MOBILE_RE", "TABLET_RE"];
|
|
62
|
+
/** v7's device context: the binding's compat subpath, which knows where the server's device comes from. */
|
|
63
|
+
const DEVICE_CONTEXT = ["DeviceContext", "DeviceProvider", "useDevice"];
|
|
64
|
+
const SCRIPT = ["useScript", "useScriptAsDataURI", "inlineScript"];
|
|
65
|
+
const TYPES = ["SectionProps", "Resolved", "LoadingFallbackProps", "Flag"];
|
|
66
|
+
const WIDGETS = [
|
|
67
|
+
"ButtonWidget",
|
|
68
|
+
"Color",
|
|
69
|
+
"HTMLWidget",
|
|
70
|
+
"ImageWidget",
|
|
71
|
+
"RichText",
|
|
72
|
+
"Secret",
|
|
73
|
+
"TextArea",
|
|
74
|
+
"TextWidget",
|
|
75
|
+
"VideoWidget",
|
|
76
|
+
];
|
|
77
|
+
const MIXINS = ["compose", "withDevice", "withMobile", "withSearchParam", "withSectionLoader"];
|
|
78
|
+
|
|
79
|
+
const to = (names: string[], target: string) => names.map((n) => [n, target] as const);
|
|
80
|
+
const byBinding = (names: string[]) =>
|
|
81
|
+
names.map(
|
|
82
|
+
(n) =>
|
|
83
|
+
[
|
|
84
|
+
n,
|
|
85
|
+
{ tanstack: "@decocms/compat-v7/tanstack", nextjs: "@decocms/compat-v7/nextjs" },
|
|
86
|
+
] as const,
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
/** v7 helper names `@decocms/compat-v7` exports with v7's output, by v7 specifier (merged into `MOVES`). */
|
|
90
|
+
export const COMPAT_MOVES = {
|
|
91
|
+
"@decocms/blocks": Object.fromEntries(to(TYPES, "@decocms/compat-v7/types")),
|
|
92
|
+
"@decocms/blocks/types": Object.fromEntries(to(TYPES, "@decocms/compat-v7/types")),
|
|
93
|
+
"@decocms/blocks/types/widgets": Object.fromEntries(
|
|
94
|
+
to(WIDGETS, "@decocms/compat-v7/types/widgets"),
|
|
95
|
+
),
|
|
96
|
+
"@decocms/blocks/sdk": Object.fromEntries([
|
|
97
|
+
...to(["clx"], "@decocms/compat-v7/clx"),
|
|
98
|
+
...to(DEVICE, "@decocms/compat-v7/device"),
|
|
99
|
+
...to(SCRIPT, "@decocms/compat-v7/script"),
|
|
100
|
+
...byBinding(DEVICE_CONTEXT),
|
|
101
|
+
]),
|
|
102
|
+
"@decocms/blocks/sdk/clx": Object.fromEntries(to(["clx"], "@decocms/compat-v7/clx")),
|
|
103
|
+
"@decocms/blocks/sdk/detectDevice": Object.fromEntries(to(DEVICE, "@decocms/compat-v7/device")),
|
|
104
|
+
"@decocms/blocks/sdk/useDevice": Object.fromEntries([
|
|
105
|
+
...to(DEVICE, "@decocms/compat-v7/device"),
|
|
106
|
+
...byBinding(DEVICE_CONTEXT),
|
|
107
|
+
]),
|
|
108
|
+
"@decocms/blocks/sdk/useScript": Object.fromEntries(to(SCRIPT, "@decocms/compat-v7/script")),
|
|
109
|
+
"@decocms/blocks/cms": Object.fromEntries(to(MIXINS, "@decocms/compat-v7/sections")),
|
|
110
|
+
"@decocms/blocks/cms/client": Object.fromEntries(to(MIXINS, "@decocms/compat-v7/sections")),
|
|
111
|
+
} satisfies Record<string, Record<string, string | Partial<Record<Framework, string>>>>;
|
|
112
|
+
|
|
113
|
+
/** Hints for the v7 names next to those helpers that have no port, by v7 specifier. */
|
|
114
|
+
export const COMPAT_NAME_HINTS: { specifier: RegExp; names: string[]; hint: string }[] = [
|
|
115
|
+
{
|
|
116
|
+
specifier: /^@decocms\/blocks\/sdk(\/useDevice)?$/,
|
|
117
|
+
names: [
|
|
118
|
+
...DEVICE_CONTEXT,
|
|
119
|
+
"resolveDeviceFromRuntime",
|
|
120
|
+
"checkMobile",
|
|
121
|
+
"checkTablet",
|
|
122
|
+
"checkDesktop",
|
|
123
|
+
],
|
|
124
|
+
hint: "v7 read the device from its request scope, which v8 has none of: requestDevice() from @decocms/compat-v7/tanstack/server (or /nextjs/server) on the server, useDevice() under DeviceProvider from @decocms/compat-v7/tanstack (or /nextjs) in components, detectDevice(userAgent) from @decocms/compat-v7/device anywhere",
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
specifier: /^@decocms\/blocks\/sdk(\/useScript)?$/,
|
|
128
|
+
names: ["useSection", "usePartialSection", "HTMX_LEGACY_URL"],
|
|
129
|
+
hint: "v7's partial sections (HTMX) have no v8 equivalent; v7 7.x's own useSection only returned a placeholder URL (the script points named imports at src/migration-stubs.ts, which keeps it): render the section on the server and fetch it with a server function (references/server-functions.md)",
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
specifier: /^@decocms\/blocks(\/types)?$/,
|
|
133
|
+
names: ["FnContext", "App", "AppContext", "Section"],
|
|
134
|
+
hint: "v7's ctx and apps are gone: a section loader is (props, req) (@decocms/compat-v7/sections), an app is its client created in code (/next/upstream-clients), and a section is a block returning { component, props } (.deco/index.ts)",
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
specifier: /^@decocms\/blocks\/cms(\/client)?$/,
|
|
138
|
+
names: [
|
|
139
|
+
"registerSectionLoaders",
|
|
140
|
+
"registerSectionLoader",
|
|
141
|
+
"runSectionLoaders",
|
|
142
|
+
"runSingleSectionLoader",
|
|
143
|
+
"registerNonCriticalSections",
|
|
144
|
+
"registerLayoutSections",
|
|
145
|
+
"unregisterLayoutSections",
|
|
146
|
+
],
|
|
147
|
+
hint: "v7's section-loader registry: the script turns registerSectionLoaders({ … }) into an exported map the block map's loadSections runs (runSectionLoaders from @decocms/compat-v7/sections); a call it couldn't convert, and runSingleSectionLoader, become runSectionLoader(section, { loaders, request }); a layout section goes in nonCritical (v7 never degraded one), and v8 has no layout cache",
|
|
148
|
+
},
|
|
149
|
+
];
|
|
150
|
+
|
|
151
|
+
const SOURCE = /\.(tsx?|mts|cts)$/;
|
|
152
|
+
|
|
153
|
+
export function listSources(dir: string): string[] {
|
|
154
|
+
if (!fs.existsSync(dir)) return [];
|
|
155
|
+
return fs
|
|
156
|
+
.readdirSync(dir, { recursive: true, withFileTypes: true })
|
|
157
|
+
.filter((d) => d.isFile() && SOURCE.test(d.name) && !d.name.endsWith(".d.ts"))
|
|
158
|
+
.map((d) => path.join(d.parentPath, d.name))
|
|
159
|
+
.filter((f) => !f.split(path.sep).includes("node_modules") && !/\.test\.tsx?$/.test(f))
|
|
160
|
+
.sort();
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function parse(file: string, text: string): ts.SourceFile {
|
|
164
|
+
return ts.createSourceFile(
|
|
165
|
+
file,
|
|
166
|
+
text,
|
|
167
|
+
ts.ScriptTarget.Latest,
|
|
168
|
+
true,
|
|
169
|
+
file.endsWith("x") ? ts.ScriptKind.TSX : ts.ScriptKind.TS,
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export interface Edit {
|
|
174
|
+
start: number;
|
|
175
|
+
end: number;
|
|
176
|
+
text: string;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function applyEdits(text: string, edits: Edit[]): string {
|
|
180
|
+
let out = text;
|
|
181
|
+
for (const edit of [...edits].sort((a, b) => b.start - a.start)) {
|
|
182
|
+
out = out.slice(0, edit.start) + edit.text + out.slice(edit.end);
|
|
183
|
+
}
|
|
184
|
+
return out;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Where a new import line goes: after the last import declaration, else at the top (after a directive). */
|
|
188
|
+
export function importInsertion(source: ts.SourceFile): number {
|
|
189
|
+
let at = 0;
|
|
190
|
+
for (const statement of source.statements) {
|
|
191
|
+
if (ts.isImportDeclaration(statement)) at = statement.getEnd();
|
|
192
|
+
else if (
|
|
193
|
+
at === 0 &&
|
|
194
|
+
ts.isExpressionStatement(statement) &&
|
|
195
|
+
ts.isStringLiteral(statement.expression)
|
|
196
|
+
) {
|
|
197
|
+
at = statement.getEnd(); // "use client" and the like stay first
|
|
198
|
+
} else break;
|
|
199
|
+
}
|
|
200
|
+
return at;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** Whether `name` is bound by an import declaration of the file. */
|
|
204
|
+
export function importsName(source: ts.SourceFile, name: string): boolean {
|
|
205
|
+
return source.statements.some((s) => {
|
|
206
|
+
if (!ts.isImportDeclaration(s)) return false;
|
|
207
|
+
const clause = s.importClause;
|
|
208
|
+
if (clause?.name?.text === name) return true;
|
|
209
|
+
const b = clause?.namedBindings;
|
|
210
|
+
if (b && ts.isNamespaceImport(b)) return b.name.text === name;
|
|
211
|
+
return Boolean(b && ts.isNamedImports(b) && b.elements.some((el) => el.name.text === name));
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
type FunctionLike =
|
|
216
|
+
| ts.FunctionDeclaration
|
|
217
|
+
| ts.FunctionExpression
|
|
218
|
+
| ts.ArrowFunction
|
|
219
|
+
| ts.MethodDeclaration;
|
|
220
|
+
|
|
221
|
+
function isFunctionLike(node: ts.Node): node is FunctionLike {
|
|
222
|
+
return (
|
|
223
|
+
ts.isFunctionDeclaration(node) ||
|
|
224
|
+
ts.isFunctionExpression(node) ||
|
|
225
|
+
ts.isArrowFunction(node) ||
|
|
226
|
+
ts.isMethodDeclaration(node)
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* When `node` is a v7 loader, `(props, req, ctx)`, its context and request
|
|
232
|
+
* parameter names. The context is named so (`ctx`, `context`) or typed as one
|
|
233
|
+
* (`AppContext`, `FnContext`, …).
|
|
234
|
+
*/
|
|
235
|
+
export function loaderContextOf(
|
|
236
|
+
node: ts.Node,
|
|
237
|
+
source: ts.SourceFile,
|
|
238
|
+
): { ctx: string; req?: string; isRequest: boolean } | undefined {
|
|
239
|
+
if (!isFunctionLike(node) || node.parameters.length < 3) return undefined;
|
|
240
|
+
const [, req, ctx] = node.parameters;
|
|
241
|
+
if (!ts.isIdentifier(ctx.name)) return undefined;
|
|
242
|
+
const isContext =
|
|
243
|
+
/^_?(ctx|context)$/i.test(ctx.name.text) ||
|
|
244
|
+
(ctx.type !== undefined && /Context\b/.test(ctx.type.getText(source)));
|
|
245
|
+
if (!isContext) return undefined;
|
|
246
|
+
return {
|
|
247
|
+
ctx: ctx.name.text,
|
|
248
|
+
req: ts.isIdentifier(req.name) ? req.name.text : undefined,
|
|
249
|
+
// A v7 loader's second parameter is the request: named so, or typed as one.
|
|
250
|
+
isRequest:
|
|
251
|
+
(ts.isIdentifier(req.name) && /^_?(req|request)$/i.test(req.name.text)) ||
|
|
252
|
+
(req.type !== undefined && /^Request$/.test(req.type.getText(source).trim())),
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** A read of v7's loader context the codemod leaves: its line and the property read. */
|
|
257
|
+
export interface ContextRead {
|
|
258
|
+
line: number;
|
|
259
|
+
read: string;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** `ctx`, `(ctx as X)`, `ctx!`: the identifier under the wrappers, if any. */
|
|
263
|
+
export function unwrapIdentifier(expr: ts.Expression): ts.Identifier | undefined {
|
|
264
|
+
let e = expr;
|
|
265
|
+
while (ts.isParenthesizedExpression(e) || ts.isAsExpression(e) || ts.isNonNullExpression(e))
|
|
266
|
+
e = e.expression;
|
|
267
|
+
return ts.isIdentifier(e) ? e : undefined;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Rewrites one file's `ctx.device` reads in v7 section loaders (`(props, req, ctx)`):
|
|
272
|
+
* returns the new text, or undefined when nothing changed. `left` gets the
|
|
273
|
+
* context reads it leaves: `ctx.device` with no request parameter to read,
|
|
274
|
+
* and every other property (`ctx.invoke`, `ctx.response`, an app's state),
|
|
275
|
+
* which v8 has no equivalent for.
|
|
276
|
+
*/
|
|
277
|
+
export function rewriteDeviceReadsIn(
|
|
278
|
+
file: string,
|
|
279
|
+
text: string,
|
|
280
|
+
left: ContextRead[] = [],
|
|
281
|
+
): string | undefined {
|
|
282
|
+
if (!/\b(ctx|context)\b|Context\b/i.test(text)) return undefined;
|
|
283
|
+
const source = parse(file, text);
|
|
284
|
+
const edits: Edit[] = [];
|
|
285
|
+
const local =
|
|
286
|
+
/\bdetectDevice\b/.test(text) && !importsName(source, "detectDevice")
|
|
287
|
+
? "detectV7Device"
|
|
288
|
+
: "detectDevice";
|
|
289
|
+
const visit = (node: ts.Node, scope: { ctx: string; req?: string }[]) => {
|
|
290
|
+
let inner = scope;
|
|
291
|
+
const context = loaderContextOf(node, source);
|
|
292
|
+
if (context) inner = [...scope, context];
|
|
293
|
+
if (ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node)) {
|
|
294
|
+
const id = unwrapIdentifier(node.expression);
|
|
295
|
+
const owner = id && [...inner].reverse().find((s) => s.ctx === id.text);
|
|
296
|
+
if (owner) {
|
|
297
|
+
const read = ts.isPropertyAccessExpression(node)
|
|
298
|
+
? node.name.text
|
|
299
|
+
: node.argumentExpression.getText(source);
|
|
300
|
+
const line = source.getLineAndCharacterOfPosition(node.getStart(source)).line + 1;
|
|
301
|
+
if (read === "device" && owner.req) {
|
|
302
|
+
edits.push({
|
|
303
|
+
start: node.getStart(source),
|
|
304
|
+
end: node.getEnd(),
|
|
305
|
+
text: `${local}(${owner.req}.headers.get("user-agent") ?? "")`,
|
|
306
|
+
});
|
|
307
|
+
} else {
|
|
308
|
+
left.push({ line, read });
|
|
309
|
+
}
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
ts.forEachChild(node, (child) => visit(child, inner));
|
|
314
|
+
};
|
|
315
|
+
visit(source, []);
|
|
316
|
+
if (edits.length === 0) return undefined;
|
|
317
|
+
if (!importsName(source, local)) {
|
|
318
|
+
const at = importInsertion(source);
|
|
319
|
+
const line =
|
|
320
|
+
local === "detectDevice"
|
|
321
|
+
? 'import { detectDevice } from "@decocms/compat-v7/device";'
|
|
322
|
+
: 'import { detectDevice as detectV7Device } from "@decocms/compat-v7/device";';
|
|
323
|
+
edits.push({ start: at, end: at, text: at === 0 ? `${line}\n` : `\n${line}` });
|
|
324
|
+
}
|
|
325
|
+
return applyEdits(text, edits);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/** `ctx.device` in v7 section loaders under `src/`: v7's device, read from the request the loader gets. */
|
|
329
|
+
export function rewriteDeviceReads(root: string, report: Report): void {
|
|
330
|
+
const changed: string[] = [];
|
|
331
|
+
for (const file of listSources(path.join(root, "src"))) {
|
|
332
|
+
const text = fs.readFileSync(file, "utf8");
|
|
333
|
+
const left: ContextRead[] = [];
|
|
334
|
+
const out = rewriteDeviceReadsIn(file, text, left);
|
|
335
|
+
const rel = path.relative(root, file).split(path.sep).join("/");
|
|
336
|
+
if (out !== undefined) {
|
|
337
|
+
fs.writeFileSync(file, out);
|
|
338
|
+
changed.push(rel);
|
|
339
|
+
}
|
|
340
|
+
if (left.length > 0) {
|
|
341
|
+
const reads = [...new Set(left.map((r) => `ctx.${r.read}`))].join(", ");
|
|
342
|
+
const lines = [...new Set(left.map((r) => r.line))].join(", ");
|
|
343
|
+
report.manual.push({
|
|
344
|
+
step: "imports",
|
|
345
|
+
subject: `${rel}:${lines}`,
|
|
346
|
+
message: `reads ${reads} in a v7 loader, and v8 has no ctx: a loader run by loadSections that reads it throws, and its section keeps its saved props. Read the device with detectDevice(req.headers.get("user-agent") ?? "") from @decocms/compat-v7/device (with the request as the second parameter), call the app's client or the site's server function instead of ctx.invoke, and pass settings in instead of reading app state`,
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
if (changed.length > 0) {
|
|
351
|
+
report.done.push({
|
|
352
|
+
step: "imports",
|
|
353
|
+
subject: "ctx.device",
|
|
354
|
+
message: `read from the loader's request with detectDevice (@decocms/compat-v7/device), as v7 computed it, in ${changed.join(", ")}`,
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Content: the next major reads saved blocks only from `.deco/blocks`, one
|
|
3
|
+
* JSON file per entry, named by the protocol's file-name rule
|
|
4
|
+
* (`@decocms/blocks/protocol/keys`). A v7 site that has them there keeps
|
|
5
|
+
* them as they are; one that only has a decofile (the `/.decofile` dump, or
|
|
6
|
+
* the generated `.deco/blocks.gen.json`) gets it split into files. The v7
|
|
7
|
+
* generated files in `.deco/` go: `deco schema` and `deco content` write
|
|
8
|
+
* their replacements.
|
|
9
|
+
*/
|
|
10
|
+
import fs from "node:fs";
|
|
11
|
+
import path from "node:path";
|
|
12
|
+
import { blockFileName, checkBlockName, serializeBlock } from "@decocms/blocks/protocol/keys";
|
|
13
|
+
import type { Report } from "./report";
|
|
14
|
+
import { isPlainObject, readContent } from "./walk";
|
|
15
|
+
|
|
16
|
+
/** Generated by the v7 codegen; nothing in the next major reads them. */
|
|
17
|
+
const V7_GENERATED = [
|
|
18
|
+
"blocks.gen.json",
|
|
19
|
+
"blocks.gen.ts",
|
|
20
|
+
"loaders.gen.ts",
|
|
21
|
+
"sections.gen.ts",
|
|
22
|
+
"meta.gen.json",
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
/** Where a v7 site may keep a whole decofile, tried in order. */
|
|
26
|
+
const DECOFILE_CANDIDATES = [".deco/blocks.gen.json", ".decofile.json", ".decofile"];
|
|
27
|
+
|
|
28
|
+
function hasJsonFiles(dir: string): boolean {
|
|
29
|
+
try {
|
|
30
|
+
return fs.readdirSync(dir).some((f) => f.endsWith(".json"));
|
|
31
|
+
} catch {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The entries of a decofile: a map of entry name to block, either at the top
|
|
38
|
+
* level or under `blocks` (the shape `/.decofile` serves).
|
|
39
|
+
*/
|
|
40
|
+
export function decofileEntries(decofile: unknown): Record<string, Record<string, unknown>> {
|
|
41
|
+
if (!isPlainObject(decofile)) return {};
|
|
42
|
+
const values = Object.values(decofile);
|
|
43
|
+
const flat = values.length > 0 && values.every((v) => isPlainObject(v) && "__resolveType" in v);
|
|
44
|
+
const map = flat ? decofile : isPlainObject(decofile.blocks) ? decofile.blocks : {};
|
|
45
|
+
const entries: Record<string, Record<string, unknown>> = {};
|
|
46
|
+
for (const [name, block] of Object.entries(map)) {
|
|
47
|
+
if (isPlainObject(block)) entries[name] = block;
|
|
48
|
+
}
|
|
49
|
+
return entries;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
interface MoveContentOptions {
|
|
53
|
+
/** A decofile to split into `.deco/blocks`, relative to the root. */
|
|
54
|
+
decofile?: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function moveContent(root: string, report: Report, options: MoveContentOptions = {}): void {
|
|
58
|
+
const blocksDir = path.join(root, ".deco", "blocks");
|
|
59
|
+
const hasContent = hasJsonFiles(blocksDir);
|
|
60
|
+
if (options.decofile && hasContent) {
|
|
61
|
+
throw new Error(`--decofile given but .deco/blocks already has content`);
|
|
62
|
+
}
|
|
63
|
+
if (!hasContent) {
|
|
64
|
+
const candidates = options.decofile ? [options.decofile] : DECOFILE_CANDIDATES;
|
|
65
|
+
const source = candidates.map((c) => path.resolve(root, c)).find((c) => fs.existsSync(c));
|
|
66
|
+
if (source) {
|
|
67
|
+
const entries = decofileEntries(JSON.parse(fs.readFileSync(source, "utf8")));
|
|
68
|
+
fs.mkdirSync(blocksDir, { recursive: true });
|
|
69
|
+
let written = 0;
|
|
70
|
+
for (const [name, block] of Object.entries(entries)) {
|
|
71
|
+
const [invalid] = checkBlockName(name);
|
|
72
|
+
if (invalid) {
|
|
73
|
+
report.manual.push({
|
|
74
|
+
step: "content",
|
|
75
|
+
subject: name,
|
|
76
|
+
message: `not a valid entry name (${invalid.message}); save it under another name`,
|
|
77
|
+
});
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
fs.writeFileSync(path.join(blocksDir, blockFileName(name)), serializeBlock(block));
|
|
81
|
+
written++;
|
|
82
|
+
}
|
|
83
|
+
report.done.push({
|
|
84
|
+
step: "content",
|
|
85
|
+
subject: path.relative(root, source),
|
|
86
|
+
message: `split into ${written} files in .deco/blocks`,
|
|
87
|
+
});
|
|
88
|
+
} else if (options.decofile) {
|
|
89
|
+
throw new Error(`no decofile at ${options.decofile}`);
|
|
90
|
+
} else {
|
|
91
|
+
report.manual.push({
|
|
92
|
+
step: "content",
|
|
93
|
+
subject: ".deco/blocks",
|
|
94
|
+
message:
|
|
95
|
+
"no saved blocks found; download the live content (GET <site>/.decofile) and pass it with --decofile",
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Names the file-name rule rejects (v7 saved editor previews like
|
|
101
|
+
// "Preview site/sections/Hero.tsx"): deco content stops on them.
|
|
102
|
+
for (const d of readContent(root).diagnostics) {
|
|
103
|
+
if (d.severity !== "error") continue;
|
|
104
|
+
report.manual.push({
|
|
105
|
+
step: "content",
|
|
106
|
+
subject: `.deco/blocks/${d.file}`,
|
|
107
|
+
message: `${d.message}, or delete it if nothing references it`,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
for (const file of V7_GENERATED) {
|
|
112
|
+
const full = path.join(root, ".deco", file);
|
|
113
|
+
if (!fs.existsSync(full)) continue;
|
|
114
|
+
fs.rmSync(full);
|
|
115
|
+
report.done.push({
|
|
116
|
+
step: "content",
|
|
117
|
+
subject: `.deco/${file}`,
|
|
118
|
+
message: "removed (v7 generated; deco schema and deco content write the next major's files)",
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
}
|