aem-eds-cli 1.0.0
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/LICENSE +52 -0
- package/bin/aem-eds.js +967 -0
- package/package.json +17 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
|
|
4
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
5
|
+
|
|
6
|
+
1. Definitions.
|
|
7
|
+
|
|
8
|
+
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined in Sections 1 through 9 of this document.
|
|
9
|
+
|
|
10
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
|
11
|
+
|
|
12
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
|
13
|
+
|
|
14
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
|
15
|
+
|
|
16
|
+
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
|
17
|
+
|
|
18
|
+
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
|
19
|
+
|
|
20
|
+
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
|
21
|
+
|
|
22
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
|
23
|
+
|
|
24
|
+
"Contribution" shall mean any work of authorship, including the original Work and any Derivative Works thereof, that is intentionally submitted to, or received by, Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work.
|
|
25
|
+
|
|
26
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
|
27
|
+
|
|
28
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
|
29
|
+
|
|
30
|
+
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work.
|
|
31
|
+
|
|
32
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
|
33
|
+
|
|
34
|
+
(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
|
35
|
+
|
|
36
|
+
(b) You must cause any modified files to carry prominent notices stating that You changed the files; and
|
|
37
|
+
|
|
38
|
+
(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
|
39
|
+
|
|
40
|
+
(d) If the Work includes a "NOTICE" text file, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file.
|
|
41
|
+
|
|
42
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contribution.
|
|
43
|
+
|
|
44
|
+
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
|
45
|
+
|
|
46
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE.
|
|
47
|
+
|
|
48
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work.
|
|
49
|
+
|
|
50
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License.
|
|
51
|
+
|
|
52
|
+
END OF TERMS AND CONDITIONS
|
package/bin/aem-eds.js
ADDED
|
@@ -0,0 +1,967 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* AEM EDS Block Scaffolder
|
|
5
|
+
*
|
|
6
|
+
* Commands:
|
|
7
|
+
* eds-scaffold create [block-name] Scaffold a new block
|
|
8
|
+
* eds-scaffold remove <block-name> Delete block folder (JSON goes with it)
|
|
9
|
+
* eds-scaffold list Show all blocks with status
|
|
10
|
+
*
|
|
11
|
+
* Flags:
|
|
12
|
+
* --dry-run Print what would be generated without writing any files
|
|
13
|
+
*
|
|
14
|
+
* Block-level JSON pattern:
|
|
15
|
+
* Each block owns blocks/<name>/_<name>.json containing its definitions,
|
|
16
|
+
* models and filters. Run `npm run build:json` (merge-json-cli in the
|
|
17
|
+
* boilerplate) to assemble the global component-*.json files.
|
|
18
|
+
* Never edit global files by hand.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
const fs = require('fs');
|
|
22
|
+
const path = require('path');
|
|
23
|
+
const readline = require('readline');
|
|
24
|
+
|
|
25
|
+
// ─── ANSI colours ─────────────────────────────────────────────────────────────
|
|
26
|
+
const c = {
|
|
27
|
+
reset: '\x1b[0m', bold: '\x1b[1m', dim: '\x1b[2m',
|
|
28
|
+
green: '\x1b[32m', cyan: '\x1b[36m', yellow: '\x1b[33m', red: '\x1b[31m',
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const log = {
|
|
32
|
+
info: (m) => console.log(` ${c.cyan}ℹ${c.reset} ${m}`),
|
|
33
|
+
success: (m) => console.log(` ${c.green}✔${c.reset} ${m}`),
|
|
34
|
+
warn: (m) => console.log(` ${c.yellow}⚠${c.reset} ${m}`),
|
|
35
|
+
error: (m) => console.log(` ${c.red}✖${c.reset} ${m}`),
|
|
36
|
+
dry: (m) => console.log(` ${c.yellow}~${c.reset} ${c.dim}[dry-run]${c.reset} ${m}`),
|
|
37
|
+
title: (m) => console.log(`\n${c.bold}${c.cyan}${m}${c.reset}\n`),
|
|
38
|
+
divider: () => console.log(` ${c.dim}${'─'.repeat(50)}${c.reset}`),
|
|
39
|
+
blank: () => console.log(''),
|
|
40
|
+
section: (m) => console.log(`\n ${c.bold}${m}${c.reset}\n`),
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// ─── Helpers ──────────────────────────────────────────────────────────────────
|
|
44
|
+
const toTitleCase = (s) => s.replace(/-/g, ' ').replace(/\b\w/g, (ch) => ch.toUpperCase());
|
|
45
|
+
const isValidName = (s) => /^[a-z][a-z0-9-]*$/.test(s);
|
|
46
|
+
const prompt = (rl, q) => new Promise((res) => rl.question(` ${q}`, res));
|
|
47
|
+
const confirm = async (rl, q, def = true) => {
|
|
48
|
+
const hint = def ? '[Y/n]' : '[y/N]';
|
|
49
|
+
const a = (await prompt(rl, `${q} ${c.dim}${hint}${c.reset} `)).trim().toLowerCase();
|
|
50
|
+
return !a ? def : a === 'y' || a === 'yes';
|
|
51
|
+
};
|
|
52
|
+
const readJSON = (p) => { try { return fs.existsSync(p) ? JSON.parse(fs.readFileSync(p, 'utf8')) : null; } catch { return null; } };
|
|
53
|
+
const tick = (v) => v ? `${c.green}✔${c.reset}` : `${c.red}✖${c.reset}`;
|
|
54
|
+
const col = (s, w) => String(s || '').padEnd(w).slice(0, w);
|
|
55
|
+
|
|
56
|
+
// ─── Field types ──────────────────────────────────────────────────────────────
|
|
57
|
+
const FIELD_TYPES = {
|
|
58
|
+
text: { label: 'Single-line text', valueType: 'string', multi: true },
|
|
59
|
+
textarea: { label: 'Multi-line plain text', valueType: 'string', multi: false },
|
|
60
|
+
richtext: { label: 'Rich text (HTML / WYSIWYG)', valueType: 'string', multi: false },
|
|
61
|
+
reference: { label: 'Image / Media (DAM asset)', valueType: 'string', multi: true },
|
|
62
|
+
'aem-content': { label: 'AEM page / content / link', valueType: 'string', multi: true },
|
|
63
|
+
'aem-content-fragment': { label: 'Content Fragment picker', valueType: 'string', multi: false },
|
|
64
|
+
'aem-experience-fragment': { label: 'Experience Fragment picker', valueType: 'string', multi: false },
|
|
65
|
+
boolean: { label: 'Boolean toggle (true / false)', valueType: 'boolean', multi: false },
|
|
66
|
+
select: { label: 'Dropdown — single value', valueType: 'string', multi: false },
|
|
67
|
+
multiselect: { label: 'Dropdown — multiple values', valueType: 'string[]', multi: false },
|
|
68
|
+
'radio-group': { label: 'Radio buttons — single value', valueType: 'string', multi: false },
|
|
69
|
+
'checkbox-group': { label: 'Checkboxes — multiple values', valueType: 'string[]', multi: false },
|
|
70
|
+
number: { label: 'Number input', valueType: 'number', multi: false },
|
|
71
|
+
'date-time': { label: 'Date / time picker', valueType: 'date', multi: false },
|
|
72
|
+
'aem-tag': { label: 'AEM Tag picker (cq:tags)', valueType: 'string', multi: false },
|
|
73
|
+
container: { label: 'Grouped / repeating field set', valueType: 'string', multi: false },
|
|
74
|
+
tab: { label: 'Tab — organises fields in panel', valueType: null, multi: false },
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const FIELD_GROUPS = [
|
|
78
|
+
{ title: 'Text inputs', keys: ['text', 'textarea', 'richtext'] },
|
|
79
|
+
{ title: 'Media & references', keys: ['reference', 'aem-content', 'aem-content-fragment', 'aem-experience-fragment'] },
|
|
80
|
+
{ title: 'Selection & toggles', keys: ['boolean', 'select', 'multiselect', 'radio-group', 'checkbox-group'] },
|
|
81
|
+
{ title: 'Specialised', keys: ['number', 'date-time', 'aem-tag'] },
|
|
82
|
+
{ title: 'Layout (no value)', keys: ['container', 'tab'] },
|
|
83
|
+
];
|
|
84
|
+
|
|
85
|
+
const ALL_FIELD_KEYS = Object.keys(FIELD_TYPES);
|
|
86
|
+
|
|
87
|
+
function printFieldTypes() {
|
|
88
|
+
let idx = 1;
|
|
89
|
+
for (const group of FIELD_GROUPS) {
|
|
90
|
+
console.log(`\n ${c.dim}── ${group.title} ${'─'.repeat(28 - group.title.length)}${c.reset}`);
|
|
91
|
+
for (const k of group.keys) {
|
|
92
|
+
const e = FIELD_TYPES[k];
|
|
93
|
+
const vt = e.valueType ? `${c.dim}[${e.valueType}]${c.reset}` : `${c.dim}[layout]${c.reset}`;
|
|
94
|
+
const mu = e.multi ? ` ${c.dim}(multi ok)${c.reset}` : '';
|
|
95
|
+
console.log(` ${c.dim}${String(idx).padStart(3)}.${c.reset} ${c.cyan}${k.padEnd(26)}${c.reset}${vt}${mu} ${c.dim}${e.label}${c.reset}`);
|
|
96
|
+
idx++;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
console.log('');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// ─── Validation config ────────────────────────────────────────────────────────
|
|
103
|
+
const VALIDATIONS = {
|
|
104
|
+
text: ['required', 'description', 'readOnly', 'hidden', 'minLength', 'maxLength'],
|
|
105
|
+
textarea: ['required', 'description', 'readOnly', 'hidden', 'minLength', 'maxLength'],
|
|
106
|
+
richtext: ['required', 'description', 'readOnly', 'hidden'],
|
|
107
|
+
reference: ['required', 'description', 'readOnly', 'hidden'],
|
|
108
|
+
'aem-content': ['required', 'description', 'readOnly', 'hidden', 'rootPath'],
|
|
109
|
+
'aem-content-fragment': ['required', 'description', 'readOnly', 'hidden', 'rootPath'],
|
|
110
|
+
'aem-experience-fragment':['required', 'description', 'readOnly', 'hidden'],
|
|
111
|
+
boolean: ['description', 'readOnly', 'hidden', 'customErrorMsg'],
|
|
112
|
+
select: ['required', 'description', 'readOnly', 'hidden'],
|
|
113
|
+
multiselect: ['required', 'description', 'readOnly', 'hidden'],
|
|
114
|
+
'radio-group': ['required', 'description', 'readOnly', 'hidden'],
|
|
115
|
+
'checkbox-group': ['required', 'description', 'readOnly', 'hidden'],
|
|
116
|
+
number: ['required', 'description', 'readOnly', 'hidden', 'min', 'max', 'step'],
|
|
117
|
+
'date-time': ['required', 'description', 'readOnly', 'hidden', 'min', 'max'],
|
|
118
|
+
'aem-tag': ['required', 'description', 'readOnly', 'hidden'],
|
|
119
|
+
container: ['description', 'readOnly', 'hidden'],
|
|
120
|
+
tab: [],
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
// ─── Field schema builder ─────────────────────────────────────────────────────
|
|
124
|
+
const OPTION_TYPES = new Set(['select', 'multiselect', 'radio-group', 'checkbox-group']);
|
|
125
|
+
|
|
126
|
+
function buildFieldSchema(fields) {
|
|
127
|
+
return fields.map((f) => {
|
|
128
|
+
const base = { component: f.type, name: f.name, label: f.label };
|
|
129
|
+
if (f.type !== 'tab') base.valueType = FIELD_TYPES[f.type]?.valueType || 'string';
|
|
130
|
+
if (f.required) base.required = true;
|
|
131
|
+
if (f.readOnly) base.readOnly = true;
|
|
132
|
+
if (f.hidden) base.hidden = true;
|
|
133
|
+
if (f.description) base.description = f.description;
|
|
134
|
+
if (f.multi) base.multi = true;
|
|
135
|
+
else if (f.type === 'reference') base.multi = false;
|
|
136
|
+
|
|
137
|
+
// Real options from collector (or placeholder fallback)
|
|
138
|
+
if (OPTION_TYPES.has(f.type)) {
|
|
139
|
+
base.options = f.options?.length
|
|
140
|
+
? f.options
|
|
141
|
+
: [{ name: 'Option 1', value: 'option-1' }, { name: 'Option 2', value: 'option-2' }];
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Default value
|
|
145
|
+
if (f.defaultValue !== undefined && f.defaultValue !== '') {
|
|
146
|
+
base.value = f.type === 'number' ? Number(f.defaultValue)
|
|
147
|
+
: f.type === 'boolean' ? (f.defaultValue === 'true' || f.defaultValue === true)
|
|
148
|
+
: f.defaultValue;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (f.type === 'date-time') { base.displayFormat = 'YYYY-MM-DD'; base.valueFormat = 'YYYY-MM-DD'; }
|
|
152
|
+
if (f.type === 'aem-content-fragment') base.variationName = '';
|
|
153
|
+
|
|
154
|
+
const validation = {};
|
|
155
|
+
if (f.minLength !== undefined) validation.minLength = f.minLength;
|
|
156
|
+
if (f.maxLength !== undefined) validation.maxLength = f.maxLength;
|
|
157
|
+
if (f.rootPath) validation.rootPath = f.rootPath;
|
|
158
|
+
if (f.customErrorMsg) validation.customErrorMsg = f.customErrorMsg;
|
|
159
|
+
if (Object.keys(validation).length) base.validation = validation;
|
|
160
|
+
|
|
161
|
+
// number / date-time constraints go TOP-LEVEL — not inside validation.
|
|
162
|
+
// UE maps these directly to HTML input attributes (min, max, step).
|
|
163
|
+
// Nesting them under validation means UE ignores them entirely.
|
|
164
|
+
if (f.min !== undefined) base.min = f.min;
|
|
165
|
+
if (f.max !== undefined) base.max = f.max;
|
|
166
|
+
if (f.step !== undefined) base.step = f.step;
|
|
167
|
+
|
|
168
|
+
if (f.type === 'container') {
|
|
169
|
+
base.collapsible = true;
|
|
170
|
+
base.fields = f.subFields?.length ? buildFieldSchema(f.subFields) : [];
|
|
171
|
+
}
|
|
172
|
+
return base;
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// ─── JS template — simple block ───────────────────────────────────────────────
|
|
177
|
+
function genSimpleBlockJS(blockName) {
|
|
178
|
+
return `export default function decorate(block) {
|
|
179
|
+
// TODO: implement ${blockName} block
|
|
180
|
+
}
|
|
181
|
+
`;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// ─── JS template — multi-row block ───────────────────────────────────────────
|
|
185
|
+
function genMultiRowBlockJS(blockName) {
|
|
186
|
+
return `export default function decorate(block) {
|
|
187
|
+
const rows = [...block.children];
|
|
188
|
+
rows.forEach((row) => {
|
|
189
|
+
// TODO: implement ${blockName} row decoration
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
`;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// ─── JS template — container block ───────────────────────────────────────────
|
|
196
|
+
function genContainerBlockJS(blockName, childName) {
|
|
197
|
+
return `export default function decorate(block) {
|
|
198
|
+
const [, ...itemRows] = [...block.children];
|
|
199
|
+
itemRows.forEach((item) => {
|
|
200
|
+
item.classList.add('${blockName}__item');
|
|
201
|
+
// TODO: implement ${childName} item decoration
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
`;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// ─── CSS template ─────────────────────────────────────────────────────────────
|
|
208
|
+
function genBlockCSS(blockName) {
|
|
209
|
+
return `.${blockName} {
|
|
210
|
+
display: block;
|
|
211
|
+
}
|
|
212
|
+
`;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// ─── README template ──────────────────────────────────────────────────────────
|
|
216
|
+
function genReadme(blockName, isContainer, parentFields, childName, childFields, needsVariants = false) {
|
|
217
|
+
const title = toTitleCase(blockName);
|
|
218
|
+
const childTitle = toTitleCase(childName);
|
|
219
|
+
|
|
220
|
+
// ── Field reference table ─────────────────────────────────────────────────
|
|
221
|
+
const buildTable = (fields, heading) => {
|
|
222
|
+
if (!fields.length) return `### ${heading}\n\n*No fields defined.*`;
|
|
223
|
+
const rows = fields.map((f) => {
|
|
224
|
+
const req = f.required ? ' *(required)*' : '';
|
|
225
|
+
const multi = f.multi ? ' *(multi)*' : '';
|
|
226
|
+
const opts = f.options?.length
|
|
227
|
+
? f.options.map((o) => `\`${o.value}\``).join(', ') : '—';
|
|
228
|
+
const defVal = f.defaultValue !== undefined
|
|
229
|
+
? `\`${f.defaultValue}\`` : '—';
|
|
230
|
+
const valid = [
|
|
231
|
+
f.minLength !== undefined && `minLength: ${f.minLength}`,
|
|
232
|
+
f.maxLength !== undefined && `maxLength: ${f.maxLength}`,
|
|
233
|
+
f.min !== undefined && `min: ${f.min}`,
|
|
234
|
+
f.max !== undefined && `max: ${f.max}`,
|
|
235
|
+
f.rootPath && `rootPath: ${f.rootPath}`,
|
|
236
|
+
].filter(Boolean).join(', ') || '—';
|
|
237
|
+
return `| ${f.label}${req} | \`${f.type}\`${multi} | \`${f.name}\` | ${opts} | ${defVal} | ${valid} |`;
|
|
238
|
+
}).join('\n');
|
|
239
|
+
return `### ${heading}\n\n| Label | Type | Property | Options | Default | Validation |\n|---|---|---|---|---|---|\n${rows}`;
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
// ── UE authoring steps ────────────────────────────────────────────────────
|
|
243
|
+
const simpleSteps = () => `
|
|
244
|
+
1. Open your page in **Universal Editor**
|
|
245
|
+
2. Click **+** inside a Section to open the block picker
|
|
246
|
+
3. Select **${title}** from the list
|
|
247
|
+
4. The block is added — click on it to open the **Properties panel**
|
|
248
|
+
5. Fill in the fields:
|
|
249
|
+
|
|
250
|
+
${parentFields.map((f, i) => {
|
|
251
|
+
const hint = f.type === 'reference'
|
|
252
|
+
? '→ opens DAM asset picker'
|
|
253
|
+
: f.type === 'aem-content'
|
|
254
|
+
? '→ opens page / link picker'
|
|
255
|
+
: f.type === 'select' || f.type === 'radio-group'
|
|
256
|
+
? `→ choose one of: ${(f.options||[]).map(o=>o.value).join(', ')}`
|
|
257
|
+
: f.type === 'boolean'
|
|
258
|
+
? '→ toggle on / off'
|
|
259
|
+
: f.type === 'richtext'
|
|
260
|
+
? '→ rich text editor opens inline'
|
|
261
|
+
: f.type === 'number'
|
|
262
|
+
? `→ numeric input${f.min !== undefined ? ` (${f.min}–${f.max})` : ''}`
|
|
263
|
+
: '';
|
|
264
|
+
return ` ${i + 1}. **${f.label}**${f.required ? ' *(required)*' : ''} ${hint}`;
|
|
265
|
+
}).join('\n')}
|
|
266
|
+
|
|
267
|
+
6. Click **✓ Save** in the Properties panel
|
|
268
|
+
7. Click **Publish** in the UE toolbar when ready`;
|
|
269
|
+
|
|
270
|
+
const containerSteps = () => `
|
|
271
|
+
**Step 1 — Add the ${title} block**
|
|
272
|
+
|
|
273
|
+
1. Open your page in **Universal Editor**
|
|
274
|
+
2. Click **+** inside a Section to open the block picker
|
|
275
|
+
3. Select **${title}** from the list
|
|
276
|
+
4. The block is added — click on it to open the **Properties panel**
|
|
277
|
+
${parentFields.length ? `5. Fill in the block config fields:
|
|
278
|
+
|
|
279
|
+
${parentFields.map((f, i) => {
|
|
280
|
+
const hint = f.type === 'number'
|
|
281
|
+
? `→ numeric input${f.min !== undefined ? ` (${f.min}–${f.max})` : ''}`
|
|
282
|
+
: f.type === 'boolean' ? '→ toggle on / off'
|
|
283
|
+
: f.type === 'select' ? `→ choose: ${(f.options||[]).map(o=>o.value).join(', ')}`
|
|
284
|
+
: '';
|
|
285
|
+
return ` ${i + 1}. **${f.label}**${f.required ? ' *(required)*' : ''} ${hint}`;
|
|
286
|
+
}).join('\n')}
|
|
287
|
+
6. Click **✓ Save**` : `5. This block has no config fields — proceed to adding child items`}
|
|
288
|
+
|
|
289
|
+
---
|
|
290
|
+
|
|
291
|
+
**Step 2 — Add ${childTitle} items**
|
|
292
|
+
|
|
293
|
+
1. Click **+** inside the **${title}** block *(not the section + button)*
|
|
294
|
+
2. Select **${childTitle}** from the picker
|
|
295
|
+
3. Click on the new item to open its **Properties panel**
|
|
296
|
+
4. Fill in the item fields:
|
|
297
|
+
|
|
298
|
+
${childFields.map((f, i) => {
|
|
299
|
+
const hint = f.type === 'reference'
|
|
300
|
+
? '→ opens DAM asset picker'
|
|
301
|
+
: f.type === 'aem-content'
|
|
302
|
+
? '→ opens page / link picker'
|
|
303
|
+
: f.type === 'richtext'
|
|
304
|
+
? '→ rich text editor opens inline'
|
|
305
|
+
: f.type === 'boolean'
|
|
306
|
+
? '→ toggle on / off'
|
|
307
|
+
: f.type === 'select' || f.type === 'radio-group'
|
|
308
|
+
? `→ choose one of: ${(f.options||[]).map(o=>o.value).join(', ')}`
|
|
309
|
+
: '';
|
|
310
|
+
return ` ${i + 1}. **${f.label}**${f.required ? ' *(required)*' : ''} ${hint}`;
|
|
311
|
+
}).join('\n')}
|
|
312
|
+
|
|
313
|
+
5. Click **✓ Save**
|
|
314
|
+
6. Repeat from step 1 to add more ${childTitle} items
|
|
315
|
+
7. Click **Publish** in the UE toolbar when ready`;
|
|
316
|
+
|
|
317
|
+
// ── Variants section ──────────────────────────────────────────────────────
|
|
318
|
+
const variantsSection = needsVariants ? `## Variants
|
|
319
|
+
|
|
320
|
+
Select the **${title}** block in UE → open the **Properties panel** → type variant
|
|
321
|
+
names in the **Variants (comma-separated)** field.
|
|
322
|
+
|
|
323
|
+
Each value maps directly to a CSS class on the block element:
|
|
324
|
+
|
|
325
|
+
| Variant value | CSS class applied | Example use |
|
|
326
|
+
|---|---|---|
|
|
327
|
+
| \`dark\` | \`.${blockName}.dark\` | Dark background |
|
|
328
|
+
| \`light\` | \`.${blockName}.light\` | Light background |
|
|
329
|
+
| \`compact\` | \`.${blockName}.compact\` | Reduced padding |
|
|
330
|
+
|
|
331
|
+
Multiple variants are supported: \`dark, compact\` → \`.${blockName}.dark.compact\`
|
|
332
|
+
|
|
333
|
+
> Add your own variants in \`${blockName}.css\`.
|
|
334
|
+
|
|
335
|
+
---` : `## Variants
|
|
336
|
+
|
|
337
|
+
This block was scaffolded without variant support.
|
|
338
|
+
To add variants later, add a \`classes\` text field to the model in \`_${blockName}.json\`
|
|
339
|
+
and rebuild with \`npm run build:json\`.
|
|
340
|
+
|
|
341
|
+
---`;
|
|
342
|
+
|
|
343
|
+
// ── Full README ───────────────────────────────────────────────────────────
|
|
344
|
+
return `# ${title} Block
|
|
345
|
+
|
|
346
|
+
${isContainer
|
|
347
|
+
? `Container block — a **${title}** holds one or more **${childTitle}** child items, each authored separately in Universal Editor.`
|
|
348
|
+
: `Simple block — all fields are authored together in the Universal Editor Properties panel.`}
|
|
349
|
+
|
|
350
|
+
---
|
|
351
|
+
|
|
352
|
+
## Authoring in Universal Editor
|
|
353
|
+
${isContainer ? containerSteps() : simpleSteps()}
|
|
354
|
+
|
|
355
|
+
---
|
|
356
|
+
|
|
357
|
+
## Field reference
|
|
358
|
+
|
|
359
|
+
${isContainer
|
|
360
|
+
? [
|
|
361
|
+
parentFields.length
|
|
362
|
+
? buildTable(parentFields, `${title} — config fields`)
|
|
363
|
+
: `### ${title} — config fields\n\n*No config fields on this block.*`,
|
|
364
|
+
buildTable(childFields, `${childTitle} — item fields`),
|
|
365
|
+
].join('\n\n')
|
|
366
|
+
: buildTable(parentFields, `${title} — fields`)}
|
|
367
|
+
|
|
368
|
+
---
|
|
369
|
+
|
|
370
|
+
${variantsSection}
|
|
371
|
+
|
|
372
|
+
---
|
|
373
|
+
|
|
374
|
+
## Developer notes
|
|
375
|
+
|
|
376
|
+
\`\`\`
|
|
377
|
+
blocks/${blockName}/
|
|
378
|
+
${blockName}.js ← decorate() — add implementation here
|
|
379
|
+
${blockName}.css ← styles
|
|
380
|
+
_${blockName}.json ← UE definitions, models, filters
|
|
381
|
+
README.md ← this file
|
|
382
|
+
\`\`\`
|
|
383
|
+
|
|
384
|
+
**Local preview:**
|
|
385
|
+
\`\`\`bash
|
|
386
|
+
aem up
|
|
387
|
+
# http://localhost:3000
|
|
388
|
+
\`\`\`
|
|
389
|
+
|
|
390
|
+
**After any JSON change:**
|
|
391
|
+
\`\`\`bash
|
|
392
|
+
npm run build:json && git add . && git commit -m "feat: update ${blockName}" && git push
|
|
393
|
+
\`\`\`
|
|
394
|
+
`;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// ─── Options builder (select / multiselect / radio-group / checkbox-group) ────
|
|
398
|
+
async function collectOptions(rl, fieldName, ind = '') {
|
|
399
|
+
const options = [];
|
|
400
|
+
log.blank();
|
|
401
|
+
log.info(`Define options for ${c.bold}${fieldName}${c.reset}. Leave name blank to finish.`);
|
|
402
|
+
log.blank();
|
|
403
|
+
|
|
404
|
+
while (true) {
|
|
405
|
+
const name = (await prompt(rl, `${ind} Option label (blank to finish): `)).trim();
|
|
406
|
+
if (!name) break;
|
|
407
|
+
|
|
408
|
+
// Auto-suggest a value from the label
|
|
409
|
+
const suggested = name.toLowerCase().replace(/\s+/g, '-').replace(/[^a-z0-9-]/g, '');
|
|
410
|
+
const raw = (await prompt(rl, `${ind} Option value [${suggested}]: `)).trim();
|
|
411
|
+
const value = raw || suggested;
|
|
412
|
+
|
|
413
|
+
options.push({ name, value });
|
|
414
|
+
log.success(` Option added: ${c.bold}${name}${c.reset} → ${c.dim}${value}${c.reset}`);
|
|
415
|
+
log.blank();
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
if (options.length === 0) {
|
|
419
|
+
options.push({ name: 'Option 1', value: 'option-1' }, { name: 'Option 2', value: 'option-2' });
|
|
420
|
+
log.warn('No options entered — added placeholders. Update them in component-models.json.');
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
log.info(`${options.length} option(s) defined for "${fieldName}".`);
|
|
424
|
+
log.blank();
|
|
425
|
+
return options;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
// ─── Default value collector ──────────────────────────────────────────────────
|
|
429
|
+
// Types that support a meaningful default value
|
|
430
|
+
const DEFAULT_VALUE_TYPES = new Set([
|
|
431
|
+
'text', 'textarea', 'richtext', 'number', 'boolean',
|
|
432
|
+
'select', 'multiselect', 'radio-group', 'checkbox-group',
|
|
433
|
+
'date-time', 'aem-tag',
|
|
434
|
+
]);
|
|
435
|
+
|
|
436
|
+
async function collectDefaultValue(rl, field, ind = '') {
|
|
437
|
+
if (!DEFAULT_VALUE_TYPES.has(field.type)) return;
|
|
438
|
+
|
|
439
|
+
let question = '';
|
|
440
|
+
|
|
441
|
+
switch (field.type) {
|
|
442
|
+
case 'boolean':
|
|
443
|
+
question = `${ind} Default value for "${field.name}" (true / false) [false]: `;
|
|
444
|
+
break;
|
|
445
|
+
case 'number':
|
|
446
|
+
question = `${ind} Default number for "${field.name}" (Enter to skip): `;
|
|
447
|
+
break;
|
|
448
|
+
case 'select':
|
|
449
|
+
case 'radio-group': {
|
|
450
|
+
if (field.options?.length) {
|
|
451
|
+
const opts = field.options.map((o) => `${c.cyan}${o.value}${c.reset}`).join(' | ');
|
|
452
|
+
console.log(`\n ${c.dim}Available values: ${opts}${c.reset}`);
|
|
453
|
+
}
|
|
454
|
+
question = `${ind} Default selected value for "${field.name}" (Enter to skip): `;
|
|
455
|
+
break;
|
|
456
|
+
}
|
|
457
|
+
case 'multiselect':
|
|
458
|
+
case 'checkbox-group': {
|
|
459
|
+
if (field.options?.length) {
|
|
460
|
+
const opts = field.options.map((o) => `${c.cyan}${o.value}${c.reset}`).join(' | ');
|
|
461
|
+
console.log(`\n ${c.dim}Available values: ${opts}${c.reset}`);
|
|
462
|
+
}
|
|
463
|
+
question = `${ind} Default selected values for "${field.name}" (comma-separated, Enter to skip): `;
|
|
464
|
+
break;
|
|
465
|
+
}
|
|
466
|
+
case 'date-time':
|
|
467
|
+
question = `${ind} Default date for "${field.name}" (YYYY-MM-DD, Enter to skip): `;
|
|
468
|
+
break;
|
|
469
|
+
default:
|
|
470
|
+
question = `${ind} Default value for "${field.name}" (Enter to skip): `;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
const raw = (await prompt(rl, question)).trim();
|
|
474
|
+
if (!raw) return;
|
|
475
|
+
|
|
476
|
+
// Validate boolean
|
|
477
|
+
if (field.type === 'boolean') {
|
|
478
|
+
field.defaultValue = raw === 'true' ? 'true' : 'false';
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
// Validate number
|
|
483
|
+
if (field.type === 'number') {
|
|
484
|
+
if (isNaN(Number(raw))) {
|
|
485
|
+
log.warn(`"${raw}" is not a valid number — skipping default.`);
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
const num = Number(raw);
|
|
489
|
+
if (field.min !== undefined && num < field.min) {
|
|
490
|
+
log.warn(`"${raw}" is below min:${field.min} — skipping default.`);
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
if (field.max !== undefined && num > field.max) {
|
|
494
|
+
log.warn(`"${raw}" is above max:${field.max} — skipping default.`);
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
// Validate select/radio must match one of the options
|
|
500
|
+
if ((field.type === 'select' || field.type === 'radio-group') && field.options?.length) {
|
|
501
|
+
const valid = field.options.map((o) => o.value);
|
|
502
|
+
if (!valid.includes(raw)) {
|
|
503
|
+
log.warn(`"${raw}" is not one of the defined option values — skipping default.`);
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
// Validate multiselect/checkbox — each value must match an option
|
|
509
|
+
if ((field.type === 'multiselect' || field.type === 'checkbox-group') && field.options?.length) {
|
|
510
|
+
const valid = new Set(field.options.map((o) => o.value));
|
|
511
|
+
const parts = raw.split(',').map((s) => s.trim()).filter(Boolean);
|
|
512
|
+
const bad = parts.filter((p) => !valid.has(p));
|
|
513
|
+
if (bad.length) {
|
|
514
|
+
log.warn(`Unknown option value(s): ${bad.join(', ')} — skipping default.`);
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
field.defaultValue = raw;
|
|
520
|
+
log.info(`Default set: ${c.cyan}${raw}${c.reset}`);
|
|
521
|
+
log.blank();
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
// ─── Optional validation prompt ───────────────────────────────────────────────
|
|
525
|
+
async function collectValidation(rl, field, indent = '') {
|
|
526
|
+
const available = VALIDATIONS[field.type] || [];
|
|
527
|
+
if (!available.length) return;
|
|
528
|
+
const hasExtras = await confirm(rl, `${indent}Add validation / hints for "${field.name}"?`, false);
|
|
529
|
+
if (!hasExtras) return;
|
|
530
|
+
log.blank();
|
|
531
|
+
if (available.includes('required')) { if (await confirm(rl, `${indent} Required?`, false)) field.required = true; }
|
|
532
|
+
if (available.includes('readOnly')) { if (await confirm(rl, `${indent} Read-only?`, false)) field.readOnly = true; }
|
|
533
|
+
if (available.includes('hidden')) { if (await confirm(rl, `${indent} Hidden?`, false)) field.hidden = true; }
|
|
534
|
+
if (available.includes('description')) { const d = (await prompt(rl, `${indent} Helper text (Enter to skip): `)).trim(); if (d) field.description = d; }
|
|
535
|
+
if (available.includes('minLength')) { const v = (await prompt(rl, `${indent} Min length (Enter to skip): `)).trim(); if (v && !isNaN(v)) field.minLength = +v; }
|
|
536
|
+
if (available.includes('maxLength')) { const v = (await prompt(rl, `${indent} Max length (Enter to skip): `)).trim(); if (v && !isNaN(v)) field.maxLength = +v; }
|
|
537
|
+
if (available.includes('min')) { const v = (await prompt(rl, `${indent} Min${field.type === 'date-time' ? ' (YYYY-MM-DD)' : ''} (Enter to skip): `)).trim(); if (v) field.min = field.type === 'number' ? +v : v; }
|
|
538
|
+
if (available.includes('max')) { const v = (await prompt(rl, `${indent} Max${field.type === 'date-time' ? ' (YYYY-MM-DD)' : ''} (Enter to skip): `)).trim(); if (v) field.max = field.type === 'number' ? +v : v; }
|
|
539
|
+
if (available.includes('step')) { const v = (await prompt(rl, `${indent} Step (Enter to skip): `)).trim(); if (v && !isNaN(v)) field.step = +v; }
|
|
540
|
+
if (available.includes('rootPath')) { const v = (await prompt(rl, `${indent} Root path (Enter to skip): `)).trim(); if (v) field.rootPath = v; }
|
|
541
|
+
if (available.includes('customErrorMsg')) { const v = (await prompt(rl, `${indent} Custom error message (Enter to skip): `)).trim(); if (v) field.customErrorMsg = v; }
|
|
542
|
+
log.blank();
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
// ─── Field collector ──────────────────────────────────────────────────────────
|
|
546
|
+
async function collectFields(rl, context, depth = 0) {
|
|
547
|
+
const fields = [];
|
|
548
|
+
const ind = depth > 0 ? ' ' : '';
|
|
549
|
+
log.info(`Define ${c.bold}${context}${c.reset} fields. Leave name blank to finish.`);
|
|
550
|
+
log.blank();
|
|
551
|
+
|
|
552
|
+
while (true) {
|
|
553
|
+
const name = (await prompt(rl, `${ind}Field name (camelCase, blank to finish): `)).trim();
|
|
554
|
+
if (!name) break;
|
|
555
|
+
if (!/^[a-zA-Z][a-zA-Z0-9]*$/.test(name)) { log.warn('camelCase only — no hyphens or underscores.'); continue; }
|
|
556
|
+
|
|
557
|
+
const defLabel = toTitleCase(name.replace(/([A-Z])/g, '-$1').toLowerCase());
|
|
558
|
+
const label = (await prompt(rl, `${ind}Label for "${name}" [${defLabel}]: `)).trim() || defLabel;
|
|
559
|
+
|
|
560
|
+
printFieldTypes();
|
|
561
|
+
const typeIn = (await prompt(rl, `${ind}Type (name or number) [text]: `)).trim();
|
|
562
|
+
let type = 'text';
|
|
563
|
+
if (typeIn) { const byNum = ALL_FIELD_KEYS[parseInt(typeIn, 10) - 1]; type = byNum || (FIELD_TYPES[typeIn] ? typeIn : 'text'); }
|
|
564
|
+
|
|
565
|
+
const field = { name, label, type };
|
|
566
|
+
|
|
567
|
+
// multi: true option for eligible types
|
|
568
|
+
if (FIELD_TYPES[type]?.multi) {
|
|
569
|
+
const isMulti = await confirm(rl, `${ind}Allow multiple values (multi: true)?`, false);
|
|
570
|
+
if (isMulti) field.multi = true;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
// Options builder for select / multiselect / radio-group / checkbox-group
|
|
574
|
+
if (OPTION_TYPES.has(type)) {
|
|
575
|
+
field.options = await collectOptions(rl, name, ind);
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
// Default value
|
|
579
|
+
await collectDefaultValue(rl, field, ind);
|
|
580
|
+
|
|
581
|
+
// Container — collect sub-fields
|
|
582
|
+
if (type === 'container') {
|
|
583
|
+
if (depth >= 1) {
|
|
584
|
+
log.warn('UE does not support nested containers — skipping sub-fields.');
|
|
585
|
+
field.subFields = [];
|
|
586
|
+
} else {
|
|
587
|
+
log.blank();
|
|
588
|
+
console.log(` ${c.cyan} ┌─ Container "${name}" — define sub-fields ─────────────────${c.reset}`);
|
|
589
|
+
field.subFields = await collectFields(rl, `${name} sub-fields`, depth + 1);
|
|
590
|
+
console.log(` ${c.cyan} └─ End of "${name}" ──────────────────────────────────────────${c.reset}`);
|
|
591
|
+
log.blank();
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
// Optional validation
|
|
596
|
+
await collectValidation(rl, field, ind);
|
|
597
|
+
|
|
598
|
+
fields.push(field);
|
|
599
|
+
|
|
600
|
+
// Summary
|
|
601
|
+
const extras = [
|
|
602
|
+
field.multi && 'multi',
|
|
603
|
+
field.required && 'required',
|
|
604
|
+
field.readOnly && 'readOnly',
|
|
605
|
+
field.hidden && 'hidden',
|
|
606
|
+
field.options && `${field.options.length} options`,
|
|
607
|
+
field.defaultValue !== undefined && `default:"${field.defaultValue}"`,
|
|
608
|
+
field.minLength !== undefined && `minLen:${field.minLength}`,
|
|
609
|
+
field.maxLength !== undefined && `maxLen:${field.maxLength}`,
|
|
610
|
+
field.min !== undefined && `min:${field.min}`,
|
|
611
|
+
field.max !== undefined && `max:${field.max}`,
|
|
612
|
+
field.step !== undefined && `step:${field.step}`,
|
|
613
|
+
field.rootPath && `rootPath:${field.rootPath}`,
|
|
614
|
+
field.description && `hint:"${field.description}"`,
|
|
615
|
+
].filter(Boolean);
|
|
616
|
+
|
|
617
|
+
if (type === 'container') {
|
|
618
|
+
log.success(`Added: ${c.bold}${name}${c.reset} container sub-fields: ${c.dim}${(field.subFields||[]).map((s)=>s.name).join(', ')||'none'}${c.reset}`);
|
|
619
|
+
} else {
|
|
620
|
+
log.success(`Added: ${c.bold}${name}${c.reset} ${c.cyan}${type}${c.reset}${extras.length ? ` ${c.yellow}[${extras.join(', ')}]${c.reset}` : ''}`);
|
|
621
|
+
}
|
|
622
|
+
log.blank();
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
if (!fields.length && depth === 0) { fields.push({ name: 'text', label: 'Text', type: 'text' }); log.warn('No fields — defaulted to a single "text" field.'); }
|
|
626
|
+
return fields;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
// ─── Consolidated block JSON writer ──────────────────────────────────────────
|
|
630
|
+
// Generates a single blocks/<name>/_<name>.json containing
|
|
631
|
+
// definitions, models and filters — matching the boilerplate pattern.
|
|
632
|
+
// `npm run build:json` (merge-json-cli) assembles these into global files.
|
|
633
|
+
|
|
634
|
+
function genBlockJSON(blockName, isContainer, parentFields, childName, childFields, needsVariants = false) {
|
|
635
|
+
// ── models ───────────────────────────────────────────────────────────────────
|
|
636
|
+
const models = [];
|
|
637
|
+
const parentSchema = buildFieldSchema(parentFields);
|
|
638
|
+
|
|
639
|
+
// classes field only added when author explicitly opted in during scaffolding
|
|
640
|
+
if (needsVariants) {
|
|
641
|
+
parentSchema.push({
|
|
642
|
+
component: 'text',
|
|
643
|
+
name: 'classes',
|
|
644
|
+
label: 'Variants (comma-separated)',
|
|
645
|
+
valueType: 'string',
|
|
646
|
+
value: '',
|
|
647
|
+
});
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
models.push({ id: blockName, fields: parentSchema });
|
|
651
|
+
if (isContainer) {
|
|
652
|
+
models.push({ id: childName, fields: buildFieldSchema(childFields) });
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
// ── definitions ──────────────────────────────────────────────────────────────
|
|
656
|
+
const template = { name: toTitleCase(blockName), model: blockName };
|
|
657
|
+
if (isContainer) {
|
|
658
|
+
template.filter = blockName;
|
|
659
|
+
// No :items — pre-population causes 409 JCR conflict on insert
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
const definitions = [{
|
|
663
|
+
title: toTitleCase(blockName),
|
|
664
|
+
id: blockName,
|
|
665
|
+
plugins: {
|
|
666
|
+
xwalk: {
|
|
667
|
+
page: {
|
|
668
|
+
resourceType: 'core/franklin/components/block/v1/block',
|
|
669
|
+
template,
|
|
670
|
+
},
|
|
671
|
+
},
|
|
672
|
+
},
|
|
673
|
+
}];
|
|
674
|
+
|
|
675
|
+
// Container blocks need a SECOND definition for the child item
|
|
676
|
+
// with resourceType block/item — this is what UE uses when author
|
|
677
|
+
// clicks + inside the block to add a child
|
|
678
|
+
if (isContainer) {
|
|
679
|
+
definitions.push({
|
|
680
|
+
title: toTitleCase(childName),
|
|
681
|
+
id: childName,
|
|
682
|
+
plugins: {
|
|
683
|
+
xwalk: {
|
|
684
|
+
page: {
|
|
685
|
+
resourceType: 'core/franklin/components/block/v1/block/item',
|
|
686
|
+
template: {
|
|
687
|
+
name: toTitleCase(childName),
|
|
688
|
+
model: childName,
|
|
689
|
+
},
|
|
690
|
+
},
|
|
691
|
+
},
|
|
692
|
+
},
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
// ── filters ──────────────────────────────────────────────────────────────────
|
|
697
|
+
// Section filter is NOT generated here — it is a project-level concern
|
|
698
|
+
// maintained in the boilerplate's central component-filters.json.
|
|
699
|
+
// Only the container filter is block-owned (restricts what goes inside this block).
|
|
700
|
+
const filters = [];
|
|
701
|
+
if (isContainer) {
|
|
702
|
+
filters.push({ id: blockName, components: [childName] });
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
return { definitions, models, filters };
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
function writeBlockJSON(blockDir, blockName, blockJSON, dryRun) {
|
|
709
|
+
const label = `blocks/${blockName}/_${blockName}.json`;
|
|
710
|
+
const content = JSON.stringify(blockJSON, null, 2) + '\n';
|
|
711
|
+
writeFile(path.join(blockDir, `_${blockName}.json`), content, label, dryRun);
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
// ─── File writer (dry-run aware) ──────────────────────────────────────────────
|
|
715
|
+
function writeFile(filePath, content, label, dryRun) {
|
|
716
|
+
if (!dryRun && fs.existsSync(filePath)) { log.warn(`${label} already exists — skipped.`); return; }
|
|
717
|
+
if (dryRun) {
|
|
718
|
+
log.dry(`Would create: ${label}`);
|
|
719
|
+
console.log(`\n${c.dim}${'─'.repeat(60)}${c.reset}`);
|
|
720
|
+
content.split('\n').slice(0, 20).forEach((l) => console.log(` ${c.dim}${l}${c.reset}`));
|
|
721
|
+
if (content.split('\n').length > 20) console.log(` ${c.dim}... (${content.split('\n').length - 20} more lines)${c.reset}`);
|
|
722
|
+
console.log(`${c.dim}${'─'.repeat(60)}${c.reset}\n`);
|
|
723
|
+
return;
|
|
724
|
+
}
|
|
725
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
726
|
+
fs.writeFileSync(filePath, content, 'utf8');
|
|
727
|
+
log.success(`Created ${label}`);
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
// ══════════════════════════════════════════════════════════════════════════════
|
|
731
|
+
// COMMAND: create
|
|
732
|
+
// ══════════════════════════════════════════════════════════════════════════════
|
|
733
|
+
async function cmdCreate(rl, cwd, argName, dryRun) {
|
|
734
|
+
if (dryRun) log.warn('DRY RUN — no files will be written.\n');
|
|
735
|
+
|
|
736
|
+
let blockName = argName;
|
|
737
|
+
if (!blockName) {
|
|
738
|
+
blockName = (await prompt(rl, `Block name (kebab-case): `)).trim().toLowerCase().replace(/\s+/g, '-');
|
|
739
|
+
}
|
|
740
|
+
if (!blockName || !isValidName(blockName)) { log.error('Invalid block name.'); rl.close(); process.exit(1); }
|
|
741
|
+
|
|
742
|
+
log.divider();
|
|
743
|
+
log.info(`Block: ${c.bold}${blockName}${c.reset}`);
|
|
744
|
+
log.info(`Dir : ${c.dim}${cwd}${c.reset}`);
|
|
745
|
+
log.divider();
|
|
746
|
+
|
|
747
|
+
// Block type
|
|
748
|
+
log.blank();
|
|
749
|
+
console.log(` ${c.bold}Block type:${c.reset}`);
|
|
750
|
+
console.log(` ${c.dim}1.${c.reset} ${c.cyan}Simple${c.reset} ${c.dim}Fixed fields — hero, banner, teaser, quote${c.reset}`);
|
|
751
|
+
console.log(` ${c.dim}2.${c.reset} ${c.cyan}Multi-row${c.reset} ${c.dim}Same fields repeating — slider, cards, table rows${c.reset}`);
|
|
752
|
+
console.log(` ${c.dim}3.${c.reset} ${c.cyan}Container${c.reset} ${c.dim}Children with different fields — accordion, tabs${c.reset}`);
|
|
753
|
+
log.blank();
|
|
754
|
+
const typeIn = (await prompt(rl, `Type [1]: `)).trim();
|
|
755
|
+
const isContainer = typeIn === '3' || typeIn.toLowerCase() === 'container';
|
|
756
|
+
const isMultiRow = typeIn === '2' || typeIn.toLowerCase() === 'multi-row';
|
|
757
|
+
log.blank();
|
|
758
|
+
|
|
759
|
+
// Parent fields
|
|
760
|
+
log.section(isContainer ? `Phase 1 — Parent config fields` : `Define block fields`);
|
|
761
|
+
const parentFields = await collectFields(rl, isContainer ? `${blockName} config` : blockName);
|
|
762
|
+
|
|
763
|
+
// Container: child setup
|
|
764
|
+
let childName = '', childFields = [];
|
|
765
|
+
if (isContainer) {
|
|
766
|
+
log.divider();
|
|
767
|
+
log.section(`Phase 2 — Child item`);
|
|
768
|
+
const sugg = `${blockName}-item`;
|
|
769
|
+
const childIn = (await prompt(rl, `Child item name [${sugg}]: `)).trim().toLowerCase().replace(/\s+/g, '-');
|
|
770
|
+
childName = childIn || sugg;
|
|
771
|
+
if (!isValidName(childName)) { log.error(`Invalid child name.`); rl.close(); process.exit(1); }
|
|
772
|
+
log.blank();
|
|
773
|
+
log.info(`Child: ${c.bold}${childName}${c.reset}`);
|
|
774
|
+
log.blank();
|
|
775
|
+
childFields = await collectFields(rl, `${childName} fields`);
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
// Variants prompt — asked for all block types
|
|
779
|
+
log.divider();
|
|
780
|
+
const needsVariants = await confirm(rl, `Does this block need variants? ${c.dim}(e.g. dark, light, compact)${c.reset}`, false);
|
|
781
|
+
if (needsVariants) {
|
|
782
|
+
log.info(`Variants enabled — a ${c.cyan}classes${c.reset} field will be added to the model.`);
|
|
783
|
+
log.info(`Authors type variant names in UE → they apply as CSS classes on the block.`);
|
|
784
|
+
}
|
|
785
|
+
log.blank();
|
|
786
|
+
|
|
787
|
+
rl.close();
|
|
788
|
+
|
|
789
|
+
// Write files
|
|
790
|
+
log.blank(); log.divider(); log.blank();
|
|
791
|
+
|
|
792
|
+
const dir = path.join(cwd, 'blocks', blockName);
|
|
793
|
+
|
|
794
|
+
// Minimal lint-free JS based on block type
|
|
795
|
+
let jsContent;
|
|
796
|
+
if (isContainer) jsContent = genContainerBlockJS(blockName, childName);
|
|
797
|
+
else if (isMultiRow) jsContent = genMultiRowBlockJS(blockName);
|
|
798
|
+
else jsContent = genSimpleBlockJS(blockName);
|
|
799
|
+
|
|
800
|
+
writeFile(path.join(dir, `${blockName}.js`), jsContent, `blocks/${blockName}/${blockName}.js`, dryRun);
|
|
801
|
+
writeFile(path.join(dir, `${blockName}.css`), genBlockCSS(blockName), `blocks/${blockName}/${blockName}.css`, dryRun);
|
|
802
|
+
writeFile(path.join(dir, 'README.md'), genReadme(blockName, isContainer, parentFields, childName, childFields, needsVariants), `blocks/${blockName}/README.md`, dryRun);
|
|
803
|
+
|
|
804
|
+
const blockJSON = genBlockJSON(blockName, isContainer, parentFields, childName, childFields, needsVariants);
|
|
805
|
+
writeBlockJSON(dir, blockName, blockJSON, dryRun);
|
|
806
|
+
|
|
807
|
+
// Summary
|
|
808
|
+
log.blank(); log.divider();
|
|
809
|
+
const prefix = dryRun ? `${c.yellow}~ DRY RUN${c.reset} ` : `${c.green}✔${c.reset} `;
|
|
810
|
+
const typeLabel = isContainer ? `container → ${childName}` : isMultiRow ? 'multi-row' : 'simple';
|
|
811
|
+
console.log(`\n ${prefix}${c.bold}"${blockName}" ${dryRun ? 'would be' : 'scaffolded!'} (${typeLabel})${c.reset}\n`);
|
|
812
|
+
if (!dryRun) {
|
|
813
|
+
console.log(` ${c.dim}Files created:${c.reset}`);
|
|
814
|
+
console.log(` ${c.cyan}blocks/${blockName}/${blockName}.js${c.reset}`);
|
|
815
|
+
console.log(` ${c.cyan}blocks/${blockName}/${blockName}.css${c.reset}`);
|
|
816
|
+
console.log(` ${c.cyan}blocks/${blockName}/README.md${c.reset}`);
|
|
817
|
+
console.log(` ${c.cyan}blocks/${blockName}/_${blockName}.json${c.reset} ${c.dim}(definitions + models + filters)${c.reset}`);
|
|
818
|
+
console.log(`\n ${c.dim}Next steps:${c.reset}`);
|
|
819
|
+
console.log(` 1. Add ${c.bold}"${blockName}"${c.reset} to the ${c.cyan}section${c.reset} entry in your project's ${c.bold}component-filters.json${c.reset}`);
|
|
820
|
+
console.log(` 2. ${c.bold}npm run build:json${c.reset} → assemble global JSON files`);
|
|
821
|
+
console.log(` 3. ${c.bold}aem up${c.reset} → http://localhost:3000`);
|
|
822
|
+
console.log(` 4. Add block in Universal Editor`);
|
|
823
|
+
console.log(` 5. Implement ${c.bold}blocks/${blockName}/${blockName}.js${c.reset}\n`);
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
// ══════════════════════════════════════════════════════════════════════════════
|
|
828
|
+
// COMMAND: remove
|
|
829
|
+
// ══════════════════════════════════════════════════════════════════════════════
|
|
830
|
+
async function cmdRemove(rl, cwd, blockName, dryRun) {
|
|
831
|
+
log.title('Remove Block');
|
|
832
|
+
if (!blockName) { blockName = (await prompt(rl, `Block name to remove: `)).trim().toLowerCase(); }
|
|
833
|
+
if (!isValidName(blockName)) { log.error('Invalid block name.'); rl.close(); process.exit(1); }
|
|
834
|
+
|
|
835
|
+
const blockDir = path.join(cwd, 'blocks', blockName);
|
|
836
|
+
if (!fs.existsSync(blockDir)) { log.error(`blocks/${blockName}/ does not exist.`); rl.close(); process.exit(1); }
|
|
837
|
+
|
|
838
|
+
if (!dryRun) {
|
|
839
|
+
log.warn(`This will delete blocks/${blockName}/ including all JS, CSS, README and block-level JSON files.`);
|
|
840
|
+
const ok = await confirm(rl, 'Are you sure?', false);
|
|
841
|
+
if (!ok) { log.info('Cancelled.'); rl.close(); return; }
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
rl.close();
|
|
845
|
+
log.divider();
|
|
846
|
+
|
|
847
|
+
if (dryRun) {
|
|
848
|
+
const files = fs.readdirSync(blockDir).map((f) => `blocks/${blockName}/${f}`);
|
|
849
|
+
log.dry(`Would delete blocks/${blockName}/ (${files.length} file(s)):`);
|
|
850
|
+
files.forEach((f) => console.log(` ${c.dim} ${f}${c.reset}`));
|
|
851
|
+
} else {
|
|
852
|
+
fs.rmSync(blockDir, { recursive: true, force: true });
|
|
853
|
+
log.success(`Deleted blocks/${blockName}/`);
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
log.blank();
|
|
857
|
+
log.info(`Run ${c.bold}npm run build:json${c.reset} to rebuild the global JSON files.`);
|
|
858
|
+
log.blank();
|
|
859
|
+
dryRun ? log.dry('Dry run complete — nothing deleted.') : log.success(`"${blockName}" removed.`);
|
|
860
|
+
log.blank();
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
// ══════════════════════════════════════════════════════════════════════════════
|
|
864
|
+
// COMMAND: list
|
|
865
|
+
// ══════════════════════════════════════════════════════════════════════════════
|
|
866
|
+
function cmdList(cwd) {
|
|
867
|
+
log.title('Block Inventory');
|
|
868
|
+
|
|
869
|
+
const blocksDir = path.join(cwd, 'blocks');
|
|
870
|
+
if (!fs.existsSync(blocksDir)) { log.warn('No blocks/ directory found.'); return; }
|
|
871
|
+
|
|
872
|
+
const blocks = fs.readdirSync(blocksDir, { withFileTypes: true })
|
|
873
|
+
.filter((d) => d.isDirectory())
|
|
874
|
+
.map((d) => d.name);
|
|
875
|
+
|
|
876
|
+
if (!blocks.length) { log.info('No blocks found.'); return; }
|
|
877
|
+
|
|
878
|
+
// Header
|
|
879
|
+
console.log(`\n ${c.bold}${col('Block', 26)} JS CSS JSON README${c.reset}`);
|
|
880
|
+
log.divider();
|
|
881
|
+
|
|
882
|
+
let issues = 0;
|
|
883
|
+
blocks.forEach((name) => {
|
|
884
|
+
const dir = path.join(blocksDir, name);
|
|
885
|
+
const hasJS = fs.existsSync(path.join(dir, `${name}.js`));
|
|
886
|
+
const hasCSS = fs.existsSync(path.join(dir, `${name}.css`));
|
|
887
|
+
const hasJSON = fs.existsSync(path.join(dir, `_${name}.json`));
|
|
888
|
+
const hasRM = fs.existsSync(path.join(dir, 'README.md'));
|
|
889
|
+
const allGood = hasJS && hasCSS && hasJSON;
|
|
890
|
+
if (!allGood) issues++;
|
|
891
|
+
const nameColor = allGood ? c.reset : c.yellow;
|
|
892
|
+
console.log(` ${nameColor}${col(name, 26)}${c.reset} ${tick(hasJS)} ${tick(hasCSS)} ${tick(hasJSON)} ${tick(hasRM)}`);
|
|
893
|
+
});
|
|
894
|
+
|
|
895
|
+
log.blank();
|
|
896
|
+
log.info(`${blocks.length} block(s) found${issues ? ` ${c.yellow}(${issues} with issues)${c.reset}` : ` ${c.green}(all clean)${c.reset}`}`);
|
|
897
|
+
log.info(`Run ${c.bold}npm run build:json${c.reset} to rebuild global component-*.json files`);
|
|
898
|
+
log.blank();
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
// ─── Help ──────────────────────────────────────────────────────────────────────
|
|
902
|
+
function printHelp() {
|
|
903
|
+
console.log(`
|
|
904
|
+
${c.bold}${c.cyan}AEM EDS Block Scaffolder${c.reset}
|
|
905
|
+
|
|
906
|
+
${c.bold}Usage:${c.reset}
|
|
907
|
+
npx eds-block-scaffold <command> [block-name] [flags]
|
|
908
|
+
|
|
909
|
+
${c.bold}Commands:${c.reset}
|
|
910
|
+
${c.cyan}create${c.reset} [name] Scaffold a new block ${c.dim}(default if no command given)${c.reset}
|
|
911
|
+
${c.cyan}remove${c.reset} <name> Delete block folder (JSON files go with it)
|
|
912
|
+
${c.cyan}list${c.reset} Show all blocks with file & authoring JSON status
|
|
913
|
+
|
|
914
|
+
${c.bold}Flags:${c.reset}
|
|
915
|
+
${c.cyan}--dry-run${c.reset} Preview what would be generated without writing files
|
|
916
|
+
|
|
917
|
+
${c.bold}Block-level JSON pattern:${c.reset}
|
|
918
|
+
Each block owns a single consolidated JSON file — no global file edits.
|
|
919
|
+
Run ${c.bold}npm run build:json${c.reset} (boilerplate script) to assemble globals.
|
|
920
|
+
|
|
921
|
+
blocks/<name>/
|
|
922
|
+
<name>.js ← decoration logic
|
|
923
|
+
<name>.css ← styles
|
|
924
|
+
_<name>.json ← definitions + models + filters (all in one)
|
|
925
|
+
README.md ← authoring guide
|
|
926
|
+
|
|
927
|
+
${c.bold}Examples:${c.reset}
|
|
928
|
+
npx eds-block-scaffold create
|
|
929
|
+
npx eds-block-scaffold create carousel
|
|
930
|
+
npx eds-block-scaffold create carousel --dry-run
|
|
931
|
+
npx eds-block-scaffold remove old-block
|
|
932
|
+
npx eds-block-scaffold list
|
|
933
|
+
`);
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
// ══════════════════════════════════════════════════════════════════════════════
|
|
937
|
+
// ENTRY POINT
|
|
938
|
+
// ══════════════════════════════════════════════════════════════════════════════
|
|
939
|
+
async function main() {
|
|
940
|
+
const args = process.argv.slice(2);
|
|
941
|
+
const dryRun = args.includes('--dry-run');
|
|
942
|
+
const cleaned = args.filter((a) => !a.startsWith('--'));
|
|
943
|
+
const cmd = cleaned[0] || 'create';
|
|
944
|
+
const arg1 = cleaned[1] || '';
|
|
945
|
+
const cwd = process.cwd();
|
|
946
|
+
|
|
947
|
+
if (cmd === 'help' || cmd === '--help' || cmd === '-h') { printHelp(); process.exit(0); }
|
|
948
|
+
|
|
949
|
+
log.title('AEM EDS Block Scaffolder');
|
|
950
|
+
|
|
951
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
952
|
+
process.on('SIGINT', () => { log.blank(); log.info('Cancelled.'); rl.close(); process.exit(0); });
|
|
953
|
+
|
|
954
|
+
if (cmd === 'list') { rl.close(); cmdList(cwd); }
|
|
955
|
+
else if (cmd === 'remove' || cmd === 'delete') { await cmdRemove(rl, cwd, arg1, dryRun); }
|
|
956
|
+
else if (cmd === 'create' || isValidName(cmd)) {
|
|
957
|
+
const blockName = isValidName(cmd) && cmd !== 'create' ? cmd : arg1;
|
|
958
|
+
await cmdCreate(rl, cwd, blockName, dryRun);
|
|
959
|
+
} else {
|
|
960
|
+
log.error(`Unknown command: "${cmd}"`);
|
|
961
|
+
printHelp();
|
|
962
|
+
rl.close();
|
|
963
|
+
process.exit(1);
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
main().catch((err) => { log.error(err.message); process.exit(1); });
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "aem-eds-cli",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "CLI scaffolding tool for AEM Edge delivery services blocks with Universal editor authoring support",
|
|
5
|
+
"bin": {
|
|
6
|
+
"aem-eds-cli": "./bin/aem-eds.js"
|
|
7
|
+
},
|
|
8
|
+
"keywords": ["aem", "eds", "cli", "scaffolding", "tool","adobe", "experience", "manager", "edge", "delivery", "services", "blocks", "universal", "editor", "authoring"],
|
|
9
|
+
"license": "Apache-2.0",
|
|
10
|
+
"author":"Arkaprava Majumder<arkmajumder@deloitte.com>",
|
|
11
|
+
"engines": {"node":">=16"},
|
|
12
|
+
"files": [
|
|
13
|
+
"bin/",
|
|
14
|
+
"README.md",
|
|
15
|
+
"LICENSE"
|
|
16
|
+
]
|
|
17
|
+
}
|