-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpuck-generator.js
More file actions
997 lines (852 loc) · 29.8 KB
/
puck-generator.js
File metadata and controls
997 lines (852 loc) · 29.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
#!/usr/bin/env node
/**
* Puck Generator
*
* Transforms hardcoded responsive components into Puck-ready components with configurable props
*
* Usage:
* import { generatePuckComponents } from './puck-generator.js'
* await generatePuckComponents({ sourceDir, outputDir, imagesDir, components })
*/
import fs from 'fs';
import path from 'path';
import { parse } from '@babel/parser';
import traverse from '@babel/traverse';
import generate from '@babel/generator';
import * as t from '@babel/types';
/**
* Convert string to camelCase
*/
function toCamelCase(str) {
return str
.replace(/[^a-zA-Z0-9]+(.)/g, (match, char) => char.toUpperCase())
.replace(/^./, char => char.toLowerCase());
}
/**
* Convert string to PascalCase
*/
function toPascalCase(str) {
const camel = toCamelCase(str);
return camel.charAt(0).toUpperCase() + camel.slice(1);
}
/**
* Convert camelCase to readable label
*/
function toLabel(camelCase) {
return camelCase
.replace(/([A-Z])/g, ' $1')
.replace(/^./, str => str.toUpperCase())
.trim();
}
/**
* Get data-name attribute from JSX element
*/
function getDataName(jsxElement) {
if (!jsxElement || !jsxElement.openingElement) return null;
const attributes = jsxElement.openingElement.attributes;
const dataNameAttr = attributes.find(
attr => attr.type === 'JSXAttribute' && attr.name.name === 'data-name'
);
if (dataNameAttr && dataNameAttr.value && dataNameAttr.value.type === 'StringLiteral') {
return dataNameAttr.value.value;
}
return null;
}
/**
* Extract configurable props from component code
*/
function extractProps(componentCode, componentName) {
const ast = parse(componentCode, {
sourceType: 'module',
plugins: ['jsx', 'typescript']
});
const props = {
texts: [],
images: [],
visibility: []
};
// Track imported images
const imageImports = new Map(); // importName → imagePath
traverse.default(ast, {
// Extract image imports
ImportDeclaration(path) {
if (path.node.source.value.includes('../img/')) {
const importName = path.node.specifiers[0].local.name;
const imagePath = path.node.source.value;
imageImports.set(importName, imagePath);
}
},
// Extract JSX text content
JSXText(path) {
const text = path.node.value.trim();
if (text && text.length > 0) {
// Generate prop name from text (first 20 chars, camelCase)
// Remove all non-alphanumeric except spaces first, then convert to camelCase, then remove remaining non-alphanumeric
const cleanText = text.substring(0, 20).replace(/[^a-zA-Z0-9\s]/g, '');
let propName = toCamelCase(cleanText).replace(/[^a-zA-Z0-9]/g, '') || 'text';
// If propName is empty or starts with a digit, prefix it with 'text'
if (!propName || /^\d/.test(propName)) {
propName = 'text' + (propName || 'Value');
}
// Avoid duplicates by adding index
let finalPropName = propName;
let index = 1;
while (props.texts.find(p => p.propName === finalPropName)) {
finalPropName = propName + index;
index++;
}
props.texts.push({
original: text,
propName: finalPropName,
defaultValue: text
});
}
},
// Extract visibility props from conditional classes
JSXAttribute(path) {
if (path.node.name.name === 'className') {
const classValue = path.node.value;
if (classValue && classValue.type === 'StringLiteral') {
const classes = classValue.value;
// Detect responsive visibility (max-md:hidden, max-lg:hidden)
if (classes.includes('max-md:hidden') || classes.includes('max-lg:hidden')) {
// Get parent element's data-name
let parentPath = path.parentPath;
while (parentPath && parentPath.node.type !== 'JSXElement') {
parentPath = parentPath.parentPath;
}
if (parentPath && parentPath.node.type === 'JSXElement') {
const dataName = getDataName(parentPath.node);
if (dataName) {
const propName = `show${toPascalCase(dataName)}`;
// Avoid duplicates
if (!props.visibility.find(p => p.propName === propName)) {
props.visibility.push({
element: dataName,
propName,
defaultValue: true // Visible by default
});
}
}
}
}
}
}
}
});
// Extract images from imports
imageImports.forEach((imagePath, importName) => {
let propName = toCamelCase(importName.replace(/^img/, ''));
// If propName is empty or starts with a digit, prefix it with 'image'
if (!propName || /^\d/.test(propName)) {
propName = 'image' + (propName || 'Asset');
}
// Extract filename from path (e.g., '../img/logo.svg' -> 'logo.svg')
const filename = imagePath.split('/').pop();
props.images.push({
importName,
propName,
defaultValue: filename // Just the filename, will be served via API
});
});
return props;
}
/**
* Generate Puck-ready component with props interface
*/
function generatePuckComponent(componentName, originalCode, extractedProps) {
const ast = parse(originalCode, {
sourceType: 'module',
plugins: ['jsx', 'typescript']
});
// Build props interface with deduplication
const allProps = [
...extractedProps.texts.map(p => ({ ...p, type: 'string' })),
...extractedProps.images.map(p => ({ ...p, type: 'string' })),
...extractedProps.visibility.map(p => ({ ...p, type: 'boolean' }))
];
// Deduplicate by propName (keep first occurrence)
const uniquePropsMap = new Map();
for (const prop of allProps) {
if (!uniquePropsMap.has(prop.propName)) {
uniquePropsMap.set(prop.propName, prop);
}
}
const uniqueProps = Array.from(uniquePropsMap.values());
const propsInterface = `interface ${componentName}Props {
className?: string;
${uniqueProps.map(p => ` ${p.propName}?: ${p.type};`).join('\n')}
}`;
// Remove image imports and React/CSS imports (will be re-added)
traverse.default(ast, {
ImportDeclaration(path) {
if (path.node.source.value.includes('../img/') ||
path.node.source.value === 'react' ||
path.node.source.value.endsWith('.css')) {
path.remove();
}
}
});
// Replace hardcoded texts with props
let textIndex = 0;
traverse.default(ast, {
JSXText(path) {
const text = path.node.value.trim();
if (text && text.length > 0 && textIndex < extractedProps.texts.length) {
const prop = extractedProps.texts[textIndex];
textIndex++;
// Replace with {propName}
path.replaceWith(
t.jsxExpressionContainer(
t.identifier(prop.propName)
)
);
}
}
});
// Track image usage in sub-components
const subComponentImageUsage = new Map(); // subComponentName -> Set(imagePropNames)
traverse.default(ast, {
// First pass: Find sub-components (non-exported functions) and track which images they use
FunctionDeclaration(path) {
// Skip if this is the exported default function
if (path.parent.type === 'ExportDefaultDeclaration') {
return;
}
const funcName = path.node.id.name;
const usedImages = new Set();
// Traverse this function's body to find image references
path.traverse({
JSXAttribute(innerPath) {
if (innerPath.node.name.name === 'src' && innerPath.node.value.type === 'JSXExpressionContainer') {
const expr = innerPath.node.value.expression;
if (expr.type === 'Identifier') {
const imageProp = extractedProps.images.find(p => p.importName === expr.name);
if (imageProp) {
usedImages.add(imageProp.propName);
}
}
}
}
});
if (usedImages.size > 0) {
subComponentImageUsage.set(funcName, usedImages);
}
}
});
// Replace image references with props
traverse.default(ast, {
JSXAttribute(path) {
if (path.node.name.name === 'src' && path.node.value.type === 'JSXExpressionContainer') {
const expr = path.node.value.expression;
if (expr.type === 'Identifier') {
const imageProp = extractedProps.images.find(p => p.importName === expr.name);
if (imageProp) {
// Replace importName with propName
expr.name = imageProp.propName;
}
}
}
}
});
// Update sub-component function signatures to accept image props
traverse.default(ast, {
FunctionDeclaration(path) {
// Skip if this is the exported default function
if (path.parent.type === 'ExportDefaultDeclaration') {
return;
}
const funcName = path.node.id.name;
const usedImages = subComponentImageUsage.get(funcName);
if (usedImages && usedImages.size > 0) {
// Find the TypeScript type annotation parameter
const param = path.node.params[0];
if (param && param.type === 'ObjectPattern' && param.typeAnnotation) {
const typeAnnotation = param.typeAnnotation.typeAnnotation;
if (typeAnnotation.type === 'TSTypeLiteral') {
// Add image props to TypeScript interface
usedImages.forEach(imageProp => {
typeAnnotation.members.push(
t.tsPropertySignature(
t.identifier(imageProp),
t.tsTypeAnnotation(t.tsStringKeyword())
)
);
// Also add to destructuring pattern
param.properties.push(
t.objectProperty(
t.identifier(imageProp),
t.identifier(imageProp),
false,
true
)
);
});
}
}
}
}
});
// Update JSX elements that render sub-components to pass image props
traverse.default(ast, {
JSXOpeningElement(path) {
const elementName = path.node.name.name;
const usedImages = subComponentImageUsage.get(elementName);
if (usedImages && usedImages.size > 0) {
// Add image props as JSX attributes
usedImages.forEach(imageProp => {
path.node.attributes.push(
t.jsxAttribute(
t.jsxIdentifier(imageProp),
t.jsxExpressionContainer(t.identifier(imageProp))
)
);
});
}
}
});
// Update function signature with props destructuring
traverse.default(ast, {
ExportDefaultDeclaration(path) {
const funcDecl = path.node.declaration;
if (funcDecl.type === 'FunctionDeclaration') {
// Collect all unique prop names (deduplicate across texts, images, visibility)
const allProps = [
...extractedProps.texts,
...extractedProps.images,
...extractedProps.visibility
];
// Deduplicate by propName (keep first occurrence)
const uniquePropsMap = new Map();
uniquePropsMap.set('className', { propName: 'className' }); // Always include className
for (const prop of allProps) {
if (!uniquePropsMap.has(prop.propName)) {
uniquePropsMap.set(prop.propName, prop);
}
}
const uniqueProps = Array.from(uniquePropsMap.values());
// Update params - simple destructuring pattern with unique props only
funcDecl.params = [
t.objectPattern(
uniqueProps.map(p =>
t.objectProperty(t.identifier(p.propName), t.identifier(p.propName), false, true)
)
)
];
}
}
});
// Generate final code
const transformedCode = generate.default(ast, {}, originalCode).code;
// Construct final component with interface
return `import React from 'react';
import './${componentName}.css';
${propsInterface}
${transformedCode}`;
}
/**
* Extract className for each component from Page.tsx
*/
function extractComponentClassNames(pageCode) {
const ast = parse(pageCode, {
sourceType: 'module',
plugins: ['jsx', 'typescript']
});
const componentClassNames = {};
traverse.default(ast, {
JSXElement(path) {
const openingElement = path.node.openingElement;
if (openingElement.type === 'JSXOpeningElement' &&
openingElement.name.type === 'JSXIdentifier') {
const componentName = openingElement.name.name;
// Find className attribute
const classNameAttr = openingElement.attributes.find(
attr => attr.type === 'JSXAttribute' && attr.name.name === 'className'
);
if (classNameAttr && classNameAttr.value && classNameAttr.value.type === 'StringLiteral') {
componentClassNames[componentName] = classNameAttr.value.value;
}
}
}
});
return componentClassNames;
}
/**
* Extract root layout properties from Page.tsx
*/
function extractRootLayoutProps(pageCode) {
const ast = parse(pageCode, {
sourceType: 'module',
plugins: ['jsx', 'typescript']
});
const rootProps = {
pageBackground: 'bg-white',
bodyGap: 'gap-4',
bodyPadding: 'p-6',
containerGap: 'gap-6',
containerMaxWidth: 'max-w-7xl'
};
// Helper to extract specific class pattern from className string
const extractClass = (className, pattern) => {
const classes = className.split(/\s+/);
return classes.find(c => c.match(pattern));
};
traverse.default(ast, {
JSXElement(path) {
const openingElement = path.node.openingElement;
// Find data-name attribute
const dataNameAttr = openingElement.attributes.find(
attr => attr.type === 'JSXAttribute' &&
attr.name.name === 'data-name' &&
attr.value && attr.value.type === 'StringLiteral'
);
if (dataNameAttr) {
const dataName = dataNameAttr.value.value;
// Find className attribute
const classNameAttr = openingElement.attributes.find(
attr => attr.type === 'JSXAttribute' && attr.name.name === 'className'
);
if (classNameAttr && classNameAttr.value && classNameAttr.value.type === 'StringLiteral') {
const className = classNameAttr.value.value;
// Extract from main page div (e.g., "BGS Homepage - 1440px")
if (dataName.includes('Homepage') || dataName.includes('Page')) {
const bgClass = extractClass(className, /^bg-/);
if (bgClass) rootProps.pageBackground = bgClass;
}
// Extract from body div
if (dataName === 'body') {
const gapClass = extractClass(className, /^gap-/);
const paddingClass = extractClass(className, /^p-\d+$|^p-custom-/);
if (gapClass) rootProps.bodyGap = gapClass;
if (paddingClass) rootProps.bodyPadding = paddingClass;
}
// Extract from container div
if (dataName === 'Container') {
const gapClass = extractClass(className, /^gap-/);
const maxWClass = extractClass(className, /^max-w-/);
if (gapClass) rootProps.containerGap = gapClass;
if (maxWClass) rootProps.containerMaxWidth = maxWClass;
}
}
}
}
});
return rootProps;
}
/**
* Detect if a value is a number (pure number, money, percentage, etc.)
*/
function isNumericValue(value) {
if (typeof value === 'number') return true;
if (typeof value !== 'string') return false;
// Remove currency symbols, commas, percentages
const cleaned = value.replace(/[$,€£¥%\s]/g, '');
// Check if what remains is a valid number
return !isNaN(cleaned) && cleaned !== '';
}
/**
* Detect optimal field type based on prop name and value
*/
function detectFieldType(propName, defaultValue, category) {
// Images → object with url/mode/alt
if (category === 'image') {
return 'object';
}
// Visibility → radio
if (category === 'visibility') {
return 'radio';
}
// Text fields - detect number vs textarea vs text
if (category === 'text') {
// Check if it's a number
if (isNumericValue(defaultValue)) {
return 'number';
}
// Long text → textarea
if (defaultValue && defaultValue.length > 50) {
return 'textarea';
}
// Default to text
return 'text';
}
return 'text';
}
/**
* Generate puck.config.tsx
*/
function generatePuckConfig(components, extractedPropsMap, componentClassNames = {}, rootLayoutProps = {}) {
const imports = components.map(name =>
`import ${name} from './components/${name}';`
).join('\n');
const configs = components.map(name => {
const props = extractedPropsMap[name];
const fields = [];
// ClassName field - always textarea for long CSS strings
fields.push(` className: {
type: 'textarea',
label: 'CSS Classes'
}`);
// Text fields - auto-detect type (number, textarea, or text)
props.texts.forEach(p => {
const fieldType = detectFieldType(p.propName, p.defaultValue, 'text');
fields.push(` ${p.propName}: {
type: '${fieldType}',
label: '${toLabel(p.propName)}'
}`);
});
// Image fields - use object with url/mode/alt
props.images.forEach(p => {
fields.push(` ${p.propName}: {
type: 'object',
objectFields: {
url: {
type: 'text',
label: 'Image URL'
},
mode: {
type: 'select',
label: 'Display Mode',
options: [
{ label: 'Cover', value: 'cover' },
{ label: 'Contain', value: 'contain' },
{ label: 'Fill', value: 'fill' },
{ label: 'Scale Down', value: 'scale-down' },
{ label: 'None', value: 'none' }
]
},
alt: {
type: 'text',
label: 'Alt Text'
}
},
label: '${toLabel(p.propName)}'
}`);
});
// Visibility fields - radio buttons
props.visibility.forEach(p => {
fields.push(` ${p.propName}: {
type: 'radio',
label: '${toLabel(p.propName)}',
options: [
{ label: 'Show', value: true },
{ label: 'Hide', value: false }
]
}`);
});
const defaultProps = [];
// Add className to defaultProps if available
if (componentClassNames[name]) {
defaultProps.push(`className: ${JSON.stringify(componentClassNames[name])}`);
}
// Text fields - handle numbers vs strings
props.texts.forEach(p => {
const fieldType = detectFieldType(p.propName, p.defaultValue, 'text');
if (fieldType === 'number') {
// Parse as number, removing currency/formatting
const numValue = parseFloat(p.defaultValue.replace(/[$,€£¥%\s]/g, ''));
defaultProps.push(`${p.propName}: ${numValue}`);
} else {
defaultProps.push(`${p.propName}: ${JSON.stringify(p.defaultValue)}`);
}
});
// Image fields - now objects with url/mode/alt
props.images.forEach(p => {
defaultProps.push(`${p.propName}: {
url: ${JSON.stringify(p.defaultValue)},
mode: 'cover',
alt: ''
}`);
});
// Visibility fields
props.visibility.forEach(p => defaultProps.push(`${p.propName}: ${p.defaultValue}`));
return ` ${name}: {
fields: {
${fields.join(',\n')}
},
defaultProps: {
${defaultProps.join(',\n ')}
},
render: (props) => {
// Transform image URLs to use API endpoint
const mergeId = (window as any).__PUCK_MERGE_ID__;
const transformedProps = { ...props };
${props.images.map(p => `
// Transform image object to URL string for component
if (transformedProps.${p.propName} && typeof transformedProps.${p.propName} === 'object') {
const imgUrl = transformedProps.${p.propName}.url;
if (imgUrl && !imgUrl.startsWith('http') && !imgUrl.startsWith('/api/')) {
transformedProps.${p.propName} = \`/api/responsive-merges/\${mergeId}/images/\${imgUrl}\`;
} else {
transformedProps.${p.propName} = imgUrl;
}
}`).join('')}
return <${name} {...transformedProps} />;
}
}`;
}).join(',\n');
return `import { Config } from '@measured/puck';
import { DropZone } from '@measured/puck';
import './Page.css';
${imports}
export const config: Config = {
components: {
${configs}
},
root: {
fields: {
pageBackground: {
type: 'text',
label: 'Page Background Class'
},
bodyGap: {
type: 'text',
label: 'Body Gap Class'
},
bodyPadding: {
type: 'text',
label: 'Body Padding Class'
},
containerGap: {
type: 'text',
label: 'Container Gap Class'
},
containerMaxWidth: {
type: 'text',
label: 'Container Max Width Class'
}
},
defaultProps: {
pageBackground: ${JSON.stringify(rootLayoutProps.pageBackground || 'bg-white')},
bodyGap: ${JSON.stringify(rootLayoutProps.bodyGap || 'gap-4')},
bodyPadding: ${JSON.stringify(rootLayoutProps.bodyPadding || 'p-6')},
containerGap: ${JSON.stringify(rootLayoutProps.containerGap || 'gap-6')},
containerMaxWidth: ${JSON.stringify(rootLayoutProps.containerMaxWidth || 'max-w-7xl')}
},
render: ({ pageBackground, bodyGap, bodyPadding, containerGap, containerMaxWidth }) => {
return (
<div className={\`\${pageBackground} content-stretch flex flex-col items-center relative size-full\`} data-name="BGS Homepage - 1440px">
<DropZone zone="header" />
<div className={\`box-border content-stretch flex flex-col \${bodyGap} items-center \${bodyPadding} relative shrink-0 w-full\`} data-name="body">
<div className={\`content-stretch flex flex-col \${containerGap} items-start \${containerMaxWidth} relative shrink-0 w-full\`} data-name="Container">
<DropZone zone="body" />
</div>
</div>
<DropZone zone="footer" />
</div>
);
}
}
};
`;
}
/**
* Generate puck/Page.tsx from original Page.tsx
*/
function generatePuckPage(originalPagePath, outputDir) {
const pageCode = fs.readFileSync(originalPagePath, 'utf8');
const ast = parse(pageCode, {
sourceType: 'module',
plugins: ['jsx', 'typescript']
});
// Replace imports: ./Subcomponents/ → ./components/
traverse.default(ast, {
ImportDeclaration(path) {
if (path.node.source.value.startsWith('./Subcomponents/')) {
path.node.source.value = path.node.source.value.replace(
'./Subcomponents/',
'./components/'
);
}
}
});
const transformedCode = generate.default(ast, {}, pageCode).code;
// Write puck/Page.tsx
fs.writeFileSync(
path.join(outputDir, 'Page.tsx'),
transformedCode
);
// Copy and fix Page.css imports
const pageCssPath = originalPagePath.replace('.tsx', '.css');
if (fs.existsSync(pageCssPath)) {
let cssContent = fs.readFileSync(pageCssPath, 'utf8');
// Fix relative paths for puck/ directory (up one level)
// Replace ./Subcomponents/ → ../components/ in CSS imports
// Replace ./components/ → ../components/ in CSS imports
cssContent = cssContent.replace(/\.\/Subcomponents\//g, '../components/');
cssContent = cssContent.replace(/\.\/components\//g, '../components/');
fs.writeFileSync(
path.join(outputDir, 'Page.css'),
cssContent
);
}
console.log(' ✅ puck/Page.tsx generated');
}
/**
* Main function: Generate Puck-ready components
*/
export async function generatePuckComponents({ sourceDir, outputDir, imagesDir, components }) {
console.log('🎨 Generating Puck-ready components...\n');
// Create output structure
const puckComponentsDir = path.join(outputDir, 'components');
fs.mkdirSync(puckComponentsDir, { recursive: true });
const extractedPropsMap = {};
// Process each component
for (const componentName of components) {
console.log(` Processing ${componentName}...`);
try {
// Read original component
const originalTsxPath = path.join(sourceDir, `${componentName}.tsx`);
const originalCssPath = path.join(sourceDir, `${componentName}.css`);
if (!fs.existsSync(originalTsxPath)) {
console.log(` ⚠️ ${componentName}.tsx not found, skipping`);
continue;
}
const originalTsx = fs.readFileSync(originalTsxPath, 'utf8');
// Extract props
const extractedProps = extractProps(originalTsx, componentName);
extractedPropsMap[componentName] = extractedProps;
console.log(` - Extracted ${extractedProps.texts.length} texts, ${extractedProps.images.length} images, ${extractedProps.visibility.length} visibility props`);
// Generate Puck component
const puckComponent = generatePuckComponent(componentName, originalTsx, extractedProps);
// Write component
fs.writeFileSync(
path.join(puckComponentsDir, `${componentName}.tsx`),
puckComponent
);
// Copy CSS
if (fs.existsSync(originalCssPath)) {
fs.copyFileSync(
originalCssPath,
path.join(puckComponentsDir, `${componentName}.css`)
);
}
console.log(` ✅ ${componentName}`);
} catch (error) {
console.error(` ❌ Error processing ${componentName}: ${error.message}`);
}
}
// Copy images
const puckImgDir = path.join(outputDir, 'img');
if (fs.existsSync(imagesDir)) {
fs.mkdirSync(puckImgDir, { recursive: true });
const images = fs.readdirSync(imagesDir);
for (const img of images) {
fs.copyFileSync(
path.join(imagesDir, img),
path.join(puckImgDir, img)
);
}
console.log(`\n ✅ Copied ${images.length} images to puck/img/`);
}
// Extract className and root layout props from Page.tsx
const originalPagePath = path.join(path.dirname(sourceDir), 'Page.tsx');
let componentClassNames = {};
let rootLayoutProps = {};
if (fs.existsSync(originalPagePath)) {
const pageCode = fs.readFileSync(originalPagePath, 'utf8');
componentClassNames = extractComponentClassNames(pageCode);
rootLayoutProps = extractRootLayoutProps(pageCode);
console.log(' ✅ Extracted className from Page.tsx:', componentClassNames);
console.log(' ✅ Extracted root layout props:', rootLayoutProps);
}
// Generate puck.config.tsx
const puckConfig = generatePuckConfig(Object.keys(extractedPropsMap), extractedPropsMap, componentClassNames, rootLayoutProps);
fs.writeFileSync(
path.join(outputDir, 'puck.config.tsx'),
puckConfig
);
console.log(' ✅ puck.config.tsx generated');
// Generate puck/Page.tsx
if (fs.existsSync(originalPagePath)) {
generatePuckPage(originalPagePath, outputDir);
}
// Generate initial puck-data.json with all components pre-loaded in correct zones
// NOTE: Zone keys must be in "ComponentId:zoneName" format (Puck requirement)
// NOTE: Root props must be in root.props (Puck data structure requirement)
const initialData = {
content: [],
root: {
props: {
pageBackground: rootLayoutProps.pageBackground || 'bg-white',
bodyGap: rootLayoutProps.bodyGap || 'gap-4',
bodyPadding: rootLayoutProps.bodyPadding || 'p-6',
containerGap: rootLayoutProps.containerGap || 'gap-6',
containerMaxWidth: rootLayoutProps.containerMaxWidth || 'max-w-7xl'
}
},
zones: {
"root:header": [],
"root:body": [],
"root:footer": []
}
};
// Distribute components into zones based on their names
components.forEach((componentName, index) => {
const props = extractedPropsMap[componentName];
if (!props) return;
const defaultProps = {};
// Add className if available from Page.tsx
if (componentClassNames[componentName]) {
defaultProps.className = componentClassNames[componentName];
}
// Add text props with default values - parse numbers
props.texts.forEach(p => {
const fieldType = detectFieldType(p.propName, p.defaultValue, 'text');
if (fieldType === 'number') {
// Parse as number, removing currency/formatting
defaultProps[p.propName] = parseFloat(p.defaultValue.replace(/[$,€£¥%\s]/g, ''));
} else {
defaultProps[p.propName] = p.defaultValue;
}
});
// Add image props as objects with url/mode/alt
props.images.forEach(p => {
defaultProps[p.propName] = {
url: p.defaultValue,
mode: 'cover',
alt: ''
};
});
// Add visibility props with default values
props.visibility.forEach(p => {
defaultProps[p.propName] = p.defaultValue;
});
// Add unique ID in props (Puck requirement for React keys)
defaultProps.id = `${componentName}-${Date.now()}-${index}`;
const component = {
type: componentName,
props: defaultProps
};
// Determine which zone this component belongs to
if (componentName === 'Header') {
initialData.zones["root:header"].push(component);
} else if (componentName === 'Footer') {
initialData.zones["root:footer"].push(component);
} else {
// All other components go to body
initialData.zones["root:body"].push(component);
}
});
fs.writeFileSync(
path.join(outputDir, 'puck-data.json'),
JSON.stringify(initialData, null, 2)
);
console.log(' ✅ puck-data.json generated with pre-loaded components');
console.log(`\n✅ Puck generation complete: ${Object.keys(extractedPropsMap).length} components\n`);
}
// CLI support
if (import.meta.url === `file://${process.argv[1]}`) {
const sourceDir = process.argv[2];
const outputDir = process.argv[3];
const imagesDir = process.argv[4];
const components = process.argv.slice(5);
if (!sourceDir || !outputDir || components.length === 0) {
console.error('Usage: node puck-generator.js <sourceDir> <outputDir> <imagesDir> <component1> <component2> ...');
process.exit(1);
}
generatePuckComponents({ sourceDir, outputDir, imagesDir, components })
.catch(err => {
console.error('Error:', err);
process.exit(1);
});
}