@slicemachine/adapter-next 0.3.85-beta.3 → 0.3.86-alpha.lg-page-routes.1
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/dist/hooks/page-route-create.cjs +396 -0
- package/dist/hooks/page-route-create.cjs.map +1 -0
- package/dist/hooks/page-route-create.d.ts +10 -0
- package/dist/hooks/page-route-create.js +396 -0
- package/dist/hooks/page-route-create.js.map +1 -0
- package/dist/plugin.cjs +8 -0
- package/dist/plugin.cjs.map +1 -1
- package/dist/plugin.js +9 -1
- package/dist/plugin.js.map +1 -1
- package/dist/sliceTemplates/AlternateGrid/index.cjs +4 -0
- package/dist/sliceTemplates/AlternateGrid/index.cjs.map +1 -1
- package/dist/sliceTemplates/AlternateGrid/index.js +4 -0
- package/dist/sliceTemplates/AlternateGrid/index.js.map +1 -1
- package/dist/sliceTemplates/CustomerLogos/index.cjs +5 -0
- package/dist/sliceTemplates/CustomerLogos/index.cjs.map +1 -1
- package/dist/sliceTemplates/CustomerLogos/index.js +5 -0
- package/dist/sliceTemplates/CustomerLogos/index.js.map +1 -1
- package/package.json +3 -3
- package/src/hooks/page-route-create.ts +472 -0
- package/src/plugin.ts +10 -0
- package/src/sliceTemplates/AlternateGrid/index.ts +4 -0
- package/src/sliceTemplates/CustomerLogos/index.ts +5 -0
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const commonTags = require("common-tags");
|
|
4
|
+
const fs = require("@slicemachine/plugin-kit/fs");
|
|
5
|
+
const checkIsTypeScriptProject = require("../lib/checkIsTypeScriptProject.cjs");
|
|
6
|
+
const getJSFileExtension = require("../lib/getJSFileExtension.cjs");
|
|
7
|
+
const checkHasAppRouter = require("../lib/checkHasAppRouter.cjs");
|
|
8
|
+
const path = require("node:path");
|
|
9
|
+
const createComponentFile = async ({ data, helpers, options }) => {
|
|
10
|
+
const isTypeScript = await checkIsTypeScriptProject.checkIsTypeScriptProject({
|
|
11
|
+
helpers,
|
|
12
|
+
options
|
|
13
|
+
});
|
|
14
|
+
const hasSrcDirectory = await fs.checkHasProjectFile({
|
|
15
|
+
filename: "src",
|
|
16
|
+
helpers
|
|
17
|
+
});
|
|
18
|
+
const hasAppRouter = await checkHasAppRouter.checkHasAppRouter({ helpers });
|
|
19
|
+
const pageRoute = data.model.route ?? data.model.id;
|
|
20
|
+
const pageRouteLength = pageRoute.split("/").length - 1;
|
|
21
|
+
const contents = generatePageComponent(data.model, {
|
|
22
|
+
hasAppRouter,
|
|
23
|
+
isTypeScript,
|
|
24
|
+
hasSrcDirectory,
|
|
25
|
+
pageRouteLength
|
|
26
|
+
});
|
|
27
|
+
const routeBase = hasAppRouter ? "app" : "pages";
|
|
28
|
+
const routeDirectoryPath = hasSrcDirectory ? path.join("src", routeBase) : routeBase;
|
|
29
|
+
const extension = await getJSFileExtension.getJSFileExtension({
|
|
30
|
+
helpers,
|
|
31
|
+
options,
|
|
32
|
+
jsx: true
|
|
33
|
+
});
|
|
34
|
+
let filePath;
|
|
35
|
+
if (hasAppRouter) {
|
|
36
|
+
const fileName = data.model.repeatable ? `[uid]/page.${extension}` : `page.${extension}`;
|
|
37
|
+
filePath = path.join(routeDirectoryPath, pageRoute, fileName);
|
|
38
|
+
} else {
|
|
39
|
+
const fileName = data.model.repeatable ? `${pageRoute}/[uid].${extension}` : `${pageRoute}.${extension}`;
|
|
40
|
+
filePath = path.join(routeDirectoryPath, fileName);
|
|
41
|
+
}
|
|
42
|
+
await fs.writeProjectFile({
|
|
43
|
+
filename: filePath,
|
|
44
|
+
contents,
|
|
45
|
+
format: options.format,
|
|
46
|
+
helpers
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
const pageRouteCreate = async (data, context) => {
|
|
50
|
+
await createComponentFile({ data, ...context });
|
|
51
|
+
};
|
|
52
|
+
function generatePageComponent(customType, env) {
|
|
53
|
+
if (env.hasAppRouter) {
|
|
54
|
+
return generateAppRouterPage(customType, env.isTypeScript, env.hasSrcDirectory, env.pageRouteLength);
|
|
55
|
+
} else {
|
|
56
|
+
return generatePagesRouterPage(customType, env.isTypeScript, env.hasSrcDirectory, env.pageRouteLength);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function generateAppRouterPage(customType, isTypeScript, hasSrcDirectory, pageRouteLength) {
|
|
60
|
+
const levelsUp = customType.repeatable ? pageRouteLength + 1 : pageRouteLength;
|
|
61
|
+
const levelsUpWithSrc = hasSrcDirectory ? levelsUp + 1 : levelsUp;
|
|
62
|
+
const importPath = "../".repeat(levelsUpWithSrc) + "prismicio";
|
|
63
|
+
const slicesPath = "../".repeat(levelsUpWithSrc) + "slices";
|
|
64
|
+
if (customType.repeatable) {
|
|
65
|
+
if (isTypeScript) {
|
|
66
|
+
return commonTags.stripIndent`
|
|
67
|
+
import { Metadata } from "next";
|
|
68
|
+
import { notFound } from "next/navigation";
|
|
69
|
+
import { asImageSrc } from "@prismicio/client";
|
|
70
|
+
import { SliceZone } from "@prismicio/react";
|
|
71
|
+
|
|
72
|
+
import { createClient } from "${importPath}";
|
|
73
|
+
import { components } from "${slicesPath}";
|
|
74
|
+
|
|
75
|
+
type Params = { uid: string };
|
|
76
|
+
|
|
77
|
+
export default async function Page({ params }: { params: Promise<Params> }) {
|
|
78
|
+
const { uid } = await params;
|
|
79
|
+
const client = createClient();
|
|
80
|
+
const page = await client
|
|
81
|
+
.getByUID("${customType.id}", uid)
|
|
82
|
+
.catch(() => notFound());
|
|
83
|
+
|
|
84
|
+
return <SliceZone slices={page.data.slices} components={components} />;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export async function generateMetadata({
|
|
88
|
+
params,
|
|
89
|
+
}: {
|
|
90
|
+
params: Promise<Params>;
|
|
91
|
+
}): Promise<Metadata> {
|
|
92
|
+
const { uid } = await params;
|
|
93
|
+
const client = createClient();
|
|
94
|
+
const page = await client
|
|
95
|
+
.getByUID("${customType.id}", uid)
|
|
96
|
+
.catch(() => notFound());
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
title: page.data.meta_title,
|
|
100
|
+
description: page.data.meta_description,
|
|
101
|
+
openGraph: {
|
|
102
|
+
images: [{ url: asImageSrc(page.data.meta_image) ?? "" }],
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export async function generateStaticParams() {
|
|
108
|
+
const client = createClient();
|
|
109
|
+
const pages = await client.getAllByType("${customType.id}");
|
|
110
|
+
|
|
111
|
+
return pages.map((page) => ({ uid: page.uid }));
|
|
112
|
+
}
|
|
113
|
+
`;
|
|
114
|
+
} else {
|
|
115
|
+
return commonTags.stripIndent`
|
|
116
|
+
import { notFound } from "next/navigation";
|
|
117
|
+
import { asImageSrc } from "@prismicio/client";
|
|
118
|
+
import { SliceZone } from "@prismicio/react";
|
|
119
|
+
|
|
120
|
+
import { createClient } from "${importPath}";
|
|
121
|
+
import { components } from "${slicesPath}";
|
|
122
|
+
|
|
123
|
+
export default async function Page({ params }) {
|
|
124
|
+
const { uid } = await params;
|
|
125
|
+
const client = createClient();
|
|
126
|
+
const page = await client
|
|
127
|
+
.getByUID("${customType.id}", uid)
|
|
128
|
+
.catch(() => notFound());
|
|
129
|
+
|
|
130
|
+
return <SliceZone slices={page.data.slices} components={components} />;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export async function generateMetadata({ params }) {
|
|
134
|
+
const { uid } = await params;
|
|
135
|
+
const client = createClient();
|
|
136
|
+
const page = await client
|
|
137
|
+
.getByUID("${customType.id}", uid)
|
|
138
|
+
.catch(() => notFound());
|
|
139
|
+
|
|
140
|
+
return {
|
|
141
|
+
title: page.data.meta_title,
|
|
142
|
+
description: page.data.meta_description,
|
|
143
|
+
openGraph: {
|
|
144
|
+
images: [{ url: asImageSrc(page.data.meta_image) ?? "" }],
|
|
145
|
+
},
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export async function generateStaticParams() {
|
|
150
|
+
const client = createClient();
|
|
151
|
+
const pages = await client.getAllByType("${customType.id}");
|
|
152
|
+
|
|
153
|
+
return pages.map((page) => ({ uid: page.uid }));
|
|
154
|
+
}
|
|
155
|
+
`;
|
|
156
|
+
}
|
|
157
|
+
} else {
|
|
158
|
+
if (isTypeScript) {
|
|
159
|
+
return commonTags.stripIndent`
|
|
160
|
+
import { type Metadata } from "next";
|
|
161
|
+
import { notFound } from "next/navigation";
|
|
162
|
+
import { asImageSrc } from "@prismicio/client";
|
|
163
|
+
import { SliceZone } from "@prismicio/react";
|
|
164
|
+
|
|
165
|
+
import { createClient } from "${importPath}";
|
|
166
|
+
import { components } from "${slicesPath}";
|
|
167
|
+
|
|
168
|
+
export default async function Page() {
|
|
169
|
+
const client = createClient();
|
|
170
|
+
const page = await client.getSingle("${customType.id}").catch(() => notFound());
|
|
171
|
+
|
|
172
|
+
return <SliceZone slices={page.data.slices} components={components} />;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export async function generateMetadata(): Promise<Metadata> {
|
|
176
|
+
const client = createClient();
|
|
177
|
+
const page = await client.getSingle("${customType.id}").catch(() => notFound());
|
|
178
|
+
|
|
179
|
+
return {
|
|
180
|
+
title: page.data.meta_title,
|
|
181
|
+
description: page.data.meta_description,
|
|
182
|
+
openGraph: {
|
|
183
|
+
images: [{ url: asImageSrc(page.data.meta_image) ?? "" }],
|
|
184
|
+
},
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
`;
|
|
188
|
+
} else {
|
|
189
|
+
return commonTags.stripIndent`
|
|
190
|
+
import { notFound } from "next/navigation";
|
|
191
|
+
import { asImageSrc } from "@prismicio/client";
|
|
192
|
+
import { SliceZone } from "@prismicio/react";
|
|
193
|
+
|
|
194
|
+
import { createClient } from "${importPath}";
|
|
195
|
+
import { components } from "${slicesPath}";
|
|
196
|
+
|
|
197
|
+
export default async function Page() {
|
|
198
|
+
const client = createClient();
|
|
199
|
+
const page = await client.getSingle("${customType.id}").catch(() => notFound());
|
|
200
|
+
|
|
201
|
+
return <SliceZone slices={page.data.slices} components={components} />;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export async function generateMetadata() {
|
|
205
|
+
const client = createClient();
|
|
206
|
+
const page = await client.getSingle("${customType.id}").catch(() => notFound());
|
|
207
|
+
|
|
208
|
+
return {
|
|
209
|
+
title: page.data.meta_title,
|
|
210
|
+
description: page.data.meta_description,
|
|
211
|
+
openGraph: {
|
|
212
|
+
images: [{ url: asImageSrc(page.data.meta_image) ?? "" }],
|
|
213
|
+
},
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
`;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
function generatePagesRouterPage(customType, isTypeScript, hasSrcDirectory, pageRouteLength) {
|
|
221
|
+
const levelsUp = customType.repeatable ? pageRouteLength + 1 : pageRouteLength;
|
|
222
|
+
const levelsUpWithSrc = hasSrcDirectory ? levelsUp + 1 : levelsUp;
|
|
223
|
+
const importPath = "../".repeat(levelsUpWithSrc) + "prismicio";
|
|
224
|
+
const slicesPath = "../".repeat(levelsUpWithSrc) + "slices";
|
|
225
|
+
if (customType.repeatable) {
|
|
226
|
+
if (isTypeScript) {
|
|
227
|
+
return commonTags.stripIndent`
|
|
228
|
+
import { GetStaticPropsContext, InferGetStaticPropsType } from "next";
|
|
229
|
+
import Head from "next/head";
|
|
230
|
+
import { isFilled, asLink, asImageSrc } from "@prismicio/client";
|
|
231
|
+
import { SliceZone } from "@prismicio/react";
|
|
232
|
+
|
|
233
|
+
import { components } from "${slicesPath}";
|
|
234
|
+
import { createClient } from "${importPath}";
|
|
235
|
+
|
|
236
|
+
type Params = { uid: string };
|
|
237
|
+
|
|
238
|
+
export default function Page({
|
|
239
|
+
page,
|
|
240
|
+
}: InferGetStaticPropsType<typeof getStaticProps>) {
|
|
241
|
+
return (
|
|
242
|
+
<>
|
|
243
|
+
<Head>
|
|
244
|
+
<title>{page.data.meta_title}</title>
|
|
245
|
+
{isFilled.keyText(page.data.meta_description) ? (
|
|
246
|
+
<meta name="description" content={page.data.meta_description} />
|
|
247
|
+
) : null}
|
|
248
|
+
{isFilled.image(page.data.meta_image) ? (
|
|
249
|
+
<meta property="og:image" content={asImageSrc(page.data.meta_image) || ""} />
|
|
250
|
+
) : null}
|
|
251
|
+
</Head>
|
|
252
|
+
<SliceZone slices={page.data.slices} components={components} />
|
|
253
|
+
</>
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export async function getStaticProps({
|
|
258
|
+
params,
|
|
259
|
+
previewData,
|
|
260
|
+
}: GetStaticPropsContext<Params>) {
|
|
261
|
+
const client = createClient({ previewData });
|
|
262
|
+
const page = await client.getByUID("${customType.id}", params!.uid);
|
|
263
|
+
|
|
264
|
+
return { props: { page } };
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export async function getStaticPaths() {
|
|
268
|
+
const client = createClient();
|
|
269
|
+
const pages = await client.getAllByType("${customType.id}");
|
|
270
|
+
|
|
271
|
+
return {
|
|
272
|
+
paths: pages.map((page) => asLink(page)),
|
|
273
|
+
fallback: false,
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
`;
|
|
277
|
+
} else {
|
|
278
|
+
return commonTags.stripIndent`
|
|
279
|
+
import Head from "next/head";
|
|
280
|
+
import { isFilled, asLink, asImageSrc } from "@prismicio/client";
|
|
281
|
+
import { SliceZone } from "@prismicio/react";
|
|
282
|
+
|
|
283
|
+
import { components } from "${slicesPath}";
|
|
284
|
+
import { createClient } from "${importPath}";
|
|
285
|
+
|
|
286
|
+
export default function Page({ page }) {
|
|
287
|
+
return (
|
|
288
|
+
<>
|
|
289
|
+
<Head>
|
|
290
|
+
<title>{page.data.meta_title}</title>
|
|
291
|
+
{isFilled.keyText(page.data.meta_description) ? (
|
|
292
|
+
<meta name="description" content={page.data.meta_description} />
|
|
293
|
+
) : null}
|
|
294
|
+
{isFilled.image(page.data.meta_image) ? (
|
|
295
|
+
<meta property="og:image" content={asImageSrc(page.data.meta_image) || ""} />
|
|
296
|
+
) : null}
|
|
297
|
+
</Head>
|
|
298
|
+
<SliceZone slices={page.data.slices} components={components} />
|
|
299
|
+
</>
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
export async function getStaticProps({ params, previewData }) {
|
|
304
|
+
const client = createClient({ previewData });
|
|
305
|
+
const page = await client.getByUID("${customType.id}", params.uid);
|
|
306
|
+
|
|
307
|
+
return { props: { page } };
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
export async function getStaticPaths() {
|
|
311
|
+
const client = createClient();
|
|
312
|
+
const pages = await client.getAllByType("${customType.id}");
|
|
313
|
+
|
|
314
|
+
return {
|
|
315
|
+
paths: pages.map((page) => asLink(page)),
|
|
316
|
+
fallback: false,
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
`;
|
|
320
|
+
}
|
|
321
|
+
} else {
|
|
322
|
+
if (isTypeScript) {
|
|
323
|
+
return commonTags.stripIndent`
|
|
324
|
+
import { GetStaticPropsContext, InferGetStaticPropsType } from "next";
|
|
325
|
+
import Head from "next/head";
|
|
326
|
+
import { isFilled, asImageSrc } from "@prismicio/client";
|
|
327
|
+
import { SliceZone } from "@prismicio/react";
|
|
328
|
+
|
|
329
|
+
import { components } from "${slicesPath}";
|
|
330
|
+
import { createClient } from "${importPath}";
|
|
331
|
+
|
|
332
|
+
export default function Page({
|
|
333
|
+
page,
|
|
334
|
+
}: InferGetStaticPropsType<typeof getStaticProps>) {
|
|
335
|
+
return (
|
|
336
|
+
<>
|
|
337
|
+
<Head>
|
|
338
|
+
<title>{page.data.meta_title}</title>
|
|
339
|
+
{isFilled.keyText(page.data.meta_description) ? (
|
|
340
|
+
<meta name="description" content={page.data.meta_description} />
|
|
341
|
+
) : null}
|
|
342
|
+
{isFilled.image(page.data.meta_image) ? (
|
|
343
|
+
<meta property="og:image" content={asImageSrc(page.data.meta_image) || ""} />
|
|
344
|
+
) : null}
|
|
345
|
+
</Head>
|
|
346
|
+
<SliceZone slices={page.data.slices} components={components} />
|
|
347
|
+
</>
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
export async function getStaticProps({ previewData }: GetStaticPropsContext) {
|
|
352
|
+
const client = createClient({ previewData });
|
|
353
|
+
const page = await client.getSingle("${customType.id}");
|
|
354
|
+
|
|
355
|
+
return { props: { page } };
|
|
356
|
+
}
|
|
357
|
+
`;
|
|
358
|
+
} else {
|
|
359
|
+
return commonTags.stripIndent`
|
|
360
|
+
import Head from "next/head";
|
|
361
|
+
import { isFilled, asImageSrc } from "@prismicio/client";
|
|
362
|
+
import { SliceZone } from "@prismicio/react";
|
|
363
|
+
|
|
364
|
+
import { components } from "${slicesPath}";
|
|
365
|
+
import { createClient } from "${importPath}";
|
|
366
|
+
|
|
367
|
+
export default function Page({ page }) {
|
|
368
|
+
return (
|
|
369
|
+
<>
|
|
370
|
+
<Head>
|
|
371
|
+
<title>{page.data.meta_title}</title>
|
|
372
|
+
{isFilled.keyText(page.data.meta_description) ? (
|
|
373
|
+
<meta name="description" content={page.data.meta_description} />
|
|
374
|
+
) : null}
|
|
375
|
+
{isFilled.image(page.data.meta_image) ? (
|
|
376
|
+
<meta property="og:image" content={asImageSrc(page.data.meta_image) || ""} />
|
|
377
|
+
) : null}
|
|
378
|
+
</Head>
|
|
379
|
+
<SliceZone slices={page.data.slices} components={components} />
|
|
380
|
+
</>
|
|
381
|
+
);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
export async function getStaticProps({ previewData }) {
|
|
385
|
+
const client = createClient({ previewData });
|
|
386
|
+
const page = await client.getSingle("${customType.id}");
|
|
387
|
+
|
|
388
|
+
return { props: { page } };
|
|
389
|
+
}
|
|
390
|
+
`;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
exports.generatePageComponent = generatePageComponent;
|
|
395
|
+
exports.pageRouteCreate = pageRouteCreate;
|
|
396
|
+
//# sourceMappingURL=page-route-create.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"page-route-create.cjs","sources":["../../../src/hooks/page-route-create.ts"],"sourcesContent":["import { CustomType } from \"@prismicio/types-internal/lib/customtypes\";\nimport { stripIndent } from \"common-tags\";\n\nimport type {\n\tCustomTypeUpdateRouteHook,\n\tCustomTypeUpdateRouteHookData,\n\tSliceMachineContext,\n} from \"@slicemachine/plugin-kit\";\nimport {\n\tcheckHasProjectFile,\n\twriteProjectFile,\n} from \"@slicemachine/plugin-kit/fs\";\n\nimport { checkIsTypeScriptProject } from \"../lib/checkIsTypeScriptProject\";\nimport { getJSFileExtension } from \"../lib/getJSFileExtension\";\n\nimport type { PluginOptions } from \"../types\";\nimport { checkHasAppRouter } from \"../lib/checkHasAppRouter\";\nimport path from \"node:path\";\n\ntype Args = {\n\tdata: CustomTypeUpdateRouteHookData;\n} & SliceMachineContext<PluginOptions>;\n\nconst createComponentFile = async ({ data, helpers, options }: Args) => {\n\tconst isTypeScript = await checkIsTypeScriptProject({\n\t\thelpers,\n\t\toptions,\n\t});\n\tconst hasSrcDirectory = await checkHasProjectFile({\n\t\tfilename: \"src\",\n\t\thelpers,\n\t});\n\tconst hasAppRouter = await checkHasAppRouter({ helpers });\n\n\tconst pageRoute = data.model.route ?? data.model.id;\n\tconst pageRouteLength = pageRoute.split(\"/\").length - 1;\n\n\tconst contents = generatePageComponent(data.model, {\n\t\thasAppRouter,\n\t\tisTypeScript,\n\t\thasSrcDirectory,\n\t\tpageRouteLength,\n\t});\n\n\tconst routeBase = hasAppRouter ? \"app\" : \"pages\";\n\tconst routeDirectoryPath = hasSrcDirectory\n\t\t? path.join(\"src\", routeBase)\n\t\t: routeBase;\n\n\tconst extension = await getJSFileExtension({\n\t\thelpers,\n\t\toptions,\n\t\tjsx: true,\n\t});\n\tlet filePath: string;\n\n\tif (hasAppRouter) {\n\t\tconst fileName = data.model.repeatable\n\t\t\t? `[uid]/page.${extension}`\n\t\t\t: `page.${extension}`;\n\n\t\tfilePath = path.join(routeDirectoryPath, pageRoute, fileName);\n\t} else {\n\t\tconst fileName = data.model.repeatable\n\t\t\t? `${pageRoute}/[uid].${extension}`\n\t\t\t: `${pageRoute}.${extension}`;\n\n\t\tfilePath = path.join(routeDirectoryPath, fileName);\n\t}\n\n\tawait writeProjectFile({\n\t\tfilename: filePath,\n\t\tcontents,\n\t\tformat: options.format,\n\t\thelpers,\n\t});\n};\n\nexport const pageRouteCreate: CustomTypeUpdateRouteHook<PluginOptions> = async (\n\tdata,\n\tcontext,\n) => {\n\tawait createComponentFile({ data, ...context });\n};\n\nexport function generatePageComponent(\n\tcustomType: CustomType,\n\tenv: {\n\t\thasAppRouter: boolean;\n\t\tisTypeScript: boolean;\n\t\thasSrcDirectory: boolean;\n\t\tpageRouteLength: number;\n\t},\n): string {\n\tif (env.hasAppRouter) {\n\t\treturn generateAppRouterPage(\n\t\t\tcustomType,\n\t\t\tenv.isTypeScript,\n\t\t\tenv.hasSrcDirectory,\n\t\t\tenv.pageRouteLength,\n\t\t);\n\t} else {\n\t\treturn generatePagesRouterPage(\n\t\t\tcustomType,\n\t\t\tenv.isTypeScript,\n\t\t\tenv.hasSrcDirectory,\n\t\t\tenv.pageRouteLength,\n\t\t);\n\t}\n}\n\nfunction generateAppRouterPage(\n\tcustomType: CustomType,\n\tisTypeScript: boolean,\n\thasSrcDirectory: boolean,\n\tpageRouteLength: number,\n): string {\n\t// Calculate import paths based on directory depth\n\t// Single: app/[id]/page.tsx → ../../prismicio (or ../../../prismicio if src/)\n\t// Repeatable: app/[id]/[uid]/page.tsx → ../../../prismicio (or ../../../../prismicio if src/)\n\tconst levelsUp = customType.repeatable\n\t\t? pageRouteLength + 1\n\t\t: pageRouteLength;\n\tconst levelsUpWithSrc = hasSrcDirectory ? levelsUp + 1 : levelsUp;\n\tconst importPath = \"../\".repeat(levelsUpWithSrc) + \"prismicio\";\n\tconst slicesPath = \"../\".repeat(levelsUpWithSrc) + \"slices\";\n\n\tif (customType.repeatable) {\n\t\tif (isTypeScript) {\n\t\t\treturn stripIndent`\n import { Metadata } from \"next\";\n import { notFound } from \"next/navigation\";\n import { asImageSrc } from \"@prismicio/client\";\n import { SliceZone } from \"@prismicio/react\";\n\n import { createClient } from \"${importPath}\";\n import { components } from \"${slicesPath}\";\n\n type Params = { uid: string };\n\n export default async function Page({ params }: { params: Promise<Params> }) {\n const { uid } = await params;\n const client = createClient();\n const page = await client\n .getByUID(\"${customType.id}\", uid)\n .catch(() => notFound());\n\n return <SliceZone slices={page.data.slices} components={components} />;\n }\n\n export async function generateMetadata({\n params,\n }: {\n params: Promise<Params>;\n }): Promise<Metadata> {\n const { uid } = await params;\n const client = createClient();\n const page = await client\n .getByUID(\"${customType.id}\", uid)\n .catch(() => notFound());\n\n return {\n title: page.data.meta_title,\n description: page.data.meta_description,\n openGraph: {\n images: [{ url: asImageSrc(page.data.meta_image) ?? \"\" }],\n },\n };\n }\n\n export async function generateStaticParams() {\n const client = createClient();\n const pages = await client.getAllByType(\"${customType.id}\");\n\n return pages.map((page) => ({ uid: page.uid }));\n }\n `;\n\t\t} else {\n\t\t\treturn stripIndent`\n import { notFound } from \"next/navigation\";\n import { asImageSrc } from \"@prismicio/client\";\n import { SliceZone } from \"@prismicio/react\";\n\n import { createClient } from \"${importPath}\";\n import { components } from \"${slicesPath}\";\n\n export default async function Page({ params }) {\n const { uid } = await params;\n const client = createClient();\n const page = await client\n .getByUID(\"${customType.id}\", uid)\n .catch(() => notFound());\n\n return <SliceZone slices={page.data.slices} components={components} />;\n }\n\n export async function generateMetadata({ params }) {\n const { uid } = await params;\n const client = createClient();\n const page = await client\n .getByUID(\"${customType.id}\", uid)\n .catch(() => notFound());\n\n return {\n title: page.data.meta_title,\n description: page.data.meta_description,\n openGraph: {\n images: [{ url: asImageSrc(page.data.meta_image) ?? \"\" }],\n },\n };\n }\n\n export async function generateStaticParams() {\n const client = createClient();\n const pages = await client.getAllByType(\"${customType.id}\");\n\n return pages.map((page) => ({ uid: page.uid }));\n }\n `;\n\t\t}\n\t} else {\n\t\t// Single type\n\t\tif (isTypeScript) {\n\t\t\treturn stripIndent`\n import { type Metadata } from \"next\";\n import { notFound } from \"next/navigation\";\n import { asImageSrc } from \"@prismicio/client\";\n import { SliceZone } from \"@prismicio/react\";\n\n import { createClient } from \"${importPath}\";\n import { components } from \"${slicesPath}\";\n\n export default async function Page() {\n const client = createClient();\n const page = await client.getSingle(\"${customType.id}\").catch(() => notFound());\n\n return <SliceZone slices={page.data.slices} components={components} />;\n }\n\n export async function generateMetadata(): Promise<Metadata> {\n const client = createClient();\n const page = await client.getSingle(\"${customType.id}\").catch(() => notFound());\n\n return {\n title: page.data.meta_title,\n description: page.data.meta_description,\n openGraph: {\n images: [{ url: asImageSrc(page.data.meta_image) ?? \"\" }],\n },\n };\n }\n `;\n\t\t} else {\n\t\t\treturn stripIndent`\n import { notFound } from \"next/navigation\";\n import { asImageSrc } from \"@prismicio/client\";\n import { SliceZone } from \"@prismicio/react\";\n\n import { createClient } from \"${importPath}\";\n import { components } from \"${slicesPath}\";\n\n export default async function Page() {\n const client = createClient();\n const page = await client.getSingle(\"${customType.id}\").catch(() => notFound());\n\n return <SliceZone slices={page.data.slices} components={components} />;\n }\n\n export async function generateMetadata() {\n const client = createClient();\n const page = await client.getSingle(\"${customType.id}\").catch(() => notFound());\n\n return {\n title: page.data.meta_title,\n description: page.data.meta_description,\n openGraph: {\n images: [{ url: asImageSrc(page.data.meta_image) ?? \"\" }],\n },\n };\n }\n `;\n\t\t}\n\t}\n}\n\nfunction generatePagesRouterPage(\n\tcustomType: CustomType,\n\tisTypeScript: boolean,\n\thasSrcDirectory: boolean,\n\tpageRouteLength: number,\n): string {\n\t// Calculate import paths based on directory depth\n\t// Single: pages/[id].tsx → ../prismicio (or ../../prismicio if src/)\n\t// Repeatable: pages/[id]/[uid].tsx → ../../prismicio (or ../../../prismicio if src/)\n\tconst levelsUp = customType.repeatable\n\t\t? pageRouteLength + 1\n\t\t: pageRouteLength;\n\tconst levelsUpWithSrc = hasSrcDirectory ? levelsUp + 1 : levelsUp;\n\tconst importPath = \"../\".repeat(levelsUpWithSrc) + \"prismicio\";\n\tconst slicesPath = \"../\".repeat(levelsUpWithSrc) + \"slices\";\n\n\tif (customType.repeatable) {\n\t\tif (isTypeScript) {\n\t\t\treturn stripIndent`\n import { GetStaticPropsContext, InferGetStaticPropsType } from \"next\";\n import Head from \"next/head\";\n import { isFilled, asLink, asImageSrc } from \"@prismicio/client\";\n import { SliceZone } from \"@prismicio/react\";\n\n import { components } from \"${slicesPath}\";\n import { createClient } from \"${importPath}\";\n\n type Params = { uid: string };\n\n export default function Page({\n page,\n }: InferGetStaticPropsType<typeof getStaticProps>) {\n return (\n <>\n <Head>\n <title>{page.data.meta_title}</title>\n {isFilled.keyText(page.data.meta_description) ? (\n <meta name=\"description\" content={page.data.meta_description} />\n ) : null}\n {isFilled.image(page.data.meta_image) ? (\n <meta property=\"og:image\" content={asImageSrc(page.data.meta_image) || \"\"} />\n ) : null}\n </Head>\n <SliceZone slices={page.data.slices} components={components} />\n </>\n );\n }\n\n export async function getStaticProps({\n params,\n previewData,\n }: GetStaticPropsContext<Params>) {\n const client = createClient({ previewData });\n const page = await client.getByUID(\"${customType.id}\", params!.uid);\n\n return { props: { page } };\n }\n\n export async function getStaticPaths() {\n const client = createClient();\n const pages = await client.getAllByType(\"${customType.id}\");\n\n return {\n paths: pages.map((page) => asLink(page)),\n fallback: false,\n };\n }\n `;\n\t\t} else {\n\t\t\treturn stripIndent`\n import Head from \"next/head\";\n import { isFilled, asLink, asImageSrc } from \"@prismicio/client\";\n import { SliceZone } from \"@prismicio/react\";\n\n import { components } from \"${slicesPath}\";\n import { createClient } from \"${importPath}\";\n\n export default function Page({ page }) {\n return (\n <>\n <Head>\n <title>{page.data.meta_title}</title>\n {isFilled.keyText(page.data.meta_description) ? (\n <meta name=\"description\" content={page.data.meta_description} />\n ) : null}\n {isFilled.image(page.data.meta_image) ? (\n <meta property=\"og:image\" content={asImageSrc(page.data.meta_image) || \"\"} />\n ) : null}\n </Head>\n <SliceZone slices={page.data.slices} components={components} />\n </>\n );\n }\n\n export async function getStaticProps({ params, previewData }) {\n const client = createClient({ previewData });\n const page = await client.getByUID(\"${customType.id}\", params.uid);\n\n return { props: { page } };\n }\n\n export async function getStaticPaths() {\n const client = createClient();\n const pages = await client.getAllByType(\"${customType.id}\");\n\n return {\n paths: pages.map((page) => asLink(page)),\n fallback: false,\n };\n }\n `;\n\t\t}\n\t} else {\n\t\t// Single type\n\t\tif (isTypeScript) {\n\t\t\treturn stripIndent`\n import { GetStaticPropsContext, InferGetStaticPropsType } from \"next\";\n import Head from \"next/head\";\n import { isFilled, asImageSrc } from \"@prismicio/client\";\n import { SliceZone } from \"@prismicio/react\";\n\n import { components } from \"${slicesPath}\";\n import { createClient } from \"${importPath}\";\n\n export default function Page({\n page,\n }: InferGetStaticPropsType<typeof getStaticProps>) {\n return (\n <>\n <Head>\n <title>{page.data.meta_title}</title>\n {isFilled.keyText(page.data.meta_description) ? (\n <meta name=\"description\" content={page.data.meta_description} />\n ) : null}\n {isFilled.image(page.data.meta_image) ? (\n <meta property=\"og:image\" content={asImageSrc(page.data.meta_image) || \"\"} />\n ) : null}\n </Head>\n <SliceZone slices={page.data.slices} components={components} />\n </>\n );\n }\n\n export async function getStaticProps({ previewData }: GetStaticPropsContext) {\n const client = createClient({ previewData });\n const page = await client.getSingle(\"${customType.id}\");\n\n return { props: { page } };\n }\n `;\n\t\t} else {\n\t\t\treturn stripIndent`\n import Head from \"next/head\";\n import { isFilled, asImageSrc } from \"@prismicio/client\";\n import { SliceZone } from \"@prismicio/react\";\n\n import { components } from \"${slicesPath}\";\n import { createClient } from \"${importPath}\";\n\n export default function Page({ page }) {\n return (\n <>\n <Head>\n <title>{page.data.meta_title}</title>\n {isFilled.keyText(page.data.meta_description) ? (\n <meta name=\"description\" content={page.data.meta_description} />\n ) : null}\n {isFilled.image(page.data.meta_image) ? (\n <meta property=\"og:image\" content={asImageSrc(page.data.meta_image) || \"\"} />\n ) : null}\n </Head>\n <SliceZone slices={page.data.slices} components={components} />\n </>\n );\n }\n\n export async function getStaticProps({ previewData }) {\n const client = createClient({ previewData });\n const page = await client.getSingle(\"${customType.id}\");\n\n return { props: { page } };\n }\n `;\n\t\t}\n\t}\n}\n"],"names":["checkIsTypeScriptProject","checkHasProjectFile","checkHasAppRouter","getJSFileExtension","writeProjectFile","stripIndent"],"mappings":";;;;;;;;AAwBA,MAAM,sBAAsB,OAAO,EAAE,MAAM,SAAS,cAAmB;AACtE,QAAM,eAAe,MAAMA,kDAAyB;AAAA,IACnD;AAAA,IACA;AAAA,EAAA,CACA;AACD,QAAM,kBAAkB,MAAMC,uBAAoB;AAAA,IACjD,UAAU;AAAA,IACV;AAAA,EAAA,CACA;AACD,QAAM,eAAe,MAAMC,oCAAkB,EAAE,SAAS;AAExD,QAAM,YAAY,KAAK,MAAM,SAAS,KAAK,MAAM;AACjD,QAAM,kBAAkB,UAAU,MAAM,GAAG,EAAE,SAAS;AAEtD,QAAM,WAAW,sBAAsB,KAAK,OAAO;AAAA,IAClD;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA,CACA;AAED,QAAM,YAAY,eAAe,QAAQ;AACzC,QAAM,qBAAqB,kBACxB,KAAK,KAAK,OAAO,SAAS,IAC1B;AAEH,QAAM,YAAY,MAAMC,sCAAmB;AAAA,IAC1C;AAAA,IACA;AAAA,IACA,KAAK;AAAA,EAAA,CACL;AACD,MAAI;AAEJ,MAAI,cAAc;AACjB,UAAM,WAAW,KAAK,MAAM,aACzB,cAAc,SAAS,KACvB,QAAQ,SAAS;AAEpB,eAAW,KAAK,KAAK,oBAAoB,WAAW,QAAQ;AAAA,EAC7D,OAAO;AACN,UAAM,WAAW,KAAK,MAAM,aACzB,GAAG,SAAS,UAAU,SAAS,KAC/B,GAAG,SAAS,IAAI,SAAS;AAE5B,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IAK5D;AAAA,EACD;AACD;;;"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { CustomType } from "@prismicio/types-internal/lib/customtypes";
|
|
2
|
+
import type { CustomTypeUpdateRouteHook } from "@slicemachine/plugin-kit";
|
|
3
|
+
import type { PluginOptions } from "../types";
|
|
4
|
+
export declare const pageRouteCreate: CustomTypeUpdateRouteHook<PluginOptions>;
|
|
5
|
+
export declare function generatePageComponent(customType: CustomType, env: {
|
|
6
|
+
hasAppRouter: boolean;
|
|
7
|
+
isTypeScript: boolean;
|
|
8
|
+
hasSrcDirectory: boolean;
|
|
9
|
+
pageRouteLength: number;
|
|
10
|
+
}): string;
|