@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/hydration-scanner.js
DELETED
|
@@ -1,833 +0,0 @@
|
|
|
1
|
-
import { readFileSync, readdirSync, existsSync, statSync } from "node:fs";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Scans the theme's page files (page.tsx, layout.tsx, pages/*.tsx)
|
|
6
|
-
* for <Hydrate> boundaries and extracts their resolved kebab-case component names.
|
|
7
|
-
*
|
|
8
|
-
* @param {string} themeRoot - The absolute path of the theme's root directory.
|
|
9
|
-
* @returns {string[]} An array of discovered kebab-case hydration chunk identifiers.
|
|
10
|
-
*/
|
|
11
|
-
export function scanForHydrationIslands(themeRoot) {
|
|
12
|
-
const islands = new Set();
|
|
13
|
-
const filesToScan = [];
|
|
14
|
-
|
|
15
|
-
const dirsToScan = [
|
|
16
|
-
path.join(themeRoot, "src", "app"),
|
|
17
|
-
path.join(themeRoot, "src", "components"),
|
|
18
|
-
path.join(themeRoot, "src", "blocks"),
|
|
19
|
-
];
|
|
20
|
-
|
|
21
|
-
function collectFilesRecursive(dir) {
|
|
22
|
-
if (!existsSync(dir)) return;
|
|
23
|
-
try {
|
|
24
|
-
const items = readdirSync(dir, { withFileTypes: true });
|
|
25
|
-
for (const item of items) {
|
|
26
|
-
const fullPath = path.join(dir, item.name);
|
|
27
|
-
if (item.isDirectory()) {
|
|
28
|
-
collectFilesRecursive(fullPath);
|
|
29
|
-
} else if (item.isFile() && (item.name.endsWith(".tsx") || item.name.endsWith(".ts"))) {
|
|
30
|
-
filesToScan.push(fullPath);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
} catch {}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
for (const dir of dirsToScan) {
|
|
37
|
-
collectFilesRecursive(dir);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// Scan each file for <Hydrate> occurrences
|
|
41
|
-
for (const filePath of filesToScan) {
|
|
42
|
-
try {
|
|
43
|
-
const content = readFileSync(filePath, "utf8");
|
|
44
|
-
|
|
45
|
-
// Matches <Hydrate ...> ... </Hydrate>
|
|
46
|
-
const hydrateRegex = /<Hydrate\b([\s\S]*?)>([\s\S]*?)<\/Hydrate>/g;
|
|
47
|
-
let match;
|
|
48
|
-
while ((match = hydrateRegex.exec(content)) !== null) {
|
|
49
|
-
const propsStr = match[1];
|
|
50
|
-
const childrenStr = match[2].trim();
|
|
51
|
-
|
|
52
|
-
// 1. Try to parse explicit 'id' attribute
|
|
53
|
-
const idMatch = propsStr.match(/id\s*=\s*["']([^"']*)["']/);
|
|
54
|
-
if (idMatch) {
|
|
55
|
-
islands.add(idMatch[1]);
|
|
56
|
-
continue;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// 2. Fallback: Parse Component Tag name from children (e.g. <FadeUpSection />)
|
|
60
|
-
const componentTagMatch = childrenStr.match(/<([A-Z][a-zA-Z0-9]*)\b/);
|
|
61
|
-
if (componentTagMatch) {
|
|
62
|
-
const compName = componentTagMatch[1];
|
|
63
|
-
const kebabName = compName
|
|
64
|
-
.replace(/([a-z0-9])([A-Z])/g, "$1-$2")
|
|
65
|
-
.toLowerCase();
|
|
66
|
-
islands.add(kebabName);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
} catch (err) {
|
|
70
|
-
console.warn(`[Hydration Scanner] Failed to scan file ${filePath}:`, err.message);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// 3. Smart Component Discovery: scan src/components/ for interactive/animated components
|
|
75
|
-
const compDir = path.join(themeRoot, "src", "components");
|
|
76
|
-
const interactiveComponents = new Set();
|
|
77
|
-
|
|
78
|
-
function scanComponentsRecursive(dir) {
|
|
79
|
-
if (!existsSync(dir)) return;
|
|
80
|
-
try {
|
|
81
|
-
const items = readdirSync(dir, { withFileTypes: true });
|
|
82
|
-
for (const item of items) {
|
|
83
|
-
const fullPath = path.join(dir, item.name);
|
|
84
|
-
if (item.isDirectory()) {
|
|
85
|
-
scanComponentsRecursive(fullPath);
|
|
86
|
-
} else if (item.isFile() && (item.name.endsWith(".tsx") || item.name.endsWith(".ts"))) {
|
|
87
|
-
try {
|
|
88
|
-
const fileContent = readFileSync(fullPath, "utf8");
|
|
89
|
-
const hasState = fileContent.includes("useState") || fileContent.includes("useEffect") || fileContent.includes("useRef");
|
|
90
|
-
const hasMotion = fileContent.includes("framer-motion") || fileContent.includes("gsap") || fileContent.includes("animate") || fileContent.includes("motion.");
|
|
91
|
-
if (hasState || hasMotion) {
|
|
92
|
-
const compName = path.basename(item.name, path.extname(item.name));
|
|
93
|
-
interactiveComponents.add(compName);
|
|
94
|
-
}
|
|
95
|
-
} catch {}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
} catch {}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
scanComponentsRecursive(compDir);
|
|
102
|
-
|
|
103
|
-
for (const filePath of filesToScan) {
|
|
104
|
-
try {
|
|
105
|
-
const content = readFileSync(filePath, "utf8");
|
|
106
|
-
for (const compName of interactiveComponents) {
|
|
107
|
-
if (content.includes(`<${compName}`)) {
|
|
108
|
-
const kebabName = compName.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
|
|
109
|
-
if (!islands.has(kebabName)) {
|
|
110
|
-
islands.add(kebabName);
|
|
111
|
-
console.log(`\x1b[33m[ForgeWP Optimizer] Smart Discovery: Detected "${compName}" uses interactive hooks/animations. Auto-enforcing code-splitting chunk. Wrap it in <Hydrate> in page markup for dynamic browser activation!\x1b[0m`);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
} catch {}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
return Array.from(islands);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Searches components directories for a file matching the kebab-case component name.
|
|
123
|
-
*
|
|
124
|
-
* @param {string} themeRoot - The absolute path of the theme's root directory.
|
|
125
|
-
* @param {string} kebabName - The target component's kebab-case identifier.
|
|
126
|
-
* @returns {string|null} The absolute path to the component file, or null if not found.
|
|
127
|
-
*/
|
|
128
|
-
export function findComponentPath(themeRoot, kebabName) {
|
|
129
|
-
const compDir = path.join(themeRoot, "src", "components");
|
|
130
|
-
if (!existsSync(compDir)) return null;
|
|
131
|
-
|
|
132
|
-
// Convert kebab-case back to PascalCase and check candidates
|
|
133
|
-
const pascalName = kebabName
|
|
134
|
-
.split("-")
|
|
135
|
-
.map((w) => w.charAt(0).toUpperCase() + w.slice(1))
|
|
136
|
-
.join("");
|
|
137
|
-
|
|
138
|
-
const candidates = [
|
|
139
|
-
`${pascalName}.tsx`,
|
|
140
|
-
`${pascalName}.ts`,
|
|
141
|
-
`${kebabName}.tsx`,
|
|
142
|
-
`${kebabName}.ts`,
|
|
143
|
-
];
|
|
144
|
-
|
|
145
|
-
let result = null;
|
|
146
|
-
|
|
147
|
-
function searchRecursive(dir) {
|
|
148
|
-
if (result) return;
|
|
149
|
-
try {
|
|
150
|
-
const items = readdirSync(dir, { withFileTypes: true });
|
|
151
|
-
for (const item of items) {
|
|
152
|
-
const fullPath = path.join(dir, item.name);
|
|
153
|
-
if (item.isDirectory()) {
|
|
154
|
-
searchRecursive(fullPath);
|
|
155
|
-
} else if (item.isFile()) {
|
|
156
|
-
if (candidates.includes(item.name)) {
|
|
157
|
-
result = fullPath;
|
|
158
|
-
return;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
} catch {}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
searchRecursive(compDir);
|
|
166
|
-
return result;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Resolves Rollup's multi-entry input mapping for Vite based on discovered hydration islands.
|
|
171
|
-
*
|
|
172
|
-
* @param {string} themeRoot - The absolute path of the theme's root directory.
|
|
173
|
-
* @returns {Record<string, string>} Rollup input object.
|
|
174
|
-
*/
|
|
175
|
-
export function getHydrationRollupInputs(themeRoot) {
|
|
176
|
-
const inputs = {
|
|
177
|
-
index: path.resolve(themeRoot, "index.html"),
|
|
178
|
-
};
|
|
179
|
-
|
|
180
|
-
const islands = scanForHydrationIslands(themeRoot);
|
|
181
|
-
for (const island of islands) {
|
|
182
|
-
const compPath = findComponentPath(themeRoot, island);
|
|
183
|
-
if (compPath) {
|
|
184
|
-
inputs[island] = compPath;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
// Scan and inject custom blocks from src/blocks/
|
|
189
|
-
const blocksDir = path.join(themeRoot, "src", "blocks");
|
|
190
|
-
if (existsSync(blocksDir)) {
|
|
191
|
-
try {
|
|
192
|
-
const items = readdirSync(blocksDir);
|
|
193
|
-
for (const item of items) {
|
|
194
|
-
const fullPath = path.join(blocksDir, item);
|
|
195
|
-
const isDir = statSync(fullPath).isDirectory();
|
|
196
|
-
if (isDir) {
|
|
197
|
-
const indexPath = path.join(fullPath, "index.tsx");
|
|
198
|
-
if (existsSync(indexPath)) {
|
|
199
|
-
const blockName = item
|
|
200
|
-
.replace(/([a-z0-9])([A-Z])/g, "$1-$2")
|
|
201
|
-
.toLowerCase()
|
|
202
|
-
.replace(/[^a-z0-9]+/g, "-");
|
|
203
|
-
inputs[`block-${blockName}`] = indexPath;
|
|
204
|
-
}
|
|
205
|
-
} else if (item.endsWith(".tsx") || item.endsWith(".jsx")) {
|
|
206
|
-
const blockName = item
|
|
207
|
-
.replace(/\.(tsx|jsx)$/, "")
|
|
208
|
-
.replace(/([a-z0-9])([A-Z])/g, "$1-$2")
|
|
209
|
-
.toLowerCase()
|
|
210
|
-
.replace(/[^a-z0-9]+/g, "-");
|
|
211
|
-
inputs[`block-${blockName}`] = fullPath;
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
} catch (err) {
|
|
215
|
-
console.warn("[Hydration Scanner] Failed to scan src/blocks/:", err.message);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
return inputs;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
/**
|
|
223
|
-
* Scans theme pages and layout files for <Hydrate> boundaries and extracts their kebab-case names,
|
|
224
|
-
* source files, and defined properties.
|
|
225
|
-
*
|
|
226
|
-
* @param {string} themeRoot - The absolute path of the theme's root directory.
|
|
227
|
-
* @returns {Array<{ name: string, file: string, trigger: string, preload: string, media: string|null, connection: string|null, clientOnly: boolean, smartDiscovered?: boolean }>}
|
|
228
|
-
*/
|
|
229
|
-
export function scanForHydrationIslandsWithProps(themeRoot) {
|
|
230
|
-
const discovered = [];
|
|
231
|
-
const namesFound = new Set();
|
|
232
|
-
const filesToScan = [];
|
|
233
|
-
|
|
234
|
-
const dirsToScan = [
|
|
235
|
-
path.join(themeRoot, "src", "app"),
|
|
236
|
-
path.join(themeRoot, "src", "components"),
|
|
237
|
-
path.join(themeRoot, "src", "blocks"),
|
|
238
|
-
];
|
|
239
|
-
|
|
240
|
-
function collectFilesRecursive(dir) {
|
|
241
|
-
if (!existsSync(dir)) return;
|
|
242
|
-
try {
|
|
243
|
-
const items = readdirSync(dir, { withFileTypes: true });
|
|
244
|
-
for (const item of items) {
|
|
245
|
-
const fullPath = path.join(dir, item.name);
|
|
246
|
-
if (item.isDirectory()) {
|
|
247
|
-
collectFilesRecursive(fullPath);
|
|
248
|
-
} else if (item.isFile() && (item.name.endsWith(".tsx") || item.name.endsWith(".ts"))) {
|
|
249
|
-
filesToScan.push(fullPath);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
} catch {}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
for (const dir of dirsToScan) {
|
|
256
|
-
collectFilesRecursive(dir);
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
// Helper to extract attribute values
|
|
260
|
-
const getAttr = (str, attr) => {
|
|
261
|
-
const regex1 = new RegExp(`${attr}\\s*=\\s*["']([^"']*)["']`);
|
|
262
|
-
const regex2 = new RegExp(`${attr}\\s*=\\s*\\{["']([^"']*)["']\\}`);
|
|
263
|
-
const regex3 = new RegExp(`${attr}\\s*=\\s*\\{([^\\}]*)\\}`);
|
|
264
|
-
|
|
265
|
-
const m1 = str.match(regex1);
|
|
266
|
-
if (m1) return m1[1];
|
|
267
|
-
const m2 = str.match(regex2);
|
|
268
|
-
if (m2) return m2[1];
|
|
269
|
-
const m3 = str.match(regex3);
|
|
270
|
-
if (m3) return m3[1].trim();
|
|
271
|
-
|
|
272
|
-
return null;
|
|
273
|
-
};
|
|
274
|
-
|
|
275
|
-
for (const filePath of filesToScan) {
|
|
276
|
-
const relFile = path.relative(themeRoot, filePath).replace(/\\/g, "/");
|
|
277
|
-
try {
|
|
278
|
-
const content = readFileSync(filePath, "utf8");
|
|
279
|
-
const hydrateRegex = /<Hydrate\b([\s\S]*?)>([\s\S]*?)<\/Hydrate>/g;
|
|
280
|
-
let match;
|
|
281
|
-
while ((match = hydrateRegex.exec(content)) !== null) {
|
|
282
|
-
const propsStr = match[1];
|
|
283
|
-
const childrenStr = match[2].trim();
|
|
284
|
-
|
|
285
|
-
// Resolve component identifier name
|
|
286
|
-
let islandName = null;
|
|
287
|
-
const idMatch = propsStr.match(/id\s*=\s*["']([^"']*)["']/);
|
|
288
|
-
if (idMatch) {
|
|
289
|
-
islandName = idMatch[1];
|
|
290
|
-
} else {
|
|
291
|
-
const componentTagMatch = childrenStr.match(/<([A-Z][a-zA-Z0-9]*)\b/);
|
|
292
|
-
if (componentTagMatch) {
|
|
293
|
-
const compName = componentTagMatch[1];
|
|
294
|
-
islandName = compName.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
if (islandName) {
|
|
299
|
-
namesFound.add(islandName);
|
|
300
|
-
const trigger = getAttr(propsStr, "trigger") || "visible";
|
|
301
|
-
const preload = getAttr(propsStr, "preload") || "none";
|
|
302
|
-
const media = getAttr(propsStr, "media");
|
|
303
|
-
const connection = getAttr(propsStr, "connection") || "any";
|
|
304
|
-
const clientOnly = propsStr.includes("clientOnly") && !propsStr.includes("clientOnly={false}");
|
|
305
|
-
|
|
306
|
-
discovered.push({
|
|
307
|
-
name: islandName,
|
|
308
|
-
file: relFile,
|
|
309
|
-
trigger,
|
|
310
|
-
preload,
|
|
311
|
-
media,
|
|
312
|
-
connection,
|
|
313
|
-
clientOnly,
|
|
314
|
-
});
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
} catch (err) {
|
|
318
|
-
console.warn(`[Hydration Scanner] Failed to scan file ${filePath}:`, err.message);
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
// Smart Discovery matching
|
|
323
|
-
const compDir = path.join(themeRoot, "src", "components");
|
|
324
|
-
const interactiveComponents = new Set();
|
|
325
|
-
|
|
326
|
-
function scanComponentsRecursive(dir) {
|
|
327
|
-
if (!existsSync(dir)) return;
|
|
328
|
-
try {
|
|
329
|
-
const items = readdirSync(dir, { withFileTypes: true });
|
|
330
|
-
for (const item of items) {
|
|
331
|
-
const fullPath = path.join(dir, item.name);
|
|
332
|
-
if (item.isDirectory()) {
|
|
333
|
-
scanComponentsRecursive(fullPath);
|
|
334
|
-
} else if (item.isFile() && (item.name.endsWith(".tsx") || item.name.endsWith(".ts"))) {
|
|
335
|
-
try {
|
|
336
|
-
const fileContent = readFileSync(fullPath, "utf8");
|
|
337
|
-
const hasState = fileContent.includes("useState") || fileContent.includes("useEffect") || fileContent.includes("useRef");
|
|
338
|
-
const hasMotion = fileContent.includes("framer-motion") || fileContent.includes("gsap") || fileContent.includes("animate") || fileContent.includes("motion.");
|
|
339
|
-
if (hasState || hasMotion) {
|
|
340
|
-
const compName = path.basename(item.name, path.extname(item.name));
|
|
341
|
-
interactiveComponents.add(compName);
|
|
342
|
-
}
|
|
343
|
-
} catch {}
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
} catch {}
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
scanComponentsRecursive(compDir);
|
|
350
|
-
|
|
351
|
-
for (const filePath of filesToScan) {
|
|
352
|
-
const relFile = path.relative(themeRoot, filePath).replace(/\\/g, "/");
|
|
353
|
-
try {
|
|
354
|
-
const content = readFileSync(filePath, "utf8");
|
|
355
|
-
for (const compName of interactiveComponents) {
|
|
356
|
-
if (content.includes(`<${compName}`)) {
|
|
357
|
-
const kebabName = compName.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
|
|
358
|
-
if (!namesFound.has(kebabName)) {
|
|
359
|
-
namesFound.add(kebabName);
|
|
360
|
-
discovered.push({
|
|
361
|
-
name: kebabName,
|
|
362
|
-
file: relFile,
|
|
363
|
-
trigger: "visible",
|
|
364
|
-
preload: "none",
|
|
365
|
-
media: null,
|
|
366
|
-
connection: "any",
|
|
367
|
-
clientOnly: false,
|
|
368
|
-
smartDiscovered: true,
|
|
369
|
-
});
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
} catch {}
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
return discovered;
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
/**
|
|
380
|
-
* Statically analyzes theme templates to detect missing metadata,
|
|
381
|
-
* dynamic event handlers in static WpQueryLoop scopes, and hardcoded relative URLs.
|
|
382
|
-
*
|
|
383
|
-
* @param {string} themeRoot
|
|
384
|
-
* @returns {Array<{ type: 'metadata' | 'loop' | 'url', file: string, message: string, severity: 'warning' | 'error', lineContent?: string }>}
|
|
385
|
-
*/
|
|
386
|
-
export function runStaticLintChecks(themeRoot) {
|
|
387
|
-
const violations = [];
|
|
388
|
-
const filesToScan = [];
|
|
389
|
-
|
|
390
|
-
// Load CPTs, Taxonomies, and Custom Fields from wp.config.ts and cms/mock-data.json
|
|
391
|
-
const validPostTypes = new Set(["post", "page"]);
|
|
392
|
-
const postTypeTaxonomies = {
|
|
393
|
-
"post": new Set(["category", "post_tag"]),
|
|
394
|
-
"page": new Set()
|
|
395
|
-
};
|
|
396
|
-
const postTypeCustomFields = {
|
|
397
|
-
"post": new Set(),
|
|
398
|
-
"page": new Set()
|
|
399
|
-
};
|
|
400
|
-
|
|
401
|
-
// 1. Scan CPTs from wp.config.ts
|
|
402
|
-
const configPath = path.join(themeRoot, "wp.config.ts");
|
|
403
|
-
if (existsSync(configPath)) {
|
|
404
|
-
try {
|
|
405
|
-
const configContent = readFileSync(configPath, "utf8");
|
|
406
|
-
const postTypesMatch = configContent.match(/postTypes\s*:\s*\{([\s\S]*?)\}/);
|
|
407
|
-
if (postTypesMatch) {
|
|
408
|
-
const block = postTypesMatch[1];
|
|
409
|
-
const keyRegex = /\b([a-zA-Z0-9_-]+)\s*:/g;
|
|
410
|
-
let m;
|
|
411
|
-
while ((m = keyRegex.exec(block)) !== null) {
|
|
412
|
-
const pt = m[1];
|
|
413
|
-
if (pt !== 'labels' && pt !== 'singular' && pt !== 'plural' && pt !== 'translatable') {
|
|
414
|
-
validPostTypes.add(pt);
|
|
415
|
-
if (!postTypeTaxonomies[pt]) {
|
|
416
|
-
postTypeTaxonomies[pt] = new Set(["category", "post_tag"]);
|
|
417
|
-
}
|
|
418
|
-
if (!postTypeCustomFields[pt]) {
|
|
419
|
-
postTypeCustomFields[pt] = new Set();
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
} catch {}
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
// 2. Scan CPTs, Taxonomies, and Custom Fields from cms/mock-data.json and cms/products.json
|
|
428
|
-
const dataFiles = [
|
|
429
|
-
path.join(themeRoot, "cms", "mock-data.json"),
|
|
430
|
-
path.join(themeRoot, "cms", "products.json")
|
|
431
|
-
];
|
|
432
|
-
for (const dbPath of dataFiles) {
|
|
433
|
-
if (existsSync(dbPath)) {
|
|
434
|
-
try {
|
|
435
|
-
const mockData = JSON.parse(readFileSync(dbPath, "utf8"));
|
|
436
|
-
for (const pt of Object.keys(mockData)) {
|
|
437
|
-
if (
|
|
438
|
-
pt !== 'posts' &&
|
|
439
|
-
pt !== 'pages' &&
|
|
440
|
-
pt !== 'post' &&
|
|
441
|
-
pt !== 'page' &&
|
|
442
|
-
pt !== 'menus' &&
|
|
443
|
-
pt !== 'attachment' &&
|
|
444
|
-
!pt.startsWith('_')
|
|
445
|
-
) {
|
|
446
|
-
validPostTypes.add(pt);
|
|
447
|
-
if (!postTypeTaxonomies[pt]) {
|
|
448
|
-
postTypeTaxonomies[pt] = new Set(["category", "post_tag"]);
|
|
449
|
-
}
|
|
450
|
-
if (!postTypeCustomFields[pt]) {
|
|
451
|
-
postTypeCustomFields[pt] = new Set();
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
const items = mockData[pt];
|
|
455
|
-
if (Array.isArray(items)) {
|
|
456
|
-
for (const item of items) {
|
|
457
|
-
if (item._terms && typeof item._terms === 'object') {
|
|
458
|
-
for (const tax of Object.keys(item._terms)) {
|
|
459
|
-
postTypeTaxonomies[pt].add(tax);
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
if (item.customFields && typeof item.customFields === 'object') {
|
|
463
|
-
for (const key of Object.keys(item.customFields)) {
|
|
464
|
-
postTypeCustomFields[pt].add(key);
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
} else if (pt === 'post' || pt === 'page') {
|
|
470
|
-
const items = mockData[pt];
|
|
471
|
-
if (Array.isArray(items)) {
|
|
472
|
-
for (const item of items) {
|
|
473
|
-
if (item._terms && typeof item._terms === 'object') {
|
|
474
|
-
for (const tax of Object.keys(item._terms)) {
|
|
475
|
-
postTypeTaxonomies[pt].add(tax);
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
if (item.customFields && typeof item.customFields === 'object') {
|
|
479
|
-
for (const key of Object.keys(item.customFields)) {
|
|
480
|
-
postTypeCustomFields[pt].add(key);
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
}
|
|
487
|
-
} catch {}
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
const validSlugs = new Set(["page"]);
|
|
492
|
-
const pagesDir = path.join(themeRoot, "src", "app", "pages");
|
|
493
|
-
if (existsSync(pagesDir)) {
|
|
494
|
-
try {
|
|
495
|
-
const pageFiles = readdirSync(pagesDir).filter(f => f.endsWith(".tsx"));
|
|
496
|
-
for (const pf of pageFiles) {
|
|
497
|
-
const pageName = pf.replace(".tsx", "");
|
|
498
|
-
if (pageName.startsWith("Single")) {
|
|
499
|
-
continue;
|
|
500
|
-
}
|
|
501
|
-
const slug = pageName.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
|
|
502
|
-
validSlugs.add(slug);
|
|
503
|
-
}
|
|
504
|
-
} catch {}
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
const dirsToScan = [
|
|
508
|
-
path.join(themeRoot, "src", "app"),
|
|
509
|
-
path.join(themeRoot, "src", "components"),
|
|
510
|
-
path.join(themeRoot, "src", "blocks"),
|
|
511
|
-
];
|
|
512
|
-
|
|
513
|
-
function collectFilesRecursive(dir) {
|
|
514
|
-
if (!existsSync(dir)) return;
|
|
515
|
-
try {
|
|
516
|
-
const items = readdirSync(dir, { withFileTypes: true });
|
|
517
|
-
for (const item of items) {
|
|
518
|
-
const fullPath = path.join(dir, item.name);
|
|
519
|
-
if (item.isDirectory()) {
|
|
520
|
-
collectFilesRecursive(fullPath);
|
|
521
|
-
} else if (item.isFile() && (item.name.endsWith(".tsx") || item.name.endsWith(".ts"))) {
|
|
522
|
-
filesToScan.push(fullPath);
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
} catch {}
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
for (const dir of dirsToScan) {
|
|
529
|
-
collectFilesRecursive(dir);
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
for (const filePath of filesToScan) {
|
|
533
|
-
const relFile = path.relative(themeRoot, filePath).replace(/\\/g, "/");
|
|
534
|
-
try {
|
|
535
|
-
const content = readFileSync(filePath, "utf8");
|
|
536
|
-
const lines = content.split("\n");
|
|
537
|
-
|
|
538
|
-
// 1. Metadata Checks (Only for page files)
|
|
539
|
-
const isPage = path.basename(filePath).startsWith("page.tsx") || filePath.includes("pages/");
|
|
540
|
-
if (isPage) {
|
|
541
|
-
const hasWpHead = content.includes("<WpHead") || content.includes("<WpHead\b");
|
|
542
|
-
if (!hasWpHead) {
|
|
543
|
-
violations.push({
|
|
544
|
-
type: "metadata",
|
|
545
|
-
file: relFile,
|
|
546
|
-
message: "Missing SEO <WpHead> component on this major page template.",
|
|
547
|
-
severity: "warning"
|
|
548
|
-
});
|
|
549
|
-
} else {
|
|
550
|
-
const wpHeadRegex = /<WpHead\b([\s\S]*?)\/?>/g;
|
|
551
|
-
let headMatch;
|
|
552
|
-
while ((headMatch = wpHeadRegex.exec(content)) !== null) {
|
|
553
|
-
const headProps = headMatch[1];
|
|
554
|
-
const hasTitle = /title\s*=\s*(?:["']|\{)/.test(headProps);
|
|
555
|
-
const hasDescription = /description\s*=\s*(?:["']|\{)/.test(headProps);
|
|
556
|
-
|
|
557
|
-
if (!hasTitle) {
|
|
558
|
-
violations.push({
|
|
559
|
-
type: "metadata",
|
|
560
|
-
file: relFile,
|
|
561
|
-
message: "SEO <WpHead> is missing the required 'title' attribute.",
|
|
562
|
-
severity: "warning"
|
|
563
|
-
});
|
|
564
|
-
}
|
|
565
|
-
if (!hasDescription) {
|
|
566
|
-
violations.push({
|
|
567
|
-
type: "metadata",
|
|
568
|
-
file: relFile,
|
|
569
|
-
message: "SEO <WpHead> is missing the required 'description' attribute.",
|
|
570
|
-
severity: "warning"
|
|
571
|
-
});
|
|
572
|
-
}
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
|
-
}
|
|
576
|
-
|
|
577
|
-
// 2. WpQueryLoop without Hydration check
|
|
578
|
-
const hydrateBlocks = [];
|
|
579
|
-
const hydrateRegex = /<Hydrate\b([\s\S]*?)>([\s\S]*?)<\/Hydrate>/g;
|
|
580
|
-
let hydMatch;
|
|
581
|
-
while ((hydMatch = hydrateRegex.exec(content)) !== null) {
|
|
582
|
-
hydrateBlocks.push({
|
|
583
|
-
start: hydMatch.index,
|
|
584
|
-
end: hydMatch.index + hydMatch[0].length
|
|
585
|
-
});
|
|
586
|
-
}
|
|
587
|
-
|
|
588
|
-
const loopRegex = /<WpQueryLoop\b([\s\S]*?)>([\s\S]*?)<\/WpQueryLoop>/g;
|
|
589
|
-
let loopMatch;
|
|
590
|
-
while ((loopMatch = loopRegex.exec(content)) !== null) {
|
|
591
|
-
const loopBody = loopMatch[2];
|
|
592
|
-
const loopStart = loopMatch.index;
|
|
593
|
-
|
|
594
|
-
const isHydrated = hydrateBlocks.some(hb => loopStart >= hb.start && loopStart <= hb.end);
|
|
595
|
-
|
|
596
|
-
if (!isHydrated) {
|
|
597
|
-
const hasEventHandlers = /\bon[A-Z][a-zA-Z]*\s*=\s*\{/.test(loopBody);
|
|
598
|
-
if (hasEventHandlers) {
|
|
599
|
-
const eventMatch = loopBody.match(/\b(on[A-Z][a-zA-Z]*)\s*=\s*\{/);
|
|
600
|
-
const eventName = eventMatch ? eventMatch[1] : "event handler";
|
|
601
|
-
violations.push({
|
|
602
|
-
type: "loop",
|
|
603
|
-
file: relFile,
|
|
604
|
-
message: `WpQueryLoop contains active interactive handler (${eventName}) but is not wrapped inside a <Hydrate> boundary. It will render static and non-interactive in production WordPress.`,
|
|
605
|
-
severity: "error"
|
|
606
|
-
});
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
}
|
|
610
|
-
|
|
611
|
-
// 3. Hardcoded relative internal links check
|
|
612
|
-
for (let i = 0; i < lines.length; i++) {
|
|
613
|
-
const line = lines[i];
|
|
614
|
-
const hrefRegex = /href\s*=\s*["']([^"']*)["']/g;
|
|
615
|
-
let hrefMatch;
|
|
616
|
-
while ((hrefMatch = hrefRegex.exec(line)) !== null) {
|
|
617
|
-
const val = hrefMatch[1];
|
|
618
|
-
const isRelative = val.startsWith("/") &&
|
|
619
|
-
!val.startsWith("//") &&
|
|
620
|
-
!val.startsWith("/wp-") &&
|
|
621
|
-
!val.startsWith("/feed");
|
|
622
|
-
if (isRelative) {
|
|
623
|
-
violations.push({
|
|
624
|
-
type: "url",
|
|
625
|
-
file: relFile,
|
|
626
|
-
message: `Hardcoded relative URL "${val}" found. Use useWpPermalink() to resolve dynamically from WordPress.`,
|
|
627
|
-
severity: "warning",
|
|
628
|
-
lineContent: line.trim() + ` (line ${i + 1})`
|
|
629
|
-
});
|
|
630
|
-
}
|
|
631
|
-
}
|
|
632
|
-
|
|
633
|
-
const jsxHrefRegex = /href\s*=\s*\{\s*["']([^"']*)["']\s*\}/g;
|
|
634
|
-
let jsxHrefMatch;
|
|
635
|
-
while ((jsxHrefMatch = jsxHrefRegex.exec(line)) !== null) {
|
|
636
|
-
const val = jsxHrefMatch[1];
|
|
637
|
-
const isRelative = val.startsWith("/") &&
|
|
638
|
-
!val.startsWith("//") &&
|
|
639
|
-
!val.startsWith("/wp-") &&
|
|
640
|
-
!val.startsWith("/feed");
|
|
641
|
-
if (isRelative) {
|
|
642
|
-
violations.push({
|
|
643
|
-
type: "url",
|
|
644
|
-
file: relFile,
|
|
645
|
-
message: `Hardcoded relative URL "${val}" found. Use useWpPermalink() to resolve dynamically from WordPress.`,
|
|
646
|
-
severity: "warning",
|
|
647
|
-
lineContent: line.trim() + ` (line ${i + 1})`
|
|
648
|
-
});
|
|
649
|
-
}
|
|
650
|
-
}
|
|
651
|
-
}
|
|
652
|
-
|
|
653
|
-
// 4. useWpPageLink template slug typo check
|
|
654
|
-
const pageLinkRegex = /useWpPageLink\s*\(\s*["']([^"']+)["']/g;
|
|
655
|
-
let pageLinkMatch;
|
|
656
|
-
while ((pageLinkMatch = pageLinkRegex.exec(content)) !== null) {
|
|
657
|
-
const slugUsed = pageLinkMatch[1];
|
|
658
|
-
if (!validSlugs.has(slugUsed)) {
|
|
659
|
-
violations.push({
|
|
660
|
-
type: "pagelink",
|
|
661
|
-
file: relFile,
|
|
662
|
-
message: `useWpPageLink references non-existent page template slug "${slugUsed}". Valid slugs: ${Array.from(validSlugs).join(", ")}`,
|
|
663
|
-
severity: "warning"
|
|
664
|
-
});
|
|
665
|
-
}
|
|
666
|
-
}
|
|
667
|
-
|
|
668
|
-
// 5. useWpQuery arguments audit
|
|
669
|
-
const useWpQueryRegex = /useWpQuery\s*\(\s*(\{[\s\S]*?\})\s*\)/g;
|
|
670
|
-
let queryMatch;
|
|
671
|
-
while ((queryMatch = useWpQueryRegex.exec(content)) !== null) {
|
|
672
|
-
const argsStr = queryMatch[1];
|
|
673
|
-
|
|
674
|
-
const postTypeMatch = argsStr.match(/postType\s*:\s*['"]([^'"]+)['"]/);
|
|
675
|
-
const postType = postTypeMatch ? postTypeMatch[1] : 'post';
|
|
676
|
-
|
|
677
|
-
if (!validPostTypes.has(postType)) {
|
|
678
|
-
violations.push({
|
|
679
|
-
type: "query",
|
|
680
|
-
file: relFile,
|
|
681
|
-
message: `useWpQuery references unregistered postType "${postType}".`,
|
|
682
|
-
severity: "warning"
|
|
683
|
-
});
|
|
684
|
-
} else {
|
|
685
|
-
// Validate Taxonomies inside taxQuery
|
|
686
|
-
const taxRegex = /\btaxonomy\s*:\s*['"]([^'"]+)['"]/g;
|
|
687
|
-
let taxMatch;
|
|
688
|
-
const allowedTaxonomies = postTypeTaxonomies[postType] || new Set();
|
|
689
|
-
while ((taxMatch = taxRegex.exec(argsStr)) !== null) {
|
|
690
|
-
const taxSlug = taxMatch[1];
|
|
691
|
-
if (!allowedTaxonomies.has(taxSlug)) {
|
|
692
|
-
violations.push({
|
|
693
|
-
type: "query",
|
|
694
|
-
file: relFile,
|
|
695
|
-
message: `useWpQuery in ${path.basename(filePath)} queries taxonomy "${taxSlug}", which is not registered for postType "${postType}" in wp.config.ts.`,
|
|
696
|
-
severity: "warning"
|
|
697
|
-
});
|
|
698
|
-
}
|
|
699
|
-
}
|
|
700
|
-
|
|
701
|
-
// Validate Custom Fields inside metaQuery
|
|
702
|
-
const keyRegex = /\bkey\s*:\s*['"]([^'"]+)['"]/g;
|
|
703
|
-
let keyMatch;
|
|
704
|
-
const allowedFields = postTypeCustomFields[postType] || new Set();
|
|
705
|
-
while ((keyMatch = keyRegex.exec(argsStr)) !== null) {
|
|
706
|
-
const fieldKey = keyMatch[1];
|
|
707
|
-
if (!allowedFields.has(fieldKey)) {
|
|
708
|
-
violations.push({
|
|
709
|
-
type: "query",
|
|
710
|
-
file: relFile,
|
|
711
|
-
message: `useWpQuery in ${path.basename(filePath)} queries custom field "${fieldKey}", which is not defined for postType "${postType}" in wp.config.ts.`,
|
|
712
|
-
severity: "warning"
|
|
713
|
-
});
|
|
714
|
-
}
|
|
715
|
-
}
|
|
716
|
-
}
|
|
717
|
-
}
|
|
718
|
-
|
|
719
|
-
} catch (err) {
|
|
720
|
-
console.warn(`[Static Lint] Failed to scan file ${filePath}:`, err.message);
|
|
721
|
-
}
|
|
722
|
-
}
|
|
723
|
-
|
|
724
|
-
return violations;
|
|
725
|
-
}
|
|
726
|
-
|
|
727
|
-
/**
|
|
728
|
-
* Scans theme template page files for colocated `defineEditable` exports
|
|
729
|
-
* and parses them into a unified schema dictionary.
|
|
730
|
-
*
|
|
731
|
-
* @param {string} themeRoot
|
|
732
|
-
* @returns {Record<string, Record<string, any>>} A map of template/page slug -> parsed schema
|
|
733
|
-
*/
|
|
734
|
-
export function scanForEditableSchemas(themeRoot) {
|
|
735
|
-
const schemas = {};
|
|
736
|
-
const filesToScan = [];
|
|
737
|
-
|
|
738
|
-
const dirsToScan = [
|
|
739
|
-
path.join(themeRoot, "src", "app"),
|
|
740
|
-
];
|
|
741
|
-
|
|
742
|
-
function collectFilesRecursive(dir) {
|
|
743
|
-
if (!existsSync(dir)) return;
|
|
744
|
-
try {
|
|
745
|
-
const items = readdirSync(dir, { withFileTypes: true });
|
|
746
|
-
for (const item of items) {
|
|
747
|
-
const fullPath = path.join(dir, item.name);
|
|
748
|
-
if (item.isDirectory()) {
|
|
749
|
-
collectFilesRecursive(fullPath);
|
|
750
|
-
} else if (item.isFile() && (item.name.endsWith(".tsx") || item.name.endsWith(".ts"))) {
|
|
751
|
-
filesToScan.push(fullPath);
|
|
752
|
-
}
|
|
753
|
-
}
|
|
754
|
-
} catch {}
|
|
755
|
-
}
|
|
756
|
-
|
|
757
|
-
for (const dir of dirsToScan) {
|
|
758
|
-
collectFilesRecursive(dir);
|
|
759
|
-
}
|
|
760
|
-
|
|
761
|
-
for (const filePath of filesToScan) {
|
|
762
|
-
try {
|
|
763
|
-
const content = readFileSync(filePath, "utf8");
|
|
764
|
-
if (!content.includes("defineEditable")) continue;
|
|
765
|
-
|
|
766
|
-
const schemaStr = extractDefineEditableContent(content);
|
|
767
|
-
if (schemaStr) {
|
|
768
|
-
const parsed = parseEditableSchema(schemaStr);
|
|
769
|
-
if (parsed) {
|
|
770
|
-
// Resolve template slug based on file name or path
|
|
771
|
-
// e.g. "SingleHotelPage.tsx" -> "single-hotel-page"
|
|
772
|
-
// or "page.tsx" -> "front-page" or similar
|
|
773
|
-
const baseName = path.basename(filePath, path.extname(filePath));
|
|
774
|
-
let slug = baseName
|
|
775
|
-
.replace(/([a-z0-9])([A-Z])/g, "$1-$2")
|
|
776
|
-
.toLowerCase();
|
|
777
|
-
|
|
778
|
-
if (slug === 'page') {
|
|
779
|
-
slug = 'front-page';
|
|
780
|
-
}
|
|
781
|
-
|
|
782
|
-
schemas[slug] = parsed;
|
|
783
|
-
}
|
|
784
|
-
}
|
|
785
|
-
} catch (err) {
|
|
786
|
-
console.warn(`[Schema Scanner] Failed to scan file ${filePath}:`, err.message);
|
|
787
|
-
}
|
|
788
|
-
}
|
|
789
|
-
|
|
790
|
-
return schemas;
|
|
791
|
-
}
|
|
792
|
-
|
|
793
|
-
function extractDefineEditableContent(fileContent) {
|
|
794
|
-
const match = fileContent.match(/export\s+const\s+editable\s*=\s*defineEditable\s*\(/);
|
|
795
|
-
if (!match) return null;
|
|
796
|
-
|
|
797
|
-
const startIndex = match.index + match[0].length;
|
|
798
|
-
let parenCount = 1;
|
|
799
|
-
let currentIndex = startIndex;
|
|
800
|
-
|
|
801
|
-
while (parenCount > 0 && currentIndex < fileContent.length) {
|
|
802
|
-
const char = fileContent[currentIndex];
|
|
803
|
-
if (char === '(') {
|
|
804
|
-
parenCount++;
|
|
805
|
-
} else if (char === ')') {
|
|
806
|
-
parenCount--;
|
|
807
|
-
}
|
|
808
|
-
currentIndex++;
|
|
809
|
-
}
|
|
810
|
-
|
|
811
|
-
if (parenCount === 0) {
|
|
812
|
-
return fileContent.substring(startIndex, currentIndex - 1);
|
|
813
|
-
}
|
|
814
|
-
return null;
|
|
815
|
-
}
|
|
816
|
-
|
|
817
|
-
function parseEditableSchema(schemaStr) {
|
|
818
|
-
const text = (opts) => ({ type: 'text', ...opts });
|
|
819
|
-
const richText = (opts) => ({ type: 'richText', ...opts });
|
|
820
|
-
const image = (opts) => ({ type: 'image', ...opts });
|
|
821
|
-
const boolean = (opts) => ({ type: 'boolean', ...opts });
|
|
822
|
-
const repeater = (opts) => ({ type: 'repeater', ...opts });
|
|
823
|
-
|
|
824
|
-
try {
|
|
825
|
-
const fn = new Function('text', 'richText', 'image', 'boolean', 'repeater', `return (${schemaStr})`);
|
|
826
|
-
return fn(text, richText, image, boolean, repeater);
|
|
827
|
-
} catch (e) {
|
|
828
|
-
console.error('[Schema Parser] Failed to evaluate schema literal:', e.message);
|
|
829
|
-
return null;
|
|
830
|
-
}
|
|
831
|
-
}
|
|
832
|
-
|
|
833
|
-
|