@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,177 @@
1
+ import type { CustomType, UID } 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 UID (unique identifier) field to an existing custom type.
14
+
15
+ USAGE
16
+ prismic custom-type add-field uid <type-id> <field-id> [flags]
17
+
18
+ ARGUMENTS
19
+ type-id Custom 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
+ --types string Output file for generated types (default: "prismicio-types.d.ts")
29
+ --no-types Skip type generation
30
+ -h, --help Show help for command
31
+
32
+ EXAMPLES
33
+ prismic custom-type add-field uid page uid
34
+ prismic custom-type add-field uid article slug --label "URL Slug"
35
+ prismic custom-type add-field uid product sku --placeholder "Enter unique SKU"
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 customTypeAddFieldUid(): Promise<void> {
48
+ const {
49
+ values: { help, tab, label, placeholder, types, "no-types": noTypes },
50
+ positionals: [typeId, fieldId],
51
+ } = parseArgs({
52
+ args: process.argv.slice(5), // skip: node, script, "custom-type", "add-field", "uid"
53
+ options: {
54
+ tab: { type: "string", short: "t" },
55
+ label: { type: "string", short: "l" },
56
+ placeholder: { type: "string", short: "p" },
57
+ types: { type: "string" },
58
+ "no-types": { type: "boolean" },
59
+ help: { type: "boolean", short: "h" },
60
+ },
61
+ allowPositionals: true,
62
+ });
63
+
64
+ if (help) {
65
+ console.info(HELP);
66
+ return;
67
+ }
68
+
69
+ if (!typeId) {
70
+ console.error("Missing required argument: type-id\n");
71
+ console.error("Usage: prismic custom-type add-field uid <type-id> <field-id>");
72
+ process.exitCode = 1;
73
+ return;
74
+ }
75
+
76
+ if (!fieldId) {
77
+ console.error("Missing required argument: field-id\n");
78
+ console.error("Usage: prismic custom-type add-field uid <type-id> <field-id>");
79
+ process.exitCode = 1;
80
+ return;
81
+ }
82
+
83
+ // Find the custom type file
84
+ const projectRoot = await findUpward("package.json");
85
+ if (!projectRoot) {
86
+ console.error("Could not find project root (no package.json found)");
87
+ process.exitCode = 1;
88
+ return;
89
+ }
90
+
91
+ const modelPath = new URL(`customtypes/${typeId}/index.json`, projectRoot);
92
+
93
+ // Read and parse the model
94
+ let model: CustomType;
95
+ try {
96
+ const contents = await readFile(modelPath, "utf8");
97
+ const result = v.safeParse(CustomTypeSchema, JSON.parse(contents));
98
+ if (!result.success) {
99
+ console.error(`Invalid custom type model: ${modelPath.href}`);
100
+ process.exitCode = 1;
101
+ return;
102
+ }
103
+ model = result.output as CustomType;
104
+ } catch (error) {
105
+ if (error instanceof Error && "code" in error && error.code === "ENOENT") {
106
+ console.error(`Custom type not found: ${typeId}\n`);
107
+ console.error(`Create it first with: prismic custom-type create ${typeId}`);
108
+ process.exitCode = 1;
109
+ return;
110
+ }
111
+ if (error instanceof Error) {
112
+ console.error(`Failed to read custom type: ${error.message}`);
113
+ } else {
114
+ console.error("Failed to read custom type");
115
+ }
116
+ process.exitCode = 1;
117
+ return;
118
+ }
119
+
120
+ // Determine target tab
121
+ const existingTabs = Object.keys(model.json);
122
+ const targetTab = tab ?? existingTabs[0] ?? "Main";
123
+
124
+ // Initialize tab if it doesn't exist
125
+ if (!model.json[targetTab]) {
126
+ model.json[targetTab] = {};
127
+ }
128
+
129
+ // Check if field already exists in any tab
130
+ for (const [tabName, tabFields] of Object.entries(model.json)) {
131
+ if (tabFields[fieldId]) {
132
+ console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
133
+ process.exitCode = 1;
134
+ return;
135
+ }
136
+ }
137
+
138
+ // Build field definition
139
+ const fieldDefinition: UID = {
140
+ type: "UID",
141
+ config: {
142
+ label: label ?? humanReadable(fieldId),
143
+ ...(placeholder && { placeholder }),
144
+ },
145
+ };
146
+
147
+ // Add field to model
148
+ model.json[targetTab][fieldId] = fieldDefinition;
149
+
150
+ // Write updated model
151
+ try {
152
+ await writeFile(modelPath, stringify(model));
153
+ } catch (error) {
154
+ if (error instanceof Error) {
155
+ console.error(`Failed to update custom type: ${error.message}`);
156
+ } else {
157
+ console.error("Failed to update custom type");
158
+ }
159
+ process.exitCode = 1;
160
+ return;
161
+ }
162
+
163
+ console.info(`Added field "${fieldId}" (UID) to "${targetTab}" tab in ${typeId}`);
164
+
165
+ if (!noTypes) {
166
+ try {
167
+ await buildTypes({ output: types });
168
+ console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
169
+ } catch (error) {
170
+ console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
171
+ }
172
+ }
173
+
174
+ console.info();
175
+ console.info("Next: Add more fields with `prismic custom-type add-field`");
176
+ console.info(" Run `prismic status` when done to find next steps");
177
+ }
@@ -0,0 +1,111 @@
1
+ import { parseArgs } from "node:util";
2
+
3
+ import { customTypeAddFieldBoolean } from "./custom-type-add-field-boolean";
4
+ import { customTypeAddFieldColor } from "./custom-type-add-field-color";
5
+ import { customTypeAddFieldDate } from "./custom-type-add-field-date";
6
+ import { customTypeAddFieldEmbed } from "./custom-type-add-field-embed";
7
+ import { customTypeAddFieldGeoPoint } from "./custom-type-add-field-geo-point";
8
+ import { customTypeAddFieldImage } from "./custom-type-add-field-image";
9
+ import { customTypeAddFieldKeyText } from "./custom-type-add-field-key-text";
10
+ import { customTypeAddFieldLink } from "./custom-type-add-field-link";
11
+ import { customTypeAddFieldNumber } from "./custom-type-add-field-number";
12
+ import { customTypeAddFieldRichText } from "./custom-type-add-field-rich-text";
13
+ import { customTypeAddFieldSelect } from "./custom-type-add-field-select";
14
+ import { customTypeAddFieldTimestamp } from "./custom-type-add-field-timestamp";
15
+ import { customTypeAddFieldUid } from "./custom-type-add-field-uid";
16
+
17
+ const HELP = `
18
+ Add a field to an existing custom type.
19
+
20
+ USAGE
21
+ prismic custom-type add-field <field-type> <type-id> <field-id> [flags]
22
+
23
+ FIELD TYPES
24
+ boolean Boolean toggle
25
+ color Color picker
26
+ date Date picker
27
+ embed Embed (oEmbed)
28
+ geo-point Geographic coordinates
29
+ image Image
30
+ key-text Single-line text
31
+ link Any link type
32
+ number Number
33
+ rich-text Rich text editor
34
+ select Dropdown select
35
+ timestamp Date and time
36
+ uid Unique identifier
37
+
38
+ FLAGS
39
+ -h, --help Show help for command
40
+
41
+ LEARN MORE
42
+ Use \`prismic custom-type add-field <field-type> --help\` for more information.
43
+
44
+ EXAMPLES
45
+ prismic custom-type add-field key-text homepage meta_title --tab "SEO"
46
+ prismic custom-type add-field link homepage button --allow-text
47
+ prismic custom-type add-field rich-text homepage body --multi "paragraph,heading2,strong,em"
48
+ prismic custom-type add-field select homepage layout --option "full" --option "sidebar"
49
+ `.trim();
50
+
51
+ export async function customTypeAddField(): Promise<void> {
52
+ const {
53
+ positionals: [fieldType],
54
+ } = parseArgs({
55
+ args: process.argv.slice(4), // skip: node, script, "custom-type", "add-field"
56
+ options: {
57
+ help: { type: "boolean", short: "h" },
58
+ },
59
+ allowPositionals: true,
60
+ strict: false,
61
+ });
62
+
63
+ switch (fieldType) {
64
+ case "boolean":
65
+ await customTypeAddFieldBoolean();
66
+ break;
67
+ case "color":
68
+ await customTypeAddFieldColor();
69
+ break;
70
+ case "date":
71
+ await customTypeAddFieldDate();
72
+ break;
73
+ case "embed":
74
+ await customTypeAddFieldEmbed();
75
+ break;
76
+ case "geo-point":
77
+ await customTypeAddFieldGeoPoint();
78
+ break;
79
+ case "image":
80
+ await customTypeAddFieldImage();
81
+ break;
82
+ case "key-text":
83
+ await customTypeAddFieldKeyText();
84
+ break;
85
+ case "link":
86
+ await customTypeAddFieldLink();
87
+ break;
88
+ case "number":
89
+ await customTypeAddFieldNumber();
90
+ break;
91
+ case "rich-text":
92
+ await customTypeAddFieldRichText();
93
+ break;
94
+ case "select":
95
+ await customTypeAddFieldSelect();
96
+ break;
97
+ case "timestamp":
98
+ await customTypeAddFieldTimestamp();
99
+ break;
100
+ case "uid":
101
+ await customTypeAddFieldUid();
102
+ break;
103
+ default: {
104
+ if (fieldType) {
105
+ console.error(`Unknown field type: ${fieldType}\n`);
106
+ process.exitCode = 1;
107
+ }
108
+ console.info(HELP);
109
+ }
110
+ }
111
+ }
@@ -0,0 +1,220 @@
1
+ import type {
2
+ CustomType,
3
+ DynamicSlices,
4
+ SharedSliceRef,
5
+ } from "@prismicio/types-internal/lib/customtypes";
6
+
7
+ import { readFile, writeFile } from "node:fs/promises";
8
+ import { parseArgs } from "node:util";
9
+ import * as v from "valibot";
10
+
11
+ import { buildTypes } from "./codegen-types";
12
+ import { findUpward } from "./lib/file";
13
+ import { stringify } from "./lib/json";
14
+ import { findSliceModel } from "./lib/slice";
15
+
16
+ const HELP = `
17
+ Connect a shared slice to a custom type's slice zone.
18
+
19
+ USAGE
20
+ prismic custom-type connect-slice <type-id> <slice-id> [flags]
21
+
22
+ ARGUMENTS
23
+ type-id Custom type identifier (required)
24
+ slice-id Slice identifier (required)
25
+
26
+ Types are generated by default after changes. Use --no-types to skip.
27
+
28
+ FLAGS
29
+ -z, --slice-zone string Target slice zone field ID (default: "slices")
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 custom-type connect-slice homepage CallToAction
36
+ prismic custom-type connect-slice homepage CallToAction --slice-zone slices
37
+ prismic custom-type connect-slice article HeroSection -z body
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 customTypeConnectSlice(): Promise<void> {
50
+ const {
51
+ values: { help, "slice-zone": sliceZoneId, types, "no-types": noTypes },
52
+ positionals: [typeId, sliceId],
53
+ } = parseArgs({
54
+ args: process.argv.slice(4), // skip: node, script, "custom-type", "connect-slice"
55
+ options: {
56
+ "slice-zone": { type: "string", short: "z" },
57
+ types: { type: "string" },
58
+ "no-types": { type: "boolean" },
59
+ help: { type: "boolean", short: "h" },
60
+ },
61
+ allowPositionals: true,
62
+ });
63
+
64
+ if (help) {
65
+ console.info(HELP);
66
+ return;
67
+ }
68
+
69
+ if (!typeId) {
70
+ console.error("Missing required argument: type-id\n");
71
+ console.error("Usage: prismic custom-type connect-slice <type-id> <slice-id>");
72
+ process.exitCode = 1;
73
+ return;
74
+ }
75
+
76
+ if (!sliceId) {
77
+ console.error("Missing required argument: slice-id\n");
78
+ console.error("Usage: prismic custom-type connect-slice <type-id> <slice-id>");
79
+ process.exitCode = 1;
80
+ return;
81
+ }
82
+
83
+ // Verify the slice exists
84
+ const sliceResult = await findSliceModel(sliceId);
85
+ if (!sliceResult.ok) {
86
+ console.error(sliceResult.error);
87
+ process.exitCode = 1;
88
+ return;
89
+ }
90
+
91
+ // Find the custom type file
92
+ const projectRoot = await findUpward("package.json");
93
+ if (!projectRoot) {
94
+ console.error("Could not find project root (no package.json found)");
95
+ process.exitCode = 1;
96
+ return;
97
+ }
98
+
99
+ const modelPath = new URL(`customtypes/${typeId}/index.json`, projectRoot);
100
+
101
+ // Read and parse the model
102
+ let model: CustomType;
103
+ try {
104
+ const contents = await readFile(modelPath, "utf8");
105
+ const result = v.safeParse(CustomTypeSchema, JSON.parse(contents));
106
+ if (!result.success) {
107
+ console.error(`Invalid custom type model: ${modelPath.href}`);
108
+ process.exitCode = 1;
109
+ return;
110
+ }
111
+ model = result.output as CustomType;
112
+ } catch (error) {
113
+ if (error instanceof Error && "code" in error && error.code === "ENOENT") {
114
+ console.error(`Custom type not found: ${typeId}\n`);
115
+ console.error(`Create it first with: prismic custom-type create ${typeId}`);
116
+ process.exitCode = 1;
117
+ return;
118
+ }
119
+ if (error instanceof Error) {
120
+ console.error(`Failed to read custom type: ${error.message}`);
121
+ } else {
122
+ console.error("Failed to read custom type");
123
+ }
124
+ process.exitCode = 1;
125
+ return;
126
+ }
127
+
128
+ const targetSliceZoneId = sliceZoneId ?? "slices";
129
+
130
+ // Find existing slice zone or create a new one
131
+ let sliceZone: DynamicSlices | undefined;
132
+ let sliceZoneFieldId: string | undefined;
133
+
134
+ // Search all tabs for a Slices field matching the target ID
135
+ for (const [, tabFields] of Object.entries(model.json)) {
136
+ for (const [fieldId, field] of Object.entries(tabFields)) {
137
+ if ((field as { type?: string }).type === "Slices" && fieldId === targetSliceZoneId) {
138
+ sliceZone = field as DynamicSlices;
139
+ sliceZoneFieldId = fieldId;
140
+ break;
141
+ }
142
+ }
143
+ if (sliceZone) break;
144
+ }
145
+
146
+ // Handle slice zone not found
147
+ if (!sliceZone) {
148
+ if (sliceZoneId) {
149
+ // User specified a slice zone that doesn't exist
150
+ console.error(`Slice zone "${sliceZoneId}" not found in custom type "${typeId}"`);
151
+ process.exitCode = 1;
152
+ return;
153
+ }
154
+
155
+ // Create a new slice zone in the first tab
156
+ const existingTabs = Object.keys(model.json);
157
+ const targetTab = existingTabs[0] ?? "Main";
158
+
159
+ // Initialize tab if it doesn't exist
160
+ if (!model.json[targetTab]) {
161
+ model.json[targetTab] = {};
162
+ }
163
+
164
+ const newSliceZone: DynamicSlices = {
165
+ type: "Slices",
166
+ fieldset: "Slice Zone",
167
+ config: {
168
+ choices: {},
169
+ },
170
+ };
171
+
172
+ model.json[targetTab][targetSliceZoneId] = newSliceZone;
173
+ sliceZone = newSliceZone;
174
+ sliceZoneFieldId = targetSliceZoneId;
175
+ }
176
+
177
+ // Ensure config and choices exist
178
+ if (!sliceZone.config) {
179
+ sliceZone.config = { choices: {} };
180
+ }
181
+ if (!sliceZone.config.choices) {
182
+ sliceZone.config.choices = {};
183
+ }
184
+
185
+ // Check if slice is already connected
186
+ if (sliceId in sliceZone.config.choices) {
187
+ console.info(
188
+ `Slice "${sliceId}" is already connected to slice zone "${sliceZoneFieldId}" in ${typeId}`,
189
+ );
190
+ return;
191
+ }
192
+
193
+ // Add the slice reference
194
+ const sliceRef: SharedSliceRef = { type: "SharedSlice" };
195
+ sliceZone.config.choices[sliceId] = sliceRef;
196
+
197
+ // Write updated model
198
+ try {
199
+ await writeFile(modelPath, stringify(model));
200
+ } catch (error) {
201
+ if (error instanceof Error) {
202
+ console.error(`Failed to update custom type: ${error.message}`);
203
+ } else {
204
+ console.error("Failed to update custom type");
205
+ }
206
+ process.exitCode = 1;
207
+ return;
208
+ }
209
+
210
+ console.info(`Connected slice "${sliceId}" to slice zone "${sliceZoneFieldId}" in ${typeId}`);
211
+
212
+ if (!noTypes) {
213
+ try {
214
+ await buildTypes({ output: types });
215
+ console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
216
+ } catch (error) {
217
+ console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
218
+ }
219
+ }
220
+ }
@@ -0,0 +1,118 @@
1
+ import type { CustomType } from "@prismicio/types-internal/lib/customtypes";
2
+
3
+ import { mkdir, writeFile } from "node:fs/promises";
4
+ import { parseArgs } from "node:util";
5
+
6
+ import { buildTypes } from "./codegen-types";
7
+ import { findUpward } from "./lib/file";
8
+ import { stringify } from "./lib/json";
9
+
10
+ const HELP = `
11
+ Create a new custom type in a Prismic repository.
12
+
13
+ USAGE
14
+ prismic custom-type create <id> [flags]
15
+
16
+ ARGUMENTS
17
+ id Custom type identifier (required)
18
+
19
+ Types are generated by default after changes. Use --no-types to skip.
20
+
21
+ FLAGS
22
+ -n, --name string Display name for the custom type
23
+ --single Create as a singleton (non-repeatable) type
24
+ --types string Output file for generated types (default: "prismicio-types.d.ts")
25
+ --no-types Skip type generation
26
+ -h, --help Show help for command
27
+
28
+ LEARN MORE
29
+ Use \`prismic custom-type <command> --help\` for more information about a command.
30
+ `.trim();
31
+
32
+ export async function customTypeCreate(): Promise<void> {
33
+ const {
34
+ values: { help, name, single, types, "no-types": noTypes },
35
+ positionals: [id],
36
+ } = parseArgs({
37
+ args: process.argv.slice(4), // skip: node, script, "custom-type", "create"
38
+ options: {
39
+ name: { type: "string", short: "n" },
40
+ single: { type: "boolean" },
41
+ types: { type: "string" },
42
+ "no-types": { type: "boolean" },
43
+ help: { type: "boolean", short: "h" },
44
+ },
45
+ allowPositionals: true,
46
+ });
47
+
48
+ if (help) {
49
+ console.info(HELP);
50
+ return;
51
+ }
52
+
53
+ if (!id) {
54
+ console.error("Missing required argument: id");
55
+ process.exitCode = 1;
56
+ return;
57
+ }
58
+
59
+ const model = {
60
+ id,
61
+ label: name ?? pascalCase(id),
62
+ repeatable: !single,
63
+ status: true,
64
+ format: "custom",
65
+ json: {
66
+ Main: single
67
+ ? {}
68
+ : {
69
+ uid: {
70
+ type: "UID",
71
+ config: { label: "UID", placeholder: "" },
72
+ },
73
+ },
74
+ },
75
+ } satisfies CustomType;
76
+
77
+ const projectRoot = await findUpward("package.json");
78
+ if (!projectRoot) {
79
+ console.error("Could not find project root (no package.json found)");
80
+ process.exitCode = 1;
81
+ return;
82
+ }
83
+
84
+ const customTypesDirectory = new URL("customtypes/", projectRoot);
85
+ const typeDirectory = new URL(id + "/", customTypesDirectory);
86
+ const modelPath = new URL("index.json", typeDirectory);
87
+
88
+ try {
89
+ await mkdir(new URL(".", modelPath), { recursive: true });
90
+ await writeFile(modelPath, stringify(model));
91
+ } catch (error) {
92
+ if (error instanceof Error) {
93
+ console.error(`Failed to create custom type: ${error.message}`);
94
+ } else {
95
+ console.error(`Failed to create custom type`);
96
+ }
97
+ process.exitCode = 1;
98
+ return;
99
+ }
100
+
101
+ console.info(`Created custom type at ${modelPath.href}`);
102
+
103
+ if (!noTypes) {
104
+ try {
105
+ await buildTypes({ output: types });
106
+ console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
107
+ } catch (error) {
108
+ console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
109
+ }
110
+ }
111
+
112
+ console.info();
113
+ console.info("Next: Add fields with `prismic custom-type add-field`");
114
+ }
115
+
116
+ export function pascalCase(input: string): string {
117
+ return input.toLowerCase().replace(/(^|[-_\s]+)(.)?/g, (_, __, c) => c?.toUpperCase() ?? "");
118
+ }