@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.
- package/README.md +59 -0
- package/bin/add.js +20 -14
- package/bin/cli.js +145 -70
- package/bin/doctor.js +21 -12
- package/bin/export.js +69 -20
- package/bin/fresh.js +5 -0
- package/bin/i18n-extract.js +3 -10
- package/bin/i18n-translate.js +15 -24
- package/bin/make-block.js +13 -0
- package/bin/make-editable.js +166 -0
- package/bin/make-form.js +131 -0
- package/bin/make-loop.js +40 -43
- package/bin/make-mail.js +27 -0
- package/bin/make-post-type.js +57 -29
- package/bin/make-sandbox.js +62 -11
- package/bin/make-section.js +221 -0
- package/bin/make-shell.js +375 -0
- package/bin/repair.js +14 -26
- package/bin/seed.js +119 -0
- package/bin/sync-routes.js +107 -85
- package/lib/adapters/html-adapter.js +5 -0
- package/lib/adapters/stub-adapter.js +4 -0
- package/lib/add-component.js +153 -92
- package/lib/asset-loader.js +313 -3
- package/lib/assets/asset-graph.js +432 -0
- package/lib/assets/asset-pipeline.js +125 -0
- package/lib/assets/diagnostics-report.js +345 -0
- package/lib/assets/external-fetcher.js +173 -0
- package/lib/assets/font-orchestrator.js +348 -0
- package/lib/assets/font-usage-scanner.js +242 -0
- package/lib/assets/image-optimizer.js +229 -0
- package/lib/assets/index.js +51 -0
- package/lib/assets/preload-generator.js +70 -0
- package/lib/assets/public-url.js +153 -0
- package/lib/block-authoring-help.js +11 -0
- package/lib/blocks/ast-parser.js +67 -0
- package/lib/blocks/discover-blocks.js +146 -0
- package/lib/blocks/editor-scope.js +733 -0
- package/lib/blocks/editor-transpiler.js +1229 -0
- package/lib/blocks/index.js +2838 -0
- package/lib/blocks/php-transpiler.js +4928 -0
- package/lib/blocks/scanner.js +1487 -0
- package/lib/blocks/shared-utils.js +290 -0
- package/lib/blocks/source-sanitize.js +1449 -0
- package/lib/blueprints.js +31 -91
- package/lib/build-assets.js +38 -33
- package/lib/collect-page-template-routes.js +102 -0
- package/lib/collect-static-routes.js +175 -0
- package/lib/diagnostics.js +58 -33
- package/lib/export-theme.js +84 -5
- package/lib/extract-template-content.js +90 -0
- package/lib/forms-preview.js +42 -0
- package/lib/functions/cpt-routing.js +69 -0
- package/lib/functions/forms.js +2461 -0
- package/lib/functions/hydration-enqueuer.js +400 -0
- package/lib/functions/index.js +1623 -0
- package/lib/functions/load-menus.js +112 -0
- package/lib/functions/load-translations.js +148 -0
- package/lib/functions/rest-endpoints.js +2923 -0
- package/lib/functions/routing.js +765 -0
- package/lib/functions/seed-mock-data.js +392 -0
- package/lib/functions/seed-products.js +481 -0
- package/lib/functions/settings-page.js +273 -0
- package/lib/functions/theme-setup.js +494 -0
- package/lib/generate-theme.js +1916 -339
- package/lib/hydration/editable-schemas.js +332 -0
- package/lib/hydration/enter-hidden-initial.js +65 -0
- package/lib/hydration/form-schemas.js +227 -0
- package/lib/hydration/index.js +12 -0
- package/lib/hydration/is-interactive.js +951 -0
- package/lib/hydration/island-split.js +1231 -0
- package/lib/hydration/islands-scanner.js +599 -0
- package/lib/hydration/resolve-island-chunk.js +103 -0
- package/lib/hydration/resolve-island-component.js +52 -0
- package/lib/hydration/rollup-inputs.js +81 -0
- package/lib/hydration/root-class-extractor.js +220 -0
- package/lib/hydration/scan-app-providers.js +154 -0
- package/lib/hydration/static-lint.js +382 -0
- package/lib/hydration/transform.js +257 -0
- package/lib/hydration/uses-block-area.js +49 -0
- package/lib/hydration/utils.js +119 -0
- package/lib/i18n/translation-cache.js +141 -0
- package/lib/icon-registry.js +72 -0
- package/lib/index.d.ts +287 -8
- package/lib/index.js +2 -1
- package/lib/load-config.js +338 -242
- package/lib/mail-templates.js +37 -0
- package/lib/markup-processor.js +388 -102
- package/lib/node-runtime.js +7 -0
- package/lib/page-config-plugin.js +64 -20
- package/lib/php-builders.js +383 -44
- package/lib/react-adapter.js +983 -235
- package/lib/registration-metadata.js +11 -0
- package/lib/render-theme.mts +1410 -616
- package/lib/route-layout.js +76 -0
- package/lib/template-discovery.js +58 -0
- package/lib/theme-compatibility.js +32 -0
- package/lib/validate-export.js +2 -2
- package/lib/validate.js +27 -5
- package/lib/virtual-plugin.js +319 -0
- package/lib/walkthrough.md +26 -0
- package/package.json +16 -5
- package/templates/auth/cms/email-logs.json +9 -0
- package/templates/auth/cms/menus.json +11 -0
- package/templates/auth/cms/mock-data.json +123 -0
- package/templates/auth/cms/roles.json +7 -0
- package/templates/auth/cms/sessions.json +1 -0
- package/templates/auth/cms/site-settings.json +28 -0
- package/templates/auth/cms/translations.json +4 -0
- package/templates/auth/cms/users.json +46 -0
- package/templates/auth/components/forgot-password-form-wrapper.tsx +10 -2
- package/templates/auth/components/login-form-wrapper.tsx +118 -21
- package/templates/auth/components/reset-password-form-wrapper.tsx +10 -2
- package/templates/auth/components/signup-form-wrapper.tsx +2 -2
- package/templates/auth/components/verify-email-view-wrapper.tsx +5 -5
- package/templates/auth/pages/DashboardPage.tsx +1 -4
- package/templates/auth/pages/ForgotPasswordPage.tsx +1 -4
- package/templates/auth/pages/LoginPage.tsx +1 -4
- package/templates/auth/pages/ResetPasswordPage.tsx +1 -4
- package/templates/auth/pages/SignUpPage.tsx +1 -4
- package/templates/auth/pages/VerifyEmailPage.tsx +1 -4
- package/templates/auth/ui/button.tsx +57 -0
- package/templates/auth/ui/card.tsx +76 -0
- package/templates/auth/ui/field.tsx +242 -0
- package/templates/auth/ui/input.tsx +22 -0
- package/templates/auth/ui/label.tsx +24 -0
- package/templates/auth/ui/separator.tsx +31 -0
- package/templates/menus.json +12 -10
- package/templates/users.json +8 -4
- package/templates/wordpress.tsx +690 -245
- package/lib/block-compiler.js +0 -552
- package/lib/functions-builder.js +0 -2970
- package/lib/hydration-scanner.js +0 -833
package/lib/load-config.js
CHANGED
|
@@ -1,242 +1,338 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
if (
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
if (
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}
|
|
119
|
-
if (config.
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
}
|
|
139
|
-
if (config.auth.
|
|
140
|
-
if (
|
|
141
|
-
throw new Error('auth.
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
if (
|
|
198
|
-
throw new Error('
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
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
|
+
}
|