@decocms/blocks-cli 7.79.4 → 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
|
},
|
|
@@ -30,6 +30,7 @@ describe("generate-schema site multivariate flags", () => {
|
|
|
30
30
|
|
|
31
31
|
const FLAG = "site/flags/multivariate/content.ts";
|
|
32
32
|
const LIST_FLAG = "site/flags/multivariate/products.ts";
|
|
33
|
+
const IMAGE_FLAG = "website/flags/multivariate/image.ts";
|
|
33
34
|
|
|
34
35
|
beforeAll(() => {
|
|
35
36
|
fixture = mkdtempSync(path.join(tmpdir(), "schema-flags-"));
|
|
@@ -59,7 +60,8 @@ describe("generate-schema site multivariate flags", () => {
|
|
|
59
60
|
);
|
|
60
61
|
write(
|
|
61
62
|
"src/types.ts",
|
|
62
|
-
`export
|
|
63
|
+
`export type ImageWidget = string;
|
|
64
|
+
export interface Content { matcher?: string[]; label: string; }
|
|
63
65
|
export interface Kit { name: string; content: Content; }
|
|
64
66
|
export interface Product { productID: string; }`,
|
|
65
67
|
);
|
|
@@ -92,8 +94,10 @@ describe("generate-schema site multivariate flags", () => {
|
|
|
92
94
|
);
|
|
93
95
|
write(
|
|
94
96
|
"src/sections/Example.tsx",
|
|
95
|
-
`import type { Content, Product } from "../types";
|
|
97
|
+
`import type { Content, ImageWidget, Product } from "../types";
|
|
96
98
|
export interface Props {
|
|
99
|
+
banner?: ImageWidget;
|
|
100
|
+
gallery?: ImageWidget[];
|
|
97
101
|
/**
|
|
98
102
|
* @title Main content
|
|
99
103
|
* @description Shown on top
|
|
@@ -151,6 +155,7 @@ describe("generate-schema site multivariate flags", () => {
|
|
|
151
155
|
|
|
152
156
|
it("registers each flag in the manifest and in root.flags", () => {
|
|
153
157
|
expect(meta.manifest.blocks.flags).toEqual({
|
|
158
|
+
[IMAGE_FLAG]: { $ref: `#/definitions/${btoa(IMAGE_FLAG)}`, namespace: "website" },
|
|
154
159
|
[FLAG]: { $ref: `#/definitions/${btoa(FLAG)}`, namespace: "site" },
|
|
155
160
|
[LIST_FLAG]: { $ref: `#/definitions/${btoa(LIST_FLAG)}`, namespace: "site" },
|
|
156
161
|
});
|
|
@@ -213,4 +218,25 @@ describe("generate-schema site multivariate flags", () => {
|
|
|
213
218
|
expect(hidden.anyOf).toBeUndefined();
|
|
214
219
|
expect(hidden.type).toBe("object");
|
|
215
220
|
});
|
|
221
|
+
|
|
222
|
+
it("offers image variants on an ImageWidget prop, as deco-cx/deco did", () => {
|
|
223
|
+
const { banner, gallery } = sectionProps();
|
|
224
|
+
expect(banner.anyOf).toEqual([
|
|
225
|
+
{ type: "string", format: "image-uri", title: "Banner" },
|
|
226
|
+
{
|
|
227
|
+
title: "Image Variants",
|
|
228
|
+
type: "object",
|
|
229
|
+
allOf: [{ $ref: `#/definitions/${btoa(IMAGE_FLAG)}@Props` }],
|
|
230
|
+
required: ["__resolveType"],
|
|
231
|
+
properties: {
|
|
232
|
+
__resolveType: { type: "string", enum: [IMAGE_FLAG], default: IMAGE_FLAG },
|
|
233
|
+
},
|
|
234
|
+
},
|
|
235
|
+
]);
|
|
236
|
+
const value = def(`${btoa(IMAGE_FLAG)}@Props`).properties!.variants.items!.properties!.value;
|
|
237
|
+
expect(value).toMatchObject({ type: "string", format: "image-uri" });
|
|
238
|
+
// A list of images is not an image: deco-cx/deco offered no variants there.
|
|
239
|
+
expect(gallery.anyOf).toBeUndefined();
|
|
240
|
+
expect(gallery.type).toBe("array");
|
|
241
|
+
});
|
|
216
242
|
});
|
|
@@ -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
|
+
});
|
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
EITRI_FORMAT_ALIASES,
|
|
9
9
|
WIDGET_TYPE_FORMATS,
|
|
10
10
|
applyWidgetFormat,
|
|
11
|
+
beautify,
|
|
11
12
|
definitionIdForPath,
|
|
12
13
|
normalizeFormats,
|
|
13
14
|
typeToJsonSchema,
|
|
@@ -70,7 +71,7 @@ describe("applyWidgetFormat", () => {
|
|
|
70
71
|
// returns {}. The intended widget must still be recovered.
|
|
71
72
|
const schema: any = {};
|
|
72
73
|
applyWidgetFormat(schema, "Color");
|
|
73
|
-
expect(schema).toEqual({ type: "string", format: "color" });
|
|
74
|
+
expect(schema).toEqual({ type: "string", format: "color-input" });
|
|
74
75
|
});
|
|
75
76
|
|
|
76
77
|
it.each(Object.entries(WIDGET_TYPE_FORMATS))(
|
|
@@ -85,7 +86,7 @@ describe("applyWidgetFormat", () => {
|
|
|
85
86
|
it("applies the format to a resolved string schema", () => {
|
|
86
87
|
const schema: any = { type: "string" };
|
|
87
88
|
applyWidgetFormat(schema, "Color");
|
|
88
|
-
expect(schema).toEqual({ type: "string", format: "color" });
|
|
89
|
+
expect(schema).toEqual({ type: "string", format: "color-input" });
|
|
89
90
|
});
|
|
90
91
|
|
|
91
92
|
it("does not overwrite a schema that resolved to a $ref", () => {
|
|
@@ -102,7 +103,7 @@ describe("applyWidgetFormat", () => {
|
|
|
102
103
|
});
|
|
103
104
|
|
|
104
105
|
describe("typeToJsonSchema with an unresolvable widget alias import", () => {
|
|
105
|
-
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", () => {
|
|
106
107
|
const project = new Project({
|
|
107
108
|
useInMemoryFileSystem: true,
|
|
108
109
|
compilerOptions: { skipLibCheck: true, noResolve: false },
|
|
@@ -129,7 +130,7 @@ describe("typeToJsonSchema with an unresolvable widget alias import", () => {
|
|
|
129
130
|
expect(schema.properties.textLeftColor).toEqual({
|
|
130
131
|
title: "Cor do Texto",
|
|
131
132
|
type: "string",
|
|
132
|
-
format: "color",
|
|
133
|
+
format: "color-input",
|
|
133
134
|
});
|
|
134
135
|
}, 30_000);
|
|
135
136
|
});
|
|
@@ -569,3 +570,59 @@ describe("typeToJsonSchema with RequestURLParam", () => {
|
|
|
569
570
|
expect(schema.properties.plain.type).toBe("string");
|
|
570
571
|
});
|
|
571
572
|
});
|
|
573
|
+
|
|
574
|
+
describe("default prop titles (deco-cx/deco beautify)", () => {
|
|
575
|
+
it.each([
|
|
576
|
+
["marginTop", "Margin Top"],
|
|
577
|
+
["hideUnavailableItems", "Hide Unavailable Items"],
|
|
578
|
+
["title", "Title"],
|
|
579
|
+
// Acronyms stay whole (deco-cx/deco split them letter by letter).
|
|
580
|
+
["CTA", "CTA"],
|
|
581
|
+
["titleCTA", "Title CTA"],
|
|
582
|
+
["taxID", "Tax ID"],
|
|
583
|
+
["jsonLDs", "Json L Ds"], // a plural acronym is ambiguous; same as upstream
|
|
584
|
+
["XMLHttpRequest", "XML Http Request"],
|
|
585
|
+
["RangePriceProps", "Range Price Props"],
|
|
586
|
+
])("%s → %s", (name, title) => {
|
|
587
|
+
expect(beautify(name)).toBe(title);
|
|
588
|
+
});
|
|
589
|
+
|
|
590
|
+
it("titles an un-annotated prop, keeps an explicit @title", () => {
|
|
591
|
+
const project = strictProject();
|
|
592
|
+
const sf = project.createSourceFile(
|
|
593
|
+
"/titles.ts",
|
|
594
|
+
`export interface Props {
|
|
595
|
+
marginTop?: number;
|
|
596
|
+
/** @title Custom */
|
|
597
|
+
noFlex?: boolean;
|
|
598
|
+
}`,
|
|
599
|
+
);
|
|
600
|
+
const schema = typeToJsonSchema(sf.getInterfaceOrThrow("Props").getType());
|
|
601
|
+
expect(schema.properties.marginTop.title).toBe("Margin Top");
|
|
602
|
+
expect(schema.properties.noFlex.title).toBe("Custom");
|
|
603
|
+
});
|
|
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);
|
|
@@ -132,6 +135,20 @@ export function definitionIdForPath(filePath: string, rootDir: string): string {
|
|
|
132
135
|
return toBase64(rel.startsWith("..") ? cleaned : rel);
|
|
133
136
|
}
|
|
134
137
|
|
|
138
|
+
/**
|
|
139
|
+
* Default title for a prop without `@title`, as deco-cx/deco derives it
|
|
140
|
+
* (`engine/schema/utils.ts`): words split at capitals, first character
|
|
141
|
+
* uppercased — `marginTop` → "Margin Top". Unlike deco-cx/deco, a run of
|
|
142
|
+
* capitals stays one word (`CTA` → "CTA", `titleCTA` → "Title CTA", where
|
|
143
|
+
* upstream gave " C T A") and a PascalCase name gets no leading space.
|
|
144
|
+
*/
|
|
145
|
+
export function beautify(propName: string): string {
|
|
146
|
+
return propName
|
|
147
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
148
|
+
.replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2")
|
|
149
|
+
.replace(/^./, (c) => c.toUpperCase());
|
|
150
|
+
}
|
|
151
|
+
|
|
135
152
|
/**
|
|
136
153
|
* Map JSDoc tags to JSON Schema 7 keywords.
|
|
137
154
|
* Supports all 20+ tags from deco-cx/deco.
|
|
@@ -219,7 +236,8 @@ export const WIDGET_TYPE_FORMATS: Record<string, string> = {
|
|
|
219
236
|
VideoWidget: "video-uri",
|
|
220
237
|
HTMLWidget: "html",
|
|
221
238
|
RichText: "rich-text",
|
|
222
|
-
|
|
239
|
+
// deco-cx/deco's widget format; the admin renders "color" the same way.
|
|
240
|
+
Color: "color-input",
|
|
223
241
|
Secret: "password",
|
|
224
242
|
TextArea: "textarea",
|
|
225
243
|
Code: "code",
|
|
@@ -255,6 +273,17 @@ export function normalizeFormats(node: unknown, aliases: Record<string, string>)
|
|
|
255
273
|
}
|
|
256
274
|
}
|
|
257
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
|
+
|
|
258
287
|
/**
|
|
259
288
|
* Detect known widget types and set the appropriate format.
|
|
260
289
|
*/
|
|
@@ -262,7 +291,7 @@ function applyWidgetDetection(schema: any, typeText: string): void {
|
|
|
262
291
|
if (schema.format) return;
|
|
263
292
|
|
|
264
293
|
for (const [widgetType, format] of Object.entries(WIDGET_TYPE_FORMATS)) {
|
|
265
|
-
if (typeText
|
|
294
|
+
if (mentionsWidget(typeText, widgetType)) {
|
|
266
295
|
schema.format = format;
|
|
267
296
|
return;
|
|
268
297
|
}
|
|
@@ -274,8 +303,8 @@ function applyWidgetDetection(schema: any, typeText: string): void {
|
|
|
274
303
|
* and union types by applying the format to the correct inner schema.
|
|
275
304
|
*/
|
|
276
305
|
export function applyWidgetFormat(schema: any, typeHint: string): void {
|
|
277
|
-
const matchedFormat = Object.entries(WIDGET_TYPE_FORMATS).find(
|
|
278
|
-
(
|
|
306
|
+
const matchedFormat = Object.entries(WIDGET_TYPE_FORMATS).find(([wt]) =>
|
|
307
|
+
mentionsWidget(typeHint, wt),
|
|
279
308
|
)?.[1];
|
|
280
309
|
|
|
281
310
|
if (!matchedFormat) {
|
|
@@ -523,6 +552,31 @@ export interface FlagBlock {
|
|
|
523
552
|
key: string;
|
|
524
553
|
title: string;
|
|
525
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
|
+
};
|
|
526
580
|
}
|
|
527
581
|
|
|
528
582
|
/**
|
|
@@ -545,6 +599,19 @@ export function extractFlagValueType(sourceFile: SourceFile): Type | null {
|
|
|
545
599
|
return value ? value.getTypeAtLocation(sourceFile) : null;
|
|
546
600
|
}
|
|
547
601
|
|
|
602
|
+
/**
|
|
603
|
+
* deco-cx/apps' typed multivariate flags over a widget type (annotation name).
|
|
604
|
+
* `website/flags/multivariate/image.ts` is `MultivariateFlag<ImageWidget>`.
|
|
605
|
+
*/
|
|
606
|
+
const BUILTIN_WIDGET_FLAGS = [
|
|
607
|
+
{
|
|
608
|
+
key: "website/flags/multivariate/image.ts",
|
|
609
|
+
title: "Image Variants",
|
|
610
|
+
widget: "ImageWidget",
|
|
611
|
+
valueSchema: { type: "string", format: "image-uri" },
|
|
612
|
+
},
|
|
613
|
+
];
|
|
614
|
+
|
|
548
615
|
/**
|
|
549
616
|
* A flag block as an `anyOf` branch of a prop it can fill, in deco-cx/deco's
|
|
550
617
|
* shape: inline, carrying its own `__resolveType`, with the variants schema
|
|
@@ -811,11 +878,11 @@ export function typeToJsonSchema(type: Type, visited = new Set<string>(), ctx?:
|
|
|
811
878
|
? {
|
|
812
879
|
type: "array",
|
|
813
880
|
items: { $ref: `#/definitions/${SECTION_REF_DEF_KEY}` },
|
|
814
|
-
title:
|
|
881
|
+
title: beautify(name),
|
|
815
882
|
}
|
|
816
883
|
: {
|
|
817
884
|
$ref: `#/definitions/${SECTION_REF_DEF_KEY}`,
|
|
818
|
-
title:
|
|
885
|
+
title: beautify(name),
|
|
819
886
|
};
|
|
820
887
|
if (prop.isOptional() || typeHint.includes("null") || typeHint.includes("undefined")) {
|
|
821
888
|
sectionSchema.nullable = true;
|
|
@@ -836,7 +903,7 @@ export function typeToJsonSchema(type: Type, visited = new Set<string>(), ctx?:
|
|
|
836
903
|
if (baseHint === "RequestURLParam" && nonNullTypes.every((t) => t.isString())) {
|
|
837
904
|
const paramSchema: any = {
|
|
838
905
|
$ref: `#/definitions/${REQUEST_URL_PARAM_DEF_KEY}`,
|
|
839
|
-
title:
|
|
906
|
+
title: beautify(name),
|
|
840
907
|
};
|
|
841
908
|
applyJsDocToSchema(paramSchema, tags);
|
|
842
909
|
properties[name] = paramSchema;
|
|
@@ -845,8 +912,43 @@ export function typeToJsonSchema(type: Type, visited = new Set<string>(), ctx?:
|
|
|
845
912
|
}
|
|
846
913
|
|
|
847
914
|
const outputTypeName = loaderTypeName(propType);
|
|
848
|
-
|
|
849
|
-
|
|
915
|
+
// By resolved type name, else by the annotation: `ImageWidget` is an
|
|
916
|
+
// alias of `string`, and TypeScript keeps no alias symbol on primitives.
|
|
917
|
+
// `Resolved<T>` props take only flags over `Resolved<T>`, others the rest.
|
|
918
|
+
const isResolved = /^Resolved\s*</.test(baseHint);
|
|
919
|
+
const matchingFlags = (
|
|
920
|
+
(outputTypeName ? ctx?.outputTypeToFlags?.get(outputTypeName) : undefined) ??
|
|
921
|
+
ctx?.outputTypeToFlags?.get(baseHint) ??
|
|
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
|
+
}
|
|
850
952
|
|
|
851
953
|
// Loader output type → block-ref: emit anyOf [Resolvable, ...matchingLoaders]
|
|
852
954
|
// baseHint strips "| null | undefined" so "ProductListingPage | null" → "ProductListingPage"
|
|
@@ -865,7 +967,7 @@ export function typeToJsonSchema(type: Type, visited = new Set<string>(), ctx?:
|
|
|
865
967
|
...matchingLoaders.map((k) => ({ $ref: `#/definitions/${toBase64(k)}` })),
|
|
866
968
|
...matchingFlags.map((f) => ({ $ref: `#/definitions/${f.defKey}` })),
|
|
867
969
|
],
|
|
868
|
-
title:
|
|
970
|
+
title: beautify(name),
|
|
869
971
|
};
|
|
870
972
|
if (prop.isOptional() || typeHint.includes("null") || typeHint.includes("undefined")) {
|
|
871
973
|
blockRefSchema.nullable = true;
|
|
@@ -886,7 +988,7 @@ export function typeToJsonSchema(type: Type, visited = new Set<string>(), ctx?:
|
|
|
886
988
|
schema.format = "password";
|
|
887
989
|
}
|
|
888
990
|
|
|
889
|
-
if (!schema.title) schema.title =
|
|
991
|
+
if (!schema.title) schema.title = beautify(name);
|
|
890
992
|
|
|
891
993
|
// A site flag over this prop's type → the value may also be that flag
|
|
892
994
|
// (`{ __resolveType: "site/flags/…", variants }`). Without the branch the
|
|
@@ -949,7 +1051,9 @@ export function typeToJsonSchema(type: Type, visited = new Set<string>(), ctx?:
|
|
|
949
1051
|
export function getJsDocTags(symbol: MorphSymbol): Record<string, string> {
|
|
950
1052
|
const tags: Record<string, string> = {};
|
|
951
1053
|
for (const decl of symbol.getDeclarations()) {
|
|
952
|
-
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() : [];
|
|
953
1057
|
for (const doc of jsDocs) {
|
|
954
1058
|
const desc = doc.getDescription().trim();
|
|
955
1059
|
if (desc) tags.description = desc;
|
|
@@ -1214,6 +1318,7 @@ function generateMeta(): MetaResponse {
|
|
|
1214
1318
|
const sectionsDir = path.resolve(root, SECTIONS_REL);
|
|
1215
1319
|
const loadersDir = path.resolve(root, LOADERS_REL);
|
|
1216
1320
|
const flagsDir = path.resolve(root, FLAGS_REL);
|
|
1321
|
+
const matchersDir = path.resolve(root, MATCHERS_REL);
|
|
1217
1322
|
const srcDir = path.join(root, "src");
|
|
1218
1323
|
|
|
1219
1324
|
const project = new Project({
|
|
@@ -1264,6 +1369,28 @@ function generateMeta(): MetaResponse {
|
|
|
1264
1369
|
// a `Product[]` inside a flag's value is a loader picker, not the whole
|
|
1265
1370
|
// inlined Product type.
|
|
1266
1371
|
// ---------------------------------------------------------------------------
|
|
1372
|
+
// deco-cx/apps' typed multivariate flags over widget types, which deco-cx/deco
|
|
1373
|
+
// offered on every field of that type (an image field could hold image
|
|
1374
|
+
// variants). Their value schema is the widget's own.
|
|
1375
|
+
for (const builtin of BUILTIN_WIDGET_FLAGS) {
|
|
1376
|
+
const flag: FlagBlock = {
|
|
1377
|
+
key: builtin.key,
|
|
1378
|
+
title: builtin.title,
|
|
1379
|
+
defKey: toBase64(builtin.key),
|
|
1380
|
+
};
|
|
1381
|
+
const existing = outputTypeToFlags.get(builtin.widget) ?? [];
|
|
1382
|
+
existing.push(flag);
|
|
1383
|
+
outputTypeToFlags.set(builtin.widget, existing);
|
|
1384
|
+
definitions[`${flag.defKey}@Props`] = multivariatePropsSchema(builtin.valueSchema);
|
|
1385
|
+
const { allOf, required, properties } = flagBranch(flag);
|
|
1386
|
+
definitions[flag.defKey] = { title: flag.title, type: "object", allOf, required, properties };
|
|
1387
|
+
flagBlocks[flag.key] = { $ref: `#/definitions/${flag.defKey}`, namespace: "website" };
|
|
1388
|
+
flagRootAnyOf.push({
|
|
1389
|
+
$ref: `#/definitions/${flag.defKey}`,
|
|
1390
|
+
inputSchema: `#/definitions/${flag.defKey}@Props`,
|
|
1391
|
+
});
|
|
1392
|
+
}
|
|
1393
|
+
|
|
1267
1394
|
const flagFiles = fs.existsSync(flagsDir) ? findTsxFiles(flagsDir) : [];
|
|
1268
1395
|
const siteFlags: Array<{ flag: FlagBlock; valueType: Type; valueTypeName: string | null }> = [];
|
|
1269
1396
|
for (const filePath of flagFiles) {
|
|
@@ -1282,6 +1409,7 @@ function generateMeta(): MetaResponse {
|
|
|
1282
1409
|
key: flagKey,
|
|
1283
1410
|
title: titleTag && !titleTag.includes("/") ? titleTag : flagKey,
|
|
1284
1411
|
defKey: toBase64(flagKey),
|
|
1412
|
+
resolved: /MultivariateProps\s*<\s*Resolved\s*</.test(sourceFile.getFullText()),
|
|
1285
1413
|
};
|
|
1286
1414
|
const valueTypeName = loaderTypeName(valueType);
|
|
1287
1415
|
siteFlags.push({ flag, valueType, valueTypeName });
|
|
@@ -1599,7 +1727,9 @@ function generateMeta(): MetaResponse {
|
|
|
1599
1727
|
],
|
|
1600
1728
|
}
|
|
1601
1729
|
: typeToJsonSchema(valueType, undefined, ctx);
|
|
1602
|
-
definitions[`${flag.defKey}@Props`] = multivariatePropsSchema(
|
|
1730
|
+
definitions[`${flag.defKey}@Props`] = multivariatePropsSchema(
|
|
1731
|
+
flag.resolved ? resolvedSchema(valueSchema) : valueSchema,
|
|
1732
|
+
);
|
|
1603
1733
|
const { allOf, required, properties } = flagBranch(flag);
|
|
1604
1734
|
definitions[flag.defKey] = { title: flag.title, type: "object", allOf, required, properties };
|
|
1605
1735
|
flagBlocks[flag.key] = { $ref: `#/definitions/${flag.defKey}`, namespace: SITE_NAMESPACE };
|
|
@@ -1881,6 +2011,59 @@ function generateMeta(): MetaResponse {
|
|
|
1881
2011
|
}
|
|
1882
2012
|
}
|
|
1883
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
|
+
|
|
1884
2067
|
// Eitri @format aliases → JSON-Schema formats (e.g. datetime → date-time).
|
|
1885
2068
|
// Done as a final pass over the generated definitions so every prop schema,
|
|
1886
2069
|
// however deeply nested, is normalized before it reaches Studio.
|
|
@@ -1901,6 +2084,7 @@ function generateMeta(): MetaResponse {
|
|
|
1901
2084
|
sections: sectionBlocks,
|
|
1902
2085
|
loaders: loaderBlocks,
|
|
1903
2086
|
flags: flagBlocks,
|
|
2087
|
+
matchers: matcherBlocks,
|
|
1904
2088
|
apps: appBlocks,
|
|
1905
2089
|
},
|
|
1906
2090
|
},
|
|
@@ -1912,7 +2096,7 @@ function generateMeta(): MetaResponse {
|
|
|
1912
2096
|
actions: emptyAnyOf,
|
|
1913
2097
|
pages: emptyAnyOf,
|
|
1914
2098
|
handlers: emptyAnyOf,
|
|
1915
|
-
matchers:
|
|
2099
|
+
matchers: { anyOf: matcherRootAnyOf },
|
|
1916
2100
|
flags: { anyOf: flagRootAnyOf },
|
|
1917
2101
|
functions: emptyAnyOf,
|
|
1918
2102
|
apps: { anyOf: appRootAnyOf },
|