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,4 +1,4 @@
1
- import{_ as e,a as t,b as n,o as r,t as i,y as a}from"./frameworks-DtGBrEuY.mjs";import{n as o}from"./builders-hKD4IrLX-ClhMQQkN.mjs";import{createRequire as s}from"node:module";import{writeFile as c}from"node:fs/promises";function l(e){let{typescript:n}=e;return n?t`
1
+ import{C as e,T as t,a as n,b as r,n as i,o as a,t as o,v as s,w as c,y as l,z as u}from"./string-BUjs_2AH.mjs";import{n as d}from"./builders-hKD4IrLX-BrpqCAS2.mjs";import{createRequire as f}from"node:module";import{writeFile as p}from"node:fs/promises";import{fileURLToPath as m}from"node:url";import{relative as h}from"node:path";function g(e){let{typescript:t}=e;return t?o`
2
2
  import { createClient as baseCreateClient, type Route } from "@prismicio/client";
3
3
  import { type CreateClientConfig, enableAutoPreviews } from '@prismicio/svelte/kit';
4
4
  import prismicConfig from "../../prismic.config.json";
@@ -39,7 +39,7 @@ import{_ as e,a as t,b as n,o as r,t as i,y as a}from"./frameworks-DtGBrEuY.mjs"
39
39
 
40
40
  return client;
41
41
  };
42
- `:t`
42
+ `:o`
43
43
  import { createClient as baseCreateClient } from "@prismicio/client";
44
44
  import { enableAutoPreviews } from '@prismicio/svelte/kit';
45
45
  import prismicConfig from "../../prismic.config.json";
@@ -82,7 +82,7 @@ import{_ as e,a as t,b as n,o as r,t as i,y as a}from"./frameworks-DtGBrEuY.mjs"
82
82
 
83
83
  return client;
84
84
  };
85
- `}function u(e){let{version:n}=e,r=t`
85
+ `}function _(e){let{version:t}=e,n=o`
86
86
  <script>
87
87
  import { SliceSimulator, SliceZone } from '@prismicio/svelte';
88
88
  import { components } from '$lib/slices';
@@ -92,7 +92,7 @@ import{_ as e,a as t,b as n,o as r,t as i,y as a}from"./frameworks-DtGBrEuY.mjs"
92
92
  <SliceSimulator let:slices>
93
93
  <SliceZone {slices} {components} />
94
94
  </SliceSimulator>
95
- `,i=t`
95
+ `,r=o`
96
96
  <script>
97
97
  import { SliceSimulator, SliceZone } from '@prismicio/svelte';
98
98
  import { components } from '$lib/slices';
@@ -101,7 +101,7 @@ import{_ as e,a as t,b as n,o as r,t as i,y as a}from"./frameworks-DtGBrEuY.mjs"
101
101
  <SliceSimulator let:slices>
102
102
  <SliceZone {slices} {components} />
103
103
  </SliceSimulator>
104
- `;return n<=4?i:r}function d(e){let{typescript:n}=e;return n?t`
104
+ `;return t<=4?r:n}function v(e){let{typescript:t}=e;return t?o`
105
105
  import { redirectToPreviewURL } from '@prismicio/svelte/kit';
106
106
  import { createClient } from '$lib/prismicio';
107
107
  import type { RequestHandler } from "./$types";
@@ -111,7 +111,7 @@ import{_ as e,a as t,b as n,o as r,t as i,y as a}from"./frameworks-DtGBrEuY.mjs"
111
111
 
112
112
  return await redirectToPreviewURL({ client, request, cookies });
113
113
  }
114
- `:t`
114
+ `:o`
115
115
  import { redirectToPreviewURL } from '@prismicio/svelte/kit';
116
116
  import { createClient } from '$lib/prismicio';
117
117
 
@@ -121,7 +121,7 @@ import{_ as e,a as t,b as n,o as r,t as i,y as a}from"./frameworks-DtGBrEuY.mjs"
121
121
 
122
122
  return await redirectToPreviewURL({ client, request, cookies });
123
123
  }
124
- `}function f(e){let{version:n}=e,r=t`
124
+ `}function y(e){let{version:t}=e,n=o`
125
125
  <script>
126
126
  import { isFilled, asImageSrc } from '@prismicio/client';
127
127
  import { PrismicPreview } from '@prismicio/svelte/kit';
@@ -144,7 +144,7 @@ import{_ as e,a as t,b as n,o as r,t as i,y as a}from"./frameworks-DtGBrEuY.mjs"
144
144
  </svelte:head>
145
145
  {@render children()}
146
146
  <PrismicPreview {repositoryName} />
147
- `,i=t`
147
+ `,r=o`
148
148
  <script>
149
149
  import { isFilled, asImageSrc } from '@prismicio/client';
150
150
  import { PrismicPreview } from '@prismicio/svelte/kit';
@@ -165,62 +165,72 @@ import{_ as e,a as t,b as n,o as r,t as i,y as a}from"./frameworks-DtGBrEuY.mjs"
165
165
  </svelte:head>
166
166
  <slot />
167
167
  <PrismicPreview {repositoryName} />
168
- `;return n<=4?i:r}const p=t`
168
+ `;return t<=4?r:n}const b=o`
169
169
  <section data-slice-type={slice.slice_type} data-slice-variation={slice.variation}>
170
170
  Placeholder component for {slice.slice_type} (variation: {slice.variation}) slices.
171
171
  <br />
172
172
  <strong>You can edit this slice directly in your code editor.</strong>
173
173
  </section>
174
- `;function m(e){let{name:n,typescript:i,version:a}=e,o=r(n),s=t`
174
+ `;function x(e){let{name:t,typescript:n,version:r}=e,i=u(t),a=o`
175
175
  <script lang="ts">
176
176
  import type { Content } from '@prismicio/client';
177
177
  import type { SliceComponentProps } from '@prismicio/svelte';
178
178
 
179
- type Props = SliceComponentProps<Content.${o}Slice>;
179
+ type Props = SliceComponentProps<Content.${i}Slice>;
180
180
 
181
181
  const { slice }: Props = $props();
182
182
  <\/script>
183
183
 
184
- ${p}
185
- `,c=t`
184
+ ${b}
185
+ `,s=o`
186
186
  <script>
187
187
  /* @typedef {import("@prismicio/client").Content} Content */
188
188
  /* @typedef {import("@prismicio/svelte").SliceComponentProps} SliceComponentProps */
189
189
 
190
- /* @type {SliceComponentProps<Content.${o}Slice>} */
190
+ /* @type {SliceComponentProps<Content.${i}Slice>} */
191
191
  const { slice } = $props();
192
192
  <\/script>
193
193
 
194
- ${p}
195
- `,l=t`
194
+ ${b}
195
+ `,c=o`
196
196
  <script lang="ts">
197
197
  import type { Content } from '@prismicio/client';
198
198
 
199
- export let slice: Content.${o}Slice;
199
+ export let slice: Content.${i}Slice;
200
200
  <\/script>
201
201
 
202
- ${p}
203
- `,u=t`
202
+ ${b}
203
+ `,l=o`
204
204
  <script>
205
- /** @type {import("@prismicio/client").Content.${o}Slice} */
205
+ /** @type {import("@prismicio/client").Content.${i}Slice} */
206
206
  export let slice;
207
207
  <\/script>
208
208
 
209
- ${p}
210
- `;return i?a<=4?l:s:a<=4?u:c}var h=class extends i{id=`sveltekit`;async getDependencies(){return{"@prismicio/client":`^${await e(`@prismicio/client`)}`,"@prismicio/svelte":`^${await e(`@prismicio/svelte`)}`}}async initProject(){await super.initProject(),await this.#e(),await this.#t(),await this.#n(),await this.#r(),await this.#i(),await this.#a(),await this.#o(),await this.#s()}async createSliceComponent(e,t){let r=new URL(`index.svelte`,t);return await n(r,m({name:e.name,typescript:await this.checkIsTypeScriptProject(),version:await this.#c()})),{componentPath:r}}getSliceImportPath(e){return`./${e}/index.svelte`}async getDefaultSliceLibraryPath(e){return new URL(`src/lib/slices/`,e)}async getClientFilePath(){return`src/lib/prismicio.ts`}async getSlicesDirectoryPath(){return`src/lib/slices/`}getSliceComponentExtensions(){return[`.svelte`]}async getRoutePath(e){switch(e){case`/slice-simulator`:return{path:`src/routes/slice-simulator/+page`,extensions:[`.svelte`]};case`/api/preview`:return{path:`src/routes/api/preview/+server`,extensions:[`.ts`,`.js`]};default:return null}}async#e(){let e=await this.getJsFileExtension(),t=await this.getProjectRoot(),r=new URL(`src/lib/prismicio.${e}`,t);await a(r)||await n(r,l({typescript:await this.checkIsTypeScriptProject()}))}async#t(){let e=await this.getProjectRoot(),t=new URL(`src/routes/slice-simulator/+page.svelte`,e);await a(t)||await n(t,u({version:await this.#c()}))}async#n(){let e=await this.getJsFileExtension(),r=await this.getProjectRoot(),i=new URL(`src/params/preview.${e}`,r);await a(i)||await n(i,t`
211
- export function match(param) {
212
- return param === 'preview';
213
- }
214
- `)}async#r(){let e=await this.getJsFileExtension(),t=await this.getProjectRoot(),r=new URL(`src/routes/api/preview/+server.${e}`,t);await a(r)||await n(r,d({typescript:await this.checkIsTypeScriptProject()}))}async#i(){let e=await this.getProjectRoot(),r=new URL(`src/routes/[[preview=preview]]/README.md`,e);await a(r)||await n(r,t`
215
- This directory adds support for optional \`/preview\` routes. Do not remove this directory.
209
+ ${b}
210
+ `;return n?r<=4?c:a:r<=4?l:s}var S=class extends i{id=`sveltekit`;async onProjectInitialized(){await s({"@prismicio/client":`^${await r(`@prismicio/client`)}`,"@prismicio/svelte":`^${await r(`@prismicio/svelte`)}`}),await C(),await w(),await T(),await E(),await D(),await O(),await k(),await A()}async onSliceCreated(e,r){let i=u(e.name),a=new URL(i,t(r));await c(new URL(`index.svelte`,a),x({name:e.name,typescript:await n(),version:await M()}))}onSliceUpdated(){}onSliceDeleted(){}onCustomTypeCreated(){}onCustomTypeUpdated(){}onCustomTypeDeleted(){}async createSliceIndexFile(e){let t=(await this.getSlices()).filter(t=>t.library.href===e.href),n=t.map(t=>`import ${u(t.model.name)} from "./${h(m(e),m(t.directory))}/index.svelte";`),r=t.map(e=>{let t=u(e.model.name);return`${e.model.id}: ${t}`}),i=o`
211
+ // Code generated by Prismic. DO NOT EDIT.
212
+
213
+ ${n.join(`
214
+ `)}
215
+
216
+ export const components = {
217
+ ${r.join(`,
218
+ `)}
219
+ };
220
+ `,a=`index.${await j()}`;await c(new URL(a,e),i)}async getDefaultSliceLibrary(){let e=await a();return new URL(`src/lib/slices/`,e)}};async function C(){let t=await j(),r=await a(),i=new URL(`src/lib/prismicio.${t}`,r);await e(i)||await c(i,g({typescript:await n()}))}async function w(){let t=await a(),n=new URL(`src/routes/slice-simulator/+page.svelte`,t);await e(n)||await c(n,_({version:await M()}))}async function T(){let t=await j(),n=await a(),r=new URL(`src/params/preview.${t}`,n);await e(r)||await c(r,o`
221
+ export function match(param) {
222
+ return param === 'preview';
223
+ }
224
+ `)}async function E(){let t=await j(),r=await a(),i=new URL(`src/routes/api/preview/+server.${t}`,r);await e(i)||await c(i,v({typescript:await n()}))}async function D(){let t=await a(),n=new URL(`src/routes/[[preview=preview]]/README.md`,t);await e(n)||await c(n,o`
225
+ This directory adds support for optional \`/preview\` routes. Do not remove this directory.
216
226
 
217
- All routes within this directory will be served using the following URLs:
227
+ All routes within this directory will be served using the following URLs:
218
228
 
219
- - \`/example-route\` (prerendered)
220
- - \`/preview/example-route\` (server-rendered)
229
+ - \`/example-route\` (prerendered)
230
+ - \`/preview/example-route\` (server-rendered)
221
231
 
222
- See <https://prismic.io/docs/svelte-preview> for more information.
223
- `)}async#a(){let e=await this.getJsFileExtension(),r=await this.getProjectRoot(),i=new URL(`src/routes/+layout.server.${e}`,r);await a(i)||await n(i,t`
224
- export const prerender = "auto";
225
- `)}async#o(){let e=await this.getProjectRoot(),t=new URL(`src/routes/+layout.svelte`,e);await a(t)||await n(t,f({version:await this.#c()}))}async#s(){let e=await this.getProjectRoot(),t=new URL(`vite.config.js`,e);if(await a(t)||(t=new URL(`vite.config.ts`,e)),!await a(t))return;let n=t.pathname,r=await o(n);if(r.exports.default.$type!==`function-call`)return;let i=r.exports.default.$args[0];i.server??={},i.server.fs??={},i.server.fs.allow??=[],i.server.fs.allow.includes(`./prismic.config.json`)||i.server.fs.allow.push(`./prismic.config.json`);let s=r.generate().code.replace(/\n\s*\n(?=\s*server:)/,`
226
- `);await c(t,s)}async#c(){let e=await this.getProjectRoot(),{version:t}=s(new URL(`package.json`,e))(`svelte/package.json`),n=Number.parseInt(t.split(`.`)[0]);return Number.isNaN(n)?1/0:n}};export{h as SvelteKitFramework};
232
+ See <https://prismic.io/docs/svelte-preview> for more information.
233
+ `)}async function O(){let t=await j(),n=await a(),r=new URL(`src/routes/+layout.server.${t}`,n);await e(r)||await c(r,o`
234
+ export const prerender = "auto";
235
+ `)}async function k(){let t=await a(),n=new URL(`src/routes/+layout.svelte`,t);await e(n)||await c(n,y({version:await M()}))}async function A(){let t=await a(),n=new URL(`vite.config.js`,t);if(await e(n)||(n=new URL(`vite.config.ts`,t)),!await e(n))return;let r=n.pathname,i=await d(r);if(i.exports.default.$type!==`function-call`)return;let o=i.exports.default.$args[0];o.server??={},o.server.fs??={},o.server.fs.allow??=[],o.server.fs.allow.includes(`./prismic.config.json`)||o.server.fs.allow.push(`./prismic.config.json`);let s=i.generate().code.replace(/\n\s*\n(?=\s*server:)/,`
236
+ `);await p(n,s)}async function j(){return await n()?`ts`:`js`}async function M(){let{version:e}=f(await l())(`svelte/package.json`),t=Number.parseInt(e.split(`.`)[0]);return Number.isNaN(t)?1/0:t}export{S as SvelteKitAdapter};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prismic",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Prismic's official command line tool",
5
5
  "keywords": [
6
6
  "prismic",
@@ -0,0 +1,177 @@
1
+ import type { CustomType, SharedSlice } from "@prismicio/types-internal/lib/customtypes";
2
+
3
+ import { pascalCase } from "change-case";
4
+ import { rm } from "node:fs/promises";
5
+ import { fileURLToPath, pathToFileURL } from "node:url";
6
+ import { glob } from "tinyglobby";
7
+
8
+ import { readJsonFile, writeFileRecursive } from "../lib/file";
9
+ import { stringify } from "../lib/json";
10
+ import { readPackageJson } from "../lib/packageJson";
11
+ import { appendTrailingSlash } from "../lib/url";
12
+ import { findProjectRoot, getLibraries } from "../project";
13
+
14
+ type CustomTypeMeta = { model: CustomType; directory: URL };
15
+ type SharedSliceMeta = { model: SharedSlice; directory: URL; library: URL };
16
+
17
+ export async function getAdapter(): Promise<Adapter> {
18
+ const { dependencies, devDependencies, peerDependencies } = await readPackageJson();
19
+ const allDependencies = { ...dependencies, ...devDependencies, ...peerDependencies };
20
+ if ("next" in allDependencies) {
21
+ const { NextJsAdapter } = await import("./nextjs");
22
+ return new NextJsAdapter();
23
+ }
24
+ if ("nuxt" in allDependencies) {
25
+ const { NuxtAdapter } = await import("./nuxt");
26
+ return new NuxtAdapter();
27
+ }
28
+ if ("@sveltejs/kit" in allDependencies) {
29
+ const { SvelteKitAdapter } = await import("./sveltekit");
30
+ return new SvelteKitAdapter();
31
+ }
32
+ throw new NoSupportedFrameworkError();
33
+ }
34
+
35
+ export class NoSupportedFrameworkError extends Error {
36
+ message = "No supported framework found (Next.js, Nuxt, or SvelteKit required)";
37
+ }
38
+
39
+ export abstract class Adapter {
40
+ abstract readonly id: string;
41
+
42
+ abstract onProjectInitialized(): Promise<void> | void;
43
+ abstract onSliceCreated(model: SharedSlice, library: URL): Promise<void> | void;
44
+ abstract onSliceUpdated(model: SharedSlice): Promise<void> | void;
45
+ abstract onSliceDeleted(id: string): Promise<void> | void;
46
+ abstract onCustomTypeCreated(model: CustomType): Promise<void> | void;
47
+ abstract onCustomTypeUpdated(model: CustomType): Promise<void> | void;
48
+ abstract onCustomTypeDeleted(id: string): Promise<void> | void;
49
+
50
+ abstract createSliceIndexFile(library: URL): Promise<void>;
51
+ abstract getDefaultSliceLibrary(): Promise<URL>;
52
+
53
+ async initProject(): Promise<void> {
54
+ const libraries = await this.getSliceLibraries();
55
+ for (const library of libraries) {
56
+ await this.createSliceIndexFile(library);
57
+ }
58
+ await this.onProjectInitialized();
59
+ }
60
+
61
+ async getSliceLibraries(): Promise<URL[]> {
62
+ let libraries = await getLibraries();
63
+ if (libraries) return libraries;
64
+ const defaultSliceLibrary = await this.getDefaultSliceLibrary();
65
+ return [defaultSliceLibrary];
66
+ }
67
+
68
+ async getSlices(): Promise<SharedSliceMeta[]> {
69
+ const allSlices: SharedSliceMeta[] = [];
70
+
71
+ const libraries = await this.getSliceLibraries();
72
+ for (const library of libraries) {
73
+ const modelGlob = new URL("*/model.json", library);
74
+ const sliceModelPaths = Array.from(
75
+ await glob(fileURLToPath(modelGlob), { absolute: true }),
76
+ (path) => pathToFileURL(path),
77
+ );
78
+ const slices = await Promise.all(
79
+ sliceModelPaths.map(async (sliceModelPath) => {
80
+ const directory = new URL(".", sliceModelPath);
81
+ const model = await readJsonFile<SharedSlice>(sliceModelPath);
82
+ return { library, directory, model };
83
+ }),
84
+ );
85
+ allSlices.push(...slices);
86
+ }
87
+
88
+ return allSlices.sort((a, b) =>
89
+ a.model.id.toLowerCase().localeCompare(b.model.id.toLowerCase()),
90
+ );
91
+ }
92
+
93
+ async getSlice(id: string): Promise<SharedSliceMeta> {
94
+ const slices = await this.getSlices();
95
+ const slice = slices.find((s) => s.model.id === id);
96
+ if (!slice) throw new Error(`No slice found with ID: ${id}`);
97
+ return slice;
98
+ }
99
+
100
+ async createSlice(model: SharedSlice, library?: URL): Promise<void> {
101
+ library ??= await this.getDefaultSliceLibrary();
102
+ await upsertSliceModel(model, library);
103
+ await this.onSliceCreated(model, library);
104
+ }
105
+
106
+ async updateSlice(model: SharedSlice): Promise<void> {
107
+ const slice = await this.getSlice(model.id);
108
+ await upsertSliceModel(model, slice.library);
109
+ await this.onSliceUpdated(model);
110
+ }
111
+
112
+ async deleteSlice(id: string): Promise<void> {
113
+ const slice = await this.getSlice(id);
114
+ await rm(slice.directory, { recursive: true });
115
+ await this.createSliceIndexFile(slice.library);
116
+ await this.onSliceDeleted(id);
117
+ }
118
+
119
+ async getCustomTypes(): Promise<CustomTypeMeta[]> {
120
+ const projectRoot = await findProjectRoot();
121
+ const customTypesDirectory = new URL("customtypes/", projectRoot);
122
+ const modelGlob = new URL("*/index.json", customTypesDirectory);
123
+ const customTypeModelPaths = Array.from(
124
+ await glob(fileURLToPath(modelGlob), { absolute: true }),
125
+ (path) => pathToFileURL(path),
126
+ );
127
+
128
+ const customTypes = await Promise.all(
129
+ customTypeModelPaths.map(async (customTypeModelPath) => {
130
+ const directory = new URL(".", customTypeModelPath);
131
+ const model = await readJsonFile<CustomType>(customTypeModelPath);
132
+ return { directory, model };
133
+ }),
134
+ );
135
+
136
+ return customTypes.sort((a, b) =>
137
+ a.model.id.toLowerCase().localeCompare(b.model.id.toLowerCase()),
138
+ );
139
+ }
140
+
141
+ async getCustomType(id: string): Promise<CustomTypeMeta> {
142
+ const customTypes = await this.getCustomTypes();
143
+ const customType = customTypes.find((s) => s.model.id === id);
144
+ if (!customType) throw new Error(`No custom type found with ID: ${id}`);
145
+ return customType;
146
+ }
147
+
148
+ async createCustomType(model: CustomType): Promise<void> {
149
+ await upsertCustomTypeModel(model);
150
+ await this.onCustomTypeCreated(model);
151
+ }
152
+
153
+ async updateCustomType(model: CustomType): Promise<void> {
154
+ await upsertCustomTypeModel(model);
155
+ await this.onCustomTypeUpdated(model);
156
+ }
157
+
158
+ async deleteCustomType(id: string): Promise<void> {
159
+ const customType = await this.getCustomType(id);
160
+ await rm(customType.directory, { recursive: true });
161
+ await this.onCustomTypeDeleted(id);
162
+ }
163
+ }
164
+
165
+ async function upsertSliceModel(model: SharedSlice, library: URL): Promise<void> {
166
+ const sliceDirectoryName = pascalCase(model.name);
167
+ const sliceDirectory = new URL(sliceDirectoryName, appendTrailingSlash(library));
168
+ const modelPath = new URL("model.json", appendTrailingSlash(sliceDirectory));
169
+ await writeFileRecursive(modelPath, stringify(model));
170
+ }
171
+
172
+ async function upsertCustomTypeModel(model: CustomType): Promise<void> {
173
+ const projectRoot = await findProjectRoot();
174
+ const customTypesDirectory = new URL("customtypes/", projectRoot);
175
+ const modelPath = new URL(`${model.id}/index.json`, customTypesDirectory);
176
+ await writeFileRecursive(modelPath, stringify(model));
177
+ }
@@ -3,16 +3,16 @@ import { pascalCase } from "change-case";
3
3
  import { dedent } from "../lib/string";
4
4
 
5
5
  const SLICE_MARKUP = dedent`
6
- return (
7
- <section
8
- data-slice-type={slice.slice_type}
9
- data-slice-variation={slice.variation}
10
- >
11
- Placeholder component for {slice.slice_type} (variation: {slice.variation}) slices.
12
- <br />
13
- <strong>You can edit this slice directly in your code editor.</strong>
14
- </section>
15
- )
6
+ return (
7
+ <section
8
+ data-slice-type={slice.slice_type}
9
+ data-slice-variation={slice.variation}
10
+ >
11
+ Placeholder component for {slice.slice_type} (variation: {slice.variation}) slices.
12
+ <br />
13
+ <strong>You can edit this slice directly in your code editor.</strong>
14
+ </section>
15
+ )
16
16
  `;
17
17
 
18
18
  export function sliceTemplate(args: { name: string; typescript: boolean }): string {
@@ -248,53 +248,45 @@ export function sliceSimulatorPageTemplate(args: {
248
248
  }): string {
249
249
  const { typescript, appRouter } = args;
250
250
 
251
- if (appRouter) {
252
- if (typescript) {
253
- return dedent`
254
- import {
255
- SliceSimulator,
256
- SliceSimulatorParams,
257
- getSlices,
258
- } from "@prismicio/next";
259
- import { SliceZone } from "@prismicio/react";
260
-
261
- import { components } from "../../slices";
262
-
263
- export default async function SliceSimulatorPage({
264
- searchParams,
265
- }: SliceSimulatorParams) {
266
- const { state } = await searchParams
267
- const slices = getSlices(state);
268
-
269
- return (
270
- <SliceSimulator>
271
- <SliceZone slices={slices} components={components} />
272
- </SliceSimulator>
273
- );
274
- }
275
- `;
251
+ const appTS = dedent`
252
+ import { SliceSimulator, SliceSimulatorParams, getSlices } from "@prismicio/next";
253
+ import { SliceZone } from "@prismicio/react";
254
+
255
+ import { components } from "../../slices";
256
+
257
+ export default async function SliceSimulatorPage({
258
+ searchParams,
259
+ }: SliceSimulatorParams) {
260
+ const { state } = await searchParams
261
+ const slices = getSlices(state);
262
+
263
+ return (
264
+ <SliceSimulator>
265
+ <SliceZone slices={slices} components={components} />
266
+ </SliceSimulator>
267
+ );
276
268
  }
269
+ `;
277
270
 
278
- return dedent`
279
- import { SliceSimulator, getSlices } from "@prismicio/next";
280
- import { SliceZone } from "@prismicio/react";
271
+ const appJS = dedent`
272
+ import { SliceSimulator, getSlices } from "@prismicio/next";
273
+ import { SliceZone } from "@prismicio/react";
281
274
 
282
- import { components } from "../../slices";
275
+ import { components } from "../../slices";
283
276
 
284
- export default async function SliceSimulatorPage({ searchParams }) {
285
- const { state } = await searchParams
286
- const slices = getSlices(state);
277
+ export default async function SliceSimulatorPage({ searchParams }) {
278
+ const { state } = await searchParams
279
+ const slices = getSlices(state);
287
280
 
288
- return (
289
- <SliceSimulator>
290
- <SliceZone slices={slices} components={components} />
291
- </SliceSimulator>
292
- );
293
- }
294
- `;
295
- }
281
+ return (
282
+ <SliceSimulator>
283
+ <SliceZone slices={slices} components={components} />
284
+ </SliceSimulator>
285
+ );
286
+ }
287
+ `;
296
288
 
297
- return dedent`
289
+ const pages = dedent`
298
290
  import { SliceSimulator } from "@prismicio/next/pages";
299
291
  import { SliceZone } from "@prismicio/react";
300
292
 
@@ -308,58 +300,58 @@ export function sliceSimulatorPageTemplate(args: {
308
300
  );
309
301
  }
310
302
  `;
303
+
304
+ if (appRouter) {
305
+ return typescript ? appTS : appJS;
306
+ } else {
307
+ return pages;
308
+ }
311
309
  }
312
310
 
313
311
  export function previewRouteTemplate(args: { typescript: boolean; appRouter: boolean }): string {
314
312
  const { typescript, appRouter } = args;
315
313
 
316
- if (appRouter) {
317
- if (typescript) {
318
- return dedent`
319
- import { NextRequest } from "next/server";
320
- import { redirectToPreviewURL } from "@prismicio/next";
314
+ const appTS = dedent`
315
+ import { NextRequest } from "next/server";
316
+ import { redirectToPreviewURL } from "@prismicio/next";
321
317
 
322
- import { createClient } from "../../../prismicio";
318
+ import { createClient } from "../../../prismicio";
323
319
 
324
- export async function GET(request: NextRequest) {
325
- const client = createClient();
320
+ export async function GET(request: NextRequest) {
321
+ const client = createClient();
326
322
 
327
- return await redirectToPreviewURL({ client, request });
328
- }
329
- `;
323
+ return await redirectToPreviewURL({ client, request });
330
324
  }
325
+ `;
331
326
 
332
- return dedent`
333
- import { redirectToPreviewURL } from "@prismicio/next";
327
+ const appJS = dedent`
328
+ import { redirectToPreviewURL } from "@prismicio/next";
334
329
 
335
- import { createClient } from "../../../prismicio";
330
+ import { createClient } from "../../../prismicio";
336
331
 
337
- export async function GET(request) {
338
- const client = createClient();
332
+ export async function GET(request) {
333
+ const client = createClient();
339
334
 
340
- return await redirectToPreviewURL({ client, request });
341
- }
342
- `;
343
- }
335
+ return await redirectToPreviewURL({ client, request });
336
+ }
337
+ `;
344
338
 
345
- if (typescript) {
346
- return dedent`
347
- import { NextApiRequest, NextApiResponse } from "next";
348
- import { setPreviewData, redirectToPreviewURL } from "@prismicio/next/pages";
339
+ const pagesTS = dedent`
340
+ import { NextApiRequest, NextApiResponse } from "next";
341
+ import { setPreviewData, redirectToPreviewURL } from "@prismicio/next/pages";
349
342
 
350
- import { createClient } from "../../prismicio";
343
+ import { createClient } from "../../prismicio";
351
344
 
352
- export default async function handler(req: NextApiRequest, res: NextApiResponse) {
353
- const client = createClient({ req });
345
+ export default async function handler(req: NextApiRequest, res: NextApiResponse) {
346
+ const client = createClient({ req });
354
347
 
355
- setPreviewData({ req, res });
348
+ setPreviewData({ req, res });
356
349
 
357
- return await redirectToPreviewURL({ req, res, client });
358
- };
359
- `;
360
- }
350
+ return await redirectToPreviewURL({ req, res, client });
351
+ };
352
+ `;
361
353
 
362
- return dedent`
354
+ const pagesJS = dedent`
363
355
  import { setPreviewData, redirectToPreviewURL } from "@prismicio/next/pages";
364
356
 
365
357
  import { createClient } from "../../prismicio";
@@ -372,6 +364,12 @@ export function previewRouteTemplate(args: { typescript: boolean; appRouter: boo
372
364
  return await redirectToPreviewURL({ req, res, client });
373
365
  };
374
366
  `;
367
+
368
+ if (appRouter) {
369
+ return typescript ? appTS : appJS;
370
+ } else {
371
+ return typescript ? pagesTS : pagesJS;
372
+ }
375
373
  }
376
374
 
377
375
  export function exitPreviewRouteTemplate(args: {
@@ -380,34 +378,36 @@ export function exitPreviewRouteTemplate(args: {
380
378
  }): string {
381
379
  const { typescript, appRouter } = args;
382
380
 
383
- if (appRouter) {
384
- return dedent`
385
- import { exitPreview } from "@prismicio/next";
381
+ const app = dedent`
382
+ import { exitPreview } from "@prismicio/next";
386
383
 
387
- export function GET() {
388
- return exitPreview();
389
- }
390
- `;
391
- }
384
+ export function GET() {
385
+ return exitPreview();
386
+ }
387
+ `;
392
388
 
393
- if (typescript) {
394
- return dedent`
395
- import { NextApiRequest, NextApiResponse } from "next";
396
- import { exitPreview } from "@prismicio/next/pages";
389
+ const pagesTS = dedent`
390
+ import { NextApiRequest, NextApiResponse } from "next";
391
+ import { exitPreview } from "@prismicio/next/pages";
397
392
 
398
- export default function handler(req: NextApiRequest, res: NextApiResponse) {
399
- return exitPreview({ req, res });
400
- }
401
- `;
402
- }
393
+ export default function handler(req: NextApiRequest, res: NextApiResponse) {
394
+ return exitPreview({ req, res });
395
+ }
396
+ `;
403
397
 
404
- return dedent`
398
+ const pagesJS = dedent`
405
399
  import { exitPreview } from "@prismicio/next/pages";
406
400
 
407
401
  export default function handler(req, res) {
408
402
  return exitPreview({ req, res });
409
403
  }
410
404
  `;
405
+
406
+ if (appRouter) {
407
+ return app;
408
+ } else {
409
+ return typescript ? pagesTS : pagesJS;
410
+ }
411
411
  }
412
412
 
413
413
  export function revalidateRouteTemplate(args: { supportsCacheLife: boolean }): string {