@angeloashmore/prismic-cli-poc 0.0.0-canary.2ff9563

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 (119) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +98 -0
  3. package/dist/index.mjs +1996 -0
  4. package/package.json +52 -0
  5. package/src/custom-type-add-field-boolean.ts +171 -0
  6. package/src/custom-type-add-field-color.ts +158 -0
  7. package/src/custom-type-add-field-date.ts +161 -0
  8. package/src/custom-type-add-field-embed.ts +158 -0
  9. package/src/custom-type-add-field-geo-point.ts +155 -0
  10. package/src/custom-type-add-field-image.ts +158 -0
  11. package/src/custom-type-add-field-key-text.ts +158 -0
  12. package/src/custom-type-add-field-link.ts +180 -0
  13. package/src/custom-type-add-field-number.ts +190 -0
  14. package/src/custom-type-add-field-rich-text.ts +181 -0
  15. package/src/custom-type-add-field-select.ts +164 -0
  16. package/src/custom-type-add-field-timestamp.ts +161 -0
  17. package/src/custom-type-add-field-uid.ts +158 -0
  18. package/src/custom-type-add-field.ts +111 -0
  19. package/src/custom-type-connect-slice.ts +221 -0
  20. package/src/custom-type-create.ts +92 -0
  21. package/src/custom-type-disconnect-slice.ts +179 -0
  22. package/src/custom-type-list.ts +110 -0
  23. package/src/custom-type-remove-field.ts +161 -0
  24. package/src/custom-type-remove.ts +126 -0
  25. package/src/custom-type-set-name.ts +128 -0
  26. package/src/custom-type-view.ts +118 -0
  27. package/src/custom-type.ts +85 -0
  28. package/src/index.ts +100 -0
  29. package/src/init.ts +62 -0
  30. package/src/lib/auth.ts +60 -0
  31. package/src/lib/config.ts +111 -0
  32. package/src/lib/file.ts +49 -0
  33. package/src/lib/json.ts +3 -0
  34. package/src/lib/request.ts +116 -0
  35. package/src/lib/slice.ts +112 -0
  36. package/src/lib/url.ts +25 -0
  37. package/src/locale-add.ts +116 -0
  38. package/src/locale-list.ts +107 -0
  39. package/src/locale-remove.ts +88 -0
  40. package/src/locale-set-default.ts +131 -0
  41. package/src/locale.ts +60 -0
  42. package/src/login.ts +143 -0
  43. package/src/logout.ts +36 -0
  44. package/src/page-type-add-field-boolean.ts +171 -0
  45. package/src/page-type-add-field-color.ts +158 -0
  46. package/src/page-type-add-field-date.ts +161 -0
  47. package/src/page-type-add-field-embed.ts +158 -0
  48. package/src/page-type-add-field-geo-point.ts +155 -0
  49. package/src/page-type-add-field-image.ts +158 -0
  50. package/src/page-type-add-field-key-text.ts +158 -0
  51. package/src/page-type-add-field-link.ts +180 -0
  52. package/src/page-type-add-field-number.ts +190 -0
  53. package/src/page-type-add-field-rich-text.ts +181 -0
  54. package/src/page-type-add-field-select.ts +164 -0
  55. package/src/page-type-add-field-timestamp.ts +161 -0
  56. package/src/page-type-add-field-uid.ts +158 -0
  57. package/src/page-type-add-field.ts +111 -0
  58. package/src/page-type-connect-slice.ts +221 -0
  59. package/src/page-type-create.ts +93 -0
  60. package/src/page-type-disconnect-slice.ts +179 -0
  61. package/src/page-type-list.ts +109 -0
  62. package/src/page-type-remove-field.ts +161 -0
  63. package/src/page-type-remove.ts +126 -0
  64. package/src/page-type-set-name.ts +128 -0
  65. package/src/page-type-set-repeatable.ts +137 -0
  66. package/src/page-type-view.ts +118 -0
  67. package/src/page-type.ts +90 -0
  68. package/src/preview-add.ts +126 -0
  69. package/src/preview-list.ts +106 -0
  70. package/src/preview-remove.ts +109 -0
  71. package/src/preview-set-name.ts +137 -0
  72. package/src/preview.ts +60 -0
  73. package/src/repo-create.ts +136 -0
  74. package/src/repo-list.ts +100 -0
  75. package/src/repo-set-name.ts +102 -0
  76. package/src/repo-view.ts +113 -0
  77. package/src/repo.ts +60 -0
  78. package/src/slice-add-field-boolean.ts +150 -0
  79. package/src/slice-add-field-color.ts +137 -0
  80. package/src/slice-add-field-date.ts +137 -0
  81. package/src/slice-add-field-embed.ts +137 -0
  82. package/src/slice-add-field-geo-point.ts +134 -0
  83. package/src/slice-add-field-image.ts +134 -0
  84. package/src/slice-add-field-key-text.ts +137 -0
  85. package/src/slice-add-field-link.ts +155 -0
  86. package/src/slice-add-field-number.ts +137 -0
  87. package/src/slice-add-field-rich-text.ts +160 -0
  88. package/src/slice-add-field-select.ts +143 -0
  89. package/src/slice-add-field-timestamp.ts +137 -0
  90. package/src/slice-add-field.ts +106 -0
  91. package/src/slice-add-variation.ts +137 -0
  92. package/src/slice-create.ts +129 -0
  93. package/src/slice-list-variations.ts +67 -0
  94. package/src/slice-list.ts +88 -0
  95. package/src/slice-remove-field.ts +117 -0
  96. package/src/slice-remove-variation.ts +108 -0
  97. package/src/slice-remove.ts +81 -0
  98. package/src/slice-rename.ts +112 -0
  99. package/src/slice-view.ts +77 -0
  100. package/src/slice.ts +90 -0
  101. package/src/sync.ts +309 -0
  102. package/src/token-create.ts +185 -0
  103. package/src/token-delete.ts +161 -0
  104. package/src/token-list.ts +212 -0
  105. package/src/token-set-name.ts +165 -0
  106. package/src/token.ts +60 -0
  107. package/src/webhook-add-header.ts +118 -0
  108. package/src/webhook-create.ts +152 -0
  109. package/src/webhook-disable.ts +109 -0
  110. package/src/webhook-enable.ts +132 -0
  111. package/src/webhook-list.ts +93 -0
  112. package/src/webhook-remove-header.ts +117 -0
  113. package/src/webhook-remove.ts +106 -0
  114. package/src/webhook-set-triggers.ts +148 -0
  115. package/src/webhook-status.ts +90 -0
  116. package/src/webhook-test.ts +106 -0
  117. package/src/webhook-view.ts +147 -0
  118. package/src/webhook.ts +95 -0
  119. package/src/whoami.ts +62 -0
@@ -0,0 +1,132 @@
1
+ import { parseArgs } from "node:util";
2
+
3
+ import type { RequestResponse } from "./lib/request";
4
+
5
+ import { isAuthenticated } from "./lib/auth";
6
+ import { safeGetRepositoryFromConfig } from "./lib/config";
7
+ import { stringify } from "./lib/json";
8
+ import { ForbiddenRequestError, request } from "./lib/request";
9
+ import { getRepoUrl } from "./lib/url";
10
+ import { getWebhooks, type Webhook } from "./webhook-view";
11
+
12
+ const HELP = `
13
+ Enable a webhook in a Prismic repository.
14
+
15
+ By default, this command reads the repository from prismic.config.json at the
16
+ project root.
17
+
18
+ USAGE
19
+ prismic webhook enable <url> [flags]
20
+
21
+ ARGUMENTS
22
+ <url> Webhook URL
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 webhookEnable(): Promise<void> {
33
+ const {
34
+ values: { help, repo = await safeGetRepositoryFromConfig() },
35
+ positionals: [webhookUrl],
36
+ } = parseArgs({
37
+ args: process.argv.slice(4), // skip: node, script, "webhook", "enable"
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 (!webhookUrl) {
51
+ console.error("Missing required argument: <url>");
52
+ process.exitCode = 1;
53
+ return;
54
+ }
55
+
56
+ if (!repo) {
57
+ console.error("Missing prismic.config.json or --repo option");
58
+ process.exitCode = 1;
59
+ return;
60
+ }
61
+
62
+ const authenticated = await isAuthenticated();
63
+ if (!authenticated) {
64
+ handleUnauthenticated();
65
+ return;
66
+ }
67
+
68
+ const webhooksResponse = await getWebhooks(repo);
69
+ if (!webhooksResponse.ok) {
70
+ if (webhooksResponse.error instanceof ForbiddenRequestError) {
71
+ handleUnauthenticated();
72
+ } else {
73
+ console.error(`Failed to enable webhook: ${stringify(webhooksResponse.value)}`);
74
+ process.exitCode = 1;
75
+ }
76
+ return;
77
+ }
78
+
79
+ const webhook = webhooksResponse.value.find((w) => w.config.url === webhookUrl);
80
+ if (!webhook) {
81
+ console.error(`Webhook not found: ${webhookUrl}`);
82
+ process.exitCode = 1;
83
+ return;
84
+ }
85
+
86
+ if (webhook.config.active) {
87
+ console.info(`Webhook already enabled: ${webhookUrl}`);
88
+ return;
89
+ }
90
+
91
+ const updatedConfig = structuredClone(webhook.config);
92
+ updatedConfig.active = true;
93
+
94
+ const response = await updateWebhook(repo, webhook.config._id, updatedConfig);
95
+ if (!response.ok) {
96
+ if (response.error instanceof ForbiddenRequestError) {
97
+ handleUnauthenticated();
98
+ } else {
99
+ console.error(`Failed to enable webhook: ${stringify(response.value)}`);
100
+ process.exitCode = 1;
101
+ }
102
+ return;
103
+ }
104
+
105
+ console.info(`Webhook enabled: ${webhookUrl}`);
106
+ }
107
+
108
+ export async function updateWebhook(
109
+ repo: string,
110
+ webhookId: string,
111
+ config: Omit<Webhook["config"], "_id">,
112
+ ): Promise<RequestResponse<unknown>> {
113
+ const url = new URL(`/app/settings/webhooks/${webhookId}`, await getRepoUrl(repo));
114
+ const body = new FormData();
115
+ body.set("url", config.url);
116
+ body.set("name", config.name ?? "");
117
+ body.set("secret", config.secret ?? "");
118
+ body.set("headers", JSON.stringify(config.headers ?? {}));
119
+ body.set("active", config.active ? "on" : "off");
120
+ body.set("documentsPublished", config.documentsUnpublished.toString());
121
+ body.set("documentsUnpublished", config.documentsUnpublished.toString());
122
+ body.set("releasesCreated", config.documentsUnpublished.toString());
123
+ body.set("releasesUpdated", config.documentsUnpublished.toString());
124
+ body.set("tagsCreated", config.documentsUnpublished.toString());
125
+ body.set("documentsPublished", config.documentsUnpublished.toString());
126
+ return await request(url, { method: "POST", body });
127
+ }
128
+
129
+ function handleUnauthenticated() {
130
+ console.error("Not logged in. Run `prismic login` first.");
131
+ process.exitCode = 1;
132
+ }
@@ -0,0 +1,93 @@
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 } from "./lib/request";
8
+ import { getWebhooks } from "./webhook-view";
9
+
10
+ const HELP = `
11
+ List all webhooks 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 webhook 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 webhookList(): Promise<void> {
29
+ const {
30
+ values: { help, repo = await safeGetRepositoryFromConfig(), json },
31
+ } = parseArgs({
32
+ args: process.argv.slice(4), // skip: node, script, "webhook", "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 getWebhooks(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 webhooks: Invalid response: ${stringify(response.error.issues)}`,
65
+ );
66
+ process.exitCode = 1;
67
+ } else {
68
+ console.error(`Failed to list webhooks: ${stringify(response.value)}`);
69
+ process.exitCode = 1;
70
+ }
71
+ return;
72
+ }
73
+
74
+ const webhooks = response.value;
75
+ if (json) {
76
+ console.info(stringify(webhooks.map((w) => w.config)));
77
+ } else {
78
+ if (webhooks.length === 0) {
79
+ console.info("No webhooks configured.");
80
+ return;
81
+ }
82
+ for (const webhook of webhooks) {
83
+ const status = webhook.config.active ? "enabled" : "disabled";
84
+ const name = webhook.config.name ? ` (${webhook.config.name})` : "";
85
+ console.info(`${webhook.config.url}${name} [${status}]`);
86
+ }
87
+ }
88
+ }
89
+
90
+ function handleUnauthenticated() {
91
+ console.error("Not logged in. Run `prismic login` first.");
92
+ process.exitCode = 1;
93
+ }
@@ -0,0 +1,117 @@
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 } from "./lib/request";
7
+ import { updateWebhook } from "./webhook-enable";
8
+ import { getWebhooks } from "./webhook-view";
9
+
10
+ const HELP = `
11
+ Remove a custom HTTP header from a webhook.
12
+
13
+ By default, this command reads the repository from prismic.config.json at the
14
+ project root.
15
+
16
+ USAGE
17
+ prismic webhook remove-header <url> <key> [flags]
18
+
19
+ ARGUMENTS
20
+ <url> Webhook URL
21
+ <key> Header name
22
+
23
+ FLAGS
24
+ -r, --repo string Repository domain
25
+ -h, --help Show help for command
26
+
27
+ LEARN MORE
28
+ Use \`prismic <command> <subcommand> --help\` for more information about a command.
29
+ `.trim();
30
+
31
+ export async function webhookRemoveHeader(): Promise<void> {
32
+ const {
33
+ values: { help, repo = await safeGetRepositoryFromConfig() },
34
+ positionals: [webhookUrl, headerKey],
35
+ } = parseArgs({
36
+ args: process.argv.slice(4), // skip: node, script, "webhook", "remove-header"
37
+ options: {
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 (!webhookUrl) {
50
+ console.error("Missing required argument: <url>");
51
+ process.exitCode = 1;
52
+ return;
53
+ }
54
+
55
+ if (!headerKey) {
56
+ console.error("Missing required argument: <key>");
57
+ process.exitCode = 1;
58
+ return;
59
+ }
60
+
61
+ if (!repo) {
62
+ console.error("Missing prismic.config.json or --repo option");
63
+ process.exitCode = 1;
64
+ return;
65
+ }
66
+
67
+ const authenticated = await isAuthenticated();
68
+ if (!authenticated) {
69
+ handleUnauthenticated();
70
+ return;
71
+ }
72
+
73
+ const webhooksResponse = await getWebhooks(repo);
74
+ if (!webhooksResponse.ok) {
75
+ if (webhooksResponse.error instanceof ForbiddenRequestError) {
76
+ handleUnauthenticated();
77
+ } else {
78
+ console.error(`Failed to remove header: ${stringify(webhooksResponse.value)}`);
79
+ process.exitCode = 1;
80
+ }
81
+ return;
82
+ }
83
+
84
+ const webhook = webhooksResponse.value.find((w) => w.config.url === webhookUrl);
85
+ if (!webhook) {
86
+ console.error(`Webhook not found: ${webhookUrl}`);
87
+ process.exitCode = 1;
88
+ return;
89
+ }
90
+
91
+ if (!(headerKey in webhook.config.headers)) {
92
+ console.error(`Header not found: ${headerKey}`);
93
+ process.exitCode = 1;
94
+ return;
95
+ }
96
+
97
+ const updatedConfig = structuredClone(webhook.config);
98
+ delete updatedConfig.headers[headerKey];
99
+
100
+ const response = await updateWebhook(repo, webhook.config._id, updatedConfig);
101
+ if (!response.ok) {
102
+ if (response.error instanceof ForbiddenRequestError) {
103
+ handleUnauthenticated();
104
+ } else {
105
+ console.error(`Failed to remove header: ${stringify(response.value)}`);
106
+ process.exitCode = 1;
107
+ }
108
+ return;
109
+ }
110
+
111
+ console.info(`Header removed: ${headerKey}`);
112
+ }
113
+
114
+ function handleUnauthenticated() {
115
+ console.error("Not logged in. Run `prismic login` first.");
116
+ process.exitCode = 1;
117
+ }
@@ -0,0 +1,106 @@
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 { getWebhooks } from "./webhook-view";
9
+
10
+ const HELP = `
11
+ Delete a webhook 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 webhook remove <url> [flags]
18
+
19
+ ARGUMENTS
20
+ <url> Webhook URL
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 webhookRemove(): Promise<void> {
31
+ const {
32
+ values: { help, repo = await safeGetRepositoryFromConfig() },
33
+ positionals: [webhookUrl],
34
+ } = parseArgs({
35
+ args: process.argv.slice(4), // skip: node, script, "webhook", "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 (!webhookUrl) {
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 webhooksResponse = await getWebhooks(repo);
67
+ if (!webhooksResponse.ok) {
68
+ if (webhooksResponse.error instanceof ForbiddenRequestError) {
69
+ handleUnauthenticated();
70
+ } else {
71
+ console.error(`Failed to remove webhook: ${stringify(webhooksResponse.value)}`);
72
+ process.exitCode = 1;
73
+ }
74
+ return;
75
+ }
76
+
77
+ const webhook = webhooksResponse.value.find((w) => w.config.url === webhookUrl);
78
+ if (!webhook) {
79
+ console.error(`Webhook not found: ${webhookUrl}`);
80
+ process.exitCode = 1;
81
+ return;
82
+ }
83
+
84
+ const response = await deleteWebhook(repo, webhook.config._id);
85
+ if (!response.ok) {
86
+ if (response.error instanceof ForbiddenRequestError) {
87
+ handleUnauthenticated();
88
+ } else {
89
+ console.error(`Failed to remove webhook: ${stringify(response.value)}`);
90
+ process.exitCode = 1;
91
+ }
92
+ return;
93
+ }
94
+
95
+ console.info(`Webhook removed: ${webhookUrl}`);
96
+ }
97
+
98
+ async function deleteWebhook(repo: string, webhookId: string) {
99
+ const url = new URL(`/app/settings/webhooks/${webhookId}/delete`, await getRepoUrl(repo));
100
+ return await request(url, { method: "POST" });
101
+ }
102
+
103
+ function handleUnauthenticated() {
104
+ console.error("Not logged in. Run `prismic login` first.");
105
+ process.exitCode = 1;
106
+ }
@@ -0,0 +1,148 @@
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 } from "./lib/request";
7
+ import { updateWebhook } from "./webhook-enable";
8
+ import { getWebhooks, TRIGGER_DISPLAY } from "./webhook-view";
9
+
10
+ const HELP = `
11
+ Update which events trigger a webhook.
12
+
13
+ By default, this command reads the repository from prismic.config.json at the
14
+ project root.
15
+
16
+ USAGE
17
+ prismic webhook set-triggers <url> [flags]
18
+
19
+ ARGUMENTS
20
+ <url> Webhook URL
21
+
22
+ FLAGS
23
+ -t, --trigger string Trigger events (can be repeated, at least one required)
24
+ -r, --repo string Repository domain
25
+ -h, --help Show help for command
26
+
27
+ TRIGGERS
28
+ document.published When documents are published
29
+ document.unpublished When documents are unpublished
30
+ release.created When a release is created
31
+ release.updated When a release is edited or deleted
32
+ tag.created When a tag is created
33
+ tag.deleted When a tag is deleted
34
+
35
+ LEARN MORE
36
+ Use \`prismic <command> <subcommand> --help\` for more information about a command.
37
+ `.trim();
38
+
39
+ const VALID_TRIGGERS = Object.values(TRIGGER_DISPLAY);
40
+
41
+ export async function webhookSetTriggers(): Promise<void> {
42
+ const {
43
+ values: { help, repo = await safeGetRepositoryFromConfig(), trigger },
44
+ positionals: [webhookUrl],
45
+ } = parseArgs({
46
+ args: process.argv.slice(4), // skip: node, script, "webhook", "set-triggers"
47
+ options: {
48
+ trigger: { type: "string", multiple: true, short: "t" },
49
+ repo: { type: "string", short: "r" },
50
+ help: { type: "boolean", short: "h" },
51
+ },
52
+ allowPositionals: true,
53
+ });
54
+
55
+ if (help) {
56
+ console.info(HELP);
57
+ return;
58
+ }
59
+
60
+ if (!webhookUrl) {
61
+ console.error("Missing required argument: <url>");
62
+ process.exitCode = 1;
63
+ return;
64
+ }
65
+
66
+ if (!repo) {
67
+ console.error("Missing prismic.config.json or --repo option");
68
+ process.exitCode = 1;
69
+ return;
70
+ }
71
+
72
+ if (!trigger || trigger.length === 0) {
73
+ console.error("Missing required option: --trigger");
74
+ process.exitCode = 1;
75
+ return;
76
+ }
77
+
78
+ // Validate triggers
79
+ for (const t of trigger) {
80
+ if (!VALID_TRIGGERS.includes(t)) {
81
+ console.error(`Invalid trigger: ${t}`);
82
+ console.error(`Valid triggers: ${VALID_TRIGGERS.join(", ")}`);
83
+ process.exitCode = 1;
84
+ return;
85
+ }
86
+ }
87
+
88
+ const authenticated = await isAuthenticated();
89
+ if (!authenticated) {
90
+ handleUnauthenticated();
91
+ return;
92
+ }
93
+
94
+ const webhooksResponse = await getWebhooks(repo);
95
+ if (!webhooksResponse.ok) {
96
+ if (webhooksResponse.error instanceof ForbiddenRequestError) {
97
+ handleUnauthenticated();
98
+ } else {
99
+ console.error(`Failed to update webhook triggers: ${stringify(webhooksResponse.value)}`);
100
+ process.exitCode = 1;
101
+ }
102
+ return;
103
+ }
104
+
105
+ const webhook = webhooksResponse.value.find((w) => w.config.url === webhookUrl);
106
+ if (!webhook) {
107
+ console.error(`Webhook not found: ${webhookUrl}`);
108
+ process.exitCode = 1;
109
+ return;
110
+ }
111
+
112
+ // Build trigger settings: all false, then enable specified ones
113
+ const defaultValue = trigger.length > 0 ? false : true;
114
+ const triggers: Record<keyof typeof TRIGGER_DISPLAY, boolean> = {
115
+ documentsPublished: defaultValue,
116
+ documentsUnpublished: defaultValue,
117
+ releasesCreated: defaultValue,
118
+ releasesUpdated: defaultValue,
119
+ tagsCreated: defaultValue,
120
+ tagsDeleted: defaultValue,
121
+ };
122
+ for (const t of trigger) {
123
+ const [apiField] = Object.entries(TRIGGER_DISPLAY).find(([, display]) => t === display) ?? [];
124
+ if (!apiField) continue;
125
+ triggers[apiField as keyof typeof TRIGGER_DISPLAY] = true;
126
+ }
127
+
128
+ const response = await updateWebhook(repo, webhook.config._id, {
129
+ ...webhook.config,
130
+ ...triggers,
131
+ });
132
+ if (!response.ok) {
133
+ if (response.error instanceof ForbiddenRequestError) {
134
+ handleUnauthenticated();
135
+ } else {
136
+ console.error(`Failed to update webhook triggers: ${stringify(response.value)}`);
137
+ process.exitCode = 1;
138
+ }
139
+ return;
140
+ }
141
+
142
+ console.info(`Webhook triggers updated: ${trigger.join(", ")}`);
143
+ }
144
+
145
+ function handleUnauthenticated() {
146
+ console.error("Not logged in. Run `prismic login` first.");
147
+ process.exitCode = 1;
148
+ }
@@ -0,0 +1,90 @@
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 } from "./lib/request";
7
+ import { getWebhooks } from "./webhook-view";
8
+
9
+ const HELP = `
10
+ Show the enabled/disabled status of a webhook.
11
+
12
+ By default, this command reads the repository from prismic.config.json at the
13
+ project root.
14
+
15
+ USAGE
16
+ prismic webhook status <url> [flags]
17
+
18
+ ARGUMENTS
19
+ <url> Webhook URL
20
+
21
+ FLAGS
22
+ -r, --repo string Repository domain
23
+ -h, --help Show help for command
24
+
25
+ LEARN MORE
26
+ Use \`prismic <command> <subcommand> --help\` for more information about a command.
27
+ `.trim();
28
+
29
+ export async function webhookStatus(): Promise<void> {
30
+ const {
31
+ values: { help, repo = await safeGetRepositoryFromConfig() },
32
+ positionals: [webhookUrl],
33
+ } = parseArgs({
34
+ args: process.argv.slice(4), // skip: node, script, "webhook", "status"
35
+ options: {
36
+ repo: { type: "string", short: "r" },
37
+ help: { type: "boolean", short: "h" },
38
+ },
39
+ allowPositionals: true,
40
+ });
41
+
42
+ if (help) {
43
+ console.info(HELP);
44
+ return;
45
+ }
46
+
47
+ if (!webhookUrl) {
48
+ console.error("Missing required argument: <url>");
49
+ process.exitCode = 1;
50
+ return;
51
+ }
52
+
53
+ if (!repo) {
54
+ console.error("Missing prismic.config.json or --repo option");
55
+ process.exitCode = 1;
56
+ return;
57
+ }
58
+
59
+ const authenticated = await isAuthenticated();
60
+ if (!authenticated) {
61
+ handleUnauthenticated();
62
+ return;
63
+ }
64
+
65
+ const response = await getWebhooks(repo);
66
+ if (!response.ok) {
67
+ if (response.error instanceof ForbiddenRequestError) {
68
+ handleUnauthenticated();
69
+ } else {
70
+ console.error(`Failed to get webhook status: ${stringify(response.value)}`);
71
+ process.exitCode = 1;
72
+ }
73
+ return;
74
+ }
75
+
76
+ const webhook = response.value.find((w) => w.config.url === webhookUrl);
77
+ if (!webhook) {
78
+ console.error(`Webhook not found: ${webhookUrl}`);
79
+ process.exitCode = 1;
80
+ return;
81
+ }
82
+
83
+ const status = webhook.config.active ? "enabled" : "disabled";
84
+ console.info(status);
85
+ }
86
+
87
+ function handleUnauthenticated() {
88
+ console.error("Not logged in. Run `prismic login` first.");
89
+ process.exitCode = 1;
90
+ }