@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,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
+ }
package/src/preview.ts ADDED
@@ -0,0 +1,60 @@
1
+ import { parseArgs } from "node:util";
2
+
3
+ import { previewAdd } from "./preview-add";
4
+ import { previewList } from "./preview-list";
5
+ import { previewRemove } from "./preview-remove";
6
+ import { previewSetName } from "./preview-set-name";
7
+
8
+ const HELP = `
9
+ Manage preview configurations in a Prismic repository.
10
+
11
+ USAGE
12
+ prismic preview <command> [flags]
13
+
14
+ COMMANDS
15
+ add Add a preview configuration
16
+ list List preview configurations
17
+ remove Remove a preview configuration
18
+ set-name Update a preview's name
19
+
20
+ FLAGS
21
+ -h, --help Show help for command
22
+
23
+ LEARN MORE
24
+ Use \`prismic preview <command> --help\` for more information about a command.
25
+ `.trim();
26
+
27
+ export async function preview(): Promise<void> {
28
+ const {
29
+ positionals: [subcommand],
30
+ } = parseArgs({
31
+ args: process.argv.slice(3), // skip: node, script, "preview"
32
+ options: {
33
+ help: { type: "boolean", short: "h" },
34
+ },
35
+ allowPositionals: true,
36
+ strict: false,
37
+ });
38
+
39
+ switch (subcommand) {
40
+ case "add":
41
+ await previewAdd();
42
+ break;
43
+ case "list":
44
+ await previewList();
45
+ break;
46
+ case "remove":
47
+ await previewRemove();
48
+ break;
49
+ case "set-name":
50
+ await previewSetName();
51
+ break;
52
+ default: {
53
+ if (subcommand) {
54
+ console.error(`Unknown preview subcommand: ${subcommand}\n`);
55
+ process.exitCode = 1;
56
+ }
57
+ console.info(HELP);
58
+ }
59
+ }
60
+ }
@@ -0,0 +1,136 @@
1
+ import { parseArgs } from "node:util";
2
+
3
+ import { isAuthenticated, readHost } from "./lib/auth";
4
+ import { createConfig, readConfig, updateConfig } 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
+ Create a new Prismic repository.
11
+
12
+ Creates prismic.config.json in the current directory. If a config file already
13
+ exists, use --replace to update it with the new repository.
14
+
15
+ USAGE
16
+ prismic repo create <domain> [flags]
17
+
18
+ ARGUMENTS
19
+ domain Repository domain (required). Must be at least 4 characters,
20
+ start and end with alphanumeric, and contain only alphanumerics and hyphens.
21
+
22
+ FLAGS
23
+ -n, --name string Display name for the repository (defaults to domain)
24
+ --no-config Skip creating or updating prismic.config.json
25
+ --replace Replace existing repositoryName in prismic.config.json
26
+ -h, --help Show help for command
27
+
28
+ LEARN MORE
29
+ Use \`prismic repo <command> --help\` for more information about a command.
30
+ `.trim();
31
+
32
+ const DOMAIN_REGEX = /^[a-zA-Z0-9][-a-zA-Z0-9]{2,}[a-zA-Z0-9]$/;
33
+
34
+ export async function repoCreate(): Promise<void> {
35
+ const {
36
+ values: { help, name, "no-config": noConfig, replace },
37
+ positionals: [domain],
38
+ } = parseArgs({
39
+ args: process.argv.slice(4), // skip: node, script, "repo", "create"
40
+ options: {
41
+ help: { type: "boolean", short: "h" },
42
+ name: { type: "string", short: "n" },
43
+ "no-config": { type: "boolean" },
44
+ replace: { type: "boolean" },
45
+ },
46
+ allowPositionals: true,
47
+ });
48
+
49
+ if (help) {
50
+ console.info(HELP);
51
+ return;
52
+ }
53
+
54
+ if (!domain) {
55
+ console.error("Missing required argument: domain");
56
+ process.exitCode = 1;
57
+ return;
58
+ }
59
+
60
+ if (!DOMAIN_REGEX.test(domain)) {
61
+ console.error("Invalid domain format.");
62
+ console.error(
63
+ "Must be at least 4 characters, start and end with alphanumeric, and contain only alphanumerics and hyphens.",
64
+ );
65
+ process.exitCode = 1;
66
+ return;
67
+ }
68
+
69
+ const authenticated = await isAuthenticated();
70
+ if (!authenticated) {
71
+ handleUnauthenticated();
72
+ return;
73
+ }
74
+
75
+ // Check existing config before repo creation (unless --no-config)
76
+ const existingConfig = await readConfig();
77
+ if (!noConfig && existingConfig.ok && !replace) {
78
+ console.error(`This project already has a repository: ${existingConfig.config.repositoryName}`);
79
+ console.error("Use --replace to replace it, or --no-config to skip config creation.");
80
+ process.exitCode = 1;
81
+ return;
82
+ }
83
+
84
+ const response = await createRepository(domain, name);
85
+ if (!response.ok) {
86
+ if (response.error instanceof ForbiddenRequestError) {
87
+ handleUnauthenticated();
88
+ } else {
89
+ console.error(`Failed to create repository: ${stringify(response.error)}`);
90
+ process.exitCode = 1;
91
+ }
92
+ return;
93
+ }
94
+
95
+ // Create or update config after successful repo creation
96
+ if (!noConfig) {
97
+ if (existingConfig.ok) {
98
+ const result = await updateConfig({ repositoryName: domain });
99
+ if (result.ok) {
100
+ console.info("Updated prismic.config.json");
101
+ } else {
102
+ console.warn("Could not update prismic.config.json: " + result.error.message);
103
+ }
104
+ } else {
105
+ const result = await createConfig({ repositoryName: domain });
106
+ if (result.ok) {
107
+ console.info("Created prismic.config.json");
108
+ } else {
109
+ console.warn("Could not create prismic.config.json: " + result.error.message);
110
+ }
111
+ }
112
+ }
113
+
114
+ console.info(`Repository created: ${domain}`);
115
+ console.info(`URL: ${await getRepoUrl(domain)}`);
116
+ }
117
+
118
+ async function createRepository(domain: string, name = domain) {
119
+ const url = new URL("/app/dashboard/repositories", await readHost());
120
+ return await request(url, {
121
+ method: "POST",
122
+ body: {
123
+ domain,
124
+ name,
125
+ framework: "next",
126
+ plan: "personal",
127
+ usageIntent: "Exploring Prismic's features for future projects.",
128
+ usageIntentIndex: 0,
129
+ },
130
+ });
131
+ }
132
+
133
+ function handleUnauthenticated() {
134
+ console.error("Not logged in. Run `prismic login` first.");
135
+ process.exitCode = 1;
136
+ }
@@ -0,0 +1,100 @@
1
+ import { parseArgs } from "node:util";
2
+ import * as v from "valibot";
3
+
4
+ import { isAuthenticated } from "./lib/auth";
5
+ import { stringify } from "./lib/json";
6
+ import { ForbiddenRequestError, request } from "./lib/request";
7
+ import { getRepoUrl, getUserServiceUrl } from "./lib/url";
8
+
9
+ const HELP = `
10
+ List all Prismic repositories.
11
+
12
+ USAGE
13
+ prismic repo list [flags]
14
+
15
+ FLAGS
16
+ --json Output as JSON
17
+ -h, --help Show help for command
18
+
19
+ LEARN MORE
20
+ Use \`prismic repo <command> --help\` for more information about a command.
21
+ `.trim();
22
+
23
+ const ProfileSchema = v.object({
24
+ repositories: v.array(
25
+ v.object({
26
+ domain: v.string(),
27
+ name: v.optional(v.string()),
28
+ role: v.string(),
29
+ }),
30
+ ),
31
+ });
32
+
33
+ export async function repoList(): Promise<void> {
34
+ const {
35
+ values: { help, json },
36
+ } = parseArgs({
37
+ args: process.argv.slice(4), // skip: node, script, "repo", "list"
38
+ options: {
39
+ help: { type: "boolean", short: "h" },
40
+ json: { type: "boolean" },
41
+ },
42
+ });
43
+
44
+ if (help) {
45
+ console.info(HELP);
46
+ return;
47
+ }
48
+
49
+ const authenticated = await isAuthenticated();
50
+ if (!authenticated) {
51
+ handleUnauthenticated();
52
+ return;
53
+ }
54
+
55
+ const response = await fetchProfile();
56
+ if (!response.ok) {
57
+ if (response.error instanceof ForbiddenRequestError) {
58
+ handleUnauthenticated();
59
+ } else {
60
+ console.error(`Failed to fetch repositories: ${stringify(response.value)}`);
61
+ process.exitCode = 1;
62
+ }
63
+ return;
64
+ }
65
+
66
+ const repos = response.value.repositories;
67
+
68
+ if (json) {
69
+ const output = await Promise.all(
70
+ repos.map(async (repo) => ({
71
+ domain: repo.domain,
72
+ name: repo.name || null,
73
+ role: repo.role,
74
+ url: (await getRepoUrl(repo.domain)).toString(),
75
+ })),
76
+ );
77
+ console.info(stringify(output));
78
+ return;
79
+ }
80
+
81
+ if (repos.length === 0) {
82
+ console.info("No repositories found.");
83
+ return;
84
+ }
85
+
86
+ for (const repo of repos) {
87
+ const name = repo.name || "(no name)";
88
+ console.info(`${repo.domain} ${name} ${repo.role}`);
89
+ }
90
+ }
91
+
92
+ async function fetchProfile(): ReturnType<typeof request<v.InferOutput<typeof ProfileSchema>>> {
93
+ const url = new URL("profile", await getUserServiceUrl());
94
+ return await request(url, { schema: ProfileSchema });
95
+ }
96
+
97
+ function handleUnauthenticated() {
98
+ console.error("Not logged in. Run `prismic login` first.");
99
+ process.exitCode = 1;
100
+ }
@@ -0,0 +1,102 @@
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, request } from "./lib/request";
8
+ import { getRepoUrl } from "./lib/url";
9
+
10
+ const HELP = `
11
+ Set the display name of 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 repo set-name <name> [flags]
18
+
19
+ ARGUMENTS
20
+ <name> The new display name for the repository
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 repoSetName(): Promise<void> {
31
+ const {
32
+ values: { help, repo = await safeGetRepositoryFromConfig() },
33
+ positionals: [displayName],
34
+ } = parseArgs({
35
+ args: process.argv.slice(4), // skip: node, script, "repo", "set-name"
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 (!displayName) {
49
+ console.error("Missing required argument: <name>");
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 response = await setRepositoryName(repo, displayName);
67
+ if (!response.ok) {
68
+ if (response.error instanceof ForbiddenRequestError) {
69
+ handleUnauthenticated();
70
+ } else if (v.isValiError(response.error)) {
71
+ console.error(
72
+ `Failed to set repository name: Invalid response: ${stringify(response.error.issues)}`,
73
+ );
74
+ process.exitCode = 1;
75
+ } else {
76
+ console.error(`Failed to set repository name: ${stringify(response.value)}`);
77
+ process.exitCode = 1;
78
+ }
79
+ return;
80
+ }
81
+
82
+ console.info(`Repository name set to: ${response.value.repository.name}`);
83
+ }
84
+
85
+ async function setRepositoryName(domain: string, displayName: string) {
86
+ const repoUrl = await getRepoUrl(domain);
87
+ const url = new URL("app/settings/repository", repoUrl);
88
+
89
+ const formData = new FormData();
90
+ formData.set("displayname", displayName);
91
+
92
+ return await request(url, {
93
+ method: "POST",
94
+ body: formData,
95
+ schema: v.object({ repository: v.object({ name: v.string() }) }),
96
+ });
97
+ }
98
+
99
+ function handleUnauthenticated() {
100
+ console.error("Not logged in. Run `prismic login` first.");
101
+ process.exitCode = 1;
102
+ }