@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,228 @@
1
+ import type { CustomType, Link } 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 { stringify } from "./lib/json";
11
+ import { humanReadable } from "./lib/string";
12
+
13
+ const HELP = `
14
+ Add a link field to an existing custom type.
15
+
16
+ USAGE
17
+ prismic custom-type add-field link <type-id> <field-id> [flags]
18
+
19
+ ARGUMENTS
20
+ type-id Custom type identifier (required)
21
+ field-id Field identifier (required)
22
+
23
+ FLAGS
24
+ -t, --tab string Target tab (default: first existing tab, or "Main")
25
+ -l, --label string Display label for the field (inferred from field-id if omitted)
26
+ -p, --placeholder string Placeholder text
27
+ --variation string Slice variations (can be used multiple times)
28
+ --allow-text Allow text with link
29
+ --allow-target-blank Allow opening link in new tab
30
+ --repeatable Allow multiple links
31
+ --types string Output file for generated types (default: "prismicio-types.d.ts")
32
+ -h, --help Show help for command
33
+
34
+ EXAMPLES
35
+ prismic custom-type add-field link homepage button
36
+ prismic custom-type add-field link homepage cta --allow-text
37
+ prismic custom-type add-field link homepage cta --variation Primary --variation Secondary
38
+ prismic custom-type add-field link homepage links --repeatable
39
+ `.trim();
40
+
41
+ const CustomTypeSchema = v.object({
42
+ id: v.string(),
43
+ label: v.string(),
44
+ repeatable: v.boolean(),
45
+ status: v.boolean(),
46
+ format: v.string(),
47
+ json: v.record(v.string(), v.record(v.string(), v.unknown())),
48
+ });
49
+
50
+ export async function customTypeAddFieldLink(): Promise<void> {
51
+ const {
52
+ values: {
53
+ help,
54
+ tab,
55
+ label,
56
+ placeholder,
57
+ variation,
58
+ "allow-text": allowText,
59
+ "allow-target-blank": allowTargetBlank,
60
+ repeatable,
61
+ types,
62
+ },
63
+ positionals: [typeId, fieldId],
64
+ } = parseArgs({
65
+ args: process.argv.slice(5), // skip: node, script, "custom-type", "add-field", "link"
66
+ options: {
67
+ tab: { type: "string", short: "t" },
68
+ label: { type: "string", short: "l" },
69
+ placeholder: { type: "string", short: "p" },
70
+ variation: { type: "string", multiple: true },
71
+ "allow-text": { type: "boolean" },
72
+ "allow-target-blank": { type: "boolean" },
73
+ repeatable: { type: "boolean" },
74
+ types: { type: "string" },
75
+ help: { type: "boolean", short: "h" },
76
+ },
77
+ allowPositionals: true,
78
+ });
79
+
80
+ if (help) {
81
+ console.info(HELP);
82
+ return;
83
+ }
84
+
85
+ if (!typeId) {
86
+ console.error("Missing required argument: type-id\n");
87
+ console.error("Usage: prismic custom-type add-field link <type-id> <field-id>");
88
+ process.exitCode = 1;
89
+ return;
90
+ }
91
+
92
+ if (!fieldId) {
93
+ console.error("Missing required argument: field-id\n");
94
+ console.error("Usage: prismic custom-type add-field link <type-id> <field-id>");
95
+ process.exitCode = 1;
96
+ return;
97
+ }
98
+
99
+ // Parse and validate field path
100
+ const fieldPath = parseFieldPath(fieldId);
101
+ const pathValidation = validateNestedFieldPath(fieldPath);
102
+ if (!pathValidation.ok) {
103
+ console.error(pathValidation.error);
104
+ process.exitCode = 1;
105
+ return;
106
+ }
107
+
108
+ // Find the custom 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 custom 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(`Custom type not found: ${typeId}\n`);
132
+ console.error(`Create it first with: prismic custom-type create ${typeId}`);
133
+ process.exitCode = 1;
134
+ return;
135
+ }
136
+ if (error instanceof Error) {
137
+ console.error(`Failed to read custom type: ${error.message}`);
138
+ } else {
139
+ console.error("Failed to read custom 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
+ // Build field definition
155
+ const fieldDefinition: Link = {
156
+ type: "Link",
157
+ config: {
158
+ label: label ?? humanReadable(fieldPath.type === "nested" ? fieldPath.nestedFieldId : fieldId),
159
+ ...(placeholder && { placeholder }),
160
+ ...(variation && variation.length > 0 && { variants: variation }),
161
+ ...(allowText && { allowText: true }),
162
+ ...(allowTargetBlank && { allowTargetBlank: true }),
163
+ ...(repeatable && { repeat: true }),
164
+ },
165
+ };
166
+
167
+ // Add field to model
168
+ if (fieldPath.type === "nested") {
169
+ const groupResult = findGroupInTab(model.json[targetTab], fieldPath.groupId, targetTab);
170
+ if (!groupResult.ok) {
171
+ console.error(groupResult.error);
172
+ process.exitCode = 1;
173
+ return;
174
+ }
175
+ if (groupResult.group.config.fields[fieldPath.nestedFieldId]) {
176
+ console.error(`Field "${fieldPath.nestedFieldId}" already exists in group "${fieldPath.groupId}"`);
177
+ process.exitCode = 1;
178
+ return;
179
+ }
180
+ groupResult.group.config.fields[fieldPath.nestedFieldId] = fieldDefinition;
181
+ } else {
182
+ for (const [tabName, tabFields] of Object.entries(model.json)) {
183
+ if (tabFields[fieldId]) {
184
+ console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
185
+ process.exitCode = 1;
186
+ return;
187
+ }
188
+ for (const [groupFieldId, groupField] of Object.entries(tabFields)) {
189
+ if (isGroupField(groupField) && groupField.config.fields[fieldId]) {
190
+ console.error(`Field "${fieldId}" already exists in group "${groupFieldId}" in tab "${tabName}"`);
191
+ process.exitCode = 1;
192
+ return;
193
+ }
194
+ }
195
+ }
196
+ model.json[targetTab][fieldId] = fieldDefinition;
197
+ }
198
+
199
+ // Write updated model
200
+ try {
201
+ await writeFile(modelPath, stringify(model));
202
+ } catch (error) {
203
+ if (error instanceof Error) {
204
+ console.error(`Failed to update custom type: ${error.message}`);
205
+ } else {
206
+ console.error("Failed to update custom type");
207
+ }
208
+ process.exitCode = 1;
209
+ return;
210
+ }
211
+
212
+ if (fieldPath.type === "nested") {
213
+ console.info(`Added field "${fieldPath.nestedFieldId}" (Link) to group "${fieldPath.groupId}" in ${typeId}`);
214
+ } else {
215
+ console.info(`Added field "${fieldId}" (Link) to "${targetTab}" tab in ${typeId}`);
216
+ }
217
+
218
+ try {
219
+ await buildTypes({ output: types });
220
+ console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
221
+ } catch (error) {
222
+ console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
223
+ }
224
+
225
+ console.info();
226
+ console.info("Next: Add more fields with `prismic custom-type add-field`");
227
+ console.info(" Run `prismic status` when done to find next steps");
228
+ }
@@ -0,0 +1,237 @@
1
+ import type { CustomType, Number as NumberField } 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 { stringify } from "./lib/json";
11
+ import { humanReadable } from "./lib/string";
12
+
13
+ const HELP = `
14
+ Add a number field to an existing custom type.
15
+
16
+ USAGE
17
+ prismic custom-type add-field number <type-id> <field-id> [flags]
18
+
19
+ ARGUMENTS
20
+ type-id Custom type identifier (required)
21
+ field-id Field identifier (required)
22
+
23
+ FLAGS
24
+ -t, --tab string Target tab (default: first existing tab, or "Main")
25
+ -l, --label string Display label for the field (inferred from field-id if omitted)
26
+ -p, --placeholder string Placeholder text
27
+ --min number Minimum value
28
+ --max number Maximum value
29
+ --step number Step increment
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 custom-type add-field number homepage price
35
+ prismic custom-type add-field number product quantity --min 0 --max 100
36
+ prismic custom-type add-field number settings rating --min 1 --max 5 --step 1
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
+ export async function customTypeAddFieldNumber(): Promise<void> {
49
+ const {
50
+ values: { help, tab, label, placeholder, min, max, step, types },
51
+ positionals: [typeId, fieldId],
52
+ } = parseArgs({
53
+ args: process.argv.slice(5), // skip: node, script, "custom-type", "add-field", "number"
54
+ options: {
55
+ tab: { type: "string", short: "t" },
56
+ label: { type: "string", short: "l" },
57
+ placeholder: { type: "string", short: "p" },
58
+ min: { type: "string" },
59
+ max: { type: "string" },
60
+ step: { type: "string" },
61
+ types: { type: "string" },
62
+ help: { type: "boolean", short: "h" },
63
+ },
64
+ allowPositionals: true,
65
+ });
66
+
67
+ if (help) {
68
+ console.info(HELP);
69
+ return;
70
+ }
71
+
72
+ if (!typeId) {
73
+ console.error("Missing required argument: type-id\n");
74
+ console.error("Usage: prismic custom-type add-field number <type-id> <field-id>");
75
+ process.exitCode = 1;
76
+ return;
77
+ }
78
+
79
+ if (!fieldId) {
80
+ console.error("Missing required argument: field-id\n");
81
+ console.error("Usage: prismic custom-type add-field number <type-id> <field-id>");
82
+ process.exitCode = 1;
83
+ return;
84
+ }
85
+
86
+ // Parse and validate field path
87
+ const fieldPath = parseFieldPath(fieldId);
88
+ const pathValidation = validateNestedFieldPath(fieldPath);
89
+ if (!pathValidation.ok) {
90
+ console.error(pathValidation.error);
91
+ process.exitCode = 1;
92
+ return;
93
+ }
94
+
95
+ // Parse numeric values
96
+ const minValue = min !== undefined ? Number(min) : undefined;
97
+ const maxValue = max !== undefined ? Number(max) : undefined;
98
+ const stepValue = step !== undefined ? Number(step) : undefined;
99
+
100
+ if (min !== undefined && Number.isNaN(minValue)) {
101
+ console.error("Invalid --min value: must be a number");
102
+ process.exitCode = 1;
103
+ return;
104
+ }
105
+
106
+ if (max !== undefined && Number.isNaN(maxValue)) {
107
+ console.error("Invalid --max value: must be a number");
108
+ process.exitCode = 1;
109
+ return;
110
+ }
111
+
112
+ if (step !== undefined && Number.isNaN(stepValue)) {
113
+ console.error("Invalid --step value: must be a number");
114
+ process.exitCode = 1;
115
+ return;
116
+ }
117
+
118
+ // Find the custom type file
119
+ const projectRoot = await findUpward("package.json");
120
+ if (!projectRoot) {
121
+ console.error("Could not find project root (no package.json found)");
122
+ process.exitCode = 1;
123
+ return;
124
+ }
125
+
126
+ const modelPath = new URL(`customtypes/${typeId}/index.json`, projectRoot);
127
+
128
+ // Read and parse the model
129
+ let model: CustomType;
130
+ try {
131
+ const contents = await readFile(modelPath, "utf8");
132
+ const result = v.safeParse(CustomTypeSchema, JSON.parse(contents));
133
+ if (!result.success) {
134
+ console.error(`Invalid custom type model: ${modelPath.href}`);
135
+ process.exitCode = 1;
136
+ return;
137
+ }
138
+ model = result.output as CustomType;
139
+ } catch (error) {
140
+ if (error instanceof Error && "code" in error && error.code === "ENOENT") {
141
+ console.error(`Custom type not found: ${typeId}\n`);
142
+ console.error(`Create it first with: prismic custom-type create ${typeId}`);
143
+ process.exitCode = 1;
144
+ return;
145
+ }
146
+ if (error instanceof Error) {
147
+ console.error(`Failed to read custom type: ${error.message}`);
148
+ } else {
149
+ console.error("Failed to read custom type");
150
+ }
151
+ process.exitCode = 1;
152
+ return;
153
+ }
154
+
155
+ // Determine target tab
156
+ const existingTabs = Object.keys(model.json);
157
+ const targetTab = tab ?? existingTabs[0] ?? "Main";
158
+
159
+ // Initialize tab if it doesn't exist
160
+ if (!model.json[targetTab]) {
161
+ model.json[targetTab] = {};
162
+ }
163
+
164
+ // Build field definition
165
+ const fieldDefinition: NumberField = {
166
+ type: "Number",
167
+ config: {
168
+ label: label ?? humanReadable(fieldPath.type === "nested" ? fieldPath.nestedFieldId : fieldId),
169
+ ...(placeholder && { placeholder }),
170
+ ...(minValue !== undefined && { min: minValue }),
171
+ ...(maxValue !== undefined && { max: maxValue }),
172
+ ...(stepValue !== undefined && { step: stepValue }),
173
+ },
174
+ };
175
+
176
+ // Add field to model
177
+ if (fieldPath.type === "nested") {
178
+ const groupResult = findGroupInTab(model.json[targetTab], fieldPath.groupId, targetTab);
179
+ if (!groupResult.ok) {
180
+ console.error(groupResult.error);
181
+ process.exitCode = 1;
182
+ return;
183
+ }
184
+ if (groupResult.group.config.fields[fieldPath.nestedFieldId]) {
185
+ console.error(`Field "${fieldPath.nestedFieldId}" already exists in group "${fieldPath.groupId}"`);
186
+ process.exitCode = 1;
187
+ return;
188
+ }
189
+ groupResult.group.config.fields[fieldPath.nestedFieldId] = fieldDefinition;
190
+ } else {
191
+ for (const [tabName, tabFields] of Object.entries(model.json)) {
192
+ if (tabFields[fieldId]) {
193
+ console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
194
+ process.exitCode = 1;
195
+ return;
196
+ }
197
+ for (const [groupFieldId, groupField] of Object.entries(tabFields)) {
198
+ if (isGroupField(groupField) && groupField.config.fields[fieldId]) {
199
+ console.error(`Field "${fieldId}" already exists in group "${groupFieldId}" in tab "${tabName}"`);
200
+ process.exitCode = 1;
201
+ return;
202
+ }
203
+ }
204
+ }
205
+ model.json[targetTab][fieldId] = fieldDefinition;
206
+ }
207
+
208
+ // Write updated model
209
+ try {
210
+ await writeFile(modelPath, stringify(model));
211
+ } catch (error) {
212
+ if (error instanceof Error) {
213
+ console.error(`Failed to update custom type: ${error.message}`);
214
+ } else {
215
+ console.error("Failed to update custom type");
216
+ }
217
+ process.exitCode = 1;
218
+ return;
219
+ }
220
+
221
+ if (fieldPath.type === "nested") {
222
+ console.info(`Added field "${fieldPath.nestedFieldId}" (Number) to group "${fieldPath.groupId}" in ${typeId}`);
223
+ } else {
224
+ console.info(`Added field "${fieldId}" (Number) to "${targetTab}" tab in ${typeId}`);
225
+ }
226
+
227
+ try {
228
+ await buildTypes({ output: types });
229
+ console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
230
+ } catch (error) {
231
+ console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
232
+ }
233
+
234
+ console.info();
235
+ console.info("Next: Add more fields with `prismic custom-type add-field`");
236
+ console.info(" Run `prismic status` when done to find next steps");
237
+ }
@@ -0,0 +1,229 @@
1
+ import type { CustomType, RichText } 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 { stringify } from "./lib/json";
11
+ import { humanReadable } from "./lib/string";
12
+
13
+ const HELP = `
14
+ Add a rich text field to an existing custom type.
15
+
16
+ USAGE
17
+ prismic custom-type add-field rich-text <type-id> <field-id> [flags]
18
+
19
+ ARGUMENTS
20
+ type-id Custom type identifier (required)
21
+ field-id Field identifier (required)
22
+
23
+ FLAGS
24
+ -t, --tab string Target tab (default: first existing tab, or "Main")
25
+ -l, --label string Display label for the field (inferred from field-id if omitted)
26
+ -p, --placeholder string Placeholder text
27
+ --single string Allowed block types for single-line (comma-separated)
28
+ --multi string Allowed block types for multi-line (comma-separated)
29
+ --allow-target-blank Allow opening links in new tab
30
+ --types string Output file for generated types (default: "prismicio-types.d.ts")
31
+ -h, --help Show help for command
32
+
33
+ BLOCK TYPES
34
+ heading1, heading2, heading3, heading4, heading5, heading6,
35
+ paragraph, strong, em, preformatted, hyperlink, image, embed,
36
+ list-item, o-list-item, rtl
37
+
38
+ EXAMPLES
39
+ prismic custom-type add-field rich-text homepage body
40
+ prismic custom-type add-field rich-text article content --multi "paragraph,heading2,heading3,strong,em,hyperlink"
41
+ prismic custom-type add-field rich-text page tagline --single "heading1"
42
+ prismic custom-type add-field rich-text blog post --multi "paragraph,strong,em,hyperlink" --allow-target-blank
43
+ `.trim();
44
+
45
+ const CustomTypeSchema = v.object({
46
+ id: v.string(),
47
+ label: v.string(),
48
+ repeatable: v.boolean(),
49
+ status: v.boolean(),
50
+ format: v.string(),
51
+ json: v.record(v.string(), v.record(v.string(), v.unknown())),
52
+ });
53
+
54
+ export async function customTypeAddFieldRichText(): Promise<void> {
55
+ const {
56
+ values: {
57
+ help,
58
+ tab,
59
+ label,
60
+ placeholder,
61
+ single,
62
+ multi,
63
+ "allow-target-blank": allowTargetBlank,
64
+ types,
65
+ },
66
+ positionals: [typeId, fieldId],
67
+ } = parseArgs({
68
+ args: process.argv.slice(5), // skip: node, script, "custom-type", "add-field", "rich-text"
69
+ options: {
70
+ tab: { type: "string", short: "t" },
71
+ label: { type: "string", short: "l" },
72
+ placeholder: { type: "string", short: "p" },
73
+ single: { type: "string" },
74
+ multi: { type: "string" },
75
+ "allow-target-blank": { type: "boolean" },
76
+ types: { type: "string" },
77
+ help: { type: "boolean", short: "h" },
78
+ },
79
+ allowPositionals: true,
80
+ });
81
+
82
+ if (help) {
83
+ console.info(HELP);
84
+ return;
85
+ }
86
+
87
+ if (!typeId) {
88
+ console.error("Missing required argument: type-id\n");
89
+ console.error("Usage: prismic custom-type add-field rich-text <type-id> <field-id>");
90
+ process.exitCode = 1;
91
+ return;
92
+ }
93
+
94
+ if (!fieldId) {
95
+ console.error("Missing required argument: field-id\n");
96
+ console.error("Usage: prismic custom-type add-field rich-text <type-id> <field-id>");
97
+ process.exitCode = 1;
98
+ return;
99
+ }
100
+
101
+ // Parse and validate field path
102
+ const fieldPath = parseFieldPath(fieldId);
103
+ const pathValidation = validateNestedFieldPath(fieldPath);
104
+ if (!pathValidation.ok) {
105
+ console.error(pathValidation.error);
106
+ process.exitCode = 1;
107
+ return;
108
+ }
109
+
110
+ // Find the custom type file
111
+ const projectRoot = await findUpward("package.json");
112
+ if (!projectRoot) {
113
+ console.error("Could not find project root (no package.json found)");
114
+ process.exitCode = 1;
115
+ return;
116
+ }
117
+
118
+ const modelPath = new URL(`customtypes/${typeId}/index.json`, projectRoot);
119
+
120
+ // Read and parse the model
121
+ let model: CustomType;
122
+ try {
123
+ const contents = await readFile(modelPath, "utf8");
124
+ const result = v.safeParse(CustomTypeSchema, JSON.parse(contents));
125
+ if (!result.success) {
126
+ console.error(`Invalid custom type model: ${modelPath.href}`);
127
+ process.exitCode = 1;
128
+ return;
129
+ }
130
+ model = result.output as CustomType;
131
+ } catch (error) {
132
+ if (error instanceof Error && "code" in error && error.code === "ENOENT") {
133
+ console.error(`Custom type not found: ${typeId}\n`);
134
+ console.error(`Create it first with: prismic custom-type create ${typeId}`);
135
+ process.exitCode = 1;
136
+ return;
137
+ }
138
+ if (error instanceof Error) {
139
+ console.error(`Failed to read custom type: ${error.message}`);
140
+ } else {
141
+ console.error("Failed to read custom type");
142
+ }
143
+ process.exitCode = 1;
144
+ return;
145
+ }
146
+
147
+ // Determine target tab
148
+ const existingTabs = Object.keys(model.json);
149
+ const targetTab = tab ?? existingTabs[0] ?? "Main";
150
+
151
+ // Initialize tab if it doesn't exist
152
+ if (!model.json[targetTab]) {
153
+ model.json[targetTab] = {};
154
+ }
155
+
156
+ // Build field definition
157
+ const fieldDefinition: RichText = {
158
+ type: "StructuredText",
159
+ config: {
160
+ label: label ?? humanReadable(fieldPath.type === "nested" ? fieldPath.nestedFieldId : fieldId),
161
+ ...(placeholder && { placeholder }),
162
+ ...(single && { single }),
163
+ ...(multi && { multi }),
164
+ ...(allowTargetBlank && { allowTargetBlank: true }),
165
+ },
166
+ };
167
+
168
+ // Add field to model
169
+ if (fieldPath.type === "nested") {
170
+ const groupResult = findGroupInTab(model.json[targetTab], fieldPath.groupId, targetTab);
171
+ if (!groupResult.ok) {
172
+ console.error(groupResult.error);
173
+ process.exitCode = 1;
174
+ return;
175
+ }
176
+ if (groupResult.group.config.fields[fieldPath.nestedFieldId]) {
177
+ console.error(`Field "${fieldPath.nestedFieldId}" already exists in group "${fieldPath.groupId}"`);
178
+ process.exitCode = 1;
179
+ return;
180
+ }
181
+ groupResult.group.config.fields[fieldPath.nestedFieldId] = fieldDefinition;
182
+ } else {
183
+ for (const [tabName, tabFields] of Object.entries(model.json)) {
184
+ if (tabFields[fieldId]) {
185
+ console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
186
+ process.exitCode = 1;
187
+ return;
188
+ }
189
+ for (const [groupFieldId, groupField] of Object.entries(tabFields)) {
190
+ if (isGroupField(groupField) && groupField.config.fields[fieldId]) {
191
+ console.error(`Field "${fieldId}" already exists in group "${groupFieldId}" in tab "${tabName}"`);
192
+ process.exitCode = 1;
193
+ return;
194
+ }
195
+ }
196
+ }
197
+ model.json[targetTab][fieldId] = fieldDefinition;
198
+ }
199
+
200
+ // Write updated model
201
+ try {
202
+ await writeFile(modelPath, stringify(model));
203
+ } catch (error) {
204
+ if (error instanceof Error) {
205
+ console.error(`Failed to update custom type: ${error.message}`);
206
+ } else {
207
+ console.error("Failed to update custom type");
208
+ }
209
+ process.exitCode = 1;
210
+ return;
211
+ }
212
+
213
+ if (fieldPath.type === "nested") {
214
+ console.info(`Added field "${fieldPath.nestedFieldId}" (StructuredText) to group "${fieldPath.groupId}" in ${typeId}`);
215
+ } else {
216
+ console.info(`Added field "${fieldId}" (StructuredText) to "${targetTab}" tab in ${typeId}`);
217
+ }
218
+
219
+ try {
220
+ await buildTypes({ output: types });
221
+ console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
222
+ } catch (error) {
223
+ console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
224
+ }
225
+
226
+ console.info();
227
+ console.info("Next: Add more fields with `prismic custom-type add-field`");
228
+ console.info(" Run `prismic status` when done to find next steps");
229
+ }