@ikas/component-cli 1.4.0-beta.22 → 1.4.0-beta.221
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 +15 -167
- package/dist/commands/build.js.map +1 -1
- package/dist/commands/config-texts.d.ts +38 -0
- package/dist/commands/config-texts.d.ts.map +1 -0
- package/dist/commands/config-texts.js +186 -0
- package/dist/commands/config-texts.js.map +1 -0
- package/dist/commands/config.d.ts +45 -0
- package/dist/commands/config.d.ts.map +1 -1
- package/dist/commands/config.js +1148 -154
- 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 +388 -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 +50 -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 +308 -39
- 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 +28 -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/list-translations.d.ts +3 -0
- package/dist/commands/list-translations.d.ts.map +1 -0
- package/dist/commands/list-translations.js +33 -0
- package/dist/commands/list-translations.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 +65 -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/translation-languages.d.ts +3 -0
- package/dist/commands/translation-languages.d.ts.map +1 -0
- package/dist/commands/translation-languages.js +43 -0
- package/dist/commands/translation-languages.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/update-translations.d.ts +3 -0
- package/dist/commands/update-translations.d.ts.map +1 -0
- package/dist/commands/update-translations.js +37 -0
- package/dist/commands/update-translations.js.map +1 -0
- 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 +60 -1
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +77 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/archive-project.d.ts +50 -0
- package/dist/utils/archive-project.d.ts.map +1 -0
- package/dist/utils/archive-project.js +178 -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 +759 -117
- package/dist/utils/compile.js.map +1 -1
- package/dist/utils/component-helpers.d.ts +222 -4
- package/dist/utils/component-helpers.d.ts.map +1 -1
- package/dist/utils/component-helpers.js +558 -37
- 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/dev-server-auth.d.ts +19 -0
- package/dist/utils/dev-server-auth.d.ts.map +1 -0
- package/dist/utils/dev-server-auth.js +60 -0
- package/dist/utils/dev-server-auth.js.map +1 -0
- 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 +56 -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/i18n.d.ts +81 -0
- package/dist/utils/i18n.d.ts.map +1 -0
- package/dist/utils/i18n.js +268 -0
- package/dist/utils/i18n.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/storefront-t.d.ts +17 -0
- package/dist/utils/storefront-t.d.ts.map +1 -0
- package/dist/utils/storefront-t.js +187 -0
- package/dist/utils/storefront-t.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 +161 -7
- package/dist/utils/websocket-server.d.ts.map +1 -1
- package/dist/utils/websocket-server.js +324 -45
- 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,69 @@ 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 {
|
|
231
|
+
error: `Each option in the array must be an object { "name", "value" }, e.g. ${ENUM_OPTIONS_EXAMPLE}.`,
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
const { name, label, value } = item;
|
|
235
|
+
const optionLabel = name ?? label;
|
|
236
|
+
if (typeof optionLabel !== "string" || !optionLabel.trim()) {
|
|
237
|
+
return {
|
|
238
|
+
error: `Every option needs a non-empty string "name", e.g. ${ENUM_OPTIONS_EXAMPLE}.`,
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
if ((typeof value !== "string" && typeof value !== "number") ||
|
|
242
|
+
String(value).trim() === "") {
|
|
243
|
+
return {
|
|
244
|
+
error: `Option "${optionLabel}" needs a non-empty string "value", e.g. ${ENUM_OPTIONS_EXAMPLE}.`,
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
if (Object.prototype.hasOwnProperty.call(options, optionLabel)) {
|
|
248
|
+
return { error: `Duplicate option name "${optionLabel}".` };
|
|
249
|
+
}
|
|
250
|
+
options[optionLabel] = String(value);
|
|
251
|
+
}
|
|
252
|
+
if (Object.keys(options).length === 0) {
|
|
253
|
+
return {
|
|
254
|
+
error: `Options must not be empty, e.g. ${ENUM_OPTIONS_EXAMPLE}.`,
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
return { options };
|
|
258
|
+
}
|
|
259
|
+
if (input && typeof input === "object" && Object.keys(input).length > 0) {
|
|
260
|
+
return { options: input };
|
|
261
|
+
}
|
|
262
|
+
return {
|
|
263
|
+
error: `Options must be a non-empty JSON object or array, e.g. ${ENUM_OPTIONS_EXAMPLE}.`,
|
|
264
|
+
};
|
|
265
|
+
}
|
|
114
266
|
export function toKebabCase(str) {
|
|
115
267
|
return str
|
|
116
268
|
.replace(/([a-z])([A-Z])/g, "$1-$2")
|
|
@@ -149,14 +301,16 @@ function resolveTypeId(typeId) {
|
|
|
149
301
|
*/
|
|
150
302
|
function resolveCustomEnumTypeName(enumTypeId, editorTypes, customTypes) {
|
|
151
303
|
if (editorTypes) {
|
|
152
|
-
const editorType = editorTypes.find(t => t.id === enumTypeId);
|
|
153
|
-
if (editorType?.enumOptions &&
|
|
304
|
+
const editorType = editorTypes.find((t) => t.id === enumTypeId);
|
|
305
|
+
if (editorType?.enumOptions &&
|
|
306
|
+
Object.keys(editorType.enumOptions).length > 0) {
|
|
154
307
|
return toPascalCase(editorType.name);
|
|
155
308
|
}
|
|
156
309
|
}
|
|
157
310
|
if (customTypes) {
|
|
158
|
-
const customType = customTypes.find(t => t.id === enumTypeId);
|
|
159
|
-
if (customType?.enumOptions &&
|
|
311
|
+
const customType = customTypes.find((t) => t.id === enumTypeId);
|
|
312
|
+
if (customType?.enumOptions &&
|
|
313
|
+
Object.keys(customType.enumOptions).length > 0) {
|
|
160
314
|
return toPascalCase(customType.name);
|
|
161
315
|
}
|
|
162
316
|
}
|
|
@@ -176,7 +330,9 @@ export function generateTypesFile(componentName, props, type, editorTypes, custo
|
|
|
176
330
|
typeIdResolved.set(prop.name, resolved.isArray ? `${resolved.typeName}[]` : resolved.typeName);
|
|
177
331
|
}
|
|
178
332
|
}
|
|
179
|
-
else if (prop.type === "ENUM" &&
|
|
333
|
+
else if (prop.type === "ENUM" &&
|
|
334
|
+
prop.enumTypeId &&
|
|
335
|
+
prop.enumTypeId !== PATTERN_ELEMENT_ENUM_ID) {
|
|
180
336
|
const resolved = resolveTypeId(prop.enumTypeId);
|
|
181
337
|
if (resolved) {
|
|
182
338
|
// Built-in enum — import from @ikas/bp-storefront
|
|
@@ -193,6 +349,9 @@ export function generateTypesFile(componentName, props, type, editorTypes, custo
|
|
|
193
349
|
}
|
|
194
350
|
}
|
|
195
351
|
else {
|
|
352
|
+
// Non-ENUM props, plus the font-style (pattern-element) sentinel whose
|
|
353
|
+
// runtime value is a plain pattern-element id string → PROP_TYPE_TO_TS
|
|
354
|
+
// (PROP_TYPE_TO_TS["ENUM"] === "string"), no import emitted.
|
|
196
355
|
const tsType = PROP_TYPE_TO_TS[prop.type];
|
|
197
356
|
if (tsType?.includes("Ikas")) {
|
|
198
357
|
const matches = tsType.match(/Ikas\w+/g);
|
|
@@ -298,6 +457,96 @@ export function collectUsedCustomTypeIds(customTypes, components) {
|
|
|
298
457
|
}
|
|
299
458
|
return used;
|
|
300
459
|
}
|
|
460
|
+
/**
|
|
461
|
+
* Enum ids referenced by ENUM theme settings. Their union types must be emitted into
|
|
462
|
+
* `global-types.ts` even when no component prop uses the enum, so the setting's value-map
|
|
463
|
+
* entry (and `themeValue()`) can narrow to it.
|
|
464
|
+
*/
|
|
465
|
+
export function collectThemeSettingEnumIds(themeSettings) {
|
|
466
|
+
const ids = new Set();
|
|
467
|
+
for (const s of themeSettings || []) {
|
|
468
|
+
if (s.type === "ENUM" && s.enumTypeId)
|
|
469
|
+
ids.add(s.enumTypeId);
|
|
470
|
+
}
|
|
471
|
+
return ids;
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Map a global-variable kind to a TS type. Primitives narrowed; complex kinds stay `any`. An
|
|
475
|
+
* ENUM setting narrows to the union type THIS file declares for its enum: `resolveEnumTypeName`
|
|
476
|
+
* only knows the enums emitted above the theme block, so the map can never name an undeclared
|
|
477
|
+
* identifier. An ENUM whose enum is not emitted here — a deleted type, or a built-in `@ikas/*`
|
|
478
|
+
* enum (which the editor does not accept for a theme global, and whose import from
|
|
479
|
+
* `@ikas/bp-storefront` this generator cannot verify) — degrades to `string`.
|
|
480
|
+
*/
|
|
481
|
+
function themeSettingTsType(setting, resolveEnumTypeName) {
|
|
482
|
+
switch (setting.type) {
|
|
483
|
+
case "NUMBER":
|
|
484
|
+
return "number";
|
|
485
|
+
case "BOOLEAN":
|
|
486
|
+
return "boolean";
|
|
487
|
+
case "TEXT":
|
|
488
|
+
case "RICH_TEXT":
|
|
489
|
+
case "COLOR":
|
|
490
|
+
return "string";
|
|
491
|
+
case "ENUM":
|
|
492
|
+
return ((setting.enumTypeId && resolveEnumTypeName(setting.enumTypeId)) ||
|
|
493
|
+
"string");
|
|
494
|
+
default:
|
|
495
|
+
return "any"; // IMAGE, BORDER, SHADOW — object shapes can drift from runtime
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* Import line for the typed theme-globals wrapper. Must sit at the top of the generated
|
|
500
|
+
* file (after the header). Emitted only when there are theme globals.
|
|
501
|
+
*
|
|
502
|
+
* A module augmentation can't deliver key-safety here: the runtime `getThemeSettingValue`
|
|
503
|
+
* is declared `(name: string): any`, so an added narrower overload never wins resolution —
|
|
504
|
+
* unknown keys still match `string` and return `any`. A generated wrapper whose parameter
|
|
505
|
+
* is `ThemeSettingName` (no `string` overload) does reject unknown keys and narrow the value.
|
|
506
|
+
*/
|
|
507
|
+
const THEME_GLOBALS_IMPORT = `import { getThemeSettingValue as _getThemeSettingValueRaw } from "@ikas/bp-storefront";\n`;
|
|
508
|
+
/**
|
|
509
|
+
* Sentinel that marks a generated `global-types.ts` as containing the theme-globals block.
|
|
510
|
+
* This is the exported symbol authors import (stable public API), and it must stay a substring
|
|
511
|
+
* of what {@link generateThemeGlobalsBlock} emits — keep the two in sync if that output changes.
|
|
512
|
+
*/
|
|
513
|
+
const THEME_GLOBALS_MARKER = "export type ThemeSettingName";
|
|
514
|
+
/**
|
|
515
|
+
* Build the theme-globals block for `src/global-types.ts`: the key union, a value-type map,
|
|
516
|
+
* and a type-safe `themeValue` wrapper (authors import it from `./global-types`). The wrapper
|
|
517
|
+
* is deliberately named differently from the SDK's `getThemeSettingValue` so authors can't
|
|
518
|
+
* accidentally import the untyped SDK version and lose key-safety (no shadowing).
|
|
519
|
+
* Returns "" when there are no globals (an empty union would resolve to `never`). The import
|
|
520
|
+
* the wrapper needs is emitted separately via {@link THEME_GLOBALS_IMPORT}.
|
|
521
|
+
*/
|
|
522
|
+
function generateThemeGlobalsBlock(themeSettings, resolveEnumTypeName = () => null) {
|
|
523
|
+
if (!themeSettings || themeSettings.length === 0)
|
|
524
|
+
return "";
|
|
525
|
+
const sorted = [...themeSettings].sort((a, b) => a.name.localeCompare(b.name));
|
|
526
|
+
const unionLines = sorted
|
|
527
|
+
.map((s) => {
|
|
528
|
+
// Escape the JSDoc comment terminator to avoid prematurely closing the
|
|
529
|
+
// generated `/** ... */` block when a displayName contains `*/`.
|
|
530
|
+
const label = String(s.displayName).replace(/\*\//g, "*\\/");
|
|
531
|
+
return ` /** ${label} (${s.type}) */\n | ${JSON.stringify(s.name)}`;
|
|
532
|
+
})
|
|
533
|
+
.join("\n");
|
|
534
|
+
const mapLines = sorted
|
|
535
|
+
.map((s) => ` ${JSON.stringify(s.name)}: ${themeSettingTsType(s, resolveEnumTypeName)};`)
|
|
536
|
+
.join("\n");
|
|
537
|
+
return (`\n/** Stable keys of theme global variables defined in the editor's "Styles" panel. */\n` +
|
|
538
|
+
`export type ThemeSettingName =\n${unionLines};\n\n` +
|
|
539
|
+
`/** Narrowed value type per global key (primitives and ENUM unions; complex kinds are \`any\`). */\n` +
|
|
540
|
+
`type ThemeSettingValueMap = {\n${mapLines}\n};\n\n` +
|
|
541
|
+
`/**\n` +
|
|
542
|
+
` * Type-safe theme global accessor — only accepts keys declared in the editor's Styles panel.\n` +
|
|
543
|
+
` * Import this from \`./global-types\`, not \`getThemeSettingValue\` from \`@ikas/bp-storefront\`\n` +
|
|
544
|
+
` * (that one is untyped: it accepts any string and returns \`any\`).\n` +
|
|
545
|
+
` */\n` +
|
|
546
|
+
`export function themeValue<K extends ThemeSettingName>(name: K): ThemeSettingValueMap[K] {\n` +
|
|
547
|
+
` return _getThemeSettingValueRaw(name) as ThemeSettingValueMap[K];\n` +
|
|
548
|
+
`}\n`);
|
|
549
|
+
}
|
|
301
550
|
/**
|
|
302
551
|
* Generate the content for `src/global-types.ts`.
|
|
303
552
|
* Collects all custom enum types from editorTypes (preferred) or customTypes (fallback)
|
|
@@ -311,8 +560,11 @@ export function generateGlobalTypesFile(editorTypes, customTypes,
|
|
|
311
560
|
* Callers should compute this from `config.components[*].props[*].enumTypeId`
|
|
312
561
|
* so unused enums are excluded from `global-types.ts`.
|
|
313
562
|
*/
|
|
314
|
-
usedEnumIds) {
|
|
563
|
+
usedEnumIds, themeSettings) {
|
|
315
564
|
const header = `// Auto-generated global type definitions for ikas code components.\n// This file is regenerated automatically — do not edit manually.\n`;
|
|
565
|
+
// Enums an ENUM theme setting selects from are emitted regardless of `usedEnumIds`: the
|
|
566
|
+
// setting's value-map entry names them, so they must exist even if no prop uses them.
|
|
567
|
+
const forcedEnumIds = collectThemeSettingEnumIds(themeSettings);
|
|
316
568
|
// Collect all custom enum types with their options
|
|
317
569
|
// Use a Map keyed by type ID to deduplicate
|
|
318
570
|
const enumTypes = new Map();
|
|
@@ -320,10 +572,10 @@ usedEnumIds) {
|
|
|
320
572
|
if (editorTypes) {
|
|
321
573
|
for (const t of editorTypes) {
|
|
322
574
|
if (t.enumOptions && Object.keys(t.enumOptions).length > 0) {
|
|
323
|
-
// Skip built-in types
|
|
324
|
-
if (t.id
|
|
575
|
+
// Skip built-in types (@ikas/* and the pattern-element font-style sentinel)
|
|
576
|
+
if (isBuiltInEnumTypeId(t.id))
|
|
325
577
|
continue;
|
|
326
|
-
if (usedEnumIds && !usedEnumIds.has(t.id))
|
|
578
|
+
if (usedEnumIds && !usedEnumIds.has(t.id) && !forcedEnumIds.has(t.id))
|
|
327
579
|
continue;
|
|
328
580
|
enumTypes.set(t.id, {
|
|
329
581
|
name: toPascalCase(t.name),
|
|
@@ -339,9 +591,9 @@ usedEnumIds) {
|
|
|
339
591
|
const id = t.id;
|
|
340
592
|
if (enumTypes.has(id))
|
|
341
593
|
continue;
|
|
342
|
-
if (id
|
|
594
|
+
if (isBuiltInEnumTypeId(id))
|
|
343
595
|
continue;
|
|
344
|
-
if (usedEnumIds && !usedEnumIds.has(id))
|
|
596
|
+
if (usedEnumIds && !usedEnumIds.has(id) && !forcedEnumIds.has(id))
|
|
345
597
|
continue;
|
|
346
598
|
if (t.enumOptions && Object.keys(t.enumOptions).length > 0) {
|
|
347
599
|
enumTypes.set(id, {
|
|
@@ -352,17 +604,56 @@ usedEnumIds) {
|
|
|
352
604
|
}
|
|
353
605
|
}
|
|
354
606
|
}
|
|
607
|
+
// The theme block may only name enum types declared in THIS file (see themeSettingTsType).
|
|
608
|
+
const resolveEnumTypeName = (enumTypeId) => enumTypes.get(enumTypeId)?.name ?? null;
|
|
609
|
+
const themeBlock = generateThemeGlobalsBlock(themeSettings, resolveEnumTypeName);
|
|
610
|
+
// The wrapper's runtime import must sit at the top of the file (after the header).
|
|
611
|
+
const themeImport = themeBlock ? THEME_GLOBALS_IMPORT : "";
|
|
355
612
|
if (enumTypes.size === 0) {
|
|
356
|
-
|
|
613
|
+
if (!themeBlock) {
|
|
614
|
+
return `${header}// Custom enum types from the editor will appear here.\n\nexport {};\n`;
|
|
615
|
+
}
|
|
616
|
+
return `${header}${themeImport}${themeBlock}`;
|
|
357
617
|
}
|
|
358
618
|
// Sort by type name for stable output
|
|
359
619
|
const sorted = Array.from(enumTypes.values()).sort((a, b) => a.name.localeCompare(b.name));
|
|
360
|
-
let content = `${header}\n`;
|
|
620
|
+
let content = `${header}${themeImport}\n`;
|
|
361
621
|
for (const enumType of sorted) {
|
|
362
|
-
const unionType = enumType.options.map(v => `"${v}"`).join(" | ");
|
|
622
|
+
const unionType = enumType.options.map((v) => `"${v}"`).join(" | ");
|
|
363
623
|
content += `/** Enum type: ${enumType.id} */\nexport type ${enumType.name} = ${unionType};\n`;
|
|
364
624
|
}
|
|
365
|
-
return content
|
|
625
|
+
return `${content}${themeBlock}`;
|
|
626
|
+
}
|
|
627
|
+
/**
|
|
628
|
+
* Decide whether freshly generated `global-types.ts` content should overwrite
|
|
629
|
+
* the existing file on disk.
|
|
630
|
+
*
|
|
631
|
+
* Guards the dev-server startup race: before the editor connects, the theme
|
|
632
|
+
* settings cache is empty, so the generated content omits the theme-globals
|
|
633
|
+
* block (`themeValue`, `ThemeSettingName`). Writing it would clobber the rich
|
|
634
|
+
* file left from the previous session and break every component that imports
|
|
635
|
+
* those exports until the editor reconnects and re-syncs. While theme settings
|
|
636
|
+
* are not yet cached, preserve an existing theme-globals block instead of
|
|
637
|
+
* stubbing it out — the editor's `onSyncTypes` is the authoritative writer once
|
|
638
|
+
* connected.
|
|
639
|
+
*
|
|
640
|
+
* @param existing current file content, or null if the file is absent
|
|
641
|
+
* @param next freshly generated content
|
|
642
|
+
* @param hasThemeSettingsCached whether the editor has synced theme settings yet
|
|
643
|
+
*/
|
|
644
|
+
export function shouldWriteGlobalTypes(existing, next, hasThemeSettingsCached) {
|
|
645
|
+
// No file yet — always write so imports resolve.
|
|
646
|
+
if (existing === null)
|
|
647
|
+
return true;
|
|
648
|
+
// Identical content — skip to avoid file-watcher loops.
|
|
649
|
+
if (existing === next)
|
|
650
|
+
return false;
|
|
651
|
+
// Editor has synced theme settings — it's authoritative, write freely.
|
|
652
|
+
if (hasThemeSettingsCached)
|
|
653
|
+
return true;
|
|
654
|
+
// Not synced yet: write unless we'd drop an existing theme-globals block for a stub.
|
|
655
|
+
return (!existing.includes(THEME_GLOBALS_MARKER) ||
|
|
656
|
+
next.includes(THEME_GLOBALS_MARKER));
|
|
366
657
|
}
|
|
367
658
|
export function generateComponentFile(componentName, props, type) {
|
|
368
659
|
const propsDestructure = props.map((p) => p.name).join(", ");
|
|
@@ -380,18 +671,136 @@ export function generateStylesFile(componentName, type) {
|
|
|
380
671
|
return readTemplate(templateName, { KEBAB_NAME: kebabName });
|
|
381
672
|
}
|
|
382
673
|
/**
|
|
383
|
-
* Update the barrel export file (src/components/index.ts) to include all components
|
|
674
|
+
* Update the barrel export file (src/components/index.ts) to include all components. Each is exported by its
|
|
675
|
+
* entry's folder, which is named after the function it exports when created; its name may change later.
|
|
384
676
|
*/
|
|
385
|
-
export function updateBarrelExport(projectRoot,
|
|
677
|
+
export function updateBarrelExport(projectRoot, components) {
|
|
386
678
|
const indexPath = path.resolve(projectRoot, "src/components/index.ts");
|
|
387
|
-
const content =
|
|
388
|
-
.map((
|
|
389
|
-
const
|
|
390
|
-
return `export { ${
|
|
679
|
+
const content = components
|
|
680
|
+
.map((c) => {
|
|
681
|
+
const folder = path.basename(path.dirname(c.entry));
|
|
682
|
+
return `export { ${folder} } from "./${folder}/index";`;
|
|
391
683
|
})
|
|
392
684
|
.join("\n") + "\n";
|
|
393
685
|
fs.writeFileSync(indexPath, content);
|
|
394
686
|
}
|
|
687
|
+
/**
|
|
688
|
+
* A config visibility gate in wire form, or nothing at all — spread it.
|
|
689
|
+
*
|
|
690
|
+
* Config names the governing prop; the editor needs its id, and ids are minted here from position.
|
|
691
|
+
* A gate naming a prop this component does not have is dropped rather than passed on: the editor
|
|
692
|
+
* would show the control anyway, and a dangling id in the theme is worse than none.
|
|
693
|
+
*/
|
|
694
|
+
export function resolveCondition(gate, propIdByName) {
|
|
695
|
+
if (!gate?.requiredProp)
|
|
696
|
+
return {};
|
|
697
|
+
const requiredPropId = propIdByName.get(gate.requiredProp);
|
|
698
|
+
if (!requiredPropId)
|
|
699
|
+
return {};
|
|
700
|
+
// An ENUM gate carries its option and no boolean state; a BOOLEAN one the reverse. Emitting only
|
|
701
|
+
// the half that applies keeps a stale option from a re-typed governing prop out of the theme.
|
|
702
|
+
if (gate.requiredEnumOptionValue !== undefined) {
|
|
703
|
+
return {
|
|
704
|
+
requiredPropId,
|
|
705
|
+
requiredEnumOptionId: gate.requiredEnumOptionId,
|
|
706
|
+
requiredEnumOptionValue: gate.requiredEnumOptionValue,
|
|
707
|
+
};
|
|
708
|
+
}
|
|
709
|
+
return { requiredPropId, requiredPropValue: gate.requiredPropValue ?? true };
|
|
710
|
+
}
|
|
711
|
+
/**
|
|
712
|
+
* Config prop name to the id this component's props are broadcast under.
|
|
713
|
+
*
|
|
714
|
+
* The formula is duplicated at each mapping site (dev broadcast, config rebuild, publish); this
|
|
715
|
+
* reads it back off whatever that site produced rather than restating it a fourth time.
|
|
716
|
+
*/
|
|
717
|
+
export function propIdsByName(props) {
|
|
718
|
+
return new Map(props.map((prop) => [prop.name, prop.id]));
|
|
719
|
+
}
|
|
720
|
+
/**
|
|
721
|
+
* Sets a field, or removes it when emptied (a blank text, a map without entries, so the key leaves
|
|
722
|
+
* the config); a required field keeps its old value instead.
|
|
723
|
+
*/
|
|
724
|
+
function setField(target, field, value, required = false) {
|
|
725
|
+
const fields = target;
|
|
726
|
+
const empty = !value || (typeof value === "object" && Object.keys(value).length === 0);
|
|
727
|
+
if (!empty)
|
|
728
|
+
fields[field] = value;
|
|
729
|
+
else if (!required)
|
|
730
|
+
delete fields[field];
|
|
731
|
+
}
|
|
732
|
+
/**
|
|
733
|
+
* Writes the technical texts the editor saved back onto a config component: the base fields and the
|
|
734
|
+
* editor-language `translations`. A store language's `propValues` on the component stay as they are,
|
|
735
|
+
* unless the editor sent its store content too (a component's, not a section's).
|
|
736
|
+
*/
|
|
737
|
+
export function applyTechnicalTexts(component, texts) {
|
|
738
|
+
setField(component, "name", texts.name, true);
|
|
739
|
+
const locales = new Set([
|
|
740
|
+
...Object.keys(component.translations || {}),
|
|
741
|
+
...Object.keys(texts.translations),
|
|
742
|
+
...Object.keys(texts.propValues || {}),
|
|
743
|
+
]);
|
|
744
|
+
const translations = {};
|
|
745
|
+
locales.forEach((locale) => {
|
|
746
|
+
const { name: _name, propValues: configValues, ...rest } = component.translations?.[locale] || {};
|
|
747
|
+
const name = texts.translations[locale]?.name;
|
|
748
|
+
const propValues = texts.propValues
|
|
749
|
+
? texts.propValues[locale]
|
|
750
|
+
: configValues;
|
|
751
|
+
const record = {
|
|
752
|
+
...(name ? { name } : {}),
|
|
753
|
+
...rest,
|
|
754
|
+
...(propValues && Object.keys(propValues).length ? { propValues } : {}),
|
|
755
|
+
};
|
|
756
|
+
if (Object.keys(record).length > 0)
|
|
757
|
+
translations[locale] = record;
|
|
758
|
+
});
|
|
759
|
+
setField(component, "translations", translations);
|
|
760
|
+
const defaults = texts.defaults;
|
|
761
|
+
if (defaults)
|
|
762
|
+
component.props.forEach((prop) => {
|
|
763
|
+
if (!(prop.name in defaults))
|
|
764
|
+
return;
|
|
765
|
+
const value = defaults[prop.name];
|
|
766
|
+
if (value === null || value === undefined)
|
|
767
|
+
delete prop.defaultValue;
|
|
768
|
+
else
|
|
769
|
+
prop.defaultValue = value;
|
|
770
|
+
});
|
|
771
|
+
component.props.forEach((prop) => {
|
|
772
|
+
const text = texts.props[prop.name];
|
|
773
|
+
if (!text)
|
|
774
|
+
return;
|
|
775
|
+
setField(prop, "displayName", text.displayName, true);
|
|
776
|
+
setField(prop, "description", text.description);
|
|
777
|
+
setField(prop, "translations", text.translations);
|
|
778
|
+
});
|
|
779
|
+
// The editor knows a group by its global id; prop wrappers carry no texts of their own
|
|
780
|
+
(component.propGroups || [])
|
|
781
|
+
.flatMap((group) => [group, ...(group.children || [])])
|
|
782
|
+
.forEach((group) => {
|
|
783
|
+
const text = texts.propGroups[propGroupGlobalId(component.id, group.id)];
|
|
784
|
+
if (!text)
|
|
785
|
+
return;
|
|
786
|
+
setField(group, "name", text.name, true);
|
|
787
|
+
setField(group, "description", text.description);
|
|
788
|
+
setField(group, "translations", text.translations);
|
|
789
|
+
});
|
|
790
|
+
}
|
|
791
|
+
/**
|
|
792
|
+
* A config translation map (`{ en: { displayName: "Heading" } }`) as the editor's record list
|
|
793
|
+
* (`[{ locale: "en", displayName: "Heading" }]`); nothing for an empty or missing map, so the
|
|
794
|
+
* wire payload carries the key only when there is something to carry. Spread the result.
|
|
795
|
+
*/
|
|
796
|
+
export function translationRecords(translations) {
|
|
797
|
+
const records = Object.entries(translations || {})
|
|
798
|
+
.filter(([, fields]) => fields && Object.keys(fields).length > 0)
|
|
799
|
+
.map(([locale, fields]) => ({ locale, ...fields }));
|
|
800
|
+
return records.length > 0 ? { translations: records } : {};
|
|
801
|
+
}
|
|
802
|
+
/** The editor's id of a config prop group: the component's id prefixed, so it is unique across components. */
|
|
803
|
+
export const propGroupGlobalId = (componentId, groupId) => `${componentId}_pg_${groupId}`;
|
|
395
804
|
/**
|
|
396
805
|
* Convert flat config-format prop groups into an IThemePropGroup[] hierarchy.
|
|
397
806
|
*
|
|
@@ -405,11 +814,14 @@ export function updateBarrelExport(projectRoot, componentNames) {
|
|
|
405
814
|
*/
|
|
406
815
|
export function buildPropGroupHierarchy(componentId, propGroups, props) {
|
|
407
816
|
const result = [];
|
|
817
|
+
// A group names its governing prop; the leaf wrappers below carry no gate of their own, because a
|
|
818
|
+
// prop's gate travels on the prop itself (see `resolveCondition` at each prop mapping site).
|
|
819
|
+
const propIdByName = propIdsByName(props);
|
|
408
820
|
for (const group of propGroups) {
|
|
409
|
-
const groupGlobalId =
|
|
821
|
+
const groupGlobalId = propGroupGlobalId(componentId, group.id);
|
|
410
822
|
// Collect props assigned to this group
|
|
411
|
-
const groupProps = props.filter(p => p.groupId === group.id);
|
|
412
|
-
const propChildren = groupProps.map(p => ({
|
|
823
|
+
const groupProps = props.filter((p) => p.groupId === group.id);
|
|
824
|
+
const propChildren = groupProps.map((p) => ({
|
|
413
825
|
id: `${componentId}_pgp_${p.id}`,
|
|
414
826
|
propId: p.id,
|
|
415
827
|
}));
|
|
@@ -417,10 +829,10 @@ export function buildPropGroupHierarchy(componentId, propGroups, props) {
|
|
|
417
829
|
let nestedChildren = [];
|
|
418
830
|
if (group.children) {
|
|
419
831
|
for (const child of group.children) {
|
|
420
|
-
const childGlobalId =
|
|
832
|
+
const childGlobalId = propGroupGlobalId(componentId, child.id);
|
|
421
833
|
// Collect props assigned to this child group
|
|
422
|
-
const childProps = props.filter(p => p.groupId === child.id);
|
|
423
|
-
const childPropChildren = childProps.map(p => ({
|
|
834
|
+
const childProps = props.filter((p) => p.groupId === child.id);
|
|
835
|
+
const childPropChildren = childProps.map((p) => ({
|
|
424
836
|
id: `${componentId}_pgp_${p.id}`,
|
|
425
837
|
propId: p.id,
|
|
426
838
|
}));
|
|
@@ -428,7 +840,11 @@ export function buildPropGroupHierarchy(componentId, propGroups, props) {
|
|
|
428
840
|
id: childGlobalId,
|
|
429
841
|
name: child.name,
|
|
430
842
|
...(child.description ? { description: child.description } : {}),
|
|
431
|
-
...(
|
|
843
|
+
...translationRecords(child.translations),
|
|
844
|
+
...resolveCondition(child, propIdByName),
|
|
845
|
+
...(childPropChildren.length > 0
|
|
846
|
+
? { children: childPropChildren }
|
|
847
|
+
: {}),
|
|
432
848
|
});
|
|
433
849
|
}
|
|
434
850
|
}
|
|
@@ -437,19 +853,118 @@ export function buildPropGroupHierarchy(componentId, propGroups, props) {
|
|
|
437
853
|
id: groupGlobalId,
|
|
438
854
|
name: group.name,
|
|
439
855
|
...(group.description ? { description: group.description } : {}),
|
|
856
|
+
...translationRecords(group.translations),
|
|
857
|
+
...resolveCondition(group, propIdByName),
|
|
440
858
|
...(allChildren.length > 0 ? { children: allChildren } : {}),
|
|
441
859
|
});
|
|
442
860
|
}
|
|
443
861
|
return result;
|
|
444
862
|
}
|
|
863
|
+
/** Every group id under `group`, itself included — the groups a prop assigned "inside" it can sit in. */
|
|
864
|
+
export function groupIdsUnder(group) {
|
|
865
|
+
return new Set([
|
|
866
|
+
group.id,
|
|
867
|
+
...(group.children || []).map((child) => child.id),
|
|
868
|
+
]);
|
|
869
|
+
}
|
|
870
|
+
/**
|
|
871
|
+
* Validate a visibility condition — the `requiredProp` family on a prop or a prop group — against
|
|
872
|
+
* the component it lives on. Returns an error message, or null when it is sound (or absent).
|
|
873
|
+
*
|
|
874
|
+
* What is rejected, and why each one would otherwise strand a control with no way to reach it:
|
|
875
|
+
*
|
|
876
|
+
* - a name no prop on this component has, or a prop that is not BOOLEAN — nothing to read a
|
|
877
|
+
* true/false off, so the gate could never resolve;
|
|
878
|
+
* - a prop gating itself, or a chain of props that comes back around to it — every prop in the
|
|
879
|
+
* cycle stays hidden forever, including the toggles that would let someone out of it;
|
|
880
|
+
* - an ENUM gate with no option to wait for, or an option value hung on a BOOLEAN prop;
|
|
881
|
+
* - a group gated by a toggle that lives inside it — turning that toggle off takes the toggle away
|
|
882
|
+
* with the rest of the group.
|
|
883
|
+
*/
|
|
884
|
+
export function visibilityGateError(component, gate, where, opts = {}) {
|
|
885
|
+
const { requiredProp, requiredPropValue, requiredEnumOptionValue } = gate;
|
|
886
|
+
if (requiredProp === undefined || requiredProp === null) {
|
|
887
|
+
return requiredPropValue === undefined &&
|
|
888
|
+
requiredEnumOptionValue === undefined
|
|
889
|
+
? null
|
|
890
|
+
: `${where} declares a visibility state with no requiredProp to read it off. Name the prop that gates it, or drop both.`;
|
|
891
|
+
}
|
|
892
|
+
if (typeof requiredProp !== "string" || !requiredProp) {
|
|
893
|
+
return `requiredProp on ${where} must be the name of a BOOLEAN or ENUM prop on this component.`;
|
|
894
|
+
}
|
|
895
|
+
if (requiredPropValue !== undefined &&
|
|
896
|
+
typeof requiredPropValue !== "boolean") {
|
|
897
|
+
return `requiredPropValue on ${where} must be true or false (it defaults to true).`;
|
|
898
|
+
}
|
|
899
|
+
if (opts.selfPropName && requiredProp === opts.selfPropName) {
|
|
900
|
+
return `${where} cannot gate itself on "${requiredProp}".`;
|
|
901
|
+
}
|
|
902
|
+
const governing = component.props.find((prop) => prop.name === requiredProp);
|
|
903
|
+
if (!governing) {
|
|
904
|
+
const names = component.props.map((prop) => prop.name).join(", ");
|
|
905
|
+
return `requiredProp "${requiredProp}" on ${where} is not a prop of component "${component.name}". Available props: ${names || "(none)"}.`;
|
|
906
|
+
}
|
|
907
|
+
if (governing.type !== "BOOLEAN" && governing.type !== "ENUM") {
|
|
908
|
+
return `requiredProp "${requiredProp}" on ${where} is a ${governing.type} prop; only a BOOLEAN or ENUM prop can gate visibility.`;
|
|
909
|
+
}
|
|
910
|
+
// An ENUM gate is meaningless without the option it waits for: nothing would ever match it, so
|
|
911
|
+
// the node would never appear at all.
|
|
912
|
+
if (governing.type === "ENUM" &&
|
|
913
|
+
typeof requiredEnumOptionValue !== "string") {
|
|
914
|
+
return `requiredProp "${requiredProp}" on ${where} is an ENUM prop, so requiredEnumOptionValue must name the option value that shows it.`;
|
|
915
|
+
}
|
|
916
|
+
if (governing.type === "BOOLEAN" && requiredEnumOptionValue !== undefined) {
|
|
917
|
+
return `requiredEnumOptionValue on ${where} has no meaning against the BOOLEAN prop "${requiredProp}" — use requiredPropValue instead.`;
|
|
918
|
+
}
|
|
919
|
+
if (opts.group &&
|
|
920
|
+
governing.groupId &&
|
|
921
|
+
groupIdsUnder(opts.group).has(governing.groupId)) {
|
|
922
|
+
return `requiredProp "${requiredProp}" on ${where} sits inside that group — hiding the group would take that control with it, leaving nothing to bring it back.`;
|
|
923
|
+
}
|
|
924
|
+
// Walk the chain the governing prop hangs on. It cannot come back to this prop, and it cannot
|
|
925
|
+
// close on itself either — a cycle anywhere in it hides everything on it.
|
|
926
|
+
const byName = new Map(component.props.map((prop) => [prop.name, prop]));
|
|
927
|
+
const seen = new Set([requiredProp]);
|
|
928
|
+
let current = governing.requiredProp;
|
|
929
|
+
while (current) {
|
|
930
|
+
if (opts.selfPropName && current === opts.selfPropName) {
|
|
931
|
+
return `requiredProp "${requiredProp}" on ${where} depends on it in turn — the two would hide each other permanently.`;
|
|
932
|
+
}
|
|
933
|
+
if (seen.has(current)) {
|
|
934
|
+
return `requiredProp "${requiredProp}" on ${where} sits in a visibility cycle through "${current}" — everything on it would stay hidden.`;
|
|
935
|
+
}
|
|
936
|
+
seen.add(current);
|
|
937
|
+
current = byName.get(current)?.requiredProp;
|
|
938
|
+
}
|
|
939
|
+
return null;
|
|
940
|
+
}
|
|
445
941
|
/**
|
|
446
942
|
* Validate prop groups configuration for a component.
|
|
447
943
|
* Returns an array of error messages (empty = valid).
|
|
448
944
|
*/
|
|
449
945
|
export function validatePropGroups(component) {
|
|
450
946
|
const errors = [];
|
|
451
|
-
const propGroups = component.propGroups;
|
|
452
|
-
|
|
947
|
+
const propGroups = component.propGroups || [];
|
|
948
|
+
// A prop can name a visibility gate whether or not this component has groups at all, so gates are
|
|
949
|
+
// checked before the group walk bails out below.
|
|
950
|
+
for (const prop of component.props) {
|
|
951
|
+
const error = visibilityGateError(component, prop, `prop "${prop.name}"`, {
|
|
952
|
+
selfPropName: prop.name,
|
|
953
|
+
});
|
|
954
|
+
if (error)
|
|
955
|
+
errors.push(error);
|
|
956
|
+
}
|
|
957
|
+
for (const group of [
|
|
958
|
+
...propGroups,
|
|
959
|
+
...propGroups.flatMap((group) => group.children || []),
|
|
960
|
+
]) {
|
|
961
|
+
const error = visibilityGateError(component, group, `group "${group.id}"`, {
|
|
962
|
+
group,
|
|
963
|
+
});
|
|
964
|
+
if (error)
|
|
965
|
+
errors.push(error);
|
|
966
|
+
}
|
|
967
|
+
if (propGroups.length === 0)
|
|
453
968
|
return errors;
|
|
454
969
|
// Collect all group IDs and check uniqueness
|
|
455
970
|
const allIds = new Set();
|
|
@@ -528,7 +1043,9 @@ export function movePropGroupInTree(propGroups, propGroupId, parentGroupId, inde
|
|
|
528
1043
|
const found = findPropGroup(propGroups, propGroupId);
|
|
529
1044
|
if (!found)
|
|
530
1045
|
throw new Error(`Prop group "${propGroupId}" not found`);
|
|
531
|
-
if (parentGroupId &&
|
|
1046
|
+
if (parentGroupId &&
|
|
1047
|
+
found.group.children &&
|
|
1048
|
+
found.group.children.length > 0) {
|
|
532
1049
|
throw new Error(`Cannot nest "${propGroupId}": it already has child groups`);
|
|
533
1050
|
}
|
|
534
1051
|
if (parentGroupId) {
|
|
@@ -542,10 +1059,10 @@ export function movePropGroupInTree(propGroups, propGroupId, parentGroupId, inde
|
|
|
542
1059
|
}
|
|
543
1060
|
let roots = propGroups;
|
|
544
1061
|
if (found.parent) {
|
|
545
|
-
found.parent.children = (found.parent.children || []).filter(c => c.id !== propGroupId);
|
|
1062
|
+
found.parent.children = (found.parent.children || []).filter((c) => c.id !== propGroupId);
|
|
546
1063
|
}
|
|
547
1064
|
else {
|
|
548
|
-
roots = roots.filter(g => g.id !== propGroupId);
|
|
1065
|
+
roots = roots.filter((g) => g.id !== propGroupId);
|
|
549
1066
|
}
|
|
550
1067
|
if (parentGroupId) {
|
|
551
1068
|
const target = findPropGroup(roots, parentGroupId);
|
|
@@ -553,13 +1070,17 @@ export function movePropGroupInTree(propGroups, propGroupId, parentGroupId, inde
|
|
|
553
1070
|
throw new Error(`Target parent group "${parentGroupId}" not found after removal`);
|
|
554
1071
|
if (!target.group.children)
|
|
555
1072
|
target.group.children = [];
|
|
556
|
-
const insertAt = typeof index === "number" &&
|
|
1073
|
+
const insertAt = typeof index === "number" &&
|
|
1074
|
+
index >= 0 &&
|
|
1075
|
+
index <= target.group.children.length
|
|
557
1076
|
? index
|
|
558
1077
|
: target.group.children.length;
|
|
559
1078
|
target.group.children.splice(insertAt, 0, found.group);
|
|
560
1079
|
}
|
|
561
1080
|
else {
|
|
562
|
-
const insertAt = typeof index === "number" && index >= 0 && index <= roots.length
|
|
1081
|
+
const insertAt = typeof index === "number" && index >= 0 && index <= roots.length
|
|
1082
|
+
? index
|
|
1083
|
+
: roots.length;
|
|
563
1084
|
roots.splice(insertAt, 0, found.group);
|
|
564
1085
|
}
|
|
565
1086
|
return roots;
|