@forgewp/compiler 0.4.3 → 0.4.4

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 (133) hide show
  1. package/README.md +59 -0
  2. package/bin/add.js +20 -14
  3. package/bin/cli.js +145 -70
  4. package/bin/doctor.js +21 -12
  5. package/bin/export.js +69 -20
  6. package/bin/fresh.js +5 -0
  7. package/bin/i18n-extract.js +3 -10
  8. package/bin/i18n-translate.js +15 -24
  9. package/bin/make-block.js +13 -0
  10. package/bin/make-editable.js +166 -0
  11. package/bin/make-form.js +131 -0
  12. package/bin/make-loop.js +40 -43
  13. package/bin/make-mail.js +27 -0
  14. package/bin/make-post-type.js +57 -29
  15. package/bin/make-sandbox.js +62 -11
  16. package/bin/make-section.js +221 -0
  17. package/bin/make-shell.js +375 -0
  18. package/bin/repair.js +14 -26
  19. package/bin/seed.js +119 -0
  20. package/bin/sync-routes.js +107 -85
  21. package/lib/adapters/html-adapter.js +5 -0
  22. package/lib/adapters/stub-adapter.js +4 -0
  23. package/lib/add-component.js +153 -92
  24. package/lib/asset-loader.js +313 -3
  25. package/lib/assets/asset-graph.js +432 -0
  26. package/lib/assets/asset-pipeline.js +125 -0
  27. package/lib/assets/diagnostics-report.js +345 -0
  28. package/lib/assets/external-fetcher.js +173 -0
  29. package/lib/assets/font-orchestrator.js +348 -0
  30. package/lib/assets/font-usage-scanner.js +242 -0
  31. package/lib/assets/image-optimizer.js +229 -0
  32. package/lib/assets/index.js +51 -0
  33. package/lib/assets/preload-generator.js +70 -0
  34. package/lib/assets/public-url.js +153 -0
  35. package/lib/block-authoring-help.js +11 -0
  36. package/lib/blocks/ast-parser.js +67 -0
  37. package/lib/blocks/discover-blocks.js +146 -0
  38. package/lib/blocks/editor-scope.js +733 -0
  39. package/lib/blocks/editor-transpiler.js +1229 -0
  40. package/lib/blocks/index.js +2838 -0
  41. package/lib/blocks/php-transpiler.js +4928 -0
  42. package/lib/blocks/scanner.js +1487 -0
  43. package/lib/blocks/shared-utils.js +290 -0
  44. package/lib/blocks/source-sanitize.js +1449 -0
  45. package/lib/blueprints.js +31 -91
  46. package/lib/build-assets.js +38 -33
  47. package/lib/collect-page-template-routes.js +102 -0
  48. package/lib/collect-static-routes.js +175 -0
  49. package/lib/diagnostics.js +58 -33
  50. package/lib/export-theme.js +84 -5
  51. package/lib/extract-template-content.js +90 -0
  52. package/lib/forms-preview.js +42 -0
  53. package/lib/functions/cpt-routing.js +69 -0
  54. package/lib/functions/forms.js +2461 -0
  55. package/lib/functions/hydration-enqueuer.js +400 -0
  56. package/lib/functions/index.js +1623 -0
  57. package/lib/functions/load-menus.js +112 -0
  58. package/lib/functions/load-translations.js +148 -0
  59. package/lib/functions/rest-endpoints.js +2923 -0
  60. package/lib/functions/routing.js +765 -0
  61. package/lib/functions/seed-mock-data.js +392 -0
  62. package/lib/functions/seed-products.js +481 -0
  63. package/lib/functions/settings-page.js +273 -0
  64. package/lib/functions/theme-setup.js +494 -0
  65. package/lib/generate-theme.js +1916 -339
  66. package/lib/hydration/editable-schemas.js +332 -0
  67. package/lib/hydration/enter-hidden-initial.js +65 -0
  68. package/lib/hydration/form-schemas.js +227 -0
  69. package/lib/hydration/index.js +12 -0
  70. package/lib/hydration/is-interactive.js +951 -0
  71. package/lib/hydration/island-split.js +1231 -0
  72. package/lib/hydration/islands-scanner.js +599 -0
  73. package/lib/hydration/resolve-island-chunk.js +103 -0
  74. package/lib/hydration/resolve-island-component.js +52 -0
  75. package/lib/hydration/rollup-inputs.js +81 -0
  76. package/lib/hydration/root-class-extractor.js +220 -0
  77. package/lib/hydration/scan-app-providers.js +154 -0
  78. package/lib/hydration/static-lint.js +382 -0
  79. package/lib/hydration/transform.js +257 -0
  80. package/lib/hydration/uses-block-area.js +49 -0
  81. package/lib/hydration/utils.js +119 -0
  82. package/lib/i18n/translation-cache.js +141 -0
  83. package/lib/icon-registry.js +72 -0
  84. package/lib/index.d.ts +287 -8
  85. package/lib/index.js +2 -1
  86. package/lib/load-config.js +338 -242
  87. package/lib/mail-templates.js +37 -0
  88. package/lib/markup-processor.js +388 -102
  89. package/lib/node-runtime.js +7 -0
  90. package/lib/page-config-plugin.js +64 -20
  91. package/lib/php-builders.js +383 -44
  92. package/lib/react-adapter.js +983 -235
  93. package/lib/registration-metadata.js +11 -0
  94. package/lib/render-theme.mts +1410 -616
  95. package/lib/route-layout.js +76 -0
  96. package/lib/template-discovery.js +58 -0
  97. package/lib/theme-compatibility.js +32 -0
  98. package/lib/validate-export.js +2 -2
  99. package/lib/validate.js +27 -5
  100. package/lib/virtual-plugin.js +319 -0
  101. package/lib/walkthrough.md +26 -0
  102. package/package.json +16 -5
  103. package/templates/auth/cms/email-logs.json +9 -0
  104. package/templates/auth/cms/menus.json +11 -0
  105. package/templates/auth/cms/mock-data.json +123 -0
  106. package/templates/auth/cms/roles.json +7 -0
  107. package/templates/auth/cms/sessions.json +1 -0
  108. package/templates/auth/cms/site-settings.json +28 -0
  109. package/templates/auth/cms/translations.json +4 -0
  110. package/templates/auth/cms/users.json +46 -0
  111. package/templates/auth/components/forgot-password-form-wrapper.tsx +10 -2
  112. package/templates/auth/components/login-form-wrapper.tsx +118 -21
  113. package/templates/auth/components/reset-password-form-wrapper.tsx +10 -2
  114. package/templates/auth/components/signup-form-wrapper.tsx +2 -2
  115. package/templates/auth/components/verify-email-view-wrapper.tsx +5 -5
  116. package/templates/auth/pages/DashboardPage.tsx +1 -4
  117. package/templates/auth/pages/ForgotPasswordPage.tsx +1 -4
  118. package/templates/auth/pages/LoginPage.tsx +1 -4
  119. package/templates/auth/pages/ResetPasswordPage.tsx +1 -4
  120. package/templates/auth/pages/SignUpPage.tsx +1 -4
  121. package/templates/auth/pages/VerifyEmailPage.tsx +1 -4
  122. package/templates/auth/ui/button.tsx +57 -0
  123. package/templates/auth/ui/card.tsx +76 -0
  124. package/templates/auth/ui/field.tsx +242 -0
  125. package/templates/auth/ui/input.tsx +22 -0
  126. package/templates/auth/ui/label.tsx +24 -0
  127. package/templates/auth/ui/separator.tsx +31 -0
  128. package/templates/menus.json +12 -10
  129. package/templates/users.json +8 -4
  130. package/templates/wordpress.tsx +690 -245
  131. package/lib/block-compiler.js +0 -552
  132. package/lib/functions-builder.js +0 -2970
  133. package/lib/hydration-scanner.js +0 -833
@@ -1,242 +1,338 @@
1
- import fs, { existsSync } from 'node:fs';
2
- import path from 'node:path';
3
- import { pathToFileURL } from 'node:url';
4
- import { createJiti } from 'jiti';
5
-
6
- /**
7
- * @param {string} themeRoot
8
- * @returns {Promise<import('./types.js').ForgeWPThemeConfig>}
9
- */
10
- export async function loadConfig(themeRoot) {
11
- const configPath = path.join(themeRoot, 'wp.config.ts');
12
-
13
- if (!existsSync(configPath)) {
14
- throw new Error(`Missing wp.config.ts in ${themeRoot}`);
15
- }
16
-
17
- const jiti = createJiti(pathToFileURL(configPath).href, {
18
- interopDefault: true,
19
- });
20
-
21
- const config = await jiti.import(configPath);
22
-
23
- if (!config?.slug || !config?.name) {
24
- throw new Error(
25
- 'wp.config.ts must export name, slug, version, description, textDomain',
26
- );
27
- }
28
-
29
- // Validate configVersion
30
- if (config.configVersion === undefined) {
31
- console.warn(
32
- '\x1b[33m%s\x1b[0m',
33
- '⚠️ [ForgeWP Warning]: "configVersion" is missing in wp.config.ts. In a future version, "configVersion: 1" will be required.'
34
- );
35
- } else if (config.configVersion !== 1) {
36
- throw new Error(
37
- `Unsupported configVersion: ${config.configVersion}. The current supported version is 1.`
38
- );
39
- }
40
-
41
- // Validate favicon
42
- if (config.favicon !== undefined) {
43
- if (typeof config.favicon !== 'string') {
44
- throw new Error(
45
- 'favicon must be a string pointing to an asset file (e.g. "/Logo/favicon.svg")'
46
- );
47
- }
48
- const cleanedFavicon = config.favicon.replace(/^\//, '');
49
- const path1 = path.join(themeRoot, cleanedFavicon);
50
- const path2 = path.join(themeRoot, 'public', cleanedFavicon);
51
- if (!existsSync(path1) && !existsSync(path2)) {
52
- console.warn(
53
- '\x1b[33m%s\x1b[0m',
54
- `⚠️ [ForgeWP Warning]: Favicon file not found at "${config.favicon}". Please verify the file path exists in your theme directory.`
55
- );
56
- }
57
- }
58
-
59
- // Validate headerPath
60
- if (config.headerPath !== undefined) {
61
- if (typeof config.headerPath !== 'string') {
62
- throw new Error('headerPath must be a string pointing to a header component file');
63
- }
64
- const absPath = path.isAbsolute(config.headerPath) ? config.headerPath : path.join(themeRoot, config.headerPath);
65
- if (!existsSync(absPath)) {
66
- console.warn(
67
- '\x1b[33m%s\x1b[0m',
68
- `⚠️ [ForgeWP Warning]: Header component file not found at "${config.headerPath}".`
69
- );
70
- }
71
- }
72
-
73
- // Validate footerPath
74
- if (config.footerPath !== undefined) {
75
- if (typeof config.footerPath !== 'string') {
76
- throw new Error('footerPath must be a string pointing to a footer component file');
77
- }
78
- const absPath = path.isAbsolute(config.footerPath) ? config.footerPath : path.join(themeRoot, config.footerPath);
79
- if (!existsSync(absPath)) {
80
- console.warn(
81
- '\x1b[33m%s\x1b[0m',
82
- `⚠️ [ForgeWP Warning]: Footer component file not found at "${config.footerPath}".`
83
- );
84
- }
85
- }
86
-
87
- // Validate headless
88
- if (config.headless !== undefined) {
89
- if (typeof config.headless !== 'object' || config.headless === null) {
90
- throw new Error('headless configuration must be an object');
91
- }
92
- if (config.headless.apiUrl !== undefined && typeof config.headless.apiUrl !== 'string') {
93
- throw new Error('headless.apiUrl must be a string');
94
- }
95
- if (config.headless.jwtAuth !== undefined && typeof config.headless.jwtAuth !== 'boolean') {
96
- throw new Error('headless.jwtAuth must be a boolean');
97
- }
98
- }
99
-
100
- // Validate auth
101
- if (config.auth !== undefined) {
102
- if (typeof config.auth !== 'object' || config.auth === null) {
103
- throw new Error('auth configuration must be an object');
104
- }
105
- if (
106
- config.auth.loginField !== undefined &&
107
- !['usernameOnly', 'emailOnly', 'usernameAndEmail'].includes(config.auth.loginField)
108
- ) {
109
- throw new Error('auth.loginField must be "usernameOnly", "emailOnly", or "usernameAndEmail"');
110
- }
111
- if (config.auth.defaultRole !== undefined && typeof config.auth.defaultRole !== 'string') {
112
- throw new Error('auth.defaultRole must be a string');
113
- }
114
- if (config.auth.reservedUsernames !== undefined) {
115
- if (!Array.isArray(config.auth.reservedUsernames) || config.auth.reservedUsernames.some((u) => typeof u !== 'string')) {
116
- throw new Error('auth.reservedUsernames must be an array of strings');
117
- }
118
- }
119
- if (config.auth.features !== undefined) {
120
- if (typeof config.auth.features !== 'object' || config.auth.features === null) {
121
- throw new Error('auth.features must be an object');
122
- }
123
- if (config.auth.features.registration !== undefined && typeof config.auth.features.registration !== 'boolean') {
124
- throw new Error('auth.features.registration must be a boolean');
125
- }
126
- if (config.auth.features.emailVerification !== undefined && typeof config.auth.features.emailVerification !== 'boolean') {
127
- throw new Error('auth.features.emailVerification must be a boolean');
128
- }
129
- if (config.auth.features.blockLoginUntilVerified !== undefined && typeof config.auth.features.blockLoginUntilVerified !== 'boolean') {
130
- throw new Error('auth.features.blockLoginUntilVerified must be a boolean');
131
- }
132
- if (config.auth.features.autoLoginAfterSignup !== undefined && typeof config.auth.features.autoLoginAfterSignup !== 'boolean') {
133
- throw new Error('auth.features.autoLoginAfterSignup must be a boolean');
134
- }
135
- if (config.auth.features.blockLoginUntilVerified === true && config.auth.features.autoLoginAfterSignup === true) {
136
- console.warn('\x1b[33m%s\x1b[0m', '⚠️ Warning: Both blockLoginUntilVerified and autoLoginAfterSignup are set to true. Unverified users will be blocked from auto-logging in after signup.');
137
- }
138
- }
139
- if (config.auth.emails !== undefined) {
140
- if (typeof config.auth.emails !== 'object' || config.auth.emails === null) {
141
- throw new Error('auth.emails must be an object');
142
- }
143
- if (config.auth.emails.verification !== undefined) {
144
- if (typeof config.auth.emails.verification !== 'object' || config.auth.emails.verification === null) {
145
- throw new Error('auth.emails.verification must be an object');
146
- }
147
- if (config.auth.emails.verification.subject !== undefined && typeof config.auth.emails.verification.subject !== 'string') {
148
- throw new Error('auth.emails.verification.subject must be a string');
149
- }
150
- if (config.auth.emails.verification.body !== undefined && typeof config.auth.emails.verification.body !== 'string') {
151
- throw new Error('auth.emails.verification.body must be a string');
152
- }
153
- }
154
- }
155
- }
156
-
157
- // Validate seo
158
- if (config.seo !== undefined) {
159
- if (typeof config.seo !== 'object' || config.seo === null) {
160
- throw new Error('seo configuration must be an object');
161
- }
162
- if (config.seo.sitemaps !== undefined) {
163
- const { postTypes, taxonomies } = config.seo.sitemaps;
164
- if (
165
- postTypes !== undefined &&
166
- (!Array.isArray(postTypes) || postTypes.some((pt) => typeof pt !== 'string'))
167
- ) {
168
- throw new Error('seo.sitemaps.postTypes must be an array of strings');
169
- }
170
- if (postTypes && new Set(postTypes).size !== postTypes.length) {
171
- throw new Error('seo.sitemaps.postTypes contains duplicate entries');
172
- }
173
-
174
- if (
175
- taxonomies !== undefined &&
176
- (!Array.isArray(taxonomies) || taxonomies.some((t) => typeof t !== 'string'))
177
- ) {
178
- throw new Error('seo.sitemaps.taxonomies must be an array of strings');
179
- }
180
- if (taxonomies && new Set(taxonomies).size !== taxonomies.length) {
181
- throw new Error('seo.sitemaps.taxonomies contains duplicate entries');
182
- }
183
- }
184
- }
185
-
186
- if (config.i18n) {
187
- const { locales, defaultLocale, provider } = config.i18n;
188
- if (locales !== undefined) {
189
- if (!Array.isArray(locales) || locales.some(l => typeof l !== 'string')) {
190
- throw new Error('i18n.locales must be an array of locale code strings (e.g. ["en", "de"])');
191
- }
192
- if (locales.length === 0) {
193
- throw new Error('i18n.locales array cannot be empty');
194
- }
195
- }
196
- if (defaultLocale !== undefined) {
197
- if (typeof defaultLocale !== 'string') {
198
- throw new Error('i18n.defaultLocale must be a string (e.g. "en")');
199
- }
200
- if (locales && !locales.includes(defaultLocale)) {
201
- throw new Error(`i18n.defaultLocale "${defaultLocale}" must be one of the configured locales: ${JSON.stringify(locales)}`);
202
- }
203
- }
204
- if (provider !== undefined && typeof provider !== 'string') {
205
- throw new Error('i18n.provider must be a string (e.g. "local", "deepl")');
206
- }
207
- }
208
-
209
- if (config.postTypes) {
210
- for (const pt of Object.keys(config.postTypes)) {
211
- if (pt.length > 20) {
212
- throw new Error(
213
- `Custom Post Type "${pt}" configured in wp.config.ts exceeds the WordPress limit of 20 characters.`
214
- );
215
- }
216
- }
217
- }
218
-
219
- if (!config.frameworkAdapter) {
220
- config.frameworkAdapter = 'react';
221
- }
222
-
223
- // Execute plugin configuration validation hooks
224
- if (config.plugins && Array.isArray(config.plugins)) {
225
- for (const plugin of config.plugins) {
226
- if (typeof plugin.validateConfig === 'function') {
227
- plugin.validateConfig(config, themeRoot, { fs, path });
228
- }
229
- }
230
- }
231
-
232
- return config;
233
- }
234
-
235
- /**
236
- * Helper to define theme configurations with TypeScript Intellisense autocompletion.
237
- * @param {import('./types.js').ForgeWPThemeConfig} config
238
- * @returns {import('./types.js').ForgeWPThemeConfig}
239
- */
240
- export function defineConfig(config) {
241
- return config;
242
- }
1
+ import { resolveMailTemplates } from './mail-templates.js';
2
+ import { registrationMetaFields } from './registration-metadata.js';
3
+ import fs, { existsSync } from 'node:fs';
4
+ import path from 'node:path';
5
+ import { pathToFileURL } from 'node:url';
6
+ import { createJiti } from 'jiti';
7
+ import { scanForFormSchemas } from './hydration/form-schemas.js';
8
+ import { validateMockSeedStrategy } from './functions/seed-mock-data.js';
9
+ import { resolveThemeCompatibility } from './theme-compatibility.js';
10
+ import { validateCptRewrite } from './functions/cpt-routing.js';
11
+
12
+ /**
13
+ * @param {string} themeRoot
14
+ * @returns {Promise<import('./types.js').ForgeWPThemeConfig>}
15
+ */
16
+ export async function loadConfig(themeRoot) {
17
+ const configPath = path.join(themeRoot, 'wp.config.ts');
18
+
19
+ if (!existsSync(configPath)) {
20
+ throw new Error(`Missing wp.config.ts in ${themeRoot}`);
21
+ }
22
+
23
+ const jiti = createJiti(pathToFileURL(configPath).href, {
24
+ interopDefault: true,
25
+ moduleCache: false,
26
+ });
27
+
28
+ const importedConfig = await jiti.import(configPath);
29
+
30
+ // With interopDefault, jiti (this version, at least) returns a CJS-style
31
+ // wrapper `{ __esModule: true, default: <real config object> }` whose
32
+ // properties forward reads to `.default` — but that forwarding is
33
+ // resolved once via static analysis of wp.config.ts's source, not a live
34
+ // proxy: a NEW property added to `.default` at runtime (e.g. this
35
+ // function's own `config.forms = ...` a few lines down) is invisible
36
+ // through the wrapper forever afterward, even though it's genuinely
37
+ // there on `.default`. Unwrap to the real object up front so every
38
+ // mutation below (there are many) is a normal, unsurprising object
39
+ // mutation with no wrapper involved.
40
+ const config =
41
+ importedConfig && importedConfig.__esModule && importedConfig.default
42
+ ? importedConfig.default
43
+ : importedConfig;
44
+
45
+ if (!config?.slug || !config?.name) {
46
+ throw new Error(
47
+ 'wp.config.ts must export name, slug, version, description, textDomain',
48
+ );
49
+ }
50
+
51
+ resolveThemeCompatibility(config);
52
+
53
+ // Validate configVersion
54
+ if (config.configVersion === undefined) {
55
+ console.warn(
56
+ '\x1b[33m%s\x1b[0m',
57
+ '⚠️ [ForgeWP Warning]: "configVersion" is missing in wp.config.ts. In a future version, "configVersion: 1" will be required.'
58
+ );
59
+ } else if (config.configVersion !== 1) {
60
+ throw new Error(
61
+ `Unsupported configVersion: ${config.configVersion}. The current supported version is 1.`
62
+ );
63
+ }
64
+
65
+ // Validate favicon
66
+ if (config.favicon !== undefined) {
67
+ if (typeof config.favicon !== 'string') {
68
+ throw new Error(
69
+ 'favicon must be a string pointing to an asset file (e.g. "/Logo/favicon.svg")'
70
+ );
71
+ }
72
+ const cleanedFavicon = config.favicon.replace(/^\//, '');
73
+ const path1 = path.join(themeRoot, cleanedFavicon);
74
+ const path2 = path.join(themeRoot, 'public', cleanedFavicon);
75
+ if (!existsSync(path1) && !existsSync(path2)) {
76
+ console.warn(
77
+ '\x1b[33m%s\x1b[0m',
78
+ `⚠️ [ForgeWP Warning]: Favicon file not found at "${config.favicon}". Please verify the file path exists in your theme directory.`
79
+ );
80
+ }
81
+ }
82
+
83
+ // Validate headerPath
84
+ if (config.headerPath !== undefined) {
85
+ if (typeof config.headerPath !== 'string') {
86
+ throw new Error('headerPath must be a string pointing to a header component file');
87
+ }
88
+ const absPath = path.isAbsolute(config.headerPath) ? config.headerPath : path.join(themeRoot, config.headerPath);
89
+ if (!existsSync(absPath)) {
90
+ console.warn(
91
+ '\x1b[33m%s\x1b[0m',
92
+ `⚠️ [ForgeWP Warning]: Header component file not found at "${config.headerPath}".`
93
+ );
94
+ }
95
+ }
96
+
97
+ // Validate footerPath
98
+ if (config.footerPath !== undefined) {
99
+ if (typeof config.footerPath !== 'string') {
100
+ throw new Error('footerPath must be a string pointing to a footer component file');
101
+ }
102
+ const absPath = path.isAbsolute(config.footerPath) ? config.footerPath : path.join(themeRoot, config.footerPath);
103
+ if (!existsSync(absPath)) {
104
+ console.warn(
105
+ '\x1b[33m%s\x1b[0m',
106
+ `⚠️ [ForgeWP Warning]: Footer component file not found at "${config.footerPath}".`
107
+ );
108
+ }
109
+ }
110
+
111
+ // Validate headless
112
+ if (config.headless !== undefined) {
113
+ if (typeof config.headless !== 'object' || config.headless === null) {
114
+ throw new Error('headless configuration must be an object');
115
+ }
116
+ if (config.headless.apiUrl !== undefined && typeof config.headless.apiUrl !== 'string') {
117
+ throw new Error('headless.apiUrl must be a string');
118
+ }
119
+ if (config.headless.jwtAuth !== undefined && typeof config.headless.jwtAuth !== 'boolean') {
120
+ throw new Error('headless.jwtAuth must be a boolean');
121
+ }
122
+ }
123
+
124
+ // Validate auth
125
+ registrationMetaFields(config.auth);
126
+ if (config.auth !== undefined) {
127
+ if (typeof config.auth !== 'object' || config.auth === null) {
128
+ throw new Error('auth configuration must be an object');
129
+ }
130
+ if (
131
+ config.auth.loginField !== undefined &&
132
+ !['usernameOnly', 'emailOnly', 'usernameAndEmail'].includes(config.auth.loginField)
133
+ ) {
134
+ throw new Error('auth.loginField must be "usernameOnly", "emailOnly", or "usernameAndEmail"');
135
+ }
136
+ if (config.auth.defaultRole !== undefined && typeof config.auth.defaultRole !== 'string') {
137
+ throw new Error('auth.defaultRole must be a string');
138
+ }
139
+ if (config.auth.reservedUsernames !== undefined) {
140
+ if (!Array.isArray(config.auth.reservedUsernames) || config.auth.reservedUsernames.some((u) => typeof u !== 'string')) {
141
+ throw new Error('auth.reservedUsernames must be an array of strings');
142
+ }
143
+ }
144
+ if (config.auth.features !== undefined) {
145
+ if (typeof config.auth.features !== 'object' || config.auth.features === null) {
146
+ throw new Error('auth.features must be an object');
147
+ }
148
+ if (config.auth.features.registration !== undefined && typeof config.auth.features.registration !== 'boolean') {
149
+ throw new Error('auth.features.registration must be a boolean');
150
+ }
151
+ if (config.auth.features.emailVerification !== undefined && typeof config.auth.features.emailVerification !== 'boolean') {
152
+ throw new Error('auth.features.emailVerification must be a boolean');
153
+ }
154
+ if (config.auth.features.blockLoginUntilVerified !== undefined && typeof config.auth.features.blockLoginUntilVerified !== 'boolean') {
155
+ throw new Error('auth.features.blockLoginUntilVerified must be a boolean');
156
+ }
157
+ if (config.auth.features.autoLoginAfterSignup !== undefined && typeof config.auth.features.autoLoginAfterSignup !== 'boolean') {
158
+ throw new Error('auth.features.autoLoginAfterSignup must be a boolean');
159
+ }
160
+ if (config.auth.features.blockLoginUntilVerified === true && config.auth.features.autoLoginAfterSignup === true) {
161
+ console.warn('\x1b[33m%s\x1b[0m', '⚠️ Warning: Both blockLoginUntilVerified and autoLoginAfterSignup are set to true. Unverified users will be blocked from auto-logging in after signup.');
162
+ }
163
+ }
164
+ if (config.auth.emails !== undefined) {
165
+ if (typeof config.auth.emails !== 'object' || config.auth.emails === null) {
166
+ throw new Error('auth.emails must be an object');
167
+ }
168
+ for (const k of ['verification', 'passwordReset']) {
169
+ const entry = config.auth.emails[k];
170
+ if (entry === undefined) continue;
171
+ if (typeof entry !== 'object' || entry === null || Array.isArray(entry)) {
172
+ throw new Error(`auth.emails.${k} must be an object`);
173
+ }
174
+ if (entry.subject !== undefined && typeof entry.subject !== 'string') {
175
+ throw new Error(`auth.emails.${k}.subject must be a string`);
176
+ }
177
+ if (entry.body !== undefined && typeof entry.body !== 'string') {
178
+ throw new Error(`auth.emails.${k}.body must be a string`);
179
+ }
180
+ }
181
+ }
182
+ }
183
+
184
+ // Validate seo
185
+ if (config.seo !== undefined) {
186
+ if (typeof config.seo !== 'object' || config.seo === null) {
187
+ throw new Error('seo configuration must be an object');
188
+ }
189
+ if (config.seo.sitemaps !== undefined) {
190
+ const { postTypes, taxonomies } = config.seo.sitemaps;
191
+ if (
192
+ postTypes !== undefined &&
193
+ (!Array.isArray(postTypes) || postTypes.some((pt) => typeof pt !== 'string'))
194
+ ) {
195
+ throw new Error('seo.sitemaps.postTypes must be an array of strings');
196
+ }
197
+ if (postTypes && new Set(postTypes).size !== postTypes.length) {
198
+ throw new Error('seo.sitemaps.postTypes contains duplicate entries');
199
+ }
200
+
201
+ if (
202
+ taxonomies !== undefined &&
203
+ (!Array.isArray(taxonomies) || taxonomies.some((t) => typeof t !== 'string'))
204
+ ) {
205
+ throw new Error('seo.sitemaps.taxonomies must be an array of strings');
206
+ }
207
+ if (taxonomies && new Set(taxonomies).size !== taxonomies.length) {
208
+ throw new Error('seo.sitemaps.taxonomies contains duplicate entries');
209
+ }
210
+ }
211
+ }
212
+
213
+ if (config.i18n) {
214
+ const { locales, defaultLocale, provider } = config.i18n;
215
+ if (locales !== undefined) {
216
+ if (!Array.isArray(locales) || locales.some(l => typeof l !== 'string')) {
217
+ throw new Error('i18n.locales must be an array of locale code strings (e.g. ["en", "de"])');
218
+ }
219
+ if (locales.length === 0) {
220
+ throw new Error('i18n.locales array cannot be empty');
221
+ }
222
+ }
223
+ if (defaultLocale !== undefined) {
224
+ if (typeof defaultLocale !== 'string') {
225
+ throw new Error('i18n.defaultLocale must be a string (e.g. "en")');
226
+ }
227
+ if (locales && !locales.includes(defaultLocale)) {
228
+ throw new Error(`i18n.defaultLocale "${defaultLocale}" must be one of the configured locales: ${JSON.stringify(locales)}`);
229
+ }
230
+ }
231
+ if (provider !== undefined && typeof provider !== 'string') {
232
+ throw new Error('i18n.provider must be a string (e.g. "local", "deepl")');
233
+ }
234
+ if (config.i18n.cache !== undefined && (typeof config.i18n.cache !== 'string' || !config.i18n.cache.trim())) {
235
+ throw new Error('i18n.cache must be a nonempty project-relative JSON path');
236
+ }
237
+ }
238
+
239
+ if (config.postTypes) {
240
+ for (const pt of Object.keys(config.postTypes)) {
241
+ if (pt.length > 20) {
242
+ throw new Error(
243
+ `Custom Post Type "${pt}" configured in wp.config.ts exceeds the WordPress limit of 20 characters.`
244
+ );
245
+ }
246
+ }
247
+ }
248
+
249
+ for (const [group, flags] of [
250
+ ['postTypes', ['public', 'hasArchive', 'showInRest']],
251
+ ['taxonomies', ['public', 'showUi', 'showInRest', 'hierarchical']],
252
+ ]) {
253
+ if (config[group] === undefined) continue;
254
+ if (!config[group] || typeof config[group] !== 'object' || Array.isArray(config[group])) {
255
+ throw new Error(`${group} must be a configuration object`);
256
+ }
257
+ for (const [key, entry] of Object.entries(config[group])) {
258
+ if (!entry || typeof entry !== 'object' || Array.isArray(entry)) {
259
+ throw new Error(`${group}.${key} must be a configuration object`);
260
+ }
261
+ for (const flag of flags) {
262
+ if (entry[flag] !== undefined && typeof entry[flag] !== 'boolean') {
263
+ throw new Error(`${group}.${key}.${flag} must be a boolean`);
264
+ }
265
+ }
266
+ if (group === 'postTypes') validateCptRewrite(entry.rewrite, key);
267
+ if (group === 'postTypes' && entry.supports !== undefined &&
268
+ (!Array.isArray(entry.supports) || entry.supports.some((feature) => typeof feature !== 'string'))) {
269
+ throw new Error(`postTypes.${key}.supports must be an array of strings`);
270
+ }
271
+ }
272
+ }
273
+
274
+ // Validate seed configuration
275
+ if (config.seed !== undefined) {
276
+ if (typeof config.seed !== 'object' || config.seed === null) {
277
+ throw new Error('seed must be an object configuration in wp.config.ts');
278
+ }
279
+ const validStrategies = [true, false, 'once', 'upsert', 'force'];
280
+ validateMockSeedStrategy(config.seed.mockData);
281
+ if (config.seed.products !== undefined && !validStrategies.includes(config.seed.products)) {
282
+ throw new Error(
283
+ `Invalid seed.products strategy: "${config.seed.products}". Expected true, false, "once", "upsert", or "force".`
284
+ );
285
+ }
286
+ if (config.seed.developmentOnly === undefined) {
287
+ config.seed.developmentOnly = true;
288
+ }
289
+ if (config.seed.sideloadImages === undefined) {
290
+ config.seed.sideloadImages = true;
291
+ }
292
+ }
293
+
294
+ if (!config.frameworkAdapter) {
295
+ config.frameworkAdapter = 'react';
296
+ }
297
+
298
+ // Execute plugin configuration validation hooks
299
+ if (config.plugins && Array.isArray(config.plugins)) {
300
+ for (const plugin of config.plugins) {
301
+ if (typeof plugin.validateConfig === 'function') {
302
+ plugin.validateConfig(config, themeRoot, { fs, path });
303
+ }
304
+ }
305
+ }
306
+
307
+ // Merge in forms discovered under cms/forms/** (one defineWpForm() call
308
+ // per file, filename → form key — mirrors cms/editables/**'s convention
309
+ // and scanForEditableSchemas' discovery approach). An explicit `forms` key
310
+ // already in wp.config.ts always wins on a same-key collision — inline
311
+ // config is the more deliberate declaration and shouldn't be silently
312
+ // overridden by a colocated file the developer might not remember exists.
313
+ const discoveredForms = scanForFormSchemas(themeRoot);
314
+ if (Object.keys(discoveredForms).length > 0) {
315
+ const inlineForms = config.forms || {};
316
+ for (const key of Object.keys(discoveredForms)) {
317
+ if (Object.prototype.hasOwnProperty.call(inlineForms, key)) {
318
+ console.warn(
319
+ `\x1b[33m%s\x1b[0m`,
320
+ `⚠️ Form "${key}" is declared both in wp.config.ts's \`forms\` key and in cms/forms/${key}.ts — the wp.config.ts version wins. Remove one of them.`,
321
+ );
322
+ }
323
+ }
324
+ config.forms = { ...discoveredForms, ...inlineForms };
325
+ }
326
+
327
+ resolveMailTemplates(config.forms, themeRoot);
328
+ return config;
329
+ }
330
+
331
+ /**
332
+ * Helper to define theme configurations with TypeScript Intellisense autocompletion.
333
+ * @param {import('./types.js').ForgeWPThemeConfig} config
334
+ * @returns {import('./types.js').ForgeWPThemeConfig}
335
+ */
336
+ export function defineConfig(config) {
337
+ return config;
338
+ }
@@ -0,0 +1,37 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+
4
+ /** Resolve plain data into existing mail defaults; never ship source files to the browser. */
5
+ export function resolveMailTemplates(forms, themeRoot) {
6
+ if (!forms) return;
7
+ for (const [name, form] of Object.entries(forms)) {
8
+ if (form.mode === 'shortcode') continue;
9
+ const resolve = (template, target, subjectKey, bodyKey, allowClient = false) => {
10
+ if (template === undefined) return;
11
+ if (typeof template !== 'string' || !/^[a-z][a-z0-9-]*$/.test(template)) {
12
+ throw new Error(`[ForgeWP Mail] forms.${name}: template must be a lowercase name such as "contact-receipt".`);
13
+ }
14
+ const file = path.join(themeRoot, 'cms/mail', `${template}.json`);
15
+ let data;
16
+ try { data = JSON.parse(fs.readFileSync(file, 'utf8')); }
17
+ catch (error) { throw new Error(`[ForgeWP Mail] Cannot read template cms/mail/${template}.json for form "${name}": ${error.message}`); }
18
+ if (!data || typeof data.subject !== 'string' || typeof data.body !== 'string') {
19
+ throw new Error(`[ForgeWP Mail] ${template}.json requires plain-text subject and body strings.`);
20
+ }
21
+ for (const [key, value] of [[subjectKey, data.subject], [bodyKey, data.body]]) {
22
+ // Explicit per-form text takes priority over the reusable source default.
23
+ if (target[key] !== undefined) continue;
24
+ for (const [, field] of value.matchAll(/\{([a-zA-Z0-9_-]+)\}/g)) {
25
+ const seed = form.clientFields?.enabled ? form.clientFields.seed || [] : [];
26
+ const clientFields = Array.isArray(seed) ? seed : Object.values(seed).flat();
27
+ if (!Object.hasOwn(form.fields || {}, field) && !(allowClient && clientFields.some((entry) => entry.name === field))) {
28
+ throw new Error(`[ForgeWP Mail] ${template}.json references undeclared ${allowClient ? '' : 'developer-owned '}field "${field}" in form "${name}".`);
29
+ }
30
+ }
31
+ target[key] = value;
32
+ }
33
+ };
34
+ resolve(form.notificationTemplate, form, 'subject', 'notificationBody', true);
35
+ if (form.confirmation?.enabled) resolve(form.confirmation.template, form.confirmation, 'subject', 'body');
36
+ }
37
+ }