@forgewp/compiler 0.1.2 → 0.1.8
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/README.md +29 -2
- package/bin/add.js +14 -2
- package/bin/clean.js +37 -0
- package/bin/cli.js +87 -0
- package/bin/doctor.js +99 -0
- package/bin/export.js +0 -0
- package/bin/make-block.js +130 -0
- package/bin/make-component.js +200 -0
- package/bin/make-post-type.js +129 -0
- package/bin/repair.js +62 -0
- package/lib/add-component.js +5 -2
- package/lib/blueprints.js +497 -0
- package/lib/export-theme.js +11 -0
- package/lib/generate-theme.js +531 -17
- package/lib/index.d.ts +61 -0
- package/lib/index.js +1 -0
- package/lib/render-static.js +20 -0
- package/lib/render-theme.mts +200 -48
- package/lib/validate.js +54 -0
- package/package.json +11 -5
package/lib/index.js
CHANGED
package/lib/render-static.js
CHANGED
|
@@ -53,6 +53,11 @@ export async function renderStaticMarkup(themeRoot) {
|
|
|
53
53
|
|
|
54
54
|
const headerHtmlPath = path.join(outDir, "header.html");
|
|
55
55
|
const footerHtmlPath = path.join(outDir, "footer.html");
|
|
56
|
+
const headHtmlPath = path.join(outDir, "head.html");
|
|
57
|
+
const singleHtmlPath = path.join(outDir, "single.html");
|
|
58
|
+
const singleHeadHtmlPath = path.join(outDir, "single-head.html");
|
|
59
|
+
const notFoundHtmlPath = path.join(outDir, "404.html");
|
|
60
|
+
const archiveHtmlPath = path.join(outDir, "archive.html");
|
|
56
61
|
|
|
57
62
|
return {
|
|
58
63
|
appHtml: readFileSync(appHtmlPath, "utf8"),
|
|
@@ -62,5 +67,20 @@ export async function renderStaticMarkup(themeRoot) {
|
|
|
62
67
|
footerHtml: existsSync(footerHtmlPath)
|
|
63
68
|
? readFileSync(footerHtmlPath, "utf8")
|
|
64
69
|
: "",
|
|
70
|
+
headHtml: existsSync(headHtmlPath)
|
|
71
|
+
? readFileSync(headHtmlPath, "utf8")
|
|
72
|
+
: "",
|
|
73
|
+
singleHtml: existsSync(singleHtmlPath)
|
|
74
|
+
? readFileSync(singleHtmlPath, "utf8")
|
|
75
|
+
: "",
|
|
76
|
+
singleHeadHtml: existsSync(singleHeadHtmlPath)
|
|
77
|
+
? readFileSync(singleHeadHtmlPath, "utf8")
|
|
78
|
+
: "",
|
|
79
|
+
notFoundHtml: existsSync(notFoundHtmlPath)
|
|
80
|
+
? readFileSync(notFoundHtmlPath, "utf8")
|
|
81
|
+
: "",
|
|
82
|
+
archiveHtml: existsSync(archiveHtmlPath)
|
|
83
|
+
? readFileSync(archiveHtmlPath, "utf8")
|
|
84
|
+
: "",
|
|
65
85
|
};
|
|
66
86
|
}
|
package/lib/render-theme.mts
CHANGED
|
@@ -1,48 +1,200 @@
|
|
|
1
|
-
import { mkdirSync, writeFileSync } from "node:fs";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import { pathToFileURL } from "node:url";
|
|
4
|
-
import { createRequire } from "node:module";
|
|
5
|
-
|
|
6
|
-
const themeRoot = process.argv[2];
|
|
7
|
-
|
|
8
|
-
if (!themeRoot) {
|
|
9
|
-
console.error("Usage: render-theme.mts <theme-root>");
|
|
10
|
-
process.exit(1);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
const
|
|
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
|
-
|
|
1
|
+
import { existsSync, readFileSync, mkdirSync, writeFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { pathToFileURL } from "node:url";
|
|
4
|
+
import { createRequire } from "node:module";
|
|
5
|
+
|
|
6
|
+
const themeRoot = process.argv[2];
|
|
7
|
+
|
|
8
|
+
if (!themeRoot) {
|
|
9
|
+
console.error("Usage: render-theme.mts <theme-root>");
|
|
10
|
+
process.exit(1);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Use the theme's own node_modules for ALL packages (React 19)
|
|
14
|
+
const require = createRequire(path.join(themeRoot, "package.json"));
|
|
15
|
+
const React = require("react");
|
|
16
|
+
const { renderToStaticMarkup } = require("react-dom/server");
|
|
17
|
+
|
|
18
|
+
const appUrl = pathToFileURL(path.join(themeRoot, "src", "app", "page.tsx")).href;
|
|
19
|
+
const headerUrl = pathToFileURL(path.join(themeRoot, "src", "components", "SiteHeader.tsx")).href;
|
|
20
|
+
const footerUrl = pathToFileURL(path.join(themeRoot, "src", "components", "SiteFooter.tsx")).href;
|
|
21
|
+
const layoutUrl = pathToFileURL(path.join(themeRoot, "src", "app", "layout.tsx")).href;
|
|
22
|
+
const layoutPath = path.join(themeRoot, "src", "app", "layout.tsx");
|
|
23
|
+
|
|
24
|
+
// ── Load pages ────────────────────────────────────────────────────────────────
|
|
25
|
+
const { default: App } = await import(appUrl);
|
|
26
|
+
|
|
27
|
+
let RootLayout: (props: any) => any = ({ children }: any) =>
|
|
28
|
+
React.createElement(React.Fragment, null, children);
|
|
29
|
+
try {
|
|
30
|
+
const { default: LoadedLayout } = await import(layoutUrl);
|
|
31
|
+
RootLayout = LoadedLayout;
|
|
32
|
+
} catch {
|
|
33
|
+
console.warn("RootLayout not found, using Fragment fallback");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// ── Render a page inside RootLayout ──────────────────────────────────────────
|
|
37
|
+
function renderPage(PageComponent: any): string {
|
|
38
|
+
return renderToStaticMarkup(
|
|
39
|
+
React.createElement(RootLayout, null, React.createElement(PageComponent))
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// ── SEO extraction from source files (React 19 SSR-safe) ─────────────────────
|
|
44
|
+
// react-helmet-async v3 disabled server-side context for React 19 —
|
|
45
|
+
// HelmetProvider is a Fragment at SSR time so helmetContext.helmet is never set.
|
|
46
|
+
// Instead we parse <SEO .../> props directly from the source TSX file.
|
|
47
|
+
|
|
48
|
+
function extractSeoPropsFromSource(filePath: string): Record<string, string> {
|
|
49
|
+
if (!existsSync(filePath)) return {};
|
|
50
|
+
|
|
51
|
+
let src = readFileSync(filePath, "utf8");
|
|
52
|
+
|
|
53
|
+
// Strip JSX block comments so we don't match <SEO /> mentions inside them
|
|
54
|
+
src = src.replace(/\{\/\*[\s\S]*?\*\/\}/g, "");
|
|
55
|
+
|
|
56
|
+
// Find the first <SEO ... /> (multiline, s flag)
|
|
57
|
+
const seoMatch = src.match(/<SEO\b([\s\S]*?)\/>/s);
|
|
58
|
+
if (!seoMatch) return {};
|
|
59
|
+
|
|
60
|
+
const attribsStr = seoMatch[1];
|
|
61
|
+
const props: Record<string, string> = {};
|
|
62
|
+
|
|
63
|
+
// Double-quoted values: prop="value with & and spaces"
|
|
64
|
+
let m: RegExpExecArray | null;
|
|
65
|
+
const dqPattern = /(\w+)\s*=\s*"([^"]*)"/g;
|
|
66
|
+
while ((m = dqPattern.exec(attribsStr)) !== null) props[m[1]] = m[2];
|
|
67
|
+
|
|
68
|
+
// Single-quoted values: prop='value'
|
|
69
|
+
const sqPattern = /(\w+)\s*=\s*'([^']*)'/g;
|
|
70
|
+
while ((m = sqPattern.exec(attribsStr)) !== null) if (!props[m[1]]) props[m[1]] = m[2];
|
|
71
|
+
|
|
72
|
+
// JSX string expressions: prop={"value"} or prop={'value'}
|
|
73
|
+
const jsxPattern = /(\w+)\s*=\s*\{["']([^"']*)["']\}/g;
|
|
74
|
+
while ((m = jsxPattern.exec(attribsStr)) !== null) if (!props[m[1]]) props[m[1]] = m[2];
|
|
75
|
+
|
|
76
|
+
return props;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function buildHeadHtml(
|
|
80
|
+
base: Record<string, string>,
|
|
81
|
+
override: Record<string, string> = {}
|
|
82
|
+
): string {
|
|
83
|
+
// Merge: per-page overrides win over layout defaults
|
|
84
|
+
const p = { ...base, ...override };
|
|
85
|
+
|
|
86
|
+
const {
|
|
87
|
+
title,
|
|
88
|
+
description,
|
|
89
|
+
keywords,
|
|
90
|
+
ogTitle,
|
|
91
|
+
ogDescription,
|
|
92
|
+
ogImage,
|
|
93
|
+
ogType = "website",
|
|
94
|
+
twitterCard = "summary_large_image",
|
|
95
|
+
twitterCreator,
|
|
96
|
+
canonical,
|
|
97
|
+
} = p;
|
|
98
|
+
|
|
99
|
+
const tags: string[] = [];
|
|
100
|
+
|
|
101
|
+
if (title && !title.includes("FORGEWP")) tags.push(`<title>${title}</title>`);
|
|
102
|
+
if (description) tags.push(`<meta name="description" content="${description}">`);
|
|
103
|
+
if (keywords) tags.push(`<meta name="keywords" content="${keywords}">`);
|
|
104
|
+
if (canonical) tags.push(`<link rel="canonical" href="${canonical}">`);
|
|
105
|
+
|
|
106
|
+
// Open Graph
|
|
107
|
+
tags.push(`<meta property="og:type" content="${ogType}">`);
|
|
108
|
+
const ogTitleVal = ogTitle || title;
|
|
109
|
+
if (ogTitleVal && !ogTitleVal.includes("FORGEWP")) {
|
|
110
|
+
tags.push(`<meta property="og:title" content="${ogTitleVal}">`);
|
|
111
|
+
}
|
|
112
|
+
const ogDescVal = ogDescription || description;
|
|
113
|
+
if (ogDescVal) tags.push(`<meta property="og:description" content="${ogDescVal}">`);
|
|
114
|
+
if (ogImage) tags.push(`<meta property="og:image" content="${ogImage}">`);
|
|
115
|
+
|
|
116
|
+
// Twitter
|
|
117
|
+
tags.push(`<meta name="twitter:card" content="${twitterCard}">`);
|
|
118
|
+
if (ogTitleVal && !ogTitleVal.includes("FORGEWP")) {
|
|
119
|
+
tags.push(`<meta name="twitter:title" content="${ogTitleVal}">`);
|
|
120
|
+
}
|
|
121
|
+
if (ogDescVal) tags.push(`<meta name="twitter:description" content="${ogDescVal}">`);
|
|
122
|
+
if (ogImage) tags.push(`<meta name="twitter:image" content="${ogImage}">`);
|
|
123
|
+
if (twitterCreator) tags.push(`<meta name="twitter:creator" content="${twitterCreator}">`);
|
|
124
|
+
|
|
125
|
+
return tags.join("\n");
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// ── Render header / footer fragments ─────────────────────────────────────────
|
|
129
|
+
let headerHtml = "";
|
|
130
|
+
try {
|
|
131
|
+
const { SiteHeader } = await import(headerUrl);
|
|
132
|
+
headerHtml = renderToStaticMarkup(React.createElement(SiteHeader));
|
|
133
|
+
} catch {
|
|
134
|
+
console.warn("SiteHeader not found, skipping separate render");
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
let footerHtml = "";
|
|
138
|
+
try {
|
|
139
|
+
const { SiteFooter } = await import(footerUrl);
|
|
140
|
+
footerHtml = renderToStaticMarkup(React.createElement(SiteFooter));
|
|
141
|
+
} catch {
|
|
142
|
+
console.warn("SiteFooter not found, skipping separate render");
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// ── Render pages ──────────────────────────────────────────────────────────────
|
|
146
|
+
const appHtml = renderPage(App);
|
|
147
|
+
|
|
148
|
+
// single.tsx
|
|
149
|
+
const singlePath = path.join(themeRoot, "src", "app", "single.tsx");
|
|
150
|
+
let singleHtml = "";
|
|
151
|
+
if (existsSync(singlePath)) {
|
|
152
|
+
const { default: SinglePage } = await import(pathToFileURL(singlePath).href);
|
|
153
|
+
singleHtml = renderPage(SinglePage);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// 404.tsx — rendered bare, no RootLayout (WP 404.php calls get_header/get_footer)
|
|
157
|
+
const notFoundPath = path.join(themeRoot, "src", "app", "404.tsx");
|
|
158
|
+
let notFoundHtml = "";
|
|
159
|
+
if (existsSync(notFoundPath)) {
|
|
160
|
+
const { default: NotFoundPage } = await import(pathToFileURL(notFoundPath).href);
|
|
161
|
+
notFoundHtml = renderToStaticMarkup(React.createElement(NotFoundPage));
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// archive.tsx
|
|
165
|
+
const archivePath = path.join(themeRoot, "src", "app", "archive.tsx");
|
|
166
|
+
let archiveHtml = "";
|
|
167
|
+
if (existsSync(archivePath)) {
|
|
168
|
+
const { default: ArchivePage } = await import(pathToFileURL(archivePath).href);
|
|
169
|
+
archiveHtml = renderPage(ArchivePage);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// ── Extract SEO from source files ─────────────────────────────────────────────
|
|
173
|
+
// Global defaults come from layout.tsx <SEO> props.
|
|
174
|
+
// Per-page overrides come from the individual page file's <SEO> props.
|
|
175
|
+
const layoutSeo = extractSeoPropsFromSource(layoutPath);
|
|
176
|
+
const singleSeo = existsSync(singlePath)
|
|
177
|
+
? extractSeoPropsFromSource(singlePath)
|
|
178
|
+
: {};
|
|
179
|
+
|
|
180
|
+
// Global head.html (used on all pages that don't have a per-page override)
|
|
181
|
+
const headHtml = buildHeadHtml(layoutSeo);
|
|
182
|
+
|
|
183
|
+
// Per-page head HTML for single.html (merged: layout defaults + single overrides)
|
|
184
|
+
const singleHeadHtml = buildHeadHtml(layoutSeo, singleSeo);
|
|
185
|
+
|
|
186
|
+
// ── Write outputs ─────────────────────────────────────────────────────────────
|
|
187
|
+
const outDir = path.join(themeRoot, ".forgewp");
|
|
188
|
+
mkdirSync(outDir, { recursive: true });
|
|
189
|
+
|
|
190
|
+
writeFileSync(path.join(outDir, "header.html"), headerHtml, "utf8");
|
|
191
|
+
writeFileSync(path.join(outDir, "footer.html"), footerHtml, "utf8");
|
|
192
|
+
writeFileSync(path.join(outDir, "app.html"), appHtml, "utf8");
|
|
193
|
+
writeFileSync(path.join(outDir, "head.html"), headHtml, "utf8");
|
|
194
|
+
writeFileSync(path.join(outDir, "single-head.html"), singleHeadHtml, "utf8");
|
|
195
|
+
|
|
196
|
+
if (singleHtml) writeFileSync(path.join(outDir, "single.html"), singleHtml, "utf8");
|
|
197
|
+
if (notFoundHtml) writeFileSync(path.join(outDir, "404.html"), notFoundHtml, "utf8");
|
|
198
|
+
if (archiveHtml) writeFileSync(path.join(outDir, "archive.html"), archiveHtml, "utf8");
|
|
199
|
+
|
|
200
|
+
process.stdout.write(outDir);
|
package/lib/validate.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { existsSync, writeFileSync, mkdirSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import pc from "picocolors";
|
|
4
|
+
import { SYSTEM_BLUEPRINTS } from "./blueprints.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Preflight Check & Self-Healing Validator
|
|
8
|
+
*
|
|
9
|
+
* Runs before compiling the theme.
|
|
10
|
+
* - Automatically restores any deleted system-critical utility files using pre-compiled blueprints.
|
|
11
|
+
* - Throws descriptive, highly actionable errors if a user-editable core file is deleted.
|
|
12
|
+
*
|
|
13
|
+
* @param {string} themeRoot Absolute path to the theme root folder.
|
|
14
|
+
*/
|
|
15
|
+
export function validateCriticalFiles(themeRoot) {
|
|
16
|
+
const USER_EDITABLE_FILES = [
|
|
17
|
+
{ relativePath: "wp.config.ts", name: "Theme configuration file" },
|
|
18
|
+
{ relativePath: "index.html", name: "Main HTML entry wrapper" },
|
|
19
|
+
{ relativePath: "package.json", name: "NPM Package configuration" },
|
|
20
|
+
{ relativePath: "src/main.tsx", name: "React development entry point" },
|
|
21
|
+
{ relativePath: "src/app/layout.tsx", name: "Root HTML structural layout" },
|
|
22
|
+
{ relativePath: "src/app/routes.tsx", name: "Local Vite preview routing switcher" },
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
// 1. Validate User-Editable Files (throw clear, actionable errors)
|
|
26
|
+
for (const file of USER_EDITABLE_FILES) {
|
|
27
|
+
const fullPath = path.join(themeRoot, file.relativePath);
|
|
28
|
+
if (!existsSync(fullPath)) {
|
|
29
|
+
throw new Error(
|
|
30
|
+
`Critical project file is missing: ${pc.bold(file.relativePath)}\n` +
|
|
31
|
+
` Description: ${file.name}\n\n` +
|
|
32
|
+
` 👉 To fix this, please restore the file from your version control (Git) or re-create it from the original ForgeWP theme template.`
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// 2. Validate & Self-Heal System-Critical Files
|
|
38
|
+
for (const [relativePath, blueprintContent] of Object.entries(SYSTEM_BLUEPRINTS)) {
|
|
39
|
+
const fullPath = path.join(themeRoot, relativePath);
|
|
40
|
+
|
|
41
|
+
if (!existsSync(fullPath)) {
|
|
42
|
+
console.log(pc.yellow(` ⚠️ System file ${pc.bold(relativePath)} was missing! Restoring default system blueprint...`));
|
|
43
|
+
|
|
44
|
+
// Ensure target directory exists (e.g. src/lib/)
|
|
45
|
+
const dirPath = path.dirname(fullPath);
|
|
46
|
+
if (!existsSync(dirPath)) {
|
|
47
|
+
mkdirSync(dirPath, { recursive: true });
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
writeFileSync(fullPath, blueprintContent, "utf8");
|
|
51
|
+
console.log(pc.green(` ✅ Successfully restored system file: ${relativePath}`));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
package/package.json
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@forgewp/compiler",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8",
|
|
4
4
|
"description": "Compile ForgeWP React themes into installable WordPress themes",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"bin": {
|
|
8
|
-
"forgewp
|
|
9
|
-
"forgewp-add": "./bin/add.js"
|
|
8
|
+
"forgewp": "./bin/cli.js"
|
|
10
9
|
},
|
|
11
10
|
"exports": {
|
|
12
|
-
".":
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./lib/index.d.ts",
|
|
13
|
+
"default": "./lib/index.js"
|
|
14
|
+
}
|
|
13
15
|
},
|
|
14
16
|
"files": [
|
|
15
17
|
"bin",
|
|
@@ -22,6 +24,10 @@
|
|
|
22
24
|
"archiver": "^7.0.1",
|
|
23
25
|
"jiti": "^2.6.1",
|
|
24
26
|
"picocolors": "^1.1.1",
|
|
25
|
-
"
|
|
27
|
+
"react-helmet-async": "^3.0.0",
|
|
28
|
+
"@forgewp/registry": "0.1.8"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@types/node": "^22.19.19"
|
|
26
32
|
}
|
|
27
33
|
}
|