@angeloashmore/prismic-cli-poc 0.0.0-canary.1d36cd8

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 (131) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +98 -0
  3. package/dist/index.mjs +2548 -0
  4. package/package.json +53 -0
  5. package/src/codegen-types.ts +82 -0
  6. package/src/codegen.ts +45 -0
  7. package/src/custom-type-add-field-boolean.ts +192 -0
  8. package/src/custom-type-add-field-color.ts +177 -0
  9. package/src/custom-type-add-field-date.ts +180 -0
  10. package/src/custom-type-add-field-embed.ts +177 -0
  11. package/src/custom-type-add-field-geo-point.ts +174 -0
  12. package/src/custom-type-add-field-image.ts +177 -0
  13. package/src/custom-type-add-field-key-text.ts +177 -0
  14. package/src/custom-type-add-field-link.ts +201 -0
  15. package/src/custom-type-add-field-number.ts +209 -0
  16. package/src/custom-type-add-field-rich-text.ts +202 -0
  17. package/src/custom-type-add-field-select.ts +192 -0
  18. package/src/custom-type-add-field-timestamp.ts +180 -0
  19. package/src/custom-type-add-field-uid.ts +177 -0
  20. package/src/custom-type-add-field.ts +111 -0
  21. package/src/custom-type-connect-slice.ts +220 -0
  22. package/src/custom-type-create.ts +118 -0
  23. package/src/custom-type-disconnect-slice.ts +177 -0
  24. package/src/custom-type-list.ts +110 -0
  25. package/src/custom-type-remove-field.ts +177 -0
  26. package/src/custom-type-remove.ts +144 -0
  27. package/src/custom-type-set-name.ts +144 -0
  28. package/src/custom-type-view.ts +118 -0
  29. package/src/custom-type.ts +85 -0
  30. package/src/index.ts +127 -0
  31. package/src/init.ts +64 -0
  32. package/src/lib/auth.ts +83 -0
  33. package/src/lib/config.ts +111 -0
  34. package/src/lib/custom-types-api.ts +438 -0
  35. package/src/lib/file.ts +49 -0
  36. package/src/lib/framework.ts +143 -0
  37. package/src/lib/json.ts +3 -0
  38. package/src/lib/request.ts +116 -0
  39. package/src/lib/slice.ts +115 -0
  40. package/src/lib/string.ts +6 -0
  41. package/src/lib/url.ts +25 -0
  42. package/src/locale-add.ts +116 -0
  43. package/src/locale-list.ts +107 -0
  44. package/src/locale-remove.ts +88 -0
  45. package/src/locale-set-default.ts +131 -0
  46. package/src/locale.ts +60 -0
  47. package/src/login.ts +152 -0
  48. package/src/logout.ts +36 -0
  49. package/src/page-type-add-field-boolean.ts +192 -0
  50. package/src/page-type-add-field-color.ts +177 -0
  51. package/src/page-type-add-field-date.ts +180 -0
  52. package/src/page-type-add-field-embed.ts +177 -0
  53. package/src/page-type-add-field-geo-point.ts +174 -0
  54. package/src/page-type-add-field-image.ts +177 -0
  55. package/src/page-type-add-field-key-text.ts +177 -0
  56. package/src/page-type-add-field-link.ts +201 -0
  57. package/src/page-type-add-field-number.ts +209 -0
  58. package/src/page-type-add-field-rich-text.ts +202 -0
  59. package/src/page-type-add-field-select.ts +192 -0
  60. package/src/page-type-add-field-timestamp.ts +180 -0
  61. package/src/page-type-add-field-uid.ts +177 -0
  62. package/src/page-type-add-field.ts +111 -0
  63. package/src/page-type-connect-slice.ts +220 -0
  64. package/src/page-type-create.ts +142 -0
  65. package/src/page-type-disconnect-slice.ts +177 -0
  66. package/src/page-type-list.ts +109 -0
  67. package/src/page-type-remove-field.ts +177 -0
  68. package/src/page-type-remove.ts +144 -0
  69. package/src/page-type-set-name.ts +144 -0
  70. package/src/page-type-set-repeatable.ts +153 -0
  71. package/src/page-type-view.ts +118 -0
  72. package/src/page-type.ts +90 -0
  73. package/src/preview-add.ts +126 -0
  74. package/src/preview-get-simulator.ts +104 -0
  75. package/src/preview-list.ts +106 -0
  76. package/src/preview-remove-simulator.ts +80 -0
  77. package/src/preview-remove.ts +109 -0
  78. package/src/preview-set-name.ts +137 -0
  79. package/src/preview-set-simulator.ts +116 -0
  80. package/src/preview.ts +75 -0
  81. package/src/pull.ts +247 -0
  82. package/src/push.ts +405 -0
  83. package/src/repo-create.ts +136 -0
  84. package/src/repo-get-access.ts +86 -0
  85. package/src/repo-list.ts +100 -0
  86. package/src/repo-set-access.ts +100 -0
  87. package/src/repo-set-name.ts +102 -0
  88. package/src/repo-view.ts +113 -0
  89. package/src/repo.ts +70 -0
  90. package/src/slice-add-field-boolean.ts +173 -0
  91. package/src/slice-add-field-color.ts +158 -0
  92. package/src/slice-add-field-date.ts +158 -0
  93. package/src/slice-add-field-embed.ts +158 -0
  94. package/src/slice-add-field-geo-point.ts +155 -0
  95. package/src/slice-add-field-image.ts +155 -0
  96. package/src/slice-add-field-key-text.ts +158 -0
  97. package/src/slice-add-field-link.ts +178 -0
  98. package/src/slice-add-field-number.ts +158 -0
  99. package/src/slice-add-field-rich-text.ts +183 -0
  100. package/src/slice-add-field-select.ts +173 -0
  101. package/src/slice-add-field-timestamp.ts +158 -0
  102. package/src/slice-add-field.ts +106 -0
  103. package/src/slice-add-variation.ts +145 -0
  104. package/src/slice-create.ts +148 -0
  105. package/src/slice-list-variations.ts +67 -0
  106. package/src/slice-list.ts +88 -0
  107. package/src/slice-remove-field.ts +128 -0
  108. package/src/slice-remove-variation.ts +118 -0
  109. package/src/slice-remove.ts +97 -0
  110. package/src/slice-rename.ts +128 -0
  111. package/src/slice-view.ts +77 -0
  112. package/src/slice.ts +90 -0
  113. package/src/status.ts +733 -0
  114. package/src/token-create.ts +203 -0
  115. package/src/token-delete.ts +182 -0
  116. package/src/token-list.ts +223 -0
  117. package/src/token-set-name.ts +193 -0
  118. package/src/token.ts +60 -0
  119. package/src/webhook-add-header.ts +118 -0
  120. package/src/webhook-create.ts +152 -0
  121. package/src/webhook-disable.ts +109 -0
  122. package/src/webhook-enable.ts +132 -0
  123. package/src/webhook-list.ts +93 -0
  124. package/src/webhook-remove-header.ts +117 -0
  125. package/src/webhook-remove.ts +106 -0
  126. package/src/webhook-set-triggers.ts +148 -0
  127. package/src/webhook-status.ts +90 -0
  128. package/src/webhook-test.ts +106 -0
  129. package/src/webhook-view.ts +147 -0
  130. package/src/webhook.ts +95 -0
  131. package/src/whoami.ts +62 -0
@@ -0,0 +1,126 @@
1
+ import { parseArgs } from "node:util";
2
+
3
+ import { isAuthenticated } from "./lib/auth";
4
+ import { safeGetRepositoryFromConfig } from "./lib/config";
5
+ import { stringify } from "./lib/json";
6
+ import { ForbiddenRequestError, request } from "./lib/request";
7
+ import { getRepoUrl } from "./lib/url";
8
+
9
+ const HELP = `
10
+ Add a preview configuration to a Prismic repository.
11
+
12
+ By default, this command reads the repository from prismic.config.json at the
13
+ project root.
14
+
15
+ USAGE
16
+ prismic preview add <url> [flags]
17
+
18
+ ARGUMENTS
19
+ <url> Preview URL (e.g. https://example.com/api/preview)
20
+
21
+ FLAGS
22
+ -n, --name string Display name (defaults to hostname)
23
+ -r, --repo string Repository domain
24
+ -h, --help Show help for command
25
+
26
+ LEARN MORE
27
+ Use \`prismic <command> <subcommand> --help\` for more information about a command.
28
+ `.trim();
29
+
30
+ export async function previewAdd(): Promise<void> {
31
+ const {
32
+ values: { help, name, repo = await safeGetRepositoryFromConfig() },
33
+ positionals: [previewUrl],
34
+ } = parseArgs({
35
+ args: process.argv.slice(4), // skip: node, script, "preview", "add"
36
+ options: {
37
+ name: { type: "string", short: "n" },
38
+ repo: { type: "string", short: "r" },
39
+ help: { type: "boolean", short: "h" },
40
+ },
41
+ allowPositionals: true,
42
+ });
43
+
44
+ if (help) {
45
+ console.info(HELP);
46
+ return;
47
+ }
48
+
49
+ if (!previewUrl) {
50
+ console.error("Missing required argument: <url>");
51
+ process.exitCode = 1;
52
+ return;
53
+ }
54
+
55
+ if (!repo) {
56
+ console.error("Missing prismic.config.json or --repo option");
57
+ process.exitCode = 1;
58
+ return;
59
+ }
60
+
61
+ const authenticated = await isAuthenticated();
62
+ if (!authenticated) {
63
+ handleUnauthenticated();
64
+ return;
65
+ }
66
+
67
+ const parsed = parsePreviewUrl(previewUrl);
68
+ if (!parsed) {
69
+ console.error(`Invalid URL: ${previewUrl}`);
70
+ process.exitCode = 1;
71
+ return;
72
+ }
73
+
74
+ const displayName = name || parsed.hostname;
75
+
76
+ const response = await addPreview(repo, {
77
+ name: displayName,
78
+ websiteURL: parsed.websiteURL,
79
+ resolverPath: parsed.resolverPath,
80
+ });
81
+
82
+ if (!response.ok) {
83
+ if (response.error instanceof ForbiddenRequestError) {
84
+ handleUnauthenticated();
85
+ } else {
86
+ console.error(`Failed to add preview: ${stringify(response.value)}`);
87
+ process.exitCode = 1;
88
+ }
89
+ return;
90
+ }
91
+
92
+ console.info(`Preview added: ${previewUrl}`);
93
+ }
94
+
95
+ export function parsePreviewUrl(
96
+ url: string,
97
+ ): { websiteURL: string; resolverPath: string | undefined; hostname: string } | undefined {
98
+ try {
99
+ const parsed = new URL(url);
100
+ const websiteURL = `${parsed.protocol}//${parsed.host}`;
101
+ const resolverPath = parsed.pathname === "/" ? undefined : parsed.pathname;
102
+ return { websiteURL, resolverPath, hostname: parsed.hostname };
103
+ } catch {
104
+ return undefined;
105
+ }
106
+ }
107
+
108
+ async function addPreview(
109
+ repo: string,
110
+ config: { name: string; websiteURL: string; resolverPath: string | undefined },
111
+ ) {
112
+ const url = new URL("/previews/new", await getRepoUrl(repo));
113
+ return await request(url, {
114
+ method: "POST",
115
+ body: {
116
+ name: config.name,
117
+ websiteURL: config.websiteURL,
118
+ resolverPath: config.resolverPath,
119
+ },
120
+ });
121
+ }
122
+
123
+ function handleUnauthenticated() {
124
+ console.error("Not logged in. Run `prismic login` first.");
125
+ process.exitCode = 1;
126
+ }
@@ -0,0 +1,104 @@
1
+ import { parseArgs } from "node:util";
2
+ import * as v from "valibot";
3
+
4
+ import { isAuthenticated } from "./lib/auth";
5
+ import { safeGetRepositoryFromConfig } from "./lib/config";
6
+ import { stringify } from "./lib/json";
7
+ import { ForbiddenRequestError, type ParsedRequestResponse, request } from "./lib/request";
8
+ import { getRepoUrl } from "./lib/url";
9
+
10
+ const HELP = `
11
+ Show the slice simulator URL for a Prismic repository.
12
+
13
+ By default, this command reads the repository from prismic.config.json at the
14
+ project root.
15
+
16
+ USAGE
17
+ prismic preview get-simulator [flags]
18
+
19
+ FLAGS
20
+ --json Output as JSON
21
+ -r, --repo string Repository domain
22
+ -h, --help Show help for command
23
+
24
+ LEARN MORE
25
+ Use \`prismic <command> <subcommand> --help\` for more information about a command.
26
+ `.trim();
27
+
28
+ export async function previewGetSimulator(): Promise<void> {
29
+ const {
30
+ values: { help, repo = await safeGetRepositoryFromConfig(), json },
31
+ } = parseArgs({
32
+ args: process.argv.slice(4), // skip: node, script, "preview", "get-simulator"
33
+ options: {
34
+ json: { type: "boolean" },
35
+ repo: { type: "string", short: "r" },
36
+ help: { type: "boolean", short: "h" },
37
+ },
38
+ allowPositionals: false,
39
+ });
40
+
41
+ if (help) {
42
+ console.info(HELP);
43
+ return;
44
+ }
45
+
46
+ if (!repo) {
47
+ console.error("Missing prismic.config.json or --repo option");
48
+ process.exitCode = 1;
49
+ return;
50
+ }
51
+
52
+ const authenticated = await isAuthenticated();
53
+ if (!authenticated) {
54
+ handleUnauthenticated();
55
+ return;
56
+ }
57
+
58
+ const response = await getSimulatorUrl(repo);
59
+ if (!response.ok) {
60
+ if (response.error instanceof ForbiddenRequestError) {
61
+ handleUnauthenticated();
62
+ } else if (v.isValiError(response.error)) {
63
+ console.error(
64
+ `Failed to get simulator URL: Invalid response: ${stringify(response.error.issues)}`,
65
+ );
66
+ process.exitCode = 1;
67
+ } else {
68
+ console.error(`Failed to get simulator URL: ${stringify(response.value)}`);
69
+ process.exitCode = 1;
70
+ }
71
+ return;
72
+ }
73
+
74
+ const simulatorUrl = response.value.simulator_url;
75
+ if (!simulatorUrl) {
76
+ if (json) {
77
+ console.info(stringify({ simulator_url: null }));
78
+ } else {
79
+ console.info("No simulator URL configured.");
80
+ }
81
+ return;
82
+ }
83
+
84
+ if (json) {
85
+ console.info(stringify({ simulator_url: simulatorUrl }));
86
+ } else {
87
+ console.info(simulatorUrl);
88
+ }
89
+ }
90
+
91
+ const RepositoryResponseSchema = v.object({
92
+ simulator_url: v.optional(v.string()),
93
+ });
94
+ type RepositoryResponse = v.InferOutput<typeof RepositoryResponseSchema>;
95
+
96
+ async function getSimulatorUrl(repo: string): Promise<ParsedRequestResponse<RepositoryResponse>> {
97
+ const url = new URL("/core/repository", await getRepoUrl(repo));
98
+ return await request(url, { schema: RepositoryResponseSchema });
99
+ }
100
+
101
+ function handleUnauthenticated() {
102
+ console.error("Not logged in. Run `prismic login` first.");
103
+ process.exitCode = 1;
104
+ }
@@ -0,0 +1,106 @@
1
+ import { parseArgs } from "node:util";
2
+ import * as v from "valibot";
3
+
4
+ import { isAuthenticated } from "./lib/auth";
5
+ import { safeGetRepositoryFromConfig } from "./lib/config";
6
+ import { stringify } from "./lib/json";
7
+ import { ForbiddenRequestError, type ParsedRequestResponse, request } from "./lib/request";
8
+ import { getRepoUrl } from "./lib/url";
9
+
10
+ const HELP = `
11
+ List all preview configurations in a Prismic repository.
12
+
13
+ By default, this command reads the repository from prismic.config.json at the
14
+ project root.
15
+
16
+ USAGE
17
+ prismic preview list [flags]
18
+
19
+ FLAGS
20
+ --json Output as JSON
21
+ -r, --repo string Repository domain
22
+ -h, --help Show help for command
23
+
24
+ LEARN MORE
25
+ Use \`prismic <command> <subcommand> --help\` for more information about a command.
26
+ `.trim();
27
+
28
+ export async function previewList(): Promise<void> {
29
+ const {
30
+ values: { help, repo = await safeGetRepositoryFromConfig(), json },
31
+ } = parseArgs({
32
+ args: process.argv.slice(4), // skip: node, script, "preview", "list"
33
+ options: {
34
+ json: { type: "boolean" },
35
+ repo: { type: "string", short: "r" },
36
+ help: { type: "boolean", short: "h" },
37
+ },
38
+ allowPositionals: false,
39
+ });
40
+
41
+ if (help) {
42
+ console.info(HELP);
43
+ return;
44
+ }
45
+
46
+ if (!repo) {
47
+ console.error("Missing prismic.config.json or --repo option");
48
+ process.exitCode = 1;
49
+ return;
50
+ }
51
+
52
+ const authenticated = await isAuthenticated();
53
+ if (!authenticated) {
54
+ handleUnauthenticated();
55
+ return;
56
+ }
57
+
58
+ const response = await getPreviews(repo);
59
+ if (!response.ok) {
60
+ if (response.error instanceof ForbiddenRequestError) {
61
+ handleUnauthenticated();
62
+ } else if (v.isValiError(response.error)) {
63
+ console.error(
64
+ `Failed to list previews: Invalid response: ${stringify(response.error.issues)}`,
65
+ );
66
+ process.exitCode = 1;
67
+ } else {
68
+ console.error(`Failed to list previews: ${stringify(response.value)}`);
69
+ process.exitCode = 1;
70
+ }
71
+ return;
72
+ }
73
+
74
+ const previews = response.value.results;
75
+ if (json) {
76
+ console.info(stringify(previews));
77
+ } else {
78
+ for (const preview of previews) {
79
+ console.info(`${preview.url} ${preview.label}`);
80
+ }
81
+ }
82
+ }
83
+
84
+ const PreviewSchema = v.object({
85
+ id: v.string(),
86
+ label: v.string(),
87
+ url: v.string(),
88
+ });
89
+ export type Preview = v.InferOutput<typeof PreviewSchema>;
90
+
91
+ const GetPreviewsResponseSchema = v.object({
92
+ results: v.array(PreviewSchema),
93
+ });
94
+ type GetPreviewsResponse = v.InferOutput<typeof GetPreviewsResponseSchema>;
95
+
96
+ export async function getPreviews(
97
+ repo: string,
98
+ ): Promise<ParsedRequestResponse<GetPreviewsResponse>> {
99
+ const url = new URL("/core/repository/preview_configs", await getRepoUrl(repo));
100
+ return await request(url, { schema: GetPreviewsResponseSchema });
101
+ }
102
+
103
+ function handleUnauthenticated() {
104
+ console.error("Not logged in. Run `prismic login` first.");
105
+ process.exitCode = 1;
106
+ }
@@ -0,0 +1,80 @@
1
+ import { parseArgs } from "node:util";
2
+
3
+ import { isAuthenticated } from "./lib/auth";
4
+ import { safeGetRepositoryFromConfig } from "./lib/config";
5
+ import { stringify } from "./lib/json";
6
+ import { ForbiddenRequestError, request } from "./lib/request";
7
+ import { getRepoUrl } from "./lib/url";
8
+
9
+ const HELP = `
10
+ Remove the slice simulator URL from a Prismic repository.
11
+
12
+ By default, this command reads the repository from prismic.config.json at the
13
+ project root.
14
+
15
+ USAGE
16
+ prismic preview remove-simulator [flags]
17
+
18
+ FLAGS
19
+ -r, --repo string Repository domain
20
+ -h, --help Show help for command
21
+
22
+ LEARN MORE
23
+ Use \`prismic <command> <subcommand> --help\` for more information about a command.
24
+ `.trim();
25
+
26
+ export async function previewRemoveSimulator(): Promise<void> {
27
+ const {
28
+ values: { help, repo = await safeGetRepositoryFromConfig() },
29
+ } = parseArgs({
30
+ args: process.argv.slice(4), // skip: node, script, "preview", "remove-simulator"
31
+ options: {
32
+ repo: { type: "string", short: "r" },
33
+ help: { type: "boolean", short: "h" },
34
+ },
35
+ allowPositionals: false,
36
+ });
37
+
38
+ if (help) {
39
+ console.info(HELP);
40
+ return;
41
+ }
42
+
43
+ if (!repo) {
44
+ console.error("Missing prismic.config.json or --repo option");
45
+ process.exitCode = 1;
46
+ return;
47
+ }
48
+
49
+ const authenticated = await isAuthenticated();
50
+ if (!authenticated) {
51
+ handleUnauthenticated();
52
+ return;
53
+ }
54
+
55
+ const response = await removeSimulatorUrl(repo);
56
+ if (!response.ok) {
57
+ if (response.error instanceof ForbiddenRequestError) {
58
+ handleUnauthenticated();
59
+ } else {
60
+ console.error(`Failed to remove simulator URL: ${stringify(response.value)}`);
61
+ process.exitCode = 1;
62
+ }
63
+ return;
64
+ }
65
+
66
+ console.info("Simulator URL removed.");
67
+ }
68
+
69
+ async function removeSimulatorUrl(repo: string) {
70
+ const url = new URL("/core/repository", await getRepoUrl(repo));
71
+ return await request(url, {
72
+ method: "PATCH",
73
+ body: { simulator_url: "" },
74
+ });
75
+ }
76
+
77
+ function handleUnauthenticated() {
78
+ console.error("Not logged in. Run `prismic login` first.");
79
+ process.exitCode = 1;
80
+ }
@@ -0,0 +1,109 @@
1
+ import { parseArgs } from "node:util";
2
+
3
+ import { isAuthenticated } from "./lib/auth";
4
+ import { safeGetRepositoryFromConfig } from "./lib/config";
5
+ import { stringify } from "./lib/json";
6
+ import { ForbiddenRequestError, request } from "./lib/request";
7
+ import { getRepoUrl } from "./lib/url";
8
+ import { getPreviews } from "./preview-list";
9
+
10
+ const HELP = `
11
+ Remove a preview configuration from a Prismic repository.
12
+
13
+ By default, this command reads the repository from prismic.config.json at the
14
+ project root.
15
+
16
+ USAGE
17
+ prismic preview remove <url> [flags]
18
+
19
+ ARGUMENTS
20
+ <url> Preview URL to remove
21
+
22
+ FLAGS
23
+ -r, --repo string Repository domain
24
+ -h, --help Show help for command
25
+
26
+ LEARN MORE
27
+ Use \`prismic <command> <subcommand> --help\` for more information about a command.
28
+ `.trim();
29
+
30
+ export async function previewRemove(): Promise<void> {
31
+ const {
32
+ values: { help, repo = await safeGetRepositoryFromConfig() },
33
+ positionals: [previewUrl],
34
+ } = parseArgs({
35
+ args: process.argv.slice(4), // skip: node, script, "preview", "remove"
36
+ options: {
37
+ repo: { type: "string", short: "r" },
38
+ help: { type: "boolean", short: "h" },
39
+ },
40
+ allowPositionals: true,
41
+ });
42
+
43
+ if (help) {
44
+ console.info(HELP);
45
+ return;
46
+ }
47
+
48
+ if (!previewUrl) {
49
+ console.error("Missing required argument: <url>");
50
+ process.exitCode = 1;
51
+ return;
52
+ }
53
+
54
+ if (!repo) {
55
+ console.error("Missing prismic.config.json or --repo option");
56
+ process.exitCode = 1;
57
+ return;
58
+ }
59
+
60
+ const authenticated = await isAuthenticated();
61
+ if (!authenticated) {
62
+ handleUnauthenticated();
63
+ return;
64
+ }
65
+
66
+ const previewsResponse = await getPreviews(repo);
67
+ if (!previewsResponse.ok) {
68
+ if (previewsResponse.error instanceof ForbiddenRequestError) {
69
+ handleUnauthenticated();
70
+ } else {
71
+ console.error(`Failed to fetch previews: ${stringify(previewsResponse.value)}`);
72
+ process.exitCode = 1;
73
+ }
74
+ return;
75
+ }
76
+
77
+ const preview = previewsResponse.value.results.find((p) => p.url === previewUrl);
78
+ if (!preview) {
79
+ console.error(`Preview not found: ${previewUrl}`);
80
+ process.exitCode = 1;
81
+ return;
82
+ }
83
+
84
+ const response = await removePreview(repo, preview.id);
85
+ if (!response.ok) {
86
+ if (response.error instanceof ForbiddenRequestError) {
87
+ handleUnauthenticated();
88
+ } else {
89
+ console.error(`Failed to remove preview: ${stringify(response.value)}`);
90
+ process.exitCode = 1;
91
+ }
92
+ return;
93
+ }
94
+
95
+ console.info(`Preview removed: ${previewUrl}`);
96
+ }
97
+
98
+ async function removePreview(repo: string, id: string) {
99
+ const url = new URL(`/previews/delete/${id}`, await getRepoUrl(repo));
100
+ return await request(url, {
101
+ method: "POST",
102
+ body: {},
103
+ });
104
+ }
105
+
106
+ function handleUnauthenticated() {
107
+ console.error("Not logged in. Run `prismic login` first.");
108
+ process.exitCode = 1;
109
+ }
@@ -0,0 +1,137 @@
1
+ import { parseArgs } from "node:util";
2
+
3
+ import { isAuthenticated } from "./lib/auth";
4
+ import { safeGetRepositoryFromConfig } from "./lib/config";
5
+ import { stringify } from "./lib/json";
6
+ import { ForbiddenRequestError, request } from "./lib/request";
7
+ import { getRepoUrl } from "./lib/url";
8
+ import { parsePreviewUrl } from "./preview-add";
9
+ import { getPreviews } from "./preview-list";
10
+
11
+ const HELP = `
12
+ Update the name of a preview configuration.
13
+
14
+ By default, this command reads the repository from prismic.config.json at the
15
+ project root.
16
+
17
+ USAGE
18
+ prismic preview set-name <url> <name> [flags]
19
+
20
+ ARGUMENTS
21
+ <url> Preview URL to update
22
+ <name> New display name
23
+
24
+ FLAGS
25
+ -r, --repo string Repository domain
26
+ -h, --help Show help for command
27
+
28
+ LEARN MORE
29
+ Use \`prismic <command> <subcommand> --help\` for more information about a command.
30
+ `.trim();
31
+
32
+ export async function previewSetName(): Promise<void> {
33
+ const {
34
+ values: { help, repo = await safeGetRepositoryFromConfig() },
35
+ positionals: [previewUrl, name],
36
+ } = parseArgs({
37
+ args: process.argv.slice(4), // skip: node, script, "preview", "set-name"
38
+ options: {
39
+ repo: { type: "string", short: "r" },
40
+ help: { type: "boolean", short: "h" },
41
+ },
42
+ allowPositionals: true,
43
+ });
44
+
45
+ if (help) {
46
+ console.info(HELP);
47
+ return;
48
+ }
49
+
50
+ if (!previewUrl) {
51
+ console.error("Missing required argument: <url>");
52
+ process.exitCode = 1;
53
+ return;
54
+ }
55
+
56
+ if (!name) {
57
+ console.error("Missing required argument: <name>");
58
+ process.exitCode = 1;
59
+ return;
60
+ }
61
+
62
+ if (!repo) {
63
+ console.error("Missing prismic.config.json or --repo option");
64
+ process.exitCode = 1;
65
+ return;
66
+ }
67
+
68
+ const authenticated = await isAuthenticated();
69
+ if (!authenticated) {
70
+ handleUnauthenticated();
71
+ return;
72
+ }
73
+
74
+ const parsed = parsePreviewUrl(previewUrl);
75
+ if (!parsed) {
76
+ console.error(`Invalid URL: ${previewUrl}`);
77
+ process.exitCode = 1;
78
+ return;
79
+ }
80
+
81
+ const previewsResponse = await getPreviews(repo);
82
+ if (!previewsResponse.ok) {
83
+ if (previewsResponse.error instanceof ForbiddenRequestError) {
84
+ handleUnauthenticated();
85
+ } else {
86
+ console.error(`Failed to fetch previews: ${stringify(previewsResponse.value)}`);
87
+ process.exitCode = 1;
88
+ }
89
+ return;
90
+ }
91
+
92
+ const preview = previewsResponse.value.results.find((p) => p.url === previewUrl);
93
+ if (!preview) {
94
+ console.error(`Preview not found: ${previewUrl}`);
95
+ process.exitCode = 1;
96
+ return;
97
+ }
98
+
99
+ const response = await updatePreview(repo, preview.id, {
100
+ name,
101
+ websiteURL: parsed.websiteURL,
102
+ resolverPath: parsed.resolverPath,
103
+ });
104
+
105
+ if (!response.ok) {
106
+ if (response.error instanceof ForbiddenRequestError) {
107
+ handleUnauthenticated();
108
+ } else {
109
+ console.error(`Failed to update preview: ${stringify(response.value)}`);
110
+ process.exitCode = 1;
111
+ }
112
+ return;
113
+ }
114
+
115
+ console.info(`Preview updated: ${previewUrl}`);
116
+ }
117
+
118
+ async function updatePreview(
119
+ repo: string,
120
+ id: string,
121
+ config: { name: string; websiteURL: string; resolverPath: string | undefined },
122
+ ) {
123
+ const url = new URL(`/previews/save/${id}`, await getRepoUrl(repo));
124
+ return await request(url, {
125
+ method: "POST",
126
+ body: {
127
+ name: config.name,
128
+ websiteURL: config.websiteURL,
129
+ resolverPath: config.resolverPath,
130
+ },
131
+ });
132
+ }
133
+
134
+ function handleUnauthenticated() {
135
+ console.error("Not logged in. Run `prismic login` first.");
136
+ process.exitCode = 1;
137
+ }