@decocms/blocks-cli 7.77.0 → 7.77.2
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 +2 -2
- package/scripts/generate-schema.test.ts +168 -0
- package/scripts/generate-schema.ts +99 -14
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@decocms/blocks-cli",
|
|
3
|
-
"version": "7.77.
|
|
3
|
+
"version": "7.77.2",
|
|
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.77.
|
|
36
|
+
"@decocms/blocks": "7.77.2",
|
|
37
37
|
"ts-morph": "^27.0.0",
|
|
38
38
|
"tsx": "^4.22.5"
|
|
39
39
|
},
|
|
@@ -191,6 +191,174 @@ describe("typeToJsonSchema with intersection types", () => {
|
|
|
191
191
|
}, 30_000);
|
|
192
192
|
});
|
|
193
193
|
|
|
194
|
+
// Sites compile with `strict`, so an optional prop is `T | undefined` (nullable).
|
|
195
|
+
function strictProject() {
|
|
196
|
+
return new Project({
|
|
197
|
+
useInMemoryFileSystem: true,
|
|
198
|
+
compilerOptions: { skipLibCheck: true, strict: true },
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
describe("typeToJsonSchema underscore-prefixed props", () => {
|
|
203
|
+
it("keeps `__title` and `_id` (admin label, dynamic-options key) but not __resolveType", () => {
|
|
204
|
+
const project = strictProject();
|
|
205
|
+
const sf = project.createSourceFile(
|
|
206
|
+
"/props.ts",
|
|
207
|
+
`
|
|
208
|
+
/** @title {{__title}} */
|
|
209
|
+
export interface Rule {
|
|
210
|
+
/**
|
|
211
|
+
* @title Título (opcional)
|
|
212
|
+
* @description Título do CMS.
|
|
213
|
+
*/
|
|
214
|
+
__title?: string;
|
|
215
|
+
/**
|
|
216
|
+
* @title Regulamento
|
|
217
|
+
* @format dynamic-options
|
|
218
|
+
* @options site/loaders/admin/regulation.ts
|
|
219
|
+
*/
|
|
220
|
+
_id?: string;
|
|
221
|
+
__resolveType?: string;
|
|
222
|
+
pathname: string;
|
|
223
|
+
}
|
|
224
|
+
export interface Props { rules: Rule[] }
|
|
225
|
+
`,
|
|
226
|
+
);
|
|
227
|
+
const item = typeToJsonSchema(sf.getInterfaceOrThrow("Props").getType()).properties.rules.items;
|
|
228
|
+
expect(Object.keys(item.properties)).toEqual(["__title", "_id", "pathname"]);
|
|
229
|
+
expect(item.properties.__title).toEqual({
|
|
230
|
+
type: "string",
|
|
231
|
+
nullable: true,
|
|
232
|
+
title: "Título (opcional)",
|
|
233
|
+
description: "Título do CMS.",
|
|
234
|
+
});
|
|
235
|
+
expect(item.properties._id).toMatchObject({
|
|
236
|
+
type: "string",
|
|
237
|
+
format: "dynamic-options",
|
|
238
|
+
options: "site/loaders/admin/regulation.ts",
|
|
239
|
+
});
|
|
240
|
+
expect(item.required).toEqual(["pathname"]);
|
|
241
|
+
}, 30_000);
|
|
242
|
+
|
|
243
|
+
it("emits a hidden literal discriminator with const and an equal default", () => {
|
|
244
|
+
const project = strictProject();
|
|
245
|
+
const sf = project.createSourceFile(
|
|
246
|
+
"/props.ts",
|
|
247
|
+
`
|
|
248
|
+
export interface Props {
|
|
249
|
+
/** @hide true */
|
|
250
|
+
__type: "image";
|
|
251
|
+
src: string;
|
|
252
|
+
}
|
|
253
|
+
`,
|
|
254
|
+
);
|
|
255
|
+
const schema = typeToJsonSchema(sf.getInterfaceOrThrow("Props").getType());
|
|
256
|
+
expect(schema.properties.__type).toEqual({
|
|
257
|
+
type: "string",
|
|
258
|
+
const: "image",
|
|
259
|
+
default: "image",
|
|
260
|
+
hide: "true",
|
|
261
|
+
title: "__type",
|
|
262
|
+
});
|
|
263
|
+
// Hidden but defaulted: the admin writes the literal, so it stays required
|
|
264
|
+
// (as in deco-cx/deco).
|
|
265
|
+
expect(schema.required).toEqual(["__type", "src"]);
|
|
266
|
+
}, 30_000);
|
|
267
|
+
|
|
268
|
+
it("still leaves a hidden prop without a default out of `required`", () => {
|
|
269
|
+
const project = strictProject();
|
|
270
|
+
const sf = project.createSourceFile(
|
|
271
|
+
"/props.ts",
|
|
272
|
+
`
|
|
273
|
+
export interface Props {
|
|
274
|
+
/** @hide true */
|
|
275
|
+
internal: string;
|
|
276
|
+
src: string;
|
|
277
|
+
}
|
|
278
|
+
`,
|
|
279
|
+
);
|
|
280
|
+
const schema = typeToJsonSchema(sf.getInterfaceOrThrow("Props").getType());
|
|
281
|
+
expect(schema.properties.internal.hide).toBe("true");
|
|
282
|
+
expect(schema.required).toEqual(["src"]);
|
|
283
|
+
}, 30_000);
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
describe("typeToJsonSchema with a recursive named type", () => {
|
|
287
|
+
// A navigation tree written as `interface Link { submenu?: Link[] }`. The
|
|
288
|
+
// cycle used to collapse to `{ type: "object" }`, so the admin edited the
|
|
289
|
+
// submenu as an object instead of a list of links. deco-cx/deco emits the
|
|
290
|
+
// type once as a definition and references it from inside the cycle.
|
|
291
|
+
const source = `
|
|
292
|
+
/** @title {{name}} */
|
|
293
|
+
export interface Link {
|
|
294
|
+
/** @title Name */
|
|
295
|
+
name: string;
|
|
296
|
+
/** @title Submenu */
|
|
297
|
+
submenu?: Link[];
|
|
298
|
+
}
|
|
299
|
+
export interface Props { links: Link[] }
|
|
300
|
+
`;
|
|
301
|
+
const refId = `${Buffer.from("props.ts").toString("base64")}@Link`;
|
|
302
|
+
|
|
303
|
+
it("references the type from inside the cycle and registers its definition", () => {
|
|
304
|
+
const sf = strictProject().createSourceFile("/props.ts", source);
|
|
305
|
+
const definitions: Record<string, any> = {};
|
|
306
|
+
const schema = typeToJsonSchema(sf.getInterfaceOrThrow("Props").getType(), new Set(), {
|
|
307
|
+
definitions,
|
|
308
|
+
rootDir: "/",
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
const item = schema.properties.links.items;
|
|
312
|
+
expect(item.title).toBe("{{name}}");
|
|
313
|
+
expect(item.properties.submenu).toEqual({
|
|
314
|
+
type: "array",
|
|
315
|
+
items: { $ref: `#/definitions/${refId}` },
|
|
316
|
+
nullable: true,
|
|
317
|
+
title: "Submenu",
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
// The definition the $ref points at is the full type, itself recursive.
|
|
321
|
+
expect(Object.keys(definitions)).toEqual([refId]);
|
|
322
|
+
expect(definitions[refId].title).toBe("{{name}}");
|
|
323
|
+
expect(definitions[refId].required).toEqual(["name"]);
|
|
324
|
+
expect(definitions[refId].properties.submenu.items).toEqual({
|
|
325
|
+
$ref: `#/definitions/${refId}`,
|
|
326
|
+
});
|
|
327
|
+
}, 30_000);
|
|
328
|
+
|
|
329
|
+
it("falls back to a bare object when there is no definitions sink", () => {
|
|
330
|
+
const sf = strictProject().createSourceFile("/props.ts", source);
|
|
331
|
+
const schema = typeToJsonSchema(sf.getInterfaceOrThrow("Props").getType());
|
|
332
|
+
expect(schema.properties.links.items.properties.submenu).toEqual({
|
|
333
|
+
type: "object",
|
|
334
|
+
nullable: true,
|
|
335
|
+
title: "Submenu",
|
|
336
|
+
});
|
|
337
|
+
}, 30_000);
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
describe("typeToJsonSchema JSDoc tag parsing", () => {
|
|
341
|
+
it("ignores `@description:` (colon after the tag name), as deco-cx/deco does", () => {
|
|
342
|
+
const sf = strictProject().createSourceFile(
|
|
343
|
+
"/props.ts",
|
|
344
|
+
`
|
|
345
|
+
export interface Props {
|
|
346
|
+
/**
|
|
347
|
+
* @title Título
|
|
348
|
+
* @description: Nome que irá aparecer na pílula.
|
|
349
|
+
*/
|
|
350
|
+
name: string;
|
|
351
|
+
/** @description Kept. */
|
|
352
|
+
path: string;
|
|
353
|
+
}
|
|
354
|
+
`,
|
|
355
|
+
);
|
|
356
|
+
const schema = typeToJsonSchema(sf.getInterfaceOrThrow("Props").getType());
|
|
357
|
+
expect(schema.properties.name).toEqual({ type: "string", title: "Título" });
|
|
358
|
+
expect(schema.properties.path.description).toBe("Kept.");
|
|
359
|
+
}, 30_000);
|
|
360
|
+
});
|
|
361
|
+
|
|
194
362
|
describe("typeToJsonSchema Section-typed props", () => {
|
|
195
363
|
// The framework's `Section` is opaque (`export type Section = any`), so a
|
|
196
364
|
// Section-typed prop is a "pick any section" reference emitted as a
|
|
@@ -367,7 +367,62 @@ const RUNTIME_INJECTED_TYPES = new Set([
|
|
|
367
367
|
]);
|
|
368
368
|
|
|
369
369
|
interface GenerationContext {
|
|
370
|
-
|
|
370
|
+
/** Loader output type name → loader keys, for block-ref pickers. Sections only. */
|
|
371
|
+
outputTypeToLoaderKeys?: Map<string, string[]>;
|
|
372
|
+
/**
|
|
373
|
+
* Shared definitions sink. When present, a recursive named type (e.g.
|
|
374
|
+
* `interface Link { submenu?: Link[] }`) is emitted once as a definition and
|
|
375
|
+
* the cycle becomes a `$ref` to it — what deco-cx/deco emits. Without it the
|
|
376
|
+
* cycle collapses to a bare `{ type: "object" }`.
|
|
377
|
+
*/
|
|
378
|
+
definitions?: Record<string, any>;
|
|
379
|
+
/** Root the definition ids are relative to (see definitionIdForPath). */
|
|
380
|
+
rootDir?: string;
|
|
381
|
+
/** Definition ids referenced from inside a cycle, pending registration. */
|
|
382
|
+
recursiveRefs?: Set<string>;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Definition id for a named object/interface type, or null when the type has
|
|
387
|
+
* no stable name to key on (anonymous literal, generic instantiation).
|
|
388
|
+
*/
|
|
389
|
+
function namedTypeDefinitionId(type: Type, rootDir: string): string | null {
|
|
390
|
+
if (!type.isObject() && !type.isInterface()) return null;
|
|
391
|
+
if (type.isArray() || type.getTypeArguments().length > 0) return null;
|
|
392
|
+
if (type.getAliasTypeArguments().length > 0) return null;
|
|
393
|
+
const sym = type.getAliasSymbol() ?? type.getSymbol();
|
|
394
|
+
const name = sym?.getName();
|
|
395
|
+
if (!sym || !name || name === "__type" || name === "__object") return null;
|
|
396
|
+
const file = sym.getDeclarations()[0]?.getSourceFile().getFilePath();
|
|
397
|
+
if (!file) return null;
|
|
398
|
+
return `${definitionIdForPath(file, rootDir)}@${name}`;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Schema for a type met again while it is still being expanded. Looks through
|
|
403
|
+
* the array / `| null | undefined` wrapper the cycle was detected on, so
|
|
404
|
+
* `submenu?: Link[]` becomes `{ type: "array", items: { $ref }, nullable }`.
|
|
405
|
+
* Returns null when there is nothing to reference (falls back to `object`).
|
|
406
|
+
*/
|
|
407
|
+
function recursiveTypeRef(type: Type, ctx?: GenerationContext): any | null {
|
|
408
|
+
if (!ctx?.definitions) return null;
|
|
409
|
+
if (type.isUnion()) {
|
|
410
|
+
const parts = type.getUnionTypes();
|
|
411
|
+
const nonNull = parts.filter((t) => !t.isNull() && !t.isUndefined());
|
|
412
|
+
if (nonNull.length !== 1) return null;
|
|
413
|
+
const inner = recursiveTypeRef(nonNull[0], ctx);
|
|
414
|
+
return inner && nonNull.length < parts.length ? { ...inner, nullable: true } : inner;
|
|
415
|
+
}
|
|
416
|
+
if (type.isArray()) {
|
|
417
|
+
const el = type.getArrayElementType();
|
|
418
|
+
const items = el ? recursiveTypeRef(el, ctx) : null;
|
|
419
|
+
return items ? { type: "array", items } : null;
|
|
420
|
+
}
|
|
421
|
+
const defId = namedTypeDefinitionId(type, ctx.rootDir ?? process.cwd());
|
|
422
|
+
if (!defId) return null;
|
|
423
|
+
ctx.recursiveRefs ??= new Set();
|
|
424
|
+
ctx.recursiveRefs.add(defId);
|
|
425
|
+
return { $ref: `#/definitions/${defId}` };
|
|
371
426
|
}
|
|
372
427
|
|
|
373
428
|
function namedLoaderType(type: Type | undefined): string | null {
|
|
@@ -420,7 +475,7 @@ function extractLoaderOutputTypeName(sourceFile: SourceFile): string | null {
|
|
|
420
475
|
|
|
421
476
|
export function typeToJsonSchema(type: Type, visited = new Set<string>(), ctx?: GenerationContext): any {
|
|
422
477
|
const typeText = type.getText();
|
|
423
|
-
if (visited.has(typeText)) return { type: "object" };
|
|
478
|
+
if (visited.has(typeText)) return recursiveTypeRef(type, ctx) ?? { type: "object" };
|
|
424
479
|
visited.add(typeText);
|
|
425
480
|
|
|
426
481
|
try {
|
|
@@ -437,9 +492,12 @@ export function typeToJsonSchema(type: Type, visited = new Set<string>(), ctx?:
|
|
|
437
492
|
}
|
|
438
493
|
|
|
439
494
|
if (type.isString() || type.isStringLiteral()) {
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
495
|
+
if (!type.isStringLiteral()) return { type: "string" };
|
|
496
|
+
// deco-cx/deco pairs every `const` with an equal `default`, so the admin
|
|
497
|
+
// writes the literal into new entries — e.g. a hidden `__type: "image"`
|
|
498
|
+
// discriminator the component branches on.
|
|
499
|
+
const value = type.getLiteralValue();
|
|
500
|
+
return { type: "string", const: value, default: value };
|
|
443
501
|
}
|
|
444
502
|
if (type.isNumber() || type.isNumberLiteral()) return { type: "number" };
|
|
445
503
|
if (type.isBoolean() || type.isBooleanLiteral()) return { type: "boolean" };
|
|
@@ -569,7 +627,10 @@ export function typeToJsonSchema(type: Type, visited = new Set<string>(), ctx?:
|
|
|
569
627
|
|
|
570
628
|
for (const prop of type.getProperties()) {
|
|
571
629
|
const name = prop.getName();
|
|
572
|
-
|
|
630
|
+
// `_`-prefixed props are user data (`__title` labels a list entry in the
|
|
631
|
+
// admin, `_id` keys a dynamic-options picker) and deco-cx/deco keeps
|
|
632
|
+
// them; only the block-reference discriminator is framework-owned.
|
|
633
|
+
if (name === "__resolveType" || name.startsWith("$") || name === "@type") continue;
|
|
573
634
|
if (REACT_INTERNAL_PROPS.has(name)) continue;
|
|
574
635
|
|
|
575
636
|
// getValueDeclaration() returns undefined for computed/mapped-type
|
|
@@ -669,7 +730,10 @@ export function typeToJsonSchema(type: Type, visited = new Set<string>(), ctx?:
|
|
|
669
730
|
properties[name] = schema;
|
|
670
731
|
// A hidden prop (runtime-injected type, ReactNode, @hide) can never be
|
|
671
732
|
// filled in by the CMS user — requiring it would deadlock the form.
|
|
672
|
-
|
|
733
|
+
// Unless it has a default the admin writes for the user (a hidden
|
|
734
|
+
// literal such as `__title: "…"`), which deco-cx/deco keeps required.
|
|
735
|
+
const fillable = schema.hide !== "true" || "default" in schema;
|
|
736
|
+
if (!prop.isOptional() && fillable) required.push(name);
|
|
673
737
|
}
|
|
674
738
|
|
|
675
739
|
const result: any = { type: "object", properties };
|
|
@@ -681,6 +745,16 @@ export function typeToJsonSchema(type: Type, visited = new Set<string>(), ctx?:
|
|
|
681
745
|
applyJsDocToSchema(result, ifaceTags);
|
|
682
746
|
}
|
|
683
747
|
|
|
748
|
+
// A nested occurrence of this type referenced it (recursive type):
|
|
749
|
+
// register the definition the `$ref` points at. Cloned, because callers
|
|
750
|
+
// decorate the returned schema with the prop's own title/description.
|
|
751
|
+
if (ctx?.definitions && ctx.recursiveRefs?.size) {
|
|
752
|
+
const defId = namedTypeDefinitionId(type, ctx.rootDir ?? process.cwd());
|
|
753
|
+
if (defId && ctx.recursiveRefs.has(defId) && !(defId in ctx.definitions)) {
|
|
754
|
+
ctx.definitions[defId] = structuredClone(result);
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
|
|
684
758
|
return result;
|
|
685
759
|
}
|
|
686
760
|
|
|
@@ -698,6 +772,11 @@ export function getJsDocTags(symbol: MorphSymbol): Record<string, string> {
|
|
|
698
772
|
const desc = doc.getDescription().trim();
|
|
699
773
|
if (desc) tags.description = desc;
|
|
700
774
|
for (const tag of doc.getTags()) {
|
|
775
|
+
// deco-cx/deco reads tags with /@(\w+)\s+/, so `@description: text` was
|
|
776
|
+
// never a description there and the admin did not show it. The TS
|
|
777
|
+
// parser splits it into tag `description` + comment `: text`; skip it
|
|
778
|
+
// rather than emit a description that starts with ": ".
|
|
779
|
+
if (/^@\w+[^\w\s]/.test(tag.getText())) continue;
|
|
701
780
|
tags[tag.getTagName()] = tag.getCommentText()?.trim() || "true";
|
|
702
781
|
}
|
|
703
782
|
}
|
|
@@ -968,6 +1047,8 @@ function generateMeta(): MetaResponse {
|
|
|
968
1047
|
const sourceFileCache: SourceFileCache = new Map();
|
|
969
1048
|
const moduleResolutionCache: ModuleResolutionCache = new Map();
|
|
970
1049
|
const propsSchemaCache: PropsSchemaCache = new Map();
|
|
1050
|
+
// Shared by every pass so each recursive type is emitted as one definition.
|
|
1051
|
+
const schemaRefs: GenerationContext = { definitions, rootDir: root, recursiveRefs: new Set() };
|
|
971
1052
|
|
|
972
1053
|
// Resolvable: the admin's deRefUntil expects the LITERAL key "Resolvable",
|
|
973
1054
|
// not a base64-encoded version. We store both for compatibility.
|
|
@@ -1002,14 +1083,16 @@ function generateMeta(): MetaResponse {
|
|
|
1002
1083
|
// Extract Props (input schema)
|
|
1003
1084
|
let propsSchema: any = null;
|
|
1004
1085
|
const propsInterface = sourceFile.getInterface("Props");
|
|
1005
|
-
if (propsInterface)
|
|
1086
|
+
if (propsInterface)
|
|
1087
|
+
propsSchema = typeToJsonSchema(propsInterface.getType(), undefined, schemaRefs);
|
|
1006
1088
|
|
|
1007
1089
|
const propsTypeAlias = sourceFile.getTypeAlias("Props");
|
|
1008
|
-
if (!propsSchema && propsTypeAlias)
|
|
1090
|
+
if (!propsSchema && propsTypeAlias)
|
|
1091
|
+
propsSchema = typeToJsonSchema(propsTypeAlias.getType(), undefined, schemaRefs);
|
|
1009
1092
|
|
|
1010
1093
|
if (!propsSchema) {
|
|
1011
1094
|
const localPropsType = extractDefaultExportPropsType(sourceFile);
|
|
1012
|
-
if (localPropsType) propsSchema = typeToJsonSchema(localPropsType);
|
|
1095
|
+
if (localPropsType) propsSchema = typeToJsonSchema(localPropsType, undefined, schemaRefs);
|
|
1013
1096
|
}
|
|
1014
1097
|
|
|
1015
1098
|
if (!propsSchema) propsSchema = { type: "object", properties: {} };
|
|
@@ -1162,14 +1245,16 @@ function generateMeta(): MetaResponse {
|
|
|
1162
1245
|
// Extract Props (input schema)
|
|
1163
1246
|
let propsSchema: any = null;
|
|
1164
1247
|
const propsInterface = sourceFile.getInterface("Props");
|
|
1165
|
-
if (propsInterface)
|
|
1248
|
+
if (propsInterface)
|
|
1249
|
+
propsSchema = typeToJsonSchema(propsInterface.getType(), undefined, schemaRefs);
|
|
1166
1250
|
|
|
1167
1251
|
const propsTypeAlias = sourceFile.getTypeAlias("Props");
|
|
1168
|
-
if (!propsSchema && propsTypeAlias)
|
|
1252
|
+
if (!propsSchema && propsTypeAlias)
|
|
1253
|
+
propsSchema = typeToJsonSchema(propsTypeAlias.getType(), undefined, schemaRefs);
|
|
1169
1254
|
|
|
1170
1255
|
if (!propsSchema) {
|
|
1171
1256
|
const localPropsType = extractDefaultExportPropsType(sourceFile);
|
|
1172
|
-
if (localPropsType) propsSchema = typeToJsonSchema(localPropsType);
|
|
1257
|
+
if (localPropsType) propsSchema = typeToJsonSchema(localPropsType, undefined, schemaRefs);
|
|
1173
1258
|
}
|
|
1174
1259
|
|
|
1175
1260
|
if (!propsSchema) propsSchema = { type: "object", properties: {} };
|
|
@@ -1220,7 +1305,7 @@ function generateMeta(): MetaResponse {
|
|
|
1220
1305
|
}
|
|
1221
1306
|
}
|
|
1222
1307
|
|
|
1223
|
-
const ctx: GenerationContext = { outputTypeToLoaderKeys };
|
|
1308
|
+
const ctx: GenerationContext = { ...schemaRefs, outputTypeToLoaderKeys };
|
|
1224
1309
|
|
|
1225
1310
|
// ---------------------------------------------------------------------------
|
|
1226
1311
|
// Commerce "extension wrapper" loaders (deco-cx parity).
|