dynamic-zone 1.0.0
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 +1 -0
- package/index.js +95 -0
- package/package.json +15 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Dynamic-zone package
|
package/index.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
|
|
6
|
+
const basePath = process.cwd();
|
|
7
|
+
|
|
8
|
+
// Paths
|
|
9
|
+
const cmsPath = path.join(basePath, "cms");
|
|
10
|
+
const componentsPath = path.join(cmsPath, "components");
|
|
11
|
+
const dynamicZonePath = path.join(cmsPath, "dynamic-zone");
|
|
12
|
+
|
|
13
|
+
// Create folders
|
|
14
|
+
[cmsPath, componentsPath, dynamicZonePath].forEach((dir) => {
|
|
15
|
+
if (!fs.existsSync(dir)) {
|
|
16
|
+
fs.mkdirSync(dir);
|
|
17
|
+
console.log("✅ Created:", dir);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
// File content
|
|
22
|
+
const fileContent = `import dynamic from "next/dynamic";
|
|
23
|
+
import { Suspense } from "react";
|
|
24
|
+
|
|
25
|
+
type SectionContentType = {
|
|
26
|
+
sections: { __component: string }[];
|
|
27
|
+
};
|
|
28
|
+
type SectionType = NonNullable<SectionContentType["sections"][number]>;
|
|
29
|
+
|
|
30
|
+
interface Props {
|
|
31
|
+
sections?: unknown;
|
|
32
|
+
slug?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
type ComponentMapType = React.ComponentType<{
|
|
36
|
+
index?: number;
|
|
37
|
+
content?: unknown;
|
|
38
|
+
}>;
|
|
39
|
+
|
|
40
|
+
const dynamicImport = (path: string) =>
|
|
41
|
+
dynamic(() => import(\`../components/\${path}\`), {
|
|
42
|
+
loading: () => <div>Loading...</div>,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const componentMap: { [key: string]: ComponentMapType } = {};
|
|
46
|
+
|
|
47
|
+
export default function SectionRenderer({ sections, slug }: Props) {
|
|
48
|
+
if (!sections) return null;
|
|
49
|
+
|
|
50
|
+
if (Array.isArray(sections) && sections.length === 0) {
|
|
51
|
+
return (
|
|
52
|
+
<div className="w-full px-2 md:px-4 py-4">
|
|
53
|
+
No Sections Found for {slug}
|
|
54
|
+
</div>
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const renderSection = (section: SectionType, index: number) => {
|
|
59
|
+
const sectionType = section.__component?.split(".")[1]?.replace(/-/g, "_");
|
|
60
|
+
const Component = componentMap[sectionType];
|
|
61
|
+
|
|
62
|
+
if (!Component) {
|
|
63
|
+
return (
|
|
64
|
+
<div className="w-full" key={index}>
|
|
65
|
+
No Section Found {sectionType}
|
|
66
|
+
</div>
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return (
|
|
71
|
+
<div key={index}>
|
|
72
|
+
<Suspense fallback={<div>Loading...</div>}>
|
|
73
|
+
<Component index={index} content={section} />
|
|
74
|
+
</Suspense>
|
|
75
|
+
</div>
|
|
76
|
+
);
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
return (
|
|
80
|
+
<div className="flex flex-col">
|
|
81
|
+
{(sections || []).map(renderSection)}
|
|
82
|
+
</div>
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
`;
|
|
86
|
+
|
|
87
|
+
// Create file
|
|
88
|
+
const filePath = path.join(dynamicZonePath, "section-renderer.tsx");
|
|
89
|
+
|
|
90
|
+
if (!fs.existsSync(filePath)) {
|
|
91
|
+
fs.writeFileSync(filePath, fileContent);
|
|
92
|
+
console.log("📄 Created file:", filePath);
|
|
93
|
+
} else {
|
|
94
|
+
console.log("⚠️ File already exists:", filePath);
|
|
95
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "dynamic-zone",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "dynamic zone for cms platforms",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"dynamic-zone"
|
|
7
|
+
],
|
|
8
|
+
"license": "ISC",
|
|
9
|
+
"author": "Web Dev Simplified",
|
|
10
|
+
"type": "commonjs",
|
|
11
|
+
"main": "index.js",
|
|
12
|
+
"scripts": {
|
|
13
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
14
|
+
}
|
|
15
|
+
}
|