@forgewp/compiler 0.1.7 โ†’ 0.1.9

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/bin/fresh.js ADDED
@@ -0,0 +1,421 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { rmSync, existsSync, writeFileSync, mkdirSync, readdirSync } from "node:fs";
4
+ import path from "node:path";
5
+ import { spawnSync } from "node:child_process";
6
+ import pc from "picocolors";
7
+ import { SYSTEM_BLUEPRINTS } from "../lib/blueprints.js";
8
+
9
+ console.log(`\n๐Ÿงน ${pc.bold(pc.bgRed(pc.black(" FORGEWP FACTORY RESET (FRESH CANVAS) ")))}\n`);
10
+
11
+ const projectRoot = process.cwd();
12
+
13
+ // 1. Clean Build Caches & Artifacts
14
+ const foldersToClean = [".forgewp", "dist", ".vite"];
15
+ for (const folder of foldersToClean) {
16
+ const target = path.join(projectRoot, folder);
17
+ if (existsSync(target)) {
18
+ try {
19
+ rmSync(target, { recursive: true, force: true });
20
+ console.log(` ${pc.green("โœ… Cleared cache folder")}: ${pc.cyan(folder)}`);
21
+ } catch (err) {
22
+ console.log(pc.red(` โŒ Failed to clear ${folder}: ${err.message}`));
23
+ }
24
+ }
25
+ }
26
+
27
+ // 2. Prune Scaffolded Directories (pages, blocks, components/ui, components)
28
+ const directoriesToPrune = [
29
+ { dir: "src/app/pages", label: "Pages (src/app/pages/)" },
30
+ { dir: "src/blocks", label: "Gutenberg Blocks (src/blocks/)" },
31
+ { dir: "src/components/ui", label: "UI Components (src/components/ui/)" },
32
+ { dir: "src/components", label: "Components (src/components/)" }
33
+ ];
34
+
35
+ for (const targetPrune of directoriesToPrune) {
36
+ const fullPath = path.join(projectRoot, targetPrune.dir);
37
+ if (existsSync(fullPath)) {
38
+ try {
39
+ rmSync(fullPath, { recursive: true, force: true });
40
+ console.log(` ${pc.green("โœ… Removed folder")}: ${pc.cyan(targetPrune.label)}`);
41
+ } catch (err) {
42
+ console.log(pc.red(` โŒ Failed to remove ${targetPrune.label}: ${err.message}`));
43
+ }
44
+ }
45
+ }
46
+
47
+ // 3. Restore Standard Database & Sitemap Menus from Core Blueprints
48
+ const coreFilesToReset = ["wordpress/menus.json", "wordpress/mock-data.json"];
49
+ for (const relPath of coreFilesToReset) {
50
+ const fullPath = path.join(projectRoot, relPath);
51
+ const blueprintContent = SYSTEM_BLUEPRINTS[relPath];
52
+ if (blueprintContent) {
53
+ try {
54
+ const dirPath = path.dirname(fullPath);
55
+ if (!existsSync(dirPath)) {
56
+ mkdirSync(dirPath, { recursive: true });
57
+ }
58
+ writeFileSync(fullPath, blueprintContent, "utf8");
59
+ console.log(` ${pc.green("โœ… Reset database schema")}: ${pc.cyan(relPath)}`);
60
+ } catch (err) {
61
+ console.log(pc.red(` โŒ Failed to reset ${relPath}: ${err.message}`));
62
+ }
63
+ }
64
+ }
65
+
66
+ // 4. Restore Pristine App Routes (src/app/routes.tsx)
67
+ const routesPath = path.join(projectRoot, "src/app/routes.tsx");
68
+ const routesContent = `import { Route, Switch } from "wouter";
69
+ import HomePage from "./page";
70
+
71
+ /**
72
+ * Local Developer Routes โ€” ForgeWP.
73
+ *
74
+ * Edit this file to add new routes/components for your local Vite preview server.
75
+ *
76
+ * @example
77
+ * // 1. Create a component in src/app/about.tsx
78
+ * // 2. Import it here: import AboutPage from "./about";
79
+ * // 3. Add the Route: <Route path="/about" component={AboutPage} />
80
+ */
81
+ export default function AppRoutes() {
82
+ return (
83
+ <Switch>
84
+ {/* Home preview */}
85
+ <Route path="/" component={HomePage} />
86
+
87
+ {/* Fallback route */}
88
+ <Route>
89
+ <div className="flex min-h-[60vh] flex-col items-center justify-center text-center p-6">
90
+ <h1 className="text-4xl font-bold font-serif text-zinc-950">404</h1>
91
+ <p className="mt-2 text-zinc-600">Page not found locally.</p>
92
+ <a href="/" className="mt-4 text-brand font-semibold hover:underline">
93
+ Go back home
94
+ </a>
95
+ </div>
96
+ </Route>
97
+ </Switch>
98
+ );
99
+ }
100
+ `;
101
+
102
+ try {
103
+ writeFileSync(routesPath, routesContent, "utf8");
104
+ console.log(` ${pc.green("โœ… Reset routes definition")}: src/app/routes.tsx`);
105
+ } catch (err) {
106
+ console.log(pc.red(` โŒ Failed to reset src/app/routes.tsx: ${err.message}`));
107
+ }
108
+
109
+ // 5. Restore Pristine Home Page (src/app/page.tsx)
110
+ const pagePath = path.join(projectRoot, "src/app/page.tsx");
111
+ const pageContent = `import { SEO } from "../.forgewp/SEO";
112
+ import wpConfig from "../../wp.config";
113
+ import { useWpCustomField, WpMenu, WpQueryLoop } from "../.forgewp/wordpress";
114
+
115
+ export default function HomePage() {
116
+ const colors = wpConfig.settings?.color?.palette || [];
117
+ const fontFamilies = wpConfig.settings?.typography?.fontFamilies || [];
118
+ const googleFonts = wpConfig.settings?.typography?.googleFonts || [];
119
+ const layout = wpConfig.settings?.layout || {};
120
+
121
+ return (
122
+ <div className="min-h-screen bg-bg-light p-6 md:p-12 font-sans selection:bg-brand selection:text-white">
123
+ <SEO
124
+ title="ForgeWP Starter โ€” React & Tailwind CSS for WordPress"
125
+ description="A premium, sharp-edge developer framework for creating modern block-themes using React."
126
+ />
127
+
128
+ {/* Main Container โ€” Sharp brutalist outer grid */}
129
+ <main className="mx-auto max-w-6xl border-4 border-zinc-950 bg-white shadow-[8px_8px_0px_0px_rgba(0,0,0,1)]">
130
+ {/* Header Grid Bar */}
131
+ <header className="flex flex-col sm:flex-row items-stretch border-b-4 border-zinc-950">
132
+ <div className="bg-brand text-white px-6 py-6 flex items-center border-b-4 sm:border-b-0 sm:border-r-4 border-zinc-950 font-black tracking-wider text-xl uppercase select-none">
133
+ โšก FORGEWP
134
+ </div>
135
+ <div className="flex-1 px-6 py-4 flex items-center text-xs md:text-sm font-semibold text-zinc-600 font-mono tracking-tight bg-zinc-50">
136
+ theme-compiler://v{wpConfig.version} // status: online
137
+ </div>
138
+ <div className="px-6 py-4 border-t-4 sm:border-t-0 sm:border-l-4 border-zinc-950 flex items-center bg-accent font-black text-sm uppercase tracking-wide text-zinc-950 select-none">
139
+ Developer Console
140
+ </div>
141
+ </header>
142
+
143
+ {/* Hero Section */}
144
+ <section className="grid md:grid-cols-12 border-b-4 border-zinc-950">
145
+ <div className="md:col-span-8 p-8 md:p-12 flex flex-col justify-center border-b-4 md:border-b-0 md:border-r-4 border-zinc-950">
146
+ <span className="inline-block bg-zinc-950 text-white text-xs font-bold font-mono tracking-widest px-3 py-1 uppercase max-w-fit mb-6 select-none">
147
+ v{wpConfig.version} Active
148
+ </span>
149
+ <h1 className="text-4xl md:text-6xl font-serif font-black tracking-tight leading-none text-zinc-950">
150
+ React structure.<br />
151
+ Tailwind speed.<br />
152
+ WordPress power.
153
+ </h1>
154
+ <p className="mt-6 text-base md:text-lg text-zinc-700 leading-relaxed font-sans font-medium max-w-xl">
155
+ Welcome to the next generation of WordPress theme development. Build your layout dynamically using standard React components, mock hooks, and modern utilities.
156
+ </p>
157
+ <div className="mt-8 flex flex-wrap gap-4">
158
+ <a
159
+ href="https://forgewp.dev/docs"
160
+ target="_blank"
161
+ rel="noreferrer"
162
+ className="group relative inline-flex items-center justify-center border-2 border-zinc-950 bg-zinc-950 text-white font-bold text-sm tracking-wider uppercase px-6 py-3 transition-colors hover:bg-brand hover:text-white"
163
+ >
164
+ Read Framework Docs
165
+ </a>
166
+ <a
167
+ href="#tokens"
168
+ className="group relative inline-flex items-center justify-center border-2 border-zinc-950 bg-white text-zinc-950 font-bold text-sm tracking-wider uppercase px-6 py-3 transition-all hover:bg-zinc-100"
169
+ >
170
+ Inspect Config Settings
171
+ </a>
172
+ </div>
173
+ </div>
174
+
175
+ {/* Quick Stats sidebar */}
176
+ <div className="md:col-span-4 bg-zinc-50 p-8 flex flex-col justify-between font-mono text-xs">
177
+ <div className="space-y-6">
178
+ <div className="border-b-2 border-zinc-200 pb-4">
179
+ <span className="block text-zinc-400 font-bold uppercase tracking-wider mb-1">Theme Name</span>
180
+ <span className="text-sm font-black text-zinc-900">{wpConfig.name}</span>
181
+ </div>
182
+ <div className="border-b-2 border-zinc-200 pb-4">
183
+ <span className="block text-zinc-400 font-bold uppercase tracking-wider mb-1">Theme Slug</span>
184
+ <span className="text-sm font-black text-zinc-900">{wpConfig.slug}</span>
185
+ </div>
186
+ <div className="border-b-2 border-zinc-200 pb-4">
187
+ <span className="block text-zinc-400 font-bold uppercase tracking-wider mb-1">Text Domain</span>
188
+ <span className="text-sm font-black text-zinc-900">{wpConfig.textDomain}</span>
189
+ </div>
190
+ <div>
191
+ <span className="block text-zinc-400 font-bold uppercase tracking-wider mb-1">PHP Engine Target</span>
192
+ <span className="text-sm font-black text-zinc-900">PHP 7.4+ // WP 6.0+</span>
193
+ </div>
194
+ </div>
195
+
196
+ <div className="mt-8 border-t-2 border-zinc-950 pt-4 text-[10px] text-zinc-500 font-bold uppercase">
197
+ โšก Generated via @forgewp/compiler
198
+ </div>
199
+ </div>
200
+ </section>
201
+
202
+ {/* Dynamic Tokens Section */}
203
+ <section id="tokens" className="p-8 md:p-12">
204
+ <div className="border-2 border-zinc-950 p-6 md:p-8 bg-zinc-50">
205
+ <div className="flex items-center gap-3 border-b-2 border-zinc-950 pb-4 mb-8">
206
+ <div className="w-4 h-4 bg-secondary"></div>
207
+ <h2 className="text-lg md:text-xl font-bold uppercase tracking-wider text-zinc-950">
208
+ Design Tokens Synchronization Check
209
+ </h2>
210
+ </div>
211
+
212
+ <div className="grid gap-8 md:grid-cols-2 lg:grid-cols-3">
213
+ {/* Color Presets */}
214
+ <div className="border border-zinc-300 bg-white p-6 shadow-[4px_4px_0px_0px_rgba(0,0,0,1)]">
215
+ <h3 className="font-serif font-black text-lg text-zinc-900 mb-4 border-b border-zinc-200 pb-2 uppercase tracking-wide">
216
+ Color Palette
217
+ </h3>
218
+ <div className="space-y-3 font-mono text-xs">
219
+ {colors.map((c) => (
220
+ <div key={c.slug} className="flex items-center gap-3">
221
+ <div
222
+ className="w-6 h-6 border border-zinc-950"
223
+ style={{ backgroundColor: c.color }}
224
+ ></div>
225
+ <div className="flex-1">
226
+ <span className="block font-black text-zinc-800">{c.name}</span>
227
+ <span className="text-zinc-500">{c.color} // {c.slug}</span>
228
+ </div>
229
+ </div>
230
+ ))}
231
+ </div>
232
+ </div>
233
+
234
+ {/* Typography Presets */}
235
+ <div className="border border-zinc-300 bg-white p-6 shadow-[4px_4px_0px_0px_rgba(0,0,0,1)]">
236
+ <h3 className="font-serif font-black text-lg text-zinc-900 mb-4 border-b border-zinc-200 pb-2 uppercase tracking-wide">
237
+ Font Families
238
+ </h3>
239
+ <div className="space-y-4">
240
+ {fontFamilies.map((f) => (
241
+ <div key={f.slug} className="font-mono text-xs">
242
+ <span className="block font-black text-zinc-800 uppercase">{f.name}</span>
243
+ <span className="text-zinc-500 block mb-2">{f.fontFamily}</span>
244
+ <span
245
+ className="text-lg block font-semibold text-zinc-950 border border-zinc-200 p-2 bg-zinc-50"
246
+ style={{ fontFamily: f.fontFamily }}
247
+ >
248
+ The quick brown fox
249
+ </span>
250
+ </div>
251
+ ))}
252
+ </div>
253
+ </div>
254
+
255
+ {/* Layout Presets */}
256
+ <div className="border border-zinc-300 bg-white p-6 shadow-[4px_4px_0px_0px_rgba(0,0,0,1)] flex flex-col justify-between">
257
+ <div>
258
+ <h3 className="font-serif font-black text-lg text-zinc-900 mb-4 border-b border-zinc-200 pb-2 uppercase tracking-wide">
259
+ Layout Bounds
260
+ </h3>
261
+ <div className="space-y-4 font-mono text-xs">
262
+ <div>
263
+ <span className="block font-black text-zinc-800">CONTENT SIZE</span>
264
+ <span className="text-zinc-500 text-lg font-black">{layout.contentSize || "N/A"}</span>
265
+ </div>
266
+ <div>
267
+ <span className="block font-black text-zinc-800">WIDE SIZE</span>
268
+ <span className="text-zinc-500 text-lg font-black">{layout.wideSize || "N/A"}</span>
269
+ </div>
270
+ </div>
271
+ </div>
272
+
273
+ <div className="mt-8 border-t border-zinc-200 pt-4">
274
+ <span className="block font-mono text-[10px] text-zinc-400 font-bold uppercase tracking-wider mb-2">
275
+ Synced Google Fonts
276
+ </span>
277
+ <div className="flex flex-wrap gap-2">
278
+ {googleFonts.map((font) => (
279
+ <span
280
+ key={font}
281
+ className="inline-block bg-zinc-900 text-white text-[10px] font-mono px-2 py-1 uppercase"
282
+ >
283
+ {font.split(":")[0]}
284
+ </span>
285
+ ))}
286
+ </div>
287
+ </div>
288
+ </div>
289
+ </div>
290
+ </div>
291
+ </section>
292
+
293
+ {/* WordPress Dynamic Data Layer Console Section */}
294
+ <section id="data-layer" className="p-8 md:p-12 border-t-4 border-zinc-950 bg-zinc-50">
295
+ <div className="border-2 border-zinc-950 p-6 md:p-8 bg-white shadow-[4px_4px_0px_0px_rgba(0,0,0,1)]">
296
+ <div className="flex items-center gap-3 border-b-2 border-zinc-950 pb-4 mb-8">
297
+ <div className="w-4 h-4 bg-brand"></div>
298
+ <h2 className="text-lg md:text-xl font-bold uppercase tracking-wider text-zinc-950">
299
+ WordPress Data Layer & Compiler Sandbox
300
+ </h2>
301
+ </div>
302
+
303
+ <div className="grid gap-8 lg:grid-cols-3">
304
+ {/* Dynamic Menus Console */}
305
+ <div className="border border-zinc-200 bg-zinc-50 p-6">
306
+ <span className="inline-block bg-zinc-950 text-white text-[9px] font-mono font-black uppercase tracking-widest px-2 py-0.5 mb-4">
307
+ Hook: &lt;WpMenu&gt;
308
+ </span>
309
+ <h3 className="font-serif font-black text-base text-zinc-900 mb-2">
310
+ Navigation Menu
311
+ </h3>
312
+ <p className="text-zinc-500 text-xs mb-4">
313
+ Compiles into dynamic WP site menus (\`wp_nav_menu\`) managed in the WP Dashboard.
314
+ </p>
315
+ <div className="border border-zinc-300 p-4 bg-white">
316
+ <span className="block font-mono text-[9px] text-zinc-400 font-bold uppercase tracking-wider mb-2">
317
+ Rendered Header Navigation:
318
+ </span>
319
+ <WpMenu
320
+ location="primary"
321
+ className="flex flex-col gap-2"
322
+ linkClassName="font-mono text-xs font-bold uppercase tracking-wide text-zinc-700 hover:text-brand transition-colors"
323
+ />
324
+ </div>
325
+ </div>
326
+
327
+ {/* Custom Meta Fields */}
328
+ <div className="border border-zinc-200 bg-zinc-50 p-6">
329
+ <span className="inline-block bg-zinc-950 text-white text-[9px] font-mono font-black uppercase tracking-widest px-2 py-0.5 mb-4">
330
+ Hook: useWpCustomField()
331
+ </span>
332
+ <h3 className="font-serif font-black text-base text-zinc-900 mb-2">
333
+ Metadata & Custom Fields
334
+ </h3>
335
+ <p className="text-zinc-500 text-xs mb-4">
336
+ Bridges WordPress metadata and ACF (Advanced Custom Fields) directly to your React markup.
337
+ </p>
338
+ <div className="border border-zinc-300 p-4 bg-white font-mono text-xs">
339
+ <div className="mb-3">
340
+ <span className="block text-[9px] text-zinc-400 font-bold uppercase">Field: "author_bio"</span>
341
+ <span className="text-zinc-800 font-bold">{useWpCustomField("author_bio", "React Developer & WP theme engineer")}</span>
342
+ </div>
343
+ <div>
344
+ <span className="block text-[9px] text-zinc-400 font-bold uppercase">Field: "article_rating"</span>
345
+ <span className="text-zinc-800 font-bold">{useWpCustomField("article_rating", "โญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธ")}</span>
346
+ </div>
347
+ </div>
348
+ </div>
349
+
350
+ {/* Custom Query Loop */}
351
+ <div className="border border-zinc-200 bg-zinc-50 p-6">
352
+ <span className="inline-block bg-zinc-950 text-white text-[9px] font-mono font-black uppercase tracking-widest px-2 py-0.5 mb-4">
353
+ Hook: &lt;WpQueryLoop&gt;
354
+ </span>
355
+ <h3 className="font-serif font-black text-base text-zinc-900 mb-2">
356
+ Custom WP_Query Loop
357
+ </h3>
358
+ <p className="text-zinc-500 text-xs mb-4">
359
+ Fetches specialized collections of posts (like category grids or sidebars).
360
+ </p>
361
+ <div className="border border-zinc-300 p-3 bg-white space-y-2.5 max-h-[140px] overflow-y-auto">
362
+ <WpQueryLoop postType="post" postsPerPage={3}>
363
+ <div className="border border-zinc-100 p-2 hover:bg-zinc-50 transition-colors">
364
+ <h4 className="font-mono text-xs font-black text-zinc-950 uppercase tracking-tight truncate">
365
+ โšก Recent Block Post
366
+ </h4>
367
+ <span className="text-[9px] text-zinc-400 font-mono">Date: {new Date().toLocaleDateString()}</span>
368
+ </div>
369
+ </WpQueryLoop>
370
+ </div>
371
+ </div>
372
+ </div>
373
+ </div>
374
+ </section>
375
+
376
+ {/* Footer info console bar */}
377
+ <footer className="border-t-4 border-zinc-950 bg-zinc-950 text-white p-6 font-mono text-xs flex flex-col sm:flex-row justify-between items-center gap-4">
378
+ <div className="flex items-center gap-2">
379
+ <span className="inline-block w-2.5 h-2.5 bg-green-500 animate-pulse"></span>
380
+ <span>Vite Dev Environment active on port 5173</span>
381
+ </div>
382
+ <div>
383
+ <span>Press <kbd className="bg-zinc-800 px-1 py-0.5 border border-zinc-700">Ctrl + C</kbd> to exit console</span>
384
+ </div>
385
+ </footer>
386
+ </main>
387
+ </div>
388
+ );
389
+ }
390
+ `;
391
+
392
+ try {
393
+ writeFileSync(pagePath, pageContent, "utf8");
394
+ console.log(` ${pc.green("โœ… Reset core template")}: src/app/page.tsx`);
395
+ } catch (err) {
396
+ console.log(pc.red(` โŒ Failed to reset src/app/page.tsx: err.message`));
397
+ }
398
+
399
+ // 6. Automatically Trigger Monorepo CLI Template Synchronization
400
+ // If running inside the monorepo starter, sync the template as well!
401
+ const monorepoSyncScript = path.join(projectRoot, "..", "..", "scripts", "sync-cli-template.mjs");
402
+ if (existsSync(monorepoSyncScript)) {
403
+ console.log(`\n๐Ÿ“ฆ ${pc.bold("MONOREPO CONTEXT DETECTED")} โ€” Syncing CLI Template...`);
404
+ try {
405
+ const result = spawnSync("node", [monorepoSyncScript], {
406
+ stdio: "inherit",
407
+ shell: process.platform === "win32"
408
+ });
409
+ if (result.status === 0) {
410
+ console.log(`\n๐ŸŽ‰ ${pc.green("Successfully synced factory-fresh canvas directly to create-forgewp CLI template!")}`);
411
+ } else {
412
+ console.log(`\nโš ๏ธ CLI sync template script returned non-zero status code: ${result.status}`);
413
+ }
414
+ } catch (err) {
415
+ console.log(pc.red(`\nโŒ Failed to sync CLI template: ${err.message}`));
416
+ }
417
+ }
418
+
419
+ console.log("\n" + "โ”€".repeat(60));
420
+ console.log(pc.green(`\n๐ŸŽ‰ ${pc.bold("CANVAS RESET COMPLETE:")} Reverted workspace back to standard empty blueprints.`));
421
+ console.log(pc.cyan(" Your ForgeWP canvas is now factory-fresh, clean, and perfectly synced!\n"));
@@ -0,0 +1,130 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { existsSync, mkdirSync, writeFileSync } from "node:fs";
4
+ import path from "node:path";
5
+ import pc from "picocolors";
6
+
7
+ const args = process.argv.slice(2);
8
+ let rawBlockName = args.find(a => !a.startsWith("-"));
9
+
10
+ // Support --name TestimonialBlock or --name=TestimonialBlock
11
+ const nameIndex = args.indexOf("--name");
12
+ if (nameIndex !== -1 && args[nameIndex + 1]) {
13
+ rawBlockName = args[nameIndex + 1];
14
+ } else {
15
+ const nameEqual = args.find(a => a.startsWith("--name="));
16
+ if (nameEqual) {
17
+ rawBlockName = nameEqual.split("=")[1];
18
+ }
19
+ }
20
+
21
+ if (!rawBlockName) {
22
+ console.error(pc.red("\nโŒ Error: Please specify a block name."));
23
+ console.log(pc.cyan(" Example: pnpm forgewp make:block TestimonialBlock"));
24
+ console.log(pc.cyan(" Or: pnpm forgewp make:block --name TestimonialBlock\n"));
25
+ process.exit(1);
26
+ }
27
+
28
+ // 1. Sanitize & convert to PascalCase and kebab-case
29
+ const cleanName = rawBlockName.replace(/[^a-zA-Z0-9_-]/g, "");
30
+ if (!cleanName) {
31
+ console.error(pc.red("\nโŒ Error: Invalid block name. Use letters, numbers, dashes, and underscores.\n"));
32
+ process.exit(1);
33
+ }
34
+
35
+ // Convert to PascalCase: e.g., testimonial-block -> TestimonialBlock
36
+ const pascalCase = cleanName
37
+ .replace(/(?:^\w|[A-Z]|\b\w)/g, (word) => word.toUpperCase())
38
+ .replace(/[^a-zA-Z0-9]/g, "");
39
+
40
+ // Convert to Gutenberg Title: e.g., TestimonialBlock -> Testimonial Block
41
+ const readableTitle = pascalCase
42
+ .replace(/([A-Z])/g, " $1")
43
+ .trim();
44
+
45
+ // Ensure we are inside a ForgeWP theme workspace
46
+ const projectRoot = process.cwd();
47
+ const blocksDir = path.join(projectRoot, "src", "blocks");
48
+
49
+ if (!existsSync(path.join(projectRoot, "src"))) {
50
+ console.error(pc.red(`\nโŒ Error: "src" folder not found. Are you in your theme's root directory?\n`));
51
+ process.exit(1);
52
+ }
53
+
54
+ // Ensure src/blocks directory exists
55
+ if (!existsSync(blocksDir)) {
56
+ mkdirSync(blocksDir, { recursive: true });
57
+ }
58
+
59
+ const targetFile = path.join(blocksDir, `${pascalCase}.tsx`);
60
+
61
+ if (existsSync(targetFile)) {
62
+ console.error(pc.red(`\nโŒ Error: Block "${pascalCase}.tsx" already exists at src/blocks/\n`));
63
+ process.exit(1);
64
+ }
65
+
66
+ // 2. Parse custom attributes if provided via --attributes or --attrs
67
+ let attributesList = ["title", "description"];
68
+ const attrsArg = args.find(a => /^-+attributes=/.test(a) || /^-+attrs=/.test(a));
69
+ if (attrsArg) {
70
+ const rawAttrs = attrsArg.split("=")[1];
71
+ if (rawAttrs) {
72
+ attributesList = rawAttrs.split(",").map(a => a.trim()).filter(Boolean);
73
+ }
74
+ } else {
75
+ const attrsIndex = args.findIndex(a => /^-+attributes$/.test(a) || /^-+attrs$/.test(a));
76
+ if (attrsIndex !== -1 && args[attrsIndex + 1]) {
77
+ attributesList = args[attrsIndex + 1].split(",").map(a => a.trim()).filter(Boolean);
78
+ }
79
+ }
80
+
81
+ const propSignature = attributesList.map(a => `${a}: string`).join("; ");
82
+ const propDestructuring = attributesList.join(", ");
83
+
84
+ const layoutMarkup = attributesList.map((attr, index) => {
85
+ if (index === 0) {
86
+ return `<h3 className="text-2xl font-black text-zinc-950 uppercase tracking-tight leading-none mb-3">
87
+ {${attr}}
88
+ </h3>`;
89
+ }
90
+ if (attr.toLowerCase().includes("image") || attr.toLowerCase().includes("pic") || attr.toLowerCase().includes("img")) {
91
+ return `<img src={${attr}} alt="Block Media" className="w-full border-2 border-zinc-950 mb-3" />`;
92
+ }
93
+ return `<p className="text-sm text-zinc-600 font-medium font-sans leading-relaxed mb-3">
94
+ {${attr}}
95
+ </p>`;
96
+ }).join("\n ");
97
+
98
+ const attributesRegistry = attributesList.map(a => {
99
+ return ` ${a}: { type: "string", default: "Customize ${a} here" }`;
100
+ }).join(",\n");
101
+
102
+ // 3. Generate the beautiful Neo-Brutalist React block template
103
+ const blockTemplate = `export default function ${pascalCase}({ ${propDestructuring} }: { ${propSignature} }) {
104
+ return (
105
+ <div className="p-8 bg-white border-4 border-zinc-950 shadow-[6px_6px_0px_0px_rgba(0,0,0,1)] rounded-none my-6 selection:bg-brand selection:text-white">
106
+ <span className="inline-block bg-brand text-white text-xs font-mono font-bold uppercase tracking-wider px-2 py-0.5 mb-3 border-2 border-zinc-950">
107
+ Gutenberg Custom Block
108
+ </span>
109
+ ${layoutMarkup}
110
+ </div>
111
+ );
112
+ }
113
+
114
+ export const settings = {
115
+ title: "Sharp ${readableTitle}",
116
+ icon: "admin-post", // Choose icons from: https://developer.wordpress.org/resource/dashicons/
117
+ category: "design",
118
+ attributes: {
119
+ ${attributesRegistry}
120
+ }
121
+ };
122
+ `;
123
+
124
+ // 4. Write template out to disk
125
+ writeFileSync(targetFile, blockTemplate, "utf8");
126
+
127
+ console.log(pc.green(`\nโšก Block "${pascalCase}" successfully created!`));
128
+ console.log(` Location: ${pc.cyan(`src/blocks/${pascalCase}.tsx`)}`);
129
+ console.log(` Gutenberg Title: ${pc.yellow(`Sharp ${readableTitle}`)}`);
130
+ console.log(`\n๐ŸŽ‰ Run ${pc.cyan("pnpm export")} to automatically register it inside your WordPress theme!\n`);