@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/bin/i18n-extract.js
CHANGED
|
@@ -5,6 +5,7 @@ import path from 'node:path';
|
|
|
5
5
|
import pc from 'picocolors';
|
|
6
6
|
import { loadConfig } from '../lib/load-config.js';
|
|
7
7
|
import { extractKeysFromDirectory } from '../lib/i18n/extractor.js';
|
|
8
|
+
import { loadTranslationsData, writeTranslationsData } from '../lib/functions/load-translations.js';
|
|
8
9
|
|
|
9
10
|
console.log(`\n🗣️ ${pc.bold(pc.bgCyan(pc.black(" FORGEWP I18N EXTRACTOR ")))}\n`);
|
|
10
11
|
|
|
@@ -43,15 +44,7 @@ async function run() {
|
|
|
43
44
|
fs.mkdirSync(cmsDir, { recursive: true });
|
|
44
45
|
}
|
|
45
46
|
|
|
46
|
-
|
|
47
|
-
let translations = {};
|
|
48
|
-
if (fs.existsSync(translationsPath)) {
|
|
49
|
-
try {
|
|
50
|
-
translations = JSON.parse(fs.readFileSync(translationsPath, 'utf8'));
|
|
51
|
-
} catch (err) {
|
|
52
|
-
console.warn(pc.yellow(`⚠️ Could not parse existing translations.json, starting fresh.`));
|
|
53
|
-
}
|
|
54
|
-
}
|
|
47
|
+
let translations = loadTranslationsData(themeRoot);
|
|
55
48
|
|
|
56
49
|
// Initialize missing locales
|
|
57
50
|
for (const locale of locales) {
|
|
@@ -100,7 +93,7 @@ async function run() {
|
|
|
100
93
|
sortedTranslations[locale] = sortedMap;
|
|
101
94
|
}
|
|
102
95
|
|
|
103
|
-
|
|
96
|
+
const translationsPath = writeTranslationsData(themeRoot, sortedTranslations);
|
|
104
97
|
|
|
105
98
|
console.log(`\n${pc.bold("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")}`);
|
|
106
99
|
console.log(` ✨ ${pc.green("Extraction completed successfully!")}`);
|
package/bin/i18n-translate.js
CHANGED
|
@@ -5,6 +5,8 @@ import path from 'node:path';
|
|
|
5
5
|
import pc from 'picocolors';
|
|
6
6
|
import { loadConfig } from '../lib/load-config.js';
|
|
7
7
|
import { getProvider } from '../lib/i18n/providers.js';
|
|
8
|
+
import { createTranslationCache, translateMissingEntries } from '../lib/i18n/translation-cache.js';
|
|
9
|
+
import { loadTranslationsData, writeTranslationsData, translationsSourcePath } from '../lib/functions/load-translations.js';
|
|
8
10
|
|
|
9
11
|
console.log(`\n🤖 ${pc.bold(pc.bgCyan(pc.black(" FORGEWP I18N AUTO-TRANSLATOR ")))}\n`);
|
|
10
12
|
|
|
@@ -30,22 +32,19 @@ async function run() {
|
|
|
30
32
|
const locales = i18n.locales || ['en', 'de'];
|
|
31
33
|
const providerName = i18n.provider || 'local';
|
|
32
34
|
|
|
33
|
-
const translationsPath =
|
|
34
|
-
if (!
|
|
35
|
-
throw new Error(`Translations catalog not found at
|
|
35
|
+
const translationsPath = translationsSourcePath(themeRoot);
|
|
36
|
+
if (!translationsPath) {
|
|
37
|
+
throw new Error(`Translations catalog not found at cms/translations.ts or cms/translations.json. Please run "pnpm forgewp i18n:extract" first.`);
|
|
36
38
|
}
|
|
37
39
|
|
|
38
|
-
|
|
39
|
-
try {
|
|
40
|
-
translations = JSON.parse(fs.readFileSync(translationsPath, 'utf8'));
|
|
41
|
-
} catch (err) {
|
|
42
|
-
throw new Error(`Failed to parse translations.json: ${err.message}`);
|
|
43
|
-
}
|
|
40
|
+
const translations = loadTranslationsData(themeRoot);
|
|
44
41
|
|
|
45
42
|
console.log(` 🔌 Active provider: ${pc.cyan(providerName)}`);
|
|
46
43
|
console.log(` 🌐 Default locale: ${pc.yellow(defaultLocale)}`);
|
|
47
44
|
|
|
48
45
|
const provider = getProvider(providerName, i18n.providerConfig || {});
|
|
46
|
+
const cache = providerName === 'local' || providerName === 'manual' ? null :
|
|
47
|
+
createTranslationCache(themeRoot, i18n.cache, providerName, i18n.providerConfig || {});
|
|
49
48
|
|
|
50
49
|
let totalTranslated = 0;
|
|
51
50
|
|
|
@@ -74,32 +73,24 @@ async function run() {
|
|
|
74
73
|
continue;
|
|
75
74
|
}
|
|
76
75
|
|
|
77
|
-
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
for (const key of untranslatedKeys) {
|
|
82
|
-
if (translatedMap[key]) {
|
|
83
|
-
localeMap[key] = translatedMap[key];
|
|
84
|
-
successCount++;
|
|
85
|
-
totalTranslated++;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
76
|
+
const result = await translateMissingEntries(localeMap, defaultLocale, locale, provider, cache);
|
|
77
|
+
const successCount = result.translated + result.restored;
|
|
78
|
+
totalTranslated += result.translated;
|
|
79
|
+
if (result.restored) console.log(` Restored ${result.restored} translation(s) from cache.`);
|
|
88
80
|
console.log(` ✅ Translated ${pc.green(successCount)}/${pc.yellow(untranslatedKeys.length)} keys successfully.`);
|
|
89
81
|
}
|
|
90
82
|
|
|
91
|
-
|
|
92
|
-
fs.writeFileSync(translationsPath, JSON.stringify(translations, null, 2), 'utf8');
|
|
83
|
+
const writtenPath = writeTranslationsData(themeRoot, translations);
|
|
93
84
|
|
|
94
85
|
console.log(`\n${pc.bold("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")}`);
|
|
95
86
|
console.log(` ✨ ${pc.green("Translation execution completed successfully!")}`);
|
|
96
87
|
if (totalTranslated > 0) {
|
|
97
88
|
console.log(` 🎉 Total keys automated: ${pc.green(totalTranslated)}`);
|
|
98
89
|
} else if (providerName === 'local' || providerName === 'manual') {
|
|
99
|
-
console.log(` 💡 Setup complete. Open ${pc.cyan(
|
|
90
|
+
console.log(` 💡 Setup complete. Open ${pc.cyan(path.relative(themeRoot, writtenPath))} to write translations manually.`);
|
|
100
91
|
console.log(` Or configure 'deepl' to auto-translate using environment variables.`);
|
|
101
92
|
} else {
|
|
102
|
-
console.log(`
|
|
93
|
+
console.log(` No new translations were returned. Review any remaining blank catalog entries.`);
|
|
103
94
|
}
|
|
104
95
|
console.log(`\n`);
|
|
105
96
|
|
package/bin/make-block.js
CHANGED
|
@@ -7,8 +7,21 @@ import { stdin as input, stdout as output } from "node:process";
|
|
|
7
7
|
import pc from "picocolors";
|
|
8
8
|
import { loadConfig } from "../lib/load-config.js";
|
|
9
9
|
import { loadFrameworkAdapter } from "../lib/framework-adapter.js";
|
|
10
|
+
import { blockAuthoringHelp } from "../lib/block-authoring-help.js";
|
|
10
11
|
|
|
11
12
|
const args = process.argv.slice(2);
|
|
13
|
+
if (args.includes('--help') || args.includes('-h')) {
|
|
14
|
+
console.log(`forgewp make:block <Name> [--attributes=title,description]
|
|
15
|
+
|
|
16
|
+
Creates editable React block source at src/blocks/<Name>.tsx.
|
|
17
|
+
--name <Name> Alternative to the positional name.
|
|
18
|
+
--attributes, --attrs Comma-separated string attribute names (default: title,description).
|
|
19
|
+
-h, --help Show help without creating files or prompting.
|
|
20
|
+
|
|
21
|
+
Example: pnpm forgewp make:block PromoCard --attributes=title,description
|
|
22
|
+
${blockAuthoringHelp}`);
|
|
23
|
+
process.exit(0);
|
|
24
|
+
}
|
|
12
25
|
let rawBlockName = args.find(a => !a.startsWith("-"));
|
|
13
26
|
|
|
14
27
|
// Support --name TestimonialBlock or --name=TestimonialBlock
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import readline from "node:readline/promises";
|
|
6
|
+
import { stdin as input, stdout as output } from "node:process";
|
|
7
|
+
import pc from "picocolors";
|
|
8
|
+
|
|
9
|
+
const args = process.argv.slice(2);
|
|
10
|
+
let rawName = args.find(a => !a.startsWith("-"));
|
|
11
|
+
|
|
12
|
+
// Support --name AboutPage or --name=AboutPage
|
|
13
|
+
const nameIndex = args.indexOf("--name");
|
|
14
|
+
if (nameIndex !== -1 && args[nameIndex + 1]) {
|
|
15
|
+
rawName = args[nameIndex + 1];
|
|
16
|
+
} else {
|
|
17
|
+
const nameEqual = args.find(a => a.startsWith("--name="));
|
|
18
|
+
if (nameEqual) {
|
|
19
|
+
rawName = nameEqual.split("=")[1];
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (!rawName) {
|
|
24
|
+
const rl = readline.createInterface({ input, output });
|
|
25
|
+
try {
|
|
26
|
+
const answer = await rl.question(pc.cyan("? Enter a schema name (e.g. AboutPage or about-page): "));
|
|
27
|
+
rawName = answer.trim();
|
|
28
|
+
} catch (err) {}
|
|
29
|
+
rl.close();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (!rawName) {
|
|
33
|
+
console.error(pc.red("\n❌ Error: Please specify a schema name."));
|
|
34
|
+
console.log(pc.cyan(" Example: pnpm forgewp make:editable AboutPage"));
|
|
35
|
+
console.log(pc.cyan(" Or: pnpm forgewp make:editable --name AboutPage\n"));
|
|
36
|
+
process.exit(1);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// kebab-case slug, matching the cms/editables/<slug>.ts convention
|
|
40
|
+
// (front-page.ts, hotels-page.ts, …)
|
|
41
|
+
const slug = rawName
|
|
42
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1-$2")
|
|
43
|
+
.replace(/[\s_]+/g, "-")
|
|
44
|
+
.toLowerCase()
|
|
45
|
+
.replace(/[^a-z0-9-]/g, "");
|
|
46
|
+
|
|
47
|
+
if (!slug) {
|
|
48
|
+
console.error(pc.red("\n❌ Error: Invalid schema name. Use letters, numbers, dashes, and underscores.\n"));
|
|
49
|
+
process.exit(1);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const readableTitle = slug
|
|
53
|
+
.split("-")
|
|
54
|
+
.map(w => w.charAt(0).toUpperCase() + w.slice(1))
|
|
55
|
+
.join(" ");
|
|
56
|
+
|
|
57
|
+
const projectRoot = process.cwd();
|
|
58
|
+
|
|
59
|
+
if (!existsSync(path.join(projectRoot, "src"))) {
|
|
60
|
+
console.error(pc.red(`\n❌ Error: "src" folder not found. Are you in your theme's root directory?\n`));
|
|
61
|
+
process.exit(1);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Parse --fields=name:type,name2:type2 (type defaults to "text" when omitted)
|
|
65
|
+
const VALID_TYPES = ["text", "richText", "image", "boolean", "repeater", "color", "url", "select", "number", "icon"];
|
|
66
|
+
let fieldsList = [];
|
|
67
|
+
const fieldsArg = args.find(a => /^-+fields=/.test(a));
|
|
68
|
+
if (fieldsArg) {
|
|
69
|
+
const raw = fieldsArg.split("=")[1] || "";
|
|
70
|
+
fieldsList = raw.split(",").map(f => f.trim()).filter(Boolean).map(f => {
|
|
71
|
+
const [fieldName, fieldType] = f.split(":").map(s => s.trim());
|
|
72
|
+
const type = VALID_TYPES.includes(fieldType) ? fieldType : "text";
|
|
73
|
+
return { name: fieldName, type };
|
|
74
|
+
});
|
|
75
|
+
} else {
|
|
76
|
+
const fieldsIndex = args.findIndex(a => a === "--fields");
|
|
77
|
+
if (fieldsIndex !== -1 && args[fieldsIndex + 1]) {
|
|
78
|
+
fieldsList = args[fieldsIndex + 1].split(",").map(f => f.trim()).filter(Boolean).map(f => {
|
|
79
|
+
const [fieldName, fieldType] = f.split(":").map(s => s.trim());
|
|
80
|
+
const type = VALID_TYPES.includes(fieldType) ? fieldType : "text";
|
|
81
|
+
return { name: fieldName, type };
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (fieldsList.length === 0) {
|
|
87
|
+
fieldsList = [
|
|
88
|
+
{ name: "hero_title", type: "text" },
|
|
89
|
+
{ name: "hero_subtitle", type: "text" },
|
|
90
|
+
];
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const editablesDir = path.join(projectRoot, "cms", "editables");
|
|
94
|
+
if (!existsSync(editablesDir)) {
|
|
95
|
+
mkdirSync(editablesDir, { recursive: true });
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const targetFile = path.join(editablesDir, `${slug}.ts`);
|
|
99
|
+
if (existsSync(targetFile)) {
|
|
100
|
+
console.error(pc.red(`\n❌ Error: Schema "${slug}.ts" already exists at cms/editables/\n`));
|
|
101
|
+
process.exit(1);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const toLabel = (name) =>
|
|
105
|
+
name
|
|
106
|
+
.replace(/[_-]+/g, " ")
|
|
107
|
+
.split(" ")
|
|
108
|
+
.map(w => w.charAt(0).toUpperCase() + w.slice(1))
|
|
109
|
+
.join(" ");
|
|
110
|
+
|
|
111
|
+
const usedTypes = Array.from(new Set(fieldsList.map(f => f.type))).sort();
|
|
112
|
+
const importList = ["defineEditable", "getEditableDefaults", ...usedTypes].join(",\n ");
|
|
113
|
+
|
|
114
|
+
const fieldsPhp = fieldsList
|
|
115
|
+
.map(({ name, type }) => {
|
|
116
|
+
const label = toLabel(name);
|
|
117
|
+
if (type === "image") {
|
|
118
|
+
return ` ${name}: image({
|
|
119
|
+
label: '${label}',
|
|
120
|
+
default: '',
|
|
121
|
+
}),`;
|
|
122
|
+
}
|
|
123
|
+
if (type === "boolean") {
|
|
124
|
+
return ` ${name}: boolean({
|
|
125
|
+
label: '${label}',
|
|
126
|
+
default: false,
|
|
127
|
+
}),`;
|
|
128
|
+
}
|
|
129
|
+
return ` ${name}: ${type}({
|
|
130
|
+
label: '${label}',
|
|
131
|
+
default: 'Edit me',
|
|
132
|
+
}),`;
|
|
133
|
+
})
|
|
134
|
+
.join("\n");
|
|
135
|
+
|
|
136
|
+
const schemaTemplate = `/**
|
|
137
|
+
* ${readableTitle} — single editable contract.
|
|
138
|
+
*
|
|
139
|
+
* Source of truth for:
|
|
140
|
+
* - ACF field group (page-${slug}.php)
|
|
141
|
+
* - useWpMeta defaults on this template's section components
|
|
142
|
+
*
|
|
143
|
+
* Read a field with useWpMeta('key', defaults.key), or share a subset with
|
|
144
|
+
* a Gutenberg block via pickEditable(editable, { localName: 'key' }).
|
|
145
|
+
*
|
|
146
|
+
* Available field helpers: text, richText, image, boolean, repeater,
|
|
147
|
+
* color, url, select, number, icon — import the ones you use above.
|
|
148
|
+
*/
|
|
149
|
+
import {
|
|
150
|
+
${importList},
|
|
151
|
+
} from '@forgewp/react';
|
|
152
|
+
|
|
153
|
+
export const editable = defineEditable({
|
|
154
|
+
${fieldsPhp}
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
export const defaults = getEditableDefaults(editable);
|
|
158
|
+
`;
|
|
159
|
+
|
|
160
|
+
writeFileSync(targetFile, schemaTemplate, "utf8");
|
|
161
|
+
|
|
162
|
+
console.log(pc.green(`\n⚡ Editable schema "${slug}" successfully created!`));
|
|
163
|
+
console.log(` File: ${pc.cyan(`cms/editables/${slug}.ts`)}`);
|
|
164
|
+
console.log(`\n🎉 Use it in a page or section component:`);
|
|
165
|
+
console.log(` ${pc.yellow(`import { editable, defaults } from '../../cms/editables/${slug}';`)}`);
|
|
166
|
+
console.log(` ${pc.yellow(`const ${fieldsList[0].name} = useWpMeta('${fieldsList[0].name}', defaults.${fieldsList[0].name});`)}\n`);
|
package/bin/make-form.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { existsSync } from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import readline from "node:readline/promises";
|
|
6
|
+
import { stdin as input, stdout as output } from "node:process";
|
|
7
|
+
import pc from "picocolors";
|
|
8
|
+
import { loadConfig } from "../lib/load-config.js";
|
|
9
|
+
import { loadFrameworkAdapter } from "../lib/framework-adapter.js";
|
|
10
|
+
|
|
11
|
+
const args = process.argv.slice(2);
|
|
12
|
+
let rawName = args.find(a => !a.startsWith("-"));
|
|
13
|
+
|
|
14
|
+
// Support --name Contact or --name=Contact
|
|
15
|
+
const nameIndex = args.indexOf("--name");
|
|
16
|
+
if (nameIndex !== -1 && args[nameIndex + 1]) {
|
|
17
|
+
rawName = args[nameIndex + 1];
|
|
18
|
+
} else {
|
|
19
|
+
const nameEqual = args.find(a => a.startsWith("--name="));
|
|
20
|
+
if (nameEqual) {
|
|
21
|
+
rawName = nameEqual.split("=")[1];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (!rawName) {
|
|
26
|
+
const rl = readline.createInterface({ input, output });
|
|
27
|
+
try {
|
|
28
|
+
const answer = await rl.question(pc.cyan("? Enter a form name (e.g. Contact or contact): "));
|
|
29
|
+
rawName = answer.trim();
|
|
30
|
+
} catch (err) {}
|
|
31
|
+
rl.close();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (!rawName) {
|
|
35
|
+
console.error(pc.red("\n❌ Error: Please specify a form name."));
|
|
36
|
+
console.log(pc.cyan(" Example: pnpm forgewp make:form Contact"));
|
|
37
|
+
console.log(pc.cyan(" Or: pnpm forgewp make:form --name Contact\n"));
|
|
38
|
+
process.exit(1);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// kebab-case slug, matching the cms/forms/<slug>.ts convention
|
|
42
|
+
// (mirrors cms/editables/<slug>.ts's filename-as-key convention)
|
|
43
|
+
const slug = rawName
|
|
44
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1-$2")
|
|
45
|
+
.replace(/[\s_]+/g, "-")
|
|
46
|
+
.toLowerCase()
|
|
47
|
+
.replace(/[^a-z0-9-]/g, "");
|
|
48
|
+
|
|
49
|
+
if (!slug) {
|
|
50
|
+
console.error(pc.red("\n❌ Error: Invalid form name. Use letters, numbers, dashes, and underscores.\n"));
|
|
51
|
+
process.exit(1);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const pascalCase = slug
|
|
55
|
+
.split("-")
|
|
56
|
+
.map(w => w.charAt(0).toUpperCase() + w.slice(1))
|
|
57
|
+
.join("");
|
|
58
|
+
|
|
59
|
+
const projectRoot = process.cwd();
|
|
60
|
+
|
|
61
|
+
if (!existsSync(path.join(projectRoot, "src"))) {
|
|
62
|
+
console.error(pc.red(`\n❌ Error: "src" folder not found. Are you in your theme's root directory?\n`));
|
|
63
|
+
process.exit(1);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (existsSync(path.join(projectRoot, "cms", "forms", `${slug}.ts`))) {
|
|
67
|
+
console.error(pc.red(`\n❌ Error: Form "${slug}.ts" already exists at cms/forms/\n`));
|
|
68
|
+
process.exit(1);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Parse --fields=name:type,name2:type2 (type defaults to "text" when omitted)
|
|
72
|
+
const VALID_TYPES = ["text", "email", "tel", "number", "textarea", "select", "checkbox"];
|
|
73
|
+
let fieldsList = [];
|
|
74
|
+
const fieldsArg = args.find(a => /^-+fields=/.test(a));
|
|
75
|
+
const fieldsRaw = fieldsArg
|
|
76
|
+
? fieldsArg.split("=")[1] || ""
|
|
77
|
+
: (() => {
|
|
78
|
+
const i = args.findIndex(a => a === "--fields");
|
|
79
|
+
return i !== -1 && args[i + 1] ? args[i + 1] : "";
|
|
80
|
+
})();
|
|
81
|
+
|
|
82
|
+
if (fieldsRaw) {
|
|
83
|
+
fieldsList = fieldsRaw.split(",").map(f => f.trim()).filter(Boolean).map(f => {
|
|
84
|
+
const [fieldName, fieldType] = f.split(":").map(s => s.trim());
|
|
85
|
+
const type = VALID_TYPES.includes(fieldType) ? fieldType : "text";
|
|
86
|
+
return { name: fieldName, type };
|
|
87
|
+
});
|
|
88
|
+
} else {
|
|
89
|
+
fieldsList = [
|
|
90
|
+
{ name: "name", type: "text" },
|
|
91
|
+
{ name: "email", type: "email" },
|
|
92
|
+
{ name: "message", type: "textarea" },
|
|
93
|
+
];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Parse --mailTo=admin (or a literal email / option:key — see WpFormConfig.mailTo)
|
|
97
|
+
let mailTo = "admin";
|
|
98
|
+
const mailToArg = args.find(a => a.startsWith("--mailTo="));
|
|
99
|
+
if (mailToArg) {
|
|
100
|
+
mailTo = mailToArg.split("=")[1] || "admin";
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const projectRootForConfig = process.cwd();
|
|
104
|
+
|
|
105
|
+
let frameworkAdapter = "react";
|
|
106
|
+
try {
|
|
107
|
+
const config = await loadConfig(projectRootForConfig);
|
|
108
|
+
frameworkAdapter = config.frameworkAdapter || "react";
|
|
109
|
+
} catch (err) {
|
|
110
|
+
// Use default
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
try {
|
|
114
|
+
const adapterModule = await loadFrameworkAdapter(frameworkAdapter);
|
|
115
|
+
const adapter = adapterModule.default || adapterModule;
|
|
116
|
+
if (typeof adapter.onMakeForm === "function") {
|
|
117
|
+
await adapter.onMakeForm(projectRoot, {
|
|
118
|
+
slug,
|
|
119
|
+
pascalCase,
|
|
120
|
+
fields: fieldsList,
|
|
121
|
+
mailTo,
|
|
122
|
+
pc,
|
|
123
|
+
});
|
|
124
|
+
} else {
|
|
125
|
+
console.error(pc.red(`\n❌ Error: Framework adapter "${frameworkAdapter}" does not support making forms.`));
|
|
126
|
+
process.exit(1);
|
|
127
|
+
}
|
|
128
|
+
} catch (err) {
|
|
129
|
+
console.error(pc.red(`\n❌ Failed to execute make:form: ${err.message}`));
|
|
130
|
+
process.exit(1);
|
|
131
|
+
}
|
package/bin/make-loop.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
import { existsSync,
|
|
3
|
+
import { existsSync, writeFileSync } from "node:fs";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import readline from "node:readline/promises";
|
|
6
6
|
import { stdin as input, stdout as output } from "node:process";
|
|
7
7
|
import pc from "picocolors";
|
|
8
8
|
import { loadConfig } from "../lib/load-config.js";
|
|
9
9
|
import { loadFrameworkAdapter } from "../lib/framework-adapter.js";
|
|
10
|
+
import { loadMockData } from "../lib/functions/seed-mock-data.js";
|
|
10
11
|
|
|
11
12
|
const args = process.argv.slice(2);
|
|
12
13
|
let rawLoopName = args.find(a => !a.startsWith("-"));
|
|
@@ -62,59 +63,55 @@ if (postTypeArg) {
|
|
|
62
63
|
}
|
|
63
64
|
|
|
64
65
|
const projectRoot = process.cwd();
|
|
65
|
-
const
|
|
66
|
+
const mockDataTsPath = path.join(projectRoot, "cms", "mock-data.ts");
|
|
67
|
+
const mockDataJsonPath = path.join(projectRoot, "cms", "mock-data.json");
|
|
66
68
|
|
|
67
69
|
if (!existsSync(path.join(projectRoot, "src"))) {
|
|
68
70
|
console.error(pc.red(`\n❌ Error: "src" folder not found. Are you in your theme's root directory?\n`));
|
|
69
71
|
process.exit(1);
|
|
70
72
|
}
|
|
71
73
|
|
|
72
|
-
//
|
|
74
|
+
// Load custom fields from cms/mock-data.ts (preferred) or mock-data.json
|
|
73
75
|
let customFields = [];
|
|
74
76
|
let automaticallySeeded = false;
|
|
77
|
+
const mockData = { ...loadMockData(projectRoot) };
|
|
78
|
+
const existingRecords = mockData[postType] || [];
|
|
75
79
|
|
|
76
|
-
if (
|
|
80
|
+
if (existingRecords.length > 0 && existingRecords[0].customFields) {
|
|
81
|
+
customFields = Object.keys(existingRecords[0].customFields);
|
|
82
|
+
} else if (!mockData[postType] && existsSync(mockDataJsonPath) && !existsSync(mockDataTsPath)) {
|
|
77
83
|
try {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
{
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
customFields: {
|
|
91
|
-
client_name: "Mock Enterprise",
|
|
92
|
-
project_budget: "$30,000",
|
|
93
|
-
}
|
|
94
|
-
},
|
|
95
|
-
{
|
|
96
|
-
id: 2,
|
|
97
|
-
title: `Sample ${postType.charAt(0).toUpperCase() + postType.slice(1)} Item 2`,
|
|
98
|
-
excerpt: `This is another custom ${postType} post seeded dynamically via ForgeWP CLI.`,
|
|
99
|
-
content: `<p>This is the second custom post for the ${postType} post type.</p>`,
|
|
100
|
-
date: new Date().toLocaleDateString("en-US", { year: "numeric", month: "long", day: "numeric" }),
|
|
101
|
-
author: "ForgeWP CLI",
|
|
102
|
-
featuredImage: `https://picsum.photos/seed/${postType}2/1200/630`,
|
|
103
|
-
customFields: {
|
|
104
|
-
client_name: "Mock Organization",
|
|
105
|
-
project_budget: "$55,000",
|
|
106
|
-
}
|
|
84
|
+
mockData[postType] = [
|
|
85
|
+
{
|
|
86
|
+
id: 1,
|
|
87
|
+
title: `Sample ${postType.charAt(0).toUpperCase() + postType.slice(1)} Item 1`,
|
|
88
|
+
excerpt: `This is a custom ${postType} post seeded dynamically via ForgeWP CLI.`,
|
|
89
|
+
content: `<p>Welcome to your new custom <strong>${postType}</strong> post loop! Edit this in cms/mock-data.json.</p>`,
|
|
90
|
+
date: new Date().toLocaleDateString("en-US", { year: "numeric", month: "long", day: "numeric" }),
|
|
91
|
+
author: "ForgeWP CLI",
|
|
92
|
+
featuredImage: `https://picsum.photos/seed/${postType}1/1200/630`,
|
|
93
|
+
customFields: {
|
|
94
|
+
client_name: "Mock Enterprise",
|
|
95
|
+
project_budget: "$30,000",
|
|
107
96
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
id: 2,
|
|
100
|
+
title: `Sample ${postType.charAt(0).toUpperCase() + postType.slice(1)} Item 2`,
|
|
101
|
+
excerpt: `This is another custom ${postType} post seeded dynamically via ForgeWP CLI.`,
|
|
102
|
+
content: `<p>This is the second custom post for the ${postType} post type.</p>`,
|
|
103
|
+
date: new Date().toLocaleDateString("en-US", { year: "numeric", month: "long", day: "numeric" }),
|
|
104
|
+
author: "ForgeWP CLI",
|
|
105
|
+
featuredImage: `https://picsum.photos/seed/${postType}2/1200/630`,
|
|
106
|
+
customFields: {
|
|
107
|
+
client_name: "Mock Organization",
|
|
108
|
+
project_budget: "$55,000",
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
];
|
|
112
|
+
writeFileSync(mockDataJsonPath, JSON.stringify(mockData, null, 2), "utf8");
|
|
113
|
+
automaticallySeeded = true;
|
|
114
|
+
customFields = Object.keys(mockData[postType][0].customFields);
|
|
118
115
|
} catch (err) {
|
|
119
116
|
// Ignore and fallback
|
|
120
117
|
}
|
package/bin/make-mail.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
|
|
5
|
+
const args = process.argv.slice(2);
|
|
6
|
+
if (args.includes('--help') || args.includes('-h')) {
|
|
7
|
+
console.log('Usage: forgewp make:mail <name>\nCreates cms/mail/<name>.json with plain-text subject and body.');
|
|
8
|
+
} else {
|
|
9
|
+
const name = args[0];
|
|
10
|
+
if (!name || !/^[a-z][a-z0-9-]*$/.test(name) || args.length !== 1) {
|
|
11
|
+
console.error('Use a lowercase template name, e.g. forgewp make:mail contact-receipt');
|
|
12
|
+
process.exitCode = 1;
|
|
13
|
+
} else if (!fs.existsSync(path.resolve('wp.config.ts'))) {
|
|
14
|
+
console.error('Run make:mail from a theme containing wp.config.ts.');
|
|
15
|
+
process.exitCode = 1;
|
|
16
|
+
} else {
|
|
17
|
+
const file = path.resolve('cms/mail', `${name}.json`);
|
|
18
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
19
|
+
try {
|
|
20
|
+
fs.writeFileSync(file, JSON.stringify({ subject: 'New form submission', body: 'A new submission has been received.' }, null, 2) + '\n', { flag: 'wx' });
|
|
21
|
+
console.log(`Created cms/mail/${name}.json. Reference notificationTemplate: '${name}' or confirmation.template: '${name}'.`);
|
|
22
|
+
} catch (error) {
|
|
23
|
+
console.error(`Cannot create template: ${error.message}`);
|
|
24
|
+
process.exitCode = 1;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|