@slicemachine/adapter-next 0.3.2-dev-next-release.5 → 0.3.2-dev-next-release.7
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/project-init.cjs +330 -58
- package/dist/hooks/project-init.cjs.map +1 -1
- package/dist/hooks/project-init.js +331 -59
- package/dist/hooks/project-init.js.map +1 -1
- package/dist/hooks/slice-create.cjs +3 -2
- package/dist/hooks/slice-create.cjs.map +1 -1
- package/dist/hooks/slice-create.js +3 -2
- package/dist/hooks/slice-create.js.map +1 -1
- package/dist/hooks/sliceSimulator-setup-read.cjs +3 -5
- package/dist/hooks/sliceSimulator-setup-read.cjs.map +1 -1
- package/dist/hooks/sliceSimulator-setup-read.js +3 -5
- package/dist/hooks/sliceSimulator-setup-read.js.map +1 -1
- package/dist/lib/checkHasAppRouter.cjs +10 -0
- package/dist/lib/checkHasAppRouter.cjs.map +1 -0
- package/dist/lib/checkHasAppRouter.d.ts +5 -0
- package/dist/lib/checkHasAppRouter.js +10 -0
- package/dist/lib/checkHasAppRouter.js.map +1 -0
- package/dist/lib/checkHasSrcDirectory.cjs +8 -0
- package/dist/lib/checkHasSrcDirectory.cjs.map +1 -0
- package/dist/lib/checkHasSrcDirectory.d.ts +5 -0
- package/dist/lib/checkHasSrcDirectory.js +8 -0
- package/dist/lib/checkHasSrcDirectory.js.map +1 -0
- package/dist/lib/getJSFileExtension.cjs +16 -0
- package/dist/lib/getJSFileExtension.cjs.map +1 -0
- package/dist/lib/getJSFileExtension.d.ts +7 -0
- package/dist/lib/getJSFileExtension.js +16 -0
- package/dist/lib/getJSFileExtension.js.map +1 -0
- package/dist/lib/upsertGlobalContentTypes.cjs +1 -1
- package/dist/lib/upsertGlobalContentTypes.cjs.map +1 -1
- package/dist/lib/upsertGlobalContentTypes.js +1 -1
- package/dist/lib/upsertGlobalContentTypes.js.map +1 -1
- package/dist/lib/upsertSliceLibraryIndexFile.cjs +3 -3
- package/dist/lib/upsertSliceLibraryIndexFile.cjs.map +1 -1
- package/dist/lib/upsertSliceLibraryIndexFile.js +3 -3
- package/dist/lib/upsertSliceLibraryIndexFile.js.map +1 -1
- package/dist/types.d.ts +40 -0
- package/package.json +3 -4
- package/src/hooks/project-init.ts +412 -64
- package/src/hooks/slice-create.ts +3 -5
- package/src/hooks/sliceSimulator-setup-read.ts +3 -5
- package/src/lib/checkHasAppRouter.ts +22 -0
- package/src/lib/checkHasSrcDirectory.ts +15 -0
- package/src/lib/getJSFileExtension.ts +29 -0
- package/src/lib/upsertGlobalContentTypes.ts +3 -1
- package/src/lib/upsertSliceLibraryIndexFile.ts +3 -4
- package/src/types.ts +44 -0
- package/dist/lib/getJSOrTSXFileExtension.cjs +0 -18
- package/dist/lib/getJSOrTSXFileExtension.cjs.map +0 -1
- package/dist/lib/getJSOrTSXFileExtension.d.ts +0 -8
- package/dist/lib/getJSOrTSXFileExtension.js +0 -18
- package/dist/lib/getJSOrTSXFileExtension.js.map +0 -1
- package/src/lib/getJSOrTSXFileExtension.ts +0 -26
|
@@ -5,14 +5,16 @@ import type {
|
|
|
5
5
|
ProjectInitHookData,
|
|
6
6
|
SliceMachineContext,
|
|
7
7
|
} from "@slicemachine/plugin-kit";
|
|
8
|
-
import {
|
|
8
|
+
import { source } from "common-tags";
|
|
9
9
|
|
|
10
|
+
import { checkHasAppRouter } from "../lib/checkHasAppRouter";
|
|
11
|
+
import { checkHasSrcDirectory } from "../lib/checkHasSrcDirectory";
|
|
12
|
+
import { checkIsTypeScriptProject } from "../lib/checkIsTypeScriptProject";
|
|
10
13
|
import { checkPathExists } from "../lib/checkPathExists";
|
|
11
|
-
import {
|
|
14
|
+
import { getJSFileExtension } from "../lib/getJSFileExtension";
|
|
12
15
|
import { rejectIfNecessary } from "../lib/rejectIfNecessary";
|
|
13
16
|
|
|
14
17
|
import type { PluginOptions } from "../types";
|
|
15
|
-
import { checkIsTypeScriptProject } from "../lib/checkIsTypeScriptProject";
|
|
16
18
|
|
|
17
19
|
type InstallDependenciesArgs = {
|
|
18
20
|
installDependencies: ProjectInitHookData["installDependencies"];
|
|
@@ -40,22 +42,134 @@ const createPrismicIOFile = async ({
|
|
|
40
42
|
helpers,
|
|
41
43
|
options,
|
|
42
44
|
});
|
|
45
|
+
const hasSrcDirectory = await checkHasSrcDirectory({ helpers });
|
|
46
|
+
const hasAppRouter = await checkHasAppRouter({ helpers });
|
|
43
47
|
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
48
|
+
const extension = await getJSFileExtension({ helpers, options });
|
|
49
|
+
const filename = `prismicio.${extension}`;
|
|
50
|
+
const filePath = hasSrcDirectory
|
|
51
|
+
? helpers.joinPathFromRoot("src", filename)
|
|
52
|
+
: helpers.joinPathFromRoot(filename);
|
|
47
53
|
|
|
48
54
|
if (await checkPathExists(filePath)) {
|
|
49
55
|
return;
|
|
50
56
|
}
|
|
51
57
|
|
|
58
|
+
let createClientContents: string;
|
|
59
|
+
|
|
60
|
+
if (hasAppRouter) {
|
|
61
|
+
if (isTypeScriptProject) {
|
|
62
|
+
createClientContents = source`
|
|
63
|
+
/**
|
|
64
|
+
* Creates a Prismic client for the project's repository. The client is used to
|
|
65
|
+
* query content from the Prismic API.
|
|
66
|
+
*
|
|
67
|
+
* @param config - Configuration for the Prismic client.
|
|
68
|
+
*/
|
|
69
|
+
export const createClient = (config: prismicNext.CreateClientConfig = {}) => {
|
|
70
|
+
const client = prismic.createClient(repositoryName, {
|
|
71
|
+
routes,
|
|
72
|
+
fetchOptions:
|
|
73
|
+
process.env.NODE_ENV === 'production'
|
|
74
|
+
? { next: { tags: ['prismic'] }, cache: 'force-cache' }
|
|
75
|
+
: { next: { revalidate: 5 } },
|
|
76
|
+
...config,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
prismicNext.enableAutoPreviews({
|
|
80
|
+
client,
|
|
81
|
+
previewData: config.previewData,
|
|
82
|
+
req: config.req,
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
return client;
|
|
86
|
+
};
|
|
87
|
+
`;
|
|
88
|
+
} else {
|
|
89
|
+
createClientContents = source`
|
|
90
|
+
/**
|
|
91
|
+
* Creates a Prismic client for the project's repository. The client is used to
|
|
92
|
+
* query content from the Prismic API.
|
|
93
|
+
*
|
|
94
|
+
* @param {prismicNext.CreateClientConfig} config - Configuration for the Prismic client.
|
|
95
|
+
*/
|
|
96
|
+
export const createClient = (config = {}) => {
|
|
97
|
+
const client = prismic.createClient(repositoryName, {
|
|
98
|
+
routes,
|
|
99
|
+
fetchOptions:
|
|
100
|
+
process.env.NODE_ENV === 'production'
|
|
101
|
+
? { next: { tags: ['prismic'] }, cache: 'force-cache' }
|
|
102
|
+
: { next: { revalidate: 5 } },
|
|
103
|
+
...config,
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
prismicNext.enableAutoPreviews({
|
|
107
|
+
client,
|
|
108
|
+
previewData: config.previewData,
|
|
109
|
+
req: config.req,
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
return client;
|
|
113
|
+
};
|
|
114
|
+
`;
|
|
115
|
+
}
|
|
116
|
+
} else {
|
|
117
|
+
if (isTypeScriptProject) {
|
|
118
|
+
createClientContents = source`
|
|
119
|
+
/**
|
|
120
|
+
* Creates a Prismic client for the project's repository. The client is used to
|
|
121
|
+
* query content from the Prismic API.
|
|
122
|
+
*
|
|
123
|
+
* @param config - Configuration for the Prismic client.
|
|
124
|
+
*/
|
|
125
|
+
export const createClient = (config: prismicNext.CreateClientConfig = {}) => {
|
|
126
|
+
const client = prismic.createClient(repositoryName, {
|
|
127
|
+
routes,
|
|
128
|
+
...config,
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
prismicNext.enableAutoPreviews({
|
|
132
|
+
client,
|
|
133
|
+
previewData: config.previewData,
|
|
134
|
+
req: config.req,
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
return client;
|
|
138
|
+
};
|
|
139
|
+
`;
|
|
140
|
+
} else {
|
|
141
|
+
createClientContents = source`
|
|
142
|
+
/**
|
|
143
|
+
* Creates a Prismic client for the project's repository. The client is used to
|
|
144
|
+
* query content from the Prismic API.
|
|
145
|
+
*
|
|
146
|
+
* @param {prismicNext.CreateClientConfig} config - Configuration for the Prismic client.
|
|
147
|
+
*/
|
|
148
|
+
export const createClient = (config = {}) => {
|
|
149
|
+
const client = prismic.createClient(repositoryName, {
|
|
150
|
+
routes,
|
|
151
|
+
...config,
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
prismicNext.enableAutoPreviews({
|
|
155
|
+
client,
|
|
156
|
+
previewData: config.previewData,
|
|
157
|
+
req: config.req,
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
return client;
|
|
161
|
+
};
|
|
162
|
+
`;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
52
166
|
let contents: string;
|
|
53
167
|
|
|
54
168
|
if (isTypeScriptProject) {
|
|
55
|
-
contents =
|
|
169
|
+
contents = source`
|
|
56
170
|
import * as prismic from "@prismicio/client";
|
|
57
171
|
import * as prismicNext from "@prismicio/next";
|
|
58
|
-
import config from "
|
|
172
|
+
import config from "${hasSrcDirectory ? ".." : "."}/slicemachine.config.json";
|
|
59
173
|
|
|
60
174
|
/**
|
|
61
175
|
* The project's Prismic repository name.
|
|
@@ -63,8 +177,7 @@ const createPrismicIOFile = async ({
|
|
|
63
177
|
export const repositoryName = config.repositoryName;
|
|
64
178
|
|
|
65
179
|
/**
|
|
66
|
-
* A list of Route Resolver objects that define how a document's \`url\` field
|
|
67
|
-
* is resolved.
|
|
180
|
+
* A list of Route Resolver objects that define how a document's \`url\` field is resolved.
|
|
68
181
|
*
|
|
69
182
|
* {@link https://prismic.io/docs/route-resolver#route-resolver}
|
|
70
183
|
*/
|
|
@@ -80,32 +193,13 @@ const createPrismicIOFile = async ({
|
|
|
80
193
|
},
|
|
81
194
|
];
|
|
82
195
|
|
|
83
|
-
|
|
84
|
-
* Creates a Prismic client for the project's repository. The client is used to
|
|
85
|
-
* query content from the Prismic API.
|
|
86
|
-
*
|
|
87
|
-
* @param config - Configuration for the Prismic client.
|
|
88
|
-
*/
|
|
89
|
-
export const createClient = (config: prismicNext.CreateClientConfig = {}) => {
|
|
90
|
-
const client = prismic.createClient(repositoryName, {
|
|
91
|
-
routes,
|
|
92
|
-
...config,
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
prismicNext.enableAutoPreviews({
|
|
96
|
-
client,
|
|
97
|
-
previewData: config.previewData,
|
|
98
|
-
req: config.req,
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
return client;
|
|
102
|
-
};
|
|
196
|
+
${createClientContents}
|
|
103
197
|
`;
|
|
104
198
|
} else {
|
|
105
|
-
contents =
|
|
199
|
+
contents = source`
|
|
106
200
|
import * as prismic from "@prismicio/client";
|
|
107
201
|
import * as prismicNext from "@prismicio/next";
|
|
108
|
-
import config from "
|
|
202
|
+
import config from "${hasSrcDirectory ? ".." : "."}/slicemachine.config.json";
|
|
109
203
|
|
|
110
204
|
/**
|
|
111
205
|
* The project's Prismic repository name.
|
|
@@ -113,8 +207,7 @@ const createPrismicIOFile = async ({
|
|
|
113
207
|
export const repositoryName = config.repositoryName;
|
|
114
208
|
|
|
115
209
|
/**
|
|
116
|
-
* A list of Route Resolver objects that define how a document's \`url\` field
|
|
117
|
-
* is resolved.
|
|
210
|
+
* A list of Route Resolver objects that define how a document's \`url\` field is resolved.
|
|
118
211
|
*
|
|
119
212
|
* {@link https://prismic.io/docs/route-resolver#route-resolver}
|
|
120
213
|
*
|
|
@@ -132,26 +225,7 @@ const createPrismicIOFile = async ({
|
|
|
132
225
|
},
|
|
133
226
|
];
|
|
134
227
|
|
|
135
|
-
|
|
136
|
-
* Creates a Prismic client for the project's repository. The client is used to
|
|
137
|
-
* query content from the Prismic API.
|
|
138
|
-
*
|
|
139
|
-
* @param {prismicNext.CreateClientConfig} config - Configuration for the Prismic client.
|
|
140
|
-
*/
|
|
141
|
-
export const createClient = (config = {}) => {
|
|
142
|
-
const client = prismic.createClient(repositoryName, {
|
|
143
|
-
routes,
|
|
144
|
-
...config,
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
prismicNext.enableAutoPreviews({
|
|
148
|
-
client,
|
|
149
|
-
previewData: config.previewData,
|
|
150
|
-
req: config.req,
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
return client;
|
|
154
|
-
};
|
|
228
|
+
${createClientContents}
|
|
155
229
|
`;
|
|
156
230
|
}
|
|
157
231
|
|
|
@@ -168,13 +242,19 @@ const createSliceSimulatorPage = async ({
|
|
|
168
242
|
helpers,
|
|
169
243
|
options,
|
|
170
244
|
}: CreateSliceSimulatorPageArgs) => {
|
|
171
|
-
const
|
|
172
|
-
|
|
173
|
-
);
|
|
245
|
+
const hasSrcDirectory = await checkHasSrcDirectory({ helpers });
|
|
246
|
+
const hasAppRouter = await checkHasAppRouter({ helpers });
|
|
174
247
|
|
|
248
|
+
const extension = await getJSFileExtension({ helpers, options, jsx: true });
|
|
175
249
|
const filePath = helpers.joinPathFromRoot(
|
|
176
|
-
|
|
177
|
-
|
|
250
|
+
...[
|
|
251
|
+
hasSrcDirectory ? "src" : undefined,
|
|
252
|
+
hasAppRouter
|
|
253
|
+
? `app/slice-simulator/page.${extension}`
|
|
254
|
+
: `pages/slice-simulator.${extension}`,
|
|
255
|
+
].filter((segment): segment is NonNullable<typeof segment> =>
|
|
256
|
+
Boolean(segment),
|
|
257
|
+
),
|
|
178
258
|
);
|
|
179
259
|
|
|
180
260
|
if (await checkPathExists(filePath)) {
|
|
@@ -183,11 +263,11 @@ const createSliceSimulatorPage = async ({
|
|
|
183
263
|
|
|
184
264
|
await fs.mkdir(path.dirname(filePath), { recursive: true });
|
|
185
265
|
|
|
186
|
-
let contents =
|
|
266
|
+
let contents = source`
|
|
187
267
|
import { SliceSimulator } from "@slicemachine/adapter-next/simulator";
|
|
188
268
|
import { SliceZone } from "@prismicio/react";
|
|
189
269
|
|
|
190
|
-
import { components } from "
|
|
270
|
+
import { components } from "${hasAppRouter ? "../.." : ".."}/slices";
|
|
191
271
|
|
|
192
272
|
export default function SliceSimulatorPage() {
|
|
193
273
|
return (
|
|
@@ -205,15 +285,283 @@ const createSliceSimulatorPage = async ({
|
|
|
205
285
|
await fs.writeFile(filePath, contents);
|
|
206
286
|
};
|
|
207
287
|
|
|
288
|
+
const createPreviewRoute = async ({
|
|
289
|
+
helpers,
|
|
290
|
+
options,
|
|
291
|
+
}: SliceMachineContext<PluginOptions>) => {
|
|
292
|
+
const hasSrcDirectory = await checkHasSrcDirectory({ helpers });
|
|
293
|
+
const hasAppRouter = await checkHasAppRouter({ helpers });
|
|
294
|
+
const isTypeScriptProject = await checkIsTypeScriptProject({
|
|
295
|
+
helpers,
|
|
296
|
+
options,
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
const extension = await getJSFileExtension({ helpers, options });
|
|
300
|
+
const filePath = helpers.joinPathFromRoot(
|
|
301
|
+
...[
|
|
302
|
+
hasSrcDirectory ? "src" : undefined,
|
|
303
|
+
hasAppRouter
|
|
304
|
+
? `app/api/preview/route.${extension}`
|
|
305
|
+
: `pages/api/preview.${extension}`,
|
|
306
|
+
].filter((segment): segment is NonNullable<typeof segment> =>
|
|
307
|
+
Boolean(segment),
|
|
308
|
+
),
|
|
309
|
+
);
|
|
310
|
+
|
|
311
|
+
if (await checkPathExists(filePath)) {
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
let contents: string;
|
|
316
|
+
|
|
317
|
+
if (hasAppRouter) {
|
|
318
|
+
if (isTypeScriptProject) {
|
|
319
|
+
contents = source`
|
|
320
|
+
import { NextRequest } from "next/server";
|
|
321
|
+
import { draftMode } from "next/headers";
|
|
322
|
+
import { redirectToPreviewURL } from "@prismicio/next";
|
|
323
|
+
|
|
324
|
+
import { createClient } from "../../../prismicio";
|
|
325
|
+
|
|
326
|
+
export async function GET(request: NextRequest) {
|
|
327
|
+
const client = createClient();
|
|
328
|
+
|
|
329
|
+
draftMode().enable();
|
|
330
|
+
|
|
331
|
+
await redirectToPreviewURL({ client, request });
|
|
332
|
+
}
|
|
333
|
+
`;
|
|
334
|
+
} else {
|
|
335
|
+
contents = source`
|
|
336
|
+
import { draftMode } from "next/headers";
|
|
337
|
+
import { redirectToPreviewURL } from "@prismicio/next";
|
|
338
|
+
|
|
339
|
+
import { createClient } from "../../../prismicio";
|
|
340
|
+
|
|
341
|
+
export async function GET(request) {
|
|
342
|
+
const client = createClient();
|
|
343
|
+
|
|
344
|
+
draftMode().enable();
|
|
345
|
+
|
|
346
|
+
await redirectToPreviewURL({ client, request });
|
|
347
|
+
}
|
|
348
|
+
`;
|
|
349
|
+
}
|
|
350
|
+
} else {
|
|
351
|
+
if (isTypeScriptProject) {
|
|
352
|
+
contents = source`
|
|
353
|
+
import { NextApiRequest, NextApiResponse } from "next";
|
|
354
|
+
import { setPreviewData, redirectToPreviewURL } from "@prismicio/next";
|
|
355
|
+
|
|
356
|
+
import { createClient } from "../../prismicio";
|
|
357
|
+
|
|
358
|
+
export default async (req: NextApiRequest, res: NextApiResponse) => {
|
|
359
|
+
const client = createClient({ req });
|
|
360
|
+
|
|
361
|
+
await setPreviewData({ req, res });
|
|
362
|
+
|
|
363
|
+
await redirectToPreviewURL({ req, res, client });
|
|
364
|
+
};
|
|
365
|
+
`;
|
|
366
|
+
} else {
|
|
367
|
+
contents = source`
|
|
368
|
+
import { setPreviewData, redirectToPreviewURL } from "@prismicio/next";
|
|
369
|
+
|
|
370
|
+
import { createClient } from "../../prismicio";
|
|
371
|
+
|
|
372
|
+
export default async (req, res) => {
|
|
373
|
+
const client = createClient({ req });
|
|
374
|
+
|
|
375
|
+
await setPreviewData({ req, res });
|
|
376
|
+
|
|
377
|
+
await redirectToPreviewURL({ req, res, client });
|
|
378
|
+
};
|
|
379
|
+
`;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
if (options.format) {
|
|
384
|
+
contents = await helpers.format(contents, filePath);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
await fs.mkdir(path.dirname(filePath), { recursive: true });
|
|
388
|
+
await fs.writeFile(filePath, contents);
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
const createExitPreviewRoute = async ({
|
|
392
|
+
helpers,
|
|
393
|
+
options,
|
|
394
|
+
}: SliceMachineContext<PluginOptions>) => {
|
|
395
|
+
const hasSrcDirectory = await checkHasSrcDirectory({ helpers });
|
|
396
|
+
const hasAppRouter = await checkHasAppRouter({ helpers });
|
|
397
|
+
const isTypeScriptProject = await checkIsTypeScriptProject({
|
|
398
|
+
helpers,
|
|
399
|
+
options,
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
const extension = await getJSFileExtension({ helpers, options });
|
|
403
|
+
const filePath = helpers.joinPathFromRoot(
|
|
404
|
+
...[
|
|
405
|
+
hasSrcDirectory ? "src" : undefined,
|
|
406
|
+
hasAppRouter
|
|
407
|
+
? `app/api/exit-preview/route.${extension}`
|
|
408
|
+
: `pages/api/exit-preview.${extension}`,
|
|
409
|
+
].filter((segment): segment is NonNullable<typeof segment> =>
|
|
410
|
+
Boolean(segment),
|
|
411
|
+
),
|
|
412
|
+
);
|
|
413
|
+
|
|
414
|
+
if (await checkPathExists(filePath)) {
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
let contents: string;
|
|
419
|
+
|
|
420
|
+
if (hasAppRouter) {
|
|
421
|
+
contents = `
|
|
422
|
+
import { exitPreview } from "@prismicio/next";
|
|
423
|
+
|
|
424
|
+
export async function GET() {
|
|
425
|
+
return await exitPreview();
|
|
426
|
+
}
|
|
427
|
+
`;
|
|
428
|
+
} else {
|
|
429
|
+
if (isTypeScriptProject) {
|
|
430
|
+
contents = source`
|
|
431
|
+
import { NextApiRequest, NextApiResponse } from "next";
|
|
432
|
+
import { exitPreview } from "@prismicio/next";
|
|
433
|
+
|
|
434
|
+
export async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
435
|
+
return await exitPreview({ req, res });
|
|
436
|
+
}
|
|
437
|
+
`;
|
|
438
|
+
} else {
|
|
439
|
+
contents = source`
|
|
440
|
+
import { exitPreview } from "@prismicio/next";
|
|
441
|
+
|
|
442
|
+
export async function handler(req, res) {
|
|
443
|
+
return await exitPreview({ req, res });
|
|
444
|
+
}
|
|
445
|
+
`;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
if (options.format) {
|
|
450
|
+
contents = await helpers.format(contents, filePath);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
await fs.mkdir(path.dirname(filePath), { recursive: true });
|
|
454
|
+
await fs.writeFile(filePath, contents);
|
|
455
|
+
};
|
|
456
|
+
|
|
457
|
+
const modifySliceMachineConfig = async ({
|
|
458
|
+
helpers,
|
|
459
|
+
options,
|
|
460
|
+
}: SliceMachineContext<PluginOptions>) => {
|
|
461
|
+
const hasSrcDirectory = await checkHasSrcDirectory({ helpers });
|
|
462
|
+
const project = await helpers.getProject();
|
|
463
|
+
|
|
464
|
+
// Add Slice Simulator URL.
|
|
465
|
+
project.config.localSliceSimulatorURL ||=
|
|
466
|
+
"http://localhost:3000/slice-simulator";
|
|
467
|
+
|
|
468
|
+
// Nest the default Slice Library in the src directory if it exists and
|
|
469
|
+
// is empty.
|
|
470
|
+
if (
|
|
471
|
+
hasSrcDirectory &&
|
|
472
|
+
JSON.stringify(project.config.libraries) === JSON.stringify(["./slices"])
|
|
473
|
+
) {
|
|
474
|
+
try {
|
|
475
|
+
const entries = await fs.readdir(helpers.joinPathFromRoot("slices"));
|
|
476
|
+
|
|
477
|
+
if (!entries.map((entry) => path.parse(entry).name).includes("index")) {
|
|
478
|
+
project.config.libraries = ["./src/slices"];
|
|
479
|
+
}
|
|
480
|
+
} catch (error) {
|
|
481
|
+
if (
|
|
482
|
+
error instanceof Error &&
|
|
483
|
+
"code" in error &&
|
|
484
|
+
error.code === "ENOENT"
|
|
485
|
+
) {
|
|
486
|
+
// The directory does not exist, which means we
|
|
487
|
+
// can safely nest the library.
|
|
488
|
+
project.config.libraries = ["./src/slices"];
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
const filePath = helpers.joinPathFromRoot("slicemachine.config.json");
|
|
494
|
+
|
|
495
|
+
let contents = JSON.stringify(project.config);
|
|
496
|
+
|
|
497
|
+
if (options.format) {
|
|
498
|
+
contents = await helpers.format(contents, filePath);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
await fs.writeFile(
|
|
502
|
+
helpers.joinPathFromRoot("slicemachine.config.json"),
|
|
503
|
+
contents,
|
|
504
|
+
);
|
|
505
|
+
};
|
|
506
|
+
|
|
507
|
+
const createRevalidateRoute = async ({
|
|
508
|
+
helpers,
|
|
509
|
+
options,
|
|
510
|
+
}: SliceMachineContext<PluginOptions>) => {
|
|
511
|
+
const hasAppRouter = await checkHasAppRouter({ helpers });
|
|
512
|
+
|
|
513
|
+
if (!hasAppRouter) {
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
const hasSrcDirectory = await checkHasSrcDirectory({ helpers });
|
|
518
|
+
|
|
519
|
+
const extension = await getJSFileExtension({ helpers, options });
|
|
520
|
+
const filePath = helpers.joinPathFromRoot(
|
|
521
|
+
...[
|
|
522
|
+
hasSrcDirectory ? "src" : undefined,
|
|
523
|
+
`app/api/revalidate/route.${extension}`,
|
|
524
|
+
].filter((segment): segment is NonNullable<typeof segment> =>
|
|
525
|
+
Boolean(segment),
|
|
526
|
+
),
|
|
527
|
+
);
|
|
528
|
+
|
|
529
|
+
if (await checkPathExists(filePath)) {
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
let contents = source`
|
|
534
|
+
import { NextResponse } from "next/server";
|
|
535
|
+
import { revalidateTag } from "next/cache";
|
|
536
|
+
|
|
537
|
+
export async function POST() {
|
|
538
|
+
revalidateTag("prismic");
|
|
539
|
+
|
|
540
|
+
return NextResponse.json({ revalidated: true, now: Date.now() });
|
|
541
|
+
}
|
|
542
|
+
`;
|
|
543
|
+
|
|
544
|
+
if (options.format) {
|
|
545
|
+
contents = await helpers.format(contents, filePath);
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
await fs.mkdir(path.dirname(filePath), { recursive: true });
|
|
549
|
+
await fs.writeFile(filePath, contents);
|
|
550
|
+
};
|
|
551
|
+
|
|
208
552
|
export const projectInit: ProjectInitHook<PluginOptions> = async (
|
|
209
553
|
{ installDependencies: _installDependencies },
|
|
210
554
|
context,
|
|
211
555
|
) => {
|
|
212
556
|
rejectIfNecessary(
|
|
213
557
|
await Promise.allSettled([
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
558
|
+
installDependencies({ installDependencies: _installDependencies }),
|
|
559
|
+
modifySliceMachineConfig(context),
|
|
560
|
+
createPrismicIOFile(context),
|
|
561
|
+
createSliceSimulatorPage(context),
|
|
562
|
+
createPreviewRoute(context),
|
|
563
|
+
createExitPreviewRoute(context),
|
|
564
|
+
createRevalidateRoute(context),
|
|
217
565
|
]),
|
|
218
566
|
);
|
|
219
567
|
};
|
|
@@ -9,7 +9,7 @@ import * as path from "node:path";
|
|
|
9
9
|
|
|
10
10
|
import { buildSliceDirectoryPath } from "../lib/buildSliceDirectoryPath";
|
|
11
11
|
import { checkIsTypeScriptProject } from "../lib/checkIsTypeScriptProject";
|
|
12
|
-
import {
|
|
12
|
+
import { getJSFileExtension } from "../lib/getJSFileExtension";
|
|
13
13
|
import { pascalCase } from "../lib/pascalCase";
|
|
14
14
|
import { rejectIfNecessary } from "../lib/rejectIfNecessary";
|
|
15
15
|
import { updateSliceModelFile } from "../lib/updateSliceModelFile";
|
|
@@ -24,10 +24,8 @@ type Args = {
|
|
|
24
24
|
} & SliceMachineContext<PluginOptions>;
|
|
25
25
|
|
|
26
26
|
const createComponentFile = async ({ dir, data, helpers, options }: Args) => {
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
`index.${await getJSOrTSXFileExtension({ helpers, options })}`,
|
|
30
|
-
);
|
|
27
|
+
const extension = await getJSFileExtension({ helpers, options, jsx: true });
|
|
28
|
+
const filePath = path.join(dir, `index.${extension}`);
|
|
31
29
|
const model = data.model;
|
|
32
30
|
const pascalName = pascalCase(model.name);
|
|
33
31
|
|
|
@@ -7,7 +7,7 @@ import { source } from "common-tags";
|
|
|
7
7
|
import { createRequire } from "node:module";
|
|
8
8
|
import fetch, { Response } from "node-fetch";
|
|
9
9
|
|
|
10
|
-
import {
|
|
10
|
+
import { getJSFileExtension } from "../lib/getJSFileExtension";
|
|
11
11
|
|
|
12
12
|
import type { PluginOptions } from "../types";
|
|
13
13
|
|
|
@@ -79,10 +79,8 @@ const createStep2 = async ({
|
|
|
79
79
|
helpers,
|
|
80
80
|
options,
|
|
81
81
|
}: Args): Promise<SliceSimulatorSetupStep> => {
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
options,
|
|
85
|
-
})}`;
|
|
82
|
+
const extension = await getJSFileExtension({ helpers, options, jsx: true });
|
|
83
|
+
const fileName = `slice-simulator.${extension}`;
|
|
86
84
|
const filePath = helpers.joinPathFromRoot("pages", fileName);
|
|
87
85
|
|
|
88
86
|
const fileContents = await helpers.format(
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { SliceMachineContext } from "@slicemachine/plugin-kit";
|
|
2
|
+
|
|
3
|
+
import { PluginOptions } from "../types";
|
|
4
|
+
import { checkHasSrcDirectory } from "./checkHasSrcDirectory";
|
|
5
|
+
import { checkPathExists } from "./checkPathExists";
|
|
6
|
+
|
|
7
|
+
type CheckHasAppRouterArgs = Pick<
|
|
8
|
+
SliceMachineContext<PluginOptions>,
|
|
9
|
+
"helpers"
|
|
10
|
+
>;
|
|
11
|
+
|
|
12
|
+
export async function checkHasAppRouter(
|
|
13
|
+
args: CheckHasAppRouterArgs,
|
|
14
|
+
): Promise<boolean> {
|
|
15
|
+
const hasSrcDirectory = await checkHasSrcDirectory({ helpers: args.helpers });
|
|
16
|
+
|
|
17
|
+
return await checkPathExists(
|
|
18
|
+
hasSrcDirectory
|
|
19
|
+
? args.helpers.joinPathFromRoot("src", "app")
|
|
20
|
+
: args.helpers.joinPathFromRoot("app"),
|
|
21
|
+
);
|
|
22
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { SliceMachineContext } from "@slicemachine/plugin-kit";
|
|
2
|
+
|
|
3
|
+
import { PluginOptions } from "../types";
|
|
4
|
+
import { checkPathExists } from "./checkPathExists";
|
|
5
|
+
|
|
6
|
+
type CheckHasSrcDirectoryArgs = Pick<
|
|
7
|
+
SliceMachineContext<PluginOptions>,
|
|
8
|
+
"helpers"
|
|
9
|
+
>;
|
|
10
|
+
|
|
11
|
+
export async function checkHasSrcDirectory(
|
|
12
|
+
args: CheckHasSrcDirectoryArgs,
|
|
13
|
+
): Promise<boolean> {
|
|
14
|
+
return await checkPathExists(args.helpers.joinPathFromRoot("src"));
|
|
15
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { SliceMachineContext } from "@slicemachine/plugin-kit";
|
|
2
|
+
|
|
3
|
+
import { PluginOptions } from "../types";
|
|
4
|
+
|
|
5
|
+
import { checkIsTypeScriptProject } from "./checkIsTypeScriptProject";
|
|
6
|
+
|
|
7
|
+
type GetJSFileExtensionArgs = Pick<
|
|
8
|
+
SliceMachineContext<PluginOptions>,
|
|
9
|
+
"helpers" | "options"
|
|
10
|
+
> & {
|
|
11
|
+
jsx?: boolean;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const getJSFileExtension = async ({
|
|
15
|
+
helpers,
|
|
16
|
+
options,
|
|
17
|
+
jsx = false,
|
|
18
|
+
}: GetJSFileExtensionArgs): Promise<string> => {
|
|
19
|
+
const isTypeScriptProject = await checkIsTypeScriptProject({
|
|
20
|
+
helpers,
|
|
21
|
+
options,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
if (isTypeScriptProject) {
|
|
25
|
+
return jsx ? "tsx" : "ts";
|
|
26
|
+
} else {
|
|
27
|
+
return jsx && options.jsxExtension ? "jsx" : "js";
|
|
28
|
+
}
|
|
29
|
+
};
|
|
@@ -21,7 +21,9 @@ export const upsertGlobalContentTypes = async ({
|
|
|
21
21
|
options,
|
|
22
22
|
project,
|
|
23
23
|
}: UpsertGlobalTypesArgs): Promise<void> => {
|
|
24
|
-
const filePath = helpers.joinPathFromRoot(
|
|
24
|
+
const filePath = helpers.joinPathFromRoot(
|
|
25
|
+
options.generatedTypesFilePath || "prismicio-types.d.ts",
|
|
26
|
+
);
|
|
25
27
|
|
|
26
28
|
const [customTypeModelDescriptors, sharedSliceModelDescriptors] =
|
|
27
29
|
await Promise.all([
|