@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
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@angeloashmore/prismic-cli-poc",
3
+ "version": "0.0.0-canary.1143872",
4
+ "description": "A proof-of-concept developer CLI for Prismic.",
5
+ "keywords": [
6
+ "prismic",
7
+ "typescript"
8
+ ],
9
+ "license": "Apache-2.0",
10
+ "author": "Prismic <contact@prismic.io> (https://prismic.io)",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "ssh://git@github.com/prismicio-community/prismic-cli-poc.git"
14
+ },
15
+ "bin": {
16
+ "prismic": "./dist/index.mjs"
17
+ },
18
+ "files": [
19
+ "./dist",
20
+ "./src"
21
+ ],
22
+ "type": "module",
23
+ "publishConfig": {
24
+ "access": "public"
25
+ },
26
+ "scripts": {
27
+ "build": "tsdown",
28
+ "dev": "tsdown --watch",
29
+ "format": "oxfmt",
30
+ "prepare": "npm run build",
31
+ "lint": "oxlint --deny-warnings",
32
+ "types": "tsc --noEmit",
33
+ "unit": "vitest run --coverage",
34
+ "unit:watch": "vitest watch",
35
+ "test": "npm run lint && npm run types && npm run unit && npm run build"
36
+ },
37
+ "devDependencies": {
38
+ "@prismicio/e2e-tests-utils": "1.15.0",
39
+ "@prismicio/types-internal": "3.16.1",
40
+ "@types/node": "25.0.9",
41
+ "@vitest/coverage-v8": "4.0.17",
42
+ "oxfmt": "^0.24.0",
43
+ "oxlint": "1.39.0",
44
+ "prismic-ts-codegen": "^0.1.28",
45
+ "tsdown": "0.19.0",
46
+ "typescript": "5.9.3",
47
+ "valibot": "^1.2.0",
48
+ "vitest": "4.0.17"
49
+ },
50
+ "engines": {
51
+ "node": ">=24"
52
+ }
53
+ }
@@ -0,0 +1,82 @@
1
+ import type { CustomTypeModel, SharedSliceModel } from "@prismicio/client";
2
+
3
+ import { writeFile } from "node:fs/promises";
4
+ import { parseArgs } from "node:util";
5
+ import { generateTypes, NON_EDITABLE_FILE_HEADER } from "prismic-ts-codegen";
6
+
7
+ import { readLocalCustomTypes, readLocalSlices } from "./lib/custom-types-api";
8
+
9
+ const HELP = `
10
+ Generate TypeScript types from local custom type and slice models.
11
+
12
+ USAGE
13
+ prismic codegen types [flags]
14
+
15
+ FLAGS
16
+ -o, --output string Output file path (default: "prismicio-types.d.ts")
17
+ -h, --help Show help for command
18
+
19
+ EXAMPLES
20
+ prismic codegen types
21
+ prismic codegen types --output custom.d.ts
22
+ `.trim();
23
+
24
+ export async function codegenTypes(): Promise<void> {
25
+ const {
26
+ values: { help, output },
27
+ } = parseArgs({
28
+ args: process.argv.slice(4), // skip: node, script, "codegen", "types"
29
+ options: {
30
+ output: { type: "string", short: "o" },
31
+ help: { type: "boolean", short: "h" },
32
+ },
33
+ allowPositionals: false,
34
+ });
35
+
36
+ if (help) {
37
+ console.info(HELP);
38
+ return;
39
+ }
40
+
41
+ try {
42
+ await buildTypes({ output });
43
+ console.info(`Generated types written to ${output ?? "prismicio-types.d.ts"}`);
44
+ } catch (error) {
45
+ console.error(error instanceof Error ? error.message : String(error));
46
+ process.exitCode = 1;
47
+ }
48
+ }
49
+
50
+ export async function buildTypes(args?: { output?: string }): Promise<void> {
51
+ const output = args?.output ?? "prismicio-types.d.ts";
52
+
53
+ const [customTypesResult, slicesResult] = await Promise.all([
54
+ readLocalCustomTypes(),
55
+ readLocalSlices(),
56
+ ]);
57
+
58
+ if (!customTypesResult.ok) {
59
+ throw new Error(`failed to read local custom types: ${customTypesResult.error}`);
60
+ }
61
+
62
+ if (!slicesResult.ok) {
63
+ throw new Error(`failed to read local slices: ${slicesResult.error}`);
64
+ }
65
+
66
+ const customTypes = customTypesResult.value as unknown as CustomTypeModel[];
67
+ const slices = slicesResult.value as unknown as SharedSliceModel[];
68
+
69
+ const types = generateTypes({
70
+ customTypeModels: customTypes,
71
+ sharedSliceModels: slices,
72
+ typesProvider: "@prismicio/client",
73
+ clientIntegration: {
74
+ includeCreateClientInterface: customTypes.length > 0 || slices.length > 0,
75
+ includeContentNamespace: true,
76
+ },
77
+ });
78
+
79
+ const content = NON_EDITABLE_FILE_HEADER + "\n\n" + types;
80
+
81
+ await writeFile(output, content);
82
+ }
package/src/codegen.ts ADDED
@@ -0,0 +1,45 @@
1
+ import { parseArgs } from "node:util";
2
+
3
+ import { codegenTypes } from "./codegen-types";
4
+
5
+ const HELP = `
6
+ Generate code from Prismic models.
7
+
8
+ USAGE
9
+ prismic codegen <command> [flags]
10
+
11
+ COMMANDS
12
+ types Generate TypeScript types from models pushed to Prismic
13
+
14
+ FLAGS
15
+ -h, --help Show help for command
16
+
17
+ LEARN MORE
18
+ Use \`prismic codegen <command> --help\` for more information about a command.
19
+ `.trim();
20
+
21
+ export async function codegen(): Promise<void> {
22
+ const {
23
+ positionals: [subcommand],
24
+ } = parseArgs({
25
+ args: process.argv.slice(3), // skip: node, script, "codegen"
26
+ options: {
27
+ help: { type: "boolean", short: "h" },
28
+ },
29
+ allowPositionals: true,
30
+ strict: false,
31
+ });
32
+
33
+ switch (subcommand) {
34
+ case "types":
35
+ await codegenTypes();
36
+ break;
37
+ default: {
38
+ if (subcommand) {
39
+ console.error(`Unknown codegen subcommand: ${subcommand}\n`);
40
+ process.exitCode = 1;
41
+ }
42
+ console.info(HELP);
43
+ }
44
+ }
45
+ }
@@ -0,0 +1,222 @@
1
+ import type { BooleanField, CustomType } 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 boolean (toggle) field to an existing custom type.
15
+
16
+ USAGE
17
+ prismic custom-type add-field boolean <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
+ --default Set default value to true
27
+ --true-label string Label shown when toggle is on
28
+ --false-label string Label shown when toggle is off
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 custom-type add-field boolean homepage featured
34
+ prismic custom-type add-field boolean article published --default
35
+ prismic custom-type add-field boolean product available --true-label "In Stock" --false-label "Out of Stock"
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
+ export async function customTypeAddFieldBoolean(): Promise<void> {
48
+ const {
49
+ values: {
50
+ help,
51
+ tab,
52
+ label,
53
+ default: defaultValue,
54
+ "true-label": trueLabel,
55
+ "false-label": falseLabel,
56
+ types,
57
+ },
58
+ positionals: [typeId, fieldId],
59
+ } = parseArgs({
60
+ args: process.argv.slice(5), // skip: node, script, "custom-type", "add-field", "boolean"
61
+ options: {
62
+ tab: { type: "string", short: "t" },
63
+ label: { type: "string", short: "l" },
64
+ default: { type: "boolean" },
65
+ "true-label": { type: "string" },
66
+ "false-label": { type: "string" },
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 custom-type add-field boolean <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 custom-type add-field boolean <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
+ // Find the custom type file
102
+ const projectRoot = await findUpward("package.json");
103
+ if (!projectRoot) {
104
+ console.error("Could not find project root (no package.json found)");
105
+ process.exitCode = 1;
106
+ return;
107
+ }
108
+
109
+ const modelPath = new URL(`customtypes/${typeId}/index.json`, projectRoot);
110
+
111
+ // Read and parse the model
112
+ let model: CustomType;
113
+ try {
114
+ const contents = await readFile(modelPath, "utf8");
115
+ const result = v.safeParse(CustomTypeSchema, JSON.parse(contents));
116
+ if (!result.success) {
117
+ console.error(`Invalid custom type model: ${modelPath.href}`);
118
+ process.exitCode = 1;
119
+ return;
120
+ }
121
+ model = result.output as CustomType;
122
+ } catch (error) {
123
+ if (error instanceof Error && "code" in error && error.code === "ENOENT") {
124
+ console.error(`Custom type not found: ${typeId}\n`);
125
+ console.error(`Create it first with: prismic custom-type create ${typeId}`);
126
+ process.exitCode = 1;
127
+ return;
128
+ }
129
+ if (error instanceof Error) {
130
+ console.error(`Failed to read custom type: ${error.message}`);
131
+ } else {
132
+ console.error("Failed to read custom type");
133
+ }
134
+ process.exitCode = 1;
135
+ return;
136
+ }
137
+
138
+ // Determine target tab
139
+ const existingTabs = Object.keys(model.json);
140
+ const targetTab = tab ?? existingTabs[0] ?? "Main";
141
+
142
+ // Initialize tab if it doesn't exist
143
+ if (!model.json[targetTab]) {
144
+ model.json[targetTab] = {};
145
+ }
146
+
147
+ // Build field definition
148
+ const fieldDefinition: BooleanField = {
149
+ type: "Boolean",
150
+ config: {
151
+ label: label ?? humanReadable(fieldPath.type === "nested" ? fieldPath.nestedFieldId : fieldId),
152
+ ...(defaultValue && { default_value: true }),
153
+ ...(trueLabel && { placeholder_true: trueLabel }),
154
+ ...(falseLabel && { placeholder_false: falseLabel }),
155
+ },
156
+ };
157
+
158
+ // Add field to model
159
+ if (fieldPath.type === "nested") {
160
+ const groupResult = findGroupInTab(model.json[targetTab], fieldPath.groupId, targetTab);
161
+ if (!groupResult.ok) {
162
+ console.error(groupResult.error);
163
+ process.exitCode = 1;
164
+ return;
165
+ }
166
+ // Check if field already exists in the group
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
+ // Check if field already exists in any tab
175
+ for (const [tabName, tabFields] of Object.entries(model.json)) {
176
+ if (tabFields[fieldId]) {
177
+ console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
178
+ process.exitCode = 1;
179
+ return;
180
+ }
181
+ // Also check inside groups
182
+ for (const [groupFieldId, groupField] of Object.entries(tabFields)) {
183
+ if (isGroupField(groupField) && groupField.config.fields[fieldId]) {
184
+ console.error(`Field "${fieldId}" already exists in group "${groupFieldId}" in tab "${tabName}"`);
185
+ process.exitCode = 1;
186
+ return;
187
+ }
188
+ }
189
+ }
190
+ model.json[targetTab][fieldId] = fieldDefinition;
191
+ }
192
+
193
+ // Write updated model
194
+ try {
195
+ await writeFile(modelPath, stringify(model));
196
+ } catch (error) {
197
+ if (error instanceof Error) {
198
+ console.error(`Failed to update custom type: ${error.message}`);
199
+ } else {
200
+ console.error("Failed to update custom type");
201
+ }
202
+ process.exitCode = 1;
203
+ return;
204
+ }
205
+
206
+ if (fieldPath.type === "nested") {
207
+ console.info(`Added field "${fieldPath.nestedFieldId}" (Boolean) to group "${fieldPath.groupId}" in ${typeId}`);
208
+ } else {
209
+ console.info(`Added field "${fieldId}" (Boolean) to "${targetTab}" tab in ${typeId}`);
210
+ }
211
+
212
+ try {
213
+ await buildTypes({ output: types });
214
+ console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
215
+ } catch (error) {
216
+ console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
217
+ }
218
+
219
+ console.info();
220
+ console.info("Next: Add more fields with `prismic custom-type add-field`");
221
+ console.info(" Run `prismic status` when done to find next steps");
222
+ }
@@ -0,0 +1,205 @@
1
+ import type { Color, CustomType } 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 color picker field to an existing custom type.
15
+
16
+ USAGE
17
+ prismic custom-type add-field color <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
+ --types string Output file for generated types (default: "prismicio-types.d.ts")
28
+ -h, --help Show help for command
29
+
30
+ EXAMPLES
31
+ prismic custom-type add-field color homepage bg_color
32
+ prismic custom-type add-field color homepage accent --tab "Design"
33
+ prismic custom-type add-field color homepage text_color --label "Text Color"
34
+ `.trim();
35
+
36
+ const CustomTypeSchema = v.object({
37
+ id: v.string(),
38
+ label: v.string(),
39
+ repeatable: v.boolean(),
40
+ status: v.boolean(),
41
+ format: v.string(),
42
+ json: v.record(v.string(), v.record(v.string(), v.unknown())),
43
+ });
44
+
45
+ export async function customTypeAddFieldColor(): Promise<void> {
46
+ const {
47
+ values: { help, tab, label, placeholder, types },
48
+ positionals: [typeId, fieldId],
49
+ } = parseArgs({
50
+ args: process.argv.slice(5), // skip: node, script, "custom-type", "add-field", "color"
51
+ options: {
52
+ tab: { type: "string", short: "t" },
53
+ label: { type: "string", short: "l" },
54
+ placeholder: { type: "string", short: "p" },
55
+ types: { type: "string" },
56
+ help: { type: "boolean", short: "h" },
57
+ },
58
+ allowPositionals: true,
59
+ });
60
+
61
+ if (help) {
62
+ console.info(HELP);
63
+ return;
64
+ }
65
+
66
+ if (!typeId) {
67
+ console.error("Missing required argument: type-id\n");
68
+ console.error("Usage: prismic custom-type add-field color <type-id> <field-id>");
69
+ process.exitCode = 1;
70
+ return;
71
+ }
72
+
73
+ if (!fieldId) {
74
+ console.error("Missing required argument: field-id\n");
75
+ console.error("Usage: prismic custom-type add-field color <type-id> <field-id>");
76
+ process.exitCode = 1;
77
+ return;
78
+ }
79
+
80
+ // Parse and validate field path
81
+ const fieldPath = parseFieldPath(fieldId);
82
+ const pathValidation = validateNestedFieldPath(fieldPath);
83
+ if (!pathValidation.ok) {
84
+ console.error(pathValidation.error);
85
+ process.exitCode = 1;
86
+ return;
87
+ }
88
+
89
+ // Find the custom type file
90
+ const projectRoot = await findUpward("package.json");
91
+ if (!projectRoot) {
92
+ console.error("Could not find project root (no package.json found)");
93
+ process.exitCode = 1;
94
+ return;
95
+ }
96
+
97
+ const modelPath = new URL(`customtypes/${typeId}/index.json`, projectRoot);
98
+
99
+ // Read and parse the model
100
+ let model: CustomType;
101
+ try {
102
+ const contents = await readFile(modelPath, "utf8");
103
+ const result = v.safeParse(CustomTypeSchema, JSON.parse(contents));
104
+ if (!result.success) {
105
+ console.error(`Invalid custom type model: ${modelPath.href}`);
106
+ process.exitCode = 1;
107
+ return;
108
+ }
109
+ model = result.output as CustomType;
110
+ } catch (error) {
111
+ if (error instanceof Error && "code" in error && error.code === "ENOENT") {
112
+ console.error(`Custom type not found: ${typeId}\n`);
113
+ console.error(`Create it first with: prismic custom-type create ${typeId}`);
114
+ process.exitCode = 1;
115
+ return;
116
+ }
117
+ if (error instanceof Error) {
118
+ console.error(`Failed to read custom type: ${error.message}`);
119
+ } else {
120
+ console.error("Failed to read custom type");
121
+ }
122
+ process.exitCode = 1;
123
+ return;
124
+ }
125
+
126
+ // Determine target tab
127
+ const existingTabs = Object.keys(model.json);
128
+ const targetTab = tab ?? existingTabs[0] ?? "Main";
129
+
130
+ // Initialize tab if it doesn't exist
131
+ if (!model.json[targetTab]) {
132
+ model.json[targetTab] = {};
133
+ }
134
+
135
+ // Build field definition
136
+ const fieldDefinition: Color = {
137
+ type: "Color",
138
+ config: {
139
+ label: label ?? humanReadable(fieldPath.type === "nested" ? fieldPath.nestedFieldId : fieldId),
140
+ ...(placeholder && { placeholder }),
141
+ },
142
+ };
143
+
144
+ // Add field to model
145
+ if (fieldPath.type === "nested") {
146
+ const groupResult = findGroupInTab(model.json[targetTab], fieldPath.groupId, targetTab);
147
+ if (!groupResult.ok) {
148
+ console.error(groupResult.error);
149
+ process.exitCode = 1;
150
+ return;
151
+ }
152
+ if (groupResult.group.config.fields[fieldPath.nestedFieldId]) {
153
+ console.error(`Field "${fieldPath.nestedFieldId}" already exists in group "${fieldPath.groupId}"`);
154
+ process.exitCode = 1;
155
+ return;
156
+ }
157
+ groupResult.group.config.fields[fieldPath.nestedFieldId] = fieldDefinition;
158
+ } else {
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
+ for (const [groupFieldId, groupField] of Object.entries(tabFields)) {
166
+ if (isGroupField(groupField) && groupField.config.fields[fieldId]) {
167
+ console.error(`Field "${fieldId}" already exists in group "${groupFieldId}" in tab "${tabName}"`);
168
+ process.exitCode = 1;
169
+ return;
170
+ }
171
+ }
172
+ }
173
+ model.json[targetTab][fieldId] = fieldDefinition;
174
+ }
175
+
176
+ // Write updated model
177
+ try {
178
+ await writeFile(modelPath, stringify(model));
179
+ } catch (error) {
180
+ if (error instanceof Error) {
181
+ console.error(`Failed to update custom type: ${error.message}`);
182
+ } else {
183
+ console.error("Failed to update custom type");
184
+ }
185
+ process.exitCode = 1;
186
+ return;
187
+ }
188
+
189
+ if (fieldPath.type === "nested") {
190
+ console.info(`Added field "${fieldPath.nestedFieldId}" (Color) to group "${fieldPath.groupId}" in ${typeId}`);
191
+ } else {
192
+ console.info(`Added field "${fieldId}" (Color) to "${targetTab}" tab in ${typeId}`);
193
+ }
194
+
195
+ try {
196
+ await buildTypes({ output: types });
197
+ console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
198
+ } catch (error) {
199
+ console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
200
+ }
201
+
202
+ console.info();
203
+ console.info("Next: Add more fields with `prismic custom-type add-field`");
204
+ console.info(" Run `prismic status` when done to find next steps");
205
+ }