prismic 0.0.0-pr.28.59bf330

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 (158) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +69 -0
  3. package/dist/builders-hKD4IrLX-DsO7BUQw.mjs +97 -0
  4. package/dist/dist-B11B2hHn.mjs +1 -0
  5. package/dist/dist-DT8CtumB.mjs +1 -0
  6. package/dist/framework-CfjEoVk0.mjs +17 -0
  7. package/dist/index.mjs +2537 -0
  8. package/dist/nextjs-9z7YrSnS.mjs +312 -0
  9. package/dist/nuxt-KoJ61G2q.mjs +59 -0
  10. package/dist/sveltekit-DjXKCG78.mjs +226 -0
  11. package/package.json +58 -0
  12. package/src/codegen-types.ts +82 -0
  13. package/src/codegen.ts +45 -0
  14. package/src/custom-type-add-field-boolean.ts +185 -0
  15. package/src/custom-type-add-field-color.ts +168 -0
  16. package/src/custom-type-add-field-date.ts +171 -0
  17. package/src/custom-type-add-field-embed.ts +168 -0
  18. package/src/custom-type-add-field-geo-point.ts +165 -0
  19. package/src/custom-type-add-field-group.ts +142 -0
  20. package/src/custom-type-add-field-image.ts +168 -0
  21. package/src/custom-type-add-field-key-text.ts +168 -0
  22. package/src/custom-type-add-field-link.ts +191 -0
  23. package/src/custom-type-add-field-number.ts +200 -0
  24. package/src/custom-type-add-field-rich-text.ts +192 -0
  25. package/src/custom-type-add-field-select.ts +174 -0
  26. package/src/custom-type-add-field-timestamp.ts +171 -0
  27. package/src/custom-type-add-field-uid.ts +151 -0
  28. package/src/custom-type-add-field.ts +116 -0
  29. package/src/custom-type-connect-slice.ts +178 -0
  30. package/src/custom-type-create.ts +98 -0
  31. package/src/custom-type-disconnect-slice.ts +134 -0
  32. package/src/custom-type-list.ts +110 -0
  33. package/src/custom-type-remove-field.ts +135 -0
  34. package/src/custom-type-remove.ts +103 -0
  35. package/src/custom-type-set-name.ts +102 -0
  36. package/src/custom-type-view.ts +118 -0
  37. package/src/custom-type.ts +85 -0
  38. package/src/docs-fetch.ts +146 -0
  39. package/src/docs-list.ts +131 -0
  40. package/src/docs.ts +54 -0
  41. package/src/env.d.ts +12 -0
  42. package/src/framework/index.ts +399 -0
  43. package/src/framework/nextjs.templates.ts +426 -0
  44. package/src/framework/nextjs.ts +216 -0
  45. package/src/framework/nuxt.templates.ts +74 -0
  46. package/src/framework/nuxt.ts +250 -0
  47. package/src/framework/sveltekit.templates.ts +278 -0
  48. package/src/framework/sveltekit.ts +241 -0
  49. package/src/index.ts +155 -0
  50. package/src/init.ts +173 -0
  51. package/src/lib/auth.ts +200 -0
  52. package/src/lib/browser.ts +11 -0
  53. package/src/lib/config.ts +111 -0
  54. package/src/lib/custom-types-api.ts +385 -0
  55. package/src/lib/field-path.ts +81 -0
  56. package/src/lib/file.ts +49 -0
  57. package/src/lib/json.ts +3 -0
  58. package/src/lib/packageJson.ts +35 -0
  59. package/src/lib/profile.ts +39 -0
  60. package/src/lib/request.ts +116 -0
  61. package/src/lib/segment.ts +145 -0
  62. package/src/lib/sentry.ts +63 -0
  63. package/src/lib/string.ts +10 -0
  64. package/src/lib/url.ts +31 -0
  65. package/src/locale-add.ts +116 -0
  66. package/src/locale-list.ts +107 -0
  67. package/src/locale-remove.ts +88 -0
  68. package/src/locale-set-default.ts +131 -0
  69. package/src/locale.ts +60 -0
  70. package/src/login.ts +45 -0
  71. package/src/logout.ts +36 -0
  72. package/src/page-type-add-field-boolean.ts +179 -0
  73. package/src/page-type-add-field-color.ts +165 -0
  74. package/src/page-type-add-field-date.ts +168 -0
  75. package/src/page-type-add-field-embed.ts +165 -0
  76. package/src/page-type-add-field-geo-point.ts +162 -0
  77. package/src/page-type-add-field-group.ts +139 -0
  78. package/src/page-type-add-field-image.ts +165 -0
  79. package/src/page-type-add-field-key-text.ts +165 -0
  80. package/src/page-type-add-field-link.ts +188 -0
  81. package/src/page-type-add-field-number.ts +197 -0
  82. package/src/page-type-add-field-rich-text.ts +189 -0
  83. package/src/page-type-add-field-select.ts +171 -0
  84. package/src/page-type-add-field-timestamp.ts +168 -0
  85. package/src/page-type-add-field-uid.ts +148 -0
  86. package/src/page-type-add-field.ts +116 -0
  87. package/src/page-type-connect-slice.ts +178 -0
  88. package/src/page-type-create.ts +128 -0
  89. package/src/page-type-disconnect-slice.ts +134 -0
  90. package/src/page-type-list.ts +109 -0
  91. package/src/page-type-remove-field.ts +135 -0
  92. package/src/page-type-remove.ts +103 -0
  93. package/src/page-type-set-name.ts +102 -0
  94. package/src/page-type-set-repeatable.ts +111 -0
  95. package/src/page-type-view.ts +118 -0
  96. package/src/page-type.ts +90 -0
  97. package/src/preview-add.ts +126 -0
  98. package/src/preview-get-simulator.ts +104 -0
  99. package/src/preview-list.ts +106 -0
  100. package/src/preview-remove-simulator.ts +80 -0
  101. package/src/preview-remove.ts +109 -0
  102. package/src/preview-set-name.ts +137 -0
  103. package/src/preview-set-simulator.ts +116 -0
  104. package/src/preview.ts +75 -0
  105. package/src/pull.ts +236 -0
  106. package/src/push.ts +409 -0
  107. package/src/repo-create.ts +175 -0
  108. package/src/repo-get-access.ts +86 -0
  109. package/src/repo-list.ts +100 -0
  110. package/src/repo-set-access.ts +100 -0
  111. package/src/repo-set-name.ts +102 -0
  112. package/src/repo-view.ts +113 -0
  113. package/src/repo.ts +70 -0
  114. package/src/slice-add-field-boolean.ts +219 -0
  115. package/src/slice-add-field-color.ts +205 -0
  116. package/src/slice-add-field-date.ts +205 -0
  117. package/src/slice-add-field-embed.ts +205 -0
  118. package/src/slice-add-field-geo-point.ts +202 -0
  119. package/src/slice-add-field-group.ts +170 -0
  120. package/src/slice-add-field-image.ts +202 -0
  121. package/src/slice-add-field-key-text.ts +205 -0
  122. package/src/slice-add-field-link.ts +224 -0
  123. package/src/slice-add-field-number.ts +205 -0
  124. package/src/slice-add-field-rich-text.ts +229 -0
  125. package/src/slice-add-field-select.ts +211 -0
  126. package/src/slice-add-field-timestamp.ts +205 -0
  127. package/src/slice-add-field.ts +111 -0
  128. package/src/slice-add-variation.ts +142 -0
  129. package/src/slice-create.ts +164 -0
  130. package/src/slice-list-variations.ts +71 -0
  131. package/src/slice-list.ts +60 -0
  132. package/src/slice-remove-field.ts +125 -0
  133. package/src/slice-remove-variation.ts +113 -0
  134. package/src/slice-remove.ts +92 -0
  135. package/src/slice-rename.ts +104 -0
  136. package/src/slice-set-screenshot.ts +239 -0
  137. package/src/slice-view.ts +83 -0
  138. package/src/slice.ts +95 -0
  139. package/src/status.ts +834 -0
  140. package/src/sync.ts +259 -0
  141. package/src/token-create.ts +203 -0
  142. package/src/token-delete.ts +182 -0
  143. package/src/token-list.ts +223 -0
  144. package/src/token-set-name.ts +193 -0
  145. package/src/token.ts +60 -0
  146. package/src/webhook-add-header.ts +118 -0
  147. package/src/webhook-create.ts +152 -0
  148. package/src/webhook-disable.ts +109 -0
  149. package/src/webhook-enable.ts +132 -0
  150. package/src/webhook-list.ts +93 -0
  151. package/src/webhook-remove-header.ts +117 -0
  152. package/src/webhook-remove.ts +106 -0
  153. package/src/webhook-set-triggers.ts +148 -0
  154. package/src/webhook-status.ts +90 -0
  155. package/src/webhook-test.ts +106 -0
  156. package/src/webhook-view.ts +147 -0
  157. package/src/webhook.ts +95 -0
  158. package/src/whoami.ts +62 -0
@@ -0,0 +1,197 @@
1
+ import type { CustomType, Number as NumberField } from "@prismicio/types-internal/lib/customtypes";
2
+
3
+ import { parseArgs } from "node:util";
4
+
5
+ import { buildTypes } from "./codegen-types";
6
+ import { findGroupInTab, isGroupField, parseFieldPath, validateNestedFieldPath } from "./lib/field-path";
7
+ import { getDocsPath, requireFramework } from "./framework";
8
+ import { humanReadable } from "./lib/string";
9
+
10
+ const HELP = `
11
+ Add a number field to an existing page type.
12
+
13
+ USAGE
14
+ prismic page-type add-field number <type-id> <field-id> [flags]
15
+
16
+ ARGUMENTS
17
+ type-id Page type identifier (required)
18
+ field-id Field identifier (required)
19
+
20
+ FLAGS
21
+ -t, --tab string Target tab (default: first existing tab, or "Main")
22
+ -l, --label string Display label for the field (inferred from field-id if omitted)
23
+ -p, --placeholder string Placeholder text
24
+ --min number Minimum value
25
+ --max number Maximum value
26
+ --step number Step increment
27
+ --types string Output file for generated types (default: "prismicio-types.d.ts")
28
+ -h, --help Show help for command
29
+
30
+ EXAMPLES
31
+ prismic page-type add-field number homepage price
32
+ prismic page-type add-field number product quantity --min 0 --max 100
33
+ prismic page-type add-field number settings rating --min 1 --max 5 --step 1
34
+ `.trim();
35
+
36
+
37
+ export async function pageTypeAddFieldNumber(): Promise<void> {
38
+ const {
39
+ values: { help, tab, label, placeholder, min, max, step, types },
40
+ positionals: [typeId, fieldId],
41
+ } = parseArgs({
42
+ args: process.argv.slice(5), // skip: node, script, "page-type", "add-field", "number"
43
+ options: {
44
+ tab: { type: "string", short: "t" },
45
+ label: { type: "string", short: "l" },
46
+ placeholder: { type: "string", short: "p" },
47
+ min: { type: "string" },
48
+ max: { type: "string" },
49
+ step: { type: "string" },
50
+ types: { type: "string" },
51
+ help: { type: "boolean", short: "h" },
52
+ },
53
+ allowPositionals: true,
54
+ });
55
+
56
+ if (help) {
57
+ console.info(HELP);
58
+ return;
59
+ }
60
+
61
+ if (!typeId) {
62
+ console.error("Missing required argument: type-id\n");
63
+ console.error("Usage: prismic page-type add-field number <type-id> <field-id>");
64
+ process.exitCode = 1;
65
+ return;
66
+ }
67
+
68
+ if (!fieldId) {
69
+ console.error("Missing required argument: field-id\n");
70
+ console.error("Usage: prismic page-type add-field number <type-id> <field-id>");
71
+ process.exitCode = 1;
72
+ return;
73
+ }
74
+
75
+ // Parse and validate field path
76
+ const fieldPath = parseFieldPath(fieldId);
77
+ const pathValidation = validateNestedFieldPath(fieldPath);
78
+ if (!pathValidation.ok) {
79
+ console.error(pathValidation.error);
80
+ process.exitCode = 1;
81
+ return;
82
+ }
83
+
84
+ // Parse numeric values
85
+ const minValue = min !== undefined ? Number(min) : undefined;
86
+ const maxValue = max !== undefined ? Number(max) : undefined;
87
+ const stepValue = step !== undefined ? Number(step) : undefined;
88
+
89
+ if (min !== undefined && Number.isNaN(minValue)) {
90
+ console.error("Invalid --min value: must be a number");
91
+ process.exitCode = 1;
92
+ return;
93
+ }
94
+
95
+ if (max !== undefined && Number.isNaN(maxValue)) {
96
+ console.error("Invalid --max value: must be a number");
97
+ process.exitCode = 1;
98
+ return;
99
+ }
100
+
101
+ if (step !== undefined && Number.isNaN(stepValue)) {
102
+ console.error("Invalid --step value: must be a number");
103
+ process.exitCode = 1;
104
+ return;
105
+ }
106
+
107
+ const framework = await requireFramework();
108
+ if (!framework) return;
109
+
110
+ let model: CustomType;
111
+ try {
112
+ model = await framework.readCustomType(typeId);
113
+ } catch {
114
+ console.error(`Page type not found: ${typeId}\n\nCreate it first with: prismic page-type create ${typeId}`);
115
+ process.exitCode = 1;
116
+ return;
117
+ }
118
+
119
+ // Determine target tab
120
+ const existingTabs = Object.keys(model.json);
121
+ const targetTab = tab ?? existingTabs[0] ?? "Main";
122
+
123
+ // Initialize tab if it doesn't exist
124
+ if (!model.json[targetTab]) {
125
+ model.json[targetTab] = {};
126
+ }
127
+
128
+ // Build field definition
129
+ const fieldDefinition: NumberField = {
130
+ type: "Number",
131
+ config: {
132
+ label: label ?? humanReadable(fieldPath.type === "nested" ? fieldPath.nestedFieldId : fieldId),
133
+ ...(placeholder && { placeholder }),
134
+ ...(minValue !== undefined && { min: minValue }),
135
+ ...(maxValue !== undefined && { max: maxValue }),
136
+ ...(stepValue !== undefined && { step: stepValue }),
137
+ },
138
+ };
139
+
140
+ // Add field to model
141
+ if (fieldPath.type === "nested") {
142
+ const groupResult = findGroupInTab(model.json[targetTab], fieldPath.groupId, targetTab);
143
+ if (!groupResult.ok) {
144
+ console.error(groupResult.error);
145
+ process.exitCode = 1;
146
+ return;
147
+ }
148
+ if (groupResult.group.config.fields[fieldPath.nestedFieldId]) {
149
+ console.error(`Field "${fieldPath.nestedFieldId}" already exists in group "${fieldPath.groupId}"`);
150
+ process.exitCode = 1;
151
+ return;
152
+ }
153
+ groupResult.group.config.fields[fieldPath.nestedFieldId] = fieldDefinition;
154
+ } else {
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
+ for (const [groupFieldId, groupField] of Object.entries(tabFields)) {
162
+ if (isGroupField(groupField) && groupField.config.fields[fieldId]) {
163
+ console.error(`Field "${fieldId}" already exists in group "${groupFieldId}" in tab "${tabName}"`);
164
+ process.exitCode = 1;
165
+ return;
166
+ }
167
+ }
168
+ }
169
+ model.json[targetTab][fieldId] = fieldDefinition;
170
+ }
171
+
172
+ // Write updated model
173
+ await framework.updateCustomType(model);
174
+
175
+ if (fieldPath.type === "nested") {
176
+ console.info(`Added field "${fieldPath.nestedFieldId}" (Number) to group "${fieldPath.groupId}" in ${typeId}`);
177
+ } else {
178
+ console.info(`Added field "${fieldId}" (Number) to "${targetTab}" tab in ${typeId}`);
179
+ }
180
+
181
+ try {
182
+ await buildTypes({ output: types });
183
+ console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
184
+ } catch (error) {
185
+ console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
186
+ }
187
+
188
+ console.info();
189
+ console.info("Next: Add more fields with `prismic page-type add-field`");
190
+
191
+ if (framework) {
192
+ const docsPath = getDocsPath(framework.id);
193
+ console.info(
194
+ ` Run \`prismic docs fetch ${docsPath}#write-page-components\` to learn how to implement a page file`,
195
+ );
196
+ }
197
+ }
@@ -0,0 +1,189 @@
1
+ import type { CustomType, RichText } from "@prismicio/types-internal/lib/customtypes";
2
+
3
+ import { parseArgs } from "node:util";
4
+
5
+ import { buildTypes } from "./codegen-types";
6
+ import { findGroupInTab, isGroupField, parseFieldPath, validateNestedFieldPath } from "./lib/field-path";
7
+ import { getDocsPath, requireFramework } from "./framework";
8
+ import { humanReadable } from "./lib/string";
9
+
10
+ const HELP = `
11
+ Add a rich text field to an existing page type.
12
+
13
+ USAGE
14
+ prismic page-type add-field rich-text <type-id> <field-id> [flags]
15
+
16
+ ARGUMENTS
17
+ type-id Page type identifier (required)
18
+ field-id Field identifier (required)
19
+
20
+ FLAGS
21
+ -t, --tab string Target tab (default: first existing tab, or "Main")
22
+ -l, --label string Display label for the field (inferred from field-id if omitted)
23
+ -p, --placeholder string Placeholder text
24
+ --single string Allowed block types for single-line (comma-separated)
25
+ --multi string Allowed block types for multi-line (comma-separated)
26
+ --allow-target-blank Allow opening links in new tab
27
+ --types string Output file for generated types (default: "prismicio-types.d.ts")
28
+ -h, --help Show help for command
29
+
30
+ BLOCK TYPES
31
+ heading1, heading2, heading3, heading4, heading5, heading6,
32
+ paragraph, strong, em, preformatted, hyperlink, image, embed,
33
+ list-item, o-list-item, rtl
34
+
35
+ EXAMPLES
36
+ prismic page-type add-field rich-text homepage body
37
+ prismic page-type add-field rich-text article content --multi "paragraph,heading2,heading3,strong,em,hyperlink"
38
+ prismic page-type add-field rich-text page tagline --single "heading1"
39
+ prismic page-type add-field rich-text blog post --multi "paragraph,strong,em,hyperlink" --allow-target-blank
40
+ `.trim();
41
+
42
+
43
+ export async function pageTypeAddFieldRichText(): Promise<void> {
44
+ const {
45
+ values: {
46
+ help,
47
+ tab,
48
+ label,
49
+ placeholder,
50
+ single,
51
+ multi,
52
+ "allow-target-blank": allowTargetBlank,
53
+ types,
54
+ },
55
+ positionals: [typeId, fieldId],
56
+ } = parseArgs({
57
+ args: process.argv.slice(5), // skip: node, script, "page-type", "add-field", "rich-text"
58
+ options: {
59
+ tab: { type: "string", short: "t" },
60
+ label: { type: "string", short: "l" },
61
+ placeholder: { type: "string", short: "p" },
62
+ single: { type: "string" },
63
+ multi: { type: "string" },
64
+ "allow-target-blank": { type: "boolean" },
65
+ types: { type: "string" },
66
+ help: { type: "boolean", short: "h" },
67
+ },
68
+ allowPositionals: true,
69
+ });
70
+
71
+ if (help) {
72
+ console.info(HELP);
73
+ return;
74
+ }
75
+
76
+ if (!typeId) {
77
+ console.error("Missing required argument: type-id\n");
78
+ console.error("Usage: prismic page-type add-field rich-text <type-id> <field-id>");
79
+ process.exitCode = 1;
80
+ return;
81
+ }
82
+
83
+ if (!fieldId) {
84
+ console.error("Missing required argument: field-id\n");
85
+ console.error("Usage: prismic page-type add-field rich-text <type-id> <field-id>");
86
+ process.exitCode = 1;
87
+ return;
88
+ }
89
+
90
+ // Parse and validate field path
91
+ const fieldPath = parseFieldPath(fieldId);
92
+ const pathValidation = validateNestedFieldPath(fieldPath);
93
+ if (!pathValidation.ok) {
94
+ console.error(pathValidation.error);
95
+ process.exitCode = 1;
96
+ return;
97
+ }
98
+
99
+ const framework = await requireFramework();
100
+ if (!framework) return;
101
+
102
+ let model: CustomType;
103
+ try {
104
+ model = await framework.readCustomType(typeId);
105
+ } catch {
106
+ console.error(`Page type not found: ${typeId}\n\nCreate it first with: prismic page-type create ${typeId}`);
107
+ process.exitCode = 1;
108
+ return;
109
+ }
110
+
111
+ // Determine target tab
112
+ const existingTabs = Object.keys(model.json);
113
+ const targetTab = tab ?? existingTabs[0] ?? "Main";
114
+
115
+ // Initialize tab if it doesn't exist
116
+ if (!model.json[targetTab]) {
117
+ model.json[targetTab] = {};
118
+ }
119
+
120
+ // Build field definition
121
+ const fieldDefinition: RichText = {
122
+ type: "StructuredText",
123
+ config: {
124
+ label: label ?? humanReadable(fieldPath.type === "nested" ? fieldPath.nestedFieldId : fieldId),
125
+ ...(placeholder && { placeholder }),
126
+ ...(single && { single }),
127
+ ...(multi && { multi }),
128
+ ...(allowTargetBlank && { allowTargetBlank: true }),
129
+ },
130
+ };
131
+
132
+ // Add field to model
133
+ if (fieldPath.type === "nested") {
134
+ const groupResult = findGroupInTab(model.json[targetTab], fieldPath.groupId, targetTab);
135
+ if (!groupResult.ok) {
136
+ console.error(groupResult.error);
137
+ process.exitCode = 1;
138
+ return;
139
+ }
140
+ if (groupResult.group.config.fields[fieldPath.nestedFieldId]) {
141
+ console.error(`Field "${fieldPath.nestedFieldId}" already exists in group "${fieldPath.groupId}"`);
142
+ process.exitCode = 1;
143
+ return;
144
+ }
145
+ groupResult.group.config.fields[fieldPath.nestedFieldId] = fieldDefinition;
146
+ } else {
147
+ for (const [tabName, tabFields] of Object.entries(model.json)) {
148
+ if (tabFields[fieldId]) {
149
+ console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
150
+ process.exitCode = 1;
151
+ return;
152
+ }
153
+ for (const [groupFieldId, groupField] of Object.entries(tabFields)) {
154
+ if (isGroupField(groupField) && groupField.config.fields[fieldId]) {
155
+ console.error(`Field "${fieldId}" already exists in group "${groupFieldId}" in tab "${tabName}"`);
156
+ process.exitCode = 1;
157
+ return;
158
+ }
159
+ }
160
+ }
161
+ model.json[targetTab][fieldId] = fieldDefinition;
162
+ }
163
+
164
+ // Write updated model
165
+ await framework.updateCustomType(model);
166
+
167
+ if (fieldPath.type === "nested") {
168
+ console.info(`Added field "${fieldPath.nestedFieldId}" (StructuredText) to group "${fieldPath.groupId}" in ${typeId}`);
169
+ } else {
170
+ console.info(`Added field "${fieldId}" (StructuredText) to "${targetTab}" tab in ${typeId}`);
171
+ }
172
+
173
+ try {
174
+ await buildTypes({ output: types });
175
+ console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
176
+ } catch (error) {
177
+ console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
178
+ }
179
+
180
+ console.info();
181
+ console.info("Next: Add more fields with `prismic page-type add-field`");
182
+
183
+ if (framework) {
184
+ const docsPath = getDocsPath(framework.id);
185
+ console.info(
186
+ ` Run \`prismic docs fetch ${docsPath}#write-page-components\` to learn how to implement a page file`,
187
+ );
188
+ }
189
+ }
@@ -0,0 +1,171 @@
1
+ import type { CustomType, Select } from "@prismicio/types-internal/lib/customtypes";
2
+
3
+ import { parseArgs } from "node:util";
4
+
5
+ import { buildTypes } from "./codegen-types";
6
+ import { findGroupInTab, isGroupField, parseFieldPath, validateNestedFieldPath } from "./lib/field-path";
7
+ import { getDocsPath, requireFramework } from "./framework";
8
+ import { humanReadable } from "./lib/string";
9
+
10
+ const HELP = `
11
+ Add a select (dropdown) field to an existing page type.
12
+
13
+ USAGE
14
+ prismic page-type add-field select <type-id> <field-id> [flags]
15
+
16
+ ARGUMENTS
17
+ type-id Page type identifier (required)
18
+ field-id Field identifier (required)
19
+
20
+ FLAGS
21
+ -t, --tab string Target tab (default: first existing tab, or "Main")
22
+ -l, --label string Display label for the field (inferred from field-id if omitted)
23
+ -p, --placeholder string Placeholder text
24
+ --option string Add an option (can be used multiple times)
25
+ --default string Default selected value
26
+ --types string Output file for generated types (default: "prismicio-types.d.ts")
27
+ -h, --help Show help for command
28
+
29
+ EXAMPLES
30
+ prismic page-type add-field select homepage layout --option "full" --option "sidebar"
31
+ prismic page-type add-field select product size --option "small" --option "medium" --option "large" --default "medium"
32
+ prismic page-type add-field select article status --option "draft" --option "published" --label "Status"
33
+ `.trim();
34
+
35
+
36
+ export async function pageTypeAddFieldSelect(): Promise<void> {
37
+ const {
38
+ values: { help, tab, label, placeholder, option, default: defaultValue, types },
39
+ positionals: [typeId, fieldId],
40
+ } = parseArgs({
41
+ args: process.argv.slice(5), // skip: node, script, "page-type", "add-field", "select"
42
+ options: {
43
+ tab: { type: "string", short: "t" },
44
+ label: { type: "string", short: "l" },
45
+ placeholder: { type: "string", short: "p" },
46
+ option: { type: "string", multiple: true },
47
+ default: { type: "string" },
48
+ types: { type: "string" },
49
+ help: { type: "boolean", short: "h" },
50
+ },
51
+ allowPositionals: true,
52
+ });
53
+
54
+ if (help) {
55
+ console.info(HELP);
56
+ return;
57
+ }
58
+
59
+ if (!typeId) {
60
+ console.error("Missing required argument: type-id\n");
61
+ console.error("Usage: prismic page-type add-field select <type-id> <field-id>");
62
+ process.exitCode = 1;
63
+ return;
64
+ }
65
+
66
+ if (!fieldId) {
67
+ console.error("Missing required argument: field-id\n");
68
+ console.error("Usage: prismic page-type add-field select <type-id> <field-id>");
69
+ process.exitCode = 1;
70
+ return;
71
+ }
72
+
73
+ // Parse and validate field path
74
+ const fieldPath = parseFieldPath(fieldId);
75
+ const pathValidation = validateNestedFieldPath(fieldPath);
76
+ if (!pathValidation.ok) {
77
+ console.error(pathValidation.error);
78
+ process.exitCode = 1;
79
+ return;
80
+ }
81
+
82
+ const framework = await requireFramework();
83
+ if (!framework) return;
84
+
85
+ let model: CustomType;
86
+ try {
87
+ model = await framework.readCustomType(typeId);
88
+ } catch {
89
+ console.error(`Page type not found: ${typeId}\n\nCreate it first with: prismic page-type create ${typeId}`);
90
+ process.exitCode = 1;
91
+ return;
92
+ }
93
+
94
+ // Determine target tab
95
+ const existingTabs = Object.keys(model.json);
96
+ const targetTab = tab ?? existingTabs[0] ?? "Main";
97
+
98
+ // Initialize tab if it doesn't exist
99
+ if (!model.json[targetTab]) {
100
+ model.json[targetTab] = {};
101
+ }
102
+
103
+ // Build field definition
104
+ const fieldDefinition: Select = {
105
+ type: "Select",
106
+ config: {
107
+ label: label ?? humanReadable(fieldPath.type === "nested" ? fieldPath.nestedFieldId : fieldId),
108
+ ...(placeholder && { placeholder }),
109
+ ...(option && option.length > 0 && { options: option }),
110
+ ...(defaultValue && { default_value: defaultValue }),
111
+ },
112
+ };
113
+
114
+ // Add field to model
115
+ if (fieldPath.type === "nested") {
116
+ const groupResult = findGroupInTab(model.json[targetTab], fieldPath.groupId, targetTab);
117
+ if (!groupResult.ok) {
118
+ console.error(groupResult.error);
119
+ process.exitCode = 1;
120
+ return;
121
+ }
122
+ if (groupResult.group.config.fields[fieldPath.nestedFieldId]) {
123
+ console.error(`Field "${fieldPath.nestedFieldId}" already exists in group "${fieldPath.groupId}"`);
124
+ process.exitCode = 1;
125
+ return;
126
+ }
127
+ groupResult.group.config.fields[fieldPath.nestedFieldId] = fieldDefinition;
128
+ } else {
129
+ for (const [tabName, tabFields] of Object.entries(model.json)) {
130
+ if (tabFields[fieldId]) {
131
+ console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
132
+ process.exitCode = 1;
133
+ return;
134
+ }
135
+ for (const [groupFieldId, groupField] of Object.entries(tabFields)) {
136
+ if (isGroupField(groupField) && groupField.config.fields[fieldId]) {
137
+ console.error(`Field "${fieldId}" already exists in group "${groupFieldId}" in tab "${tabName}"`);
138
+ process.exitCode = 1;
139
+ return;
140
+ }
141
+ }
142
+ }
143
+ model.json[targetTab][fieldId] = fieldDefinition;
144
+ }
145
+
146
+ // Write updated model
147
+ await framework.updateCustomType(model);
148
+
149
+ if (fieldPath.type === "nested") {
150
+ console.info(`Added field "${fieldPath.nestedFieldId}" (Select) to group "${fieldPath.groupId}" in ${typeId}`);
151
+ } else {
152
+ console.info(`Added field "${fieldId}" (Select) to "${targetTab}" tab in ${typeId}`);
153
+ }
154
+
155
+ try {
156
+ await buildTypes({ output: types });
157
+ console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
158
+ } catch (error) {
159
+ console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
160
+ }
161
+
162
+ console.info();
163
+ console.info("Next: Add more fields with `prismic page-type add-field`");
164
+
165
+ if (framework) {
166
+ const docsPath = getDocsPath(framework.id);
167
+ console.info(
168
+ ` Run \`prismic docs fetch ${docsPath}#write-page-components\` to learn how to implement a page file`,
169
+ );
170
+ }
171
+ }