@forgewp/compiler 0.4.1 → 0.4.3

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.
Files changed (51) hide show
  1. package/README.md +11 -3
  2. package/bin/add.js +15 -6
  3. package/bin/analyze.js +66 -1
  4. package/bin/cli.js +20 -12
  5. package/bin/fresh.js +1 -1
  6. package/bin/make-block.js +11 -0
  7. package/bin/make-island.js +11 -0
  8. package/bin/{make-template.js → make-loop.js} +24 -19
  9. package/bin/make-page.js +83 -0
  10. package/bin/make-post-type.js +11 -0
  11. package/bin/make-sandbox.js +558 -0
  12. package/bin/sync-hooks.js +221 -0
  13. package/lib/adapters/html-adapter.js +8 -3
  14. package/lib/adapters/stub-adapter.js +6 -2
  15. package/lib/asset-loader.js +22 -0
  16. package/lib/blueprints.js +122 -1434
  17. package/lib/functions-builder.js +1172 -3
  18. package/lib/generate-theme.js +340 -23
  19. package/lib/hydration-scanner.js +170 -11
  20. package/lib/index.d.ts +38 -1
  21. package/lib/index.js +1 -1
  22. package/lib/load-config.js +98 -0
  23. package/lib/markup-processor.js +110 -0
  24. package/lib/page-config-plugin.js +40 -0
  25. package/lib/php-builders.js +4 -2
  26. package/lib/react-adapter.js +82 -23
  27. package/lib/render-theme.mts +115 -25
  28. package/lib/validate-export.js +49 -0
  29. package/package.json +4 -2
  30. package/templates/PresetsStyle.tsx +74 -0
  31. package/templates/SEO.tsx +109 -0
  32. package/templates/auth/components/dashboard-view-wrapper.tsx +168 -0
  33. package/templates/auth/components/forgot-password-form-wrapper.tsx +133 -0
  34. package/templates/auth/components/login-form-wrapper.tsx +203 -0
  35. package/templates/auth/components/reset-password-form-wrapper.tsx +182 -0
  36. package/templates/auth/components/signup-form-wrapper.tsx +257 -0
  37. package/templates/auth/components/verify-email-view-wrapper.tsx +265 -0
  38. package/templates/auth/pages/DashboardPage.tsx +15 -0
  39. package/templates/auth/pages/ForgotPasswordPage.tsx +14 -0
  40. package/templates/auth/pages/LoginPage.tsx +14 -0
  41. package/templates/auth/pages/ResetPasswordPage.tsx +14 -0
  42. package/templates/auth/pages/SignUpPage.tsx +14 -0
  43. package/templates/auth/pages/VerifyEmailPage.tsx +14 -0
  44. package/templates/forgewp-config.ts +110 -0
  45. package/templates/menus.json +11 -0
  46. package/templates/mock-data.json +123 -0
  47. package/templates/products.json +5 -0
  48. package/templates/roles.json +7 -0
  49. package/templates/sessions.json +1 -0
  50. package/templates/users.json +34 -0
  51. package/templates/wordpress.tsx +1390 -0
@@ -0,0 +1,221 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * forgewp sync-hooks
4
+ *
5
+ * Propagates the canonical src/.forgewp/wordpress.tsx template to every
6
+ * ForgeWP project in the workspace that contains a src/.forgewp/ directory.
7
+ *
8
+ * Usage:
9
+ * pnpm forgewp sync-hooks # sync all projects
10
+ * pnpm forgewp sync-hooks --dry-run # preview without writing
11
+ * pnpm forgewp sync-hooks --verbose # show skipped projects too
12
+ */
13
+
14
+ import {
15
+ existsSync,
16
+ readFileSync,
17
+ writeFileSync,
18
+ readdirSync,
19
+ statSync,
20
+ } from "node:fs";
21
+ import { createHash } from "node:crypto";
22
+ import path from "node:path";
23
+ import { fileURLToPath } from "node:url";
24
+ import pc from "picocolors";
25
+
26
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
27
+
28
+ // ── CLI flags ──────────────────────────────────────────────────────────────────
29
+ const argv = process.argv.slice(2);
30
+ const isDryRun = argv.includes("--dry-run") || argv.includes("-n");
31
+ const isVerbose = argv.includes("--verbose") || argv.includes("-v");
32
+
33
+ // ── Banner ─────────────────────────────────────────────────────────────────────
34
+ console.log(
35
+ `\n🔄 ${pc.bold(pc.bgCyan(pc.black(" FORGEWP SYNC-HOOKS ")))} ${
36
+ isDryRun ? pc.yellow("[DRY RUN — no files will be written]") : ""
37
+ }\n`
38
+ );
39
+
40
+ // ── Locate the canonical template (source of truth) ───────────────────────────
41
+ const canonicalTemplatePath = path.resolve(
42
+ __dirname,
43
+ "..",
44
+ "templates",
45
+ "wordpress.tsx"
46
+ );
47
+
48
+ if (!existsSync(canonicalTemplatePath)) {
49
+ console.error(
50
+ pc.red(
51
+ `\n❌ Cannot find canonical template at:\n ${canonicalTemplatePath}\n`
52
+ )
53
+ );
54
+ process.exit(1);
55
+ }
56
+
57
+ const canonicalContent = readFileSync(canonicalTemplatePath, "utf8");
58
+ const canonicalHash = md5(canonicalContent);
59
+
60
+ console.log(
61
+ ` ${pc.bold("Source:")} ${pc.cyan("packages/compiler/templates/wordpress.tsx")}`
62
+ );
63
+ console.log(` ${pc.bold("Hash:")} ${pc.dim(canonicalHash.slice(0, 12))}...\n`);
64
+
65
+ // ── Find workspace root (directory containing pnpm-workspace.yaml) ─────────────
66
+ function findWorkspaceRoot(startDir) {
67
+ let dir = startDir;
68
+ for (let i = 0; i < 10; i++) {
69
+ if (existsSync(path.join(dir, "pnpm-workspace.yaml"))) return dir;
70
+ const parent = path.dirname(dir);
71
+ if (parent === dir) break;
72
+ dir = parent;
73
+ }
74
+ return null;
75
+ }
76
+
77
+ const projectRoot = process.cwd();
78
+ const workspaceRoot = findWorkspaceRoot(projectRoot) || projectRoot;
79
+
80
+ console.log(` ${pc.bold("Workspace:")} ${pc.dim(workspaceRoot)}\n`);
81
+
82
+ // ── Collect candidate project directories ─────────────────────────────────────
83
+ function collectProjectDirs(workspaceRoot) {
84
+ const dirs = new Set();
85
+
86
+ // 1. Parse pnpm-workspace.yaml for declared packages
87
+ const workspaceYaml = path.join(workspaceRoot, "pnpm-workspace.yaml");
88
+ if (existsSync(workspaceYaml)) {
89
+ const raw = readFileSync(workspaceYaml, "utf8");
90
+ // Simple line-by-line parse — handles " - "pattern-or-glob" entries
91
+ const lines = raw.split(/\r?\n/);
92
+ for (const line of lines) {
93
+ const match = line.match(/^\s+-\s+"?([^"#\s]+)"?\s*$/);
94
+ if (!match) continue;
95
+ const pattern = match[1].trim();
96
+ if (pattern.startsWith("!")) continue; // exclusion — handled below
97
+
98
+ if (pattern.endsWith("/*")) {
99
+ // Glob: enumerate immediate children
100
+ const base = path.join(workspaceRoot, pattern.slice(0, -2));
101
+ if (existsSync(base)) {
102
+ for (const entry of readdirSync(base)) {
103
+ const full = path.join(base, entry);
104
+ if (statSync(full).isDirectory()) dirs.add(full);
105
+ }
106
+ }
107
+ } else {
108
+ // Literal path
109
+ const full = path.join(workspaceRoot, pattern);
110
+ if (existsSync(full)) dirs.add(full);
111
+ }
112
+ }
113
+ }
114
+
115
+ // 2. Always include template scaffold directories (excluded from workspace installs
116
+ // but still need to be kept in sync as they seed new projects)
117
+ const extraTemplates = [
118
+ path.join(workspaceRoot, "packages", "create-forgewp", "template"),
119
+ path.join(workspaceRoot, "packages", "create-forgewp", "template-html"),
120
+ ];
121
+ for (const t of extraTemplates) {
122
+ if (existsSync(t)) dirs.add(t);
123
+ }
124
+
125
+ return [...dirs];
126
+ }
127
+
128
+ const candidateDirs = collectProjectDirs(workspaceRoot);
129
+
130
+ // ── Sync logic ─────────────────────────────────────────────────────────────────
131
+ const TARGET_RELATIVE = path.join("src", ".forgewp", "wordpress.tsx");
132
+
133
+ let updatedCount = 0;
134
+ let alreadyCurrentCount = 0;
135
+ let skippedCount = 0;
136
+
137
+ for (const dir of candidateDirs) {
138
+ const targetPath = path.join(dir, TARGET_RELATIVE);
139
+ const relativeDir = path.relative(workspaceRoot, dir);
140
+
141
+ // Skip if this dir doesn't have a src/.forgewp/ at all — it's not a ForgeWP theme
142
+ if (!existsSync(path.dirname(targetPath))) {
143
+ if (isVerbose) {
144
+ console.log(
145
+ ` ${pc.dim("⊘ Skipped")} ${pc.dim(relativeDir)} ${pc.dim("(no src/.forgewp/)")}`
146
+ );
147
+ }
148
+ skippedCount++;
149
+ continue;
150
+ }
151
+
152
+ // Skip the compiler's own templates directory to avoid self-overwrite
153
+ if (dir === path.resolve(__dirname, "..")) {
154
+ if (isVerbose) {
155
+ console.log(` ${pc.dim("⊘ Skipped")} ${pc.dim(relativeDir)} ${pc.dim("(source package)")}`);
156
+ }
157
+ skippedCount++;
158
+ continue;
159
+ }
160
+
161
+ if (existsSync(targetPath)) {
162
+ const existingContent = readFileSync(targetPath, "utf8");
163
+ const existingHash = md5(existingContent);
164
+
165
+ if (existingHash === canonicalHash) {
166
+ if (isVerbose) {
167
+ console.log(
168
+ ` ${pc.green("✓ Up-to-date")} ${pc.cyan(relativeDir)}`
169
+ );
170
+ }
171
+ alreadyCurrentCount++;
172
+ continue;
173
+ }
174
+ }
175
+
176
+ // Write update
177
+ if (!isDryRun) {
178
+ writeFileSync(targetPath, canonicalContent, "utf8");
179
+ }
180
+
181
+ console.log(
182
+ ` ${isDryRun ? pc.yellow("~ Would update") : pc.green("✅ Updated")} ${pc.cyan(relativeDir + "/" + TARGET_RELATIVE.replace(/\\/g, "/"))}`
183
+ );
184
+ updatedCount++;
185
+ }
186
+
187
+ // ── Summary ────────────────────────────────────────────────────────────────────
188
+ console.log("\n" + "─".repeat(60));
189
+
190
+ if (updatedCount === 0 && alreadyCurrentCount > 0) {
191
+ console.log(
192
+ pc.green(
193
+ `\n✨ ${pc.bold("All projects are already up-to-date.")} (${alreadyCurrentCount} checked)\n`
194
+ )
195
+ );
196
+ } else if (isDryRun) {
197
+ console.log(
198
+ pc.yellow(
199
+ `\n🔍 ${pc.bold("Dry run complete.")} ${updatedCount} file(s) would be updated, ${alreadyCurrentCount} already current.\n`
200
+ )
201
+ );
202
+ } else {
203
+ const total = updatedCount + alreadyCurrentCount + skippedCount;
204
+ console.log(
205
+ pc.green(
206
+ `\n🎉 ${pc.bold("Sync complete!")} Updated ${pc.bold(String(updatedCount))} project(s). ${alreadyCurrentCount} already current. ${skippedCount} non-theme packages skipped.\n`
207
+ )
208
+ );
209
+ if (updatedCount > 0) {
210
+ console.log(
211
+ pc.cyan(
212
+ ` All projects now share the same wordpress.tsx hooks — zero drift.\n`
213
+ )
214
+ );
215
+ }
216
+ }
217
+
218
+ // ── Helpers ───────────────────────────────────────────────────────────────────
219
+ function md5(content) {
220
+ return createHash("md5").update(content).digest("hex");
221
+ }
@@ -55,9 +55,14 @@ export function onMakeBlock(themeRoot, { pascalCase, pc }) {
55
55
  console.log(` Block "${pascalCase}" was not created.\n`);
56
56
  }
57
57
 
58
- export function onMakeComponent(themeRoot, { pascalCase, pc }) {
59
- console.log(pc.yellow(`\n⚠️ The Static HTML framework adapter does not support custom loop components.`));
60
- console.log(` Component "${pascalCase}" was not created.\n`);
58
+ export function onMakeLoop(themeRoot, { pascalCase, pc }) {
59
+ console.log(pc.yellow(`\n⚠️ The Static HTML framework adapter does not support custom loops.`));
60
+ console.log(` Loop "${pascalCase}" was not created.\n`);
61
+ }
62
+
63
+ export function onMakePage(themeRoot, { pascalCase, pc }) {
64
+ console.log(pc.yellow(`\n⚠️ The Static HTML framework adapter does not support custom page templates.`));
65
+ console.log(` Page "${pascalCase}" was not created.\n`);
61
66
  }
62
67
 
63
68
  export function onSyncRoutes(themeRoot, { pc }) {
@@ -40,8 +40,12 @@ export function onMakeBlock(themeRoot, { pascalCase, pc }) {
40
40
  console.log(pc.yellow(`\n⚠️ The Stub framework adapter does not support custom Gutenberg blocks.`));
41
41
  }
42
42
 
43
- export function onMakeComponent(themeRoot, { pascalCase, pc }) {
44
- console.log(pc.yellow(`\n⚠️ The Stub framework adapter does not support custom loop components.`));
43
+ export function onMakeLoop(themeRoot, { pascalCase, pc }) {
44
+ console.log(pc.yellow(`\n⚠️ The Stub framework adapter does not support custom loops.`));
45
+ }
46
+
47
+ export function onMakePage(themeRoot, { pascalCase, pc }) {
48
+ console.log(pc.yellow(`\n⚠️ The Stub framework adapter does not support custom page templates.`));
45
49
  }
46
50
 
47
51
  export function onSyncRoutes(themeRoot, { pc }) {
@@ -1,3 +1,6 @@
1
+ import { fileURLToPath } from 'node:url';
2
+ import path from 'node:path';
3
+
1
4
  /**
2
5
  * Node.js ESM Loader hook to intercept and mock static asset imports (images, styles, fonts)
3
6
  * during server-side theme compilation.
@@ -17,6 +20,24 @@ export async function load(url, context, defaultLoad) {
17
20
  cleanUrl.endsWith('.ttf') ||
18
21
  cleanUrl.endsWith('.eot')
19
22
  ) {
23
+ try {
24
+ const filePath = fileURLToPath(cleanUrl);
25
+ const themeRoot = process.cwd();
26
+ const relPath = path.relative(themeRoot, filePath);
27
+ if (!relPath.startsWith('..') && !path.isAbsolute(relPath)) {
28
+ let normalized = relPath.replace(/\\/g, '/');
29
+ if (normalized.startsWith('public/')) {
30
+ normalized = normalized.substring(7); // Strip 'public/'
31
+ }
32
+ return {
33
+ format: 'module',
34
+ source: `export default "__FORGEWP_ASSET__${normalized}";`,
35
+ shortCircuit: true,
36
+ };
37
+ }
38
+ } catch (e) {
39
+ // fallback to empty string on error
40
+ }
20
41
  return {
21
42
  format: 'module',
22
43
  source: 'export default "";',
@@ -25,3 +46,4 @@ export async function load(url, context, defaultLoad) {
25
46
  }
26
47
  return defaultLoad(url, context, defaultLoad);
27
48
  }
49
+