@decocms/blocks 8.1.0-next.6 → 8.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/v8/cli/check/index.d.ts +6 -2
- package/dist/v8/cli/check/index.d.ts.map +1 -1
- package/dist/v8/cli/check/index.js +34 -7
- package/dist/v8/cli/check/index.js.map +1 -1
- package/dist/v8/cli/color.d.ts +14 -0
- package/dist/v8/cli/color.d.ts.map +1 -0
- package/dist/v8/cli/color.js +14 -0
- package/dist/v8/cli/color.js.map +1 -0
- package/dist/v8/cli/content.d.ts +24 -3
- package/dist/v8/cli/content.d.ts.map +1 -1
- package/dist/v8/cli/content.js +70 -5
- package/dist/v8/cli/content.js.map +1 -1
- package/dist/v8/cli/log.d.ts +4 -0
- package/dist/v8/cli/log.d.ts.map +1 -1
- package/dist/v8/cli/log.js +9 -0
- package/dist/v8/cli/log.js.map +1 -1
- package/dist/v8/cli/run.d.ts +1 -0
- package/dist/v8/cli/run.d.ts.map +1 -1
- package/dist/v8/cli/run.js +18 -4
- package/dist/v8/cli/run.js.map +1 -1
- package/dist/v8/cli/schema/index.d.ts.map +1 -1
- package/dist/v8/cli/schema/index.js +5 -3
- package/dist/v8/cli/schema/index.js.map +1 -1
- package/dist/v8/cli/schema/tsProgram.d.ts +1 -0
- package/dist/v8/cli/schema/tsProgram.d.ts.map +1 -1
- package/dist/v8/cli/schema/tsProgram.js +1 -0
- package/dist/v8/cli/schema/tsProgram.js.map +1 -1
- package/dist/v8/cli/schema/typeToSchema.d.ts.map +1 -1
- package/dist/v8/cli/schema/typeToSchema.js +50 -7
- package/dist/v8/cli/schema/typeToSchema.js.map +1 -1
- package/dist/v8/cms.d.ts.map +1 -1
- package/dist/v8/cms.js +16 -19
- package/dist/v8/cms.js.map +1 -1
- package/dist/v8/content.d.ts +4 -5
- package/dist/v8/content.d.ts.map +1 -1
- package/dist/v8/content.js +46 -44
- package/dist/v8/content.js.map +1 -1
- package/dist/v8/draftChanges.d.ts +37 -14
- package/dist/v8/draftChanges.d.ts.map +1 -1
- package/dist/v8/draftChanges.js +66 -44
- package/dist/v8/draftChanges.js.map +1 -1
- package/dist/v8/identity.d.ts +0 -2
- package/dist/v8/identity.d.ts.map +1 -1
- package/dist/v8/identity.js +0 -10
- package/dist/v8/identity.js.map +1 -1
- package/dist/v8/matchRoute.d.ts +12 -10
- package/dist/v8/matchRoute.d.ts.map +1 -1
- package/dist/v8/matchRoute.js +60 -86
- package/dist/v8/matchRoute.js.map +1 -1
- package/dist/v8/remoteLoader.d.ts +4 -4
- package/dist/v8/remoteLoader.d.ts.map +1 -1
- package/dist/v8/remoteLoader.js +106 -96
- package/dist/v8/remoteLoader.js.map +1 -1
- package/dist/v8/routeTrie.d.ts +43 -0
- package/dist/v8/routeTrie.d.ts.map +1 -0
- package/dist/v8/routeTrie.js +88 -0
- package/dist/v8/routeTrie.js.map +1 -0
- package/dist/v8/telemetry.d.ts +7 -7
- package/dist/v8/telemetry.d.ts.map +1 -1
- package/dist/v8/telemetry.js +24 -100
- package/dist/v8/telemetry.js.map +1 -1
- package/dist/v8/types.d.ts +59 -17
- package/dist/v8/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/v8/cli/check/index.ts +32 -7
- package/src/v8/cli/color.ts +29 -0
- package/src/v8/cli/content.ts +85 -5
- package/src/v8/cli/log.ts +12 -0
- package/src/v8/cli/run.ts +19 -4
- package/src/v8/cli/schema/index.ts +5 -3
- package/src/v8/cli/schema/tsProgram.ts +2 -1
- package/src/v8/cli/schema/typeToSchema.ts +47 -7
- package/src/v8/cms.ts +17 -20
- package/src/v8/content.ts +54 -46
- package/src/v8/draftChanges.ts +79 -43
- package/src/v8/identity.ts +0 -10
- package/src/v8/matchRoute.ts +70 -106
- package/src/v8/remoteLoader.ts +112 -111
- package/src/v8/routeTrie.ts +103 -0
- package/src/v8/telemetry.ts +27 -108
- package/src/v8/types.ts +60 -15
package/src/v8/cli/content.ts
CHANGED
|
@@ -3,9 +3,10 @@
|
|
|
3
3
|
* module, `.deco/blocks.gen.ts` (spec: content › The content module). It never
|
|
4
4
|
* reads the block map.
|
|
5
5
|
*/
|
|
6
|
+
import { execFileSync } from "node:child_process";
|
|
6
7
|
import fs from "node:fs";
|
|
7
8
|
import path from "node:path";
|
|
8
|
-
import { computeContentRevision } from "../../protocol/canonical.ts";
|
|
9
|
+
import { canonicalJson, computeContentRevision, sha256Hex } from "../../protocol/canonical.ts";
|
|
9
10
|
import {
|
|
10
11
|
blockNameFromFile,
|
|
11
12
|
checkBlockName,
|
|
@@ -154,7 +155,8 @@ function inlinedFiles(saved: SavedBlocks): string[] {
|
|
|
154
155
|
/**
|
|
155
156
|
* The `.deco` folder's identity for `createCMS` (one instance per folder, so
|
|
156
157
|
* a hot-reloaded module keeps its instance): its path from the repository
|
|
157
|
-
* root, or `.deco` outside a repository. Relative, so
|
|
158
|
+
* root, or `.deco` outside a repository. Relative, so it doesn't depend on
|
|
159
|
+
* where the repository is checked out.
|
|
158
160
|
*/
|
|
159
161
|
export function contentRoot(deco: string): string {
|
|
160
162
|
for (let dir = path.dirname(deco); ; dir = path.dirname(dir)) {
|
|
@@ -165,8 +167,66 @@ export function contentRoot(deco: string): string {
|
|
|
165
167
|
}
|
|
166
168
|
}
|
|
167
169
|
|
|
168
|
-
/**
|
|
169
|
-
|
|
170
|
+
/**
|
|
171
|
+
* The `schemaHash` of a `.deco/schema.gen.json`: the SHA-256 of its parsed
|
|
172
|
+
* JSON in canonical form (`sha256Hex(canonicalJson(JSON.parse(text)))`, the
|
|
173
|
+
* `@decocms/blocks/protocol` helpers), so Studio computes the same value
|
|
174
|
+
* from the file at a commit. Hosted releases swap only when it matches.
|
|
175
|
+
*/
|
|
176
|
+
async function computeSchemaHash(text: string): Promise<string> {
|
|
177
|
+
return sha256Hex(canonicalJson(JSON.parse(text)));
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** `schema.gen.json`'s hash, or `undefined` when there's no such file. */
|
|
181
|
+
async function readSchemaHash(schemaFile: string): Promise<string | undefined> {
|
|
182
|
+
let text: string;
|
|
183
|
+
try {
|
|
184
|
+
text = fs.readFileSync(schemaFile, "utf8");
|
|
185
|
+
} catch (error) {
|
|
186
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return undefined;
|
|
187
|
+
throw error;
|
|
188
|
+
}
|
|
189
|
+
try {
|
|
190
|
+
return await computeSchemaHash(text);
|
|
191
|
+
} catch (error) {
|
|
192
|
+
throw new CliError(
|
|
193
|
+
`.deco/schema.gen.json: not valid JSON (${(error as Error).message}); run deco schema`,
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* The commit time of git HEAD in `dir` (committer date, ISO 8601), or
|
|
200
|
+
* undefined when git is missing, `dir` isn't in a repository, or it has no
|
|
201
|
+
* commits. Commit time, not build time: a slow build of an older commit that
|
|
202
|
+
* finishes after a publish must not win over it.
|
|
203
|
+
*/
|
|
204
|
+
export function readCommittedAt(dir: string): string | undefined {
|
|
205
|
+
try {
|
|
206
|
+
const out = execFileSync("git", ["log", "-1", "--format=%cI"], {
|
|
207
|
+
cwd: dir,
|
|
208
|
+
encoding: "utf8",
|
|
209
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
210
|
+
}).trim();
|
|
211
|
+
return out === "" || Number.isNaN(Date.parse(out)) ? undefined : out;
|
|
212
|
+
} catch {
|
|
213
|
+
return undefined;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* The source of `.deco/blocks.gen.ts` for a set of saved blocks, with the
|
|
219
|
+
* `schemaHash` of the schema it was built with when there is one, and
|
|
220
|
+
* `committedAt`: the commit time of git HEAD it was built from (ISO 8601),
|
|
221
|
+
* when there is one. A hosted release replaces the bundled content only when
|
|
222
|
+
* it was published later (see ../remoteLoader.ts).
|
|
223
|
+
*/
|
|
224
|
+
export async function renderContentModule(
|
|
225
|
+
saved: SavedBlocks,
|
|
226
|
+
root = ".deco",
|
|
227
|
+
schemaHash?: string,
|
|
228
|
+
committedAt?: string,
|
|
229
|
+
): Promise<string> {
|
|
170
230
|
const names = Object.keys(saved.blocks).sort();
|
|
171
231
|
const revision = await computeContentRevision(saved.blocks);
|
|
172
232
|
const taken = new Set<string>();
|
|
@@ -194,11 +254,15 @@ export async function renderContentModule(saved: SavedBlocks, root = ".deco"): P
|
|
|
194
254
|
"",
|
|
195
255
|
"const content: {",
|
|
196
256
|
" revision: string;",
|
|
257
|
+
...(schemaHash === undefined ? [] : [" schemaHash: string;"]),
|
|
258
|
+
...(committedAt === undefined ? [] : [" committedAt: string;"]),
|
|
197
259
|
" blocks: Record<string, unknown>;",
|
|
198
260
|
" aliases: Record<string, string>;",
|
|
199
261
|
" root: string;",
|
|
200
262
|
"} = {",
|
|
201
263
|
` revision: ${JSON.stringify(revision)},`,
|
|
264
|
+
...(schemaHash === undefined ? [] : [` schemaHash: ${JSON.stringify(schemaHash)},`]),
|
|
265
|
+
...(committedAt === undefined ? [] : [` committedAt: ${JSON.stringify(committedAt)},`]),
|
|
202
266
|
" blocks: {",
|
|
203
267
|
...entries,
|
|
204
268
|
" },",
|
|
@@ -217,7 +281,14 @@ export interface ContentResult {
|
|
|
217
281
|
root: string;
|
|
218
282
|
file: string;
|
|
219
283
|
revision: string;
|
|
284
|
+
/** The hash of `.deco/schema.gen.json`, written into the module; absent without one. */
|
|
285
|
+
schemaHash?: string;
|
|
220
286
|
count: number;
|
|
287
|
+
/**
|
|
288
|
+
* The commit time of git HEAD in the root, written into the module as
|
|
289
|
+
* `committedAt`; absent when git isn't available there.
|
|
290
|
+
*/
|
|
291
|
+
committedAt?: string;
|
|
221
292
|
/** False when the file already had this content. */
|
|
222
293
|
changed: boolean;
|
|
223
294
|
/** Files written into the module instead of imported (see `inlinedFiles`). */
|
|
@@ -246,12 +317,16 @@ export async function writeContent(paths: DecoPaths): Promise<ContentResult> {
|
|
|
246
317
|
if (errors.length > 0) {
|
|
247
318
|
throw new CliError(errors.map((d) => `.deco/blocks/${d.file}: ${d.message}`).join("\n"));
|
|
248
319
|
}
|
|
249
|
-
const
|
|
320
|
+
const schemaHash = await readSchemaHash(paths.schema);
|
|
321
|
+
const committedAt = readCommittedAt(paths.root);
|
|
322
|
+
const source = await renderContentModule(saved, contentRoot(paths.deco), schemaHash, committedAt);
|
|
250
323
|
const changed = writeIfChanged(paths.content, source);
|
|
251
324
|
return {
|
|
252
325
|
root: paths.root,
|
|
253
326
|
file: paths.content,
|
|
254
327
|
revision: await computeContentRevision(saved.blocks),
|
|
328
|
+
...(schemaHash === undefined ? {} : { schemaHash }),
|
|
329
|
+
...(committedAt === undefined ? {} : { committedAt }),
|
|
255
330
|
count: Object.keys(saved.blocks).length,
|
|
256
331
|
changed,
|
|
257
332
|
inlined: inlinedFiles(saved),
|
|
@@ -273,6 +348,11 @@ export async function content(options: ContentOptions = {}): Promise<number> {
|
|
|
273
348
|
const paths = decoPaths(findDecoRoot(options));
|
|
274
349
|
const result = await writeContent(paths);
|
|
275
350
|
for (const d of result.diagnostics) reporter.warn(`.deco/blocks/${d.file}: ${d.message}`);
|
|
351
|
+
if (result.committedAt === undefined) {
|
|
352
|
+
reporter.info(
|
|
353
|
+
"no git commit found, so .deco/blocks.gen.ts has no committedAt: hosted releases with the same schema will replace it",
|
|
354
|
+
);
|
|
355
|
+
}
|
|
276
356
|
reporter.info(
|
|
277
357
|
`${result.changed ? "wrote" : "unchanged"} .deco/blocks.gen.ts (${result.count} blocks, revision ${result.revision.slice(0, 12)})`,
|
|
278
358
|
);
|
package/src/v8/cli/log.ts
CHANGED
|
@@ -3,6 +3,8 @@ export interface Reporter {
|
|
|
3
3
|
info(message: string): void;
|
|
4
4
|
warn(message: string): void;
|
|
5
5
|
error(message: string): void;
|
|
6
|
+
/** Whether messages may carry ANSI colour (`--color`, or a terminal). Off when absent. */
|
|
7
|
+
color?: boolean;
|
|
6
8
|
}
|
|
7
9
|
|
|
8
10
|
export const consoleReporter: Reporter = {
|
|
@@ -11,6 +13,16 @@ export const consoleReporter: Reporter = {
|
|
|
11
13
|
error: (message) => console.error(message),
|
|
12
14
|
};
|
|
13
15
|
|
|
16
|
+
/** The same reporter, with colour turned on or off. */
|
|
17
|
+
export function withColor(reporter: Reporter, color: boolean): Reporter {
|
|
18
|
+
return {
|
|
19
|
+
info: (message) => reporter.info(message),
|
|
20
|
+
warn: (message) => reporter.warn(message),
|
|
21
|
+
error: (message) => reporter.error(message),
|
|
22
|
+
color,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
14
26
|
export const silentReporter: Reporter = {
|
|
15
27
|
info: () => {},
|
|
16
28
|
warn: () => {},
|
package/src/v8/cli/run.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import { check } from "./check/index.ts";
|
|
6
6
|
import { content, writeContent } from "./content.ts";
|
|
7
|
-
import { consoleReporter, type Reporter } from "./log.ts";
|
|
7
|
+
import { consoleReporter, type Reporter, withColor } from "./log.ts";
|
|
8
8
|
import { CliError, decoPaths, findDecoRoot } from "./root.ts";
|
|
9
9
|
import { reportSchema, schema, writeSchema } from "./schema/index.ts";
|
|
10
10
|
import { serve, startServer } from "./serve/server.ts";
|
|
@@ -48,7 +48,8 @@ ${Object.values(COMMANDS)
|
|
|
48
48
|
.map((c) => ` ${c.usage}`)
|
|
49
49
|
.join("\n")}
|
|
50
50
|
|
|
51
|
-
Every command finds .deco/ by walking up from the current folder, or takes --root
|
|
51
|
+
Every command finds .deco/ by walking up from the current folder, or takes --root.
|
|
52
|
+
Every command takes --color or --no-color; without either, output is coloured only on a terminal.`;
|
|
52
53
|
|
|
53
54
|
export type ParsedFlags = Record<string, string | boolean | string[]>;
|
|
54
55
|
|
|
@@ -59,8 +60,14 @@ export function parseFlags(command: string, args: string[]): ParsedFlags {
|
|
|
59
60
|
const arg = args[i];
|
|
60
61
|
if (!arg.startsWith("--"))
|
|
61
62
|
throw new CliError(`unexpected argument "${arg}"\n${COMMANDS[command].usage}`);
|
|
63
|
+
// Every command's: `color` is true, false or absent; the last one given wins.
|
|
64
|
+
if (arg === "--color" || arg === "--no-color") {
|
|
65
|
+
flags.color = arg === "--color";
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
62
68
|
const eq = arg.indexOf("=");
|
|
63
69
|
const name = arg.slice(2, eq === -1 ? undefined : eq);
|
|
70
|
+
if (name === "color" || name === "no-color") throw new CliError(`--${name} takes no value`);
|
|
64
71
|
const kind = Object.hasOwn(spec, name) ? spec[name] : undefined;
|
|
65
72
|
if (!kind) throw new CliError(`unknown flag --${name}\n${COMMANDS[command].usage}`);
|
|
66
73
|
if (kind === "boolean") {
|
|
@@ -83,6 +90,7 @@ export function parseFlags(command: string, args: string[]): ParsedFlags {
|
|
|
83
90
|
|
|
84
91
|
export interface RunOptions {
|
|
85
92
|
cwd?: string;
|
|
93
|
+
/** Defaults to the console, coloured when stdout and stderr are both terminals. */
|
|
86
94
|
reporter?: Reporter;
|
|
87
95
|
/** Resolves when a `--watch` or `serve` run should stop (tests); defaults to SIGINT/SIGTERM. */
|
|
88
96
|
until?: Promise<void>;
|
|
@@ -124,11 +132,11 @@ async function watchLoop(
|
|
|
124
132
|
*/
|
|
125
133
|
const PUBLISH_SIGNPOST = `There is no publish command: Git is the source of truth for content, so publishing is committing.
|
|
126
134
|
Commit the changes in .deco/blocks (and push them). A deploy ships the commit; with the hosted
|
|
127
|
-
Deco CMS,
|
|
135
|
+
Deco CMS, Studio's Publish (or Resync) makes the commit a release without a deploy.`;
|
|
128
136
|
|
|
129
137
|
/** Run one `deco` invocation; returns the exit code. */
|
|
130
138
|
export async function runCli(argv: string[], options: RunOptions = {}): Promise<number> {
|
|
131
|
-
|
|
139
|
+
let reporter = options.reporter ?? consoleReporter;
|
|
132
140
|
const [command, ...rest] = argv;
|
|
133
141
|
if (!command || command === "help" || command === "--help" || command === "-h") {
|
|
134
142
|
reporter.info(USAGE);
|
|
@@ -145,6 +153,13 @@ export async function runCli(argv: string[], options: RunOptions = {}): Promise<
|
|
|
145
153
|
}
|
|
146
154
|
try {
|
|
147
155
|
const flags = parseFlags(command, rest);
|
|
156
|
+
const color =
|
|
157
|
+
typeof flags.color === "boolean"
|
|
158
|
+
? flags.color
|
|
159
|
+
: options.reporter
|
|
160
|
+
? Boolean(options.reporter.color)
|
|
161
|
+
: Boolean(process.stdout.isTTY && process.stderr.isTTY);
|
|
162
|
+
reporter = withColor(reporter, color);
|
|
148
163
|
const root = flags.root as string | undefined;
|
|
149
164
|
const base = { root, cwd: options.cwd, reporter };
|
|
150
165
|
switch (command) {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import path from "node:path";
|
|
2
|
+
import { paint } from "../color.ts";
|
|
2
3
|
import { writeIfChanged } from "../content.ts";
|
|
3
4
|
import { consoleReporter, type Reporter } from "../log.ts";
|
|
4
5
|
import { CliError, type DecoPaths, decoPaths, findDecoRoot } from "../root.ts";
|
|
@@ -29,12 +30,13 @@ export async function writeSchema(paths: DecoPaths): Promise<WriteSchemaResult>
|
|
|
29
30
|
}
|
|
30
31
|
|
|
31
32
|
export function reportSchema(result: WriteSchemaResult, reporter: Reporter): number {
|
|
33
|
+
const c = paint(reporter.color);
|
|
32
34
|
for (const d of result.diagnostics) {
|
|
33
|
-
if (d.severity === "error") reporter.error(d.message);
|
|
34
|
-
else reporter.warn(
|
|
35
|
+
if (d.severity === "error") reporter.error(c.red(d.message));
|
|
36
|
+
else reporter.warn(`${c.yellow("warning")}: ${d.message}`);
|
|
35
37
|
}
|
|
36
38
|
if (!result.written) {
|
|
37
|
-
reporter.error(".deco/schema.gen.json not written: fix the errors above");
|
|
39
|
+
reporter.error(c.red(".deco/schema.gen.json not written: fix the errors above"));
|
|
38
40
|
return 1;
|
|
39
41
|
}
|
|
40
42
|
const groups = result.meta.manifest.blocks;
|
|
@@ -293,7 +293,7 @@ export class TsSignature {
|
|
|
293
293
|
/** One JSDoc comment: its description and its tags. */
|
|
294
294
|
export interface JsDocView {
|
|
295
295
|
getDescription(): string;
|
|
296
|
-
getTags(): { getTagName(): string; getCommentText(): string | undefined }[];
|
|
296
|
+
getTags(): { getTagName(): string; getCommentText(): string | undefined; getText(): string }[];
|
|
297
297
|
}
|
|
298
298
|
|
|
299
299
|
export class TsNode {
|
|
@@ -458,6 +458,7 @@ function jsDocView(ts: TypeScript, doc: TS.JSDoc): JsDocView {
|
|
|
458
458
|
return (doc.tags ?? []).map((tag) => ({
|
|
459
459
|
getTagName: () => tag.tagName.getText(sourceFile),
|
|
460
460
|
getCommentText: () => commentText(tag.comment),
|
|
461
|
+
getText: () => tag.getText(sourceFile),
|
|
461
462
|
}));
|
|
462
463
|
},
|
|
463
464
|
};
|
|
@@ -81,6 +81,11 @@ export function getJsDocTags(symbol: MorphSymbol): Record<string, string> {
|
|
|
81
81
|
const desc = doc.getDescription().trim();
|
|
82
82
|
if (desc) tags.description = desc;
|
|
83
83
|
for (const tag of doc.getTags()) {
|
|
84
|
+
// deco-cx/deco reads tags with /@(\w+)\s+/, so `@description: text` was
|
|
85
|
+
// never a description there and the editor did not show it. The TS
|
|
86
|
+
// parser splits it into tag `description` + comment `: text`; skip it
|
|
87
|
+
// rather than emit a description that starts with ": ".
|
|
88
|
+
if (/^@\w+[^\w\s]/.test(tag.getText())) continue;
|
|
84
89
|
tags[tag.getTagName()] = tag.getCommentText()?.trim() || "true";
|
|
85
90
|
}
|
|
86
91
|
}
|
|
@@ -543,13 +548,39 @@ function isOpaqueSectionType(propType: Type): boolean {
|
|
|
543
548
|
return type.isAny() || type.isUnknown();
|
|
544
549
|
}
|
|
545
550
|
|
|
551
|
+
/**
|
|
552
|
+
* The schema of a type met again while it is still being expanded (a
|
|
553
|
+
* recursive type, `interface Link { submenu?: Link[] }`): a `$ref` to the
|
|
554
|
+
* type's shared definition, looking through the array / `| null` wrapper the
|
|
555
|
+
* cycle was met on, so `submenu` is `{ type: "array", items: { $ref } }`.
|
|
556
|
+
* Null when there is nothing to reference (anonymous or generic types).
|
|
557
|
+
*/
|
|
558
|
+
function recursiveRef(type: Type, ctx?: SchemaContext): any | null {
|
|
559
|
+
if (!ctx) return null;
|
|
560
|
+
if (type.isUnion()) {
|
|
561
|
+
const parts = type.getUnionTypes();
|
|
562
|
+
const nonNull = parts.filter((t) => !t.isNull() && !t.isUndefined());
|
|
563
|
+
if (nonNull.length !== 1) return null;
|
|
564
|
+
const inner = recursiveRef(nonNull[0], ctx);
|
|
565
|
+
return inner && nonNull.length < parts.length ? { ...inner, nullable: true } : inner;
|
|
566
|
+
}
|
|
567
|
+
if (type.isArray()) {
|
|
568
|
+
const element = type.getArrayElementType();
|
|
569
|
+
const items = element ? recursiveRef(element, ctx) : null;
|
|
570
|
+
return items ? { type: "array", items } : null;
|
|
571
|
+
}
|
|
572
|
+
return ctx.namedDefinition(type);
|
|
573
|
+
}
|
|
574
|
+
|
|
546
575
|
export function typeToJsonSchema(
|
|
547
576
|
type: Type,
|
|
548
577
|
visited = new Set<string>(),
|
|
549
578
|
ctx?: SchemaContext,
|
|
550
579
|
): any {
|
|
551
580
|
const typeText = type.getText();
|
|
552
|
-
|
|
581
|
+
// A cycle: reference the type's shared definition instead of collapsing it
|
|
582
|
+
// to a bare object, as deco-cx/deco does.
|
|
583
|
+
if (visited.has(typeText)) return recursiveRef(type, ctx) ?? { type: "object" };
|
|
553
584
|
visited.add(typeText);
|
|
554
585
|
|
|
555
586
|
try {
|
|
@@ -571,9 +602,12 @@ export function typeToJsonSchema(
|
|
|
571
602
|
if (isSectionType(type)) return sectionRef();
|
|
572
603
|
|
|
573
604
|
if (type.isString() || type.isStringLiteral()) {
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
605
|
+
if (!type.isStringLiteral()) return { type: "string" };
|
|
606
|
+
// deco-cx/deco pairs every `const` with an equal `default`, so the editor
|
|
607
|
+
// writes the literal into new entries, e.g. a hidden `__type: "image"`
|
|
608
|
+
// discriminator the component branches on.
|
|
609
|
+
const value = type.getLiteralValue();
|
|
610
|
+
return { type: "string", const: value, default: value };
|
|
577
611
|
}
|
|
578
612
|
if (type.isNumber() || type.isNumberLiteral()) return { type: "number" };
|
|
579
613
|
if (type.isBoolean() || type.isBooleanLiteral()) return { type: "boolean" };
|
|
@@ -682,7 +716,10 @@ export function typeToJsonSchema(
|
|
|
682
716
|
|
|
683
717
|
for (const prop of type.getProperties()) {
|
|
684
718
|
const name = prop.getName();
|
|
685
|
-
|
|
719
|
+
// `_`-prefixed props are site data (`__title` labels a list entry in the
|
|
720
|
+
// editor, `_id` keys a dynamic-options picker) and deco-cx/deco keeps
|
|
721
|
+
// them; only the block-reference discriminator is framework-owned.
|
|
722
|
+
if (name === "__resolveType" || name.startsWith("$") || name === "@type") continue;
|
|
686
723
|
if (REACT_INTERNAL_PROPS.has(name)) continue;
|
|
687
724
|
|
|
688
725
|
const decl = prop.getValueDeclaration() ?? prop.getDeclarations()[0];
|
|
@@ -732,8 +769,11 @@ export function typeToJsonSchema(
|
|
|
732
769
|
schema.title ??= titleCase(name);
|
|
733
770
|
properties[name] = schema;
|
|
734
771
|
// A hidden prop can't be filled in by an editor; requiring it would
|
|
735
|
-
// deadlock the form.
|
|
736
|
-
|
|
772
|
+
// deadlock the form. Unless it has a default the editor writes for
|
|
773
|
+
// the user (a hidden literal such as `__title: "…"`), which
|
|
774
|
+
// deco-cx/deco keeps required.
|
|
775
|
+
const fillable = schema.hide !== "true" || "default" in schema;
|
|
776
|
+
if (!optional && fillable) required.push(name);
|
|
737
777
|
}
|
|
738
778
|
|
|
739
779
|
const result: any = { type: "object", properties };
|
package/src/v8/cms.ts
CHANGED
|
@@ -13,8 +13,9 @@ import { secretBlock } from "./builtins/secret.ts";
|
|
|
13
13
|
import { CMSClient } from "./client.ts";
|
|
14
14
|
import { ContentStore, isLoader, isSnapshot } from "./content.ts";
|
|
15
15
|
import { draftCookieFor, endsPreview, parseDraftPointer, readDraftPointer } from "./draft.ts";
|
|
16
|
+
import { parseDraftHosts } from "./draftChanges.ts";
|
|
16
17
|
import { allowsHost, type HostPattern, parseHostPattern } from "./hosts.ts";
|
|
17
|
-
import { clearGlobals, contentIdentity, fnv1a
|
|
18
|
+
import { clearGlobals, contentIdentity, fnv1a } from "./identity.ts";
|
|
18
19
|
import { isPlainObject } from "./json.ts";
|
|
19
20
|
import { remoteLoader, resetRemoteLoaders } from "./remoteLoader.ts";
|
|
20
21
|
import {
|
|
@@ -55,6 +56,7 @@ interface Fingerprint {
|
|
|
55
56
|
telemetry: string;
|
|
56
57
|
preview: string;
|
|
57
58
|
secrets: string;
|
|
59
|
+
token: string;
|
|
58
60
|
}
|
|
59
61
|
|
|
60
62
|
/**
|
|
@@ -87,13 +89,13 @@ class CMSInstance {
|
|
|
87
89
|
constructor(config: CMSConfig, interval: number) {
|
|
88
90
|
this.config = config;
|
|
89
91
|
this.#interval = interval;
|
|
90
|
-
this.#store = new ContentStore(contentOf(config));
|
|
92
|
+
this.#store = new ContentStore(contentOf(config), parseDraftHosts(config.preview));
|
|
91
93
|
this.fingerprint = fingerprintOf(config, interval);
|
|
92
94
|
this.#caps = {
|
|
93
95
|
hosts: parseCodeHosts(config.preview),
|
|
94
96
|
limits: telemetryLimits(config.telemetry),
|
|
95
97
|
};
|
|
96
|
-
const destination = resolveDestination(config.telemetry, config.site);
|
|
98
|
+
const destination = resolveDestination(config.telemetry, config.site, config.token);
|
|
97
99
|
if (destination !== null) {
|
|
98
100
|
this.#telemetry = new TelemetryPipeline(destination);
|
|
99
101
|
setCurrentTelemetry(this.#telemetry);
|
|
@@ -153,10 +155,6 @@ class CMSInstance {
|
|
|
153
155
|
return this.#client(blocks, () => load().then((snapshot) => forceVariants(snapshot, variants)));
|
|
154
156
|
}
|
|
155
157
|
|
|
156
|
-
forRevision(blocks: Blocks, revision: string): Client {
|
|
157
|
-
return this.#client(blocks, () => this.#store.revision(revision));
|
|
158
|
-
}
|
|
159
|
-
|
|
160
158
|
update(): Promise<{ updated: boolean }> {
|
|
161
159
|
this.#nextCheck = this.#due();
|
|
162
160
|
return this.#store.update();
|
|
@@ -302,10 +300,6 @@ class CMSHandle implements CMS {
|
|
|
302
300
|
return this.#instance.forDraft(this.#blocks, pointer);
|
|
303
301
|
}
|
|
304
302
|
|
|
305
|
-
forRevision(revision: string): Client {
|
|
306
|
-
return this.#instance.forRevision(this.#blocks, revision);
|
|
307
|
-
}
|
|
308
|
-
|
|
309
303
|
update(): Promise<{ updated: boolean }> {
|
|
310
304
|
return this.#instance.update();
|
|
311
305
|
}
|
|
@@ -365,10 +359,10 @@ export function resetForTests(): void {
|
|
|
365
359
|
setCurrentTelemetry(undefined);
|
|
366
360
|
}
|
|
367
361
|
|
|
368
|
-
/** With `site
|
|
362
|
+
/** With `site`, the content is the fallback of hosted releases. */
|
|
369
363
|
function contentOf(config: CMSConfig): Snapshot | Loader {
|
|
370
|
-
if (!config.site
|
|
371
|
-
return remoteLoader(config.content, { site: config.site,
|
|
364
|
+
if (!config.site) return config.content;
|
|
365
|
+
return remoteLoader(config.content, { site: config.site, dev: config.dev });
|
|
372
366
|
}
|
|
373
367
|
|
|
374
368
|
function validate(config: CMSConfig): void {
|
|
@@ -383,12 +377,16 @@ function validate(config: CMSConfig): void {
|
|
|
383
377
|
"createCMS: `content` must be the content module ({ revision, blocks }) or a loader with load()",
|
|
384
378
|
);
|
|
385
379
|
}
|
|
380
|
+
// The token is the site's credential: it means nothing without the site it belongs to.
|
|
381
|
+
if (config.token && !config.site) {
|
|
382
|
+
throw new TypeError("createCMS: token needs site: pass both, or site alone");
|
|
383
|
+
}
|
|
386
384
|
parseCodeHosts(config.preview);
|
|
385
|
+
parseDraftHosts(config.preview);
|
|
387
386
|
}
|
|
388
387
|
|
|
389
388
|
function resolveInterval(configured: number | undefined): number {
|
|
390
|
-
const
|
|
391
|
-
const raw = configured ?? (env ? Number(env) : MIN_INTERVAL);
|
|
389
|
+
const raw = configured ?? MIN_INTERVAL;
|
|
392
390
|
if (!Number.isFinite(raw)) return MIN_INTERVAL;
|
|
393
391
|
if (raw < MIN_INTERVAL) {
|
|
394
392
|
console.warn(
|
|
@@ -401,8 +399,7 @@ function resolveInterval(configured: number | undefined): number {
|
|
|
401
399
|
|
|
402
400
|
/**
|
|
403
401
|
* The instance key: the content's identity (never its revision, so a hot
|
|
404
|
-
* reload keeps the instance) and, with
|
|
405
|
-
* hash of the token, which never appears in the global symbol registry.
|
|
402
|
+
* reload keeps the instance) and, with hosted releases, the site.
|
|
406
403
|
*
|
|
407
404
|
* A content module is identified by the `.deco` folder it was generated from
|
|
408
405
|
* (its `root`). One without a `root` is identified by the object itself, so
|
|
@@ -411,8 +408,7 @@ function resolveInterval(configured: number | undefined): number {
|
|
|
411
408
|
*/
|
|
412
409
|
function identityOf(config: CMSConfig): string {
|
|
413
410
|
const content = contentIdentity(config.content);
|
|
414
|
-
|
|
415
|
-
return `${content}|site:${config.site}|token:${fnv1a(config.token)}`;
|
|
411
|
+
return config.site ? `${content}|site:${config.site}` : content;
|
|
416
412
|
}
|
|
417
413
|
|
|
418
414
|
function fingerprintOf(config: CMSConfig, interval: number): Fingerprint {
|
|
@@ -421,6 +417,7 @@ function fingerprintOf(config: CMSConfig, interval: number): Fingerprint {
|
|
|
421
417
|
telemetry: stableJson(config.telemetry ?? null),
|
|
422
418
|
preview: stableJson(config.preview ?? null),
|
|
423
419
|
secrets: config.secrets?.key ? fnv1a(config.secrets.key) : "",
|
|
420
|
+
token: config.token ? fnv1a(config.token) : "",
|
|
424
421
|
};
|
|
425
422
|
}
|
|
426
423
|
|