@angeloashmore/prismic-cli-poc 0.0.0-canary.1143872

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 (139) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +98 -0
  3. package/dist/index.mjs +2508 -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 +222 -0
  8. package/src/custom-type-add-field-color.ts +205 -0
  9. package/src/custom-type-add-field-date.ts +208 -0
  10. package/src/custom-type-add-field-embed.ts +205 -0
  11. package/src/custom-type-add-field-geo-point.ts +202 -0
  12. package/src/custom-type-add-field-group.ts +179 -0
  13. package/src/custom-type-add-field-image.ts +205 -0
  14. package/src/custom-type-add-field-key-text.ts +205 -0
  15. package/src/custom-type-add-field-link.ts +228 -0
  16. package/src/custom-type-add-field-number.ts +237 -0
  17. package/src/custom-type-add-field-rich-text.ts +229 -0
  18. package/src/custom-type-add-field-select.ts +211 -0
  19. package/src/custom-type-add-field-timestamp.ts +208 -0
  20. package/src/custom-type-add-field-uid.ts +188 -0
  21. package/src/custom-type-add-field.ts +116 -0
  22. package/src/custom-type-connect-slice.ts +214 -0
  23. package/src/custom-type-create.ts +112 -0
  24. package/src/custom-type-disconnect-slice.ts +171 -0
  25. package/src/custom-type-list.ts +110 -0
  26. package/src/custom-type-remove-field.ts +171 -0
  27. package/src/custom-type-remove.ts +138 -0
  28. package/src/custom-type-set-name.ts +138 -0
  29. package/src/custom-type-view.ts +118 -0
  30. package/src/custom-type.ts +85 -0
  31. package/src/docs-fetch.ts +146 -0
  32. package/src/docs-list.ts +131 -0
  33. package/src/docs.ts +54 -0
  34. package/src/index.ts +132 -0
  35. package/src/init.ts +64 -0
  36. package/src/lib/auth.ts +83 -0
  37. package/src/lib/config.ts +111 -0
  38. package/src/lib/custom-types-api.ts +438 -0
  39. package/src/lib/field-path.ts +81 -0
  40. package/src/lib/file.ts +49 -0
  41. package/src/lib/framework.ts +143 -0
  42. package/src/lib/json.ts +3 -0
  43. package/src/lib/request.ts +116 -0
  44. package/src/lib/slice.ts +115 -0
  45. package/src/lib/string.ts +6 -0
  46. package/src/lib/url.ts +25 -0
  47. package/src/locale-add.ts +116 -0
  48. package/src/locale-list.ts +107 -0
  49. package/src/locale-remove.ts +88 -0
  50. package/src/locale-set-default.ts +131 -0
  51. package/src/locale.ts +60 -0
  52. package/src/login.ts +152 -0
  53. package/src/logout.ts +36 -0
  54. package/src/page-type-add-field-boolean.ts +238 -0
  55. package/src/page-type-add-field-color.ts +224 -0
  56. package/src/page-type-add-field-date.ts +227 -0
  57. package/src/page-type-add-field-embed.ts +224 -0
  58. package/src/page-type-add-field-geo-point.ts +221 -0
  59. package/src/page-type-add-field-group.ts +198 -0
  60. package/src/page-type-add-field-image.ts +224 -0
  61. package/src/page-type-add-field-key-text.ts +224 -0
  62. package/src/page-type-add-field-link.ts +247 -0
  63. package/src/page-type-add-field-number.ts +256 -0
  64. package/src/page-type-add-field-rich-text.ts +248 -0
  65. package/src/page-type-add-field-select.ts +230 -0
  66. package/src/page-type-add-field-timestamp.ts +227 -0
  67. package/src/page-type-add-field-uid.ts +207 -0
  68. package/src/page-type-add-field.ts +116 -0
  69. package/src/page-type-connect-slice.ts +214 -0
  70. package/src/page-type-create.ts +161 -0
  71. package/src/page-type-disconnect-slice.ts +171 -0
  72. package/src/page-type-list.ts +109 -0
  73. package/src/page-type-remove-field.ts +171 -0
  74. package/src/page-type-remove.ts +138 -0
  75. package/src/page-type-set-name.ts +138 -0
  76. package/src/page-type-set-repeatable.ts +147 -0
  77. package/src/page-type-view.ts +118 -0
  78. package/src/page-type.ts +90 -0
  79. package/src/preview-add.ts +126 -0
  80. package/src/preview-get-simulator.ts +104 -0
  81. package/src/preview-list.ts +106 -0
  82. package/src/preview-remove-simulator.ts +80 -0
  83. package/src/preview-remove.ts +109 -0
  84. package/src/preview-set-name.ts +137 -0
  85. package/src/preview-set-simulator.ts +116 -0
  86. package/src/preview.ts +75 -0
  87. package/src/pull.ts +242 -0
  88. package/src/push.ts +405 -0
  89. package/src/repo-create.ts +195 -0
  90. package/src/repo-get-access.ts +86 -0
  91. package/src/repo-list.ts +100 -0
  92. package/src/repo-set-access.ts +100 -0
  93. package/src/repo-set-name.ts +102 -0
  94. package/src/repo-view.ts +113 -0
  95. package/src/repo.ts +70 -0
  96. package/src/slice-add-field-boolean.ts +240 -0
  97. package/src/slice-add-field-color.ts +226 -0
  98. package/src/slice-add-field-date.ts +226 -0
  99. package/src/slice-add-field-embed.ts +226 -0
  100. package/src/slice-add-field-geo-point.ts +223 -0
  101. package/src/slice-add-field-group.ts +191 -0
  102. package/src/slice-add-field-image.ts +223 -0
  103. package/src/slice-add-field-key-text.ts +226 -0
  104. package/src/slice-add-field-link.ts +245 -0
  105. package/src/slice-add-field-number.ts +226 -0
  106. package/src/slice-add-field-rich-text.ts +250 -0
  107. package/src/slice-add-field-select.ts +232 -0
  108. package/src/slice-add-field-timestamp.ts +226 -0
  109. package/src/slice-add-field.ts +111 -0
  110. package/src/slice-add-variation.ts +139 -0
  111. package/src/slice-create.ts +203 -0
  112. package/src/slice-list-variations.ts +67 -0
  113. package/src/slice-list.ts +88 -0
  114. package/src/slice-remove-field.ts +122 -0
  115. package/src/slice-remove-variation.ts +112 -0
  116. package/src/slice-remove.ts +91 -0
  117. package/src/slice-rename.ts +122 -0
  118. package/src/slice-set-screenshot.ts +235 -0
  119. package/src/slice-view.ts +80 -0
  120. package/src/slice.ts +95 -0
  121. package/src/status.ts +873 -0
  122. package/src/token-create.ts +203 -0
  123. package/src/token-delete.ts +182 -0
  124. package/src/token-list.ts +223 -0
  125. package/src/token-set-name.ts +193 -0
  126. package/src/token.ts +60 -0
  127. package/src/webhook-add-header.ts +118 -0
  128. package/src/webhook-create.ts +152 -0
  129. package/src/webhook-disable.ts +109 -0
  130. package/src/webhook-enable.ts +132 -0
  131. package/src/webhook-list.ts +93 -0
  132. package/src/webhook-remove-header.ts +117 -0
  133. package/src/webhook-remove.ts +106 -0
  134. package/src/webhook-set-triggers.ts +148 -0
  135. package/src/webhook-status.ts +90 -0
  136. package/src/webhook-test.ts +106 -0
  137. package/src/webhook-view.ts +147 -0
  138. package/src/webhook.ts +95 -0
  139. package/src/whoami.ts +62 -0
@@ -0,0 +1,230 @@
1
+ import type { CustomType, Select } from "@prismicio/types-internal/lib/customtypes";
2
+
3
+ import { readFile, writeFile } from "node:fs/promises";
4
+ import { parseArgs } from "node:util";
5
+ import * as v from "valibot";
6
+
7
+ import { buildTypes } from "./codegen-types";
8
+ import { findUpward } from "./lib/file";
9
+ import { findGroupInTab, isGroupField, parseFieldPath, validateNestedFieldPath } from "./lib/field-path";
10
+ import { type Framework, detectFrameworkInfo } from "./lib/framework";
11
+ import { stringify } from "./lib/json";
12
+ import { humanReadable } from "./lib/string";
13
+
14
+ const HELP = `
15
+ Add a select (dropdown) field to an existing page type.
16
+
17
+ USAGE
18
+ prismic page-type add-field select <type-id> <field-id> [flags]
19
+
20
+ ARGUMENTS
21
+ type-id Page type identifier (required)
22
+ field-id Field identifier (required)
23
+
24
+ FLAGS
25
+ -t, --tab string Target tab (default: first existing tab, or "Main")
26
+ -l, --label string Display label for the field (inferred from field-id if omitted)
27
+ -p, --placeholder string Placeholder text
28
+ --option string Add an option (can be used multiple times)
29
+ --default string Default selected value
30
+ --types string Output file for generated types (default: "prismicio-types.d.ts")
31
+ -h, --help Show help for command
32
+
33
+ EXAMPLES
34
+ prismic page-type add-field select homepage layout --option "full" --option "sidebar"
35
+ prismic page-type add-field select product size --option "small" --option "medium" --option "large" --default "medium"
36
+ prismic page-type add-field select article status --option "draft" --option "published" --label "Status"
37
+ `.trim();
38
+
39
+ const CustomTypeSchema = v.object({
40
+ id: v.string(),
41
+ label: v.string(),
42
+ repeatable: v.boolean(),
43
+ status: v.boolean(),
44
+ format: v.string(),
45
+ json: v.record(v.string(), v.record(v.string(), v.unknown())),
46
+ });
47
+
48
+ function getDocsPath(framework: Framework): string {
49
+ switch (framework) {
50
+ case "next":
51
+ return "nextjs/with-cli";
52
+ case "nuxt":
53
+ return "nuxt/with-cli";
54
+ case "sveltekit":
55
+ return "sveltekit/with-cli";
56
+ }
57
+ }
58
+
59
+ export async function pageTypeAddFieldSelect(): Promise<void> {
60
+ const {
61
+ values: { help, tab, label, placeholder, option, default: defaultValue, types },
62
+ positionals: [typeId, fieldId],
63
+ } = parseArgs({
64
+ args: process.argv.slice(5), // skip: node, script, "page-type", "add-field", "select"
65
+ options: {
66
+ tab: { type: "string", short: "t" },
67
+ label: { type: "string", short: "l" },
68
+ placeholder: { type: "string", short: "p" },
69
+ option: { type: "string", multiple: true },
70
+ default: { type: "string" },
71
+ types: { type: "string" },
72
+ help: { type: "boolean", short: "h" },
73
+ },
74
+ allowPositionals: true,
75
+ });
76
+
77
+ if (help) {
78
+ console.info(HELP);
79
+ return;
80
+ }
81
+
82
+ if (!typeId) {
83
+ console.error("Missing required argument: type-id\n");
84
+ console.error("Usage: prismic page-type add-field select <type-id> <field-id>");
85
+ process.exitCode = 1;
86
+ return;
87
+ }
88
+
89
+ if (!fieldId) {
90
+ console.error("Missing required argument: field-id\n");
91
+ console.error("Usage: prismic page-type add-field select <type-id> <field-id>");
92
+ process.exitCode = 1;
93
+ return;
94
+ }
95
+
96
+ // Parse and validate field path
97
+ const fieldPath = parseFieldPath(fieldId);
98
+ const pathValidation = validateNestedFieldPath(fieldPath);
99
+ if (!pathValidation.ok) {
100
+ console.error(pathValidation.error);
101
+ process.exitCode = 1;
102
+ return;
103
+ }
104
+
105
+ // Find the page type file
106
+ const projectRoot = await findUpward("package.json");
107
+ if (!projectRoot) {
108
+ console.error("Could not find project root (no package.json found)");
109
+ process.exitCode = 1;
110
+ return;
111
+ }
112
+
113
+ const modelPath = new URL(`customtypes/${typeId}/index.json`, projectRoot);
114
+
115
+ // Read and parse the model
116
+ let model: CustomType;
117
+ try {
118
+ const contents = await readFile(modelPath, "utf8");
119
+ const result = v.safeParse(CustomTypeSchema, JSON.parse(contents));
120
+ if (!result.success) {
121
+ console.error(`Invalid page type model: ${modelPath.href}`);
122
+ process.exitCode = 1;
123
+ return;
124
+ }
125
+ model = result.output as CustomType;
126
+ } catch (error) {
127
+ if (error instanceof Error && "code" in error && error.code === "ENOENT") {
128
+ console.error(`Page type not found: ${typeId}\n`);
129
+ console.error(`Create it first with: prismic page-type create ${typeId}`);
130
+ process.exitCode = 1;
131
+ return;
132
+ }
133
+ if (error instanceof Error) {
134
+ console.error(`Failed to read page type: ${error.message}`);
135
+ } else {
136
+ console.error("Failed to read page type");
137
+ }
138
+ process.exitCode = 1;
139
+ return;
140
+ }
141
+
142
+ // Determine target tab
143
+ const existingTabs = Object.keys(model.json);
144
+ const targetTab = tab ?? existingTabs[0] ?? "Main";
145
+
146
+ // Initialize tab if it doesn't exist
147
+ if (!model.json[targetTab]) {
148
+ model.json[targetTab] = {};
149
+ }
150
+
151
+ // Build field definition
152
+ const fieldDefinition: Select = {
153
+ type: "Select",
154
+ config: {
155
+ label: label ?? humanReadable(fieldPath.type === "nested" ? fieldPath.nestedFieldId : fieldId),
156
+ ...(placeholder && { placeholder }),
157
+ ...(option && option.length > 0 && { options: option }),
158
+ ...(defaultValue && { default_value: defaultValue }),
159
+ },
160
+ };
161
+
162
+ // Add field to model
163
+ if (fieldPath.type === "nested") {
164
+ const groupResult = findGroupInTab(model.json[targetTab], fieldPath.groupId, targetTab);
165
+ if (!groupResult.ok) {
166
+ console.error(groupResult.error);
167
+ process.exitCode = 1;
168
+ return;
169
+ }
170
+ if (groupResult.group.config.fields[fieldPath.nestedFieldId]) {
171
+ console.error(`Field "${fieldPath.nestedFieldId}" already exists in group "${fieldPath.groupId}"`);
172
+ process.exitCode = 1;
173
+ return;
174
+ }
175
+ groupResult.group.config.fields[fieldPath.nestedFieldId] = fieldDefinition;
176
+ } else {
177
+ for (const [tabName, tabFields] of Object.entries(model.json)) {
178
+ if (tabFields[fieldId]) {
179
+ console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
180
+ process.exitCode = 1;
181
+ return;
182
+ }
183
+ for (const [groupFieldId, groupField] of Object.entries(tabFields)) {
184
+ if (isGroupField(groupField) && groupField.config.fields[fieldId]) {
185
+ console.error(`Field "${fieldId}" already exists in group "${groupFieldId}" in tab "${tabName}"`);
186
+ process.exitCode = 1;
187
+ return;
188
+ }
189
+ }
190
+ }
191
+ model.json[targetTab][fieldId] = fieldDefinition;
192
+ }
193
+
194
+ // Write updated model
195
+ try {
196
+ await writeFile(modelPath, stringify(model));
197
+ } catch (error) {
198
+ if (error instanceof Error) {
199
+ console.error(`Failed to update page type: ${error.message}`);
200
+ } else {
201
+ console.error("Failed to update page type");
202
+ }
203
+ process.exitCode = 1;
204
+ return;
205
+ }
206
+
207
+ if (fieldPath.type === "nested") {
208
+ console.info(`Added field "${fieldPath.nestedFieldId}" (Select) to group "${fieldPath.groupId}" in ${typeId}`);
209
+ } else {
210
+ console.info(`Added field "${fieldId}" (Select) to "${targetTab}" tab in ${typeId}`);
211
+ }
212
+
213
+ try {
214
+ await buildTypes({ output: types });
215
+ console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
216
+ } catch (error) {
217
+ console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
218
+ }
219
+
220
+ console.info();
221
+ console.info("Next: Add more fields with `prismic page-type add-field`");
222
+
223
+ const frameworkInfo = await detectFrameworkInfo();
224
+ if (frameworkInfo?.framework) {
225
+ const docsPath = getDocsPath(frameworkInfo.framework);
226
+ console.info(
227
+ ` Run \`prismic docs fetch ${docsPath}#write-page-components\` to learn how to implement a page file`,
228
+ );
229
+ }
230
+ }
@@ -0,0 +1,227 @@
1
+ import type { CustomType, Timestamp } from "@prismicio/types-internal/lib/customtypes";
2
+
3
+ import { readFile, writeFile } from "node:fs/promises";
4
+ import { parseArgs } from "node:util";
5
+ import * as v from "valibot";
6
+
7
+ import { buildTypes } from "./codegen-types";
8
+ import { findUpward } from "./lib/file";
9
+ import { findGroupInTab, isGroupField, parseFieldPath, validateNestedFieldPath } from "./lib/field-path";
10
+ import { type Framework, detectFrameworkInfo } from "./lib/framework";
11
+ import { stringify } from "./lib/json";
12
+ import { humanReadable } from "./lib/string";
13
+
14
+ const HELP = `
15
+ Add a timestamp (date and time) field to an existing page type.
16
+
17
+ USAGE
18
+ prismic page-type add-field timestamp <type-id> <field-id> [flags]
19
+
20
+ ARGUMENTS
21
+ type-id Page type identifier (required)
22
+ field-id Field identifier (required)
23
+
24
+ FLAGS
25
+ -t, --tab string Target tab (default: first existing tab, or "Main")
26
+ -l, --label string Display label for the field (inferred from field-id if omitted)
27
+ -p, --placeholder string Placeholder text
28
+ --default string Default timestamp value (ISO 8601 format)
29
+ --types string Output file for generated types (default: "prismicio-types.d.ts")
30
+ -h, --help Show help for command
31
+
32
+ EXAMPLES
33
+ prismic page-type add-field timestamp homepage event_time
34
+ prismic page-type add-field timestamp event start --tab "Schedule"
35
+ prismic page-type add-field timestamp article published_at --label "Published At"
36
+ `.trim();
37
+
38
+ const CustomTypeSchema = v.object({
39
+ id: v.string(),
40
+ label: v.string(),
41
+ repeatable: v.boolean(),
42
+ status: v.boolean(),
43
+ format: v.string(),
44
+ json: v.record(v.string(), v.record(v.string(), v.unknown())),
45
+ });
46
+
47
+ function getDocsPath(framework: Framework): string {
48
+ switch (framework) {
49
+ case "next":
50
+ return "nextjs/with-cli";
51
+ case "nuxt":
52
+ return "nuxt/with-cli";
53
+ case "sveltekit":
54
+ return "sveltekit/with-cli";
55
+ }
56
+ }
57
+
58
+ export async function pageTypeAddFieldTimestamp(): Promise<void> {
59
+ const {
60
+ values: { help, tab, label, placeholder, default: defaultValue, types },
61
+ positionals: [typeId, fieldId],
62
+ } = parseArgs({
63
+ args: process.argv.slice(5), // skip: node, script, "page-type", "add-field", "timestamp"
64
+ options: {
65
+ tab: { type: "string", short: "t" },
66
+ label: { type: "string", short: "l" },
67
+ placeholder: { type: "string", short: "p" },
68
+ default: { type: "string" },
69
+ types: { type: "string" },
70
+ help: { type: "boolean", short: "h" },
71
+ },
72
+ allowPositionals: true,
73
+ });
74
+
75
+ if (help) {
76
+ console.info(HELP);
77
+ return;
78
+ }
79
+
80
+ if (!typeId) {
81
+ console.error("Missing required argument: type-id\n");
82
+ console.error("Usage: prismic page-type add-field timestamp <type-id> <field-id>");
83
+ process.exitCode = 1;
84
+ return;
85
+ }
86
+
87
+ if (!fieldId) {
88
+ console.error("Missing required argument: field-id\n");
89
+ console.error("Usage: prismic page-type add-field timestamp <type-id> <field-id>");
90
+ process.exitCode = 1;
91
+ return;
92
+ }
93
+
94
+ // Parse and validate field path
95
+ const fieldPath = parseFieldPath(fieldId);
96
+ const pathValidation = validateNestedFieldPath(fieldPath);
97
+ if (!pathValidation.ok) {
98
+ console.error(pathValidation.error);
99
+ process.exitCode = 1;
100
+ return;
101
+ }
102
+
103
+ // Find the page type file
104
+ const projectRoot = await findUpward("package.json");
105
+ if (!projectRoot) {
106
+ console.error("Could not find project root (no package.json found)");
107
+ process.exitCode = 1;
108
+ return;
109
+ }
110
+
111
+ const modelPath = new URL(`customtypes/${typeId}/index.json`, projectRoot);
112
+
113
+ // Read and parse the model
114
+ let model: CustomType;
115
+ try {
116
+ const contents = await readFile(modelPath, "utf8");
117
+ const result = v.safeParse(CustomTypeSchema, JSON.parse(contents));
118
+ if (!result.success) {
119
+ console.error(`Invalid page type model: ${modelPath.href}`);
120
+ process.exitCode = 1;
121
+ return;
122
+ }
123
+ model = result.output as CustomType;
124
+ } catch (error) {
125
+ if (error instanceof Error && "code" in error && error.code === "ENOENT") {
126
+ console.error(`Page type not found: ${typeId}\n`);
127
+ console.error(`Create it first with: prismic page-type create ${typeId}`);
128
+ process.exitCode = 1;
129
+ return;
130
+ }
131
+ if (error instanceof Error) {
132
+ console.error(`Failed to read page type: ${error.message}`);
133
+ } else {
134
+ console.error("Failed to read page type");
135
+ }
136
+ process.exitCode = 1;
137
+ return;
138
+ }
139
+
140
+ // Determine target tab
141
+ const existingTabs = Object.keys(model.json);
142
+ const targetTab = tab ?? existingTabs[0] ?? "Main";
143
+
144
+ // Initialize tab if it doesn't exist
145
+ if (!model.json[targetTab]) {
146
+ model.json[targetTab] = {};
147
+ }
148
+
149
+ // Build field definition
150
+ const fieldDefinition: Timestamp = {
151
+ type: "Timestamp",
152
+ config: {
153
+ label: label ?? humanReadable(fieldPath.type === "nested" ? fieldPath.nestedFieldId : fieldId),
154
+ ...(placeholder && { placeholder }),
155
+ ...(defaultValue && { default: defaultValue }),
156
+ },
157
+ };
158
+
159
+ // Add field to model
160
+ if (fieldPath.type === "nested") {
161
+ const groupResult = findGroupInTab(model.json[targetTab], fieldPath.groupId, targetTab);
162
+ if (!groupResult.ok) {
163
+ console.error(groupResult.error);
164
+ process.exitCode = 1;
165
+ return;
166
+ }
167
+ if (groupResult.group.config.fields[fieldPath.nestedFieldId]) {
168
+ console.error(`Field "${fieldPath.nestedFieldId}" already exists in group "${fieldPath.groupId}"`);
169
+ process.exitCode = 1;
170
+ return;
171
+ }
172
+ groupResult.group.config.fields[fieldPath.nestedFieldId] = fieldDefinition;
173
+ } else {
174
+ for (const [tabName, tabFields] of Object.entries(model.json)) {
175
+ if (tabFields[fieldId]) {
176
+ console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
177
+ process.exitCode = 1;
178
+ return;
179
+ }
180
+ for (const [groupFieldId, groupField] of Object.entries(tabFields)) {
181
+ if (isGroupField(groupField) && groupField.config.fields[fieldId]) {
182
+ console.error(`Field "${fieldId}" already exists in group "${groupFieldId}" in tab "${tabName}"`);
183
+ process.exitCode = 1;
184
+ return;
185
+ }
186
+ }
187
+ }
188
+ model.json[targetTab][fieldId] = fieldDefinition;
189
+ }
190
+
191
+ // Write updated model
192
+ try {
193
+ await writeFile(modelPath, stringify(model));
194
+ } catch (error) {
195
+ if (error instanceof Error) {
196
+ console.error(`Failed to update page type: ${error.message}`);
197
+ } else {
198
+ console.error("Failed to update page type");
199
+ }
200
+ process.exitCode = 1;
201
+ return;
202
+ }
203
+
204
+ if (fieldPath.type === "nested") {
205
+ console.info(`Added field "${fieldPath.nestedFieldId}" (Timestamp) to group "${fieldPath.groupId}" in ${typeId}`);
206
+ } else {
207
+ console.info(`Added field "${fieldId}" (Timestamp) to "${targetTab}" tab in ${typeId}`);
208
+ }
209
+
210
+ try {
211
+ await buildTypes({ output: types });
212
+ console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
213
+ } catch (error) {
214
+ console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
215
+ }
216
+
217
+ console.info();
218
+ console.info("Next: Add more fields with `prismic page-type add-field`");
219
+
220
+ const frameworkInfo = await detectFrameworkInfo();
221
+ if (frameworkInfo?.framework) {
222
+ const docsPath = getDocsPath(frameworkInfo.framework);
223
+ console.info(
224
+ ` Run \`prismic docs fetch ${docsPath}#write-page-components\` to learn how to implement a page file`,
225
+ );
226
+ }
227
+ }
@@ -0,0 +1,207 @@
1
+ import type { CustomType, UID } from "@prismicio/types-internal/lib/customtypes";
2
+
3
+ import { readFile, writeFile } from "node:fs/promises";
4
+ import { parseArgs } from "node:util";
5
+ import * as v from "valibot";
6
+
7
+ import { buildTypes } from "./codegen-types";
8
+ import { findUpward } from "./lib/file";
9
+ import { parseFieldPath, validateNestedFieldPath } from "./lib/field-path";
10
+ import { type Framework, detectFrameworkInfo } from "./lib/framework";
11
+ import { stringify } from "./lib/json";
12
+ import { humanReadable } from "./lib/string";
13
+
14
+ const HELP = `
15
+ Add a UID (unique identifier) field to an existing page type.
16
+
17
+ USAGE
18
+ prismic page-type add-field uid <type-id> <field-id> [flags]
19
+
20
+ ARGUMENTS
21
+ type-id Page type identifier (required)
22
+ field-id Field identifier (required)
23
+
24
+ FLAGS
25
+ -t, --tab string Target tab (default: first existing tab, or "Main")
26
+ -l, --label string Display label for the field (inferred from field-id if omitted)
27
+ -p, --placeholder string Placeholder text
28
+ --types string Output file for generated types (default: "prismicio-types.d.ts")
29
+ -h, --help Show help for command
30
+
31
+ EXAMPLES
32
+ prismic page-type add-field uid page uid
33
+ prismic page-type add-field uid article slug --label "URL Slug"
34
+ prismic page-type add-field uid product sku --placeholder "Enter unique SKU"
35
+ `.trim();
36
+
37
+ const CustomTypeSchema = v.object({
38
+ id: v.string(),
39
+ label: v.string(),
40
+ repeatable: v.boolean(),
41
+ status: v.boolean(),
42
+ format: v.string(),
43
+ json: v.record(v.string(), v.record(v.string(), v.unknown())),
44
+ });
45
+
46
+ function getDocsPath(framework: Framework): string {
47
+ switch (framework) {
48
+ case "next":
49
+ return "nextjs/with-cli";
50
+ case "nuxt":
51
+ return "nuxt/with-cli";
52
+ case "sveltekit":
53
+ return "sveltekit/with-cli";
54
+ }
55
+ }
56
+
57
+ export async function pageTypeAddFieldUid(): Promise<void> {
58
+ const {
59
+ values: { help, tab, label, placeholder, types },
60
+ positionals: [typeId, fieldId],
61
+ } = parseArgs({
62
+ args: process.argv.slice(5), // skip: node, script, "page-type", "add-field", "uid"
63
+ options: {
64
+ tab: { type: "string", short: "t" },
65
+ label: { type: "string", short: "l" },
66
+ placeholder: { type: "string", short: "p" },
67
+ types: { type: "string" },
68
+ help: { type: "boolean", short: "h" },
69
+ },
70
+ allowPositionals: true,
71
+ });
72
+
73
+ if (help) {
74
+ console.info(HELP);
75
+ return;
76
+ }
77
+
78
+ if (!typeId) {
79
+ console.error("Missing required argument: type-id\n");
80
+ console.error("Usage: prismic page-type add-field uid <type-id> <field-id>");
81
+ process.exitCode = 1;
82
+ return;
83
+ }
84
+
85
+ if (!fieldId) {
86
+ console.error("Missing required argument: field-id\n");
87
+ console.error("Usage: prismic page-type add-field uid <type-id> <field-id>");
88
+ process.exitCode = 1;
89
+ return;
90
+ }
91
+
92
+ // Parse and validate field path
93
+ const fieldPath = parseFieldPath(fieldId);
94
+ const pathValidation = validateNestedFieldPath(fieldPath);
95
+ if (!pathValidation.ok) {
96
+ console.error(pathValidation.error);
97
+ process.exitCode = 1;
98
+ return;
99
+ }
100
+
101
+ // UID fields cannot be nested in groups
102
+ if (fieldPath.type === "nested") {
103
+ console.error("UID fields cannot be nested inside groups");
104
+ process.exitCode = 1;
105
+ return;
106
+ }
107
+
108
+ // Find the page type file
109
+ const projectRoot = await findUpward("package.json");
110
+ if (!projectRoot) {
111
+ console.error("Could not find project root (no package.json found)");
112
+ process.exitCode = 1;
113
+ return;
114
+ }
115
+
116
+ const modelPath = new URL(`customtypes/${typeId}/index.json`, projectRoot);
117
+
118
+ // Read and parse the model
119
+ let model: CustomType;
120
+ try {
121
+ const contents = await readFile(modelPath, "utf8");
122
+ const result = v.safeParse(CustomTypeSchema, JSON.parse(contents));
123
+ if (!result.success) {
124
+ console.error(`Invalid page type model: ${modelPath.href}`);
125
+ process.exitCode = 1;
126
+ return;
127
+ }
128
+ model = result.output as CustomType;
129
+ } catch (error) {
130
+ if (error instanceof Error && "code" in error && error.code === "ENOENT") {
131
+ console.error(`Page type not found: ${typeId}\n`);
132
+ console.error(`Create it first with: prismic page-type create ${typeId}`);
133
+ process.exitCode = 1;
134
+ return;
135
+ }
136
+ if (error instanceof Error) {
137
+ console.error(`Failed to read page type: ${error.message}`);
138
+ } else {
139
+ console.error("Failed to read page type");
140
+ }
141
+ process.exitCode = 1;
142
+ return;
143
+ }
144
+
145
+ // Determine target tab
146
+ const existingTabs = Object.keys(model.json);
147
+ const targetTab = tab ?? existingTabs[0] ?? "Main";
148
+
149
+ // Initialize tab if it doesn't exist
150
+ if (!model.json[targetTab]) {
151
+ model.json[targetTab] = {};
152
+ }
153
+
154
+ // Check if field already exists in any tab
155
+ for (const [tabName, tabFields] of Object.entries(model.json)) {
156
+ if (tabFields[fieldId]) {
157
+ console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
158
+ process.exitCode = 1;
159
+ return;
160
+ }
161
+ }
162
+
163
+ // Build field definition
164
+ const fieldDefinition: UID = {
165
+ type: "UID",
166
+ config: {
167
+ label: label ?? humanReadable(fieldId),
168
+ ...(placeholder && { placeholder }),
169
+ },
170
+ };
171
+
172
+ // Add field to model
173
+ model.json[targetTab][fieldId] = fieldDefinition;
174
+
175
+ // Write updated model
176
+ try {
177
+ await writeFile(modelPath, stringify(model));
178
+ } catch (error) {
179
+ if (error instanceof Error) {
180
+ console.error(`Failed to update page type: ${error.message}`);
181
+ } else {
182
+ console.error("Failed to update page type");
183
+ }
184
+ process.exitCode = 1;
185
+ return;
186
+ }
187
+
188
+ console.info(`Added field "${fieldId}" (UID) to "${targetTab}" tab in ${typeId}`);
189
+
190
+ try {
191
+ await buildTypes({ output: types });
192
+ console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
193
+ } catch (error) {
194
+ console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
195
+ }
196
+
197
+ console.info();
198
+ console.info("Next: Add more fields with `prismic page-type add-field`");
199
+
200
+ const frameworkInfo = await detectFrameworkInfo();
201
+ if (frameworkInfo?.framework) {
202
+ const docsPath = getDocsPath(frameworkInfo.framework);
203
+ console.info(
204
+ ` Run \`prismic docs fetch ${docsPath}#write-page-components\` to learn how to implement a page file`,
205
+ );
206
+ }
207
+ }