@angeloashmore/prismic-cli-poc 0.0.0-canary.1d36cd8

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 (131) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +98 -0
  3. package/dist/index.mjs +2548 -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 +192 -0
  8. package/src/custom-type-add-field-color.ts +177 -0
  9. package/src/custom-type-add-field-date.ts +180 -0
  10. package/src/custom-type-add-field-embed.ts +177 -0
  11. package/src/custom-type-add-field-geo-point.ts +174 -0
  12. package/src/custom-type-add-field-image.ts +177 -0
  13. package/src/custom-type-add-field-key-text.ts +177 -0
  14. package/src/custom-type-add-field-link.ts +201 -0
  15. package/src/custom-type-add-field-number.ts +209 -0
  16. package/src/custom-type-add-field-rich-text.ts +202 -0
  17. package/src/custom-type-add-field-select.ts +192 -0
  18. package/src/custom-type-add-field-timestamp.ts +180 -0
  19. package/src/custom-type-add-field-uid.ts +177 -0
  20. package/src/custom-type-add-field.ts +111 -0
  21. package/src/custom-type-connect-slice.ts +220 -0
  22. package/src/custom-type-create.ts +118 -0
  23. package/src/custom-type-disconnect-slice.ts +177 -0
  24. package/src/custom-type-list.ts +110 -0
  25. package/src/custom-type-remove-field.ts +177 -0
  26. package/src/custom-type-remove.ts +144 -0
  27. package/src/custom-type-set-name.ts +144 -0
  28. package/src/custom-type-view.ts +118 -0
  29. package/src/custom-type.ts +85 -0
  30. package/src/index.ts +127 -0
  31. package/src/init.ts +64 -0
  32. package/src/lib/auth.ts +83 -0
  33. package/src/lib/config.ts +111 -0
  34. package/src/lib/custom-types-api.ts +438 -0
  35. package/src/lib/file.ts +49 -0
  36. package/src/lib/framework.ts +143 -0
  37. package/src/lib/json.ts +3 -0
  38. package/src/lib/request.ts +116 -0
  39. package/src/lib/slice.ts +115 -0
  40. package/src/lib/string.ts +6 -0
  41. package/src/lib/url.ts +25 -0
  42. package/src/locale-add.ts +116 -0
  43. package/src/locale-list.ts +107 -0
  44. package/src/locale-remove.ts +88 -0
  45. package/src/locale-set-default.ts +131 -0
  46. package/src/locale.ts +60 -0
  47. package/src/login.ts +152 -0
  48. package/src/logout.ts +36 -0
  49. package/src/page-type-add-field-boolean.ts +192 -0
  50. package/src/page-type-add-field-color.ts +177 -0
  51. package/src/page-type-add-field-date.ts +180 -0
  52. package/src/page-type-add-field-embed.ts +177 -0
  53. package/src/page-type-add-field-geo-point.ts +174 -0
  54. package/src/page-type-add-field-image.ts +177 -0
  55. package/src/page-type-add-field-key-text.ts +177 -0
  56. package/src/page-type-add-field-link.ts +201 -0
  57. package/src/page-type-add-field-number.ts +209 -0
  58. package/src/page-type-add-field-rich-text.ts +202 -0
  59. package/src/page-type-add-field-select.ts +192 -0
  60. package/src/page-type-add-field-timestamp.ts +180 -0
  61. package/src/page-type-add-field-uid.ts +177 -0
  62. package/src/page-type-add-field.ts +111 -0
  63. package/src/page-type-connect-slice.ts +220 -0
  64. package/src/page-type-create.ts +142 -0
  65. package/src/page-type-disconnect-slice.ts +177 -0
  66. package/src/page-type-list.ts +109 -0
  67. package/src/page-type-remove-field.ts +177 -0
  68. package/src/page-type-remove.ts +144 -0
  69. package/src/page-type-set-name.ts +144 -0
  70. package/src/page-type-set-repeatable.ts +153 -0
  71. package/src/page-type-view.ts +118 -0
  72. package/src/page-type.ts +90 -0
  73. package/src/preview-add.ts +126 -0
  74. package/src/preview-get-simulator.ts +104 -0
  75. package/src/preview-list.ts +106 -0
  76. package/src/preview-remove-simulator.ts +80 -0
  77. package/src/preview-remove.ts +109 -0
  78. package/src/preview-set-name.ts +137 -0
  79. package/src/preview-set-simulator.ts +116 -0
  80. package/src/preview.ts +75 -0
  81. package/src/pull.ts +247 -0
  82. package/src/push.ts +405 -0
  83. package/src/repo-create.ts +136 -0
  84. package/src/repo-get-access.ts +86 -0
  85. package/src/repo-list.ts +100 -0
  86. package/src/repo-set-access.ts +100 -0
  87. package/src/repo-set-name.ts +102 -0
  88. package/src/repo-view.ts +113 -0
  89. package/src/repo.ts +70 -0
  90. package/src/slice-add-field-boolean.ts +173 -0
  91. package/src/slice-add-field-color.ts +158 -0
  92. package/src/slice-add-field-date.ts +158 -0
  93. package/src/slice-add-field-embed.ts +158 -0
  94. package/src/slice-add-field-geo-point.ts +155 -0
  95. package/src/slice-add-field-image.ts +155 -0
  96. package/src/slice-add-field-key-text.ts +158 -0
  97. package/src/slice-add-field-link.ts +178 -0
  98. package/src/slice-add-field-number.ts +158 -0
  99. package/src/slice-add-field-rich-text.ts +183 -0
  100. package/src/slice-add-field-select.ts +173 -0
  101. package/src/slice-add-field-timestamp.ts +158 -0
  102. package/src/slice-add-field.ts +106 -0
  103. package/src/slice-add-variation.ts +145 -0
  104. package/src/slice-create.ts +148 -0
  105. package/src/slice-list-variations.ts +67 -0
  106. package/src/slice-list.ts +88 -0
  107. package/src/slice-remove-field.ts +128 -0
  108. package/src/slice-remove-variation.ts +118 -0
  109. package/src/slice-remove.ts +97 -0
  110. package/src/slice-rename.ts +128 -0
  111. package/src/slice-view.ts +77 -0
  112. package/src/slice.ts +90 -0
  113. package/src/status.ts +733 -0
  114. package/src/token-create.ts +203 -0
  115. package/src/token-delete.ts +182 -0
  116. package/src/token-list.ts +223 -0
  117. package/src/token-set-name.ts +193 -0
  118. package/src/token.ts +60 -0
  119. package/src/webhook-add-header.ts +118 -0
  120. package/src/webhook-create.ts +152 -0
  121. package/src/webhook-disable.ts +109 -0
  122. package/src/webhook-enable.ts +132 -0
  123. package/src/webhook-list.ts +93 -0
  124. package/src/webhook-remove-header.ts +117 -0
  125. package/src/webhook-remove.ts +106 -0
  126. package/src/webhook-set-triggers.ts +148 -0
  127. package/src/webhook-status.ts +90 -0
  128. package/src/webhook-test.ts +106 -0
  129. package/src/webhook-view.ts +147 -0
  130. package/src/webhook.ts +95 -0
  131. package/src/whoami.ts +62 -0
@@ -0,0 +1,209 @@
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 { stringify } from "./lib/json";
10
+ import { humanReadable } from "./lib/string";
11
+
12
+ const HELP = `
13
+ Add a number field to an existing page type.
14
+
15
+ USAGE
16
+ prismic page-type add-field number <type-id> <field-id> [flags]
17
+
18
+ ARGUMENTS
19
+ type-id Page type identifier (required)
20
+ field-id Field identifier (required)
21
+
22
+ Types are generated by default after changes. Use --no-types to skip.
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
+ --min number Minimum value
29
+ --max number Maximum value
30
+ --step number Step increment
31
+ --types string Output file for generated types (default: "prismicio-types.d.ts")
32
+ --no-types Skip type generation
33
+ -h, --help Show help for command
34
+
35
+ EXAMPLES
36
+ prismic page-type add-field number homepage price
37
+ prismic page-type add-field number product quantity --min 0 --max 100
38
+ prismic page-type add-field number settings rating --min 1 --max 5 --step 1
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 pageTypeAddFieldNumber(): Promise<void> {
51
+ const {
52
+ values: { help, tab, label, placeholder, min, max, step, types, "no-types": noTypes },
53
+ positionals: [typeId, fieldId],
54
+ } = parseArgs({
55
+ args: process.argv.slice(5), // skip: node, script, "page-type", "add-field", "number"
56
+ options: {
57
+ tab: { type: "string", short: "t" },
58
+ label: { type: "string", short: "l" },
59
+ placeholder: { type: "string", short: "p" },
60
+ min: { type: "string" },
61
+ max: { type: "string" },
62
+ step: { type: "string" },
63
+ types: { type: "string" },
64
+ "no-types": { type: "boolean" },
65
+ help: { type: "boolean", short: "h" },
66
+ },
67
+ allowPositionals: true,
68
+ });
69
+
70
+ if (help) {
71
+ console.info(HELP);
72
+ return;
73
+ }
74
+
75
+ if (!typeId) {
76
+ console.error("Missing required argument: type-id\n");
77
+ console.error("Usage: prismic page-type add-field number <type-id> <field-id>");
78
+ process.exitCode = 1;
79
+ return;
80
+ }
81
+
82
+ if (!fieldId) {
83
+ console.error("Missing required argument: field-id\n");
84
+ console.error("Usage: prismic page-type add-field number <type-id> <field-id>");
85
+ process.exitCode = 1;
86
+ return;
87
+ }
88
+
89
+ // Parse numeric values
90
+ const minValue = min !== undefined ? Number(min) : undefined;
91
+ const maxValue = max !== undefined ? Number(max) : undefined;
92
+ const stepValue = step !== undefined ? Number(step) : undefined;
93
+
94
+ if (min !== undefined && Number.isNaN(minValue)) {
95
+ console.error("Invalid --min value: must be a number");
96
+ process.exitCode = 1;
97
+ return;
98
+ }
99
+
100
+ if (max !== undefined && Number.isNaN(maxValue)) {
101
+ console.error("Invalid --max value: must be a number");
102
+ process.exitCode = 1;
103
+ return;
104
+ }
105
+
106
+ if (step !== undefined && Number.isNaN(stepValue)) {
107
+ console.error("Invalid --step value: must be a number");
108
+ process.exitCode = 1;
109
+ return;
110
+ }
111
+
112
+ // Find the page type file
113
+ const projectRoot = await findUpward("package.json");
114
+ if (!projectRoot) {
115
+ console.error("Could not find project root (no package.json found)");
116
+ process.exitCode = 1;
117
+ return;
118
+ }
119
+
120
+ const modelPath = new URL(`customtypes/${typeId}/index.json`, projectRoot);
121
+
122
+ // Read and parse the model
123
+ let model: CustomType;
124
+ try {
125
+ const contents = await readFile(modelPath, "utf8");
126
+ const result = v.safeParse(CustomTypeSchema, JSON.parse(contents));
127
+ if (!result.success) {
128
+ console.error(`Invalid page type model: ${modelPath.href}`);
129
+ process.exitCode = 1;
130
+ return;
131
+ }
132
+ model = result.output as CustomType;
133
+ } catch (error) {
134
+ if (error instanceof Error && "code" in error && error.code === "ENOENT") {
135
+ console.error(`Page type not found: ${typeId}\n`);
136
+ console.error(`Create it first with: prismic page-type create ${typeId}`);
137
+ process.exitCode = 1;
138
+ return;
139
+ }
140
+ if (error instanceof Error) {
141
+ console.error(`Failed to read page type: ${error.message}`);
142
+ } else {
143
+ console.error("Failed to read page type");
144
+ }
145
+ process.exitCode = 1;
146
+ return;
147
+ }
148
+
149
+ // Determine target tab
150
+ const existingTabs = Object.keys(model.json);
151
+ const targetTab = tab ?? existingTabs[0] ?? "Main";
152
+
153
+ // Initialize tab if it doesn't exist
154
+ if (!model.json[targetTab]) {
155
+ model.json[targetTab] = {};
156
+ }
157
+
158
+ // Check if field already exists in any tab
159
+ for (const [tabName, tabFields] of Object.entries(model.json)) {
160
+ if (tabFields[fieldId]) {
161
+ console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
162
+ process.exitCode = 1;
163
+ return;
164
+ }
165
+ }
166
+
167
+ // Build field definition
168
+ const fieldDefinition: NumberField = {
169
+ type: "Number",
170
+ config: {
171
+ label: label ?? humanReadable(fieldId),
172
+ ...(placeholder && { placeholder }),
173
+ ...(minValue !== undefined && { min: minValue }),
174
+ ...(maxValue !== undefined && { max: maxValue }),
175
+ ...(stepValue !== undefined && { step: stepValue }),
176
+ },
177
+ };
178
+
179
+ // Add field to model
180
+ model.json[targetTab][fieldId] = fieldDefinition;
181
+
182
+ // Write updated model
183
+ try {
184
+ await writeFile(modelPath, stringify(model));
185
+ } catch (error) {
186
+ if (error instanceof Error) {
187
+ console.error(`Failed to update page type: ${error.message}`);
188
+ } else {
189
+ console.error("Failed to update page type");
190
+ }
191
+ process.exitCode = 1;
192
+ return;
193
+ }
194
+
195
+ console.info(`Added field "${fieldId}" (Number) to "${targetTab}" tab in ${typeId}`);
196
+
197
+ if (!noTypes) {
198
+ try {
199
+ await buildTypes({ output: types });
200
+ console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
201
+ } catch (error) {
202
+ console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
203
+ }
204
+ }
205
+
206
+ console.info();
207
+ console.info("Next: Add more fields with `prismic page-type add-field`");
208
+ console.info(" Run `prismic status` when done to find next steps");
209
+ }
@@ -0,0 +1,202 @@
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 { stringify } from "./lib/json";
10
+ import { humanReadable } from "./lib/string";
11
+
12
+ const HELP = `
13
+ Add a rich text field to an existing page type.
14
+
15
+ USAGE
16
+ prismic page-type add-field rich-text <type-id> <field-id> [flags]
17
+
18
+ ARGUMENTS
19
+ type-id Page type identifier (required)
20
+ field-id Field identifier (required)
21
+
22
+ Types are generated by default after changes. Use --no-types to skip.
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
+ --single string Allowed block types for single-line (comma-separated)
29
+ --multi string Allowed block types for multi-line (comma-separated)
30
+ --allow-target-blank Allow opening links in new tab
31
+ --types string Output file for generated types (default: "prismicio-types.d.ts")
32
+ --no-types Skip type generation
33
+ -h, --help Show help for command
34
+
35
+ BLOCK TYPES
36
+ heading1, heading2, heading3, heading4, heading5, heading6,
37
+ paragraph, strong, em, preformatted, hyperlink, image, embed,
38
+ list-item, o-list-item, rtl
39
+
40
+ EXAMPLES
41
+ prismic page-type add-field rich-text homepage body
42
+ prismic page-type add-field rich-text article content --multi "paragraph,heading2,heading3,strong,em,hyperlink"
43
+ prismic page-type add-field rich-text page tagline --single "heading1"
44
+ prismic page-type add-field rich-text blog post --multi "paragraph,strong,em,hyperlink" --allow-target-blank
45
+ `.trim();
46
+
47
+ const CustomTypeSchema = v.object({
48
+ id: v.string(),
49
+ label: v.string(),
50
+ repeatable: v.boolean(),
51
+ status: v.boolean(),
52
+ format: v.string(),
53
+ json: v.record(v.string(), v.record(v.string(), v.unknown())),
54
+ });
55
+
56
+ export async function pageTypeAddFieldRichText(): Promise<void> {
57
+ const {
58
+ values: {
59
+ help,
60
+ tab,
61
+ label,
62
+ placeholder,
63
+ single,
64
+ multi,
65
+ "allow-target-blank": allowTargetBlank,
66
+ types,
67
+ "no-types": noTypes,
68
+ },
69
+ positionals: [typeId, fieldId],
70
+ } = parseArgs({
71
+ args: process.argv.slice(5), // skip: node, script, "page-type", "add-field", "rich-text"
72
+ options: {
73
+ tab: { type: "string", short: "t" },
74
+ label: { type: "string", short: "l" },
75
+ placeholder: { type: "string", short: "p" },
76
+ single: { type: "string" },
77
+ multi: { type: "string" },
78
+ "allow-target-blank": { type: "boolean" },
79
+ types: { type: "string" },
80
+ "no-types": { type: "boolean" },
81
+ help: { type: "boolean", short: "h" },
82
+ },
83
+ allowPositionals: true,
84
+ });
85
+
86
+ if (help) {
87
+ console.info(HELP);
88
+ return;
89
+ }
90
+
91
+ if (!typeId) {
92
+ console.error("Missing required argument: type-id\n");
93
+ console.error("Usage: prismic page-type add-field rich-text <type-id> <field-id>");
94
+ process.exitCode = 1;
95
+ return;
96
+ }
97
+
98
+ if (!fieldId) {
99
+ console.error("Missing required argument: field-id\n");
100
+ console.error("Usage: prismic page-type add-field rich-text <type-id> <field-id>");
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
+ // Check if field already exists in any tab
152
+ for (const [tabName, tabFields] of Object.entries(model.json)) {
153
+ if (tabFields[fieldId]) {
154
+ console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
155
+ process.exitCode = 1;
156
+ return;
157
+ }
158
+ }
159
+
160
+ // Build field definition
161
+ const fieldDefinition: RichText = {
162
+ type: "StructuredText",
163
+ config: {
164
+ label: label ?? humanReadable(fieldId),
165
+ ...(placeholder && { placeholder }),
166
+ ...(single && { single }),
167
+ ...(multi && { multi }),
168
+ ...(allowTargetBlank && { allowTargetBlank: true }),
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}" (StructuredText) to "${targetTab}" tab in ${typeId}`);
189
+
190
+ if (!noTypes) {
191
+ try {
192
+ await buildTypes({ output: types });
193
+ console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
194
+ } catch (error) {
195
+ console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
196
+ }
197
+ }
198
+
199
+ console.info();
200
+ console.info("Next: Add more fields with `prismic page-type add-field`");
201
+ console.info(" Run `prismic status` when done to find next steps");
202
+ }
@@ -0,0 +1,192 @@
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 { stringify } from "./lib/json";
10
+ import { humanReadable } from "./lib/string";
11
+
12
+ const HELP = `
13
+ Add a select (dropdown) field to an existing page type.
14
+
15
+ USAGE
16
+ prismic page-type add-field select <type-id> <field-id> [flags]
17
+
18
+ ARGUMENTS
19
+ type-id Page type identifier (required)
20
+ field-id Field identifier (required)
21
+
22
+ Types are generated by default after changes. Use --no-types to skip.
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
+ --no-types Skip type generation
32
+ -h, --help Show help for command
33
+
34
+ EXAMPLES
35
+ prismic page-type add-field select homepage layout --option "full" --option "sidebar"
36
+ prismic page-type add-field select product size --option "small" --option "medium" --option "large" --default "medium"
37
+ prismic page-type add-field select article status --option "draft" --option "published" --label "Status"
38
+ `.trim();
39
+
40
+ const CustomTypeSchema = v.object({
41
+ id: v.string(),
42
+ label: v.string(),
43
+ repeatable: v.boolean(),
44
+ status: v.boolean(),
45
+ format: v.string(),
46
+ json: v.record(v.string(), v.record(v.string(), v.unknown())),
47
+ });
48
+
49
+ export async function pageTypeAddFieldSelect(): Promise<void> {
50
+ const {
51
+ values: {
52
+ help,
53
+ tab,
54
+ label,
55
+ placeholder,
56
+ option,
57
+ default: defaultValue,
58
+ types,
59
+ "no-types": noTypes,
60
+ },
61
+ positionals: [typeId, fieldId],
62
+ } = parseArgs({
63
+ args: process.argv.slice(5), // skip: node, script, "page-type", "add-field", "select"
64
+ options: {
65
+ tab: { type: "string", short: "t" },
66
+ label: { type: "string", short: "l" },
67
+ placeholder: { type: "string", short: "p" },
68
+ option: { type: "string", multiple: true },
69
+ default: { type: "string" },
70
+ types: { type: "string" },
71
+ "no-types": { type: "boolean" },
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
+ // Find the page type file
97
+ const projectRoot = await findUpward("package.json");
98
+ if (!projectRoot) {
99
+ console.error("Could not find project root (no package.json found)");
100
+ process.exitCode = 1;
101
+ return;
102
+ }
103
+
104
+ const modelPath = new URL(`customtypes/${typeId}/index.json`, projectRoot);
105
+
106
+ // Read and parse the model
107
+ let model: CustomType;
108
+ try {
109
+ const contents = await readFile(modelPath, "utf8");
110
+ const result = v.safeParse(CustomTypeSchema, JSON.parse(contents));
111
+ if (!result.success) {
112
+ console.error(`Invalid page type model: ${modelPath.href}`);
113
+ process.exitCode = 1;
114
+ return;
115
+ }
116
+ model = result.output as CustomType;
117
+ } catch (error) {
118
+ if (error instanceof Error && "code" in error && error.code === "ENOENT") {
119
+ console.error(`Page type not found: ${typeId}\n`);
120
+ console.error(`Create it first with: prismic page-type create ${typeId}`);
121
+ process.exitCode = 1;
122
+ return;
123
+ }
124
+ if (error instanceof Error) {
125
+ console.error(`Failed to read page type: ${error.message}`);
126
+ } else {
127
+ console.error("Failed to read page type");
128
+ }
129
+ process.exitCode = 1;
130
+ return;
131
+ }
132
+
133
+ // Determine target tab
134
+ const existingTabs = Object.keys(model.json);
135
+ const targetTab = tab ?? existingTabs[0] ?? "Main";
136
+
137
+ // Initialize tab if it doesn't exist
138
+ if (!model.json[targetTab]) {
139
+ model.json[targetTab] = {};
140
+ }
141
+
142
+ // Check if field already exists in any tab
143
+ for (const [tabName, tabFields] of Object.entries(model.json)) {
144
+ if (tabFields[fieldId]) {
145
+ console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
146
+ process.exitCode = 1;
147
+ return;
148
+ }
149
+ }
150
+
151
+ // Build field definition
152
+ const fieldDefinition: Select = {
153
+ type: "Select",
154
+ config: {
155
+ label: label ?? humanReadable(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
+ model.json[targetTab][fieldId] = fieldDefinition;
164
+
165
+ // Write updated model
166
+ try {
167
+ await writeFile(modelPath, stringify(model));
168
+ } catch (error) {
169
+ if (error instanceof Error) {
170
+ console.error(`Failed to update page type: ${error.message}`);
171
+ } else {
172
+ console.error("Failed to update page type");
173
+ }
174
+ process.exitCode = 1;
175
+ return;
176
+ }
177
+
178
+ console.info(`Added field "${fieldId}" (Select) to "${targetTab}" tab in ${typeId}`);
179
+
180
+ if (!noTypes) {
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
+
189
+ console.info();
190
+ console.info("Next: Add more fields with `prismic page-type add-field`");
191
+ console.info(" Run `prismic status` when done to find next steps");
192
+ }