@decocms/blocks-cli 7.79.5 → 7.79.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@decocms/blocks-cli",
|
|
3
|
-
"version": "7.79.
|
|
3
|
+
"version": "7.79.6",
|
|
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.79.
|
|
36
|
+
"@decocms/blocks": "7.79.6",
|
|
37
37
|
"ts-morph": "^27.0.0",
|
|
38
38
|
"tsx": "^4.22.5"
|
|
39
39
|
},
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
|
6
|
+
|
|
7
|
+
interface Schema {
|
|
8
|
+
type?: string;
|
|
9
|
+
title?: string;
|
|
10
|
+
description?: string;
|
|
11
|
+
icon?: string;
|
|
12
|
+
nullable?: boolean;
|
|
13
|
+
$ref?: string;
|
|
14
|
+
allOf?: Schema[];
|
|
15
|
+
anyOf?: Schema[];
|
|
16
|
+
enum?: string[];
|
|
17
|
+
const?: string;
|
|
18
|
+
properties?: Record<string, Schema>;
|
|
19
|
+
required?: string[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Regressions against deco-cx/deco's admin, driven through the real CLI:
|
|
23
|
+
// - `Resolved<T>` (an alias of T) was offered T's loaders directly, so its
|
|
24
|
+
// stored `{ __resolveType: "resolved", data }` matched no option; a flag
|
|
25
|
+
// over `Resolved<T>` (a minicart's products) was one picker option among
|
|
26
|
+
// loaders instead of the field's variants.
|
|
27
|
+
// - Site matchers (src/matchers) had no schema, so a rule using one could
|
|
28
|
+
// not be listed or opened.
|
|
29
|
+
describe("generate-schema Resolved<T> props and site matchers", () => {
|
|
30
|
+
let fixture: string;
|
|
31
|
+
let meta: any;
|
|
32
|
+
|
|
33
|
+
const FLAG = "site/flags/multivariate/products.ts";
|
|
34
|
+
const MATCHER = "site/matchers/url.ts";
|
|
35
|
+
|
|
36
|
+
beforeAll(() => {
|
|
37
|
+
fixture = mkdtempSync(path.join(tmpdir(), "schema-resolved-"));
|
|
38
|
+
const write = (relative: string, contents: string) => {
|
|
39
|
+
const file = path.join(fixture, relative);
|
|
40
|
+
mkdirSync(path.dirname(file), { recursive: true });
|
|
41
|
+
writeFileSync(file, contents);
|
|
42
|
+
};
|
|
43
|
+
write(
|
|
44
|
+
"tsconfig.json",
|
|
45
|
+
JSON.stringify({
|
|
46
|
+
compilerOptions: {
|
|
47
|
+
target: "ES2022",
|
|
48
|
+
module: "ESNext",
|
|
49
|
+
moduleResolution: "Bundler",
|
|
50
|
+
strict: true,
|
|
51
|
+
},
|
|
52
|
+
}),
|
|
53
|
+
);
|
|
54
|
+
write(
|
|
55
|
+
"src/types.ts",
|
|
56
|
+
`export type Resolved<T = any> = T;
|
|
57
|
+
export interface Product { productID: string; }
|
|
58
|
+
export interface Variant<T> { rule: unknown; value: T; }
|
|
59
|
+
export interface MultivariateProps<T> { variants: Variant<T>[]; }
|
|
60
|
+
export interface MultivariateFlag<T> { variants: Variant<T>[]; }`,
|
|
61
|
+
);
|
|
62
|
+
write(
|
|
63
|
+
"src/loaders/productList.ts",
|
|
64
|
+
`import type { Product } from "../types";
|
|
65
|
+
export default function productList(): Product[] { return []; }`,
|
|
66
|
+
);
|
|
67
|
+
write(
|
|
68
|
+
"src/flags/multivariate/products.ts",
|
|
69
|
+
`import type { MultivariateFlag, MultivariateProps, Product, Resolved } from "../../types";
|
|
70
|
+
/** @title Product Variants */
|
|
71
|
+
export default function products(
|
|
72
|
+
p: MultivariateProps<Resolved<Product[] | null>>,
|
|
73
|
+
): MultivariateFlag<Resolved<Product[] | null>> { return p; }`,
|
|
74
|
+
);
|
|
75
|
+
write(
|
|
76
|
+
"src/sections/Shelf.tsx",
|
|
77
|
+
`import type { Product, Resolved } from "../types";
|
|
78
|
+
export interface Props {
|
|
79
|
+
/** @title Products loader */
|
|
80
|
+
productsLoader?: Resolved<Product[] | null>;
|
|
81
|
+
products?: Product[] | null;
|
|
82
|
+
}
|
|
83
|
+
export default function Shelf(_props: Props) { return null; }`,
|
|
84
|
+
);
|
|
85
|
+
write(
|
|
86
|
+
"src/matchers/url.ts",
|
|
87
|
+
`export interface Props { includes?: string; match?: string; }
|
|
88
|
+
/**
|
|
89
|
+
* @title URL
|
|
90
|
+
* @description Target users by URL
|
|
91
|
+
* @icon world-www
|
|
92
|
+
*/
|
|
93
|
+
const MatchUrl = ({ includes }: Props) => !!includes;
|
|
94
|
+
export default MatchUrl;`,
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
const run = spawnSync(
|
|
98
|
+
process.execPath,
|
|
99
|
+
[
|
|
100
|
+
"--import",
|
|
101
|
+
import.meta.resolve("tsx"),
|
|
102
|
+
path.resolve(__dirname, "generate-schema.ts"),
|
|
103
|
+
"--skip-apps",
|
|
104
|
+
],
|
|
105
|
+
{ cwd: fixture, encoding: "utf8", timeout: 30_000 },
|
|
106
|
+
);
|
|
107
|
+
expect(run.status, run.error?.message ?? run.stderr).toBe(0);
|
|
108
|
+
meta = JSON.parse(readFileSync(path.join(fixture, ".deco/meta.gen.json"), "utf8"));
|
|
109
|
+
}, 30_000);
|
|
110
|
+
|
|
111
|
+
afterAll(() => {
|
|
112
|
+
if (fixture) rmSync(fixture, { recursive: true, force: true });
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
const def = (key: string): Schema => meta.schema.definitions[key];
|
|
116
|
+
const shelfProps = () => {
|
|
117
|
+
const section = def(btoa("site/sections/Shelf.tsx"));
|
|
118
|
+
const ref = section.allOf!.find((part) => part.$ref)!.$ref!;
|
|
119
|
+
return def(ref.replace("#/definitions/", "")).properties!;
|
|
120
|
+
};
|
|
121
|
+
const productLoaderPicker = [
|
|
122
|
+
{ $ref: "#/definitions/Resolvable" },
|
|
123
|
+
{ $ref: `#/definitions/${btoa("site/loaders/productList.ts")}` },
|
|
124
|
+
];
|
|
125
|
+
|
|
126
|
+
it("emits Resolved<T> as deco-cx/deco did: { __resolveType: resolved, data: T }", () => {
|
|
127
|
+
const { productsLoader } = shelfProps();
|
|
128
|
+
expect(productsLoader.title).toBe("Products loader");
|
|
129
|
+
expect(productsLoader.nullable).toBe(true);
|
|
130
|
+
const [resolved, flag] = productsLoader.anyOf!;
|
|
131
|
+
expect(resolved.required).toEqual(["data", "__resolveType"]);
|
|
132
|
+
expect(resolved.properties!.__resolveType).toMatchObject({ const: "resolved" });
|
|
133
|
+
// `data` is what the editor fills: a loader of T.
|
|
134
|
+
expect(resolved.properties!.data.anyOf).toEqual(productLoaderPicker);
|
|
135
|
+
// The only block option is the flag over Resolved<T>: the field has variants.
|
|
136
|
+
expect(flag.properties!.__resolveType.enum).toEqual([FLAG]);
|
|
137
|
+
expect(productsLoader.anyOf).toHaveLength(2);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("gives a Resolved flag variants of Resolved<T>", () => {
|
|
141
|
+
const value = def(`${btoa(FLAG)}@Props`).properties!.variants as any;
|
|
142
|
+
expect(value.items.properties.value.properties.data.anyOf).toEqual(productLoaderPicker);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("keeps a plain T prop a loader picker, without the Resolved flag", () => {
|
|
146
|
+
expect(shelfProps().products.anyOf).toEqual(productLoaderPicker);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("registers a site matcher with its JSDoc and props", () => {
|
|
150
|
+
expect(meta.manifest.blocks.matchers[MATCHER]).toEqual({
|
|
151
|
+
$ref: `#/definitions/${btoa(MATCHER)}`,
|
|
152
|
+
namespace: "site",
|
|
153
|
+
});
|
|
154
|
+
const matcher = def(btoa(MATCHER));
|
|
155
|
+
expect(matcher).toMatchObject({
|
|
156
|
+
title: "URL",
|
|
157
|
+
description: "Target users by URL",
|
|
158
|
+
icon: "world-www",
|
|
159
|
+
});
|
|
160
|
+
expect(Object.keys(matcher.properties!)).toEqual(["__resolveType", "includes", "match"]);
|
|
161
|
+
// composeMeta keeps it in the matcher union next to the built-ins.
|
|
162
|
+
expect(meta.schema.root.matchers.anyOf).toContainEqual({
|
|
163
|
+
$ref: `#/definitions/${btoa(MATCHER)}`,
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
});
|
|
@@ -71,7 +71,7 @@ describe("applyWidgetFormat", () => {
|
|
|
71
71
|
// returns {}. The intended widget must still be recovered.
|
|
72
72
|
const schema: any = {};
|
|
73
73
|
applyWidgetFormat(schema, "Color");
|
|
74
|
-
expect(schema).toEqual({ type: "string", format: "color" });
|
|
74
|
+
expect(schema).toEqual({ type: "string", format: "color-input" });
|
|
75
75
|
});
|
|
76
76
|
|
|
77
77
|
it.each(Object.entries(WIDGET_TYPE_FORMATS))(
|
|
@@ -86,7 +86,7 @@ describe("applyWidgetFormat", () => {
|
|
|
86
86
|
it("applies the format to a resolved string schema", () => {
|
|
87
87
|
const schema: any = { type: "string" };
|
|
88
88
|
applyWidgetFormat(schema, "Color");
|
|
89
|
-
expect(schema).toEqual({ type: "string", format: "color" });
|
|
89
|
+
expect(schema).toEqual({ type: "string", format: "color-input" });
|
|
90
90
|
});
|
|
91
91
|
|
|
92
92
|
it("does not overwrite a schema that resolved to a $ref", () => {
|
|
@@ -103,7 +103,7 @@ describe("applyWidgetFormat", () => {
|
|
|
103
103
|
});
|
|
104
104
|
|
|
105
105
|
describe("typeToJsonSchema with an unresolvable widget alias import", () => {
|
|
106
|
-
it("emits { type: string, format: color } for a Color field imported from a CDN", () => {
|
|
106
|
+
it("emits { type: string, format: color-input } for a Color field imported from a CDN", () => {
|
|
107
107
|
const project = new Project({
|
|
108
108
|
useInMemoryFileSystem: true,
|
|
109
109
|
compilerOptions: { skipLibCheck: true, noResolve: false },
|
|
@@ -130,7 +130,7 @@ describe("typeToJsonSchema with an unresolvable widget alias import", () => {
|
|
|
130
130
|
expect(schema.properties.textLeftColor).toEqual({
|
|
131
131
|
title: "Cor do Texto",
|
|
132
132
|
type: "string",
|
|
133
|
-
format: "color",
|
|
133
|
+
format: "color-input",
|
|
134
134
|
});
|
|
135
135
|
}, 30_000);
|
|
136
136
|
});
|
|
@@ -602,3 +602,27 @@ describe("default prop titles (deco-cx/deco beautify)", () => {
|
|
|
602
602
|
expect(schema.properties.noFlex.title).toBe("Custom");
|
|
603
603
|
});
|
|
604
604
|
});
|
|
605
|
+
|
|
606
|
+
describe("widget detection by the whole type name", () => {
|
|
607
|
+
it("does not treat a type that merely contains a widget name as that widget", () => {
|
|
608
|
+
const project = strictProject();
|
|
609
|
+
const sf = project.createSourceFile(
|
|
610
|
+
"/colors.ts",
|
|
611
|
+
`type Color = string;
|
|
612
|
+
type ButtonColor = "Default" | "Primary";
|
|
613
|
+
export interface Props {
|
|
614
|
+
brand?: Color;
|
|
615
|
+
buttons?: ButtonColor;
|
|
616
|
+
variation?: "Simple" | "Color reverse";
|
|
617
|
+
}`,
|
|
618
|
+
);
|
|
619
|
+
const schema = typeToJsonSchema(sf.getInterfaceOrThrow("Props").getType());
|
|
620
|
+
applyWidgetFormat(schema.properties.brand, "Color");
|
|
621
|
+
applyWidgetFormat(schema.properties.buttons, "ButtonColor");
|
|
622
|
+
applyWidgetFormat(schema.properties.variation, '"Simple" | "Color reverse"');
|
|
623
|
+
expect(schema.properties.variation.format).toBeUndefined();
|
|
624
|
+
expect(schema.properties.brand.format).toBe("color-input");
|
|
625
|
+
expect(schema.properties.buttons.format).toBeUndefined();
|
|
626
|
+
expect(schema.properties.buttons.enum).toEqual(["Default", "Primary"]);
|
|
627
|
+
});
|
|
628
|
+
});
|
|
@@ -24,6 +24,7 @@ import { fileURLToPath } from "node:url";
|
|
|
24
24
|
* --sections Sections directory (default: "src/sections")
|
|
25
25
|
* --loaders Loaders directory (default: "src/loaders")
|
|
26
26
|
* --flags Flags directory (default: "src/flags")
|
|
27
|
+
* --matchers Matchers directory (default: "src/matchers")
|
|
27
28
|
* --apps Apps directory (default: "src/apps")
|
|
28
29
|
* --skip-apps Skip app schema generation
|
|
29
30
|
* --out Output file (default: ".deco/meta.gen.json")
|
|
@@ -73,6 +74,7 @@ let FRAMEWORK_VERSION = "1.0.0";
|
|
|
73
74
|
let SECTIONS_REL = "src/sections";
|
|
74
75
|
let LOADERS_REL = "src/loaders";
|
|
75
76
|
let FLAGS_REL = "src/flags";
|
|
77
|
+
let MATCHERS_REL = "src/matchers";
|
|
76
78
|
let APPS_REL = "src/apps";
|
|
77
79
|
let SKIP_APPS = false;
|
|
78
80
|
let OUT_REL = NEW_DEFAULT_OUT_REL;
|
|
@@ -92,6 +94,7 @@ if (isMainModule()) {
|
|
|
92
94
|
SECTIONS_REL = arg("sections", SECTIONS_REL);
|
|
93
95
|
LOADERS_REL = arg("loaders", LOADERS_REL);
|
|
94
96
|
FLAGS_REL = arg("flags", FLAGS_REL);
|
|
97
|
+
MATCHERS_REL = arg("matchers", MATCHERS_REL);
|
|
95
98
|
APPS_REL = arg("apps", APPS_REL);
|
|
96
99
|
SKIP_APPS = argv.includes("--skip-apps");
|
|
97
100
|
OUT_REL = arg("out", NEW_DEFAULT_OUT_REL);
|
|
@@ -233,7 +236,8 @@ export const WIDGET_TYPE_FORMATS: Record<string, string> = {
|
|
|
233
236
|
VideoWidget: "video-uri",
|
|
234
237
|
HTMLWidget: "html",
|
|
235
238
|
RichText: "rich-text",
|
|
236
|
-
|
|
239
|
+
// deco-cx/deco's widget format; the admin renders "color" the same way.
|
|
240
|
+
Color: "color-input",
|
|
237
241
|
Secret: "password",
|
|
238
242
|
TextArea: "textarea",
|
|
239
243
|
Code: "code",
|
|
@@ -269,6 +273,17 @@ export function normalizeFormats(node: unknown, aliases: Record<string, string>)
|
|
|
269
273
|
}
|
|
270
274
|
}
|
|
271
275
|
|
|
276
|
+
/**
|
|
277
|
+
* Whether a type annotation names a widget type as a whole identifier:
|
|
278
|
+
* `Color`, `Color | null`, `ImageWidget[]` do; `ButtonColor`, `BgColor` (a
|
|
279
|
+
* site's own select of named colors) or a literal `"Color reverse"` don't.
|
|
280
|
+
*/
|
|
281
|
+
function mentionsWidget(typeText: string, widgetType: string): boolean {
|
|
282
|
+
// Literal members (`"Color reverse"`) are option labels, not type names.
|
|
283
|
+
const names = typeText.replace(/"[^"]*"|'[^']*'|`[^`]*`/g, '""');
|
|
284
|
+
return new RegExp(`(^|[^\\w$])${widgetType}($|[^\\w$])`).test(names);
|
|
285
|
+
}
|
|
286
|
+
|
|
272
287
|
/**
|
|
273
288
|
* Detect known widget types and set the appropriate format.
|
|
274
289
|
*/
|
|
@@ -276,7 +291,7 @@ function applyWidgetDetection(schema: any, typeText: string): void {
|
|
|
276
291
|
if (schema.format) return;
|
|
277
292
|
|
|
278
293
|
for (const [widgetType, format] of Object.entries(WIDGET_TYPE_FORMATS)) {
|
|
279
|
-
if (typeText
|
|
294
|
+
if (mentionsWidget(typeText, widgetType)) {
|
|
280
295
|
schema.format = format;
|
|
281
296
|
return;
|
|
282
297
|
}
|
|
@@ -288,8 +303,8 @@ function applyWidgetDetection(schema: any, typeText: string): void {
|
|
|
288
303
|
* and union types by applying the format to the correct inner schema.
|
|
289
304
|
*/
|
|
290
305
|
export function applyWidgetFormat(schema: any, typeHint: string): void {
|
|
291
|
-
const matchedFormat = Object.entries(WIDGET_TYPE_FORMATS).find(
|
|
292
|
-
(
|
|
306
|
+
const matchedFormat = Object.entries(WIDGET_TYPE_FORMATS).find(([wt]) =>
|
|
307
|
+
mentionsWidget(typeHint, wt),
|
|
293
308
|
)?.[1];
|
|
294
309
|
|
|
295
310
|
if (!matchedFormat) {
|
|
@@ -537,6 +552,31 @@ export interface FlagBlock {
|
|
|
537
552
|
key: string;
|
|
538
553
|
title: string;
|
|
539
554
|
defKey: string;
|
|
555
|
+
/** Over `Resolved<T>` (`MultivariateProps<Resolved<T>>`): offered only on `Resolved<T>` props. */
|
|
556
|
+
resolved?: boolean;
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/**
|
|
560
|
+
* Schema of a `Resolved<T>` prop, as deco-cx/deco emitted it: the stored value
|
|
561
|
+
* is `{ __resolveType: "resolved", data: T }`, where `data` is what the editor
|
|
562
|
+
* fills (a loader, for a loader-backed `T`). `Resolved<T>` is an alias of `T`
|
|
563
|
+
* at the type level, so without this it was offered loaders directly and its
|
|
564
|
+
* stored `{ __resolveType: "resolved", … }` value matched none of them.
|
|
565
|
+
*/
|
|
566
|
+
export function resolvedSchema(dataSchema: any): any {
|
|
567
|
+
return {
|
|
568
|
+
type: "object",
|
|
569
|
+
required: ["data", "__resolveType"],
|
|
570
|
+
properties: {
|
|
571
|
+
data: { ...dataSchema, title: "Data" },
|
|
572
|
+
__resolveType: {
|
|
573
|
+
type: "string",
|
|
574
|
+
const: "resolved",
|
|
575
|
+
default: "resolved",
|
|
576
|
+
title: "__resolve Type",
|
|
577
|
+
},
|
|
578
|
+
},
|
|
579
|
+
};
|
|
540
580
|
}
|
|
541
581
|
|
|
542
582
|
/**
|
|
@@ -874,10 +914,41 @@ export function typeToJsonSchema(type: Type, visited = new Set<string>(), ctx?:
|
|
|
874
914
|
const outputTypeName = loaderTypeName(propType);
|
|
875
915
|
// By resolved type name, else by the annotation: `ImageWidget` is an
|
|
876
916
|
// alias of `string`, and TypeScript keeps no alias symbol on primitives.
|
|
877
|
-
|
|
917
|
+
// `Resolved<T>` props take only flags over `Resolved<T>`, others the rest.
|
|
918
|
+
const isResolved = /^Resolved\s*</.test(baseHint);
|
|
919
|
+
const matchingFlags = (
|
|
878
920
|
(outputTypeName ? ctx?.outputTypeToFlags?.get(outputTypeName) : undefined) ??
|
|
879
921
|
ctx?.outputTypeToFlags?.get(baseHint) ??
|
|
880
|
-
[]
|
|
922
|
+
[]
|
|
923
|
+
).filter((f) => !!f.resolved === isResolved);
|
|
924
|
+
|
|
925
|
+
if (isResolved) {
|
|
926
|
+
const loaders =
|
|
927
|
+
(outputTypeName ? ctx?.outputTypeToLoaderKeys?.get(outputTypeName) : undefined) ?? [];
|
|
928
|
+
const dataSchema = loaders.length
|
|
929
|
+
? {
|
|
930
|
+
anyOf: [
|
|
931
|
+
{ $ref: `#/definitions/${RESOLVABLE_KEY}` },
|
|
932
|
+
...loaders.map((k) => ({ $ref: `#/definitions/${toBase64(k)}` })),
|
|
933
|
+
],
|
|
934
|
+
}
|
|
935
|
+
: typeToJsonSchema(propType, new Set(visited), ctx);
|
|
936
|
+
const inline = resolvedSchema(dataSchema);
|
|
937
|
+
applyJsDocToSchema(inline, tags);
|
|
938
|
+
if (!inline.title) inline.title = beautify(name);
|
|
939
|
+
let resolvedProp: any = inline;
|
|
940
|
+
if (matchingFlags.length > 0) {
|
|
941
|
+
const { title, description, ...rest } = inline;
|
|
942
|
+
resolvedProp = { anyOf: [{ ...rest, title }, ...matchingFlags.map(flagBranch)], title };
|
|
943
|
+
if (description) resolvedProp.description = description;
|
|
944
|
+
}
|
|
945
|
+
if (prop.isOptional() || typeHint.includes("null") || typeHint.includes("undefined")) {
|
|
946
|
+
resolvedProp.nullable = true;
|
|
947
|
+
}
|
|
948
|
+
properties[name] = resolvedProp;
|
|
949
|
+
if (!prop.isOptional()) required.push(name);
|
|
950
|
+
continue;
|
|
951
|
+
}
|
|
881
952
|
|
|
882
953
|
// Loader output type → block-ref: emit anyOf [Resolvable, ...matchingLoaders]
|
|
883
954
|
// baseHint strips "| null | undefined" so "ProductListingPage | null" → "ProductListingPage"
|
|
@@ -980,7 +1051,9 @@ export function typeToJsonSchema(type: Type, visited = new Set<string>(), ctx?:
|
|
|
980
1051
|
export function getJsDocTags(symbol: MorphSymbol): Record<string, string> {
|
|
981
1052
|
const tags: Record<string, string> = {};
|
|
982
1053
|
for (const decl of symbol.getDeclarations()) {
|
|
983
|
-
const
|
|
1054
|
+
// `/** … */ const X = …`: the JSDoc sits on the variable statement.
|
|
1055
|
+
const docHost = Node.isVariableDeclaration(decl) ? decl.getVariableStatement() : decl;
|
|
1056
|
+
const jsDocs = docHost && Node.isJSDocable(docHost) ? docHost.getJsDocs() : [];
|
|
984
1057
|
for (const doc of jsDocs) {
|
|
985
1058
|
const desc = doc.getDescription().trim();
|
|
986
1059
|
if (desc) tags.description = desc;
|
|
@@ -1245,6 +1318,7 @@ function generateMeta(): MetaResponse {
|
|
|
1245
1318
|
const sectionsDir = path.resolve(root, SECTIONS_REL);
|
|
1246
1319
|
const loadersDir = path.resolve(root, LOADERS_REL);
|
|
1247
1320
|
const flagsDir = path.resolve(root, FLAGS_REL);
|
|
1321
|
+
const matchersDir = path.resolve(root, MATCHERS_REL);
|
|
1248
1322
|
const srcDir = path.join(root, "src");
|
|
1249
1323
|
|
|
1250
1324
|
const project = new Project({
|
|
@@ -1335,6 +1409,7 @@ function generateMeta(): MetaResponse {
|
|
|
1335
1409
|
key: flagKey,
|
|
1336
1410
|
title: titleTag && !titleTag.includes("/") ? titleTag : flagKey,
|
|
1337
1411
|
defKey: toBase64(flagKey),
|
|
1412
|
+
resolved: /MultivariateProps\s*<\s*Resolved\s*</.test(sourceFile.getFullText()),
|
|
1338
1413
|
};
|
|
1339
1414
|
const valueTypeName = loaderTypeName(valueType);
|
|
1340
1415
|
siteFlags.push({ flag, valueType, valueTypeName });
|
|
@@ -1652,7 +1727,9 @@ function generateMeta(): MetaResponse {
|
|
|
1652
1727
|
],
|
|
1653
1728
|
}
|
|
1654
1729
|
: typeToJsonSchema(valueType, undefined, ctx);
|
|
1655
|
-
definitions[`${flag.defKey}@Props`] = multivariatePropsSchema(
|
|
1730
|
+
definitions[`${flag.defKey}@Props`] = multivariatePropsSchema(
|
|
1731
|
+
flag.resolved ? resolvedSchema(valueSchema) : valueSchema,
|
|
1732
|
+
);
|
|
1656
1733
|
const { allOf, required, properties } = flagBranch(flag);
|
|
1657
1734
|
definitions[flag.defKey] = { title: flag.title, type: "object", allOf, required, properties };
|
|
1658
1735
|
flagBlocks[flag.key] = { $ref: `#/definitions/${flag.defKey}`, namespace: SITE_NAMESPACE };
|
|
@@ -1934,6 +2011,59 @@ function generateMeta(): MetaResponse {
|
|
|
1934
2011
|
}
|
|
1935
2012
|
}
|
|
1936
2013
|
|
|
2014
|
+
// ---------------------------------------------------------------------------
|
|
2015
|
+
// Site matchers (src/matchers/**): rule blocks the content stores by key
|
|
2016
|
+
// (`site/matchers/url.ts`). The runtime evaluates whatever the site registers;
|
|
2017
|
+
// without a schema entry the editor lists no such rule and can't open one.
|
|
2018
|
+
// ---------------------------------------------------------------------------
|
|
2019
|
+
const matcherBlocks: Record<string, any> = {};
|
|
2020
|
+
const matcherRootAnyOf: any[] = [{ $ref: `#/definitions/${RESOLVABLE_KEY}` }];
|
|
2021
|
+
const matcherFiles = fs.existsSync(matchersDir) ? findTsxFiles(matchersDir) : [];
|
|
2022
|
+
for (const filePath of matcherFiles) {
|
|
2023
|
+
const relativePath = path.relative(srcDir, filePath).replaceAll("\\", "/");
|
|
2024
|
+
const matcherKey = `${SITE_NAMESPACE}/${relativePath}`;
|
|
2025
|
+
try {
|
|
2026
|
+
const sourceFile = getSourceFile(project, filePath, sourceFileCache);
|
|
2027
|
+
const defaultSym = sourceFile.getDefaultExportSymbol();
|
|
2028
|
+
if (!defaultSym || defaultSym.getTypeAtLocation(sourceFile).getCallSignatures().length === 0)
|
|
2029
|
+
continue;
|
|
2030
|
+
const target = defaultSym.isAlias()
|
|
2031
|
+
? (defaultSym.getAliasedSymbol() ?? defaultSym)
|
|
2032
|
+
: defaultSym;
|
|
2033
|
+
const docs = { ...getJsDocTags(defaultSym), ...getJsDocTags(target) };
|
|
2034
|
+
|
|
2035
|
+
let propsSchema: any = null;
|
|
2036
|
+
const propsInterface = sourceFile.getInterface("Props");
|
|
2037
|
+
if (propsInterface) propsSchema = typeToJsonSchema(propsInterface.getType(), undefined, ctx);
|
|
2038
|
+
const propsTypeAlias = sourceFile.getTypeAlias("Props");
|
|
2039
|
+
if (!propsSchema && propsTypeAlias)
|
|
2040
|
+
propsSchema = typeToJsonSchema(propsTypeAlias.getType(), undefined, ctx);
|
|
2041
|
+
if (!propsSchema) {
|
|
2042
|
+
const localPropsType = extractDefaultExportPropsType(sourceFile);
|
|
2043
|
+
if (localPropsType) propsSchema = typeToJsonSchema(localPropsType, undefined, ctx);
|
|
2044
|
+
}
|
|
2045
|
+
propsSchema ??= { type: "object", properties: {} };
|
|
2046
|
+
|
|
2047
|
+
const defKey = toBase64(matcherKey);
|
|
2048
|
+
definitions[defKey] = {
|
|
2049
|
+
title: docs.title && !docs.title.includes("/") ? docs.title : matcherKey,
|
|
2050
|
+
...(docs.description ? { description: docs.description } : {}),
|
|
2051
|
+
...(docs.icon ? { icon: docs.icon } : {}),
|
|
2052
|
+
type: "object",
|
|
2053
|
+
required: ["__resolveType", ...(propsSchema.required || [])],
|
|
2054
|
+
properties: {
|
|
2055
|
+
__resolveType: { type: "string", enum: [matcherKey], default: matcherKey },
|
|
2056
|
+
...(propsSchema.properties || {}),
|
|
2057
|
+
},
|
|
2058
|
+
};
|
|
2059
|
+
matcherBlocks[matcherKey] = { $ref: `#/definitions/${defKey}`, namespace: SITE_NAMESPACE };
|
|
2060
|
+
matcherRootAnyOf.push({ $ref: `#/definitions/${defKey}` });
|
|
2061
|
+
console.log(` ✓ matcher ${matcherKey}`);
|
|
2062
|
+
} catch (e) {
|
|
2063
|
+
console.warn(` ✗ matcher ${matcherKey}: ${(e as Error).message}`);
|
|
2064
|
+
}
|
|
2065
|
+
}
|
|
2066
|
+
|
|
1937
2067
|
// Eitri @format aliases → JSON-Schema formats (e.g. datetime → date-time).
|
|
1938
2068
|
// Done as a final pass over the generated definitions so every prop schema,
|
|
1939
2069
|
// however deeply nested, is normalized before it reaches Studio.
|
|
@@ -1954,6 +2084,7 @@ function generateMeta(): MetaResponse {
|
|
|
1954
2084
|
sections: sectionBlocks,
|
|
1955
2085
|
loaders: loaderBlocks,
|
|
1956
2086
|
flags: flagBlocks,
|
|
2087
|
+
matchers: matcherBlocks,
|
|
1957
2088
|
apps: appBlocks,
|
|
1958
2089
|
},
|
|
1959
2090
|
},
|
|
@@ -1965,7 +2096,7 @@ function generateMeta(): MetaResponse {
|
|
|
1965
2096
|
actions: emptyAnyOf,
|
|
1966
2097
|
pages: emptyAnyOf,
|
|
1967
2098
|
handlers: emptyAnyOf,
|
|
1968
|
-
matchers:
|
|
2099
|
+
matchers: { anyOf: matcherRootAnyOf },
|
|
1969
2100
|
flags: { anyOf: flagRootAnyOf },
|
|
1970
2101
|
functions: emptyAnyOf,
|
|
1971
2102
|
apps: { anyOf: appRootAnyOf },
|