prismic 1.2.1 → 1.3.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 (47) hide show
  1. package/dist/{builders-hKD4IrLX-cdTCGdwG.mjs → builders-hKD4IrLX-CiOmsZQP.mjs} +1 -1
  2. package/dist/index.mjs +119 -58
  3. package/dist/nextjs-c8JOjFCt.mjs +413 -0
  4. package/dist/nuxt-BhwnOusi.mjs +90 -0
  5. package/dist/string-CFNpwnbk.mjs +7 -0
  6. package/dist/sveltekit-BW5_-HtZ.mjs +263 -0
  7. package/package.json +1 -1
  8. package/src/adapters/index.ts +6 -0
  9. package/src/adapters/nextjs.templates.ts +170 -38
  10. package/src/adapters/nextjs.ts +33 -3
  11. package/src/adapters/nuxt.templates.ts +48 -0
  12. package/src/adapters/nuxt.ts +42 -20
  13. package/src/adapters/sveltekit.templates.ts +92 -35
  14. package/src/adapters/sveltekit.ts +38 -3
  15. package/src/auth.ts +1 -2
  16. package/src/clients/locale.ts +64 -0
  17. package/src/clients/user.ts +1 -0
  18. package/src/clients/wroom.ts +212 -0
  19. package/src/commands/gen-setup.ts +38 -0
  20. package/src/commands/gen-types.ts +35 -0
  21. package/src/commands/gen.ts +18 -0
  22. package/src/commands/init.ts +7 -1
  23. package/src/commands/locale-add.ts +47 -0
  24. package/src/commands/locale-list.ts +43 -0
  25. package/src/commands/locale-remove.ts +45 -0
  26. package/src/commands/locale-set-master.ts +61 -0
  27. package/src/commands/locale.ts +28 -0
  28. package/src/commands/repo-create.ts +60 -0
  29. package/src/commands/repo-list.ts +58 -0
  30. package/src/commands/repo-set-api-access.ts +53 -0
  31. package/src/commands/repo-set-name.ts +46 -0
  32. package/src/commands/repo-view.ts +74 -0
  33. package/src/commands/repo.ts +33 -0
  34. package/src/commands/sync.ts +4 -2
  35. package/src/commands/token-create.ts +55 -0
  36. package/src/commands/token-delete.ts +61 -0
  37. package/src/commands/token-list.ts +67 -0
  38. package/src/commands/token.ts +23 -0
  39. package/src/config.ts +53 -0
  40. package/src/env.ts +2 -0
  41. package/src/index.ts +20 -0
  42. package/src/lib/codegen.ts +6 -5
  43. package/src/lib/url.ts +7 -0
  44. package/dist/nextjs-DrbOdw3q.mjs +0 -318
  45. package/dist/nuxt-DO5Kp4yy.mjs +0 -59
  46. package/dist/string-CnZrLYLV.mjs +0 -7
  47. package/dist/sveltekit-KHG7YUoX.mjs +0 -236
@@ -1,3 +1,5 @@
1
+ import type { CustomType } from "@prismicio/types-internal/lib/customtypes";
2
+
1
3
  import { pascalCase } from "change-case";
2
4
 
3
5
  import { dedent } from "../lib/string";
@@ -7,7 +9,7 @@ export function prismicIOFileTemplate(args: { typescript: boolean }): string {
7
9
 
8
10
  if (typescript) {
9
11
  return dedent`
10
- import { createClient as baseCreateClient, type Route } from "@prismicio/client";
12
+ import { createClient as baseCreateClient } from "@prismicio/client";
11
13
  import { type CreateClientConfig, enableAutoPreviews } from '@prismicio/svelte/kit';
12
14
  import prismicConfig from "../../prismic.config.json";
13
15
 
@@ -16,21 +18,6 @@ export function prismicIOFileTemplate(args: { typescript: boolean }): string {
16
18
  */
17
19
  export const repositoryName = prismicConfig.repositoryName;
18
20
 
19
- /**
20
- * A list of Route Resolver objects that define how a document's \`url\` field is resolved.
21
- *
22
- * {@link https://prismic.io/docs/route-resolver}
23
- *
24
- * Note: \`prismic sync\` may append new default routes for Page Types. Feel free
25
- * to edit these to match your site's routing structure.
26
- */
27
- // TODO: Update the routes array to match your project's route structure.
28
- const routes: Route[] = [
29
- // Examples:
30
- // { type: "homepage", path: "/" },
31
- // { type: "page", path: "/:uid" },
32
- ];
33
-
34
21
  /**
35
22
  * Creates a Prismic client for the project's repository. The client is used to
36
23
  * query content from the Prismic API.
@@ -39,7 +26,7 @@ export function prismicIOFileTemplate(args: { typescript: boolean }): string {
39
26
  */
40
27
  export const createClient = ({ cookies, ...config }: CreateClientConfig = {}) => {
41
28
  const client = baseCreateClient(repositoryName, {
42
- routes,
29
+ routes: prismicConfig.routes,
43
30
  ...config,
44
31
  });
45
32
 
@@ -60,23 +47,6 @@ export function prismicIOFileTemplate(args: { typescript: boolean }): string {
60
47
  */
61
48
  export const repositoryName = prismicConfig.repositoryName;
62
49
 
63
- /**
64
- * A list of Route Resolver objects that define how a document's \`url\` field is resolved.
65
- *
66
- * {@link https://prismic.io/docs/route-resolver#route-resolver}
67
- *
68
- * Note: \`prismic sync\` may append new default routes for Page Types. Feel free
69
- * to edit these to match your site's routing structure.
70
- *
71
- * @type {import("@prismicio/client").Route[]}
72
- */
73
- // TODO: Update the routes array to match your project's route structure.
74
- const routes = [
75
- // Examples:
76
- // { type: "homepage", path: "/" },
77
- // { type: "page", path: "/:uid" },
78
- ];
79
-
80
50
  /**
81
51
  * Creates a Prismic client for the project's repository. The client is used to
82
52
  * query content from the Prismic API.
@@ -85,7 +55,7 @@ export function prismicIOFileTemplate(args: { typescript: boolean }): string {
85
55
  */
86
56
  export const createClient = ({ cookies, ...config } = {}) => {
87
57
  const client = baseCreateClient(repositoryName, {
88
- routes,
58
+ routes: prismicConfig.routes,
89
59
  ...config,
90
60
  });
91
61
 
@@ -209,6 +179,93 @@ export function rootLayoutTemplate(args: { version: number }): string {
209
179
  return version <= 4 ? v4 : v5;
210
180
  }
211
181
 
182
+ export function pageTemplate(args: { typescript: boolean }): string {
183
+ const { typescript } = args;
184
+
185
+ if (typescript) {
186
+ return dedent`
187
+ <script lang="ts">
188
+ import { SliceZone } from "@prismicio/svelte";
189
+ import { components } from "$lib/slices";
190
+ import type { PageProps } from "./$types";
191
+
192
+ const { data }: PageProps = $props();
193
+ </script>
194
+
195
+ <SliceZone slices={data.page.data.slices} {components} />
196
+ `;
197
+ }
198
+
199
+ return dedent`
200
+ <script>
201
+ import { SliceZone } from "@prismicio/svelte";
202
+ import { components } from "$lib/slices";
203
+
204
+ const { data } = $props();
205
+ </script>
206
+
207
+ <SliceZone slices={data.page.data.slices} {components} />
208
+ `;
209
+ }
210
+
211
+ export function pageServerTemplate(args: { model: CustomType; typescript: boolean }): string {
212
+ const { model, typescript } = args;
213
+
214
+ if (model.repeatable) {
215
+ if (typescript) {
216
+ return dedent`
217
+ import type { PageServerLoad } from "./$types";
218
+ import { createClient } from "$lib/prismicio";
219
+
220
+ export const load: PageServerLoad = async ({ params, fetch, cookies }) => {
221
+ const client = createClient({ fetch, cookies });
222
+ const page = await client.getByUID("${model.id}", params.uid);
223
+
224
+ return { page };
225
+ };
226
+ `;
227
+ }
228
+
229
+ return dedent`
230
+ import { createClient } from "$lib/prismicio";
231
+
232
+ /** @type {import("./$types").PageServerLoad} */
233
+ export async function load({ params, fetch, cookies }) {
234
+ const client = createClient({ fetch, cookies });
235
+ const page = await client.getByUID("${model.id}", params.uid);
236
+
237
+ return { page };
238
+ }
239
+ `;
240
+ }
241
+
242
+ if (typescript) {
243
+ return dedent`
244
+ import type { PageServerLoad } from "./$types";
245
+ import { createClient } from "$lib/prismicio";
246
+
247
+ export const load: PageServerLoad = async ({ fetch, cookies }) => {
248
+ const client = createClient({ fetch, cookies });
249
+ const page = await client.getSingle("${model.id}");
250
+
251
+ return { page };
252
+ };
253
+ `;
254
+ }
255
+
256
+ return dedent`
257
+ import { createClient } from "$lib/prismicio";
258
+
259
+ /* @type {import("./$types").PageServerLoad} */
260
+ export async function load({ fetch, cookies }) {
261
+ const client = createClient({ fetch, cookies });
262
+ const page = await client.getSingle("${model.id}");
263
+
264
+ return { page };
265
+ }
266
+ `;
267
+ }
268
+
212
269
  const SLICE_MARKUP = dedent`
213
270
  <section data-slice-type={slice.slice_type} data-slice-variation={slice.variation}>
214
271
  Placeholder component for {slice.slice_type} (variation: {slice.variation}) slices.
@@ -1,4 +1,4 @@
1
- import type { SharedSlice } from "@prismicio/types-internal/lib/customtypes";
1
+ import type { CustomType, SharedSlice } from "@prismicio/types-internal/lib/customtypes";
2
2
 
3
3
  import { pascalCase } from "change-case";
4
4
  import { loadFile } from "magicast";
@@ -8,12 +8,15 @@ import { relative } from "node:path";
8
8
  import { fileURLToPath } from "node:url";
9
9
 
10
10
  import { Adapter } from ".";
11
+ import { buildRoutePath } from "../config";
11
12
  import { exists, writeFileRecursive } from "../lib/file";
12
13
  import { addDependencies, findPackageJson, getNpmPackageVersion } from "../lib/packageJson";
13
14
  import { dedent } from "../lib/string";
14
15
  import { appendTrailingSlash } from "../lib/url";
15
16
  import { checkIsTypeScriptProject, findProjectRoot } from "../project";
16
17
  import {
18
+ pageServerTemplate,
19
+ pageTemplate,
17
20
  previewAPIRouteTemplate,
18
21
  prismicIOFileTemplate,
19
22
  rootLayoutTemplate,
@@ -24,7 +27,7 @@ import {
24
27
  export class SvelteKitAdapter extends Adapter {
25
28
  readonly id = "sveltekit";
26
29
 
27
- async onProjectInitialized(): Promise<void> {
30
+ async setupProject(): Promise<void> {
28
31
  await addDependencies({
29
32
  "@prismicio/client": `^${await getNpmPackageVersion("@prismicio/client")}`,
30
33
  "@prismicio/svelte": `^${await getNpmPackageVersion("@prismicio/svelte")}`,
@@ -39,6 +42,8 @@ export class SvelteKitAdapter extends Adapter {
39
42
  await modifyViteConfig();
40
43
  }
41
44
 
45
+ onProjectInitialized(): void {}
46
+
42
47
  async onSliceCreated(model: SharedSlice, library: URL): Promise<void> {
43
48
  const sliceDirectoryName = pascalCase(model.name);
44
49
  const sliceDirectory = new URL(sliceDirectoryName, appendTrailingSlash(library));
@@ -56,7 +61,9 @@ export class SvelteKitAdapter extends Adapter {
56
61
 
57
62
  onSliceDeleted(): void {}
58
63
 
59
- onCustomTypeCreated(): void {}
64
+ async onCustomTypeCreated(model: CustomType): Promise<void> {
65
+ if (model.format === "page") await createPageFile(model);
66
+ }
60
67
 
61
68
  onCustomTypeUpdated(): void {}
62
69
 
@@ -183,6 +190,34 @@ async function createRootLayoutFile(): Promise<void> {
183
190
  await writeFileRecursive(filePath, contents);
184
191
  }
185
192
 
193
+ async function createPageFile(model: CustomType): Promise<void> {
194
+ const routePath = buildRoutePath(model)
195
+ .split("/")
196
+ .filter(Boolean)
197
+ .map((segment) => (segment.startsWith(":") ? `[${segment.slice(1)}]` : segment))
198
+ .join("/");
199
+ const projectRoot = await findProjectRoot();
200
+ const extension = await getJsFileExtension();
201
+ const fullRoutePath = new URL(`src/routes/[[preview=preview]]/${routePath}/`, projectRoot);
202
+
203
+ const pageFilePath = new URL("+page.svelte", fullRoutePath);
204
+ if (!(await exists(pageFilePath))) {
205
+ const contents = pageTemplate({
206
+ typescript: await checkIsTypeScriptProject(),
207
+ });
208
+ await writeFileRecursive(pageFilePath, contents);
209
+ }
210
+
211
+ const serverFilePath = new URL(`+page.server.${extension}`, fullRoutePath);
212
+ if (!(await exists(serverFilePath))) {
213
+ const contents = pageServerTemplate({
214
+ model,
215
+ typescript: await checkIsTypeScriptProject(),
216
+ });
217
+ await writeFileRecursive(serverFilePath, contents);
218
+ }
219
+ }
220
+
186
221
  async function modifyViteConfig(): Promise<void> {
187
222
  const projectRoot = await findProjectRoot();
188
223
  let configUrl = new URL("vite.config.js", projectRoot);
package/src/auth.ts CHANGED
@@ -5,7 +5,7 @@ import { pathToFileURL } from "node:url";
5
5
  import * as z from "zod/mini";
6
6
 
7
7
  import { refreshToken as baseRefreshToken } from "./clients/auth";
8
- import { env } from "./env";
8
+ import { DEFAULT_PRISMIC_HOST, env } from "./env";
9
9
  import { exists } from "./lib/file";
10
10
  import { stringify } from "./lib/json";
11
11
  import { appendTrailingSlash } from "./lib/url";
@@ -14,7 +14,6 @@ const AUTH_FILE_PATH = new URL(".prismic", appendTrailingSlash(pathToFileURL(hom
14
14
  const LOGIN_TIMEOUT_MS = 3 * 60 * 1000; // 3 minutes
15
15
  const PREFERRED_PORT = 5555;
16
16
  const LOGIN_SOURCE = "prismic-cli";
17
- const DEFAULT_PRISMIC_HOST = "prismic.io";
18
17
 
19
18
  const AuthFileSchema = z.object({
20
19
  token: z.optional(z.string().check(z.minLength(1))),
@@ -0,0 +1,64 @@
1
+ import * as z from "zod/mini";
2
+
3
+ import { request } from "../lib/request";
4
+
5
+ const LocaleSchema = z.object({
6
+ id: z.string(),
7
+ label: z.string(),
8
+ customName: z.nullable(z.string()),
9
+ isMaster: z.boolean(),
10
+ });
11
+
12
+ export type Locale = z.infer<typeof LocaleSchema>;
13
+
14
+ export async function getLocales(config: {
15
+ repo: string;
16
+ token: string | undefined;
17
+ host: string;
18
+ }): Promise<Locale[]> {
19
+ const { repo, token, host } = config;
20
+ const url = new URL("repository/locales", getLocaleServiceUrl(host));
21
+ url.searchParams.set("repository", repo);
22
+ const response = await request(url, {
23
+ headers: { Authorization: `Bearer ${token}` },
24
+ schema: z.object({ results: z.array(LocaleSchema) }),
25
+ });
26
+ return response.results;
27
+ }
28
+
29
+ export async function upsertLocale(
30
+ locale: { id: string; isMaster?: boolean; customName?: string },
31
+ config: { repo: string; token: string | undefined; host: string },
32
+ ): Promise<Locale> {
33
+ const { repo, token, host } = config;
34
+ const url = new URL("repository/locales", getLocaleServiceUrl(host));
35
+ url.searchParams.set("repository", repo);
36
+ const response = await request(url, {
37
+ method: "POST",
38
+ body: {
39
+ id: locale.id,
40
+ isMaster: locale.isMaster ?? false,
41
+ ...(locale.customName ? { customName: locale.customName } : {}),
42
+ },
43
+ headers: { Authorization: `Bearer ${token}` },
44
+ schema: LocaleSchema,
45
+ });
46
+ return response;
47
+ }
48
+
49
+ export async function removeLocale(
50
+ code: string,
51
+ config: { repo: string; token: string | undefined; host: string },
52
+ ): Promise<void> {
53
+ const { repo, token, host } = config;
54
+ const url = new URL(`repository/locales/${encodeURIComponent(code)}`, getLocaleServiceUrl(host));
55
+ url.searchParams.set("repository", repo);
56
+ await request(url, {
57
+ method: "DELETE",
58
+ headers: { Authorization: `Bearer ${token}` },
59
+ });
60
+ }
61
+
62
+ function getLocaleServiceUrl(host: string): URL {
63
+ return new URL(`https://api.internal.${host}/locale/`);
64
+ }
@@ -10,6 +10,7 @@ const ProfileSchema = z.object({
10
10
  z.object({
11
11
  domain: z.string(),
12
12
  name: z.optional(z.string()),
13
+ role: z.optional(z.string()),
13
14
  }),
14
15
  ),
15
16
  });
@@ -106,6 +106,218 @@ export async function deleteWebhook(
106
106
  });
107
107
  }
108
108
 
109
+ const AccessTokenSchema = z.object({
110
+ id: z.string(),
111
+ scope: z.string(),
112
+ token: z.string(),
113
+ created_at: z.object({ $date: z.number() }),
114
+ });
115
+ type AccessToken = z.infer<typeof AccessTokenSchema>;
116
+
117
+ const OAuthAppSchema = z.object({
118
+ id: z.string(),
119
+ name: z.string(),
120
+ wroom_auths: z.array(AccessTokenSchema),
121
+ });
122
+ type OAuthApp = z.infer<typeof OAuthAppSchema>;
123
+
124
+ const WriteTokenSchema = z.object({
125
+ app_name: z.string(),
126
+ token: z.string(),
127
+ timestamp: z.number(),
128
+ });
129
+ type WriteToken = z.infer<typeof WriteTokenSchema>;
130
+
131
+ const WriteTokensInfoSchema = z.object({
132
+ max_tokens: z.number(),
133
+ tokens: z.array(WriteTokenSchema),
134
+ });
135
+ type WriteTokensInfo = z.infer<typeof WriteTokensInfoSchema>;
136
+
137
+ export async function getOAuthApps(config: {
138
+ repo: string;
139
+ token: string | undefined;
140
+ host: string;
141
+ }): Promise<OAuthApp[]> {
142
+ const url = new URL("settings/security/contentapi", getWroomUrl(config.repo, config.host));
143
+ return await request(url, {
144
+ credentials: { "prismic-auth": config.token },
145
+ schema: z.array(OAuthAppSchema),
146
+ });
147
+ }
148
+
149
+ export async function createOAuthApp(
150
+ name: string,
151
+ config: { repo: string; token: string | undefined; host: string },
152
+ ): Promise<OAuthApp> {
153
+ const url = new URL("settings/security/oauthapp", getWroomUrl(config.repo, config.host));
154
+ return await request(url, {
155
+ method: "POST",
156
+ body: { app_name: name },
157
+ credentials: { "prismic-auth": config.token },
158
+ schema: OAuthAppSchema,
159
+ });
160
+ }
161
+
162
+ export async function createOAuthAuthorization(
163
+ appId: string,
164
+ scope: string,
165
+ config: { repo: string; token: string | undefined; host: string },
166
+ ): Promise<AccessToken> {
167
+ const url = new URL("settings/security/authorizations", getWroomUrl(config.repo, config.host));
168
+ return await request(url, {
169
+ method: "POST",
170
+ body: { app: appId, scope },
171
+ credentials: { "prismic-auth": config.token },
172
+ schema: AccessTokenSchema,
173
+ });
174
+ }
175
+
176
+ export async function deleteOAuthAuthorization(
177
+ authId: string,
178
+ config: { repo: string; token: string | undefined; host: string },
179
+ ): Promise<void> {
180
+ const url = new URL(
181
+ `settings/security/authorizations/${encodeURIComponent(authId)}`,
182
+ getWroomUrl(config.repo, config.host),
183
+ );
184
+ await request(url, {
185
+ method: "DELETE",
186
+ credentials: { "prismic-auth": config.token },
187
+ });
188
+ }
189
+
190
+ export async function getWriteTokens(config: {
191
+ repo: string;
192
+ token: string | undefined;
193
+ host: string;
194
+ }): Promise<WriteTokensInfo> {
195
+ const url = new URL("settings/security/customtypesapi", getWroomUrl(config.repo, config.host));
196
+ return await request(url, {
197
+ credentials: { "prismic-auth": config.token },
198
+ schema: WriteTokensInfoSchema,
199
+ });
200
+ }
201
+
202
+ export async function createWriteToken(
203
+ name: string,
204
+ config: { repo: string; token: string | undefined; host: string },
205
+ ): Promise<WriteToken> {
206
+ const url = new URL("settings/security/token", getWroomUrl(config.repo, config.host));
207
+ return await request(url, {
208
+ method: "POST",
209
+ body: { app_name: name },
210
+ credentials: { "prismic-auth": config.token },
211
+ schema: WriteTokenSchema,
212
+ });
213
+ }
214
+
215
+ export async function deleteWriteToken(
216
+ tokenValue: string,
217
+ config: { repo: string; token: string | undefined; host: string },
218
+ ): Promise<void> {
219
+ const url = new URL(
220
+ `settings/security/token/${encodeURIComponent(tokenValue)}`,
221
+ getWroomUrl(config.repo, config.host),
222
+ );
223
+ await request(url, {
224
+ method: "DELETE",
225
+ credentials: { "prismic-auth": config.token },
226
+ });
227
+ }
228
+
229
+ export async function checkIsDomainAvailable(config: {
230
+ domain: string;
231
+ token: string | undefined;
232
+ host: string;
233
+ }): Promise<boolean> {
234
+ const { domain, token, host } = config;
235
+ const url = new URL(`app/dashboard/repositories/${domain}/exists`, getDashboardUrl(host));
236
+ const response = await request(url, {
237
+ credentials: { "prismic-auth": token },
238
+ schema: z.boolean(),
239
+ });
240
+ return response;
241
+ }
242
+
243
+ export async function createRepository(config: {
244
+ domain: string;
245
+ name: string;
246
+ framework: string;
247
+ token: string | undefined;
248
+ host: string;
249
+ }): Promise<void> {
250
+ const { domain, name, framework, token, host } = config;
251
+ const url = new URL("app/dashboard/repositories", getDashboardUrl(host));
252
+ await request(url, {
253
+ method: "POST",
254
+ body: { domain, name, framework, plan: "personal" },
255
+ credentials: { "prismic-auth": token },
256
+ });
257
+ }
258
+
259
+ const SyncStateSchema = z.object({
260
+ repository: z.object({
261
+ api_access: z.string(),
262
+ }),
263
+ });
264
+
265
+ export async function getRepositoryAccess(config: {
266
+ repo: string;
267
+ token: string | undefined;
268
+ host: string;
269
+ }): Promise<string> {
270
+ const { repo, token, host } = config;
271
+ const url = new URL("syncState", getWroomUrl(repo, host));
272
+ const response = await request(url, {
273
+ credentials: { "prismic-auth": token },
274
+ schema: SyncStateSchema,
275
+ });
276
+ return response.repository.api_access;
277
+ }
278
+
279
+ export type RepositoryAccessLevel = "private" | "public" | "open";
280
+
281
+ export async function setRepositoryAccess(
282
+ level: RepositoryAccessLevel,
283
+ config: { repo: string; token: string | undefined; host: string },
284
+ ): Promise<void> {
285
+ const { repo, token, host } = config;
286
+ const url = new URL("settings/security/apiaccess", getWroomUrl(repo, host));
287
+ await request(url, {
288
+ method: "POST",
289
+ body: { api_access: level },
290
+ credentials: { "prismic-auth": token },
291
+ });
292
+ }
293
+
294
+ const SetNameResponseSchema = z.object({
295
+ repository: z.object({
296
+ name: z.string(),
297
+ }),
298
+ });
299
+
300
+ export async function setRepositoryName(
301
+ name: string,
302
+ config: { repo: string; token: string | undefined; host: string },
303
+ ): Promise<string> {
304
+ const { repo, token, host } = config;
305
+ const url = new URL("app/settings/repository", getWroomUrl(repo, host));
306
+ const formData = new FormData();
307
+ formData.set("displayname", name);
308
+ const response = await request(url, {
309
+ method: "POST",
310
+ body: formData,
311
+ credentials: { "prismic-auth": token },
312
+ schema: SetNameResponseSchema,
313
+ });
314
+ return response.repository.name;
315
+ }
316
+
317
+ function getDashboardUrl(host: string): URL {
318
+ return new URL(`https://${host}/`);
319
+ }
320
+
109
321
  function getWroomUrl(repo: string, host: string): URL {
110
322
  return new URL(`https://${repo}.${host}/`);
111
323
  }
@@ -0,0 +1,38 @@
1
+ import { getAdapter } from "../adapters";
2
+ import { createCommand, type CommandConfig } from "../lib/command";
3
+ import { installDependencies } from "../lib/packageJson";
4
+
5
+ const config = {
6
+ name: "prismic gen setup",
7
+ description: `
8
+ Generate framework-specific setup files for a Prismic project.
9
+
10
+ Installs dependencies, creates the Prismic client file, slice simulator
11
+ page, preview routes, and other files required by the detected framework.
12
+ Skips files that already exist.
13
+ `,
14
+ options: {
15
+ "no-install": {
16
+ type: "boolean",
17
+ description: "Skip installing dependencies",
18
+ },
19
+ },
20
+ } satisfies CommandConfig;
21
+
22
+ export default createCommand(config, async ({ values }) => {
23
+ const adapter = await getAdapter();
24
+ await adapter.setupProject();
25
+
26
+ if (!values["no-install"]) {
27
+ try {
28
+ console.info("Installing dependencies...");
29
+ await installDependencies();
30
+ } catch {
31
+ console.warn(
32
+ "Could not install dependencies automatically. Please install them manually (i.e. `npm install`).",
33
+ );
34
+ }
35
+ }
36
+
37
+ console.info("Generated setup files.");
38
+ });
@@ -0,0 +1,35 @@
1
+ import { getAdapter } from "../adapters";
2
+ import { generateAndWriteTypes } from "../lib/codegen";
3
+ import { createCommand, type CommandConfig } from "../lib/command";
4
+ import { relativePathname } from "../lib/url";
5
+ import { findProjectRoot } from "../project";
6
+
7
+ const FILENAME = "prismicio-types.d.ts";
8
+
9
+ const config = {
10
+ name: "prismic gen types",
11
+ description: `
12
+ Generate TypeScript types from local custom type and slice models.
13
+
14
+ Reads models from the customtypes/ and slices directories, then writes
15
+ a prismicio-types.d.ts file at the project root.
16
+ `,
17
+ } satisfies CommandConfig;
18
+
19
+ export default createCommand(config, async () => {
20
+ const adapter = await getAdapter();
21
+ const slices = await adapter.getSlices();
22
+ const customTypes = await adapter.getCustomTypes();
23
+ const projectRoot = await findProjectRoot();
24
+
25
+ const output = new URL(FILENAME, projectRoot);
26
+ const relativeOutput = relativePathname(projectRoot, output);
27
+
28
+ await generateAndWriteTypes({
29
+ customTypes: customTypes.map((customType) => customType.model),
30
+ slices: slices.map((slice) => slice.model),
31
+ output,
32
+ });
33
+
34
+ console.info(`Generated types at ${relativeOutput}`);
35
+ });
@@ -0,0 +1,18 @@
1
+ import { createCommandRouter } from "../lib/command";
2
+ import genSetup from "./gen-setup";
3
+ import genTypes from "./gen-types";
4
+
5
+ export default createCommandRouter({
6
+ name: "prismic gen",
7
+ description: "Generate files from local Prismic models.",
8
+ commands: {
9
+ setup: {
10
+ handler: genSetup,
11
+ description: "Generate framework-specific Prismic setup",
12
+ },
13
+ types: {
14
+ handler: genTypes,
15
+ description: "Generate TypeScript types from local models",
16
+ },
17
+ },
18
+ });
@@ -12,6 +12,7 @@ import {
12
12
  readLegacySliceMachineConfig,
13
13
  UnknownProjectRoot,
14
14
  } from "../config";
15
+ import { DEFAULT_PRISMIC_HOST } from "../env";
15
16
  import { openBrowser } from "../lib/browser";
16
17
  import { generateAndWriteTypes } from "../lib/codegen";
17
18
  import { CommandError, createCommand, type CommandConfig } from "../lib/command";
@@ -108,9 +109,13 @@ export default createCommand(config, async ({ values }) => {
108
109
 
109
110
  // Create prismic.config.json
110
111
  try {
112
+ const documentAPIEndpoint =
113
+ host !== DEFAULT_PRISMIC_HOST ? `https://${repo}.cdn.${host}/api/v2/` : undefined;
111
114
  await createConfig({
112
115
  repositoryName: repo,
116
+ documentAPIEndpoint,
113
117
  libraries: legacySliceMachineConfig?.libraries,
118
+ routes: [],
114
119
  });
115
120
  } catch (error) {
116
121
  if (error instanceof UnknownProjectRoot) {
@@ -149,10 +154,11 @@ export default createCommand(config, async ({ values }) => {
149
154
  const slices = await adapter.getSlices();
150
155
  const customTypes = await adapter.getCustomTypes();
151
156
  const projectRoot = await findProjectRoot();
157
+ const output = new URL("prismicio-types.d.ts", projectRoot);
152
158
  await generateAndWriteTypes({
153
159
  customTypes: customTypes.map((customType) => customType.model),
154
160
  slices: slices.map((slice) => slice.model),
155
- projectRoot,
161
+ output,
156
162
  });
157
163
 
158
164
  console.info(`\nInitialized Prismic for repository "${repo}".`);