prismic 1.1.0 → 1.2.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.
Files changed (48) hide show
  1. package/dist/{builders-hKD4IrLX-ClhMQQkN.mjs → builders-hKD4IrLX-BrpqCAS2.mjs} +1 -1
  2. package/dist/index.mjs +115 -101
  3. package/dist/nextjs-HiDO_p-p.mjs +318 -0
  4. package/dist/nuxt-CDrqbn0o.mjs +59 -0
  5. package/dist/string-BUjs_2AH.mjs +7 -0
  6. package/dist/{sveltekit-BMDXAfYz.mjs → sveltekit-Qx8xJZOd.mjs} +45 -35
  7. package/package.json +1 -1
  8. package/src/adapters/index.ts +177 -0
  9. package/src/{frameworks → adapters}/nextjs.templates.ts +102 -102
  10. package/src/adapters/nextjs.ts +211 -0
  11. package/src/adapters/nuxt.ts +232 -0
  12. package/src/adapters/sveltekit.ts +226 -0
  13. package/src/clients/core.ts +104 -0
  14. package/src/clients/wroom.ts +111 -0
  15. package/src/commands/init.ts +57 -69
  16. package/src/commands/login.ts +12 -29
  17. package/src/commands/logout.ts +8 -28
  18. package/src/commands/preview-add.ts +57 -0
  19. package/src/commands/preview-list.ts +54 -0
  20. package/src/commands/preview-remove.ts +51 -0
  21. package/src/commands/preview-set-simulator.ts +60 -0
  22. package/src/commands/preview.ts +28 -0
  23. package/src/commands/sync.ts +49 -87
  24. package/src/commands/webhook-create.ts +89 -0
  25. package/src/commands/webhook-disable.ts +60 -0
  26. package/src/commands/webhook-enable.ts +60 -0
  27. package/src/commands/webhook-list.ts +43 -0
  28. package/src/commands/webhook-remove.ts +52 -0
  29. package/src/commands/webhook-set-triggers.ts +93 -0
  30. package/src/commands/webhook-view.ts +65 -0
  31. package/src/commands/webhook.ts +43 -0
  32. package/src/commands/whoami.ts +7 -28
  33. package/src/config.ts +2 -11
  34. package/src/index.ts +122 -105
  35. package/src/lib/command.ts +188 -0
  36. package/src/lib/file.ts +13 -1
  37. package/src/lib/packageJson.ts +70 -1
  38. package/src/lib/segment.ts +26 -30
  39. package/src/project.ts +54 -0
  40. package/dist/frameworks-DtGBrEuY.mjs +0 -17
  41. package/dist/nextjs-2qjzSaQI.mjs +0 -312
  42. package/dist/nuxt-DKsgbqpV.mjs +0 -59
  43. package/src/frameworks/index.ts +0 -398
  44. package/src/frameworks/nextjs.ts +0 -211
  45. package/src/frameworks/nuxt.ts +0 -252
  46. package/src/frameworks/sveltekit.ts +0 -234
  47. /package/src/{frameworks → adapters}/nuxt.templates.ts +0 -0
  48. /package/src/{frameworks → adapters}/sveltekit.templates.ts +0 -0
@@ -1,398 +0,0 @@
1
- import type { CustomType, SharedSlice } from "@prismicio/types-internal/lib/customtypes";
2
-
3
- import { pascalCase } from "change-case";
4
- import { readFile, rename, rm } from "node:fs/promises";
5
- import { relative } from "node:path";
6
- import { fileURLToPath, pathToFileURL } from "node:url";
7
- import { glob } from "tinyglobby";
8
- import * as z from "zod/mini";
9
-
10
- import { readConfig } from "../config";
11
- import { exists, findUpward, writeFileRecursive } from "../lib/file";
12
- import { stringify } from "../lib/json";
13
- import { addDependencies } from "../lib/packageJson";
14
- import { dedent } from "../lib/string";
15
- import { appendTrailingSlash } from "../lib/url";
16
-
17
- export abstract class FrameworkAdapter {
18
- abstract readonly id: Framework;
19
-
20
- abstract getDependencies(): Promise<Record<string, string>>;
21
-
22
- abstract getClientFilePath(): Promise<string | null>;
23
-
24
- abstract getSlicesDirectoryPath(): Promise<string>;
25
-
26
- abstract getSliceComponentExtensions(): string[];
27
-
28
- abstract getRoutePath(route: string): Promise<{ path: string; extensions: string[] } | null>;
29
-
30
- abstract createSliceComponent(
31
- model: SharedSlice,
32
- sliceDirectory: URL,
33
- ): Promise<{ componentPath: URL }>;
34
-
35
- abstract getSliceImportPath(relativeDirectory: string): string;
36
-
37
- abstract getDefaultSliceLibraryPath(projectRoot: URL): Promise<URL>;
38
-
39
- async initProject(): Promise<void> {
40
- const deps = await this.getDependencies();
41
- await addDependencies(deps);
42
- const libraries = await this.#getSliceLibraries();
43
- for (const library of libraries) {
44
- await this.#updateSliceLibraryIndexFile(library);
45
- }
46
- }
47
-
48
- async createSlice(
49
- model: SharedSlice,
50
- library: URL,
51
- ): Promise<{ modelPath: URL; componentPath: URL; indexPath: URL }> {
52
- const { modelPath } = await this.#writeSliceModel(model, library);
53
- const sliceDirectory = await this.#getSliceDirectory(model.name, library);
54
- const { componentPath } = await this.createSliceComponent(model, sliceDirectory);
55
- const { indexPath } = await this.#updateSliceLibraryIndexFile(library);
56
- return { modelPath, componentPath, indexPath };
57
- }
58
-
59
- async readSlice(sliceId: string): Promise<SharedSlice> {
60
- const slice = await this.#findSlice(sliceId);
61
- return slice.model;
62
- }
63
-
64
- async updateSlice(model: SharedSlice): Promise<{ modelPath: URL; indexPath: URL }> {
65
- const existingSlice = await this.#findSlice(model.id);
66
- const { modelPath } = await this.#writeSliceModel(model, existingSlice.library);
67
- const { indexPath } = await this.#updateSliceLibraryIndexFile(existingSlice.library);
68
- return { modelPath, indexPath };
69
- }
70
-
71
- async renameSlice(model: SharedSlice): Promise<{ modelPath: URL; indexPath: URL }> {
72
- const existingSlice = await this.#findSlice(model.id);
73
- const newSliceDirectory = await this.#getSliceDirectory(model.name, existingSlice.library);
74
- await rename(existingSlice.directory, newSliceDirectory);
75
- const { modelPath } = await this.#writeSliceModel(model, existingSlice.library);
76
- const { indexPath } = await this.#updateSliceLibraryIndexFile(existingSlice.library);
77
- return { modelPath, indexPath };
78
- }
79
-
80
- async deleteSlice(sliceId: string): Promise<{ sliceDirectory: URL; indexPath: URL }> {
81
- const slice = await this.#findSlice(sliceId);
82
- await rm(slice.directory, { recursive: true });
83
- const { indexPath } = await this.#updateSliceLibraryIndexFile(slice.library);
84
- return { sliceDirectory: slice.directory, indexPath };
85
- }
86
-
87
- async getSlices(library?: URL): Promise<{ library: URL; directory: URL; model: SharedSlice }[]> {
88
- const libraryDirs = library ? [library] : await this.#getSliceLibraries();
89
- const allSlices: {
90
- library: URL;
91
- directory: URL;
92
- model: SharedSlice;
93
- }[] = [];
94
-
95
- for (const libraryDir of libraryDirs) {
96
- const modelGlob = new URL("*/model.json", libraryDir);
97
- const sliceModelPaths = Array.from(
98
- await glob(fileURLToPath(modelGlob), { absolute: true }),
99
- (path) => pathToFileURL(path),
100
- );
101
- const slices = await Promise.all(
102
- sliceModelPaths.map(async (sliceModelPath) => {
103
- const directory = new URL(".", sliceModelPath);
104
- const model = await readFile(sliceModelPath, "utf8");
105
- return {
106
- library: libraryDir,
107
- directory,
108
- model: JSON.parse(model),
109
- };
110
- }),
111
- );
112
- allSlices.push(...slices);
113
- }
114
-
115
- return allSlices.sort((a, b) =>
116
- a.model.id.toLowerCase().localeCompare(b.model.id.toLowerCase()),
117
- );
118
- }
119
-
120
- async getDefaultSliceLibrary(): Promise<URL> {
121
- const dirs = await this.#getSliceLibraries();
122
- return dirs[0];
123
- }
124
-
125
- async createCustomType(model: CustomType): Promise<{ modelPath: URL }> {
126
- const { modelPath } = await this.#writeCustomType(model);
127
- return { modelPath };
128
- }
129
-
130
- async readCustomType(customTypeId: string): Promise<CustomType> {
131
- const customTypeDirectory = await this.#getCustomTypeDirectory(customTypeId);
132
- const modelPath = new URL("index.json", customTypeDirectory);
133
- const model = await readFile(modelPath, "utf8");
134
- const json = JSON.parse(model);
135
- return json;
136
- }
137
-
138
- async updateCustomType(model: CustomType): Promise<{ modelPath: URL }> {
139
- const { modelPath } = await this.#writeCustomType(model);
140
- return { modelPath };
141
- }
142
-
143
- async renameCustomType(model: CustomType): Promise<{ modelPath: URL }> {
144
- const existingCustomTypeDirectory = await this.#getCustomTypeDirectory(model.id);
145
- const newCustomTypeDirectory = await this.#getCustomTypeDirectory(model.id);
146
- await rename(existingCustomTypeDirectory, newCustomTypeDirectory);
147
- const { modelPath } = await this.#writeCustomType(model);
148
- return { modelPath };
149
- }
150
-
151
- async deleteCustomType(customTypeId: string): Promise<{ customTypeDirectory: URL }> {
152
- const customTypeDirectory = await this.#getCustomTypeDirectory(customTypeId);
153
- await rm(customTypeDirectory, { recursive: true });
154
- return { customTypeDirectory };
155
- }
156
-
157
- async getCustomTypes(): Promise<{ directory: URL; model: CustomType }[]> {
158
- const customTypesDirectory = await this.#getCustomTypesDirectory();
159
- const modelGlob = new URL("*/index.json", customTypesDirectory);
160
- const customTypeModelPaths = Array.from(
161
- await glob(fileURLToPath(modelGlob), { absolute: true }),
162
- (path) => pathToFileURL(path),
163
- );
164
- const customTypes = await Promise.all(
165
- customTypeModelPaths.map(async (customTypeModelPath) => {
166
- const directory = new URL(".", customTypeModelPath);
167
- const model = await readFile(customTypeModelPath, "utf8");
168
- return {
169
- directory,
170
- model: JSON.parse(model),
171
- };
172
- }),
173
- );
174
- return customTypes.sort((a, b) =>
175
- a.model.id.toLowerCase().localeCompare(b.model.id.toLowerCase()),
176
- );
177
- }
178
-
179
- async getProjectRoot(): Promise<URL> {
180
- const packageJsonPath = await findUpward("package.json");
181
- if (!packageJsonPath) {
182
- throw new Error("No package.json found");
183
- }
184
- const projectRoot = new URL("./", packageJsonPath);
185
- return projectRoot;
186
- }
187
-
188
- protected async checkIsTypeScriptProject(): Promise<boolean> {
189
- const projectRoot = await this.getProjectRoot();
190
- const tsconfigPath = new URL("tsconfig.json", projectRoot);
191
- const isTypeScriptProject = await exists(tsconfigPath);
192
- return isTypeScriptProject;
193
- }
194
-
195
- protected async getJsFileExtension(): Promise<string> {
196
- const isTypeScriptProject = await this.checkIsTypeScriptProject();
197
- const jsFileExtension = isTypeScriptProject ? "ts" : "js";
198
- return jsFileExtension;
199
- }
200
-
201
- async #findSlice(sliceId: string): Promise<{ library: URL; directory: URL; model: SharedSlice }> {
202
- const slices = await this.getSlices();
203
- const slice = slices.find((slice) => slice.model.id === sliceId);
204
- if (!slice) throw new Error(`No slice found with ID: ${sliceId}`);
205
- return slice;
206
- }
207
-
208
- async #writeSliceModel(model: SharedSlice, library: URL): Promise<{ modelPath: URL }> {
209
- const sliceDirectory = await this.#getSliceDirectory(model.name, library);
210
- const modelPath = new URL("model.json", sliceDirectory);
211
- const formattedModel = this.#formatModel(model);
212
- await writeFileRecursive(modelPath, formattedModel);
213
- return { modelPath };
214
- }
215
-
216
- async #getSliceDirectory(sliceName: string, library: URL): Promise<URL> {
217
- const sliceDirectoryName = pascalCase(sliceName);
218
- const sliceDirectory = appendTrailingSlash(new URL(sliceDirectoryName, library));
219
- return sliceDirectory;
220
- }
221
-
222
- async #getSliceLibraries(): Promise<URL[]> {
223
- const projectRoot = await this.getProjectRoot();
224
- const config = await readConfig();
225
- const sliceLibraries = config.libraries ?? [];
226
- if (sliceLibraries.length < 1) {
227
- return [await this.getDefaultSliceLibraryPath(projectRoot)];
228
- }
229
- return sliceLibraries.map((sliceLibrary) => {
230
- const withoutLeadingSlash = sliceLibrary.replace(/^\//, "");
231
- return appendTrailingSlash(new URL(withoutLeadingSlash, projectRoot));
232
- });
233
- }
234
-
235
- async #updateSliceLibraryIndexFile(library: URL): Promise<{ indexPath: URL }> {
236
- const slices = await this.getSlices(library);
237
- const contents = await this.generateSliceLibraryIndexContents(slices);
238
- const extension = await this.getJsFileExtension();
239
- const filename = `index.${extension}`;
240
- const indexPath = new URL(filename, library);
241
- await writeFileRecursive(indexPath, contents);
242
- return { indexPath };
243
- }
244
-
245
- protected async generateSliceLibraryIndexContents(
246
- slices: { library: URL; directory: URL; model: SharedSlice }[],
247
- ): Promise<string> {
248
- const imports = slices.map((slice) => {
249
- const componentName = pascalCase(slice.model.name);
250
- const relativeDirectory = relative(
251
- fileURLToPath(slice.library),
252
- fileURLToPath(slice.directory),
253
- );
254
- return `import ${componentName} from "${this.getSliceImportPath(relativeDirectory)}";`;
255
- });
256
- const componentLines = slices.map((slice) => {
257
- const componentName = pascalCase(slice.model.name);
258
- return `${slice.model.id}: ${componentName}`;
259
- });
260
- return dedent`
261
- // Code generated by Prismic. DO NOT EDIT.
262
-
263
- ${imports.join("\n")}
264
-
265
- export const components = {
266
- ${componentLines.join(",\n")}
267
- };
268
- `;
269
- }
270
-
271
- async #writeCustomType(model: CustomType): Promise<{ modelPath: URL }> {
272
- const customTypeDirectory = await this.#getCustomTypeDirectory(model.id);
273
- const modelPath = new URL("index.json", customTypeDirectory);
274
- const formattedModel = this.#formatModel(model);
275
- await writeFileRecursive(modelPath, formattedModel);
276
- return { modelPath };
277
- }
278
-
279
- async #getCustomTypeDirectory(customTypeId: string): Promise<URL> {
280
- const customTypesDirectory = await this.#getCustomTypesDirectory();
281
- const customTypeDirectoryName = customTypeId;
282
- const customTypeDirectory = appendTrailingSlash(
283
- new URL(customTypeDirectoryName, customTypesDirectory),
284
- );
285
- return customTypeDirectory;
286
- }
287
-
288
- async #getCustomTypesDirectory(): Promise<URL> {
289
- const projectRoot = await this.getProjectRoot();
290
- const customTypesDirectory = new URL(`customtypes/`, projectRoot);
291
- return customTypesDirectory;
292
- }
293
-
294
- #formatModel(model: CustomType | SharedSlice): string {
295
- const formattedModel = stringify(model);
296
- return formattedModel;
297
- }
298
- }
299
-
300
- export function getDocsPath(framework: Framework): string {
301
- switch (framework) {
302
- case "next":
303
- return "nextjs/with-cli";
304
- case "nuxt":
305
- return "nuxt/with-cli";
306
- case "sveltekit":
307
- return "sveltekit/with-cli";
308
- }
309
- }
310
-
311
- export function getWriteComponentsAnchor(framework: Framework): string {
312
- switch (framework) {
313
- case "nuxt":
314
- return "#write-vue-components";
315
- case "sveltekit":
316
- return "#write-svelte-components";
317
- default:
318
- return "#write-react-components";
319
- }
320
- }
321
-
322
- export function getClientSetupAnchor(framework: Framework): string {
323
- switch (framework) {
324
- case "nuxt":
325
- return "#configure-the-modules-prismic-client";
326
- default:
327
- return "#set-up-a-prismic-client";
328
- }
329
- }
330
-
331
- export function getPreviewSetupAnchor(framework: Framework): string {
332
- switch (framework) {
333
- case "next":
334
- return "#set-up-previews-in-next-js";
335
- case "sveltekit":
336
- return "#set-up-previews-in-sveltekit";
337
- default:
338
- return "";
339
- }
340
- }
341
-
342
- const PackageJsonSchema = z.object({
343
- dependencies: z.optional(z.record(z.string(), z.string())),
344
- devDependencies: z.optional(z.record(z.string(), z.string())),
345
- });
346
-
347
- export type Framework = "next" | "nuxt" | "sveltekit";
348
-
349
- export async function requireFramework(): Promise<FrameworkAdapter> {
350
- const framework = await getFramework();
351
- if (!framework) throw new NoSupportedFrameworkError();
352
- return framework;
353
- }
354
-
355
- export class NoSupportedFrameworkError extends Error {
356
- message = "No supported framework found (Next.js, Nuxt, or SvelteKit required)";
357
- }
358
-
359
- export async function getFramework(): Promise<FrameworkAdapter | undefined> {
360
- const id = await detectFramework();
361
- switch (id) {
362
- case "next": {
363
- const { NextJsFramework } = await import("./nextjs");
364
- return new NextJsFramework();
365
- }
366
- case "nuxt": {
367
- const { NuxtFramework } = await import("./nuxt");
368
- return new NuxtFramework();
369
- }
370
- case "sveltekit": {
371
- const { SvelteKitFramework } = await import("./sveltekit");
372
- return new SvelteKitFramework();
373
- }
374
- default:
375
- return undefined;
376
- }
377
- }
378
-
379
- async function detectFramework(): Promise<Framework | undefined> {
380
- const packageJsonPath = await findUpward("package.json");
381
- if (!packageJsonPath) return undefined;
382
-
383
- try {
384
- const contents = await readFile(packageJsonPath, "utf8");
385
- const { dependencies = {}, devDependencies = {} } = z.parse(
386
- PackageJsonSchema,
387
- JSON.parse(contents),
388
- );
389
- const allDeps = { ...dependencies, ...devDependencies };
390
- if ("next" in allDeps) return "next";
391
- if ("nuxt" in allDeps) return "nuxt";
392
- if ("@sveltejs/kit" in allDeps) return "sveltekit";
393
- } catch {
394
- // Continue with undefined
395
- }
396
-
397
- return undefined;
398
- }
@@ -1,211 +0,0 @@
1
- import type { SharedSlice } from "@prismicio/types-internal/lib/customtypes";
2
-
3
- import { createRequire } from "node:module";
4
-
5
- import type { Framework } from ".";
6
-
7
- import { FrameworkAdapter } from ".";
8
- import { exists, writeFileRecursive } from "../lib/file";
9
- import { getNpmPackageVersion } from "../lib/packageJson";
10
- import {
11
- exitPreviewRouteTemplate,
12
- previewRouteTemplate,
13
- prismicIOFileTemplate,
14
- revalidateRouteTemplate,
15
- sliceSimulatorPageTemplate,
16
- sliceTemplate,
17
- } from "./nextjs.templates";
18
-
19
- export class NextJsFramework extends FrameworkAdapter {
20
- readonly id: Framework = "next";
21
-
22
- async getDependencies(): Promise<Record<string, string>> {
23
- return {
24
- "@prismicio/client": `^${await getNpmPackageVersion("@prismicio/client")}`,
25
- "@prismicio/react": `^${await getNpmPackageVersion("@prismicio/react")}`,
26
- "@prismicio/next": `^${await getNpmPackageVersion("@prismicio/next")}`,
27
- };
28
- }
29
-
30
- async initProject(): Promise<void> {
31
- await super.initProject();
32
-
33
- await this.#createPrismicIOFile();
34
- await this.#createSliceSimulatorPage();
35
- await this.#createPreviewRoute();
36
- await this.#createExitPreviewRoute();
37
- await this.#createRevalidateRoute();
38
- }
39
-
40
- async createSliceComponent(
41
- model: SharedSlice,
42
- sliceDirectory: URL,
43
- ): Promise<{ componentPath: URL }> {
44
- const extension = await this.getJsFileExtension();
45
- const componentPath = new URL(`index.${extension}x`, sliceDirectory);
46
- const contents = sliceTemplate({
47
- name: model.name,
48
- typescript: await this.checkIsTypeScriptProject(),
49
- });
50
- await writeFileRecursive(componentPath, contents);
51
- return { componentPath };
52
- }
53
-
54
- getSliceImportPath(relativeDirectory: string): string {
55
- return `./${relativeDirectory}`;
56
- }
57
-
58
- async getDefaultSliceLibraryPath(projectRoot: URL): Promise<URL> {
59
- const srcDirectory = new URL("src/", projectRoot);
60
- const hasSrcDirectory = await exists(srcDirectory);
61
- const sourceFilesRoot = hasSrcDirectory ? srcDirectory : projectRoot;
62
- return new URL("slices/", sourceFilesRoot);
63
- }
64
-
65
- async getClientFilePath(): Promise<string | null> {
66
- const hasSrcDirectory = await this.#checkHasSrcDirectory();
67
- return hasSrcDirectory ? "src/prismicio.ts" : "prismicio.ts";
68
- }
69
-
70
- async getSlicesDirectoryPath(): Promise<string> {
71
- const hasSrcDirectory = await this.#checkHasSrcDirectory();
72
- return hasSrcDirectory ? "src/slices/" : "slices/";
73
- }
74
-
75
- getSliceComponentExtensions(): string[] {
76
- return [".tsx", ".ts", ".jsx", ".js"];
77
- }
78
-
79
- async getRoutePath(route: string): Promise<{ path: string; extensions: string[] } | null> {
80
- const hasSrcDirectory = await this.#checkHasSrcDirectory();
81
- const base = hasSrcDirectory ? "src/app" : "app";
82
- switch (route) {
83
- case "/slice-simulator":
84
- return { path: `${base}/slice-simulator/page`, extensions: [".tsx", ".ts", ".jsx", ".js"] };
85
- case "/api/preview":
86
- return { path: `${base}/api/preview/route`, extensions: [".ts", ".js"] };
87
- case "/api/exit-preview":
88
- return { path: `${base}/api/exit-preview/route`, extensions: [".ts", ".js"] };
89
- case "/api/revalidate":
90
- return { path: `${base}/api/revalidate/route`, extensions: [".ts", ".js"] };
91
- default:
92
- return null;
93
- }
94
- }
95
-
96
- async #checkHasAppRouter(): Promise<boolean> {
97
- const appPath = await this.#buildSrcPath("app");
98
- return await exists(appPath);
99
- }
100
-
101
- async #checkHasSrcDirectory(): Promise<boolean> {
102
- const projectRoot = await this.getProjectRoot();
103
- return await exists(new URL("src/", projectRoot));
104
- }
105
-
106
- async #getNextJSVersion(): Promise<string> {
107
- const projectRoot = await this.getProjectRoot();
108
- const require = createRequire(new URL("package.json", projectRoot));
109
- const { version } = require("next/package.json");
110
- return version;
111
- }
112
-
113
- async #buildSrcPath(filename: string): Promise<URL> {
114
- const projectRoot = await this.getProjectRoot();
115
- const hasSrcDirectory = await this.#checkHasSrcDirectory();
116
- const prefix = hasSrcDirectory ? "src/" : "";
117
- return new URL(`${prefix}${filename}`, projectRoot);
118
- }
119
-
120
- async #createPrismicIOFile(): Promise<void> {
121
- const extension = await this.getJsFileExtension();
122
- const filePath = await this.#buildSrcPath(`prismicio.${extension}`);
123
-
124
- if (await exists(filePath)) {
125
- return;
126
- }
127
-
128
- const typescript = await this.checkIsTypeScriptProject();
129
- const appRouter = await this.#checkHasAppRouter();
130
- const hasSrcDirectory = await this.#checkHasSrcDirectory();
131
-
132
- const contents = prismicIOFileTemplate({
133
- typescript,
134
- appRouter,
135
- hasSrcDirectory,
136
- });
137
- await writeFileRecursive(filePath, contents);
138
- }
139
-
140
- async #createSliceSimulatorPage(): Promise<void> {
141
- const appRouter = await this.#checkHasAppRouter();
142
- const typescript = await this.checkIsTypeScriptProject();
143
- const extension = `${await this.getJsFileExtension()}x`;
144
- const filename = appRouter
145
- ? `app/slice-simulator/page.${extension}`
146
- : `pages/slice-simulator.${extension}`;
147
- const filePath = await this.#buildSrcPath(filename);
148
-
149
- if (await exists(filePath)) {
150
- return;
151
- }
152
-
153
- const contents = sliceSimulatorPageTemplate({ typescript, appRouter });
154
- await writeFileRecursive(filePath, contents);
155
- }
156
-
157
- async #createPreviewRoute(): Promise<void> {
158
- const appRouter = await this.#checkHasAppRouter();
159
- const typescript = await this.checkIsTypeScriptProject();
160
- const extension = await this.getJsFileExtension();
161
- const filename = appRouter
162
- ? `app/api/preview/route.${extension}`
163
- : `pages/api/preview.${extension}`;
164
- const filePath = await this.#buildSrcPath(filename);
165
-
166
- if (await exists(filePath)) {
167
- return;
168
- }
169
-
170
- const contents = previewRouteTemplate({ typescript, appRouter });
171
- await writeFileRecursive(filePath, contents);
172
- }
173
-
174
- async #createExitPreviewRoute(): Promise<void> {
175
- const appRouter = await this.#checkHasAppRouter();
176
- const typescript = await this.checkIsTypeScriptProject();
177
- const extension = await this.getJsFileExtension();
178
- const filename = appRouter
179
- ? `app/api/exit-preview/route.${extension}`
180
- : `pages/api/exit-preview.${extension}`;
181
- const filePath = await this.#buildSrcPath(filename);
182
-
183
- if (await exists(filePath)) {
184
- return;
185
- }
186
-
187
- const contents = exitPreviewRouteTemplate({ typescript, appRouter });
188
- await writeFileRecursive(filePath, contents);
189
- }
190
-
191
- async #createRevalidateRoute(): Promise<void> {
192
- const appRouter = await this.#checkHasAppRouter();
193
- if (!appRouter) {
194
- return;
195
- }
196
-
197
- const extension = await this.getJsFileExtension();
198
- const filePath = await this.#buildSrcPath(`app/api/revalidate/route.${extension}`);
199
-
200
- if (await exists(filePath)) {
201
- return;
202
- }
203
-
204
- const version = await this.#getNextJSVersion();
205
- const major = Number.parseInt(version.split(".")[0]);
206
- const supportsCacheLife = major >= 16;
207
-
208
- const contents = revalidateRouteTemplate({ supportsCacheLife });
209
- await writeFileRecursive(filePath, contents);
210
- }
211
- }