@forgewp/compiler 0.1.9 โ 0.2.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/bin/fresh.js +55 -0
- package/lib/generate-theme.js +204 -4
- package/package.json +2 -2
package/bin/fresh.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import { rmSync, existsSync, writeFileSync, mkdirSync, readdirSync } from "node:fs";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import { spawnSync } from "node:child_process";
|
|
6
|
+
import readline from "node:readline";
|
|
6
7
|
import pc from "picocolors";
|
|
7
8
|
import { SYSTEM_BLUEPRINTS } from "../lib/blueprints.js";
|
|
8
9
|
|
|
@@ -10,6 +11,8 @@ console.log(`\n๐งน ${pc.bold(pc.bgRed(pc.black(" FORGEWP FACTORY RESET (FRESH
|
|
|
10
11
|
|
|
11
12
|
const projectRoot = process.cwd();
|
|
12
13
|
|
|
14
|
+
function runReset() {
|
|
15
|
+
|
|
13
16
|
// 1. Clean Build Caches & Artifacts
|
|
14
17
|
const foldersToClean = [".forgewp", "dist", ".vite"];
|
|
15
18
|
for (const folder of foldersToClean) {
|
|
@@ -419,3 +422,55 @@ if (existsSync(monorepoSyncScript)) {
|
|
|
419
422
|
console.log("\n" + "โ".repeat(60));
|
|
420
423
|
console.log(pc.green(`\n๐ ${pc.bold("CANVAS RESET COMPLETE:")} Reverted workspace back to standard empty blueprints.`));
|
|
421
424
|
console.log(pc.cyan(" Your ForgeWP canvas is now factory-fresh, clean, and perfectly synced!\n"));
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
function hasWorkBeenDone() {
|
|
428
|
+
const dirsToCheck = [
|
|
429
|
+
"src/app/pages",
|
|
430
|
+
"src/blocks",
|
|
431
|
+
"src/components"
|
|
432
|
+
];
|
|
433
|
+
for (const dir of dirsToCheck) {
|
|
434
|
+
const fullPath = path.join(projectRoot, dir);
|
|
435
|
+
if (existsSync(fullPath)) {
|
|
436
|
+
try {
|
|
437
|
+
const files = readdirSync(fullPath);
|
|
438
|
+
if (files.length > 0) {
|
|
439
|
+
return true;
|
|
440
|
+
}
|
|
441
|
+
} catch (e) {
|
|
442
|
+
// Directory exists but failed to read or is empty
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
return false;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
const isForce = process.argv.includes("--force") || process.argv.includes("-f") || process.argv.includes("-y");
|
|
450
|
+
|
|
451
|
+
if (hasWorkBeenDone() && !isForce) {
|
|
452
|
+
if (!process.stdin.isTTY) {
|
|
453
|
+
console.log(pc.red(`โ Non-interactive environment detected. Please use the --force flag to reset active work.`));
|
|
454
|
+
process.exit(1);
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
const rl = readline.createInterface({
|
|
458
|
+
input: process.stdin,
|
|
459
|
+
output: process.stdout
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
console.log(pc.yellow(`โ ๏ธ ${pc.bold("WARNING:")} Existing custom pages, blocks, or components detected in your canvas!`));
|
|
463
|
+
console.log(pc.yellow(` Continuing will permanently delete all custom work and reset back to factory default.`));
|
|
464
|
+
|
|
465
|
+
rl.question(`\n Are you sure you want to proceed? (y/N): `, (answer) => {
|
|
466
|
+
rl.close();
|
|
467
|
+
const confirmed = answer.trim().toLowerCase() === "y" || answer.trim().toLowerCase() === "yes";
|
|
468
|
+
if (!confirmed) {
|
|
469
|
+
console.log(pc.blue(`\nโ Reset aborted. No changes were made.\n`));
|
|
470
|
+
process.exit(0);
|
|
471
|
+
}
|
|
472
|
+
runReset();
|
|
473
|
+
});
|
|
474
|
+
} else {
|
|
475
|
+
runReset();
|
|
476
|
+
}
|
package/lib/generate-theme.js
CHANGED
|
@@ -31,6 +31,83 @@ export function generateTheme({
|
|
|
31
31
|
|
|
32
32
|
mkdirSync(outDir, { recursive: true });
|
|
33
33
|
|
|
34
|
+
// Load menus configuration for auto-registration and setup
|
|
35
|
+
let menus = {};
|
|
36
|
+
const menusJsonSrc = path.join(themeRoot, "wordpress", "menus.json");
|
|
37
|
+
if (existsSync(menusJsonSrc)) {
|
|
38
|
+
try {
|
|
39
|
+
menus = JSON.parse(readFileSync(menusJsonSrc, "utf8"));
|
|
40
|
+
} catch (e) {
|
|
41
|
+
console.warn("Failed to parse menus.json:", e.message);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Gather compiled custom page templates for auto-creation with smart slug mapping
|
|
46
|
+
const pagesToAutoCreate = [];
|
|
47
|
+
const forgewpDir = path.join(themeRoot, ".forgewp");
|
|
48
|
+
if (existsSync(forgewpDir)) {
|
|
49
|
+
const templateFiles = readdirSync(forgewpDir);
|
|
50
|
+
for (const file of templateFiles) {
|
|
51
|
+
if (file.startsWith("template-") && file.endsWith(".html") && !file.includes("-head")) {
|
|
52
|
+
const slug = file.replace(".html", "");
|
|
53
|
+
const pageTemplateSlug = slug.replace("template-", "");
|
|
54
|
+
|
|
55
|
+
let matchedSlug = null;
|
|
56
|
+
let matchedTitle = null;
|
|
57
|
+
|
|
58
|
+
const toKebabTemplateSlug = (str) => {
|
|
59
|
+
const compName = str
|
|
60
|
+
.replace(/[^a-zA-Z0-9]/g, " ")
|
|
61
|
+
.trim()
|
|
62
|
+
.split(/\s+/)
|
|
63
|
+
.map(w => w.charAt(0).toUpperCase() + w.slice(1).toLowerCase())
|
|
64
|
+
.join("") + "Page";
|
|
65
|
+
return compName.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const targetKebab = pageTemplateSlug;
|
|
69
|
+
|
|
70
|
+
// Search in menus to find matching target URL slugs defined by the developer
|
|
71
|
+
for (const [location, items] of Object.entries(menus)) {
|
|
72
|
+
if (!Array.isArray(items)) continue;
|
|
73
|
+
for (const item of items) {
|
|
74
|
+
if (!item.url || !item.url.startsWith("/")) continue;
|
|
75
|
+
const menuUrlSlug = item.url.replace(/^\//, "");
|
|
76
|
+
|
|
77
|
+
if (
|
|
78
|
+
toKebabTemplateSlug(item.title) === targetKebab ||
|
|
79
|
+
toKebabTemplateSlug(menuUrlSlug) === targetKebab
|
|
80
|
+
) {
|
|
81
|
+
matchedSlug = menuUrlSlug;
|
|
82
|
+
matchedTitle = item.title;
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (matchedSlug) break;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Fallback 1: Strip trailing "-page" from template slug
|
|
90
|
+
if (!matchedSlug) {
|
|
91
|
+
matchedSlug = pageTemplateSlug.endsWith("-page")
|
|
92
|
+
? pageTemplateSlug.slice(0, -5)
|
|
93
|
+
: pageTemplateSlug;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Fallback 2: Generate clean human-readable title
|
|
97
|
+
if (!matchedTitle) {
|
|
98
|
+
const cleanName = pageTemplateSlug.endsWith("-page") ? pageTemplateSlug.slice(0, -5) : pageTemplateSlug;
|
|
99
|
+
matchedTitle = cleanName.split("-").map(w => w.charAt(0).toUpperCase() + w.slice(1)).join(" ");
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
pagesToAutoCreate.push({
|
|
103
|
+
title: matchedTitle,
|
|
104
|
+
slug: matchedSlug,
|
|
105
|
+
template: `page-${pageTemplateSlug}.php`
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
34
111
|
const assetsOut = path.join(outDir, "assets");
|
|
35
112
|
const distAssets = path.join(themeRoot, "dist", "assets");
|
|
36
113
|
mkdirSync(assetsOut, { recursive: true });
|
|
@@ -161,7 +238,7 @@ if (window.forgeWpBlocks) {
|
|
|
161
238
|
writeFileSync(path.join(outDir, "style.css"), buildStyleCss(config), "utf8");
|
|
162
239
|
writeFileSync(
|
|
163
240
|
path.join(outDir, "functions.php"),
|
|
164
|
-
buildFunctionsPhp(config, assets, compiledBlocks, themeRoot),
|
|
241
|
+
buildFunctionsPhp(config, assets, compiledBlocks, themeRoot, pagesToAutoCreate, menus),
|
|
165
242
|
"utf8",
|
|
166
243
|
);
|
|
167
244
|
writeFileSync(path.join(outDir, "header.php"), buildHeaderPhp(config), "utf8");
|
|
@@ -174,7 +251,6 @@ if (window.forgeWpBlocks) {
|
|
|
174
251
|
writeFileSync(path.join(outDir, "page.php"), buildPagePhp(), "utf8");
|
|
175
252
|
|
|
176
253
|
// Dynamic Custom Page Templates Compiler
|
|
177
|
-
const forgewpDir = path.join(themeRoot, ".forgewp");
|
|
178
254
|
if (existsSync(forgewpDir)) {
|
|
179
255
|
const templateFiles = readdirSync(forgewpDir);
|
|
180
256
|
for (const file of templateFiles) {
|
|
@@ -418,7 +494,7 @@ Text Domain: ${config.textDomain}
|
|
|
418
494
|
* @param {import('./types.js').ForgeWPThemeConfig} config
|
|
419
495
|
* @param {import('./types.js').ForgeWPBuildAssets} assets
|
|
420
496
|
*/
|
|
421
|
-
function buildFunctionsPhp(config, assets, blockSlugs = [], themeRoot = "") {
|
|
497
|
+
function buildFunctionsPhp(config, assets, blockSlugs = [], themeRoot = "", pagesToAutoCreate = [], menus = {}) {
|
|
422
498
|
const css = assets.cssFile.replace(/^assets\//, "");
|
|
423
499
|
const version = config.version.replace(/'/g, "\\'");
|
|
424
500
|
const googleFonts = config.settings?.typography?.googleFonts || [];
|
|
@@ -535,6 +611,129 @@ add_action('enqueue_block_editor_assets', 'forgewp_enqueue_block_editor_assets')
|
|
|
535
611
|
`;
|
|
536
612
|
}
|
|
537
613
|
|
|
614
|
+
let registerMenusPhp = "";
|
|
615
|
+
const menuLocations = Object.keys(menus).filter(k => !k.startsWith("_"));
|
|
616
|
+
if (menuLocations.length > 0) {
|
|
617
|
+
registerMenusPhp = `
|
|
618
|
+
// Register custom navigation menus from sitemap config
|
|
619
|
+
register_nav_menus(array(
|
|
620
|
+
${menuLocations.map(loc => ` '${loc}' => __('${loc.charAt(0).toUpperCase() + loc.slice(1)} Navigation', '${config.textDomain}'),`).join("\n")}
|
|
621
|
+
));`;
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
const pagesPhpArray = pagesToAutoCreate.map(p => {
|
|
625
|
+
return ` array(
|
|
626
|
+
'title' => '${p.title.replace(/'/g, "\\'")}',
|
|
627
|
+
'slug' => '${p.slug.replace(/'/g, "\\'")}',
|
|
628
|
+
'template' => '${p.template.replace(/'/g, "\\'")}',
|
|
629
|
+
)`;
|
|
630
|
+
}).join(",\n");
|
|
631
|
+
|
|
632
|
+
const menuItemsPhpArray = Object.entries(menus)
|
|
633
|
+
.filter(([loc]) => !loc.startsWith("_"))
|
|
634
|
+
.map(([loc, items]) => {
|
|
635
|
+
const itemsPhp = items.map(item => {
|
|
636
|
+
return ` array(
|
|
637
|
+
'title' => '${item.title.replace(/'/g, "\\'")}',
|
|
638
|
+
'url' => '${item.url.replace(/'/g, "\\'")}',
|
|
639
|
+
)`;
|
|
640
|
+
}).join(",\n");
|
|
641
|
+
return ` '${loc}' => array(
|
|
642
|
+
${itemsPhp}
|
|
643
|
+
)`;
|
|
644
|
+
}).join(",\n");
|
|
645
|
+
|
|
646
|
+
const autoCreationPhp = `
|
|
647
|
+
/**
|
|
648
|
+
* Automatically create routes defined in menus.json and assign custom page templates upon theme activation.
|
|
649
|
+
*/
|
|
650
|
+
function forgewp_auto_create_pages_and_menus(): void {
|
|
651
|
+
$activated_option = '${config.textDomain}_activated_' . str_replace('.', '_', FORGEWP_THEME_VERSION);
|
|
652
|
+
if (get_option($activated_option) === 'yes') {
|
|
653
|
+
return;
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
$pages = array(
|
|
657
|
+
${pagesPhpArray}
|
|
658
|
+
);
|
|
659
|
+
|
|
660
|
+
foreach ($pages as $p) {
|
|
661
|
+
$existing_page = get_page_by_path($p['slug']);
|
|
662
|
+
if (!$existing_page) {
|
|
663
|
+
$page_id = wp_insert_post(array(
|
|
664
|
+
'post_title' => $p['title'],
|
|
665
|
+
'post_name' => $p['slug'],
|
|
666
|
+
'post_status' => 'publish',
|
|
667
|
+
'post_type' => 'page',
|
|
668
|
+
));
|
|
669
|
+
if (!is_wp_error($page_id) && $page_id > 0) {
|
|
670
|
+
update_post_meta($page_id, '_wp_page_template', $p['template']);
|
|
671
|
+
}
|
|
672
|
+
} else {
|
|
673
|
+
update_post_meta($existing_page->ID, '_wp_page_template', $p['template']);
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
$menu_structure = array(
|
|
678
|
+
${menuItemsPhpArray}
|
|
679
|
+
);
|
|
680
|
+
|
|
681
|
+
foreach ($menu_structure as $location => $items) {
|
|
682
|
+
$menu_name = ucfirst($location) . ' Navigation';
|
|
683
|
+
$menu_exists = wp_get_nav_menu_object($menu_name);
|
|
684
|
+
|
|
685
|
+
if (!$menu_exists) {
|
|
686
|
+
$menu_id = wp_create_nav_menu($menu_name);
|
|
687
|
+
if (!is_wp_error($menu_id)) {
|
|
688
|
+
$locations = get_theme_mod('nav_menu_locations');
|
|
689
|
+
if (!is_array($locations)) {
|
|
690
|
+
$locations = array();
|
|
691
|
+
}
|
|
692
|
+
$locations[$location] = $menu_id;
|
|
693
|
+
set_theme_mod('nav_menu_locations', $locations);
|
|
694
|
+
|
|
695
|
+
foreach ($items as $item) {
|
|
696
|
+
$item_title = $item['title'];
|
|
697
|
+
$item_url = $item['url'];
|
|
698
|
+
|
|
699
|
+
$object_id = 0;
|
|
700
|
+
$object_type = 'custom';
|
|
701
|
+
$target_url = $item_url;
|
|
702
|
+
|
|
703
|
+
if (strpos($item_url, '/') === 0) {
|
|
704
|
+
$slug = trim($item_url, '/');
|
|
705
|
+
if ($slug === '') {
|
|
706
|
+
$target_url = home_url('/');
|
|
707
|
+
} else {
|
|
708
|
+
$page = get_page_by_path($slug);
|
|
709
|
+
if ($page) {
|
|
710
|
+
$object_id = $page->ID;
|
|
711
|
+
$object_type = 'page';
|
|
712
|
+
$target_url = get_permalink($page->ID);
|
|
713
|
+
} else {
|
|
714
|
+
$target_url = home_url($item_url);
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
wp_update_nav_menu_item($menu_id, 0, array(
|
|
720
|
+
'menu-item-title' => $item_title,
|
|
721
|
+
'menu-item-url' => $target_url,
|
|
722
|
+
'menu-item-object-id' => $object_id,
|
|
723
|
+
'menu-item-object' => $object_type === 'page' ? 'page' : '',
|
|
724
|
+
'menu-item-type' => $object_type === 'page' ? 'post_type' : 'custom',
|
|
725
|
+
'menu-item-status' => 'publish',
|
|
726
|
+
));
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
update_option($activated_option, 'yes');
|
|
733
|
+
}
|
|
734
|
+
add_action('after_switch_theme', 'forgewp_auto_create_pages_and_menus');
|
|
735
|
+
`;
|
|
736
|
+
|
|
538
737
|
return `<?php
|
|
539
738
|
/**
|
|
540
739
|
* ${config.name} โ generated by ForgeWP
|
|
@@ -579,9 +778,10 @@ function forgewp_theme_setup(): void {
|
|
|
579
778
|
|
|
580
779
|
// Load compiled theme stylesheet inside Gutenberg Block Editor
|
|
581
780
|
add_editor_style('assets/${css}');
|
|
781
|
+
${registerMenusPhp}
|
|
582
782
|
}
|
|
583
783
|
add_action('after_setup_theme', 'forgewp_theme_setup');
|
|
584
|
-
${preconnectFilter}${blocksRegistration}${cptRegistration}`;
|
|
784
|
+
${preconnectFilter}${blocksRegistration}${cptRegistration}${autoCreationPhp}`;
|
|
585
785
|
}
|
|
586
786
|
|
|
587
787
|
function buildHeaderPhp(config) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@forgewp/compiler",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Compile ForgeWP React themes into installable WordPress themes",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"jiti": "^2.6.1",
|
|
26
26
|
"picocolors": "^1.1.1",
|
|
27
27
|
"react-helmet-async": "^3.0.0",
|
|
28
|
-
"@forgewp/ui": "0.
|
|
28
|
+
"@forgewp/ui": "0.2.0"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@types/node": "^22.19.19"
|