@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.
- package/LICENSE +202 -0
- package/README.md +98 -0
- package/dist/index.mjs +2508 -0
- package/package.json +53 -0
- package/src/codegen-types.ts +82 -0
- package/src/codegen.ts +45 -0
- package/src/custom-type-add-field-boolean.ts +222 -0
- package/src/custom-type-add-field-color.ts +205 -0
- package/src/custom-type-add-field-date.ts +208 -0
- package/src/custom-type-add-field-embed.ts +205 -0
- package/src/custom-type-add-field-geo-point.ts +202 -0
- package/src/custom-type-add-field-group.ts +179 -0
- package/src/custom-type-add-field-image.ts +205 -0
- package/src/custom-type-add-field-key-text.ts +205 -0
- package/src/custom-type-add-field-link.ts +228 -0
- package/src/custom-type-add-field-number.ts +237 -0
- package/src/custom-type-add-field-rich-text.ts +229 -0
- package/src/custom-type-add-field-select.ts +211 -0
- package/src/custom-type-add-field-timestamp.ts +208 -0
- package/src/custom-type-add-field-uid.ts +188 -0
- package/src/custom-type-add-field.ts +116 -0
- package/src/custom-type-connect-slice.ts +214 -0
- package/src/custom-type-create.ts +112 -0
- package/src/custom-type-disconnect-slice.ts +171 -0
- package/src/custom-type-list.ts +110 -0
- package/src/custom-type-remove-field.ts +171 -0
- package/src/custom-type-remove.ts +138 -0
- package/src/custom-type-set-name.ts +138 -0
- package/src/custom-type-view.ts +118 -0
- package/src/custom-type.ts +85 -0
- package/src/docs-fetch.ts +146 -0
- package/src/docs-list.ts +131 -0
- package/src/docs.ts +54 -0
- package/src/index.ts +132 -0
- package/src/init.ts +64 -0
- package/src/lib/auth.ts +83 -0
- package/src/lib/config.ts +111 -0
- package/src/lib/custom-types-api.ts +438 -0
- package/src/lib/field-path.ts +81 -0
- package/src/lib/file.ts +49 -0
- package/src/lib/framework.ts +143 -0
- package/src/lib/json.ts +3 -0
- package/src/lib/request.ts +116 -0
- package/src/lib/slice.ts +115 -0
- package/src/lib/string.ts +6 -0
- package/src/lib/url.ts +25 -0
- package/src/locale-add.ts +116 -0
- package/src/locale-list.ts +107 -0
- package/src/locale-remove.ts +88 -0
- package/src/locale-set-default.ts +131 -0
- package/src/locale.ts +60 -0
- package/src/login.ts +152 -0
- package/src/logout.ts +36 -0
- package/src/page-type-add-field-boolean.ts +238 -0
- package/src/page-type-add-field-color.ts +224 -0
- package/src/page-type-add-field-date.ts +227 -0
- package/src/page-type-add-field-embed.ts +224 -0
- package/src/page-type-add-field-geo-point.ts +221 -0
- package/src/page-type-add-field-group.ts +198 -0
- package/src/page-type-add-field-image.ts +224 -0
- package/src/page-type-add-field-key-text.ts +224 -0
- package/src/page-type-add-field-link.ts +247 -0
- package/src/page-type-add-field-number.ts +256 -0
- package/src/page-type-add-field-rich-text.ts +248 -0
- package/src/page-type-add-field-select.ts +230 -0
- package/src/page-type-add-field-timestamp.ts +227 -0
- package/src/page-type-add-field-uid.ts +207 -0
- package/src/page-type-add-field.ts +116 -0
- package/src/page-type-connect-slice.ts +214 -0
- package/src/page-type-create.ts +161 -0
- package/src/page-type-disconnect-slice.ts +171 -0
- package/src/page-type-list.ts +109 -0
- package/src/page-type-remove-field.ts +171 -0
- package/src/page-type-remove.ts +138 -0
- package/src/page-type-set-name.ts +138 -0
- package/src/page-type-set-repeatable.ts +147 -0
- package/src/page-type-view.ts +118 -0
- package/src/page-type.ts +90 -0
- package/src/preview-add.ts +126 -0
- package/src/preview-get-simulator.ts +104 -0
- package/src/preview-list.ts +106 -0
- package/src/preview-remove-simulator.ts +80 -0
- package/src/preview-remove.ts +109 -0
- package/src/preview-set-name.ts +137 -0
- package/src/preview-set-simulator.ts +116 -0
- package/src/preview.ts +75 -0
- package/src/pull.ts +242 -0
- package/src/push.ts +405 -0
- package/src/repo-create.ts +195 -0
- package/src/repo-get-access.ts +86 -0
- package/src/repo-list.ts +100 -0
- package/src/repo-set-access.ts +100 -0
- package/src/repo-set-name.ts +102 -0
- package/src/repo-view.ts +113 -0
- package/src/repo.ts +70 -0
- package/src/slice-add-field-boolean.ts +240 -0
- package/src/slice-add-field-color.ts +226 -0
- package/src/slice-add-field-date.ts +226 -0
- package/src/slice-add-field-embed.ts +226 -0
- package/src/slice-add-field-geo-point.ts +223 -0
- package/src/slice-add-field-group.ts +191 -0
- package/src/slice-add-field-image.ts +223 -0
- package/src/slice-add-field-key-text.ts +226 -0
- package/src/slice-add-field-link.ts +245 -0
- package/src/slice-add-field-number.ts +226 -0
- package/src/slice-add-field-rich-text.ts +250 -0
- package/src/slice-add-field-select.ts +232 -0
- package/src/slice-add-field-timestamp.ts +226 -0
- package/src/slice-add-field.ts +111 -0
- package/src/slice-add-variation.ts +139 -0
- package/src/slice-create.ts +203 -0
- package/src/slice-list-variations.ts +67 -0
- package/src/slice-list.ts +88 -0
- package/src/slice-remove-field.ts +122 -0
- package/src/slice-remove-variation.ts +112 -0
- package/src/slice-remove.ts +91 -0
- package/src/slice-rename.ts +122 -0
- package/src/slice-set-screenshot.ts +235 -0
- package/src/slice-view.ts +80 -0
- package/src/slice.ts +95 -0
- package/src/status.ts +873 -0
- package/src/token-create.ts +203 -0
- package/src/token-delete.ts +182 -0
- package/src/token-list.ts +223 -0
- package/src/token-set-name.ts +193 -0
- package/src/token.ts +60 -0
- package/src/webhook-add-header.ts +118 -0
- package/src/webhook-create.ts +152 -0
- package/src/webhook-disable.ts +109 -0
- package/src/webhook-enable.ts +132 -0
- package/src/webhook-list.ts +93 -0
- package/src/webhook-remove-header.ts +117 -0
- package/src/webhook-remove.ts +106 -0
- package/src/webhook-set-triggers.ts +148 -0
- package/src/webhook-status.ts +90 -0
- package/src/webhook-test.ts +106 -0
- package/src/webhook-view.ts +147 -0
- package/src/webhook.ts +95 -0
- package/src/whoami.ts +62 -0
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import type { CustomType, Number as NumberField } from "@prismicio/types-internal/lib/customtypes";
|
|
2
|
+
|
|
3
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
4
|
+
import { parseArgs } from "node:util";
|
|
5
|
+
import * as v from "valibot";
|
|
6
|
+
|
|
7
|
+
import { buildTypes } from "./codegen-types";
|
|
8
|
+
import { findUpward } from "./lib/file";
|
|
9
|
+
import { findGroupInTab, isGroupField, parseFieldPath, validateNestedFieldPath } from "./lib/field-path";
|
|
10
|
+
import { type Framework, detectFrameworkInfo } from "./lib/framework";
|
|
11
|
+
import { stringify } from "./lib/json";
|
|
12
|
+
import { humanReadable } from "./lib/string";
|
|
13
|
+
|
|
14
|
+
const HELP = `
|
|
15
|
+
Add a number field to an existing page type.
|
|
16
|
+
|
|
17
|
+
USAGE
|
|
18
|
+
prismic page-type add-field number <type-id> <field-id> [flags]
|
|
19
|
+
|
|
20
|
+
ARGUMENTS
|
|
21
|
+
type-id Page type identifier (required)
|
|
22
|
+
field-id Field identifier (required)
|
|
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
|
+
-h, --help Show help for command
|
|
33
|
+
|
|
34
|
+
EXAMPLES
|
|
35
|
+
prismic page-type add-field number homepage price
|
|
36
|
+
prismic page-type add-field number product quantity --min 0 --max 100
|
|
37
|
+
prismic page-type add-field number settings rating --min 1 --max 5 --step 1
|
|
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
|
+
function getDocsPath(framework: Framework): string {
|
|
50
|
+
switch (framework) {
|
|
51
|
+
case "next":
|
|
52
|
+
return "nextjs/with-cli";
|
|
53
|
+
case "nuxt":
|
|
54
|
+
return "nuxt/with-cli";
|
|
55
|
+
case "sveltekit":
|
|
56
|
+
return "sveltekit/with-cli";
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export async function pageTypeAddFieldNumber(): Promise<void> {
|
|
61
|
+
const {
|
|
62
|
+
values: { help, tab, label, placeholder, min, max, step, types },
|
|
63
|
+
positionals: [typeId, fieldId],
|
|
64
|
+
} = parseArgs({
|
|
65
|
+
args: process.argv.slice(5), // skip: node, script, "page-type", "add-field", "number"
|
|
66
|
+
options: {
|
|
67
|
+
tab: { type: "string", short: "t" },
|
|
68
|
+
label: { type: "string", short: "l" },
|
|
69
|
+
placeholder: { type: "string", short: "p" },
|
|
70
|
+
min: { type: "string" },
|
|
71
|
+
max: { type: "string" },
|
|
72
|
+
step: { type: "string" },
|
|
73
|
+
types: { type: "string" },
|
|
74
|
+
help: { type: "boolean", short: "h" },
|
|
75
|
+
},
|
|
76
|
+
allowPositionals: true,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
if (help) {
|
|
80
|
+
console.info(HELP);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (!typeId) {
|
|
85
|
+
console.error("Missing required argument: type-id\n");
|
|
86
|
+
console.error("Usage: prismic page-type add-field number <type-id> <field-id>");
|
|
87
|
+
process.exitCode = 1;
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (!fieldId) {
|
|
92
|
+
console.error("Missing required argument: field-id\n");
|
|
93
|
+
console.error("Usage: prismic page-type add-field number <type-id> <field-id>");
|
|
94
|
+
process.exitCode = 1;
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Parse and validate field path
|
|
99
|
+
const fieldPath = parseFieldPath(fieldId);
|
|
100
|
+
const pathValidation = validateNestedFieldPath(fieldPath);
|
|
101
|
+
if (!pathValidation.ok) {
|
|
102
|
+
console.error(pathValidation.error);
|
|
103
|
+
process.exitCode = 1;
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Parse numeric values
|
|
108
|
+
const minValue = min !== undefined ? Number(min) : undefined;
|
|
109
|
+
const maxValue = max !== undefined ? Number(max) : undefined;
|
|
110
|
+
const stepValue = step !== undefined ? Number(step) : undefined;
|
|
111
|
+
|
|
112
|
+
if (min !== undefined && Number.isNaN(minValue)) {
|
|
113
|
+
console.error("Invalid --min value: must be a number");
|
|
114
|
+
process.exitCode = 1;
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (max !== undefined && Number.isNaN(maxValue)) {
|
|
119
|
+
console.error("Invalid --max value: must be a number");
|
|
120
|
+
process.exitCode = 1;
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (step !== undefined && Number.isNaN(stepValue)) {
|
|
125
|
+
console.error("Invalid --step value: must be a number");
|
|
126
|
+
process.exitCode = 1;
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Find the page type file
|
|
131
|
+
const projectRoot = await findUpward("package.json");
|
|
132
|
+
if (!projectRoot) {
|
|
133
|
+
console.error("Could not find project root (no package.json found)");
|
|
134
|
+
process.exitCode = 1;
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const modelPath = new URL(`customtypes/${typeId}/index.json`, projectRoot);
|
|
139
|
+
|
|
140
|
+
// Read and parse the model
|
|
141
|
+
let model: CustomType;
|
|
142
|
+
try {
|
|
143
|
+
const contents = await readFile(modelPath, "utf8");
|
|
144
|
+
const result = v.safeParse(CustomTypeSchema, JSON.parse(contents));
|
|
145
|
+
if (!result.success) {
|
|
146
|
+
console.error(`Invalid page type model: ${modelPath.href}`);
|
|
147
|
+
process.exitCode = 1;
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
model = result.output as CustomType;
|
|
151
|
+
} catch (error) {
|
|
152
|
+
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
|
|
153
|
+
console.error(`Page type not found: ${typeId}\n`);
|
|
154
|
+
console.error(`Create it first with: prismic page-type create ${typeId}`);
|
|
155
|
+
process.exitCode = 1;
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
if (error instanceof Error) {
|
|
159
|
+
console.error(`Failed to read page type: ${error.message}`);
|
|
160
|
+
} else {
|
|
161
|
+
console.error("Failed to read page type");
|
|
162
|
+
}
|
|
163
|
+
process.exitCode = 1;
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Determine target tab
|
|
168
|
+
const existingTabs = Object.keys(model.json);
|
|
169
|
+
const targetTab = tab ?? existingTabs[0] ?? "Main";
|
|
170
|
+
|
|
171
|
+
// Initialize tab if it doesn't exist
|
|
172
|
+
if (!model.json[targetTab]) {
|
|
173
|
+
model.json[targetTab] = {};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Build field definition
|
|
177
|
+
const fieldDefinition: NumberField = {
|
|
178
|
+
type: "Number",
|
|
179
|
+
config: {
|
|
180
|
+
label: label ?? humanReadable(fieldPath.type === "nested" ? fieldPath.nestedFieldId : fieldId),
|
|
181
|
+
...(placeholder && { placeholder }),
|
|
182
|
+
...(minValue !== undefined && { min: minValue }),
|
|
183
|
+
...(maxValue !== undefined && { max: maxValue }),
|
|
184
|
+
...(stepValue !== undefined && { step: stepValue }),
|
|
185
|
+
},
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
// Add field to model
|
|
189
|
+
if (fieldPath.type === "nested") {
|
|
190
|
+
const groupResult = findGroupInTab(model.json[targetTab], fieldPath.groupId, targetTab);
|
|
191
|
+
if (!groupResult.ok) {
|
|
192
|
+
console.error(groupResult.error);
|
|
193
|
+
process.exitCode = 1;
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
if (groupResult.group.config.fields[fieldPath.nestedFieldId]) {
|
|
197
|
+
console.error(`Field "${fieldPath.nestedFieldId}" already exists in group "${fieldPath.groupId}"`);
|
|
198
|
+
process.exitCode = 1;
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
groupResult.group.config.fields[fieldPath.nestedFieldId] = fieldDefinition;
|
|
202
|
+
} else {
|
|
203
|
+
for (const [tabName, tabFields] of Object.entries(model.json)) {
|
|
204
|
+
if (tabFields[fieldId]) {
|
|
205
|
+
console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
|
|
206
|
+
process.exitCode = 1;
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
for (const [groupFieldId, groupField] of Object.entries(tabFields)) {
|
|
210
|
+
if (isGroupField(groupField) && groupField.config.fields[fieldId]) {
|
|
211
|
+
console.error(`Field "${fieldId}" already exists in group "${groupFieldId}" in tab "${tabName}"`);
|
|
212
|
+
process.exitCode = 1;
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
model.json[targetTab][fieldId] = fieldDefinition;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Write updated model
|
|
221
|
+
try {
|
|
222
|
+
await writeFile(modelPath, stringify(model));
|
|
223
|
+
} catch (error) {
|
|
224
|
+
if (error instanceof Error) {
|
|
225
|
+
console.error(`Failed to update page type: ${error.message}`);
|
|
226
|
+
} else {
|
|
227
|
+
console.error("Failed to update page type");
|
|
228
|
+
}
|
|
229
|
+
process.exitCode = 1;
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (fieldPath.type === "nested") {
|
|
234
|
+
console.info(`Added field "${fieldPath.nestedFieldId}" (Number) to group "${fieldPath.groupId}" in ${typeId}`);
|
|
235
|
+
} else {
|
|
236
|
+
console.info(`Added field "${fieldId}" (Number) to "${targetTab}" tab in ${typeId}`);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
try {
|
|
240
|
+
await buildTypes({ output: types });
|
|
241
|
+
console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
|
|
242
|
+
} catch (error) {
|
|
243
|
+
console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
console.info();
|
|
247
|
+
console.info("Next: Add more fields with `prismic page-type add-field`");
|
|
248
|
+
|
|
249
|
+
const frameworkInfo = await detectFrameworkInfo();
|
|
250
|
+
if (frameworkInfo?.framework) {
|
|
251
|
+
const docsPath = getDocsPath(frameworkInfo.framework);
|
|
252
|
+
console.info(
|
|
253
|
+
` Run \`prismic docs fetch ${docsPath}#write-page-components\` to learn how to implement a page file`,
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import type { CustomType, RichText } from "@prismicio/types-internal/lib/customtypes";
|
|
2
|
+
|
|
3
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
4
|
+
import { parseArgs } from "node:util";
|
|
5
|
+
import * as v from "valibot";
|
|
6
|
+
|
|
7
|
+
import { buildTypes } from "./codegen-types";
|
|
8
|
+
import { findUpward } from "./lib/file";
|
|
9
|
+
import { findGroupInTab, isGroupField, parseFieldPath, validateNestedFieldPath } from "./lib/field-path";
|
|
10
|
+
import { type Framework, detectFrameworkInfo } from "./lib/framework";
|
|
11
|
+
import { stringify } from "./lib/json";
|
|
12
|
+
import { humanReadable } from "./lib/string";
|
|
13
|
+
|
|
14
|
+
const HELP = `
|
|
15
|
+
Add a rich text field to an existing page type.
|
|
16
|
+
|
|
17
|
+
USAGE
|
|
18
|
+
prismic page-type add-field rich-text <type-id> <field-id> [flags]
|
|
19
|
+
|
|
20
|
+
ARGUMENTS
|
|
21
|
+
type-id Page type identifier (required)
|
|
22
|
+
field-id Field identifier (required)
|
|
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
|
+
-h, --help Show help for command
|
|
33
|
+
|
|
34
|
+
BLOCK TYPES
|
|
35
|
+
heading1, heading2, heading3, heading4, heading5, heading6,
|
|
36
|
+
paragraph, strong, em, preformatted, hyperlink, image, embed,
|
|
37
|
+
list-item, o-list-item, rtl
|
|
38
|
+
|
|
39
|
+
EXAMPLES
|
|
40
|
+
prismic page-type add-field rich-text homepage body
|
|
41
|
+
prismic page-type add-field rich-text article content --multi "paragraph,heading2,heading3,strong,em,hyperlink"
|
|
42
|
+
prismic page-type add-field rich-text page tagline --single "heading1"
|
|
43
|
+
prismic page-type add-field rich-text blog post --multi "paragraph,strong,em,hyperlink" --allow-target-blank
|
|
44
|
+
`.trim();
|
|
45
|
+
|
|
46
|
+
const CustomTypeSchema = v.object({
|
|
47
|
+
id: v.string(),
|
|
48
|
+
label: v.string(),
|
|
49
|
+
repeatable: v.boolean(),
|
|
50
|
+
status: v.boolean(),
|
|
51
|
+
format: v.string(),
|
|
52
|
+
json: v.record(v.string(), v.record(v.string(), v.unknown())),
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
function getDocsPath(framework: Framework): string {
|
|
56
|
+
switch (framework) {
|
|
57
|
+
case "next":
|
|
58
|
+
return "nextjs/with-cli";
|
|
59
|
+
case "nuxt":
|
|
60
|
+
return "nuxt/with-cli";
|
|
61
|
+
case "sveltekit":
|
|
62
|
+
return "sveltekit/with-cli";
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export async function pageTypeAddFieldRichText(): Promise<void> {
|
|
67
|
+
const {
|
|
68
|
+
values: {
|
|
69
|
+
help,
|
|
70
|
+
tab,
|
|
71
|
+
label,
|
|
72
|
+
placeholder,
|
|
73
|
+
single,
|
|
74
|
+
multi,
|
|
75
|
+
"allow-target-blank": allowTargetBlank,
|
|
76
|
+
types,
|
|
77
|
+
},
|
|
78
|
+
positionals: [typeId, fieldId],
|
|
79
|
+
} = parseArgs({
|
|
80
|
+
args: process.argv.slice(5), // skip: node, script, "page-type", "add-field", "rich-text"
|
|
81
|
+
options: {
|
|
82
|
+
tab: { type: "string", short: "t" },
|
|
83
|
+
label: { type: "string", short: "l" },
|
|
84
|
+
placeholder: { type: "string", short: "p" },
|
|
85
|
+
single: { type: "string" },
|
|
86
|
+
multi: { type: "string" },
|
|
87
|
+
"allow-target-blank": { type: "boolean" },
|
|
88
|
+
types: { type: "string" },
|
|
89
|
+
help: { type: "boolean", short: "h" },
|
|
90
|
+
},
|
|
91
|
+
allowPositionals: true,
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
if (help) {
|
|
95
|
+
console.info(HELP);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (!typeId) {
|
|
100
|
+
console.error("Missing required argument: type-id\n");
|
|
101
|
+
console.error("Usage: prismic page-type add-field rich-text <type-id> <field-id>");
|
|
102
|
+
process.exitCode = 1;
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (!fieldId) {
|
|
107
|
+
console.error("Missing required argument: field-id\n");
|
|
108
|
+
console.error("Usage: prismic page-type add-field rich-text <type-id> <field-id>");
|
|
109
|
+
process.exitCode = 1;
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Parse and validate field path
|
|
114
|
+
const fieldPath = parseFieldPath(fieldId);
|
|
115
|
+
const pathValidation = validateNestedFieldPath(fieldPath);
|
|
116
|
+
if (!pathValidation.ok) {
|
|
117
|
+
console.error(pathValidation.error);
|
|
118
|
+
process.exitCode = 1;
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Find the page type file
|
|
123
|
+
const projectRoot = await findUpward("package.json");
|
|
124
|
+
if (!projectRoot) {
|
|
125
|
+
console.error("Could not find project root (no package.json found)");
|
|
126
|
+
process.exitCode = 1;
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const modelPath = new URL(`customtypes/${typeId}/index.json`, projectRoot);
|
|
131
|
+
|
|
132
|
+
// Read and parse the model
|
|
133
|
+
let model: CustomType;
|
|
134
|
+
try {
|
|
135
|
+
const contents = await readFile(modelPath, "utf8");
|
|
136
|
+
const result = v.safeParse(CustomTypeSchema, JSON.parse(contents));
|
|
137
|
+
if (!result.success) {
|
|
138
|
+
console.error(`Invalid page type model: ${modelPath.href}`);
|
|
139
|
+
process.exitCode = 1;
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
model = result.output as CustomType;
|
|
143
|
+
} catch (error) {
|
|
144
|
+
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
|
|
145
|
+
console.error(`Page type not found: ${typeId}\n`);
|
|
146
|
+
console.error(`Create it first with: prismic page-type create ${typeId}`);
|
|
147
|
+
process.exitCode = 1;
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
if (error instanceof Error) {
|
|
151
|
+
console.error(`Failed to read page type: ${error.message}`);
|
|
152
|
+
} else {
|
|
153
|
+
console.error("Failed to read page type");
|
|
154
|
+
}
|
|
155
|
+
process.exitCode = 1;
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Determine target tab
|
|
160
|
+
const existingTabs = Object.keys(model.json);
|
|
161
|
+
const targetTab = tab ?? existingTabs[0] ?? "Main";
|
|
162
|
+
|
|
163
|
+
// Initialize tab if it doesn't exist
|
|
164
|
+
if (!model.json[targetTab]) {
|
|
165
|
+
model.json[targetTab] = {};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Build field definition
|
|
169
|
+
const fieldDefinition: RichText = {
|
|
170
|
+
type: "StructuredText",
|
|
171
|
+
config: {
|
|
172
|
+
label: label ?? humanReadable(fieldPath.type === "nested" ? fieldPath.nestedFieldId : fieldId),
|
|
173
|
+
...(placeholder && { placeholder }),
|
|
174
|
+
...(single && { single }),
|
|
175
|
+
...(multi && { multi }),
|
|
176
|
+
...(allowTargetBlank && { allowTargetBlank: true }),
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
// Add field to model
|
|
181
|
+
if (fieldPath.type === "nested") {
|
|
182
|
+
const groupResult = findGroupInTab(model.json[targetTab], fieldPath.groupId, targetTab);
|
|
183
|
+
if (!groupResult.ok) {
|
|
184
|
+
console.error(groupResult.error);
|
|
185
|
+
process.exitCode = 1;
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
if (groupResult.group.config.fields[fieldPath.nestedFieldId]) {
|
|
189
|
+
console.error(`Field "${fieldPath.nestedFieldId}" already exists in group "${fieldPath.groupId}"`);
|
|
190
|
+
process.exitCode = 1;
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
groupResult.group.config.fields[fieldPath.nestedFieldId] = fieldDefinition;
|
|
194
|
+
} else {
|
|
195
|
+
for (const [tabName, tabFields] of Object.entries(model.json)) {
|
|
196
|
+
if (tabFields[fieldId]) {
|
|
197
|
+
console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
|
|
198
|
+
process.exitCode = 1;
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
for (const [groupFieldId, groupField] of Object.entries(tabFields)) {
|
|
202
|
+
if (isGroupField(groupField) && groupField.config.fields[fieldId]) {
|
|
203
|
+
console.error(`Field "${fieldId}" already exists in group "${groupFieldId}" in tab "${tabName}"`);
|
|
204
|
+
process.exitCode = 1;
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
model.json[targetTab][fieldId] = fieldDefinition;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Write updated model
|
|
213
|
+
try {
|
|
214
|
+
await writeFile(modelPath, stringify(model));
|
|
215
|
+
} catch (error) {
|
|
216
|
+
if (error instanceof Error) {
|
|
217
|
+
console.error(`Failed to update page type: ${error.message}`);
|
|
218
|
+
} else {
|
|
219
|
+
console.error("Failed to update page type");
|
|
220
|
+
}
|
|
221
|
+
process.exitCode = 1;
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (fieldPath.type === "nested") {
|
|
226
|
+
console.info(`Added field "${fieldPath.nestedFieldId}" (StructuredText) to group "${fieldPath.groupId}" in ${typeId}`);
|
|
227
|
+
} else {
|
|
228
|
+
console.info(`Added field "${fieldId}" (StructuredText) to "${targetTab}" tab in ${typeId}`);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
try {
|
|
232
|
+
await buildTypes({ output: types });
|
|
233
|
+
console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
|
|
234
|
+
} catch (error) {
|
|
235
|
+
console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
console.info();
|
|
239
|
+
console.info("Next: Add more fields with `prismic page-type add-field`");
|
|
240
|
+
|
|
241
|
+
const frameworkInfo = await detectFrameworkInfo();
|
|
242
|
+
if (frameworkInfo?.framework) {
|
|
243
|
+
const docsPath = getDocsPath(frameworkInfo.framework);
|
|
244
|
+
console.info(
|
|
245
|
+
` Run \`prismic docs fetch ${docsPath}#write-page-components\` to learn how to implement a page file`,
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
}
|