@forgewp/compiler 0.4.2 → 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 +70 -3
- package/bin/add.js +30 -15
- package/bin/analyze.js +66 -1
- package/bin/cli.js +149 -70
- package/bin/doctor.js +21 -12
- package/bin/export.js +69 -20
- package/bin/fresh.js +6 -1
- package/bin/i18n-extract.js +3 -10
- package/bin/i18n-translate.js +15 -24
- package/bin/make-block.js +24 -0
- package/bin/make-editable.js +166 -0
- package/bin/make-form.js +131 -0
- package/bin/make-island.js +11 -0
- package/bin/make-loop.js +147 -0
- package/bin/make-mail.js +27 -0
- package/bin/make-page.js +83 -0
- package/bin/make-post-type.js +68 -29
- package/bin/make-sandbox.js +609 -0
- 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 +13 -3
- package/lib/adapters/stub-adapter.js +10 -2
- package/lib/add-component.js +153 -92
- package/lib/asset-loader.js +333 -1
- 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 +77 -5
- 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 +2076 -303
- 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 +324 -8
- package/lib/index.js +3 -2
- package/lib/load-config.js +338 -144
- package/lib/mail-templates.js +37 -0
- package/lib/markup-processor.js +483 -92
- package/lib/node-runtime.js +7 -0
- package/lib/page-config-plugin.js +84 -0
- package/lib/php-builders.js +383 -42
- package/lib/react-adapter.js +1053 -246
- package/lib/registration-metadata.js +11 -0
- package/lib/render-theme.mts +1410 -543
- 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 +51 -2
- package/lib/validate.js +27 -5
- package/lib/virtual-plugin.js +319 -0
- package/lib/walkthrough.md +26 -0
- package/package.json +17 -5
- package/templates/PresetsStyle.tsx +1 -1
- 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/dashboard-view-wrapper.tsx +168 -0
- package/templates/auth/components/forgot-password-form-wrapper.tsx +141 -0
- package/templates/auth/components/login-form-wrapper.tsx +300 -0
- package/templates/auth/components/reset-password-form-wrapper.tsx +190 -0
- package/templates/auth/components/signup-form-wrapper.tsx +257 -0
- package/templates/auth/components/verify-email-view-wrapper.tsx +265 -0
- package/templates/auth/pages/DashboardPage.tsx +12 -0
- package/templates/auth/pages/ForgotPasswordPage.tsx +11 -0
- package/templates/auth/pages/LoginPage.tsx +11 -0
- package/templates/auth/pages/ResetPasswordPage.tsx +11 -0
- package/templates/auth/pages/SignUpPage.tsx +11 -0
- package/templates/auth/pages/VerifyEmailPage.tsx +11 -0
- 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/forgewp-config.ts +70 -3
- package/templates/menus.json +12 -10
- package/templates/mock-data.json +122 -433
- package/templates/products.json +5 -0
- package/templates/roles.json +7 -0
- package/templates/sessions.json +1 -0
- package/templates/users.json +38 -0
- package/templates/wordpress.tsx +1835 -1297
- package/bin/make-template.js +0 -145
- package/lib/block-compiler.js +0 -552
- package/lib/functions-builder.js +0 -2044
- package/lib/hydration-scanner.js +0 -821
package/bin/make-loop.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { existsSync, 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
|
+
import { loadConfig } from "../lib/load-config.js";
|
|
9
|
+
import { loadFrameworkAdapter } from "../lib/framework-adapter.js";
|
|
10
|
+
import { loadMockData } from "../lib/functions/seed-mock-data.js";
|
|
11
|
+
|
|
12
|
+
const args = process.argv.slice(2);
|
|
13
|
+
let rawLoopName = args.find(a => !a.startsWith("-"));
|
|
14
|
+
|
|
15
|
+
// Support --name PortfolioGrid or --name=PortfolioGrid
|
|
16
|
+
const nameIndex = args.indexOf("--name");
|
|
17
|
+
if (nameIndex !== -1 && args[nameIndex + 1]) {
|
|
18
|
+
rawLoopName = args[nameIndex + 1];
|
|
19
|
+
} else {
|
|
20
|
+
const nameEqual = args.find(a => a.startsWith("--name="));
|
|
21
|
+
if (nameEqual) {
|
|
22
|
+
rawLoopName = nameEqual.split("=")[1];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (!rawLoopName) {
|
|
27
|
+
const rl = readline.createInterface({ input, output });
|
|
28
|
+
try {
|
|
29
|
+
const answer = await rl.question(pc.cyan("? Enter a loop name: "));
|
|
30
|
+
rawLoopName = answer.trim();
|
|
31
|
+
} catch (err) {}
|
|
32
|
+
rl.close();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (!rawLoopName) {
|
|
36
|
+
console.error(pc.red("\n❌ Error: Please specify a loop name."));
|
|
37
|
+
console.log(pc.cyan(" Example: pnpm forgewp make:loop PortfolioGrid --postType=portfolio"));
|
|
38
|
+
console.log(pc.cyan(" Or: pnpm forgewp make:loop --name PortfolioGrid --postType=portfolio\n"));
|
|
39
|
+
process.exit(1);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// PascalCase formatting
|
|
43
|
+
const cleanName = rawLoopName.replace(/[^a-zA-Z0-9_-]/g, "");
|
|
44
|
+
const pascalCase = cleanName
|
|
45
|
+
.replace(/(?:^\w|[A-Z]|\b\w)/g, (word) => word.toUpperCase())
|
|
46
|
+
.replace(/[^a-zA-Z0-9]/g, "");
|
|
47
|
+
|
|
48
|
+
if (!pascalCase) {
|
|
49
|
+
console.error(pc.red("\n❌ Error: Invalid loop name.\n"));
|
|
50
|
+
process.exit(1);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Parse postType
|
|
54
|
+
let postType = "post";
|
|
55
|
+
const postTypeArg = args.find(a => a.startsWith("--postType=") || a.startsWith("--post-type="));
|
|
56
|
+
if (postTypeArg) {
|
|
57
|
+
postType = postTypeArg.split("=")[1];
|
|
58
|
+
} else {
|
|
59
|
+
const postTypeIndex = args.findIndex(a => a === "--postType" || a === "--post-type");
|
|
60
|
+
if (postTypeIndex !== -1 && args[postTypeIndex + 1]) {
|
|
61
|
+
postType = args[postTypeIndex + 1];
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const projectRoot = process.cwd();
|
|
66
|
+
const mockDataTsPath = path.join(projectRoot, "cms", "mock-data.ts");
|
|
67
|
+
const mockDataJsonPath = path.join(projectRoot, "cms", "mock-data.json");
|
|
68
|
+
|
|
69
|
+
if (!existsSync(path.join(projectRoot, "src"))) {
|
|
70
|
+
console.error(pc.red(`\n❌ Error: "src" folder not found. Are you in your theme's root directory?\n`));
|
|
71
|
+
process.exit(1);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Load custom fields from cms/mock-data.ts (preferred) or mock-data.json
|
|
75
|
+
let customFields = [];
|
|
76
|
+
let automaticallySeeded = false;
|
|
77
|
+
const mockData = { ...loadMockData(projectRoot) };
|
|
78
|
+
const existingRecords = mockData[postType] || [];
|
|
79
|
+
|
|
80
|
+
if (existingRecords.length > 0 && existingRecords[0].customFields) {
|
|
81
|
+
customFields = Object.keys(existingRecords[0].customFields);
|
|
82
|
+
} else if (!mockData[postType] && existsSync(mockDataJsonPath) && !existsSync(mockDataTsPath)) {
|
|
83
|
+
try {
|
|
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",
|
|
96
|
+
}
|
|
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);
|
|
115
|
+
} catch (err) {
|
|
116
|
+
// Ignore and fallback
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Load configuration and invoke adapter
|
|
121
|
+
let frameworkAdapter = "react";
|
|
122
|
+
try {
|
|
123
|
+
const config = await loadConfig(projectRoot);
|
|
124
|
+
frameworkAdapter = config.frameworkAdapter || "react";
|
|
125
|
+
} catch (err) {
|
|
126
|
+
// Use default
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
try {
|
|
130
|
+
const adapterModule = await loadFrameworkAdapter(frameworkAdapter);
|
|
131
|
+
const adapter = adapterModule.default || adapterModule;
|
|
132
|
+
if (typeof adapter.onMakeLoop === "function") {
|
|
133
|
+
await adapter.onMakeLoop(projectRoot, {
|
|
134
|
+
pascalCase,
|
|
135
|
+
postType,
|
|
136
|
+
customFields,
|
|
137
|
+
automaticallySeeded,
|
|
138
|
+
pc
|
|
139
|
+
});
|
|
140
|
+
} else {
|
|
141
|
+
console.error(pc.red(`\n❌ Error: Framework adapter "${frameworkAdapter}" does not support making custom loops.`));
|
|
142
|
+
process.exit(1);
|
|
143
|
+
}
|
|
144
|
+
} catch (err) {
|
|
145
|
+
console.error(pc.red(`\n❌ Failed to execute make:loop: ${err.message}`));
|
|
146
|
+
process.exit(1);
|
|
147
|
+
}
|
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
|
+
}
|
package/bin/make-page.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
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 rawPageName = args.find(a => !a.startsWith("-"));
|
|
13
|
+
|
|
14
|
+
// Support --name AboutUs or --name=AboutUs
|
|
15
|
+
const nameIndex = args.indexOf("--name");
|
|
16
|
+
if (nameIndex !== -1 && args[nameIndex + 1]) {
|
|
17
|
+
rawPageName = args[nameIndex + 1];
|
|
18
|
+
} else {
|
|
19
|
+
const nameEqual = args.find(a => a.startsWith("--name="));
|
|
20
|
+
if (nameEqual) {
|
|
21
|
+
rawPageName = nameEqual.split("=")[1];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (!rawPageName) {
|
|
26
|
+
const rl = readline.createInterface({ input, output });
|
|
27
|
+
try {
|
|
28
|
+
const answer = await rl.question(pc.cyan("? Enter a page name: "));
|
|
29
|
+
rawPageName = answer.trim();
|
|
30
|
+
} catch (err) {}
|
|
31
|
+
rl.close();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (!rawPageName) {
|
|
35
|
+
console.error(pc.red("\n❌ Error: Please specify a page name."));
|
|
36
|
+
console.log(pc.cyan(" Example: pnpm forgewp make:page AboutUs"));
|
|
37
|
+
console.log(pc.cyan(" Or: pnpm forgewp make:page --name AboutUs\n"));
|
|
38
|
+
process.exit(1);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// PascalCase formatting
|
|
42
|
+
const cleanName = rawPageName.replace(/[^a-zA-Z0-9_-]/g, "");
|
|
43
|
+
const pascalCase = cleanName
|
|
44
|
+
.replace(/(?:^\w|[A-Z]|\b\w)/g, (word) => word.toUpperCase())
|
|
45
|
+
.replace(/[^a-zA-Z0-9]/g, "");
|
|
46
|
+
|
|
47
|
+
if (!pascalCase) {
|
|
48
|
+
console.error(pc.red("\n❌ Error: Invalid page name.\n"));
|
|
49
|
+
process.exit(1);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const projectRoot = process.cwd();
|
|
53
|
+
|
|
54
|
+
if (!existsSync(path.join(projectRoot, "src"))) {
|
|
55
|
+
console.error(pc.red(`\n❌ Error: "src" folder not found. Are you in your theme's root directory?\n`));
|
|
56
|
+
process.exit(1);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Load configuration and invoke adapter
|
|
60
|
+
let frameworkAdapter = "react";
|
|
61
|
+
try {
|
|
62
|
+
const config = await loadConfig(projectRoot);
|
|
63
|
+
frameworkAdapter = config.frameworkAdapter || "react";
|
|
64
|
+
} catch (err) {
|
|
65
|
+
// Use default
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
const adapterModule = await loadFrameworkAdapter(frameworkAdapter);
|
|
70
|
+
const adapter = adapterModule.default || adapterModule;
|
|
71
|
+
if (typeof adapter.onMakePage === "function") {
|
|
72
|
+
await adapter.onMakePage(projectRoot, {
|
|
73
|
+
pascalCase,
|
|
74
|
+
pc
|
|
75
|
+
});
|
|
76
|
+
} else {
|
|
77
|
+
console.error(pc.red(`\n❌ Error: Framework adapter "${frameworkAdapter}" does not support making custom page templates.`));
|
|
78
|
+
process.exit(1);
|
|
79
|
+
}
|
|
80
|
+
} catch (err) {
|
|
81
|
+
console.error(pc.red(`\n❌ Failed to execute make:page: ${err.message}`));
|
|
82
|
+
process.exit(1);
|
|
83
|
+
}
|
package/bin/make-post-type.js
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
import { existsSync, mkdirSync, writeFileSync, readFileSync } from "node:fs";
|
|
4
4
|
import path from "node:path";
|
|
5
|
+
import readline from "node:readline/promises";
|
|
6
|
+
import { stdin as input, stdout as output } from "node:process";
|
|
5
7
|
import pc from "picocolors";
|
|
6
8
|
|
|
7
9
|
const args = process.argv.slice(2);
|
|
@@ -18,6 +20,15 @@ if (nameIndex !== -1 && args[nameIndex + 1]) {
|
|
|
18
20
|
}
|
|
19
21
|
}
|
|
20
22
|
|
|
23
|
+
if (!rawName) {
|
|
24
|
+
const rl = readline.createInterface({ input, output });
|
|
25
|
+
try {
|
|
26
|
+
const answer = await rl.question(pc.cyan("? Enter a post type name: "));
|
|
27
|
+
rawName = answer.trim();
|
|
28
|
+
} catch (err) {}
|
|
29
|
+
rl.close();
|
|
30
|
+
}
|
|
31
|
+
|
|
21
32
|
if (!rawName) {
|
|
22
33
|
console.error(pc.red("\n❌ Error: Please specify a post type name."));
|
|
23
34
|
console.log(pc.cyan(" Example: pnpm forgewp make:post-type portfolio"));
|
|
@@ -34,30 +45,16 @@ if (!cleanName) {
|
|
|
34
45
|
|
|
35
46
|
const projectRoot = process.cwd();
|
|
36
47
|
const wpDir = path.join(projectRoot, "cms");
|
|
37
|
-
const
|
|
48
|
+
const mockDataTsPath = path.join(wpDir, "mock-data.ts");
|
|
49
|
+
const mockDataJsonPath = path.join(wpDir, "mock-data.json");
|
|
50
|
+
const isTs = existsSync(mockDataTsPath) || !existsSync(mockDataJsonPath);
|
|
51
|
+
const activeFile = isTs ? "cms/mock-data.ts" : "cms/mock-data.json";
|
|
38
52
|
|
|
39
53
|
// Ensure cms directory exists
|
|
40
54
|
if (!existsSync(wpDir)) {
|
|
41
55
|
mkdirSync(wpDir, { recursive: true });
|
|
42
56
|
}
|
|
43
57
|
|
|
44
|
-
let mockData = {};
|
|
45
|
-
|
|
46
|
-
if (existsSync(mockDataPath)) {
|
|
47
|
-
try {
|
|
48
|
-
mockData = JSON.parse(readFileSync(mockDataPath, "utf8"));
|
|
49
|
-
} catch (err) {
|
|
50
|
-
console.error(pc.red(`\n❌ Error: Failed to parse cms/mock-data.json. Enforcing clean file.`));
|
|
51
|
-
mockData = {};
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (mockData[cleanName]) {
|
|
56
|
-
console.warn(pc.yellow(`\n⚠️ Post type "${cleanName}" already exists in cms/mock-data.json.`));
|
|
57
|
-
console.log(` You can open the file directly to view or edit existing fields.\n`);
|
|
58
|
-
process.exit(0);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
58
|
// Parse custom fields if provided via --customFields, -customFields, --custom-fields, etc.
|
|
62
59
|
let customFieldsList = [];
|
|
63
60
|
const fieldsArg = args.find(a => /^-+custom[Ff]ields=/.test(a) || /^-+custom-fields=/.test(a));
|
|
@@ -83,7 +80,6 @@ if (customFieldsList.length > 0) {
|
|
|
83
80
|
seededCustomFields2[field] = `[Seeded ${field} 2]`;
|
|
84
81
|
}
|
|
85
82
|
} else {
|
|
86
|
-
// Default fallbacks if no flag is provided
|
|
87
83
|
seededCustomFields1["client_name"] = "Mock Enterprise";
|
|
88
84
|
seededCustomFields1["project_budget"] = "$30,000";
|
|
89
85
|
|
|
@@ -91,37 +87,80 @@ if (customFieldsList.length > 0) {
|
|
|
91
87
|
seededCustomFields2["project_budget"] = "$55,000";
|
|
92
88
|
}
|
|
93
89
|
|
|
94
|
-
|
|
95
|
-
|
|
90
|
+
const cleanTitle = cleanName.charAt(0).toUpperCase() + cleanName.slice(1);
|
|
91
|
+
const dateStr = new Date().toLocaleDateString("en-US", { year: "numeric", month: "long", day: "numeric" });
|
|
92
|
+
|
|
93
|
+
const seedItems = [
|
|
96
94
|
{
|
|
97
95
|
id: 1,
|
|
98
|
-
title: `Sample ${
|
|
96
|
+
title: `Sample ${cleanTitle} Item 1`,
|
|
99
97
|
excerpt: `This is a custom ${cleanName} post seeded dynamically via ForgeWP CLI.`,
|
|
100
|
-
content: `<p>Welcome to your new custom <strong>${cleanName}</strong> post template! Edit this in
|
|
101
|
-
date:
|
|
98
|
+
content: `<p>Welcome to your new custom <strong>${cleanName}</strong> post template! Edit this in ${activeFile}.</p>`,
|
|
99
|
+
date: dateStr,
|
|
102
100
|
author: "ForgeWP CLI",
|
|
103
101
|
featuredImage: `https://picsum.photos/seed/${cleanName}1/1200/630`,
|
|
104
102
|
customFields: seededCustomFields1
|
|
105
103
|
},
|
|
106
104
|
{
|
|
107
105
|
id: 2,
|
|
108
|
-
title: `Sample ${
|
|
106
|
+
title: `Sample ${cleanTitle} Item 2`,
|
|
109
107
|
excerpt: `This is another custom ${cleanName} post seeded dynamically via ForgeWP CLI.`,
|
|
110
108
|
content: `<p>This is the second custom post for the ${cleanName} post type.</p>`,
|
|
111
|
-
date:
|
|
109
|
+
date: dateStr,
|
|
112
110
|
author: "ForgeWP CLI",
|
|
113
111
|
featuredImage: `https://picsum.photos/seed/${cleanName}2/1200/630`,
|
|
114
112
|
customFields: seededCustomFields2
|
|
115
113
|
}
|
|
116
114
|
];
|
|
117
115
|
|
|
118
|
-
|
|
119
|
-
|
|
116
|
+
if (isTs) {
|
|
117
|
+
if (existsSync(mockDataTsPath)) {
|
|
118
|
+
let content = readFileSync(mockDataTsPath, "utf8");
|
|
119
|
+
if (content.includes(`"${cleanName}":`) || content.includes(`${cleanName}:`)) {
|
|
120
|
+
console.warn(pc.yellow(`\n⚠️ Post type "${cleanName}" already exists in cms/mock-data.ts.`));
|
|
121
|
+
console.log(` You can open the file directly to view or edit existing fields.\n`);
|
|
122
|
+
process.exit(0);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const snippet = ` ${cleanName}: ${JSON.stringify(seedItems, null, 4).replace(/^/gm, " ").trim()},\n`;
|
|
126
|
+
if (content.includes("defineWpPosts({")) {
|
|
127
|
+
content = content.replace("defineWpPosts({", "defineWpPosts({\n" + snippet);
|
|
128
|
+
} else if (content.lastIndexOf("};") !== -1) {
|
|
129
|
+
const idx = content.lastIndexOf("};");
|
|
130
|
+
content = content.slice(0, idx) + snippet + content.slice(idx);
|
|
131
|
+
} else {
|
|
132
|
+
content += `\n\n// Added custom post type: ${cleanName}\n${snippet}`;
|
|
133
|
+
}
|
|
134
|
+
writeFileSync(mockDataTsPath, content, "utf8");
|
|
135
|
+
} else {
|
|
136
|
+
const defaultTs = `import { defineWpPosts } from '@forgewp/react';\n\nexport const mockData = defineWpPosts({\n post: [],\n ${cleanName}: ${JSON.stringify(seedItems, null, 4).replace(/^/gm, " ").trim()}\n});\n\nexport default mockData;\n`;
|
|
137
|
+
writeFileSync(mockDataTsPath, defaultTs, "utf8");
|
|
138
|
+
}
|
|
139
|
+
} else {
|
|
140
|
+
let mockData = {};
|
|
141
|
+
if (existsSync(mockDataJsonPath)) {
|
|
142
|
+
try {
|
|
143
|
+
mockData = JSON.parse(readFileSync(mockDataJsonPath, "utf8"));
|
|
144
|
+
} catch (err) {
|
|
145
|
+
console.error(pc.red(`\n❌ Error: Failed to parse cms/mock-data.json. Enforcing clean file.`));
|
|
146
|
+
mockData = {};
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (mockData[cleanName]) {
|
|
151
|
+
console.warn(pc.yellow(`\n⚠️ Post type "${cleanName}" already exists in cms/mock-data.json.`));
|
|
152
|
+
console.log(` You can open the file directly to view or edit existing fields.\n`);
|
|
153
|
+
process.exit(0);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
mockData[cleanName] = seedItems;
|
|
157
|
+
writeFileSync(mockDataJsonPath, JSON.stringify(mockData, null, 2), "utf8");
|
|
158
|
+
}
|
|
120
159
|
|
|
121
160
|
const suggestedField = customFieldsList.length > 0 ? customFieldsList[0] : "client_name";
|
|
122
161
|
|
|
123
162
|
console.log(pc.green(`\n⚡ Post Type "${cleanName}" successfully registered!`));
|
|
124
|
-
console.log(` Local DB: ${pc.cyan(
|
|
163
|
+
console.log(` Local DB: ${pc.cyan(activeFile)}`);
|
|
125
164
|
console.log(`\n🎉 Query your seeded fields in React inside:`);
|
|
126
165
|
console.log(` ${pc.yellow(`<WpQueryLoop postType="${cleanName}" postsPerPage={2}>`)}`);
|
|
127
166
|
console.log(` ${pc.yellow(`<h3>{useWpTitle()}</h3>`)}`);
|