@ikas/component-cli 1.4.0-beta.21 → 1.4.0-beta.210
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/build-tools.d.ts +37 -0
- package/dist/build-tools.d.ts.map +1 -0
- package/dist/build-tools.js +36 -0
- package/dist/build-tools.js.map +1 -0
- package/dist/commands/add-sections-to-page.d.ts +3 -0
- package/dist/commands/add-sections-to-page.d.ts.map +1 -0
- package/dist/commands/add-sections-to-page.js +39 -0
- package/dist/commands/add-sections-to-page.js.map +1 -0
- package/dist/commands/add.d.ts.map +1 -1
- package/dist/commands/add.js +11 -2
- package/dist/commands/add.js.map +1 -1
- package/dist/commands/build.d.ts.map +1 -1
- package/dist/commands/build.js +5 -165
- package/dist/commands/build.js.map +1 -1
- package/dist/commands/config.d.ts +35 -0
- package/dist/commands/config.d.ts.map +1 -1
- package/dist/commands/config.js +1082 -147
- package/dist/commands/config.js.map +1 -1
- package/dist/commands/create-design-tokens.d.ts +24 -0
- package/dist/commands/create-design-tokens.d.ts.map +1 -0
- package/dist/commands/create-design-tokens.js +334 -0
- package/dist/commands/create-design-tokens.js.map +1 -0
- package/dist/commands/create-global-variable.d.ts +3 -0
- package/dist/commands/create-global-variable.d.ts.map +1 -0
- package/dist/commands/create-global-variable.js +73 -0
- package/dist/commands/create-global-variable.js.map +1 -0
- package/dist/commands/create-page.d.ts +3 -0
- package/dist/commands/create-page.d.ts.map +1 -0
- package/dist/commands/create-page.js +31 -0
- package/dist/commands/create-page.js.map +1 -0
- package/dist/commands/delete-page.d.ts +3 -0
- package/dist/commands/delete-page.d.ts.map +1 -0
- package/dist/commands/delete-page.js +28 -0
- package/dist/commands/delete-page.js.map +1 -0
- package/dist/commands/delete-theme-globals.d.ts +4 -0
- package/dist/commands/delete-theme-globals.d.ts.map +1 -0
- package/dist/commands/delete-theme-globals.js +48 -0
- package/dist/commands/delete-theme-globals.js.map +1 -0
- package/dist/commands/dev.d.ts.map +1 -1
- package/dist/commands/dev.js +218 -31
- package/dist/commands/dev.js.map +1 -1
- package/dist/commands/enable-builtin-pages.d.ts +3 -0
- package/dist/commands/enable-builtin-pages.d.ts.map +1 -0
- package/dist/commands/enable-builtin-pages.js +27 -0
- package/dist/commands/enable-builtin-pages.js.map +1 -0
- package/dist/commands/get-component-props.d.ts +3 -0
- package/dist/commands/get-component-props.d.ts.map +1 -0
- package/dist/commands/get-component-props.js +32 -0
- package/dist/commands/get-component-props.js.map +1 -0
- package/dist/commands/get-page-by-type.d.ts +3 -0
- package/dist/commands/get-page-by-type.d.ts.map +1 -0
- package/dist/commands/get-page-by-type.js +25 -0
- package/dist/commands/get-page-by-type.js.map +1 -0
- package/dist/commands/get-section-values.d.ts +3 -0
- package/dist/commands/get-section-values.d.ts.map +1 -0
- package/dist/commands/get-section-values.js +39 -0
- package/dist/commands/get-section-values.js.map +1 -0
- package/dist/commands/list-builtin-pages.d.ts +3 -0
- package/dist/commands/list-builtin-pages.d.ts.map +1 -0
- package/dist/commands/list-builtin-pages.js +28 -0
- package/dist/commands/list-builtin-pages.js.map +1 -0
- package/dist/commands/list-entities.d.ts +3 -0
- package/dist/commands/list-entities.d.ts.map +1 -0
- package/dist/commands/list-entities.js +32 -0
- package/dist/commands/list-entities.js.map +1 -0
- package/dist/commands/list-page-sections.d.ts.map +1 -1
- package/dist/commands/list-page-sections.js +8 -2
- package/dist/commands/list-page-sections.js.map +1 -1
- package/dist/commands/list-theme-globals.d.ts +3 -0
- package/dist/commands/list-theme-globals.d.ts.map +1 -0
- package/dist/commands/list-theme-globals.js +22 -0
- package/dist/commands/list-theme-globals.js.map +1 -0
- package/dist/commands/move-page-section.d.ts +3 -0
- package/dist/commands/move-page-section.d.ts.map +1 -0
- package/dist/commands/move-page-section.js +62 -0
- package/dist/commands/move-page-section.js.map +1 -0
- package/dist/commands/publish-theme.d.ts +3 -0
- package/dist/commands/publish-theme.d.ts.map +1 -0
- package/dist/commands/publish-theme.js +29 -0
- package/dist/commands/publish-theme.js.map +1 -0
- package/dist/commands/publish.d.ts.map +1 -1
- package/dist/commands/publish.js +54 -3
- package/dist/commands/publish.js.map +1 -1
- package/dist/commands/remove-page-section.d.ts +3 -0
- package/dist/commands/remove-page-section.d.ts.map +1 -0
- package/dist/commands/remove-page-section.js +40 -0
- package/dist/commands/remove-page-section.js.map +1 -0
- package/dist/commands/search-products.d.ts +3 -0
- package/dist/commands/search-products.d.ts.map +1 -0
- package/dist/commands/search-products.js +40 -0
- package/dist/commands/search-products.js.map +1 -0
- package/dist/commands/update-builtin-page-prop.d.ts +3 -0
- package/dist/commands/update-builtin-page-prop.d.ts.map +1 -0
- package/dist/commands/update-builtin-page-prop.js +49 -0
- package/dist/commands/update-builtin-page-prop.js.map +1 -0
- package/dist/commands/update-global-variable.d.ts +3 -0
- package/dist/commands/update-global-variable.d.ts.map +1 -0
- package/dist/commands/update-global-variable.js +70 -0
- package/dist/commands/update-global-variable.js.map +1 -0
- package/dist/commands/update-page-sections.d.ts +3 -0
- package/dist/commands/update-page-sections.d.ts.map +1 -0
- package/dist/commands/update-page-sections.js +39 -0
- package/dist/commands/update-page-sections.js.map +1 -0
- package/dist/commands/update-section-prop.d.ts.map +1 -1
- package/dist/commands/update-section-prop.js +25 -8
- package/dist/commands/update-section-prop.js.map +1 -1
- package/dist/commands/upload-image.d.ts.map +1 -1
- package/dist/commands/upload-image.js +5 -46
- package/dist/commands/upload-image.js.map +1 -1
- package/dist/commands/upload-images.d.ts +3 -0
- package/dist/commands/upload-images.d.ts.map +1 -0
- package/dist/commands/upload-images.js +48 -0
- package/dist/commands/upload-images.js.map +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +54 -1
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +51 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/archive-project.d.ts +69 -0
- package/dist/utils/archive-project.d.ts.map +1 -0
- package/dist/utils/archive-project.js +202 -0
- package/dist/utils/archive-project.js.map +1 -0
- package/dist/utils/compile.d.ts +17 -2
- package/dist/utils/compile.d.ts.map +1 -1
- package/dist/utils/compile.js +750 -117
- package/dist/utils/compile.js.map +1 -1
- package/dist/utils/component-helpers.d.ts +167 -2
- package/dist/utils/component-helpers.d.ts.map +1 -1
- package/dist/utils/component-helpers.js +452 -29
- package/dist/utils/component-helpers.js.map +1 -1
- package/dist/utils/css-import-resolver.d.ts +4 -1
- package/dist/utils/css-import-resolver.d.ts.map +1 -1
- package/dist/utils/css-import-resolver.js +9 -1
- package/dist/utils/css-import-resolver.js.map +1 -1
- package/dist/utils/editor-action-client.d.ts +4 -3
- package/dist/utils/editor-action-client.d.ts.map +1 -1
- package/dist/utils/editor-action-client.js +55 -19
- package/dist/utils/editor-action-client.js.map +1 -1
- package/dist/utils/esm-transform.d.ts +30 -0
- package/dist/utils/esm-transform.d.ts.map +1 -1
- package/dist/utils/esm-transform.js +169 -2
- package/dist/utils/esm-transform.js.map +1 -1
- package/dist/utils/find-component-for-file.d.ts +12 -0
- package/dist/utils/find-component-for-file.d.ts.map +1 -0
- package/dist/utils/find-component-for-file.js +24 -0
- package/dist/utils/find-component-for-file.js.map +1 -0
- package/dist/utils/forbid-deep-imports.d.ts +3 -0
- package/dist/utils/forbid-deep-imports.d.ts.map +1 -0
- package/dist/utils/forbid-deep-imports.js +54 -0
- package/dist/utils/forbid-deep-imports.js.map +1 -0
- package/dist/utils/forbid-external-packages.d.ts +26 -0
- package/dist/utils/forbid-external-packages.d.ts.map +1 -0
- package/dist/utils/forbid-external-packages.js +99 -0
- package/dist/utils/forbid-external-packages.js.map +1 -0
- package/dist/utils/load-image.d.ts +16 -0
- package/dist/utils/load-image.d.ts.map +1 -0
- package/dist/utils/load-image.js +50 -0
- package/dist/utils/load-image.js.map +1 -0
- package/dist/utils/vendor-storefront.d.ts +13 -0
- package/dist/utils/vendor-storefront.d.ts.map +1 -0
- package/dist/utils/vendor-storefront.js +91 -0
- package/dist/utils/vendor-storefront.js.map +1 -0
- package/dist/utils/websocket-server.d.ts +119 -5
- package/dist/utils/websocket-server.d.ts.map +1 -1
- package/dist/utils/websocket-server.js +255 -44
- package/dist/utils/websocket-server.js.map +1 -1
- package/package.json +19 -2
- package/templates/add/section.css +11 -0
- package/dist/commands/create.d.ts +0 -4
- package/dist/commands/create.d.ts.map +0 -1
- package/dist/commands/create.js +0 -228
- package/dist/commands/create.js.map +0 -1
- package/dist/commands/proxy.d.ts +0 -39
- package/dist/commands/proxy.d.ts.map +0 -1
- package/dist/commands/proxy.js +0 -210
- package/dist/commands/proxy.js.map +0 -1
|
@@ -35,6 +35,95 @@ export function generateUniqueId() {
|
|
|
35
35
|
export function generateComponentId(projectId) {
|
|
36
36
|
return `${projectId}-${generateUniqueId()}`;
|
|
37
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* The editor's publish-id machinery (rewrite-asset-ids.ts, MIN_ROOT_ID_LENGTH = 6)
|
|
40
|
+
* treats a leading run of >=6 alphanumeric characters before a `-`/`_` separator as
|
|
41
|
+
* a strippable project prefix, and republish stability (insert-vs-version matching
|
|
42
|
+
* by source suffix) depends on every projectId satisfying that shape. A projectId
|
|
43
|
+
* that is shorter, or that contains a separator/non-alphanumeric character, shifts
|
|
44
|
+
* the prefix/suffix boundary and silently re-opens the duplicate-element bug.
|
|
45
|
+
* `generateProjectId()` always satisfies this; the check exists because
|
|
46
|
+
* ikas.config.json is hand-editable and parsed without a schema.
|
|
47
|
+
*/
|
|
48
|
+
export const PROJECT_ID_PATTERN = /^[A-Za-z0-9]{6,}$/;
|
|
49
|
+
export function isValidProjectId(projectId) {
|
|
50
|
+
return PROJECT_ID_PATTERN.test(projectId);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* A component id is publishable when the editor's prefix-strip machinery parses it
|
|
54
|
+
* unambiguously: either a legacy atomic id (alphanumeric only, no separators) or a
|
|
55
|
+
* `<projectId>-<suffix>` form whose PREFIX meets the same >=6 floor as PROJECT_ID_PATTERN.
|
|
56
|
+
* The prefix must stay >=6 alphanumeric with NO separator (the editor strips exactly one
|
|
57
|
+
* leading `>=MIN_ROOT_ID_LENGTH` run before the first `-`/`_`); a short/separator-bearing
|
|
58
|
+
* prefix shifts that boundary and re-opens the duplicate-element bug.
|
|
59
|
+
*
|
|
60
|
+
* The SUFFIX, however, may be multi-segment (`<projectId>-hero-slider`,
|
|
61
|
+
* `<projectId>-product-detail-add-to-cart`): older CLI configs used human-readable
|
|
62
|
+
* suffixes, and those ids are already placed on published themes so they cannot be
|
|
63
|
+
* renamed. The editor's `normalizeSourceElementId` canonicalizes every suffix separator
|
|
64
|
+
* to `_`, so a hyphenated local suffix and its underscore-sterilized published form
|
|
65
|
+
* normalize to the same republish-invariant key — multi-segment suffixes match stably.
|
|
66
|
+
* Each segment must still be a non-empty alphanumeric run (no trailing/doubled separators).
|
|
67
|
+
*/
|
|
68
|
+
export const COMPONENT_ID_PATTERN = /^(?:[A-Za-z0-9]+|[A-Za-z0-9]{6,}(?:[-_][A-Za-z0-9]+)+)$/;
|
|
69
|
+
/**
|
|
70
|
+
* Ids from the pre-projectId CLI (`<kebab-name>-<Date.now()>`) predate the prefix
|
|
71
|
+
* contract and are already placed on themes, so the prefix check below skips them.
|
|
72
|
+
*/
|
|
73
|
+
const LEGACY_TIMESTAMP_ID_PATTERN = /-\d{13}$/;
|
|
74
|
+
/**
|
|
75
|
+
* Besides the shape check, a separator-bearing id must carry THIS project's prefix.
|
|
76
|
+
* An id with another prefix was copied from somewhere else — typically the published
|
|
77
|
+
* id the editor reports for a component installed from a design asset. Syncing it
|
|
78
|
+
* creates a second copy of the published component in the partner theme and corrupts
|
|
79
|
+
* the asset's local→published id map on the next publish.
|
|
80
|
+
*/
|
|
81
|
+
export function findInvalidComponentIds(components, projectId) {
|
|
82
|
+
return components.flatMap((c) => {
|
|
83
|
+
if (!COMPONENT_ID_PATTERN.test(c.id)) {
|
|
84
|
+
return [{ id: c.id, name: c.name, reason: "malformed id" }];
|
|
85
|
+
}
|
|
86
|
+
const prefix = c.id.split(/[-_]/)[0];
|
|
87
|
+
const hasSeparator = prefix !== c.id;
|
|
88
|
+
if (projectId &&
|
|
89
|
+
hasSeparator &&
|
|
90
|
+
prefix !== projectId &&
|
|
91
|
+
!LEGACY_TIMESTAMP_ID_PATTERN.test(c.id)) {
|
|
92
|
+
return [
|
|
93
|
+
{
|
|
94
|
+
id: c.id,
|
|
95
|
+
name: c.name,
|
|
96
|
+
reason: `does not start with this project's id "${projectId}"`,
|
|
97
|
+
},
|
|
98
|
+
];
|
|
99
|
+
}
|
|
100
|
+
return [];
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Identity edits a running dev server must not take from a config reload: a changed
|
|
105
|
+
* projectId, or a component that kept its entry file but came back under another id.
|
|
106
|
+
* The editor reads an id that disappears mid-session as "component deleted" and removes
|
|
107
|
+
* the imported component, its wrapper section and its page placements from the theme.
|
|
108
|
+
* A component whose entry is gone from the config is a real removal and is not reported.
|
|
109
|
+
*/
|
|
110
|
+
export function findIdentityChanges(previous, reloaded) {
|
|
111
|
+
const changes = [];
|
|
112
|
+
if (previous.projectId && reloaded.projectId !== previous.projectId) {
|
|
113
|
+
changes.push(`projectId "${previous.projectId}" -> "${reloaded.projectId}"`);
|
|
114
|
+
}
|
|
115
|
+
const previousIdByEntry = new Map((previous.components ?? []).map((c) => [c.entry, c.id]));
|
|
116
|
+
for (const c of reloaded.components ?? []) {
|
|
117
|
+
const previousId = previousIdByEntry.get(c.entry);
|
|
118
|
+
if (previousId && previousId !== c.id) {
|
|
119
|
+
changes.push(`${c.name}: "${previousId}" -> "${c.id}"`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return changes;
|
|
123
|
+
}
|
|
124
|
+
/** Shared hint printed under an invalid-component-id error. */
|
|
125
|
+
export const COMPONENT_ID_HINT = "Component ids are generated by the CLI — restore the original values. Never hand-edit them, " +
|
|
126
|
+
"never copy an id shown by the editor into ikas.config.json, and do not change projectId to match.";
|
|
38
127
|
/**
|
|
39
128
|
* Validate that every entry in `input` is the id of a component currently in `components`.
|
|
40
129
|
* Returns the list of unknown entries (empty array means all valid).
|
|
@@ -96,8 +185,8 @@ export const PROP_TYPE_TO_TS = {
|
|
|
96
185
|
COLOR: "string",
|
|
97
186
|
PRODUCT: "IkasProduct | null",
|
|
98
187
|
PRODUCT_LIST: "IkasProductList",
|
|
99
|
-
PRODUCT_ATTRIBUTE: "
|
|
100
|
-
PRODUCT_ATTRIBUTE_LIST: "
|
|
188
|
+
PRODUCT_ATTRIBUTE: "IkasProductAttributeDetail | null",
|
|
189
|
+
PRODUCT_ATTRIBUTE_LIST: "IkasProductAttributeList",
|
|
101
190
|
CATEGORY: "IkasCategory | null",
|
|
102
191
|
CATEGORY_LIST: "IkasCategoryList",
|
|
103
192
|
BRAND: "IkasBrand | null",
|
|
@@ -111,6 +200,58 @@ export const PROP_TYPE_TO_TS = {
|
|
|
111
200
|
COMPONENT: "any",
|
|
112
201
|
COMPONENT_LIST: "any",
|
|
113
202
|
};
|
|
203
|
+
/**
|
|
204
|
+
* Pattern-element ("Font Style" / typography) enum sentinel. The editor builds
|
|
205
|
+
* this synthetic enum at theme time (createPatternElementEnum). Mirror of
|
|
206
|
+
* PATTERN_ELEMENT_ENUM_ID in @ikas/editor-models — kept local to avoid an
|
|
207
|
+
* editor-models dependency in the CLI.
|
|
208
|
+
*/
|
|
209
|
+
export const PATTERN_ELEMENT_ENUM_ID = "__patternElementEnum__";
|
|
210
|
+
/**
|
|
211
|
+
* Built-in enum type ids do not need to exist in config.customTypes: @ikas/*
|
|
212
|
+
* enums ship with the storefront models, and the pattern-element sentinel is
|
|
213
|
+
* resolved by the editor at theme time. Author-defined enums are NOT built-in.
|
|
214
|
+
*/
|
|
215
|
+
export function isBuiltInEnumTypeId(enumTypeId) {
|
|
216
|
+
return (enumTypeId.startsWith("@ikas/") || enumTypeId === PATTERN_ELEMENT_ENUM_ID);
|
|
217
|
+
}
|
|
218
|
+
const ENUM_OPTIONS_EXAMPLE = `'{"Small":"sm","Medium":"md"}' or '[{"name":"Small","value":"sm"}]'`;
|
|
219
|
+
/**
|
|
220
|
+
* The parsed `--options` of `config add-enum` / `config update-enum`, as the `{ "Label": "value" }`
|
|
221
|
+
* record an enum stores. The object form passes through unchanged. The array form
|
|
222
|
+
* `[{ "name": "Label", "value": "value" }]` (`label` is accepted for `name`) — which older project
|
|
223
|
+
* docs showed and agents still produce — is converted, with values stringified like every enum value.
|
|
224
|
+
*/
|
|
225
|
+
export function normalizeEnumOptionsInput(input) {
|
|
226
|
+
if (Array.isArray(input)) {
|
|
227
|
+
const options = {};
|
|
228
|
+
for (const item of input) {
|
|
229
|
+
if (!item || typeof item !== "object" || Array.isArray(item)) {
|
|
230
|
+
return { error: `Each option in the array must be an object { "name", "value" }, e.g. ${ENUM_OPTIONS_EXAMPLE}.` };
|
|
231
|
+
}
|
|
232
|
+
const { name, label, value } = item;
|
|
233
|
+
const optionLabel = name ?? label;
|
|
234
|
+
if (typeof optionLabel !== "string" || !optionLabel.trim()) {
|
|
235
|
+
return { error: `Every option needs a non-empty string "name", e.g. ${ENUM_OPTIONS_EXAMPLE}.` };
|
|
236
|
+
}
|
|
237
|
+
if ((typeof value !== "string" && typeof value !== "number") || String(value).trim() === "") {
|
|
238
|
+
return { error: `Option "${optionLabel}" needs a non-empty string "value", e.g. ${ENUM_OPTIONS_EXAMPLE}.` };
|
|
239
|
+
}
|
|
240
|
+
if (Object.prototype.hasOwnProperty.call(options, optionLabel)) {
|
|
241
|
+
return { error: `Duplicate option name "${optionLabel}".` };
|
|
242
|
+
}
|
|
243
|
+
options[optionLabel] = String(value);
|
|
244
|
+
}
|
|
245
|
+
if (Object.keys(options).length === 0) {
|
|
246
|
+
return { error: `Options must not be empty, e.g. ${ENUM_OPTIONS_EXAMPLE}.` };
|
|
247
|
+
}
|
|
248
|
+
return { options };
|
|
249
|
+
}
|
|
250
|
+
if (input && typeof input === "object" && Object.keys(input).length > 0) {
|
|
251
|
+
return { options: input };
|
|
252
|
+
}
|
|
253
|
+
return { error: `Options must be a non-empty JSON object or array, e.g. ${ENUM_OPTIONS_EXAMPLE}.` };
|
|
254
|
+
}
|
|
114
255
|
export function toKebabCase(str) {
|
|
115
256
|
return str
|
|
116
257
|
.replace(/([a-z])([A-Z])/g, "$1-$2")
|
|
@@ -149,14 +290,16 @@ function resolveTypeId(typeId) {
|
|
|
149
290
|
*/
|
|
150
291
|
function resolveCustomEnumTypeName(enumTypeId, editorTypes, customTypes) {
|
|
151
292
|
if (editorTypes) {
|
|
152
|
-
const editorType = editorTypes.find(t => t.id === enumTypeId);
|
|
153
|
-
if (editorType?.enumOptions &&
|
|
293
|
+
const editorType = editorTypes.find((t) => t.id === enumTypeId);
|
|
294
|
+
if (editorType?.enumOptions &&
|
|
295
|
+
Object.keys(editorType.enumOptions).length > 0) {
|
|
154
296
|
return toPascalCase(editorType.name);
|
|
155
297
|
}
|
|
156
298
|
}
|
|
157
299
|
if (customTypes) {
|
|
158
|
-
const customType = customTypes.find(t => t.id === enumTypeId);
|
|
159
|
-
if (customType?.enumOptions &&
|
|
300
|
+
const customType = customTypes.find((t) => t.id === enumTypeId);
|
|
301
|
+
if (customType?.enumOptions &&
|
|
302
|
+
Object.keys(customType.enumOptions).length > 0) {
|
|
160
303
|
return toPascalCase(customType.name);
|
|
161
304
|
}
|
|
162
305
|
}
|
|
@@ -176,7 +319,9 @@ export function generateTypesFile(componentName, props, type, editorTypes, custo
|
|
|
176
319
|
typeIdResolved.set(prop.name, resolved.isArray ? `${resolved.typeName}[]` : resolved.typeName);
|
|
177
320
|
}
|
|
178
321
|
}
|
|
179
|
-
else if (prop.type === "ENUM" &&
|
|
322
|
+
else if (prop.type === "ENUM" &&
|
|
323
|
+
prop.enumTypeId &&
|
|
324
|
+
prop.enumTypeId !== PATTERN_ELEMENT_ENUM_ID) {
|
|
180
325
|
const resolved = resolveTypeId(prop.enumTypeId);
|
|
181
326
|
if (resolved) {
|
|
182
327
|
// Built-in enum — import from @ikas/bp-storefront
|
|
@@ -193,6 +338,9 @@ export function generateTypesFile(componentName, props, type, editorTypes, custo
|
|
|
193
338
|
}
|
|
194
339
|
}
|
|
195
340
|
else {
|
|
341
|
+
// Non-ENUM props, plus the font-style (pattern-element) sentinel whose
|
|
342
|
+
// runtime value is a plain pattern-element id string → PROP_TYPE_TO_TS
|
|
343
|
+
// (PROP_TYPE_TO_TS["ENUM"] === "string"), no import emitted.
|
|
196
344
|
const tsType = PROP_TYPE_TO_TS[prop.type];
|
|
197
345
|
if (tsType?.includes("Ikas")) {
|
|
198
346
|
const matches = tsType.match(/Ikas\w+/g);
|
|
@@ -298,6 +446,96 @@ export function collectUsedCustomTypeIds(customTypes, components) {
|
|
|
298
446
|
}
|
|
299
447
|
return used;
|
|
300
448
|
}
|
|
449
|
+
/**
|
|
450
|
+
* Enum ids referenced by ENUM theme settings. Their union types must be emitted into
|
|
451
|
+
* `global-types.ts` even when no component prop uses the enum, so the setting's value-map
|
|
452
|
+
* entry (and `themeValue()`) can narrow to it.
|
|
453
|
+
*/
|
|
454
|
+
export function collectThemeSettingEnumIds(themeSettings) {
|
|
455
|
+
const ids = new Set();
|
|
456
|
+
for (const s of themeSettings || []) {
|
|
457
|
+
if (s.type === "ENUM" && s.enumTypeId)
|
|
458
|
+
ids.add(s.enumTypeId);
|
|
459
|
+
}
|
|
460
|
+
return ids;
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Map a global-variable kind to a TS type. Primitives narrowed; complex kinds stay `any`. An
|
|
464
|
+
* ENUM setting narrows to the union type THIS file declares for its enum: `resolveEnumTypeName`
|
|
465
|
+
* only knows the enums emitted above the theme block, so the map can never name an undeclared
|
|
466
|
+
* identifier. An ENUM whose enum is not emitted here — a deleted type, or a built-in `@ikas/*`
|
|
467
|
+
* enum (which the editor does not accept for a theme global, and whose import from
|
|
468
|
+
* `@ikas/bp-storefront` this generator cannot verify) — degrades to `string`.
|
|
469
|
+
*/
|
|
470
|
+
function themeSettingTsType(setting, resolveEnumTypeName) {
|
|
471
|
+
switch (setting.type) {
|
|
472
|
+
case "NUMBER":
|
|
473
|
+
return "number";
|
|
474
|
+
case "BOOLEAN":
|
|
475
|
+
return "boolean";
|
|
476
|
+
case "TEXT":
|
|
477
|
+
case "RICH_TEXT":
|
|
478
|
+
case "COLOR":
|
|
479
|
+
return "string";
|
|
480
|
+
case "ENUM":
|
|
481
|
+
return ((setting.enumTypeId && resolveEnumTypeName(setting.enumTypeId)) ||
|
|
482
|
+
"string");
|
|
483
|
+
default:
|
|
484
|
+
return "any"; // IMAGE, BORDER, SHADOW — object shapes can drift from runtime
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* Import line for the typed theme-globals wrapper. Must sit at the top of the generated
|
|
489
|
+
* file (after the header). Emitted only when there are theme globals.
|
|
490
|
+
*
|
|
491
|
+
* A module augmentation can't deliver key-safety here: the runtime `getThemeSettingValue`
|
|
492
|
+
* is declared `(name: string): any`, so an added narrower overload never wins resolution —
|
|
493
|
+
* unknown keys still match `string` and return `any`. A generated wrapper whose parameter
|
|
494
|
+
* is `ThemeSettingName` (no `string` overload) does reject unknown keys and narrow the value.
|
|
495
|
+
*/
|
|
496
|
+
const THEME_GLOBALS_IMPORT = `import { getThemeSettingValue as _getThemeSettingValueRaw } from "@ikas/bp-storefront";\n`;
|
|
497
|
+
/**
|
|
498
|
+
* Sentinel that marks a generated `global-types.ts` as containing the theme-globals block.
|
|
499
|
+
* This is the exported symbol authors import (stable public API), and it must stay a substring
|
|
500
|
+
* of what {@link generateThemeGlobalsBlock} emits — keep the two in sync if that output changes.
|
|
501
|
+
*/
|
|
502
|
+
const THEME_GLOBALS_MARKER = "export type ThemeSettingName";
|
|
503
|
+
/**
|
|
504
|
+
* Build the theme-globals block for `src/global-types.ts`: the key union, a value-type map,
|
|
505
|
+
* and a type-safe `themeValue` wrapper (authors import it from `./global-types`). The wrapper
|
|
506
|
+
* is deliberately named differently from the SDK's `getThemeSettingValue` so authors can't
|
|
507
|
+
* accidentally import the untyped SDK version and lose key-safety (no shadowing).
|
|
508
|
+
* Returns "" when there are no globals (an empty union would resolve to `never`). The import
|
|
509
|
+
* the wrapper needs is emitted separately via {@link THEME_GLOBALS_IMPORT}.
|
|
510
|
+
*/
|
|
511
|
+
function generateThemeGlobalsBlock(themeSettings, resolveEnumTypeName = () => null) {
|
|
512
|
+
if (!themeSettings || themeSettings.length === 0)
|
|
513
|
+
return "";
|
|
514
|
+
const sorted = [...themeSettings].sort((a, b) => a.name.localeCompare(b.name));
|
|
515
|
+
const unionLines = sorted
|
|
516
|
+
.map((s) => {
|
|
517
|
+
// Escape the JSDoc comment terminator to avoid prematurely closing the
|
|
518
|
+
// generated `/** ... */` block when a displayName contains `*/`.
|
|
519
|
+
const label = String(s.displayName).replace(/\*\//g, "*\\/");
|
|
520
|
+
return ` /** ${label} (${s.type}) */\n | ${JSON.stringify(s.name)}`;
|
|
521
|
+
})
|
|
522
|
+
.join("\n");
|
|
523
|
+
const mapLines = sorted
|
|
524
|
+
.map((s) => ` ${JSON.stringify(s.name)}: ${themeSettingTsType(s, resolveEnumTypeName)};`)
|
|
525
|
+
.join("\n");
|
|
526
|
+
return (`\n/** Stable keys of theme global variables defined in the editor's "Styles" panel. */\n` +
|
|
527
|
+
`export type ThemeSettingName =\n${unionLines};\n\n` +
|
|
528
|
+
`/** Narrowed value type per global key (primitives and ENUM unions; complex kinds are \`any\`). */\n` +
|
|
529
|
+
`type ThemeSettingValueMap = {\n${mapLines}\n};\n\n` +
|
|
530
|
+
`/**\n` +
|
|
531
|
+
` * Type-safe theme global accessor — only accepts keys declared in the editor's Styles panel.\n` +
|
|
532
|
+
` * Import this from \`./global-types\`, not \`getThemeSettingValue\` from \`@ikas/bp-storefront\`\n` +
|
|
533
|
+
` * (that one is untyped: it accepts any string and returns \`any\`).\n` +
|
|
534
|
+
` */\n` +
|
|
535
|
+
`export function themeValue<K extends ThemeSettingName>(name: K): ThemeSettingValueMap[K] {\n` +
|
|
536
|
+
` return _getThemeSettingValueRaw(name) as ThemeSettingValueMap[K];\n` +
|
|
537
|
+
`}\n`);
|
|
538
|
+
}
|
|
301
539
|
/**
|
|
302
540
|
* Generate the content for `src/global-types.ts`.
|
|
303
541
|
* Collects all custom enum types from editorTypes (preferred) or customTypes (fallback)
|
|
@@ -311,8 +549,11 @@ export function generateGlobalTypesFile(editorTypes, customTypes,
|
|
|
311
549
|
* Callers should compute this from `config.components[*].props[*].enumTypeId`
|
|
312
550
|
* so unused enums are excluded from `global-types.ts`.
|
|
313
551
|
*/
|
|
314
|
-
usedEnumIds) {
|
|
552
|
+
usedEnumIds, themeSettings) {
|
|
315
553
|
const header = `// Auto-generated global type definitions for ikas code components.\n// This file is regenerated automatically — do not edit manually.\n`;
|
|
554
|
+
// Enums an ENUM theme setting selects from are emitted regardless of `usedEnumIds`: the
|
|
555
|
+
// setting's value-map entry names them, so they must exist even if no prop uses them.
|
|
556
|
+
const forcedEnumIds = collectThemeSettingEnumIds(themeSettings);
|
|
316
557
|
// Collect all custom enum types with their options
|
|
317
558
|
// Use a Map keyed by type ID to deduplicate
|
|
318
559
|
const enumTypes = new Map();
|
|
@@ -320,10 +561,10 @@ usedEnumIds) {
|
|
|
320
561
|
if (editorTypes) {
|
|
321
562
|
for (const t of editorTypes) {
|
|
322
563
|
if (t.enumOptions && Object.keys(t.enumOptions).length > 0) {
|
|
323
|
-
// Skip built-in types
|
|
324
|
-
if (t.id
|
|
564
|
+
// Skip built-in types (@ikas/* and the pattern-element font-style sentinel)
|
|
565
|
+
if (isBuiltInEnumTypeId(t.id))
|
|
325
566
|
continue;
|
|
326
|
-
if (usedEnumIds && !usedEnumIds.has(t.id))
|
|
567
|
+
if (usedEnumIds && !usedEnumIds.has(t.id) && !forcedEnumIds.has(t.id))
|
|
327
568
|
continue;
|
|
328
569
|
enumTypes.set(t.id, {
|
|
329
570
|
name: toPascalCase(t.name),
|
|
@@ -339,9 +580,9 @@ usedEnumIds) {
|
|
|
339
580
|
const id = t.id;
|
|
340
581
|
if (enumTypes.has(id))
|
|
341
582
|
continue;
|
|
342
|
-
if (id
|
|
583
|
+
if (isBuiltInEnumTypeId(id))
|
|
343
584
|
continue;
|
|
344
|
-
if (usedEnumIds && !usedEnumIds.has(id))
|
|
585
|
+
if (usedEnumIds && !usedEnumIds.has(id) && !forcedEnumIds.has(id))
|
|
345
586
|
continue;
|
|
346
587
|
if (t.enumOptions && Object.keys(t.enumOptions).length > 0) {
|
|
347
588
|
enumTypes.set(id, {
|
|
@@ -352,17 +593,56 @@ usedEnumIds) {
|
|
|
352
593
|
}
|
|
353
594
|
}
|
|
354
595
|
}
|
|
596
|
+
// The theme block may only name enum types declared in THIS file (see themeSettingTsType).
|
|
597
|
+
const resolveEnumTypeName = (enumTypeId) => enumTypes.get(enumTypeId)?.name ?? null;
|
|
598
|
+
const themeBlock = generateThemeGlobalsBlock(themeSettings, resolveEnumTypeName);
|
|
599
|
+
// The wrapper's runtime import must sit at the top of the file (after the header).
|
|
600
|
+
const themeImport = themeBlock ? THEME_GLOBALS_IMPORT : "";
|
|
355
601
|
if (enumTypes.size === 0) {
|
|
356
|
-
|
|
602
|
+
if (!themeBlock) {
|
|
603
|
+
return `${header}// Custom enum types from the editor will appear here.\n\nexport {};\n`;
|
|
604
|
+
}
|
|
605
|
+
return `${header}${themeImport}${themeBlock}`;
|
|
357
606
|
}
|
|
358
607
|
// Sort by type name for stable output
|
|
359
608
|
const sorted = Array.from(enumTypes.values()).sort((a, b) => a.name.localeCompare(b.name));
|
|
360
|
-
let content = `${header}\n`;
|
|
609
|
+
let content = `${header}${themeImport}\n`;
|
|
361
610
|
for (const enumType of sorted) {
|
|
362
|
-
const unionType = enumType.options.map(v => `"${v}"`).join(" | ");
|
|
611
|
+
const unionType = enumType.options.map((v) => `"${v}"`).join(" | ");
|
|
363
612
|
content += `/** Enum type: ${enumType.id} */\nexport type ${enumType.name} = ${unionType};\n`;
|
|
364
613
|
}
|
|
365
|
-
return content
|
|
614
|
+
return `${content}${themeBlock}`;
|
|
615
|
+
}
|
|
616
|
+
/**
|
|
617
|
+
* Decide whether freshly generated `global-types.ts` content should overwrite
|
|
618
|
+
* the existing file on disk.
|
|
619
|
+
*
|
|
620
|
+
* Guards the dev-server startup race: before the editor connects, the theme
|
|
621
|
+
* settings cache is empty, so the generated content omits the theme-globals
|
|
622
|
+
* block (`themeValue`, `ThemeSettingName`). Writing it would clobber the rich
|
|
623
|
+
* file left from the previous session and break every component that imports
|
|
624
|
+
* those exports until the editor reconnects and re-syncs. While theme settings
|
|
625
|
+
* are not yet cached, preserve an existing theme-globals block instead of
|
|
626
|
+
* stubbing it out — the editor's `onSyncTypes` is the authoritative writer once
|
|
627
|
+
* connected.
|
|
628
|
+
*
|
|
629
|
+
* @param existing current file content, or null if the file is absent
|
|
630
|
+
* @param next freshly generated content
|
|
631
|
+
* @param hasThemeSettingsCached whether the editor has synced theme settings yet
|
|
632
|
+
*/
|
|
633
|
+
export function shouldWriteGlobalTypes(existing, next, hasThemeSettingsCached) {
|
|
634
|
+
// No file yet — always write so imports resolve.
|
|
635
|
+
if (existing === null)
|
|
636
|
+
return true;
|
|
637
|
+
// Identical content — skip to avoid file-watcher loops.
|
|
638
|
+
if (existing === next)
|
|
639
|
+
return false;
|
|
640
|
+
// Editor has synced theme settings — it's authoritative, write freely.
|
|
641
|
+
if (hasThemeSettingsCached)
|
|
642
|
+
return true;
|
|
643
|
+
// Not synced yet: write unless we'd drop an existing theme-globals block for a stub.
|
|
644
|
+
return (!existing.includes(THEME_GLOBALS_MARKER) ||
|
|
645
|
+
next.includes(THEME_GLOBALS_MARKER));
|
|
366
646
|
}
|
|
367
647
|
export function generateComponentFile(componentName, props, type) {
|
|
368
648
|
const propsDestructure = props.map((p) => p.name).join(", ");
|
|
@@ -392,6 +672,39 @@ export function updateBarrelExport(projectRoot, componentNames) {
|
|
|
392
672
|
.join("\n") + "\n";
|
|
393
673
|
fs.writeFileSync(indexPath, content);
|
|
394
674
|
}
|
|
675
|
+
/**
|
|
676
|
+
* A config visibility gate in wire form, or nothing at all — spread it.
|
|
677
|
+
*
|
|
678
|
+
* Config names the governing prop; the editor needs its id, and ids are minted here from position.
|
|
679
|
+
* A gate naming a prop this component does not have is dropped rather than passed on: the editor
|
|
680
|
+
* would show the control anyway, and a dangling id in the theme is worse than none.
|
|
681
|
+
*/
|
|
682
|
+
export function resolveCondition(gate, propIdByName) {
|
|
683
|
+
if (!gate?.requiredProp)
|
|
684
|
+
return {};
|
|
685
|
+
const requiredPropId = propIdByName.get(gate.requiredProp);
|
|
686
|
+
if (!requiredPropId)
|
|
687
|
+
return {};
|
|
688
|
+
// An ENUM gate carries its option and no boolean state; a BOOLEAN one the reverse. Emitting only
|
|
689
|
+
// the half that applies keeps a stale option from a re-typed governing prop out of the theme.
|
|
690
|
+
if (gate.requiredEnumOptionValue !== undefined) {
|
|
691
|
+
return {
|
|
692
|
+
requiredPropId,
|
|
693
|
+
requiredEnumOptionId: gate.requiredEnumOptionId,
|
|
694
|
+
requiredEnumOptionValue: gate.requiredEnumOptionValue,
|
|
695
|
+
};
|
|
696
|
+
}
|
|
697
|
+
return { requiredPropId, requiredPropValue: gate.requiredPropValue ?? true };
|
|
698
|
+
}
|
|
699
|
+
/**
|
|
700
|
+
* Config prop name to the id this component's props are broadcast under.
|
|
701
|
+
*
|
|
702
|
+
* The formula is duplicated at each mapping site (dev broadcast, config rebuild, publish); this
|
|
703
|
+
* reads it back off whatever that site produced rather than restating it a fourth time.
|
|
704
|
+
*/
|
|
705
|
+
export function propIdsByName(props) {
|
|
706
|
+
return new Map(props.map((prop) => [prop.name, prop.id]));
|
|
707
|
+
}
|
|
395
708
|
/**
|
|
396
709
|
* Convert flat config-format prop groups into an IThemePropGroup[] hierarchy.
|
|
397
710
|
*
|
|
@@ -405,11 +718,14 @@ export function updateBarrelExport(projectRoot, componentNames) {
|
|
|
405
718
|
*/
|
|
406
719
|
export function buildPropGroupHierarchy(componentId, propGroups, props) {
|
|
407
720
|
const result = [];
|
|
721
|
+
// A group names its governing prop; the leaf wrappers below carry no gate of their own, because a
|
|
722
|
+
// prop's gate travels on the prop itself (see `resolveCondition` at each prop mapping site).
|
|
723
|
+
const propIdByName = propIdsByName(props);
|
|
408
724
|
for (const group of propGroups) {
|
|
409
725
|
const groupGlobalId = `${componentId}_pg_${group.id}`;
|
|
410
726
|
// Collect props assigned to this group
|
|
411
|
-
const groupProps = props.filter(p => p.groupId === group.id);
|
|
412
|
-
const propChildren = groupProps.map(p => ({
|
|
727
|
+
const groupProps = props.filter((p) => p.groupId === group.id);
|
|
728
|
+
const propChildren = groupProps.map((p) => ({
|
|
413
729
|
id: `${componentId}_pgp_${p.id}`,
|
|
414
730
|
propId: p.id,
|
|
415
731
|
}));
|
|
@@ -419,8 +735,8 @@ export function buildPropGroupHierarchy(componentId, propGroups, props) {
|
|
|
419
735
|
for (const child of group.children) {
|
|
420
736
|
const childGlobalId = `${componentId}_pg_${child.id}`;
|
|
421
737
|
// Collect props assigned to this child group
|
|
422
|
-
const childProps = props.filter(p => p.groupId === child.id);
|
|
423
|
-
const childPropChildren = childProps.map(p => ({
|
|
738
|
+
const childProps = props.filter((p) => p.groupId === child.id);
|
|
739
|
+
const childPropChildren = childProps.map((p) => ({
|
|
424
740
|
id: `${componentId}_pgp_${p.id}`,
|
|
425
741
|
propId: p.id,
|
|
426
742
|
}));
|
|
@@ -428,7 +744,10 @@ export function buildPropGroupHierarchy(componentId, propGroups, props) {
|
|
|
428
744
|
id: childGlobalId,
|
|
429
745
|
name: child.name,
|
|
430
746
|
...(child.description ? { description: child.description } : {}),
|
|
431
|
-
...(
|
|
747
|
+
...resolveCondition(child, propIdByName),
|
|
748
|
+
...(childPropChildren.length > 0
|
|
749
|
+
? { children: childPropChildren }
|
|
750
|
+
: {}),
|
|
432
751
|
});
|
|
433
752
|
}
|
|
434
753
|
}
|
|
@@ -437,19 +756,117 @@ export function buildPropGroupHierarchy(componentId, propGroups, props) {
|
|
|
437
756
|
id: groupGlobalId,
|
|
438
757
|
name: group.name,
|
|
439
758
|
...(group.description ? { description: group.description } : {}),
|
|
759
|
+
...resolveCondition(group, propIdByName),
|
|
440
760
|
...(allChildren.length > 0 ? { children: allChildren } : {}),
|
|
441
761
|
});
|
|
442
762
|
}
|
|
443
763
|
return result;
|
|
444
764
|
}
|
|
765
|
+
/** Every group id under `group`, itself included — the groups a prop assigned "inside" it can sit in. */
|
|
766
|
+
export function groupIdsUnder(group) {
|
|
767
|
+
return new Set([
|
|
768
|
+
group.id,
|
|
769
|
+
...(group.children || []).map((child) => child.id),
|
|
770
|
+
]);
|
|
771
|
+
}
|
|
772
|
+
/**
|
|
773
|
+
* Validate a visibility condition — the `requiredProp` family on a prop or a prop group — against
|
|
774
|
+
* the component it lives on. Returns an error message, or null when it is sound (or absent).
|
|
775
|
+
*
|
|
776
|
+
* What is rejected, and why each one would otherwise strand a control with no way to reach it:
|
|
777
|
+
*
|
|
778
|
+
* - a name no prop on this component has, or a prop that is not BOOLEAN — nothing to read a
|
|
779
|
+
* true/false off, so the gate could never resolve;
|
|
780
|
+
* - a prop gating itself, or a chain of props that comes back around to it — every prop in the
|
|
781
|
+
* cycle stays hidden forever, including the toggles that would let someone out of it;
|
|
782
|
+
* - an ENUM gate with no option to wait for, or an option value hung on a BOOLEAN prop;
|
|
783
|
+
* - a group gated by a toggle that lives inside it — turning that toggle off takes the toggle away
|
|
784
|
+
* with the rest of the group.
|
|
785
|
+
*/
|
|
786
|
+
export function visibilityGateError(component, gate, where, opts = {}) {
|
|
787
|
+
const { requiredProp, requiredPropValue, requiredEnumOptionValue } = gate;
|
|
788
|
+
if (requiredProp === undefined || requiredProp === null) {
|
|
789
|
+
return requiredPropValue === undefined &&
|
|
790
|
+
requiredEnumOptionValue === undefined
|
|
791
|
+
? null
|
|
792
|
+
: `${where} declares a visibility state with no requiredProp to read it off. Name the prop that gates it, or drop both.`;
|
|
793
|
+
}
|
|
794
|
+
if (typeof requiredProp !== "string" || !requiredProp) {
|
|
795
|
+
return `requiredProp on ${where} must be the name of a BOOLEAN or ENUM prop on this component.`;
|
|
796
|
+
}
|
|
797
|
+
if (requiredPropValue !== undefined &&
|
|
798
|
+
typeof requiredPropValue !== "boolean") {
|
|
799
|
+
return `requiredPropValue on ${where} must be true or false (it defaults to true).`;
|
|
800
|
+
}
|
|
801
|
+
if (opts.selfPropName && requiredProp === opts.selfPropName) {
|
|
802
|
+
return `${where} cannot gate itself on "${requiredProp}".`;
|
|
803
|
+
}
|
|
804
|
+
const governing = component.props.find((prop) => prop.name === requiredProp);
|
|
805
|
+
if (!governing) {
|
|
806
|
+
const names = component.props.map((prop) => prop.name).join(", ");
|
|
807
|
+
return `requiredProp "${requiredProp}" on ${where} is not a prop of component "${component.name}". Available props: ${names || "(none)"}.`;
|
|
808
|
+
}
|
|
809
|
+
if (governing.type !== "BOOLEAN" && governing.type !== "ENUM") {
|
|
810
|
+
return `requiredProp "${requiredProp}" on ${where} is a ${governing.type} prop; only a BOOLEAN or ENUM prop can gate visibility.`;
|
|
811
|
+
}
|
|
812
|
+
// An ENUM gate is meaningless without the option it waits for: nothing would ever match it, so
|
|
813
|
+
// the node would never appear at all.
|
|
814
|
+
if (governing.type === "ENUM" &&
|
|
815
|
+
typeof requiredEnumOptionValue !== "string") {
|
|
816
|
+
return `requiredProp "${requiredProp}" on ${where} is an ENUM prop, so requiredEnumOptionValue must name the option value that shows it.`;
|
|
817
|
+
}
|
|
818
|
+
if (governing.type === "BOOLEAN" && requiredEnumOptionValue !== undefined) {
|
|
819
|
+
return `requiredEnumOptionValue on ${where} has no meaning against the BOOLEAN prop "${requiredProp}" — use requiredPropValue instead.`;
|
|
820
|
+
}
|
|
821
|
+
if (opts.group &&
|
|
822
|
+
governing.groupId &&
|
|
823
|
+
groupIdsUnder(opts.group).has(governing.groupId)) {
|
|
824
|
+
return `requiredProp "${requiredProp}" on ${where} sits inside that group — hiding the group would take that control with it, leaving nothing to bring it back.`;
|
|
825
|
+
}
|
|
826
|
+
// Walk the chain the governing prop hangs on. It cannot come back to this prop, and it cannot
|
|
827
|
+
// close on itself either — a cycle anywhere in it hides everything on it.
|
|
828
|
+
const byName = new Map(component.props.map((prop) => [prop.name, prop]));
|
|
829
|
+
const seen = new Set([requiredProp]);
|
|
830
|
+
let current = governing.requiredProp;
|
|
831
|
+
while (current) {
|
|
832
|
+
if (opts.selfPropName && current === opts.selfPropName) {
|
|
833
|
+
return `requiredProp "${requiredProp}" on ${where} depends on it in turn — the two would hide each other permanently.`;
|
|
834
|
+
}
|
|
835
|
+
if (seen.has(current)) {
|
|
836
|
+
return `requiredProp "${requiredProp}" on ${where} sits in a visibility cycle through "${current}" — everything on it would stay hidden.`;
|
|
837
|
+
}
|
|
838
|
+
seen.add(current);
|
|
839
|
+
current = byName.get(current)?.requiredProp;
|
|
840
|
+
}
|
|
841
|
+
return null;
|
|
842
|
+
}
|
|
445
843
|
/**
|
|
446
844
|
* Validate prop groups configuration for a component.
|
|
447
845
|
* Returns an array of error messages (empty = valid).
|
|
448
846
|
*/
|
|
449
847
|
export function validatePropGroups(component) {
|
|
450
848
|
const errors = [];
|
|
451
|
-
const propGroups = component.propGroups;
|
|
452
|
-
|
|
849
|
+
const propGroups = component.propGroups || [];
|
|
850
|
+
// A prop can name a visibility gate whether or not this component has groups at all, so gates are
|
|
851
|
+
// checked before the group walk bails out below.
|
|
852
|
+
for (const prop of component.props) {
|
|
853
|
+
const error = visibilityGateError(component, prop, `prop "${prop.name}"`, {
|
|
854
|
+
selfPropName: prop.name,
|
|
855
|
+
});
|
|
856
|
+
if (error)
|
|
857
|
+
errors.push(error);
|
|
858
|
+
}
|
|
859
|
+
for (const group of [
|
|
860
|
+
...propGroups,
|
|
861
|
+
...propGroups.flatMap((group) => group.children || []),
|
|
862
|
+
]) {
|
|
863
|
+
const error = visibilityGateError(component, group, `group "${group.id}"`, {
|
|
864
|
+
group,
|
|
865
|
+
});
|
|
866
|
+
if (error)
|
|
867
|
+
errors.push(error);
|
|
868
|
+
}
|
|
869
|
+
if (propGroups.length === 0)
|
|
453
870
|
return errors;
|
|
454
871
|
// Collect all group IDs and check uniqueness
|
|
455
872
|
const allIds = new Set();
|
|
@@ -528,7 +945,9 @@ export function movePropGroupInTree(propGroups, propGroupId, parentGroupId, inde
|
|
|
528
945
|
const found = findPropGroup(propGroups, propGroupId);
|
|
529
946
|
if (!found)
|
|
530
947
|
throw new Error(`Prop group "${propGroupId}" not found`);
|
|
531
|
-
if (parentGroupId &&
|
|
948
|
+
if (parentGroupId &&
|
|
949
|
+
found.group.children &&
|
|
950
|
+
found.group.children.length > 0) {
|
|
532
951
|
throw new Error(`Cannot nest "${propGroupId}": it already has child groups`);
|
|
533
952
|
}
|
|
534
953
|
if (parentGroupId) {
|
|
@@ -542,10 +961,10 @@ export function movePropGroupInTree(propGroups, propGroupId, parentGroupId, inde
|
|
|
542
961
|
}
|
|
543
962
|
let roots = propGroups;
|
|
544
963
|
if (found.parent) {
|
|
545
|
-
found.parent.children = (found.parent.children || []).filter(c => c.id !== propGroupId);
|
|
964
|
+
found.parent.children = (found.parent.children || []).filter((c) => c.id !== propGroupId);
|
|
546
965
|
}
|
|
547
966
|
else {
|
|
548
|
-
roots = roots.filter(g => g.id !== propGroupId);
|
|
967
|
+
roots = roots.filter((g) => g.id !== propGroupId);
|
|
549
968
|
}
|
|
550
969
|
if (parentGroupId) {
|
|
551
970
|
const target = findPropGroup(roots, parentGroupId);
|
|
@@ -553,13 +972,17 @@ export function movePropGroupInTree(propGroups, propGroupId, parentGroupId, inde
|
|
|
553
972
|
throw new Error(`Target parent group "${parentGroupId}" not found after removal`);
|
|
554
973
|
if (!target.group.children)
|
|
555
974
|
target.group.children = [];
|
|
556
|
-
const insertAt = typeof index === "number" &&
|
|
975
|
+
const insertAt = typeof index === "number" &&
|
|
976
|
+
index >= 0 &&
|
|
977
|
+
index <= target.group.children.length
|
|
557
978
|
? index
|
|
558
979
|
: target.group.children.length;
|
|
559
980
|
target.group.children.splice(insertAt, 0, found.group);
|
|
560
981
|
}
|
|
561
982
|
else {
|
|
562
|
-
const insertAt = typeof index === "number" && index >= 0 && index <= roots.length
|
|
983
|
+
const insertAt = typeof index === "number" && index >= 0 && index <= roots.length
|
|
984
|
+
? index
|
|
985
|
+
: roots.length;
|
|
563
986
|
roots.splice(insertAt, 0, found.group);
|
|
564
987
|
}
|
|
565
988
|
return roots;
|