@decocms/blocks-cli 7.87.6 → 7.88.1
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@decocms/blocks-cli",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.88.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Deco codegen (generate-blocks, generate-schema, generate-invoke) and Fresh-to-TanStack migration tooling",
|
|
6
6
|
"repository": {
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"lint:unused": "knip"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@decocms/blocks": "7.
|
|
36
|
+
"@decocms/blocks": "7.88.1",
|
|
37
37
|
"ts-morph": "^27.0.0",
|
|
38
38
|
"tsx": "^4.22.5"
|
|
39
39
|
},
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `readKvDecofile` builds what the KV sync writes. A Fresh + deco repo
|
|
3
|
+
* (`deno.json`, no `package.json`) must get keys Fresh's runtime would produce
|
|
4
|
+
* (one decode pass), and only the redirects Fresh applies by type move to KV.
|
|
5
|
+
* A TanStack repo keeps today's output.
|
|
6
|
+
*/
|
|
7
|
+
import * as fs from "node:fs";
|
|
8
|
+
import * as os from "node:os";
|
|
9
|
+
import * as path from "node:path";
|
|
10
|
+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
11
|
+
import { isFreshRepo, readKvDecofile } from "./kv-snapshot";
|
|
12
|
+
|
|
13
|
+
let repo: string;
|
|
14
|
+
let blocksDir: string;
|
|
15
|
+
|
|
16
|
+
const write = (file: string, content: unknown) => fs.writeFileSync(path.join(blocksDir, file), JSON.stringify(content));
|
|
17
|
+
const single = (from: string) => ({
|
|
18
|
+
__resolveType: "website/loaders/redirect.ts",
|
|
19
|
+
redirect: { from, to: "/to", type: "permanent" },
|
|
20
|
+
});
|
|
21
|
+
const std = {
|
|
22
|
+
__resolveType: "deco-sites/std/loaders/x/redirects.ts",
|
|
23
|
+
redirects: [{ from: "/old", to: "/new", type: "permanent" }],
|
|
24
|
+
};
|
|
25
|
+
const site = {
|
|
26
|
+
__resolveType: "website/mod.ts",
|
|
27
|
+
routes: [{ __resolveType: "website/loaders/pages.ts" }, { __resolveType: "website/loaders/redirects.ts" }],
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
beforeEach(() => {
|
|
31
|
+
repo = fs.mkdtempSync(path.join(os.tmpdir(), "kv-decofile-"));
|
|
32
|
+
blocksDir = path.join(repo, ".deco", "blocks");
|
|
33
|
+
fs.mkdirSync(blocksDir, { recursive: true });
|
|
34
|
+
// Double-encoded name: Fresh's parseBlockId decodes once → "Home%20Page".
|
|
35
|
+
write("Home%2520Page.json", { __resolveType: "website/pages/Page.tsx", path: "/" });
|
|
36
|
+
write("std-redirects.json", std);
|
|
37
|
+
write("promo.json", single("/promo"));
|
|
38
|
+
write("blog.json", single("/blog/*"));
|
|
39
|
+
write("param.json", single("/p/:id"));
|
|
40
|
+
write("absolute.json", single("https://site.com/abs"));
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
afterEach(() => fs.rmSync(repo, { recursive: true, force: true }));
|
|
44
|
+
|
|
45
|
+
describe("readKvDecofile on a Fresh repo", () => {
|
|
46
|
+
beforeEach(() => fs.writeFileSync(path.join(repo, "deno.json"), "{}"));
|
|
47
|
+
|
|
48
|
+
it("moves only exact website/loaders/redirect.ts rules, when the collector is routed", () => {
|
|
49
|
+
write("Site.json", site);
|
|
50
|
+
const out = readKvDecofile(blocksDir, repo);
|
|
51
|
+
|
|
52
|
+
expect(out.fresh).toBe(true);
|
|
53
|
+
expect(out.exact.map((r) => r.path)).toEqual(["/promo"]);
|
|
54
|
+
// Moved: emptied, still resolvable by name.
|
|
55
|
+
expect(out.blocks.promo).toEqual({ __resolveType: "website/loaders/redirect.ts" });
|
|
56
|
+
// Kept verbatim (singular shape): glob, URLPattern, absolute URL.
|
|
57
|
+
expect(out.blocks.blog).toEqual(single("/blog/*"));
|
|
58
|
+
expect(out.blocks.param).toEqual(single("/p/:id"));
|
|
59
|
+
expect(out.blocks.absolute).toEqual(single("https://site.com/abs"));
|
|
60
|
+
// Fresh applies a std block only if referenced: never moved.
|
|
61
|
+
expect(out.blocks["std-redirects"]).toEqual(std);
|
|
62
|
+
// One decode pass.
|
|
63
|
+
expect(out.blocks["Home%20Page"]).toBeDefined();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("moves nothing when no route collects the redirect blocks", () => {
|
|
67
|
+
const out = readKvDecofile(blocksDir, repo);
|
|
68
|
+
expect(out.exact).toEqual([]);
|
|
69
|
+
expect(out.blocks.promo).toEqual(single("/promo"));
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
describe("readKvDecofile on a TanStack repo", () => {
|
|
74
|
+
it("keeps today's output", () => {
|
|
75
|
+
fs.writeFileSync(path.join(repo, "package.json"), "{}");
|
|
76
|
+
fs.writeFileSync(path.join(repo, "deno.json"), "{}");
|
|
77
|
+
const out = readKvDecofile(blocksDir, repo);
|
|
78
|
+
|
|
79
|
+
expect(out.fresh).toBe(false);
|
|
80
|
+
expect(Object.keys(out.blocks)).toContain("Home Page");
|
|
81
|
+
expect(out.blocks["std-redirects"]).toBeUndefined();
|
|
82
|
+
expect(out.exact.map((r) => r.path).sort()).toEqual(["/abs", "/old", "/p/:id", "/promo"]);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
describe("isFreshRepo", () => {
|
|
87
|
+
it("needs deno.json(c) and no package.json", () => {
|
|
88
|
+
expect(isFreshRepo(repo)).toBe(false);
|
|
89
|
+
fs.writeFileSync(path.join(repo, "deno.jsonc"), "{}");
|
|
90
|
+
expect(isFreshRepo(repo)).toBe(true);
|
|
91
|
+
fs.writeFileSync(path.join(repo, "package.json"), "{}");
|
|
92
|
+
expect(isFreshRepo(repo)).toBe(false);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
@@ -19,8 +19,11 @@ import {
|
|
|
19
19
|
revisionKey,
|
|
20
20
|
snapshotKey,
|
|
21
21
|
} from "@decocms/blocks/cms";
|
|
22
|
-
import
|
|
22
|
+
import * as fs from "node:fs";
|
|
23
|
+
import * as path from "node:path";
|
|
24
|
+
import { type ExactRedirect, splitExactRedirects, storedExactRedirectValue } from "@decocms/blocks/sdk/redirects";
|
|
23
25
|
import type { KvRestClient } from "./cf-kv-rest";
|
|
26
|
+
import { type ReadDecofileResult, readDecofileFromDir } from "./read-decofile";
|
|
24
27
|
|
|
25
28
|
export interface Snapshot {
|
|
26
29
|
/** Serialized decofile written to `decofile:<id>`. */
|
|
@@ -37,6 +40,85 @@ export interface DeploymentEntry {
|
|
|
37
40
|
ts: number;
|
|
38
41
|
}
|
|
39
42
|
|
|
43
|
+
/**
|
|
44
|
+
* A Fresh + deco site: `deno.json(c)` at the root and no `package.json` — the
|
|
45
|
+
* same rule the cfworkers-builder uses to pick its Deno build. Detected here,
|
|
46
|
+
* not passed as a flag, so the builder and the operator's decofile-syncer keep
|
|
47
|
+
* calling the CLI unchanged for every framework.
|
|
48
|
+
*/
|
|
49
|
+
export function isFreshRepo(dir: string): boolean {
|
|
50
|
+
const hasDenoConfig = fs.existsSync(path.join(dir, "deno.json")) || fs.existsSync(path.join(dir, "deno.jsonc"));
|
|
51
|
+
return hasDenoConfig && !fs.existsSync(path.join(dir, "package.json"));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Read `blocksDir` and split it into the KV decofile and its exact redirects.
|
|
56
|
+
* The one place both `sync-blocks-to-kv` and `migrate-blocks-to-kv` build what
|
|
57
|
+
* they write, so the two can never disagree on the snapshot.
|
|
58
|
+
*
|
|
59
|
+
* On a Fresh repo, a redirect block whose rules ALL moved to KV is kept as an
|
|
60
|
+
* empty block instead of dropped: Fresh resolves blocks by name, and a routes
|
|
61
|
+
* config that references a dropped `std/x/redirects` block would fail to
|
|
62
|
+
* resolve. TanStack collects redirects by type, so it keeps dropping them.
|
|
63
|
+
*/
|
|
64
|
+
export function readKvDecofile(
|
|
65
|
+
blocksDir: string,
|
|
66
|
+
repoDir: string,
|
|
67
|
+
): ReadDecofileResult & { exact: ExactRedirect[]; fresh: boolean } {
|
|
68
|
+
const fresh = isFreshRepo(repoDir);
|
|
69
|
+
const { blocks, collisions } = readDecofileFromDir(blocksDir, { fresh });
|
|
70
|
+
if (!fresh) {
|
|
71
|
+
const split = splitExactRedirects(blocks);
|
|
72
|
+
return { blocks: split.blocks, collisions, exact: split.exact, fresh };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return { ...splitFreshRedirects(blocks), collisions, fresh };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const FRESH_REDIRECT = "website/loaders/redirect.ts";
|
|
79
|
+
const FRESH_COLLECTOR = `"__resolveType":"website/loaders/redirects.ts"`;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Fresh applies a redirect only in two ways: a block its routes config
|
|
83
|
+
* references by name (`std/x/redirects`, a `website/loaders/redirects.ts`
|
|
84
|
+
* with props), or a `website/loaders/redirect.ts` block, which the
|
|
85
|
+
* `website/loaders/redirects.ts` collector gathers BY TYPE. A KV key applies
|
|
86
|
+
* to every request, so only the second kind moves: a referenced block, or an
|
|
87
|
+
* orphan one Fresh never applies, stays exactly as Fresh resolves it today.
|
|
88
|
+
*
|
|
89
|
+
* Moved: `website/loaders/redirect.ts` blocks with an exact source, and only
|
|
90
|
+
* when the collector is in the decofile. Kept: `/:param` sources (URLPatterns
|
|
91
|
+
* in Fresh) and absolute URLs (Fresh keys those by the full URL, which no
|
|
92
|
+
* request path matches). A moved block stays, emptied, in case something
|
|
93
|
+
* references it by name.
|
|
94
|
+
*/
|
|
95
|
+
function splitFreshRedirects(blocks: Record<string, unknown>): { blocks: Record<string, unknown>; exact: ExactRedirect[] } {
|
|
96
|
+
if (!JSON.stringify(blocks).includes(FRESH_COLLECTOR)) return { blocks, exact: [] };
|
|
97
|
+
const candidates: Record<string, unknown> = {};
|
|
98
|
+
for (const [key, block] of Object.entries(blocks)) {
|
|
99
|
+
if ((block as Record<string, unknown> | null)?.__resolveType === FRESH_REDIRECT) {
|
|
100
|
+
candidates[key] = block;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const split = splitExactRedirects(candidates, {
|
|
104
|
+
keepInDecofile: (from) => from.includes("/:") || /^https?:\/\//.test(from.trim()),
|
|
105
|
+
});
|
|
106
|
+
const out: Record<string, unknown> = {};
|
|
107
|
+
for (const [key, block] of Object.entries(blocks)) {
|
|
108
|
+
// `Object.hasOwn`: a block named `constructor` must not look moved.
|
|
109
|
+
const moved = Object.hasOwn(candidates, key) && !Object.hasOwn(split.blocks, key);
|
|
110
|
+
if (moved) {
|
|
111
|
+
const { redirect: _moved, ...rest } = block as Record<string, unknown>;
|
|
112
|
+
out[key] = rest;
|
|
113
|
+
} else {
|
|
114
|
+
// Kept verbatim: Fresh's loader reads `props.redirect`, not the
|
|
115
|
+
// `redirects: [..]` the split rewrites a kept singular block to.
|
|
116
|
+
out[key] = block;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return { blocks: out, exact: split.exact };
|
|
120
|
+
}
|
|
121
|
+
|
|
40
122
|
export function buildSnapshot(blocks: Record<string, unknown>): Snapshot {
|
|
41
123
|
return {
|
|
42
124
|
snapshot: JSON.stringify(blocks),
|
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
type CollisionRecord,
|
|
18
18
|
decodeBlockNameWithPasses,
|
|
19
19
|
mergeCandidates,
|
|
20
|
+
singleDecodeBlockName,
|
|
20
21
|
} from "./blocks-dedupe";
|
|
21
22
|
import { buildCsvRedirectBlocks } from "./csv-redirects";
|
|
22
23
|
|
|
@@ -32,7 +33,10 @@ export interface ReadDecofileResult {
|
|
|
32
33
|
* Throws if the directory does not exist. Unparseable files are skipped
|
|
33
34
|
* (logged unless `silent`).
|
|
34
35
|
*/
|
|
35
|
-
export function readDecofileFromDir(
|
|
36
|
+
export function readDecofileFromDir(
|
|
37
|
+
blocksDir: string,
|
|
38
|
+
opts: { silent?: boolean; fresh?: boolean } = {},
|
|
39
|
+
): ReadDecofileResult {
|
|
36
40
|
if (!fs.existsSync(blocksDir)) {
|
|
37
41
|
throw new Error(`blocks directory not found: ${blocksDir}`);
|
|
38
42
|
}
|
|
@@ -41,7 +45,12 @@ export function readDecofileFromDir(blocksDir: string, opts: { silent?: boolean
|
|
|
41
45
|
const candidatesWithKeys: Array<{ candidate: Candidate; key: string }> = [];
|
|
42
46
|
|
|
43
47
|
for (const file of files) {
|
|
44
|
-
const
|
|
48
|
+
const decoded = decodeBlockNameWithPasses(file);
|
|
49
|
+
const passes = decoded.passes;
|
|
50
|
+
// A Fresh site's runtime decodes the file name ONCE (`parseBlockId`), and
|
|
51
|
+
// its blocks reference each other by that key. Decoding further would turn
|
|
52
|
+
// `a%2520b.json` into `a b` where Fresh expects `a%20b`.
|
|
53
|
+
const name = opts.fresh ? singleDecodeBlockName(file) : decoded.name;
|
|
45
54
|
const fp = path.join(blocksDir, file);
|
|
46
55
|
let parsed: unknown;
|
|
47
56
|
try {
|
|
@@ -74,6 +83,9 @@ export function readDecofileFromDir(blocksDir: string, opts: { silent?: boolean
|
|
|
74
83
|
// moment the bundled snapshot stops being the fallback (the fastDeploy stub
|
|
75
84
|
// makes KV the only source), turning migration 301s into silent 404s.
|
|
76
85
|
// Same precedence as the generator — a curated CMS redirect wins over CSV.
|
|
86
|
+
// A Fresh site loads its CSV redirects at runtime (`redirectsFromCsv`), so it
|
|
87
|
+
// gets no synthetic blocks.
|
|
88
|
+
if (opts.fresh) return { blocks, collisions };
|
|
77
89
|
const csvBlocks = buildCsvRedirectBlocks(blocks, { blocksDir, silent: opts.silent });
|
|
78
90
|
const merged = Object.keys(csvBlocks).length ? { ...csvBlocks, ...blocks } : blocks;
|
|
79
91
|
|
|
@@ -30,15 +30,14 @@
|
|
|
30
30
|
*/
|
|
31
31
|
|
|
32
32
|
import * as path from "node:path";
|
|
33
|
-
import { splitExactRedirects } from "@decocms/blocks/sdk/redirects";
|
|
34
33
|
import { createKvRestClient, kvConfigFromEnv } from "./lib/cf-kv-rest";
|
|
35
34
|
import {
|
|
36
35
|
buildSnapshot,
|
|
36
|
+
readKvDecofile,
|
|
37
37
|
syncRedirectsToKv,
|
|
38
38
|
verifySnapshotInKv,
|
|
39
39
|
writeSnapshotToKv,
|
|
40
40
|
} from "./lib/kv-snapshot";
|
|
41
|
-
import { readDecofileFromDir } from "./lib/read-decofile";
|
|
42
41
|
|
|
43
42
|
function parseArgs(argv: string[]) {
|
|
44
43
|
const has = (f: string) => argv.includes(f);
|
|
@@ -71,22 +70,19 @@ async function main() {
|
|
|
71
70
|
|
|
72
71
|
const blocksDir = path.resolve(process.cwd(), opts.blocksDir);
|
|
73
72
|
|
|
74
|
-
|
|
73
|
+
// Exact redirects live under their own `redirect:<id>:<path>` keys, not in
|
|
74
|
+
// the decofile. `readKvDecofile` is shared with sync-blocks-to-kv, so a
|
|
75
|
+
// one-shot seed and a CI sync can't disagree on the snapshot.
|
|
76
|
+
let split: ReturnType<typeof readKvDecofile>;
|
|
75
77
|
try {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
console.warn(`warning: ${result.collisions.length} filename collision(s) resolved by tie-break`);
|
|
78
|
+
split = readKvDecofile(blocksDir, process.cwd());
|
|
79
|
+
if (split.collisions.length) {
|
|
80
|
+
console.warn(`warning: ${split.collisions.length} filename collision(s) resolved by tie-break`);
|
|
80
81
|
}
|
|
81
82
|
} catch (e) {
|
|
82
83
|
console.error(`error: ${e instanceof Error ? e.message : String(e)}`);
|
|
83
84
|
process.exit(2);
|
|
84
85
|
}
|
|
85
|
-
|
|
86
|
-
// Exact redirects live under their own `redirect:<id>:<path>` keys, not in
|
|
87
|
-
// the decofile — see splitExactRedirects. Must match what sync-blocks-to-kv
|
|
88
|
-
// writes, or a one-shot seed and a CI sync would disagree on the snapshot.
|
|
89
|
-
const split = splitExactRedirects(blocks);
|
|
90
86
|
const snap = buildSnapshot(split.blocks);
|
|
91
87
|
console.log(`decofile: ${snap.count} blocks, revision ${snap.revision}, ${snap.snapshot.length} bytes`);
|
|
92
88
|
if (split.exact.length > 0) {
|
|
@@ -47,17 +47,16 @@
|
|
|
47
47
|
import { execSync } from "node:child_process";
|
|
48
48
|
import * as path from "node:path";
|
|
49
49
|
import { redirectPrefix } from "@decocms/blocks/cms";
|
|
50
|
-
import { splitExactRedirects } from "@decocms/blocks/sdk/redirects";
|
|
51
50
|
import { createKvRestClient, kvConfigFromEnv } from "./lib/cf-kv-rest";
|
|
52
51
|
import {
|
|
53
52
|
buildSnapshot,
|
|
53
|
+
readKvDecofile,
|
|
54
54
|
recordAndGcDeployment,
|
|
55
55
|
setLiveDeployment,
|
|
56
56
|
syncRedirectsToKv,
|
|
57
57
|
verifySnapshotInKv,
|
|
58
58
|
writeSnapshotToKv,
|
|
59
59
|
} from "./lib/kv-snapshot";
|
|
60
|
-
import { readDecofileFromDir } from "./lib/read-decofile";
|
|
61
60
|
import { changedBlockFiles, changedBlockKeys, purgePathsForChangedKeys } from "./lib/sync-helpers";
|
|
62
61
|
|
|
63
62
|
const DEFAULT_RETAIN = 10;
|
|
@@ -155,20 +154,19 @@ async function main() {
|
|
|
155
154
|
console.log(`${changed.length} changed block file(s) since ${opts.since}.`);
|
|
156
155
|
}
|
|
157
156
|
|
|
158
|
-
|
|
157
|
+
// Exact redirects get ONE KV key each and are removed from the decofile. A
|
|
158
|
+
// bulk-migration site can carry tens of thousands; inside the snapshot they
|
|
159
|
+
// are resident in every isolate twice (parsed graph + RedirectMap) for data
|
|
160
|
+
// that usually matches nothing. Globs stay — they must be scanned in order.
|
|
161
|
+
let split: ReturnType<typeof readKvDecofile>;
|
|
159
162
|
try {
|
|
160
|
-
|
|
163
|
+
split = readKvDecofile(blocksDir, process.cwd());
|
|
161
164
|
} catch (e) {
|
|
162
165
|
console.error(`error: ${e instanceof Error ? e.message : String(e)}`);
|
|
163
166
|
process.exit(2);
|
|
164
167
|
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
// bulk-migration site can carry tens of thousands; inside the snapshot they
|
|
168
|
-
// are resident in every isolate twice (parsed graph + RedirectMap) for data
|
|
169
|
-
// that usually matches nothing. Globs stay — they must be scanned in order.
|
|
170
|
-
const split = splitExactRedirects(blocks);
|
|
171
|
-
const purgePaths = opts.all ? ["/"] : purgePathsForChangedKeys(blocks, changedKeys);
|
|
168
|
+
if (split.fresh) console.log("Fresh site: block keys decoded once, emptied redirect blocks kept.");
|
|
169
|
+
const purgePaths = opts.all ? ["/"] : purgePathsForChangedKeys(split.blocks, changedKeys);
|
|
172
170
|
const snap = buildSnapshot(split.blocks);
|
|
173
171
|
console.log(`decofile: ${snap.count} blocks, revision ${snap.revision} → deployment ${opts.deploymentId}`);
|
|
174
172
|
if (split.exact.length > 0) {
|
|
@@ -212,7 +210,10 @@ async function main() {
|
|
|
212
210
|
}
|
|
213
211
|
console.log(`synced decofile:${opts.deploymentId} (rev ${snap.revision}) → KV.`);
|
|
214
212
|
|
|
215
|
-
|
|
213
|
+
// A Fresh site has no `/_cache/purge` endpoint (it's the TanStack worker's).
|
|
214
|
+
if (opts.purgeUrl && split.fresh) {
|
|
215
|
+
console.log("Fresh site: no /_cache/purge endpoint — skipping purge.");
|
|
216
|
+
} else if (opts.purgeUrl && opts.purgeToken) {
|
|
216
217
|
await purgeCache(opts.purgeUrl, opts.purgeToken, purgePaths);
|
|
217
218
|
} else if (opts.purgeUrl) {
|
|
218
219
|
console.warn("warning: --purge-url given without a token (PURGE_TOKEN/--purge-token) — skipping purge.");
|