@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/README.md
CHANGED
|
@@ -18,7 +18,33 @@ pnpm export
|
|
|
18
18
|
A powerful utility that pulls components from the ForgeWP Registry or shadcn/ui and applies the signature **Sharpness Transformer**.
|
|
19
19
|
|
|
20
20
|
```bash
|
|
21
|
-
pnpm forgewp
|
|
21
|
+
pnpm forgewp add navbar
|
|
22
|
+
# Or
|
|
23
|
+
pnpm forgewp add --name navbar
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### 3. Native Gutenberg Block Generator
|
|
27
|
+
Dynamically scaffolds dynamic WordPress blocks authored in React with options for custom attributes:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
pnpm forgewp make:block HeroBlock --attributes=heading,text,image,buttonText
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### 4. Post-Type Loop Component Scaffolder
|
|
34
|
+
Instantly scaffolds beautifully designed React loop grid components bound to custom WordPress post types (ACF-like integration):
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
pnpm forgewp make:component PortfolioGrid --postType=portfolio
|
|
38
|
+
```
|
|
39
|
+
*Note: If the requested post type does not exist in `wordpress/mock-data.json`, the compiler will automatically register and seed it for you!*
|
|
40
|
+
|
|
41
|
+
### 5. Resilient Framework Immunity & Diagnostics
|
|
42
|
+
Keep your monorepo perfectly healthy, clean, and self-healing:
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
pnpm forgewp doctor # Run diagnostic health & dependency checks
|
|
46
|
+
pnpm forgewp repair # Force restore framework utility blueprints
|
|
47
|
+
pnpm forgewp clean # Prune build artifacts and clear caches
|
|
22
48
|
```
|
|
23
49
|
|
|
24
50
|
## ๐ Configuration
|
|
@@ -35,4 +61,5 @@ export default {
|
|
|
35
61
|
|
|
36
62
|
---
|
|
37
63
|
|
|
38
|
-
Maintainable,
|
|
64
|
+
Maintainable, Resilient, WordPress-native.
|
|
65
|
+
|
package/bin/add.js
CHANGED
|
@@ -2,11 +2,23 @@
|
|
|
2
2
|
import { addComponent } from "../lib/add-component.js";
|
|
3
3
|
|
|
4
4
|
const args = process.argv.slice(2);
|
|
5
|
-
|
|
5
|
+
let component = args.find(a => !a.startsWith("-"));
|
|
6
|
+
|
|
7
|
+
// Support --name navbar or --name=navbar
|
|
8
|
+
const nameIndex = args.indexOf("--name");
|
|
9
|
+
if (nameIndex !== -1 && args[nameIndex + 1]) {
|
|
10
|
+
component = args[nameIndex + 1];
|
|
11
|
+
} else {
|
|
12
|
+
const nameEqual = args.find(a => a.startsWith("--name="));
|
|
13
|
+
if (nameEqual) {
|
|
14
|
+
component = nameEqual.split("=")[1];
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
6
18
|
const style = args.find(a => a.startsWith("--style="))?.split("=")[1] || "forgewp";
|
|
7
19
|
|
|
8
20
|
if (!component) {
|
|
9
|
-
console.error("Usage: forgewp
|
|
21
|
+
console.error("Usage: forgewp add <component> [--style=forgewp|shadcn] or forgewp add --name <component>");
|
|
10
22
|
process.exit(1);
|
|
11
23
|
}
|
|
12
24
|
|
package/bin/clean.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { rmSync, existsSync } from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import pc from "picocolors";
|
|
6
|
+
|
|
7
|
+
console.log(`\n๐งน ${pc.bold(pc.bgMagenta(pc.black(" FORGEWP CACHE CLEANER ")))}\n`);
|
|
8
|
+
|
|
9
|
+
const projectRoot = process.cwd();
|
|
10
|
+
const foldersToClean = [
|
|
11
|
+
".forgewp",
|
|
12
|
+
"dist",
|
|
13
|
+
".vite"
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
let cleanedCount = 0;
|
|
17
|
+
|
|
18
|
+
for (const folder of foldersToClean) {
|
|
19
|
+
const target = path.join(projectRoot, folder);
|
|
20
|
+
if (existsSync(target)) {
|
|
21
|
+
try {
|
|
22
|
+
rmSync(target, { recursive: true, force: true });
|
|
23
|
+
console.log(` ${pc.green("โ
Cleared folder")}: ${pc.cyan(folder)}`);
|
|
24
|
+
cleanedCount++;
|
|
25
|
+
} catch (err) {
|
|
26
|
+
console.log(pc.red(` โ Failed to clear ${folder}: ${err.message}`));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
console.log("\n" + "โ".repeat(60));
|
|
32
|
+
if (cleanedCount > 0) {
|
|
33
|
+
console.log(pc.green(`\n๐ ${pc.bold("CLEAN COMPLETE:")} Pruned ${cleanedCount} build artifact/cache folders.`));
|
|
34
|
+
console.log(pc.cyan(" Your development workspace is now 100% clean and fresh.\n"));
|
|
35
|
+
} else {
|
|
36
|
+
console.log(pc.yellow(`\nโ ๏ธ ${pc.bold("NO ACTION NEEDED:")} No cached build folders or artifacts found. Workspace is already clean!\n`));
|
|
37
|
+
}
|
package/bin/cli.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { spawn } from "node:child_process";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import pc from "picocolors";
|
|
7
|
+
|
|
8
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
9
|
+
|
|
10
|
+
const subcommands = {
|
|
11
|
+
add: path.join(__dirname, "add.js"),
|
|
12
|
+
"make:block": path.join(__dirname, "make-block.js"),
|
|
13
|
+
"make:post-type": path.join(__dirname, "make-post-type.js"),
|
|
14
|
+
"make:component": path.join(__dirname, "make-component.js"),
|
|
15
|
+
export: path.join(__dirname, "export.js"),
|
|
16
|
+
doctor: path.join(__dirname, "doctor.js"),
|
|
17
|
+
repair: path.join(__dirname, "repair.js"),
|
|
18
|
+
clean: path.join(__dirname, "clean.js"),
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const args = process.argv.slice(2);
|
|
22
|
+
const command = args[0];
|
|
23
|
+
|
|
24
|
+
if (!command || command === "-h" || command === "--help") {
|
|
25
|
+
printHelp();
|
|
26
|
+
process.exit(0);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
let targetScript = subcommands[command];
|
|
30
|
+
|
|
31
|
+
// Helper fallback routes
|
|
32
|
+
if (!targetScript) {
|
|
33
|
+
if (command === "make-block" || command === "make") {
|
|
34
|
+
targetScript = subcommands["make:block"];
|
|
35
|
+
} else if (command === "make-post-type" || command === "post-type" || command === "make:post-type") {
|
|
36
|
+
targetScript = subcommands["make:post-type"];
|
|
37
|
+
} else if (command === "make-component" || command === "component" || command === "make:component") {
|
|
38
|
+
targetScript = subcommands["make:component"];
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (!targetScript) {
|
|
43
|
+
console.error(pc.red(`\nโ Error: Unknown command "${command}"`));
|
|
44
|
+
printHelp();
|
|
45
|
+
process.exit(1);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Forward other parameters
|
|
49
|
+
const commandArgs = args.slice(1);
|
|
50
|
+
|
|
51
|
+
// Run the subcommand using spawn, inheriting stdio for rich interactivity
|
|
52
|
+
const child = spawn(process.execPath, [targetScript, ...commandArgs], {
|
|
53
|
+
stdio: "inherit",
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
child.on("close", (code) => {
|
|
57
|
+
process.exit(code ?? 0);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
function printHelp() {
|
|
61
|
+
console.log(`
|
|
62
|
+
${pc.bold(pc.bgCyan(pc.black(" โก FORGEWP CLI ")))} ${pc.cyan("โ Fast visual frameworks for WordPress")}
|
|
63
|
+
|
|
64
|
+
${pc.bold("Usage:")}
|
|
65
|
+
pnpm forgewp <command> [options]
|
|
66
|
+
|
|
67
|
+
${pc.bold("Commands:")}
|
|
68
|
+
${pc.cyan("add <component>")} Add registry components (e.g. navbar)
|
|
69
|
+
${pc.cyan("make:block <Name>")} Scaffold a React Gutenberg block (e.g. HeroBlock)
|
|
70
|
+
${pc.cyan("make:post-type <slug>")} Register a custom mock post-type (e.g. portfolio)
|
|
71
|
+
${pc.cyan("make:component <Name>")} Scaffold a post-type grid loop component
|
|
72
|
+
${pc.cyan("export")} Package your theme into an installable WP zip
|
|
73
|
+
${pc.cyan("doctor")} Perform diagnostic check on project health
|
|
74
|
+
${pc.cyan("repair")} Force-repair/auto-heal system internals
|
|
75
|
+
${pc.cyan("clean")} Clear all build caches and temporary artifacts
|
|
76
|
+
|
|
77
|
+
${pc.bold("Examples:")}
|
|
78
|
+
pnpm forgewp add navbar
|
|
79
|
+
pnpm forgewp make:block HeroBlock --attributes=title,subtitle
|
|
80
|
+
pnpm forgewp make:post-type portfolio --customFields=client_name
|
|
81
|
+
pnpm forgewp make:component PortfolioGrid --postType=portfolio
|
|
82
|
+
pnpm forgewp doctor
|
|
83
|
+
pnpm forgewp repair
|
|
84
|
+
pnpm forgewp clean
|
|
85
|
+
pnpm forgewp export
|
|
86
|
+
`);
|
|
87
|
+
}
|
package/bin/doctor.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import pc from "picocolors";
|
|
6
|
+
import { SYSTEM_BLUEPRINTS } from "../lib/blueprints.js";
|
|
7
|
+
|
|
8
|
+
console.log(`\n๐ฉบ ${pc.bold(pc.bgCyan(pc.black(" FORGEWP SYSTEM DOCTOR ")))}\n`);
|
|
9
|
+
|
|
10
|
+
const projectRoot = process.cwd();
|
|
11
|
+
let issuesFound = 0;
|
|
12
|
+
|
|
13
|
+
function runCheck(name, fn) {
|
|
14
|
+
process.stdout.write(` Checking ${name.padEnd(45, ".")} `);
|
|
15
|
+
try {
|
|
16
|
+
const warning = fn();
|
|
17
|
+
if (warning) {
|
|
18
|
+
console.log(pc.yellow("โ ๏ธ WARNING"));
|
|
19
|
+
console.log(pc.yellow(` ๐ ${warning}`));
|
|
20
|
+
issuesFound++;
|
|
21
|
+
} else {
|
|
22
|
+
console.log(pc.green("โ
OK"));
|
|
23
|
+
}
|
|
24
|
+
} catch (err) {
|
|
25
|
+
console.log(pc.red("โ FAILED"));
|
|
26
|
+
console.log(pc.red(` ๐ Error: ${err.message}`));
|
|
27
|
+
issuesFound++;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// 1. Node Version Check
|
|
32
|
+
runCheck("Node.js Runtime Environment", () => {
|
|
33
|
+
const major = parseInt(process.versions.node.split(".")[0], 10);
|
|
34
|
+
if (major < 18) {
|
|
35
|
+
return `Your Node version is v${process.version}. ForgeWP recommends Node v18+ for compiling React themes.`;
|
|
36
|
+
}
|
|
37
|
+
return null;
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// 2. User Editable Files
|
|
41
|
+
runCheck("User Configuration & HTML Layout", () => {
|
|
42
|
+
const critical = ["wp.config.ts", "package.json", "index.html", "src/main.tsx"];
|
|
43
|
+
for (const f of critical) {
|
|
44
|
+
if (!existsSync(path.join(projectRoot, f))) {
|
|
45
|
+
throw new Error(`Critical file "${f}" is missing from project root!`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return null;
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// 3. Database Check
|
|
52
|
+
runCheck("Mock Database Schema Integration", () => {
|
|
53
|
+
const dbPath = path.join(projectRoot, "wordpress", "mock-data.json");
|
|
54
|
+
if (!existsSync(dbPath)) {
|
|
55
|
+
throw new Error(`Database "wordpress/mock-data.json" was not found!`);
|
|
56
|
+
}
|
|
57
|
+
try {
|
|
58
|
+
JSON.parse(readFileSync(dbPath, "utf8"));
|
|
59
|
+
} catch (err) {
|
|
60
|
+
throw new Error(`Database "wordpress/mock-data.json" contains invalid JSON syntax!\n Message: ${err.message}`);
|
|
61
|
+
}
|
|
62
|
+
return null;
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// 4. System Files Integrity Check
|
|
66
|
+
runCheck("System Internal Framework Libraries", () => {
|
|
67
|
+
const systemFiles = Object.keys(SYSTEM_BLUEPRINTS);
|
|
68
|
+
const missing = [];
|
|
69
|
+
for (const sf of systemFiles) {
|
|
70
|
+
if (!existsSync(path.join(projectRoot, sf))) {
|
|
71
|
+
missing.push(sf);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (missing.length > 0) {
|
|
75
|
+
throw new Error(`Framework internal files are missing: ${missing.join(", ")}\n ๐ก Fix: Run "pnpm forgewp repair" to auto-heal system boundaries!`);
|
|
76
|
+
}
|
|
77
|
+
return null;
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// 5. Tailwind Integration Check
|
|
81
|
+
runCheck("Tailwind Styling Architecture", () => {
|
|
82
|
+
const cssFile1 = path.join(projectRoot, "src", "index.css");
|
|
83
|
+
const cssFile2 = path.join(projectRoot, "src", "app", "globals.css");
|
|
84
|
+
if (!existsSync(cssFile1) && !existsSync(cssFile2)) {
|
|
85
|
+
return `Style sheet (src/index.css or src/app/globals.css) is missing. Please ensure your Tailwind styles are loaded.`;
|
|
86
|
+
}
|
|
87
|
+
return null;
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
// Final Diagnostic
|
|
91
|
+
console.log("\n" + "โ".repeat(60));
|
|
92
|
+
if (issuesFound === 0) {
|
|
93
|
+
console.log(pc.green(`\n๐ ${pc.bold("SYSTEM HEALTHY:")} Your ForgeWP monorepo is in perfect condition!`));
|
|
94
|
+
console.log(pc.cyan(" You are ready to compile, scaffold, and upload themes safely.\n"));
|
|
95
|
+
} else {
|
|
96
|
+
console.log(pc.red(`\nโ ${pc.bold("DIAGNOSTIC COMPLETED:")} Found ${issuesFound} active system issue(s).`));
|
|
97
|
+
console.log(pc.yellow(` ๐ก Tip: Follow the instructions above or run "pnpm forgewp repair" to auto-heal framework internals.\n`));
|
|
98
|
+
process.exit(1);
|
|
99
|
+
}
|
package/bin/export.js
CHANGED
|
File without changes
|
|
@@ -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`);
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { existsSync, mkdirSync, writeFileSync, readFileSync } from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import pc from "picocolors";
|
|
6
|
+
|
|
7
|
+
const args = process.argv.slice(2);
|
|
8
|
+
let rawComponentName = args.find(a => !a.startsWith("-"));
|
|
9
|
+
|
|
10
|
+
// Support --name PortfolioGrid or --name=PortfolioGrid
|
|
11
|
+
const nameIndex = args.indexOf("--name");
|
|
12
|
+
if (nameIndex !== -1 && args[nameIndex + 1]) {
|
|
13
|
+
rawComponentName = args[nameIndex + 1];
|
|
14
|
+
} else {
|
|
15
|
+
const nameEqual = args.find(a => a.startsWith("--name="));
|
|
16
|
+
if (nameEqual) {
|
|
17
|
+
rawComponentName = nameEqual.split("=")[1];
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (!rawComponentName) {
|
|
22
|
+
console.error(pc.red("\nโ Error: Please specify a component name."));
|
|
23
|
+
console.log(pc.cyan(" Example: pnpm forgewp make:component PortfolioGrid --postType=portfolio"));
|
|
24
|
+
console.log(pc.cyan(" Or: pnpm forgewp make:component --name PortfolioGrid --postType=portfolio\n"));
|
|
25
|
+
process.exit(1);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// PascalCase formatting
|
|
29
|
+
const cleanName = rawComponentName.replace(/[^a-zA-Z0-9_-]/g, "");
|
|
30
|
+
const pascalCase = cleanName
|
|
31
|
+
.replace(/(?:^\w|[A-Z]|\b\w)/g, (word) => word.toUpperCase())
|
|
32
|
+
.replace(/[^a-zA-Z0-9]/g, "");
|
|
33
|
+
|
|
34
|
+
if (!pascalCase) {
|
|
35
|
+
console.error(pc.red("\nโ Error: Invalid component name.\n"));
|
|
36
|
+
process.exit(1);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Parse postType
|
|
40
|
+
let postType = "post";
|
|
41
|
+
const postTypeArg = args.find(a => a.startsWith("--postType=") || a.startsWith("--post-type="));
|
|
42
|
+
if (postTypeArg) {
|
|
43
|
+
postType = postTypeArg.split("=")[1];
|
|
44
|
+
} else {
|
|
45
|
+
const postTypeIndex = args.findIndex(a => a === "--postType" || a === "--post-type");
|
|
46
|
+
if (postTypeIndex !== -1 && args[postTypeIndex + 1]) {
|
|
47
|
+
postType = args[postTypeIndex + 1];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const projectRoot = process.cwd();
|
|
52
|
+
const componentsDir = path.join(projectRoot, "src", "components");
|
|
53
|
+
const mockDataPath = path.join(projectRoot, "wordpress", "mock-data.json");
|
|
54
|
+
|
|
55
|
+
if (!existsSync(path.join(projectRoot, "src"))) {
|
|
56
|
+
console.error(pc.red(`\nโ Error: "src" folder not found. Are you in your theme's root directory?\n`));
|
|
57
|
+
process.exit(1);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Ensure src/components directory exists
|
|
61
|
+
if (!existsSync(componentsDir)) {
|
|
62
|
+
mkdirSync(componentsDir, { recursive: true });
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const targetFile = path.join(componentsDir, `${pascalCase}.tsx`);
|
|
66
|
+
|
|
67
|
+
if (existsSync(targetFile)) {
|
|
68
|
+
console.error(pc.red(`\nโ Error: Component "${pascalCase}.tsx" already exists at src/components/\n`));
|
|
69
|
+
process.exit(1);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Try to load custom fields from mock-data.json for this post type
|
|
73
|
+
let customFields = [];
|
|
74
|
+
let automaticallySeeded = false;
|
|
75
|
+
|
|
76
|
+
if (existsSync(mockDataPath)) {
|
|
77
|
+
try {
|
|
78
|
+
const mockData = JSON.parse(readFileSync(mockDataPath, "utf8"));
|
|
79
|
+
if (!mockData[postType]) {
|
|
80
|
+
// SMART ACTION: Automatically register and seed postType records in the JSON database
|
|
81
|
+
mockData[postType] = [
|
|
82
|
+
{
|
|
83
|
+
id: 1,
|
|
84
|
+
title: `Sample ${postType.charAt(0).toUpperCase() + postType.slice(1)} Item 1`,
|
|
85
|
+
excerpt: `This is a custom ${postType} post seeded dynamically via ForgeWP CLI.`,
|
|
86
|
+
content: `<p>Welcome to your new custom <strong>${postType}</strong> post template! Edit this in wordpress/mock-data.json.</p>`,
|
|
87
|
+
date: new Date().toLocaleDateString("en-US", { year: "numeric", month: "long", day: "numeric" }),
|
|
88
|
+
author: "ForgeWP CLI",
|
|
89
|
+
featuredImage: `https://picsum.photos/seed/${postType}1/1200/630`,
|
|
90
|
+
customFields: {
|
|
91
|
+
client_name: "Mock Enterprise",
|
|
92
|
+
project_budget: "$30,000",
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
id: 2,
|
|
97
|
+
title: `Sample ${postType.charAt(0).toUpperCase() + postType.slice(1)} Item 2`,
|
|
98
|
+
excerpt: `This is another custom ${postType} post seeded dynamically via ForgeWP CLI.`,
|
|
99
|
+
content: `<p>This is the second custom post for the ${postType} post type.</p>`,
|
|
100
|
+
date: new Date().toLocaleDateString("en-US", { year: "numeric", month: "long", day: "numeric" }),
|
|
101
|
+
author: "ForgeWP CLI",
|
|
102
|
+
featuredImage: `https://picsum.photos/seed/${postType}2/1200/630`,
|
|
103
|
+
customFields: {
|
|
104
|
+
client_name: "Mock Organization",
|
|
105
|
+
project_budget: "$55,000",
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
];
|
|
109
|
+
// Save updated JSON
|
|
110
|
+
writeFileSync(mockDataPath, JSON.stringify(mockData, null, 2), "utf8");
|
|
111
|
+
automaticallySeeded = true;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const records = mockData[postType] || [];
|
|
115
|
+
if (records.length > 0 && records[0].customFields) {
|
|
116
|
+
customFields = Object.keys(records[0].customFields);
|
|
117
|
+
}
|
|
118
|
+
} catch (err) {
|
|
119
|
+
// Ignore and fallback
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Generate dynamic JSX elements for custom fields
|
|
124
|
+
let customFieldsMarkup = "";
|
|
125
|
+
if (customFields.length > 0) {
|
|
126
|
+
customFieldsMarkup = customFields.map(f => {
|
|
127
|
+
const cleanLabel = f.replace(/_/g, " ").replace(/(?:^\w|[A-Z]|\b\w)/g, (word) => word.toUpperCase());
|
|
128
|
+
return `<div className="flex justify-between border-t border-zinc-950 pt-2 text-xs font-mono text-zinc-700">
|
|
129
|
+
<span>${cleanLabel}:</span>
|
|
130
|
+
<span className="font-bold">{useWpCustomField("${f}")}</span>
|
|
131
|
+
</div>`;
|
|
132
|
+
}).join("\n ");
|
|
133
|
+
} else {
|
|
134
|
+
customFieldsMarkup = `{/* No custom fields registered in mock-data.json. Add fields by running forgewp make:post-type */}
|
|
135
|
+
<p className="text-xs text-zinc-500 font-mono italic">No custom fields configured for post type: ${postType}</p>`;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Generate beautiful Brutalist post loop grid template
|
|
139
|
+
const componentTemplate = `import { WpQueryLoop, useWpTitle, useWpFeaturedImage, useWpExcerpt, useWpPermalink, useWpCustomField } from "@/lib/wordpress";
|
|
140
|
+
|
|
141
|
+
export default function ${pascalCase}() {
|
|
142
|
+
return (
|
|
143
|
+
<section className="py-12 bg-zinc-50">
|
|
144
|
+
<div className="max-w-6xl mx-auto px-4">
|
|
145
|
+
<div className="mb-8 border-b-4 border-zinc-950 pb-4">
|
|
146
|
+
<h2 className="text-4xl font-black uppercase tracking-tight text-zinc-950">
|
|
147
|
+
Latest ${postType.charAt(0).toUpperCase() + postType.slice(1)} Items
|
|
148
|
+
</h2>
|
|
149
|
+
<p className="text-sm font-mono font-medium text-zinc-600 mt-1">
|
|
150
|
+
Dynamic Post Type Loop Component โ Querying "${postType}"
|
|
151
|
+
</p>
|
|
152
|
+
</div>
|
|
153
|
+
|
|
154
|
+
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8">
|
|
155
|
+
<WpQueryLoop postType="${postType}" postsPerPage={3}>
|
|
156
|
+
<article className="bg-white border-4 border-zinc-950 p-6 shadow-[6px_6px_0px_0px_rgba(0,0,0,1)] rounded-none flex flex-col justify-between hover:translate-x-[-2px] hover:translate-y-[-2px] hover:shadow-[8px_8px_0px_0px_rgba(0,0,0,1)] transition-all">
|
|
157
|
+
<div>
|
|
158
|
+
<div className="relative aspect-video w-full border-2 border-zinc-950 overflow-hidden mb-4 bg-zinc-100">
|
|
159
|
+
<img
|
|
160
|
+
src={useWpFeaturedImage()}
|
|
161
|
+
alt={useWpTitle()}
|
|
162
|
+
className="object-cover w-full h-full"
|
|
163
|
+
/>
|
|
164
|
+
</div>
|
|
165
|
+
<h3 className="text-2xl font-black text-zinc-950 uppercase tracking-tight leading-none mb-3 hover:text-brand transition-colors">
|
|
166
|
+
<a href={useWpPermalink()}>{useWpTitle()}</a>
|
|
167
|
+
</h3>
|
|
168
|
+
<p className="text-sm text-zinc-600 font-sans leading-relaxed mb-6">
|
|
169
|
+
{useWpExcerpt()}
|
|
170
|
+
</p>
|
|
171
|
+
</div>
|
|
172
|
+
|
|
173
|
+
<div className="space-y-2 mt-auto">
|
|
174
|
+
${customFieldsMarkup}
|
|
175
|
+
</div>
|
|
176
|
+
</article>
|
|
177
|
+
</WpQueryLoop>
|
|
178
|
+
</div>
|
|
179
|
+
</div>
|
|
180
|
+
</section>
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
`;
|
|
184
|
+
|
|
185
|
+
// Write template to file
|
|
186
|
+
writeFileSync(targetFile, componentTemplate, "utf8");
|
|
187
|
+
|
|
188
|
+
if (automaticallySeeded) {
|
|
189
|
+
console.log(pc.yellow(`\nโ ๏ธ Post type "${postType}" was missing from local database (mock-data.json).`));
|
|
190
|
+
console.log(` We have automatically registered it and seeded default custom fields for you!`);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
console.log(pc.green(`\nโก Loop Component "${pascalCase}" successfully created!`));
|
|
194
|
+
console.log(` Location: ${pc.cyan(`src/components/${pascalCase}.tsx`)}`);
|
|
195
|
+
console.log(` Target Post Type: ${pc.yellow(postType)}`);
|
|
196
|
+
if (customFields.length > 0) {
|
|
197
|
+
console.log(` Seeded Custom Fields: ${pc.yellow(customFields.join(", "))}`);
|
|
198
|
+
}
|
|
199
|
+
console.log(`\n๐ You can now import it directly inside your pages:`);
|
|
200
|
+
console.log(` ${pc.cyan(`import ${pascalCase} from "@/components/${pascalCase}";`)}\n`);
|