@angeloashmore/prismic-cli-poc 0.0.0-canary.2ff9563

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 (119) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +98 -0
  3. package/dist/index.mjs +1996 -0
  4. package/package.json +52 -0
  5. package/src/custom-type-add-field-boolean.ts +171 -0
  6. package/src/custom-type-add-field-color.ts +158 -0
  7. package/src/custom-type-add-field-date.ts +161 -0
  8. package/src/custom-type-add-field-embed.ts +158 -0
  9. package/src/custom-type-add-field-geo-point.ts +155 -0
  10. package/src/custom-type-add-field-image.ts +158 -0
  11. package/src/custom-type-add-field-key-text.ts +158 -0
  12. package/src/custom-type-add-field-link.ts +180 -0
  13. package/src/custom-type-add-field-number.ts +190 -0
  14. package/src/custom-type-add-field-rich-text.ts +181 -0
  15. package/src/custom-type-add-field-select.ts +164 -0
  16. package/src/custom-type-add-field-timestamp.ts +161 -0
  17. package/src/custom-type-add-field-uid.ts +158 -0
  18. package/src/custom-type-add-field.ts +111 -0
  19. package/src/custom-type-connect-slice.ts +221 -0
  20. package/src/custom-type-create.ts +92 -0
  21. package/src/custom-type-disconnect-slice.ts +179 -0
  22. package/src/custom-type-list.ts +110 -0
  23. package/src/custom-type-remove-field.ts +161 -0
  24. package/src/custom-type-remove.ts +126 -0
  25. package/src/custom-type-set-name.ts +128 -0
  26. package/src/custom-type-view.ts +118 -0
  27. package/src/custom-type.ts +85 -0
  28. package/src/index.ts +100 -0
  29. package/src/init.ts +62 -0
  30. package/src/lib/auth.ts +60 -0
  31. package/src/lib/config.ts +111 -0
  32. package/src/lib/file.ts +49 -0
  33. package/src/lib/json.ts +3 -0
  34. package/src/lib/request.ts +116 -0
  35. package/src/lib/slice.ts +112 -0
  36. package/src/lib/url.ts +25 -0
  37. package/src/locale-add.ts +116 -0
  38. package/src/locale-list.ts +107 -0
  39. package/src/locale-remove.ts +88 -0
  40. package/src/locale-set-default.ts +131 -0
  41. package/src/locale.ts +60 -0
  42. package/src/login.ts +143 -0
  43. package/src/logout.ts +36 -0
  44. package/src/page-type-add-field-boolean.ts +171 -0
  45. package/src/page-type-add-field-color.ts +158 -0
  46. package/src/page-type-add-field-date.ts +161 -0
  47. package/src/page-type-add-field-embed.ts +158 -0
  48. package/src/page-type-add-field-geo-point.ts +155 -0
  49. package/src/page-type-add-field-image.ts +158 -0
  50. package/src/page-type-add-field-key-text.ts +158 -0
  51. package/src/page-type-add-field-link.ts +180 -0
  52. package/src/page-type-add-field-number.ts +190 -0
  53. package/src/page-type-add-field-rich-text.ts +181 -0
  54. package/src/page-type-add-field-select.ts +164 -0
  55. package/src/page-type-add-field-timestamp.ts +161 -0
  56. package/src/page-type-add-field-uid.ts +158 -0
  57. package/src/page-type-add-field.ts +111 -0
  58. package/src/page-type-connect-slice.ts +221 -0
  59. package/src/page-type-create.ts +93 -0
  60. package/src/page-type-disconnect-slice.ts +179 -0
  61. package/src/page-type-list.ts +109 -0
  62. package/src/page-type-remove-field.ts +161 -0
  63. package/src/page-type-remove.ts +126 -0
  64. package/src/page-type-set-name.ts +128 -0
  65. package/src/page-type-set-repeatable.ts +137 -0
  66. package/src/page-type-view.ts +118 -0
  67. package/src/page-type.ts +90 -0
  68. package/src/preview-add.ts +126 -0
  69. package/src/preview-list.ts +106 -0
  70. package/src/preview-remove.ts +109 -0
  71. package/src/preview-set-name.ts +137 -0
  72. package/src/preview.ts +60 -0
  73. package/src/repo-create.ts +136 -0
  74. package/src/repo-list.ts +100 -0
  75. package/src/repo-set-name.ts +102 -0
  76. package/src/repo-view.ts +113 -0
  77. package/src/repo.ts +60 -0
  78. package/src/slice-add-field-boolean.ts +150 -0
  79. package/src/slice-add-field-color.ts +137 -0
  80. package/src/slice-add-field-date.ts +137 -0
  81. package/src/slice-add-field-embed.ts +137 -0
  82. package/src/slice-add-field-geo-point.ts +134 -0
  83. package/src/slice-add-field-image.ts +134 -0
  84. package/src/slice-add-field-key-text.ts +137 -0
  85. package/src/slice-add-field-link.ts +155 -0
  86. package/src/slice-add-field-number.ts +137 -0
  87. package/src/slice-add-field-rich-text.ts +160 -0
  88. package/src/slice-add-field-select.ts +143 -0
  89. package/src/slice-add-field-timestamp.ts +137 -0
  90. package/src/slice-add-field.ts +106 -0
  91. package/src/slice-add-variation.ts +137 -0
  92. package/src/slice-create.ts +129 -0
  93. package/src/slice-list-variations.ts +67 -0
  94. package/src/slice-list.ts +88 -0
  95. package/src/slice-remove-field.ts +117 -0
  96. package/src/slice-remove-variation.ts +108 -0
  97. package/src/slice-remove.ts +81 -0
  98. package/src/slice-rename.ts +112 -0
  99. package/src/slice-view.ts +77 -0
  100. package/src/slice.ts +90 -0
  101. package/src/sync.ts +309 -0
  102. package/src/token-create.ts +185 -0
  103. package/src/token-delete.ts +161 -0
  104. package/src/token-list.ts +212 -0
  105. package/src/token-set-name.ts +165 -0
  106. package/src/token.ts +60 -0
  107. package/src/webhook-add-header.ts +118 -0
  108. package/src/webhook-create.ts +152 -0
  109. package/src/webhook-disable.ts +109 -0
  110. package/src/webhook-enable.ts +132 -0
  111. package/src/webhook-list.ts +93 -0
  112. package/src/webhook-remove-header.ts +117 -0
  113. package/src/webhook-remove.ts +106 -0
  114. package/src/webhook-set-triggers.ts +148 -0
  115. package/src/webhook-status.ts +90 -0
  116. package/src/webhook-test.ts +106 -0
  117. package/src/webhook-view.ts +147 -0
  118. package/src/webhook.ts +95 -0
  119. package/src/whoami.ts +62 -0
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@angeloashmore/prismic-cli-poc",
3
+ "version": "0.0.0-canary.2ff9563",
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
+ "tsdown": "0.19.0",
45
+ "typescript": "5.9.3",
46
+ "valibot": "^1.2.0",
47
+ "vitest": "4.0.17"
48
+ },
49
+ "engines": {
50
+ "node": ">=24"
51
+ }
52
+ }
@@ -0,0 +1,171 @@
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 { findUpward } from "./lib/file";
8
+ import { stringify } from "./lib/json";
9
+
10
+ const HELP = `
11
+ Add a boolean (toggle) field to an existing custom type.
12
+
13
+ USAGE
14
+ prismic custom-type add-field boolean <type-id> <field-id> [flags]
15
+
16
+ ARGUMENTS
17
+ type-id Custom 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
23
+ --default Set default value to true
24
+ --true-label string Label shown when toggle is on
25
+ --false-label string Label shown when toggle is off
26
+ -h, --help Show help for command
27
+
28
+ EXAMPLES
29
+ prismic custom-type add-field boolean homepage featured
30
+ prismic custom-type add-field boolean article published --default
31
+ prismic custom-type add-field boolean product available --true-label "In Stock" --false-label "Out of Stock"
32
+ `.trim();
33
+
34
+ const CustomTypeSchema = v.object({
35
+ id: v.string(),
36
+ label: v.string(),
37
+ repeatable: v.boolean(),
38
+ status: v.boolean(),
39
+ format: v.string(),
40
+ json: v.record(v.string(), v.record(v.string(), v.unknown())),
41
+ });
42
+
43
+ export async function customTypeAddFieldBoolean(): Promise<void> {
44
+ const {
45
+ values: {
46
+ help,
47
+ tab,
48
+ label,
49
+ default: defaultValue,
50
+ "true-label": trueLabel,
51
+ "false-label": falseLabel,
52
+ },
53
+ positionals: [typeId, fieldId],
54
+ } = parseArgs({
55
+ args: process.argv.slice(5), // skip: node, script, "custom-type", "add-field", "boolean"
56
+ options: {
57
+ tab: { type: "string", short: "t" },
58
+ label: { type: "string", short: "l" },
59
+ default: { type: "boolean" },
60
+ "true-label": { type: "string" },
61
+ "false-label": { 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 boolean <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 boolean <type-id> <field-id>");
82
+ process.exitCode = 1;
83
+ return;
84
+ }
85
+
86
+ // Find the custom type file
87
+ const projectRoot = await findUpward("package.json");
88
+ if (!projectRoot) {
89
+ console.error("Could not find project root (no package.json found)");
90
+ process.exitCode = 1;
91
+ return;
92
+ }
93
+
94
+ const modelPath = new URL(`customtypes/${typeId}/index.json`, projectRoot);
95
+
96
+ // Read and parse the model
97
+ let model: CustomType;
98
+ try {
99
+ const contents = await readFile(modelPath, "utf8");
100
+ const result = v.safeParse(CustomTypeSchema, JSON.parse(contents));
101
+ if (!result.success) {
102
+ console.error(`Invalid custom type model: ${modelPath.href}`);
103
+ process.exitCode = 1;
104
+ return;
105
+ }
106
+ model = result.output as CustomType;
107
+ } catch (error) {
108
+ if (error instanceof Error && "code" in error && error.code === "ENOENT") {
109
+ console.error(`Custom type not found: ${typeId}\n`);
110
+ console.error(`Create it first with: prismic custom-type create ${typeId}`);
111
+ process.exitCode = 1;
112
+ return;
113
+ }
114
+ if (error instanceof Error) {
115
+ console.error(`Failed to read custom type: ${error.message}`);
116
+ } else {
117
+ console.error("Failed to read custom type");
118
+ }
119
+ process.exitCode = 1;
120
+ return;
121
+ }
122
+
123
+ // Determine target tab
124
+ const existingTabs = Object.keys(model.json);
125
+ const targetTab = tab ?? existingTabs[0] ?? "Main";
126
+
127
+ // Initialize tab if it doesn't exist
128
+ if (!model.json[targetTab]) {
129
+ model.json[targetTab] = {};
130
+ }
131
+
132
+ // Check if field already exists in any tab
133
+ for (const [tabName, tabFields] of Object.entries(model.json)) {
134
+ if (tabFields[fieldId]) {
135
+ console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
136
+ process.exitCode = 1;
137
+ return;
138
+ }
139
+ }
140
+
141
+ // Build field definition
142
+ const fieldDefinition: BooleanField = {
143
+ type: "Boolean",
144
+ config: {
145
+ ...(label && { label }),
146
+ ...(defaultValue && { default_value: true }),
147
+ ...(trueLabel && { placeholder_true: trueLabel }),
148
+ ...(falseLabel && { placeholder_false: falseLabel }),
149
+ },
150
+ };
151
+
152
+ // Add field to model
153
+ model.json[targetTab][fieldId] = fieldDefinition;
154
+
155
+ // Write updated model
156
+ try {
157
+ await writeFile(modelPath, stringify(model));
158
+ } catch (error) {
159
+ if (error instanceof Error) {
160
+ console.error(`Failed to update custom type: ${error.message}`);
161
+ } else {
162
+ console.error("Failed to update custom type");
163
+ }
164
+ process.exitCode = 1;
165
+ return;
166
+ }
167
+
168
+ console.info(
169
+ `Added field "${fieldId}" (Boolean) to "${targetTab}" tab in ${typeId}`,
170
+ );
171
+ }
@@ -0,0 +1,158 @@
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 { findUpward } from "./lib/file";
8
+ import { stringify } from "./lib/json";
9
+
10
+ const HELP = `
11
+ Add a color picker field to an existing custom type.
12
+
13
+ USAGE
14
+ prismic custom-type add-field color <type-id> <field-id> [flags]
15
+
16
+ ARGUMENTS
17
+ type-id Custom 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
23
+ -p, --placeholder string Placeholder text
24
+ -h, --help Show help for command
25
+
26
+ EXAMPLES
27
+ prismic custom-type add-field color homepage bg_color
28
+ prismic custom-type add-field color homepage accent --tab "Design"
29
+ prismic custom-type add-field color homepage text_color --label "Text Color"
30
+ `.trim();
31
+
32
+ const CustomTypeSchema = v.object({
33
+ id: v.string(),
34
+ label: v.string(),
35
+ repeatable: v.boolean(),
36
+ status: v.boolean(),
37
+ format: v.string(),
38
+ json: v.record(v.string(), v.record(v.string(), v.unknown())),
39
+ });
40
+
41
+ export async function customTypeAddFieldColor(): Promise<void> {
42
+ const {
43
+ values: { help, tab, label, placeholder },
44
+ positionals: [typeId, fieldId],
45
+ } = parseArgs({
46
+ args: process.argv.slice(5), // skip: node, script, "custom-type", "add-field", "color"
47
+ options: {
48
+ tab: { type: "string", short: "t" },
49
+ label: { type: "string", short: "l" },
50
+ placeholder: { type: "string", short: "p" },
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 custom-type add-field color <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 custom-type add-field color <type-id> <field-id>");
71
+ process.exitCode = 1;
72
+ return;
73
+ }
74
+
75
+ // Find the custom type file
76
+ const projectRoot = await findUpward("package.json");
77
+ if (!projectRoot) {
78
+ console.error("Could not find project root (no package.json found)");
79
+ process.exitCode = 1;
80
+ return;
81
+ }
82
+
83
+ const modelPath = new URL(`customtypes/${typeId}/index.json`, projectRoot);
84
+
85
+ // Read and parse the model
86
+ let model: CustomType;
87
+ try {
88
+ const contents = await readFile(modelPath, "utf8");
89
+ const result = v.safeParse(CustomTypeSchema, JSON.parse(contents));
90
+ if (!result.success) {
91
+ console.error(`Invalid custom type model: ${modelPath.href}`);
92
+ process.exitCode = 1;
93
+ return;
94
+ }
95
+ model = result.output as CustomType;
96
+ } catch (error) {
97
+ if (error instanceof Error && "code" in error && error.code === "ENOENT") {
98
+ console.error(`Custom type not found: ${typeId}\n`);
99
+ console.error(`Create it first with: prismic custom-type create ${typeId}`);
100
+ process.exitCode = 1;
101
+ return;
102
+ }
103
+ if (error instanceof Error) {
104
+ console.error(`Failed to read custom type: ${error.message}`);
105
+ } else {
106
+ console.error("Failed to read custom type");
107
+ }
108
+ process.exitCode = 1;
109
+ return;
110
+ }
111
+
112
+ // Determine target tab
113
+ const existingTabs = Object.keys(model.json);
114
+ const targetTab = tab ?? existingTabs[0] ?? "Main";
115
+
116
+ // Initialize tab if it doesn't exist
117
+ if (!model.json[targetTab]) {
118
+ model.json[targetTab] = {};
119
+ }
120
+
121
+ // Check if field already exists in any tab
122
+ for (const [tabName, tabFields] of Object.entries(model.json)) {
123
+ if (tabFields[fieldId]) {
124
+ console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
125
+ process.exitCode = 1;
126
+ return;
127
+ }
128
+ }
129
+
130
+ // Build field definition
131
+ const fieldDefinition: Color = {
132
+ type: "Color",
133
+ config: {
134
+ ...(label && { label }),
135
+ ...(placeholder && { placeholder }),
136
+ },
137
+ };
138
+
139
+ // Add field to model
140
+ model.json[targetTab][fieldId] = fieldDefinition;
141
+
142
+ // Write updated model
143
+ try {
144
+ await writeFile(modelPath, stringify(model));
145
+ } catch (error) {
146
+ if (error instanceof Error) {
147
+ console.error(`Failed to update custom type: ${error.message}`);
148
+ } else {
149
+ console.error("Failed to update custom type");
150
+ }
151
+ process.exitCode = 1;
152
+ return;
153
+ }
154
+
155
+ console.info(
156
+ `Added field "${fieldId}" (Color) to "${targetTab}" tab in ${typeId}`,
157
+ );
158
+ }
@@ -0,0 +1,161 @@
1
+ import type { CustomType, Date as DateField } 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 { findUpward } from "./lib/file";
8
+ import { stringify } from "./lib/json";
9
+
10
+ const HELP = `
11
+ Add a date field to an existing custom type.
12
+
13
+ USAGE
14
+ prismic custom-type add-field date <type-id> <field-id> [flags]
15
+
16
+ ARGUMENTS
17
+ type-id Custom 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
23
+ -p, --placeholder string Placeholder text
24
+ --default string Default date value (YYYY-MM-DD format)
25
+ -h, --help Show help for command
26
+
27
+ EXAMPLES
28
+ prismic custom-type add-field date homepage publish_date
29
+ prismic custom-type add-field date event start_date --tab "Schedule"
30
+ prismic custom-type add-field date article date --label "Publication Date" --default "2024-01-01"
31
+ `.trim();
32
+
33
+ const CustomTypeSchema = v.object({
34
+ id: v.string(),
35
+ label: v.string(),
36
+ repeatable: v.boolean(),
37
+ status: v.boolean(),
38
+ format: v.string(),
39
+ json: v.record(v.string(), v.record(v.string(), v.unknown())),
40
+ });
41
+
42
+ export async function customTypeAddFieldDate(): Promise<void> {
43
+ const {
44
+ values: { help, tab, label, placeholder, default: defaultValue },
45
+ positionals: [typeId, fieldId],
46
+ } = parseArgs({
47
+ args: process.argv.slice(5), // skip: node, script, "custom-type", "add-field", "date"
48
+ options: {
49
+ tab: { type: "string", short: "t" },
50
+ label: { type: "string", short: "l" },
51
+ placeholder: { type: "string", short: "p" },
52
+ default: { type: "string" },
53
+ help: { type: "boolean", short: "h" },
54
+ },
55
+ allowPositionals: true,
56
+ });
57
+
58
+ if (help) {
59
+ console.info(HELP);
60
+ return;
61
+ }
62
+
63
+ if (!typeId) {
64
+ console.error("Missing required argument: type-id\n");
65
+ console.error("Usage: prismic custom-type add-field date <type-id> <field-id>");
66
+ process.exitCode = 1;
67
+ return;
68
+ }
69
+
70
+ if (!fieldId) {
71
+ console.error("Missing required argument: field-id\n");
72
+ console.error("Usage: prismic custom-type add-field date <type-id> <field-id>");
73
+ process.exitCode = 1;
74
+ return;
75
+ }
76
+
77
+ // Find the custom type file
78
+ const projectRoot = await findUpward("package.json");
79
+ if (!projectRoot) {
80
+ console.error("Could not find project root (no package.json found)");
81
+ process.exitCode = 1;
82
+ return;
83
+ }
84
+
85
+ const modelPath = new URL(`customtypes/${typeId}/index.json`, projectRoot);
86
+
87
+ // Read and parse the model
88
+ let model: CustomType;
89
+ try {
90
+ const contents = await readFile(modelPath, "utf8");
91
+ const result = v.safeParse(CustomTypeSchema, JSON.parse(contents));
92
+ if (!result.success) {
93
+ console.error(`Invalid custom type model: ${modelPath.href}`);
94
+ process.exitCode = 1;
95
+ return;
96
+ }
97
+ model = result.output as CustomType;
98
+ } catch (error) {
99
+ if (error instanceof Error && "code" in error && error.code === "ENOENT") {
100
+ console.error(`Custom type not found: ${typeId}\n`);
101
+ console.error(`Create it first with: prismic custom-type create ${typeId}`);
102
+ process.exitCode = 1;
103
+ return;
104
+ }
105
+ if (error instanceof Error) {
106
+ console.error(`Failed to read custom type: ${error.message}`);
107
+ } else {
108
+ console.error("Failed to read custom type");
109
+ }
110
+ process.exitCode = 1;
111
+ return;
112
+ }
113
+
114
+ // Determine target tab
115
+ const existingTabs = Object.keys(model.json);
116
+ const targetTab = tab ?? existingTabs[0] ?? "Main";
117
+
118
+ // Initialize tab if it doesn't exist
119
+ if (!model.json[targetTab]) {
120
+ model.json[targetTab] = {};
121
+ }
122
+
123
+ // Check if field already exists in any tab
124
+ for (const [tabName, tabFields] of Object.entries(model.json)) {
125
+ if (tabFields[fieldId]) {
126
+ console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
127
+ process.exitCode = 1;
128
+ return;
129
+ }
130
+ }
131
+
132
+ // Build field definition
133
+ const fieldDefinition: DateField = {
134
+ type: "Date",
135
+ config: {
136
+ ...(label && { label }),
137
+ ...(placeholder && { placeholder }),
138
+ ...(defaultValue && { default: defaultValue }),
139
+ },
140
+ };
141
+
142
+ // Add field to model
143
+ model.json[targetTab][fieldId] = fieldDefinition;
144
+
145
+ // Write updated model
146
+ try {
147
+ await writeFile(modelPath, stringify(model));
148
+ } catch (error) {
149
+ if (error instanceof Error) {
150
+ console.error(`Failed to update custom type: ${error.message}`);
151
+ } else {
152
+ console.error("Failed to update custom type");
153
+ }
154
+ process.exitCode = 1;
155
+ return;
156
+ }
157
+
158
+ console.info(
159
+ `Added field "${fieldId}" (Date) to "${targetTab}" tab in ${typeId}`,
160
+ );
161
+ }
@@ -0,0 +1,158 @@
1
+ import type { CustomType, Embed } 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 { findUpward } from "./lib/file";
8
+ import { stringify } from "./lib/json";
9
+
10
+ const HELP = `
11
+ Add an embed field to an existing custom type.
12
+
13
+ USAGE
14
+ prismic custom-type add-field embed <type-id> <field-id> [flags]
15
+
16
+ ARGUMENTS
17
+ type-id Custom 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
23
+ -p, --placeholder string Placeholder text
24
+ -h, --help Show help for command
25
+
26
+ EXAMPLES
27
+ prismic custom-type add-field embed homepage video
28
+ prismic custom-type add-field embed homepage youtube --tab "Media"
29
+ prismic custom-type add-field embed homepage media --label "Media Embed"
30
+ `.trim();
31
+
32
+ const CustomTypeSchema = v.object({
33
+ id: v.string(),
34
+ label: v.string(),
35
+ repeatable: v.boolean(),
36
+ status: v.boolean(),
37
+ format: v.string(),
38
+ json: v.record(v.string(), v.record(v.string(), v.unknown())),
39
+ });
40
+
41
+ export async function customTypeAddFieldEmbed(): Promise<void> {
42
+ const {
43
+ values: { help, tab, label, placeholder },
44
+ positionals: [typeId, fieldId],
45
+ } = parseArgs({
46
+ args: process.argv.slice(5), // skip: node, script, "custom-type", "add-field", "embed"
47
+ options: {
48
+ tab: { type: "string", short: "t" },
49
+ label: { type: "string", short: "l" },
50
+ placeholder: { type: "string", short: "p" },
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 custom-type add-field embed <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 custom-type add-field embed <type-id> <field-id>");
71
+ process.exitCode = 1;
72
+ return;
73
+ }
74
+
75
+ // Find the custom type file
76
+ const projectRoot = await findUpward("package.json");
77
+ if (!projectRoot) {
78
+ console.error("Could not find project root (no package.json found)");
79
+ process.exitCode = 1;
80
+ return;
81
+ }
82
+
83
+ const modelPath = new URL(`customtypes/${typeId}/index.json`, projectRoot);
84
+
85
+ // Read and parse the model
86
+ let model: CustomType;
87
+ try {
88
+ const contents = await readFile(modelPath, "utf8");
89
+ const result = v.safeParse(CustomTypeSchema, JSON.parse(contents));
90
+ if (!result.success) {
91
+ console.error(`Invalid custom type model: ${modelPath.href}`);
92
+ process.exitCode = 1;
93
+ return;
94
+ }
95
+ model = result.output as CustomType;
96
+ } catch (error) {
97
+ if (error instanceof Error && "code" in error && error.code === "ENOENT") {
98
+ console.error(`Custom type not found: ${typeId}\n`);
99
+ console.error(`Create it first with: prismic custom-type create ${typeId}`);
100
+ process.exitCode = 1;
101
+ return;
102
+ }
103
+ if (error instanceof Error) {
104
+ console.error(`Failed to read custom type: ${error.message}`);
105
+ } else {
106
+ console.error("Failed to read custom type");
107
+ }
108
+ process.exitCode = 1;
109
+ return;
110
+ }
111
+
112
+ // Determine target tab
113
+ const existingTabs = Object.keys(model.json);
114
+ const targetTab = tab ?? existingTabs[0] ?? "Main";
115
+
116
+ // Initialize tab if it doesn't exist
117
+ if (!model.json[targetTab]) {
118
+ model.json[targetTab] = {};
119
+ }
120
+
121
+ // Check if field already exists in any tab
122
+ for (const [tabName, tabFields] of Object.entries(model.json)) {
123
+ if (tabFields[fieldId]) {
124
+ console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
125
+ process.exitCode = 1;
126
+ return;
127
+ }
128
+ }
129
+
130
+ // Build field definition
131
+ const fieldDefinition: Embed = {
132
+ type: "Embed",
133
+ config: {
134
+ ...(label && { label }),
135
+ ...(placeholder && { placeholder }),
136
+ },
137
+ };
138
+
139
+ // Add field to model
140
+ model.json[targetTab][fieldId] = fieldDefinition;
141
+
142
+ // Write updated model
143
+ try {
144
+ await writeFile(modelPath, stringify(model));
145
+ } catch (error) {
146
+ if (error instanceof Error) {
147
+ console.error(`Failed to update custom type: ${error.message}`);
148
+ } else {
149
+ console.error("Failed to update custom type");
150
+ }
151
+ process.exitCode = 1;
152
+ return;
153
+ }
154
+
155
+ console.info(
156
+ `Added field "${fieldId}" (Embed) to "${targetTab}" tab in ${typeId}`,
157
+ );
158
+ }