@decocms/blocks 8.9.0 → 8.10.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 +1 -1
- package/dist/index.js.map +1 -1
- package/dist/protocol/channel.d.ts +98 -0
- package/dist/protocol/channel.d.ts.map +1 -0
- package/dist/protocol/channel.js +58 -0
- package/dist/protocol/channel.js.map +1 -0
- package/dist/protocol/client.js +2 -2
- package/dist/protocol/client.js.map +1 -1
- package/dist/protocol/conformance/blockMetaVectors.json +214 -0
- package/dist/protocol/conformance/contentHashFixtures.d.ts +22 -0
- package/dist/protocol/conformance/contentHashFixtures.d.ts.map +1 -1
- package/dist/protocol/conformance/contentHashFixtures.js +55 -0
- package/dist/protocol/conformance/contentHashFixtures.js.map +1 -1
- package/dist/protocol/conformance/index.d.ts +2 -1
- package/dist/protocol/conformance/index.d.ts.map +1 -1
- package/dist/protocol/conformance/index.js +2 -1
- package/dist/protocol/conformance/index.js.map +1 -1
- package/dist/protocol/conformance/vectors.d.ts +33 -0
- package/dist/protocol/conformance/vectors.d.ts.map +1 -0
- package/dist/protocol/conformance/vectors.js +3 -0
- package/dist/protocol/conformance/vectors.js.map +1 -0
- package/dist/protocol/index.d.ts +11 -1
- package/dist/protocol/index.d.ts.map +1 -1
- package/dist/protocol/index.js +11 -1
- package/dist/protocol/index.js.map +1 -1
- package/dist/protocol/keys.d.ts +1 -1
- package/dist/protocol/keys.js +1 -1
- package/dist/v8/background.d.ts +13 -0
- package/dist/v8/background.d.ts.map +1 -1
- package/dist/v8/background.js +41 -0
- package/dist/v8/background.js.map +1 -1
- package/dist/v8/blockMeta.d.ts +27 -0
- package/dist/v8/blockMeta.d.ts.map +1 -0
- package/dist/v8/blockMeta.js +75 -0
- package/dist/v8/blockMeta.js.map +1 -0
- package/dist/v8/blockStore.d.ts +43 -0
- package/dist/v8/blockStore.d.ts.map +1 -0
- package/dist/v8/blockStore.js +95 -0
- package/dist/v8/blockStore.js.map +1 -0
- package/dist/v8/boundedJson.d.ts +5 -0
- package/dist/v8/boundedJson.d.ts.map +1 -1
- package/dist/v8/boundedJson.js +10 -2
- package/dist/v8/boundedJson.js.map +1 -1
- package/dist/v8/boundedMap.d.ts +19 -0
- package/dist/v8/boundedMap.d.ts.map +1 -0
- package/dist/v8/boundedMap.js +52 -0
- package/dist/v8/boundedMap.js.map +1 -0
- package/dist/v8/builtins/data.d.ts +18 -2
- package/dist/v8/builtins/data.d.ts.map +1 -1
- package/dist/v8/builtins/data.js +17 -1
- package/dist/v8/builtins/data.js.map +1 -1
- package/dist/v8/builtins/index.d.ts.map +1 -1
- package/dist/v8/builtins/index.js +2 -1
- package/dist/v8/builtins/index.js.map +1 -1
- package/dist/v8/builtins/legacy.d.ts +6 -3
- package/dist/v8/builtins/legacy.d.ts.map +1 -1
- package/dist/v8/builtins/legacy.js +6 -3
- package/dist/v8/builtins/legacy.js.map +1 -1
- package/dist/v8/canonical.d.ts +8 -0
- package/dist/v8/canonical.d.ts.map +1 -1
- package/dist/v8/canonical.js +15 -0
- package/dist/v8/canonical.js.map +1 -1
- package/dist/v8/cli/builtins.d.ts.map +1 -1
- package/dist/v8/cli/builtins.js +1 -0
- package/dist/v8/cli/builtins.js.map +1 -1
- package/dist/v8/cli/check/index.d.ts +10 -0
- package/dist/v8/cli/check/index.d.ts.map +1 -1
- package/dist/v8/cli/check/index.js +91 -7
- package/dist/v8/cli/check/index.js.map +1 -1
- package/dist/v8/cli/content.d.ts +12 -25
- package/dist/v8/cli/content.d.ts.map +1 -1
- package/dist/v8/cli/content.js +46 -75
- package/dist/v8/cli/content.js.map +1 -1
- package/dist/v8/cli/index.d.ts +1 -2
- package/dist/v8/cli/index.d.ts.map +1 -1
- package/dist/v8/cli/index.js +1 -2
- package/dist/v8/cli/index.js.map +1 -1
- package/dist/v8/cli/remote/commands.d.ts +71 -0
- package/dist/v8/cli/remote/commands.d.ts.map +1 -0
- package/dist/v8/cli/remote/commands.js +644 -0
- package/dist/v8/cli/remote/commands.js.map +1 -0
- package/dist/v8/cli/remote/diff.d.ts +15 -0
- package/dist/v8/cli/remote/diff.d.ts.map +1 -0
- package/dist/v8/cli/remote/diff.js +102 -0
- package/dist/v8/cli/remote/diff.js.map +1 -0
- package/dist/v8/cli/remote/env.d.ts +35 -0
- package/dist/v8/cli/remote/env.d.ts.map +1 -0
- package/dist/v8/cli/remote/env.js +84 -0
- package/dist/v8/cli/remote/env.js.map +1 -0
- package/dist/v8/cli/remote/http.d.ts +149 -0
- package/dist/v8/cli/remote/http.d.ts.map +1 -0
- package/dist/v8/cli/remote/http.js +381 -0
- package/dist/v8/cli/remote/http.js.map +1 -0
- package/dist/v8/cli/remote/local.d.ts +90 -0
- package/dist/v8/cli/remote/local.d.ts.map +1 -0
- package/dist/v8/cli/remote/local.js +309 -0
- package/dist/v8/cli/remote/local.js.map +1 -0
- package/dist/v8/cli/root.d.ts +29 -0
- package/dist/v8/cli/root.d.ts.map +1 -1
- package/dist/v8/cli/root.js +28 -0
- package/dist/v8/cli/root.js.map +1 -1
- package/dist/v8/cli/run.d.ts +11 -1
- package/dist/v8/cli/run.d.ts.map +1 -1
- package/dist/v8/cli/run.js +281 -124
- package/dist/v8/cli/run.js.map +1 -1
- package/dist/v8/cli/schema/builtinSchemas.d.ts.map +1 -1
- package/dist/v8/cli/schema/builtinSchemas.js +28 -10
- package/dist/v8/cli/schema/builtinSchemas.js.map +1 -1
- package/dist/v8/cli/schema/generate.js +1 -1
- package/dist/v8/cli/schema/generate.js.map +1 -1
- package/dist/v8/client.d.ts +9 -9
- package/dist/v8/client.d.ts.map +1 -1
- package/dist/v8/client.js +138 -50
- package/dist/v8/client.js.map +1 -1
- package/dist/v8/cms.d.ts +2 -1
- package/dist/v8/cms.d.ts.map +1 -1
- package/dist/v8/cms.js +55 -46
- package/dist/v8/cms.js.map +1 -1
- package/dist/v8/content.d.ts +39 -78
- package/dist/v8/content.d.ts.map +1 -1
- package/dist/v8/content.js +155 -195
- package/dist/v8/content.js.map +1 -1
- package/dist/v8/contentIndex.d.ts +71 -0
- package/dist/v8/contentIndex.d.ts.map +1 -0
- package/dist/v8/contentIndex.js +137 -0
- package/dist/v8/contentIndex.js.map +1 -0
- package/dist/v8/draft.d.ts +7 -7
- package/dist/v8/draft.d.ts.map +1 -1
- package/dist/v8/draft.js +10 -9
- package/dist/v8/draft.js.map +1 -1
- package/dist/v8/draftChanges.d.ts +59 -32
- package/dist/v8/draftChanges.d.ts.map +1 -1
- package/dist/v8/draftChanges.js +139 -75
- package/dist/v8/draftChanges.js.map +1 -1
- package/dist/v8/errors.d.ts +21 -0
- package/dist/v8/errors.d.ts.map +1 -1
- package/dist/v8/errors.js +22 -0
- package/dist/v8/errors.js.map +1 -1
- package/dist/v8/hostedSource.d.ts +57 -0
- package/dist/v8/hostedSource.d.ts.map +1 -0
- package/dist/v8/hostedSource.js +18 -0
- package/dist/v8/hostedSource.js.map +1 -0
- package/dist/v8/identity.d.ts +1 -1
- package/dist/v8/identity.js +1 -1
- package/dist/v8/index.d.ts +1 -0
- package/dist/v8/index.d.ts.map +1 -1
- package/dist/v8/index.js +1 -0
- package/dist/v8/index.js.map +1 -1
- package/dist/v8/matchRoute.js +7 -4
- package/dist/v8/matchRoute.js.map +1 -1
- package/dist/v8/pin.d.ts +34 -0
- package/dist/v8/pin.d.ts.map +1 -0
- package/dist/v8/pin.js +156 -0
- package/dist/v8/pin.js.map +1 -0
- package/dist/v8/preview/StudioBridge.d.ts +8 -8
- package/dist/v8/preview/StudioBridge.d.ts.map +1 -1
- package/dist/v8/preview/StudioBridge.js +8 -10
- package/dist/v8/preview/StudioBridge.js.map +1 -1
- package/dist/v8/preview/StudioChannel.d.ts +51 -0
- package/dist/v8/preview/StudioChannel.d.ts.map +1 -0
- package/dist/v8/preview/StudioChannel.js +104 -0
- package/dist/v8/preview/StudioChannel.js.map +1 -0
- package/dist/v8/preview/index.d.ts +3 -0
- package/dist/v8/preview/index.d.ts.map +1 -1
- package/dist/v8/preview/index.js +3 -0
- package/dist/v8/preview/index.js.map +1 -1
- package/dist/v8/remoteLoader.d.ts +42 -5
- package/dist/v8/remoteLoader.d.ts.map +1 -1
- package/dist/v8/remoteLoader.js +317 -259
- package/dist/v8/remoteLoader.js.map +1 -1
- package/dist/v8/resolver.d.ts +22 -8
- package/dist/v8/resolver.d.ts.map +1 -1
- package/dist/v8/resolver.js +37 -32
- package/dist/v8/resolver.js.map +1 -1
- package/dist/v8/revisionId.d.ts +11 -0
- package/dist/v8/revisionId.d.ts.map +1 -0
- package/dist/v8/revisionId.js +13 -0
- package/dist/v8/revisionId.js.map +1 -0
- package/dist/v8/settings.d.ts +6 -6
- package/dist/v8/settings.d.ts.map +1 -1
- package/dist/v8/settings.js +8 -7
- package/dist/v8/settings.js.map +1 -1
- package/dist/v8/telemetry.d.ts +4 -2
- package/dist/v8/telemetry.d.ts.map +1 -1
- package/dist/v8/telemetry.js +2 -2
- package/dist/v8/telemetry.js.map +1 -1
- package/dist/v8/types.d.ts +52 -40
- package/dist/v8/types.d.ts.map +1 -1
- package/dist/v8/variants.d.ts +7 -2
- package/dist/v8/variants.d.ts.map +1 -1
- package/dist/v8/variants.js +21 -24
- package/dist/v8/variants.js.map +1 -1
- package/dist/v8/vite/index.d.ts +2 -2
- package/dist/v8/vite/index.js +1 -1
- package/dist/v8/vite/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +2 -0
- package/src/protocol/channel.ts +127 -0
- package/src/protocol/client.ts +2 -2
- package/src/protocol/conformance/blockMetaVectors.json +214 -0
- package/src/protocol/conformance/contentHashFixtures.ts +74 -0
- package/src/protocol/conformance/index.ts +9 -1
- package/src/protocol/conformance/vectors.ts +36 -0
- package/src/protocol/index.ts +32 -0
- package/src/protocol/keys.ts +1 -1
- package/src/v8/background.ts +42 -0
- package/src/v8/blockMeta.ts +100 -0
- package/src/v8/blockStore.ts +124 -0
- package/src/v8/boundedJson.ts +20 -2
- package/src/v8/boundedMap.ts +53 -0
- package/src/v8/builtins/data.ts +26 -2
- package/src/v8/builtins/index.ts +2 -1
- package/src/v8/builtins/legacy.ts +6 -3
- package/src/v8/canonical.ts +15 -0
- package/src/v8/cli/builtins.ts +1 -0
- package/src/v8/cli/check/index.ts +97 -7
- package/src/v8/cli/content.ts +49 -91
- package/src/v8/cli/index.ts +1 -2
- package/src/v8/cli/remote/commands.ts +834 -0
- package/src/v8/cli/remote/diff.ts +100 -0
- package/src/v8/cli/remote/env.ts +107 -0
- package/src/v8/cli/remote/http.ts +484 -0
- package/src/v8/cli/remote/local.ts +343 -0
- package/src/v8/cli/root.ts +40 -0
- package/src/v8/cli/run.ts +333 -136
- package/src/v8/cli/schema/builtinSchemas.ts +29 -10
- package/src/v8/cli/schema/generate.ts +1 -1
- package/src/v8/client.ts +162 -60
- package/src/v8/cms.ts +62 -53
- package/src/v8/content.ts +179 -229
- package/src/v8/contentIndex.ts +183 -0
- package/src/v8/draft.ts +15 -14
- package/src/v8/draftChanges.ts +151 -85
- package/src/v8/errors.ts +37 -0
- package/src/v8/hostedSource.ts +67 -0
- package/src/v8/identity.ts +1 -1
- package/src/v8/index.ts +1 -0
- package/src/v8/matchRoute.ts +7 -4
- package/src/v8/pin.ts +176 -0
- package/src/v8/preview/StudioBridge.ts +8 -10
- package/src/v8/preview/StudioChannel.ts +116 -0
- package/src/v8/preview/index.ts +3 -0
- package/src/v8/remoteLoader.ts +350 -286
- package/src/v8/resolver.ts +56 -33
- package/src/v8/revisionId.ts +14 -0
- package/src/v8/settings.ts +10 -9
- package/src/v8/telemetry.ts +3 -3
- package/src/v8/types.ts +52 -40
- package/src/v8/variants.ts +30 -26
- package/src/v8/vite/index.ts +3 -3
- package/dist/v8/cli/serve/server.d.ts +0 -29
- package/dist/v8/cli/serve/server.d.ts.map +0 -1
- package/dist/v8/cli/serve/server.js +0 -296
- package/dist/v8/cli/serve/server.js.map +0 -1
- package/src/v8/cli/serve/server.ts +0 -348
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"commands.d.ts","sourceRoot":"","sources":["../../../../src/v8/cli/remote/commands.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,644 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The remote content commands (spec: cli › The deco CLI): `login`, `pull`,
|
|
3
|
+
* `diff`, `push`, `publish`, `drafts`, `releases`, the remote sources of
|
|
4
|
+
* `check`, and `schema push`. The site is `DECO_SITE` (`resolveSite`). Writes go through the Studio API;
|
|
5
|
+
* only public reads go straight to the CDN. Nothing here retries and nothing
|
|
6
|
+
* publishes as a side effect: only `deco publish` goes live.
|
|
7
|
+
*/
|
|
8
|
+
import fs from "node:fs";
|
|
9
|
+
import { blockVersion } from "../../../protocol/canonical.js";
|
|
10
|
+
import { ErrorCode } from "../../../protocol/errors.js";
|
|
11
|
+
import { serializeBlock } from "../../../protocol/keys.js";
|
|
12
|
+
import { MAX_BLOCK_BYTES, MAX_OPS_PER_APPLY, MAX_REQUEST_BYTES, } from "../../../protocol/server/core.js";
|
|
13
|
+
import { TOMBSTONE_VERSION } from "../../../protocol/storage.js";
|
|
14
|
+
import { isRevisionId } from "../../revisionId.js";
|
|
15
|
+
import { paint } from "../color.js";
|
|
16
|
+
import { CliError, EXIT, usageError } from "../root.js";
|
|
17
|
+
import { formatBlockDiff } from "./diff.js";
|
|
18
|
+
import { deliveryUrl, loginCommand, resolveToken, studioUrl, } from "./env.js";
|
|
19
|
+
import { applyRequestBody, Delivery, exitCodeForRpc, layer, RpcError, Studio, toMap, } from "./http.js";
|
|
20
|
+
import { draftVersion, localChanges, mirrorBlocks, readLocal, readPull, resolveSite, writeLocalBlock, writePull, } from "./local.js";
|
|
21
|
+
const ok = (result) => ({ exitCode: EXIT.ok, result });
|
|
22
|
+
function services(ctx) {
|
|
23
|
+
const { site } = resolveSite(ctx.paths, ctx.env);
|
|
24
|
+
let token;
|
|
25
|
+
const studio = new Studio({
|
|
26
|
+
base: studioUrl(ctx.env),
|
|
27
|
+
site,
|
|
28
|
+
fetch: ctx.fetch,
|
|
29
|
+
token: () => {
|
|
30
|
+
token ??= resolveToken(ctx.env, ctx.runCommand);
|
|
31
|
+
return token;
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
return { site, studio, delivery: new Delivery(deliveryUrl(ctx.env), site, ctx.fetch) };
|
|
35
|
+
}
|
|
36
|
+
function plural(n, word) {
|
|
37
|
+
return `${n} ${word}${n === 1 ? "" : "s"}`;
|
|
38
|
+
}
|
|
39
|
+
function at(seconds) {
|
|
40
|
+
return typeof seconds === "number" ? new Date(seconds * 1000).toISOString() : "an unknown time";
|
|
41
|
+
}
|
|
42
|
+
// ---------------------------------------------------------------------------
|
|
43
|
+
// login
|
|
44
|
+
// ---------------------------------------------------------------------------
|
|
45
|
+
export function login(ctx) {
|
|
46
|
+
const command = loginCommand(ctx.env);
|
|
47
|
+
ctx.reporter.info(`The deco CLI uses Studio's login. Log in once with:\n\n ${command}\n\n` +
|
|
48
|
+
"In CI or a sandbox, set DECO_TOKEN to a Studio API key instead.");
|
|
49
|
+
return ok({ command });
|
|
50
|
+
}
|
|
51
|
+
async function readSource(studio, delivery, draft) {
|
|
52
|
+
if (draft === null) {
|
|
53
|
+
const production = await delivery.production();
|
|
54
|
+
return { draft, ...production };
|
|
55
|
+
}
|
|
56
|
+
// Resolve first: a missing login or draft is reported before the CDN is read.
|
|
57
|
+
const resolved = await studio.resolveDraft(draft);
|
|
58
|
+
const [production, body] = await Promise.all([
|
|
59
|
+
delivery.production(),
|
|
60
|
+
delivery.draft(resolved.slug, resolved.etag),
|
|
61
|
+
]);
|
|
62
|
+
return { draft, revision: production.revision, blocks: layer(production.blocks, body), body };
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Latest ⊕ a draft for a few names only: the live index and the draft's
|
|
66
|
+
* folder, then the production bodies of `names` (plus every name the draft
|
|
67
|
+
* touches when `names` is omitted). Push and publish never read the rest.
|
|
68
|
+
*/
|
|
69
|
+
async function readOverlay(delivery, resolved, names) {
|
|
70
|
+
const [index, body] = await Promise.all([
|
|
71
|
+
delivery.latestIndex(),
|
|
72
|
+
delivery.draft(resolved.slug, resolved.etag),
|
|
73
|
+
]);
|
|
74
|
+
const wanted = names ?? [...Object.keys(body.set), ...body.delete];
|
|
75
|
+
const before = index === null ? Object.create(null) : await delivery.bodies(index, wanted);
|
|
76
|
+
return { revision: index?.revision ?? null, before, body };
|
|
77
|
+
}
|
|
78
|
+
function describeSource(source) {
|
|
79
|
+
const revision = source.revision ? `revision ${source.revision}` : "no release yet";
|
|
80
|
+
return source.draft === null
|
|
81
|
+
? `production (${revision})`
|
|
82
|
+
: `draft ${source.draft} (over ${revision})`;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* What `deco diff` compared against. A draft is read as it is now (pull.json
|
|
86
|
+
* keeps only hashes), which can differ from what was pulled: the label says so.
|
|
87
|
+
*/
|
|
88
|
+
function describeDiffSource(source) {
|
|
89
|
+
if (source.draft === null)
|
|
90
|
+
return describeSource(source);
|
|
91
|
+
const revision = source.revision ? `revision ${source.revision}` : "no release yet";
|
|
92
|
+
return `draft ${source.draft} as it is now (over ${revision})`;
|
|
93
|
+
}
|
|
94
|
+
// ---------------------------------------------------------------------------
|
|
95
|
+
// pull
|
|
96
|
+
// ---------------------------------------------------------------------------
|
|
97
|
+
export async function pull(ctx, options) {
|
|
98
|
+
const { studio, delivery } = services(ctx);
|
|
99
|
+
if (!options.force) {
|
|
100
|
+
const previous = readPull(ctx.paths);
|
|
101
|
+
const changes = await localChanges(readLocal(ctx.paths), previous);
|
|
102
|
+
if (changes.length > 0) {
|
|
103
|
+
throw new CliError(`.deco/blocks has ${plural(changes.length, "change")} not pushed (${changes
|
|
104
|
+
.slice(0, 5)
|
|
105
|
+
.map((c) => c.name)
|
|
106
|
+
.join(", ")}${changes.length > 5 ? ", …" : ""}); push them, or pull with --force to overwrite them`, { code: "unpushed_changes", details: { changes: changes.map(changeRow) } });
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
const source = await readSource(studio, delivery, options.draft);
|
|
110
|
+
const blocks = Object.create(null);
|
|
111
|
+
for (const name of Object.keys(source.blocks)) {
|
|
112
|
+
blocks[name] = {
|
|
113
|
+
hash: await blockVersion(source.blocks[name]),
|
|
114
|
+
ifMatch: source.body ? await draftVersion(source.body, name) : null,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
for (const name of source.body?.delete ?? []) {
|
|
118
|
+
if (!Object.hasOwn(blocks, name))
|
|
119
|
+
blocks[name] = { hash: null, ifMatch: TOMBSTONE_VERSION };
|
|
120
|
+
}
|
|
121
|
+
mirrorBlocks(ctx.paths, source.blocks);
|
|
122
|
+
writePull(ctx.paths, {
|
|
123
|
+
format: 1,
|
|
124
|
+
draft: source.draft,
|
|
125
|
+
revision: source.revision,
|
|
126
|
+
pulledAt: new Date().toISOString(),
|
|
127
|
+
blocks,
|
|
128
|
+
});
|
|
129
|
+
const count = Object.keys(source.blocks).length;
|
|
130
|
+
ctx.reporter.info(`pulled ${plural(count, "block")} from ${describeSource(source)}`);
|
|
131
|
+
return ok({ draft: source.draft, revision: source.revision, blocks: count });
|
|
132
|
+
}
|
|
133
|
+
function changeRow(change) {
|
|
134
|
+
return { name: change.name, status: change.status };
|
|
135
|
+
}
|
|
136
|
+
const STATUS_MARK = { changed: "M", new: "A", deleted: "D" };
|
|
137
|
+
function printChange(reporter, name, status, from, to) {
|
|
138
|
+
const c = paint(reporter.color);
|
|
139
|
+
const body = formatBlockDiff(from, to, { color: reporter.color });
|
|
140
|
+
reporter.info(`${c.bold(`${STATUS_MARK[status]} ${name}`)}${body ? `\n${body}` : ""}`);
|
|
141
|
+
}
|
|
142
|
+
// ---------------------------------------------------------------------------
|
|
143
|
+
// diff
|
|
144
|
+
// ---------------------------------------------------------------------------
|
|
145
|
+
/** What differs between two sets of blocks, in name order (`to` is the newer side). */
|
|
146
|
+
async function compare(from, to) {
|
|
147
|
+
const rows = [];
|
|
148
|
+
for (const name of [...new Set([...Object.keys(from), ...Object.keys(to)])].sort()) {
|
|
149
|
+
const inFrom = Object.hasOwn(from, name);
|
|
150
|
+
const inTo = Object.hasOwn(to, name);
|
|
151
|
+
if (inFrom && inTo) {
|
|
152
|
+
if ((await blockVersion(from[name])) !== (await blockVersion(to[name])))
|
|
153
|
+
rows.push({ name, status: "changed" });
|
|
154
|
+
}
|
|
155
|
+
else
|
|
156
|
+
rows.push({ name, status: inTo ? "new" : "deleted" });
|
|
157
|
+
}
|
|
158
|
+
return rows;
|
|
159
|
+
}
|
|
160
|
+
export async function diff(ctx, options) {
|
|
161
|
+
const saved = readLocal(ctx.paths);
|
|
162
|
+
const local = saved.blocks;
|
|
163
|
+
const { studio, delivery } = services(ctx);
|
|
164
|
+
let rows;
|
|
165
|
+
let source;
|
|
166
|
+
if (options.draft !== null) {
|
|
167
|
+
source = await readSource(studio, delivery, options.draft);
|
|
168
|
+
rows = await compare(source.blocks, local);
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
const state = readPull(ctx.paths);
|
|
172
|
+
if (!state)
|
|
173
|
+
throw new CliError("nothing pulled yet: run deco pull", { code: "not_pulled" });
|
|
174
|
+
rows = (await localChanges(saved, state)).map(changeRow);
|
|
175
|
+
if (state.draft !== null) {
|
|
176
|
+
source = await readSource(studio, delivery, state.draft);
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
const blocks = state.revision === null ? Object.create(null) : await delivery.revision(state.revision);
|
|
180
|
+
if (blocks === null) {
|
|
181
|
+
throw new CliError(`the CDN has no revision ${state.revision}`, {
|
|
182
|
+
code: "revision_not_found",
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
source = { draft: null, revision: state.revision, blocks };
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
for (const row of rows) {
|
|
189
|
+
printChange(ctx.reporter, row.name, row.status, source.blocks[row.name], local[row.name]);
|
|
190
|
+
}
|
|
191
|
+
ctx.reporter.info(rows.length === 0
|
|
192
|
+
? `no changes against ${describeDiffSource(source)}`
|
|
193
|
+
: `${plural(rows.length, "change")} against ${describeDiffSource(source)}`);
|
|
194
|
+
return ok({ draft: source.draft, revision: source.revision, changes: rows });
|
|
195
|
+
}
|
|
196
|
+
const ENVELOPE_BYTES = Buffer.byteLength(applyRequestBody({ set: {}, delete: [], ifMatch: {} }));
|
|
197
|
+
function itemBytes(item) {
|
|
198
|
+
const key = JSON.stringify(item.name);
|
|
199
|
+
return {
|
|
200
|
+
set: item.block === undefined ? 0 : Buffer.byteLength(`${key}:${JSON.stringify(item.block)}`),
|
|
201
|
+
delete: item.block === undefined ? Buffer.byteLength(key) : 0,
|
|
202
|
+
ifMatch: Buffer.byteLength(`${key}:${JSON.stringify(item.ifMatch)}`),
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Packs items, in order, into batches whose serialized `blocks.apply`
|
|
207
|
+
* request stays at most `limit` bytes and that hold at most `maxNames`
|
|
208
|
+
* names (set plus delete): the two caps the content handler puts on one
|
|
209
|
+
* call. The byte count is exact: the size of a JSON object is its entries
|
|
210
|
+
* plus one comma between each two. A block whose stored form is over the
|
|
211
|
+
* handler's per-entry cap (`MAX_BLOCK_BYTES`) can never be saved, so it
|
|
212
|
+
* fails here, before anything is sent.
|
|
213
|
+
*/
|
|
214
|
+
export function packBatches(items, limit = MAX_REQUEST_BYTES, maxNames = MAX_OPS_PER_APPLY) {
|
|
215
|
+
const batches = [];
|
|
216
|
+
let current = [];
|
|
217
|
+
let size = ENVELOPE_BYTES;
|
|
218
|
+
let counts = { set: 0, delete: 0, ifMatch: 0 };
|
|
219
|
+
const added = (bytes, c) => (bytes.set ? bytes.set + (c.set > 0 ? 1 : 0) : 0) +
|
|
220
|
+
(bytes.delete ? bytes.delete + (c.delete > 0 ? 1 : 0) : 0) +
|
|
221
|
+
bytes.ifMatch +
|
|
222
|
+
(c.ifMatch > 0 ? 1 : 0);
|
|
223
|
+
for (const item of items) {
|
|
224
|
+
if (item.block !== undefined &&
|
|
225
|
+
Buffer.byteLength(serializeBlock(item.block)) > MAX_BLOCK_BYTES) {
|
|
226
|
+
throw new CliError(`block "${item.name}" is larger than a save can carry (${MAX_BLOCK_BYTES} bytes a block)`, { code: "too_large", details: { name: item.name } });
|
|
227
|
+
}
|
|
228
|
+
const bytes = itemBytes(item);
|
|
229
|
+
if (ENVELOPE_BYTES + added(bytes, { set: 0, delete: 0, ifMatch: 0 }) > limit) {
|
|
230
|
+
throw new CliError(`block "${item.name}" alone is larger than a save can carry (${limit} bytes)`, { code: "too_large", details: { name: item.name } });
|
|
231
|
+
}
|
|
232
|
+
if (current.length >= maxNames || (current.length > 0 && size + added(bytes, counts) > limit)) {
|
|
233
|
+
batches.push(current);
|
|
234
|
+
current = [];
|
|
235
|
+
size = ENVELOPE_BYTES;
|
|
236
|
+
counts = { set: 0, delete: 0, ifMatch: 0 };
|
|
237
|
+
}
|
|
238
|
+
size += added(bytes, counts);
|
|
239
|
+
current.push(item);
|
|
240
|
+
counts = {
|
|
241
|
+
set: counts.set + (bytes.set ? 1 : 0),
|
|
242
|
+
delete: counts.delete + (bytes.delete ? 1 : 0),
|
|
243
|
+
ifMatch: counts.ifMatch + 1,
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
if (current.length > 0)
|
|
247
|
+
batches.push(current);
|
|
248
|
+
return batches;
|
|
249
|
+
}
|
|
250
|
+
function applyParams(items) {
|
|
251
|
+
const set = {};
|
|
252
|
+
const deleted = [];
|
|
253
|
+
const ifMatch = {};
|
|
254
|
+
for (const item of items) {
|
|
255
|
+
// defineProperty, so a block named "__proto__" is sent as a name.
|
|
256
|
+
if (item.block !== undefined)
|
|
257
|
+
Object.defineProperty(set, item.name, {
|
|
258
|
+
value: item.block,
|
|
259
|
+
enumerable: true,
|
|
260
|
+
writable: true,
|
|
261
|
+
configurable: true,
|
|
262
|
+
});
|
|
263
|
+
else
|
|
264
|
+
deleted.push(item.name);
|
|
265
|
+
Object.defineProperty(ifMatch, item.name, {
|
|
266
|
+
value: item.ifMatch,
|
|
267
|
+
enumerable: true,
|
|
268
|
+
writable: true,
|
|
269
|
+
configurable: true,
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
return { set, delete: deleted, ifMatch };
|
|
273
|
+
}
|
|
274
|
+
function readConflicts(data) {
|
|
275
|
+
const d = (data ?? {});
|
|
276
|
+
const out = Object.create(null);
|
|
277
|
+
for (const [name, entry] of Object.entries(d.entries ?? {})) {
|
|
278
|
+
const held = d.draft && Object.hasOwn(d.draft, name) ? d.draft[name] : null;
|
|
279
|
+
out[name] = {
|
|
280
|
+
expected: entry?.expected ?? null,
|
|
281
|
+
actual: entry?.actual ?? null,
|
|
282
|
+
theirs: held ? held.block : null,
|
|
283
|
+
editor: held?.editor ?? null,
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
return out;
|
|
287
|
+
}
|
|
288
|
+
/** The versions that landed before a save stopped (`-32008` with `data.landed`), or `null`. */
|
|
289
|
+
function landedOf(error) {
|
|
290
|
+
if (error.code !== ErrorCode.Unavailable)
|
|
291
|
+
return null;
|
|
292
|
+
const landed = error.data?.landed;
|
|
293
|
+
if (typeof landed !== "object" || landed === null || Array.isArray(landed))
|
|
294
|
+
return null;
|
|
295
|
+
const out = Object.create(null);
|
|
296
|
+
for (const [name, version] of Object.entries(landed)) {
|
|
297
|
+
if (typeof version === "string")
|
|
298
|
+
out[name] = version;
|
|
299
|
+
}
|
|
300
|
+
return out;
|
|
301
|
+
}
|
|
302
|
+
export async function push(ctx, options) {
|
|
303
|
+
const { studio, delivery } = services(ctx);
|
|
304
|
+
let saved = readLocal(ctx.paths);
|
|
305
|
+
const pulled = readPull(ctx.paths);
|
|
306
|
+
// Pushing to a draft other than the one pulled: every precondition is
|
|
307
|
+
// relative to the pulled draft, so none of them hold for this one.
|
|
308
|
+
let state;
|
|
309
|
+
if (pulled && pulled.draft === options.draft)
|
|
310
|
+
state = pulled;
|
|
311
|
+
else {
|
|
312
|
+
const blocks = Object.create(null);
|
|
313
|
+
for (const [name, entry] of Object.entries(pulled?.blocks ?? {}))
|
|
314
|
+
blocks[name] = { hash: entry.hash, ifMatch: null };
|
|
315
|
+
state = {
|
|
316
|
+
format: 1,
|
|
317
|
+
draft: options.draft,
|
|
318
|
+
revision: pulled?.revision ?? null,
|
|
319
|
+
pulledAt: pulled?.pulledAt ?? new Date().toISOString(),
|
|
320
|
+
blocks,
|
|
321
|
+
};
|
|
322
|
+
if (pulled)
|
|
323
|
+
writePull(ctx.paths, state);
|
|
324
|
+
}
|
|
325
|
+
// (a) The names changed locally since the pull; deletes only with --prune.
|
|
326
|
+
const local = await localChanges(saved, state);
|
|
327
|
+
const changes = local.filter((change) => change.block !== undefined || options.prune);
|
|
328
|
+
const unsentDeletes = options.prune
|
|
329
|
+
? []
|
|
330
|
+
: local.filter((change) => change.block === undefined).map((change) => change.name);
|
|
331
|
+
const ensured = await studio.ensureDraft(options.draft);
|
|
332
|
+
if (ensured.created)
|
|
333
|
+
ctx.reporter.info(`created draft ${options.draft}`);
|
|
334
|
+
// (b) Of those, the ones that differ from latest ⊕ the draft now, with the
|
|
335
|
+
// draft's versions read now as their preconditions.
|
|
336
|
+
const now = await readOverlay(delivery, ensured, changes.map((change) => change.name));
|
|
337
|
+
const current = layer(now.before, now.body);
|
|
338
|
+
const items = [];
|
|
339
|
+
for (const change of changes) {
|
|
340
|
+
const there = Object.hasOwn(current, change.name) ? current[change.name] : undefined;
|
|
341
|
+
const same = change.block === undefined
|
|
342
|
+
? there === undefined
|
|
343
|
+
: there !== undefined && (await blockVersion(there)) === change.hash;
|
|
344
|
+
if (same) {
|
|
345
|
+
// Already what the draft shows: in sync, nothing to send.
|
|
346
|
+
state.blocks[change.name] = {
|
|
347
|
+
hash: change.block === undefined ? null : (change.hash ?? null),
|
|
348
|
+
ifMatch: await draftVersion(now.body, change.name),
|
|
349
|
+
};
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
items.push({
|
|
353
|
+
name: change.name,
|
|
354
|
+
block: change.block,
|
|
355
|
+
hash: change.hash ?? null,
|
|
356
|
+
ifMatch: await draftVersion(now.body, change.name),
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
// One change, one save: a push that doesn't fit one blocks.apply fails.
|
|
360
|
+
if (items.length < changes.length)
|
|
361
|
+
writePull(ctx.paths, state);
|
|
362
|
+
const batches = packBatches(items);
|
|
363
|
+
if (batches.length > 1) {
|
|
364
|
+
throw new CliError(`the ${plural(items.length, "changed block")} don't fit one save (${MAX_REQUEST_BYTES} bytes, ${MAX_OPS_PER_APPLY} names); push fewer at a time`, { code: "too_large", details: { names: items.map((item) => item.name) } });
|
|
365
|
+
}
|
|
366
|
+
// Production's bodies of `names` only, from the latest index now: push reads the names it pushes.
|
|
367
|
+
const productionBodies = async (names) => {
|
|
368
|
+
const index = await delivery.latestIndex();
|
|
369
|
+
return index === null ? Object.create(null) : delivery.bodies(index, names);
|
|
370
|
+
};
|
|
371
|
+
const record = (batch, versions) => {
|
|
372
|
+
for (const item of batch) {
|
|
373
|
+
state.blocks[item.name] = {
|
|
374
|
+
hash: item.block === undefined ? null : item.hash,
|
|
375
|
+
ifMatch: Object.hasOwn(versions, item.name) ? (versions[item.name] ?? null) : null,
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
writePull(ctx.paths, state);
|
|
379
|
+
};
|
|
380
|
+
let pushed = 0;
|
|
381
|
+
let taken = 0;
|
|
382
|
+
let resent = false;
|
|
383
|
+
for (const [index, original] of batches.entries()) {
|
|
384
|
+
let batch = original;
|
|
385
|
+
// --ours or --theirs is applied to one conflict; a re-send after files landed is no conflict retry.
|
|
386
|
+
let conflictRetried = false;
|
|
387
|
+
for (;;) {
|
|
388
|
+
try {
|
|
389
|
+
const result = await studio.apply(options.draft, applyRequestBody(applyParams(batch)));
|
|
390
|
+
record(batch, result.versions ?? {});
|
|
391
|
+
pushed += batch.length;
|
|
392
|
+
break;
|
|
393
|
+
}
|
|
394
|
+
catch (error) {
|
|
395
|
+
if (!(error instanceof RpcError))
|
|
396
|
+
throw error;
|
|
397
|
+
const landed = landedOf(error);
|
|
398
|
+
if (landed !== null) {
|
|
399
|
+
// Some files landed before the save stopped: keep them, send the rest once more.
|
|
400
|
+
const done = batch.filter((item) => Object.hasOwn(landed, item.name));
|
|
401
|
+
record(done, landed);
|
|
402
|
+
pushed += done.length;
|
|
403
|
+
batch = batch.filter((item) => !Object.hasOwn(landed, item.name));
|
|
404
|
+
if (batch.length === 0)
|
|
405
|
+
break;
|
|
406
|
+
if (!resent) {
|
|
407
|
+
resent = true;
|
|
408
|
+
continue;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
if (error.code !== ErrorCode.Conflict) {
|
|
412
|
+
const busy = error.code === ErrorCode.Unavailable;
|
|
413
|
+
throw new CliError(busy
|
|
414
|
+
? `Studio is busy (${error.message}); ${pushed > 0 ? `${plural(pushed, "block")} saved, the rest not` : "nothing was saved"}, run deco push again`
|
|
415
|
+
: `Studio refused the save: ${error.message}`, {
|
|
416
|
+
exitCode: exitCodeForRpc(error.code),
|
|
417
|
+
code: busy ? "unavailable" : `rpc_${-error.code}`,
|
|
418
|
+
details: { pushed, batch: index + 1, batches: batches.length, rpc: error.data },
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
const conflicts = readConflicts(error.data);
|
|
422
|
+
if (options.side === null || conflictRetried) {
|
|
423
|
+
reportConflicts(ctx.reporter, conflicts, saved);
|
|
424
|
+
throw new CliError(`${plural(Object.keys(conflicts).length, "block")} changed in draft ${options.draft} while you pushed; nothing of this save was written. ` +
|
|
425
|
+
"Pull again, or push with --ours (keep yours) or --theirs (take theirs)", {
|
|
426
|
+
exitCode: EXIT.conflict,
|
|
427
|
+
code: "conflict",
|
|
428
|
+
details: { pushed, batch: index + 1, batches: batches.length, conflicts },
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
conflictRetried = true;
|
|
432
|
+
if (options.side === "ours") {
|
|
433
|
+
batch = batch.map((item) => Object.hasOwn(conflicts, item.name)
|
|
434
|
+
? { ...item, ifMatch: conflicts[item.name].actual }
|
|
435
|
+
: item);
|
|
436
|
+
continue;
|
|
437
|
+
}
|
|
438
|
+
// --theirs: take each conflicting block into .deco/blocks as the draft holds it.
|
|
439
|
+
const fromProduction = Object.entries(conflicts)
|
|
440
|
+
.filter(([, c]) => c.theirs === null && c.actual === null)
|
|
441
|
+
.map(([name]) => name);
|
|
442
|
+
const prod = fromProduction.length > 0 ? await productionBodies(fromProduction) : null;
|
|
443
|
+
for (const [name, conflict] of Object.entries(conflicts)) {
|
|
444
|
+
let value;
|
|
445
|
+
let hash = null;
|
|
446
|
+
if (conflict.actual === TOMBSTONE_VERSION)
|
|
447
|
+
value = undefined;
|
|
448
|
+
else if (conflict.theirs !== null)
|
|
449
|
+
value = conflict.theirs;
|
|
450
|
+
else
|
|
451
|
+
value = prod && Object.hasOwn(prod, name) ? prod[name] : undefined;
|
|
452
|
+
if (value !== undefined)
|
|
453
|
+
hash = await blockVersion(value);
|
|
454
|
+
writeLocalBlock(ctx.paths, saved, name, value);
|
|
455
|
+
state.blocks[name] = { hash, ifMatch: conflict.actual };
|
|
456
|
+
taken++;
|
|
457
|
+
}
|
|
458
|
+
writePull(ctx.paths, state);
|
|
459
|
+
saved = readLocal(ctx.paths);
|
|
460
|
+
batch = batch.filter((item) => !Object.hasOwn(conflicts, item.name));
|
|
461
|
+
if (batch.length === 0)
|
|
462
|
+
break;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
const resolved = await studio.resolveDraft(options.draft);
|
|
467
|
+
const link = resolved.previewUrl ?? resolved.pointer;
|
|
468
|
+
ctx.reporter.info(`pushed ${plural(pushed, "block")} to draft ${options.draft}` +
|
|
469
|
+
(taken > 0 ? `; took theirs for ${plural(taken, "block")}` : ""));
|
|
470
|
+
if (unsentDeletes.length > 0) {
|
|
471
|
+
ctx.reporter.info(`not sent: ${plural(unsentDeletes.length, "deleted block")} (${unsentDeletes.join(", ")}); push with --prune to send deletes`);
|
|
472
|
+
}
|
|
473
|
+
if (link)
|
|
474
|
+
ctx.reporter.info(`preview: ${link}`);
|
|
475
|
+
return ok({
|
|
476
|
+
draft: options.draft,
|
|
477
|
+
pushed,
|
|
478
|
+
batches: batches.length,
|
|
479
|
+
tookTheirs: taken,
|
|
480
|
+
previewUrl: resolved.previewUrl,
|
|
481
|
+
pointer: resolved.pointer,
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
function reportConflicts(reporter, conflicts, saved) {
|
|
485
|
+
const c = paint(reporter.color);
|
|
486
|
+
for (const [name, conflict] of Object.entries(conflicts)) {
|
|
487
|
+
const who = conflict.editor
|
|
488
|
+
? `changed by ${conflict.editor.userId} at ${at(conflict.editor.at)}`
|
|
489
|
+
: "changed by someone else";
|
|
490
|
+
const theirs = conflict.actual === TOMBSTONE_VERSION
|
|
491
|
+
? "theirs deletes it"
|
|
492
|
+
: conflict.theirs === null
|
|
493
|
+
? "the draft no longer holds it (production's value applies)"
|
|
494
|
+
: "theirs (-) against yours (+)";
|
|
495
|
+
const yours = Object.hasOwn(saved.blocks, name) ? saved.blocks[name] : undefined;
|
|
496
|
+
const body = conflict.theirs === null
|
|
497
|
+
? ""
|
|
498
|
+
: formatBlockDiff(conflict.theirs, yours, { color: reporter.color });
|
|
499
|
+
reporter.error(`${c.bold(`conflict: ${name}`)}: ${who}; ${theirs}${body ? `\n${body}` : ""}`);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
// ---------------------------------------------------------------------------
|
|
503
|
+
// publish
|
|
504
|
+
// ---------------------------------------------------------------------------
|
|
505
|
+
export async function publish(ctx, options) {
|
|
506
|
+
const { studio, delivery } = services(ctx);
|
|
507
|
+
const resolved = await studio.resolveDraft(options.draft);
|
|
508
|
+
// The draft's files against those names' production bodies: nothing else is read.
|
|
509
|
+
const { revision, before, body } = await readOverlay(delivery, resolved);
|
|
510
|
+
const after = layer(before, body);
|
|
511
|
+
const rows = await compare(before, after);
|
|
512
|
+
for (const row of rows) {
|
|
513
|
+
printChange(ctx.reporter, row.name, row.status, before[row.name], after[row.name]);
|
|
514
|
+
}
|
|
515
|
+
ctx.reporter.info(`draft ${options.draft} changes ${plural(rows.length, "block")} of ${describeSource({ draft: null, revision })}`);
|
|
516
|
+
const result = await studio.publish(options.draft, options.message);
|
|
517
|
+
if (result.result === "published") {
|
|
518
|
+
ctx.reporter.info(`published revision ${result.revision}; it is live`);
|
|
519
|
+
}
|
|
520
|
+
else {
|
|
521
|
+
ctx.reporter.info(`nothing to publish: production already has every block of draft ${options.draft}`);
|
|
522
|
+
}
|
|
523
|
+
if (!result.draftEmptied && result.result === "published") {
|
|
524
|
+
ctx.reporter.warn(`draft ${options.draft} was saved during the publish, so it wasn't emptied: it still holds its blocks`);
|
|
525
|
+
}
|
|
526
|
+
return ok({ draft: options.draft, ...result, changes: rows });
|
|
527
|
+
}
|
|
528
|
+
// ---------------------------------------------------------------------------
|
|
529
|
+
// drafts, releases
|
|
530
|
+
// ---------------------------------------------------------------------------
|
|
531
|
+
export async function drafts(ctx, options) {
|
|
532
|
+
const { studio } = services(ctx);
|
|
533
|
+
const need = () => {
|
|
534
|
+
if (options.name === null)
|
|
535
|
+
throw usageError(`deco drafts ${options.action} needs a draft name`);
|
|
536
|
+
return options.name;
|
|
537
|
+
};
|
|
538
|
+
switch (options.action) {
|
|
539
|
+
case "list": {
|
|
540
|
+
if (options.name !== null)
|
|
541
|
+
throw usageError("deco drafts list takes no name");
|
|
542
|
+
const rows = await studio.listDrafts();
|
|
543
|
+
if (rows.length === 0)
|
|
544
|
+
ctx.reporter.info("no drafts");
|
|
545
|
+
for (const row of rows) {
|
|
546
|
+
ctx.reporter.info(`${row.name} ${row.createdBy} via ${row.via}, ${row.createdAt}`);
|
|
547
|
+
}
|
|
548
|
+
return ok({ drafts: rows });
|
|
549
|
+
}
|
|
550
|
+
case "new": {
|
|
551
|
+
const draft = await studio.ensureDraft(need());
|
|
552
|
+
ctx.reporter.info(draft.created ? `created draft ${draft.name}` : `draft ${draft.name} already exists`);
|
|
553
|
+
if (draft.previewUrl)
|
|
554
|
+
ctx.reporter.info(`preview: ${draft.previewUrl}`);
|
|
555
|
+
return ok({ draft });
|
|
556
|
+
}
|
|
557
|
+
case "discard": {
|
|
558
|
+
const name = need();
|
|
559
|
+
await studio.discardDraft(name);
|
|
560
|
+
ctx.reporter.info(`discarded draft ${name}`);
|
|
561
|
+
return ok({ draft: name, discarded: true });
|
|
562
|
+
}
|
|
563
|
+
case "url": {
|
|
564
|
+
const draft = await studio.resolveDraft(need());
|
|
565
|
+
if (draft.previewUrl)
|
|
566
|
+
ctx.reporter.info(draft.previewUrl);
|
|
567
|
+
else if (draft.pointer)
|
|
568
|
+
ctx.reporter.info(`${draft.pointer}\n(the site has no preview address in Studio: open the site with ?__draft=<this pointer>)`);
|
|
569
|
+
else
|
|
570
|
+
ctx.reporter.info(`draft ${draft.name} has no object yet: push to it first`);
|
|
571
|
+
return ok({ draft: draft.name, previewUrl: draft.previewUrl, pointer: draft.pointer });
|
|
572
|
+
}
|
|
573
|
+
default:
|
|
574
|
+
throw usageError(`deco drafts needs list, new <name>, discard <name> or url <name>${options.action ? `, not "${options.action}"` : ""}`);
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
export async function releases(ctx, options) {
|
|
578
|
+
const { studio } = services(ctx);
|
|
579
|
+
const page = await studio.releases(options.cursor);
|
|
580
|
+
const c = paint(ctx.reporter.color);
|
|
581
|
+
if (page.revisions.length === 0)
|
|
582
|
+
ctx.reporter.info("no releases yet");
|
|
583
|
+
for (const row of page.revisions) {
|
|
584
|
+
// Its meta.json couldn't be read: the id is all there is.
|
|
585
|
+
if (row.status === "unreadable") {
|
|
586
|
+
ctx.reporter.info(`${row.id} ${c.yellow("unreadable")} (its meta.json couldn't be read)`);
|
|
587
|
+
continue;
|
|
588
|
+
}
|
|
589
|
+
const meta = row.meta ?? {};
|
|
590
|
+
const what = typeof meta.reapplyOf === "string"
|
|
591
|
+
? `rollback to ${meta.reapplyOf}`
|
|
592
|
+
: typeof meta.draft === "string"
|
|
593
|
+
? `draft ${meta.draft}`
|
|
594
|
+
: "";
|
|
595
|
+
ctx.reporter.info(`${row.id} ${row.status === "current" ? `${c.green("current")} ` : ""}${meta.publishedAt ?? ""} ${meta.author ?? ""} via ${meta.via ?? "?"}` +
|
|
596
|
+
`${what ? ` ${what}` : ""}${meta.message ? ` ${JSON.stringify(meta.message)}` : ""}`);
|
|
597
|
+
}
|
|
598
|
+
if (page.nextCursor)
|
|
599
|
+
ctx.reporter.info(`more: deco releases --cursor=${page.nextCursor}`);
|
|
600
|
+
return ok({ ...page });
|
|
601
|
+
}
|
|
602
|
+
// ---------------------------------------------------------------------------
|
|
603
|
+
// check's remote sources, schema push
|
|
604
|
+
// ---------------------------------------------------------------------------
|
|
605
|
+
/** The blocks `deco check --draft` or `--revision` validates. */
|
|
606
|
+
export async function remoteBlocks(ctx, options) {
|
|
607
|
+
const { studio, delivery } = services(ctx);
|
|
608
|
+
if (options.revision !== null) {
|
|
609
|
+
if (!isRevisionId(options.revision)) {
|
|
610
|
+
throw usageError(`"${options.revision}" isn't a revision id (deco releases lists them)`);
|
|
611
|
+
}
|
|
612
|
+
const blocks = await delivery.revision(options.revision);
|
|
613
|
+
if (blocks === null) {
|
|
614
|
+
throw new CliError(`no revision ${options.revision} on the CDN`, {
|
|
615
|
+
code: "revision_not_found",
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
return { label: `revision ${options.revision}`, blocks };
|
|
619
|
+
}
|
|
620
|
+
const source = await readSource(studio, delivery, options.draft);
|
|
621
|
+
return { label: describeSource(source), blocks: toMap(source.blocks) };
|
|
622
|
+
}
|
|
623
|
+
export async function schemaPush(ctx) {
|
|
624
|
+
const { studio } = services(ctx);
|
|
625
|
+
let text;
|
|
626
|
+
try {
|
|
627
|
+
text = fs.readFileSync(ctx.paths.schema, "utf8");
|
|
628
|
+
}
|
|
629
|
+
catch {
|
|
630
|
+
throw new CliError("no .deco/schema.gen.json; run deco schema first", { code: "no_schema" });
|
|
631
|
+
}
|
|
632
|
+
try {
|
|
633
|
+
JSON.parse(text);
|
|
634
|
+
}
|
|
635
|
+
catch (error) {
|
|
636
|
+
throw new CliError(`.deco/schema.gen.json: invalid JSON: ${error.message}`, {
|
|
637
|
+
code: "invalid_schema",
|
|
638
|
+
});
|
|
639
|
+
}
|
|
640
|
+
const result = await studio.pushSchema(text);
|
|
641
|
+
ctx.reporter.info(`pushed .deco/schema.gen.json (${result.blocksMajor === 8 ? "a v8 schema" : "not a v8 schema"})`);
|
|
642
|
+
return ok({ etag: result.etag, blocksMajor: result.blocksMajor });
|
|
643
|
+
}
|
|
644
|
+
//# sourceMappingURL=commands.js.map
|