@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.
@@ -0,0 +1,129 @@
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 rawName = args.find(a => !a.startsWith("-"));
9
+
10
+ // Support --name portfolio or --name=portfolio
11
+ const nameIndex = args.indexOf("--name");
12
+ if (nameIndex !== -1 && args[nameIndex + 1]) {
13
+ rawName = args[nameIndex + 1];
14
+ } else {
15
+ const nameEqual = args.find(a => a.startsWith("--name="));
16
+ if (nameEqual) {
17
+ rawName = nameEqual.split("=")[1];
18
+ }
19
+ }
20
+
21
+ if (!rawName) {
22
+ console.error(pc.red("\n❌ Error: Please specify a post type name."));
23
+ console.log(pc.cyan(" Example: pnpm forgewp make:post-type portfolio"));
24
+ console.log(pc.cyan(" Or: pnpm forgewp make:post-type --name portfolio\n"));
25
+ process.exit(1);
26
+ }
27
+
28
+ // WordPress slugs should be lowercase alphanumeric with dashes or underscores
29
+ const cleanName = rawName.toLowerCase().replace(/[^a-z0-9_-]/g, "");
30
+ if (!cleanName) {
31
+ console.error(pc.red("\n❌ Error: Invalid post type name. Use letters, numbers, dashes, and underscores.\n"));
32
+ process.exit(1);
33
+ }
34
+
35
+ const projectRoot = process.cwd();
36
+ const wpDir = path.join(projectRoot, "wordpress");
37
+ const mockDataPath = path.join(wpDir, "mock-data.json");
38
+
39
+ // Ensure wordpress directory exists
40
+ if (!existsSync(wpDir)) {
41
+ mkdirSync(wpDir, { recursive: true });
42
+ }
43
+
44
+ let mockData = {};
45
+
46
+ if (existsSync(mockDataPath)) {
47
+ try {
48
+ mockData = JSON.parse(readFileSync(mockDataPath, "utf8"));
49
+ } catch (err) {
50
+ console.error(pc.red(`\n❌ Error: Failed to parse wordpress/mock-data.json. Enforcing clean file.`));
51
+ mockData = {};
52
+ }
53
+ }
54
+
55
+ if (mockData[cleanName]) {
56
+ console.warn(pc.yellow(`\n⚠️ Post type "${cleanName}" already exists in wordpress/mock-data.json.`));
57
+ console.log(` You can open the file directly to view or edit existing fields.\n`);
58
+ process.exit(0);
59
+ }
60
+
61
+ // Parse custom fields if provided via --customFields, -customFields, --custom-fields, etc.
62
+ let customFieldsList = [];
63
+ const fieldsArg = args.find(a => /^-+custom[Ff]ields=/.test(a) || /^-+custom-fields=/.test(a));
64
+ if (fieldsArg) {
65
+ const rawFields = fieldsArg.split("=")[1];
66
+ if (rawFields) {
67
+ customFieldsList = rawFields.split(",").map(f => f.trim()).filter(Boolean);
68
+ }
69
+ } else {
70
+ const fieldsIndex = args.findIndex(a => /^-+custom[Ff]ields$/.test(a) || /^-+custom-fields$/.test(a));
71
+ if (fieldsIndex !== -1 && args[fieldsIndex + 1]) {
72
+ customFieldsList = args[fieldsIndex + 1].split(",").map(f => f.trim()).filter(Boolean);
73
+ }
74
+ }
75
+
76
+ // Generate seeded customFields object
77
+ const seededCustomFields1 = {};
78
+ const seededCustomFields2 = {};
79
+
80
+ if (customFieldsList.length > 0) {
81
+ for (const field of customFieldsList) {
82
+ seededCustomFields1[field] = `[Seeded ${field} 1]`;
83
+ seededCustomFields2[field] = `[Seeded ${field} 2]`;
84
+ }
85
+ } else {
86
+ // Default fallbacks if no flag is provided
87
+ seededCustomFields1["client_name"] = "Mock Enterprise";
88
+ seededCustomFields1["project_budget"] = "$30,000";
89
+
90
+ seededCustomFields2["client_name"] = "Mock Organization";
91
+ seededCustomFields2["project_budget"] = "$55,000";
92
+ }
93
+
94
+ // Generate template seed items for this custom post type
95
+ mockData[cleanName] = [
96
+ {
97
+ id: 1,
98
+ title: `Sample ${cleanName.charAt(0).toUpperCase() + cleanName.slice(1)} Item 1`,
99
+ excerpt: `This is a custom ${cleanName} post seeded dynamically via ForgeWP CLI.`,
100
+ content: `<p>Welcome to your new custom <strong>${cleanName}</strong> post template! Edit this in wordpress/mock-data.json.</p>`,
101
+ date: new Date().toLocaleDateString("en-US", { year: "numeric", month: "long", day: "numeric" }),
102
+ author: "ForgeWP CLI",
103
+ featuredImage: `https://picsum.photos/seed/${cleanName}1/1200/630`,
104
+ customFields: seededCustomFields1
105
+ },
106
+ {
107
+ id: 2,
108
+ title: `Sample ${cleanName.charAt(0).toUpperCase() + cleanName.slice(1)} Item 2`,
109
+ excerpt: `This is another custom ${cleanName} post seeded dynamically via ForgeWP CLI.`,
110
+ content: `<p>This is the second custom post for the ${cleanName} post type.</p>`,
111
+ date: new Date().toLocaleDateString("en-US", { year: "numeric", month: "long", day: "numeric" }),
112
+ author: "ForgeWP CLI",
113
+ featuredImage: `https://picsum.photos/seed/${cleanName}2/1200/630`,
114
+ customFields: seededCustomFields2
115
+ }
116
+ ];
117
+
118
+ // Write update JSON back to file
119
+ writeFileSync(mockDataPath, JSON.stringify(mockData, null, 2), "utf8");
120
+
121
+ const suggestedField = customFieldsList.length > 0 ? customFieldsList[0] : "client_name";
122
+
123
+ console.log(pc.green(`\n⚡ Post Type "${cleanName}" successfully registered!`));
124
+ console.log(` Local DB: ${pc.cyan(`wordpress/mock-data.json`)}`);
125
+ console.log(`\n🎉 Query your seeded fields in React inside:`);
126
+ console.log(` ${pc.yellow(`<WpQueryLoop postType="${cleanName}" postsPerPage={2}>`)}`);
127
+ console.log(` ${pc.yellow(`<h3>{useWpTitle()}</h3>`)}`);
128
+ console.log(` ${pc.yellow(`<p>${suggestedField.charAt(0).toUpperCase() + suggestedField.slice(1)}: {useWpCustomField("${suggestedField}")}</p>`)}`);
129
+ console.log(` ${pc.yellow(`</WpQueryLoop>`)}\n`);
package/bin/repair.js ADDED
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { existsSync, writeFileSync, mkdirSync } 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.bgYellow(pc.black(" FORGEWP SYSTEM REPAIR TOOL ")))}\n`);
9
+
10
+ const projectRoot = process.cwd();
11
+ let repairedCount = 0;
12
+
13
+ try {
14
+ // Re-heal every system blueprint
15
+ for (const [relativePath, blueprintContent] of Object.entries(SYSTEM_BLUEPRINTS)) {
16
+ const fullPath = path.join(projectRoot, relativePath);
17
+
18
+ // Ensure the folder exists
19
+ const dirPath = path.dirname(fullPath);
20
+ if (!existsSync(dirPath)) {
21
+ mkdirSync(dirPath, { recursive: true });
22
+ }
23
+
24
+ // Force overwrite to restore pure internals
25
+ writeFileSync(fullPath, blueprintContent, "utf8");
26
+ console.log(` ${pc.green("✅ Restored/Healed")}: ${pc.cyan(relativePath)}`);
27
+ repairedCount++;
28
+ }
29
+
30
+ // Ensure default mock data table is recreated if deleted
31
+ const mockDataPath = path.join(projectRoot, "wordpress", "mock-data.json");
32
+ if (!existsSync(mockDataPath)) {
33
+ const defaultJson = {
34
+ post: [
35
+ {
36
+ id: 1,
37
+ title: "Welcome to ForgeWP: The Headless Revolution",
38
+ excerpt: "Discover how ForgeWP bridges standard WordPress themes with React.",
39
+ content: "<p>Welcome to ForgeWP! Modify this in mock-data.json.</p>",
40
+ date: "May 10, 2026",
41
+ author: "Antigravity",
42
+ featuredImage: "https://picsum.photos/seed/forgewp/1200/630",
43
+ customFields: {}
44
+ }
45
+ ]
46
+ };
47
+ const dirPath = path.dirname(mockDataPath);
48
+ if (!existsSync(dirPath)) {
49
+ mkdirSync(dirPath, { recursive: true });
50
+ }
51
+ writeFileSync(mockDataPath, JSON.stringify(defaultJson, null, 2), "utf8");
52
+ console.log(` ${pc.green("✅ Seeded Default Database")}: wordpress/mock-data.json`);
53
+ repairedCount++;
54
+ }
55
+
56
+ console.log("\n" + "─".repeat(60));
57
+ console.log(pc.green(`\n🎉 ${pc.bold("REPAIR COMPLETE:")} Successfully repaired/synced ${repairedCount} core framework elements.`));
58
+ console.log(pc.cyan(" All protected boundaries have been restored back to defaults. Build safe!\n"));
59
+ } catch (err) {
60
+ console.error(pc.red(`\n❌ Error: Failed to perform system repair.\nReason: ${err.message}\n`));
61
+ process.exit(1);
62
+ }
@@ -1,5 +1,5 @@
1
1
  import { execSync } from "node:child_process";
2
- import { readFileSync, writeFileSync, readdirSync, existsSync } from "node:fs";
2
+ import { readFileSync, writeFileSync, readdirSync, existsSync, mkdirSync } from "node:fs";
3
3
  import path from "node:path";
4
4
  import pc from "picocolors";
5
5
 
@@ -20,7 +20,10 @@ export async function addComponent(component, options) {
20
20
  registryPath = path.resolve(path.dirname(new URL(import.meta.url).pathname), "../../registry/components");
21
21
  }
22
22
 
23
- const localComponentFile = path.join(registryPath, component, `${style}.tsx`);
23
+ let localComponentFile = path.join(registryPath, component, `${style}.tsx`);
24
+ if (!existsSync(localComponentFile)) {
25
+ localComponentFile = path.join(registryPath, component, "forgewp.tsx");
26
+ }
24
27
 
25
28
  if (existsSync(localComponentFile)) {
26
29
  console.log(pc.dim(` Found ${component} in ForgeWP local registry...`));