prismic 0.0.0-pr.28.59bf330
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 +69 -0
- package/dist/builders-hKD4IrLX-DsO7BUQw.mjs +97 -0
- package/dist/dist-B11B2hHn.mjs +1 -0
- package/dist/dist-DT8CtumB.mjs +1 -0
- package/dist/framework-CfjEoVk0.mjs +17 -0
- package/dist/index.mjs +2537 -0
- package/dist/nextjs-9z7YrSnS.mjs +312 -0
- package/dist/nuxt-KoJ61G2q.mjs +59 -0
- package/dist/sveltekit-DjXKCG78.mjs +226 -0
- package/package.json +58 -0
- package/src/codegen-types.ts +82 -0
- package/src/codegen.ts +45 -0
- package/src/custom-type-add-field-boolean.ts +185 -0
- package/src/custom-type-add-field-color.ts +168 -0
- package/src/custom-type-add-field-date.ts +171 -0
- package/src/custom-type-add-field-embed.ts +168 -0
- package/src/custom-type-add-field-geo-point.ts +165 -0
- package/src/custom-type-add-field-group.ts +142 -0
- package/src/custom-type-add-field-image.ts +168 -0
- package/src/custom-type-add-field-key-text.ts +168 -0
- package/src/custom-type-add-field-link.ts +191 -0
- package/src/custom-type-add-field-number.ts +200 -0
- package/src/custom-type-add-field-rich-text.ts +192 -0
- package/src/custom-type-add-field-select.ts +174 -0
- package/src/custom-type-add-field-timestamp.ts +171 -0
- package/src/custom-type-add-field-uid.ts +151 -0
- package/src/custom-type-add-field.ts +116 -0
- package/src/custom-type-connect-slice.ts +178 -0
- package/src/custom-type-create.ts +98 -0
- package/src/custom-type-disconnect-slice.ts +134 -0
- package/src/custom-type-list.ts +110 -0
- package/src/custom-type-remove-field.ts +135 -0
- package/src/custom-type-remove.ts +103 -0
- package/src/custom-type-set-name.ts +102 -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/env.d.ts +12 -0
- package/src/framework/index.ts +399 -0
- package/src/framework/nextjs.templates.ts +426 -0
- package/src/framework/nextjs.ts +216 -0
- package/src/framework/nuxt.templates.ts +74 -0
- package/src/framework/nuxt.ts +250 -0
- package/src/framework/sveltekit.templates.ts +278 -0
- package/src/framework/sveltekit.ts +241 -0
- package/src/index.ts +155 -0
- package/src/init.ts +173 -0
- package/src/lib/auth.ts +200 -0
- package/src/lib/browser.ts +11 -0
- package/src/lib/config.ts +111 -0
- package/src/lib/custom-types-api.ts +385 -0
- package/src/lib/field-path.ts +81 -0
- package/src/lib/file.ts +49 -0
- package/src/lib/json.ts +3 -0
- package/src/lib/packageJson.ts +35 -0
- package/src/lib/profile.ts +39 -0
- package/src/lib/request.ts +116 -0
- package/src/lib/segment.ts +145 -0
- package/src/lib/sentry.ts +63 -0
- package/src/lib/string.ts +10 -0
- package/src/lib/url.ts +31 -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 +45 -0
- package/src/logout.ts +36 -0
- package/src/page-type-add-field-boolean.ts +179 -0
- package/src/page-type-add-field-color.ts +165 -0
- package/src/page-type-add-field-date.ts +168 -0
- package/src/page-type-add-field-embed.ts +165 -0
- package/src/page-type-add-field-geo-point.ts +162 -0
- package/src/page-type-add-field-group.ts +139 -0
- package/src/page-type-add-field-image.ts +165 -0
- package/src/page-type-add-field-key-text.ts +165 -0
- package/src/page-type-add-field-link.ts +188 -0
- package/src/page-type-add-field-number.ts +197 -0
- package/src/page-type-add-field-rich-text.ts +189 -0
- package/src/page-type-add-field-select.ts +171 -0
- package/src/page-type-add-field-timestamp.ts +168 -0
- package/src/page-type-add-field-uid.ts +148 -0
- package/src/page-type-add-field.ts +116 -0
- package/src/page-type-connect-slice.ts +178 -0
- package/src/page-type-create.ts +128 -0
- package/src/page-type-disconnect-slice.ts +134 -0
- package/src/page-type-list.ts +109 -0
- package/src/page-type-remove-field.ts +135 -0
- package/src/page-type-remove.ts +103 -0
- package/src/page-type-set-name.ts +102 -0
- package/src/page-type-set-repeatable.ts +111 -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 +236 -0
- package/src/push.ts +409 -0
- package/src/repo-create.ts +175 -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 +219 -0
- package/src/slice-add-field-color.ts +205 -0
- package/src/slice-add-field-date.ts +205 -0
- package/src/slice-add-field-embed.ts +205 -0
- package/src/slice-add-field-geo-point.ts +202 -0
- package/src/slice-add-field-group.ts +170 -0
- package/src/slice-add-field-image.ts +202 -0
- package/src/slice-add-field-key-text.ts +205 -0
- package/src/slice-add-field-link.ts +224 -0
- package/src/slice-add-field-number.ts +205 -0
- package/src/slice-add-field-rich-text.ts +229 -0
- package/src/slice-add-field-select.ts +211 -0
- package/src/slice-add-field-timestamp.ts +205 -0
- package/src/slice-add-field.ts +111 -0
- package/src/slice-add-variation.ts +142 -0
- package/src/slice-create.ts +164 -0
- package/src/slice-list-variations.ts +71 -0
- package/src/slice-list.ts +60 -0
- package/src/slice-remove-field.ts +125 -0
- package/src/slice-remove-variation.ts +113 -0
- package/src/slice-remove.ts +92 -0
- package/src/slice-rename.ts +104 -0
- package/src/slice-set-screenshot.ts +239 -0
- package/src/slice-view.ts +83 -0
- package/src/slice.ts +95 -0
- package/src/status.ts +834 -0
- package/src/sync.ts +259 -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,168 @@
|
|
|
1
|
+
import type { CustomType, Text } from "@prismicio/types-internal/lib/customtypes";
|
|
2
|
+
|
|
3
|
+
import { parseArgs } from "node:util";
|
|
4
|
+
|
|
5
|
+
import { buildTypes } from "./codegen-types";
|
|
6
|
+
import { findGroupInTab, isGroupField, parseFieldPath, validateNestedFieldPath } from "./lib/field-path";
|
|
7
|
+
import { requireFramework } from "./framework";
|
|
8
|
+
import { humanReadable } from "./lib/string";
|
|
9
|
+
|
|
10
|
+
const HELP = `
|
|
11
|
+
Add a key-text (single-line text) field to an existing custom type.
|
|
12
|
+
|
|
13
|
+
USAGE
|
|
14
|
+
prismic custom-type add-field key-text <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 (inferred from field-id if omitted)
|
|
23
|
+
-p, --placeholder string Placeholder text
|
|
24
|
+
--types string Output file for generated types (default: "prismicio-types.d.ts")
|
|
25
|
+
-h, --help Show help for command
|
|
26
|
+
|
|
27
|
+
EXAMPLES
|
|
28
|
+
prismic custom-type add-field key-text homepage title
|
|
29
|
+
prismic custom-type add-field key-text homepage meta_title --tab "SEO"
|
|
30
|
+
prismic custom-type add-field key-text homepage subtitle --label "Subtitle" --placeholder "Enter subtitle"
|
|
31
|
+
`.trim();
|
|
32
|
+
|
|
33
|
+
export async function customTypeAddFieldKeyText(): Promise<void> {
|
|
34
|
+
const {
|
|
35
|
+
values: { help, tab, label, placeholder, types },
|
|
36
|
+
positionals: [typeId, fieldId],
|
|
37
|
+
} = parseArgs({
|
|
38
|
+
args: process.argv.slice(5), // skip: node, script, "custom-type", "add-field", "key-text"
|
|
39
|
+
options: {
|
|
40
|
+
tab: { type: "string", short: "t" },
|
|
41
|
+
label: { type: "string", short: "l" },
|
|
42
|
+
placeholder: { type: "string", short: "p" },
|
|
43
|
+
types: { type: "string" },
|
|
44
|
+
help: { type: "boolean", short: "h" },
|
|
45
|
+
},
|
|
46
|
+
allowPositionals: true,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
if (help) {
|
|
50
|
+
console.info(HELP);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (!typeId) {
|
|
55
|
+
console.error("Missing required argument: type-id\n");
|
|
56
|
+
console.error("Usage: prismic custom-type add-field key-text <type-id> <field-id>");
|
|
57
|
+
process.exitCode = 1;
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (!fieldId) {
|
|
62
|
+
console.error("Missing required argument: field-id\n");
|
|
63
|
+
console.error("Usage: prismic custom-type add-field key-text <type-id> <field-id>");
|
|
64
|
+
process.exitCode = 1;
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Parse and validate field path
|
|
69
|
+
const fieldPath = parseFieldPath(fieldId);
|
|
70
|
+
const pathValidation = validateNestedFieldPath(fieldPath);
|
|
71
|
+
if (!pathValidation.ok) {
|
|
72
|
+
console.error(pathValidation.error);
|
|
73
|
+
process.exitCode = 1;
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const framework = await requireFramework();
|
|
78
|
+
if (!framework) return;
|
|
79
|
+
|
|
80
|
+
let model: CustomType;
|
|
81
|
+
try {
|
|
82
|
+
model = await framework.readCustomType(typeId);
|
|
83
|
+
} catch {
|
|
84
|
+
console.error(`Custom type not found: ${typeId}\n\nCreate it first with: prismic custom-type create ${typeId}`);
|
|
85
|
+
process.exitCode = 1;
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Determine target tab
|
|
90
|
+
const existingTabs = Object.keys(model.json);
|
|
91
|
+
const targetTab = tab ?? existingTabs[0] ?? "Main";
|
|
92
|
+
|
|
93
|
+
// Initialize tab if it doesn't exist
|
|
94
|
+
if (!model.json[targetTab]) {
|
|
95
|
+
model.json[targetTab] = {};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Build field definition
|
|
99
|
+
const fieldDefinition: Text = {
|
|
100
|
+
type: "Text",
|
|
101
|
+
config: {
|
|
102
|
+
label: label ?? humanReadable(fieldPath.type === "nested" ? fieldPath.nestedFieldId : fieldId),
|
|
103
|
+
...(placeholder && { placeholder }),
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
// Add field to model
|
|
108
|
+
if (fieldPath.type === "nested") {
|
|
109
|
+
const groupResult = findGroupInTab(model.json[targetTab], fieldPath.groupId, targetTab);
|
|
110
|
+
if (!groupResult.ok) {
|
|
111
|
+
console.error(groupResult.error);
|
|
112
|
+
process.exitCode = 1;
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (groupResult.group.config.fields[fieldPath.nestedFieldId]) {
|
|
116
|
+
console.error(`Field "${fieldPath.nestedFieldId}" already exists in group "${fieldPath.groupId}"`);
|
|
117
|
+
process.exitCode = 1;
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
groupResult.group.config.fields[fieldPath.nestedFieldId] = fieldDefinition;
|
|
121
|
+
} else {
|
|
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
|
+
for (const [groupFieldId, groupField] of Object.entries(tabFields)) {
|
|
129
|
+
if (isGroupField(groupField) && groupField.config.fields[fieldId]) {
|
|
130
|
+
console.error(`Field "${fieldId}" already exists in group "${groupFieldId}" in tab "${tabName}"`);
|
|
131
|
+
process.exitCode = 1;
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
model.json[targetTab][fieldId] = fieldDefinition;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Write updated model
|
|
140
|
+
try {
|
|
141
|
+
await framework.updateCustomType(model);
|
|
142
|
+
} catch (error) {
|
|
143
|
+
if (error instanceof Error) {
|
|
144
|
+
console.error(`Failed to update custom type: ${error.message}`);
|
|
145
|
+
} else {
|
|
146
|
+
console.error("Failed to update custom type");
|
|
147
|
+
}
|
|
148
|
+
process.exitCode = 1;
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (fieldPath.type === "nested") {
|
|
153
|
+
console.info(`Added field "${fieldPath.nestedFieldId}" (Text) to group "${fieldPath.groupId}" in ${typeId}`);
|
|
154
|
+
} else {
|
|
155
|
+
console.info(`Added field "${fieldId}" (Text) to "${targetTab}" tab in ${typeId}`);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
try {
|
|
159
|
+
await buildTypes({ output: types });
|
|
160
|
+
console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
|
|
161
|
+
} catch (error) {
|
|
162
|
+
console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
console.info();
|
|
166
|
+
console.info("Next: Add more fields with `prismic custom-type add-field`");
|
|
167
|
+
console.info(" Run `prismic status` when done to find next steps");
|
|
168
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import type { CustomType, Link } from "@prismicio/types-internal/lib/customtypes";
|
|
2
|
+
|
|
3
|
+
import { parseArgs } from "node:util";
|
|
4
|
+
|
|
5
|
+
import { buildTypes } from "./codegen-types";
|
|
6
|
+
import { findGroupInTab, isGroupField, parseFieldPath, validateNestedFieldPath } from "./lib/field-path";
|
|
7
|
+
import { requireFramework } from "./framework";
|
|
8
|
+
import { humanReadable } from "./lib/string";
|
|
9
|
+
|
|
10
|
+
const HELP = `
|
|
11
|
+
Add a link field to an existing custom type.
|
|
12
|
+
|
|
13
|
+
USAGE
|
|
14
|
+
prismic custom-type add-field link <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 (inferred from field-id if omitted)
|
|
23
|
+
-p, --placeholder string Placeholder text
|
|
24
|
+
--variation string Slice variations (can be used multiple times)
|
|
25
|
+
--allow-text Allow text with link
|
|
26
|
+
--allow-target-blank Allow opening link in new tab
|
|
27
|
+
--repeatable Allow multiple links
|
|
28
|
+
--types string Output file for generated types (default: "prismicio-types.d.ts")
|
|
29
|
+
-h, --help Show help for command
|
|
30
|
+
|
|
31
|
+
EXAMPLES
|
|
32
|
+
prismic custom-type add-field link homepage button
|
|
33
|
+
prismic custom-type add-field link homepage cta --allow-text
|
|
34
|
+
prismic custom-type add-field link homepage cta --variation Primary --variation Secondary
|
|
35
|
+
prismic custom-type add-field link homepage links --repeatable
|
|
36
|
+
`.trim();
|
|
37
|
+
|
|
38
|
+
export async function customTypeAddFieldLink(): Promise<void> {
|
|
39
|
+
const {
|
|
40
|
+
values: {
|
|
41
|
+
help,
|
|
42
|
+
tab,
|
|
43
|
+
label,
|
|
44
|
+
placeholder,
|
|
45
|
+
variation,
|
|
46
|
+
"allow-text": allowText,
|
|
47
|
+
"allow-target-blank": allowTargetBlank,
|
|
48
|
+
repeatable,
|
|
49
|
+
types,
|
|
50
|
+
},
|
|
51
|
+
positionals: [typeId, fieldId],
|
|
52
|
+
} = parseArgs({
|
|
53
|
+
args: process.argv.slice(5), // skip: node, script, "custom-type", "add-field", "link"
|
|
54
|
+
options: {
|
|
55
|
+
tab: { type: "string", short: "t" },
|
|
56
|
+
label: { type: "string", short: "l" },
|
|
57
|
+
placeholder: { type: "string", short: "p" },
|
|
58
|
+
variation: { type: "string", multiple: true },
|
|
59
|
+
"allow-text": { type: "boolean" },
|
|
60
|
+
"allow-target-blank": { type: "boolean" },
|
|
61
|
+
repeatable: { type: "boolean" },
|
|
62
|
+
types: { type: "string" },
|
|
63
|
+
help: { type: "boolean", short: "h" },
|
|
64
|
+
},
|
|
65
|
+
allowPositionals: true,
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
if (help) {
|
|
69
|
+
console.info(HELP);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (!typeId) {
|
|
74
|
+
console.error("Missing required argument: type-id\n");
|
|
75
|
+
console.error("Usage: prismic custom-type add-field link <type-id> <field-id>");
|
|
76
|
+
process.exitCode = 1;
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (!fieldId) {
|
|
81
|
+
console.error("Missing required argument: field-id\n");
|
|
82
|
+
console.error("Usage: prismic custom-type add-field link <type-id> <field-id>");
|
|
83
|
+
process.exitCode = 1;
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Parse and validate field path
|
|
88
|
+
const fieldPath = parseFieldPath(fieldId);
|
|
89
|
+
const pathValidation = validateNestedFieldPath(fieldPath);
|
|
90
|
+
if (!pathValidation.ok) {
|
|
91
|
+
console.error(pathValidation.error);
|
|
92
|
+
process.exitCode = 1;
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const framework = await requireFramework();
|
|
97
|
+
if (!framework) return;
|
|
98
|
+
|
|
99
|
+
let model: CustomType;
|
|
100
|
+
try {
|
|
101
|
+
model = await framework.readCustomType(typeId);
|
|
102
|
+
} catch {
|
|
103
|
+
console.error(`Custom type not found: ${typeId}\n\nCreate it first with: prismic custom-type create ${typeId}`);
|
|
104
|
+
process.exitCode = 1;
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Determine target tab
|
|
109
|
+
const existingTabs = Object.keys(model.json);
|
|
110
|
+
const targetTab = tab ?? existingTabs[0] ?? "Main";
|
|
111
|
+
|
|
112
|
+
// Initialize tab if it doesn't exist
|
|
113
|
+
if (!model.json[targetTab]) {
|
|
114
|
+
model.json[targetTab] = {};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Build field definition
|
|
118
|
+
const fieldDefinition: Link = {
|
|
119
|
+
type: "Link",
|
|
120
|
+
config: {
|
|
121
|
+
label: label ?? humanReadable(fieldPath.type === "nested" ? fieldPath.nestedFieldId : fieldId),
|
|
122
|
+
...(placeholder && { placeholder }),
|
|
123
|
+
...(variation && variation.length > 0 && { variants: variation }),
|
|
124
|
+
...(allowText && { allowText: true }),
|
|
125
|
+
...(allowTargetBlank && { allowTargetBlank: true }),
|
|
126
|
+
...(repeatable && { repeat: true }),
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
// Add field to model
|
|
131
|
+
if (fieldPath.type === "nested") {
|
|
132
|
+
const groupResult = findGroupInTab(model.json[targetTab], fieldPath.groupId, targetTab);
|
|
133
|
+
if (!groupResult.ok) {
|
|
134
|
+
console.error(groupResult.error);
|
|
135
|
+
process.exitCode = 1;
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (groupResult.group.config.fields[fieldPath.nestedFieldId]) {
|
|
139
|
+
console.error(`Field "${fieldPath.nestedFieldId}" already exists in group "${fieldPath.groupId}"`);
|
|
140
|
+
process.exitCode = 1;
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
groupResult.group.config.fields[fieldPath.nestedFieldId] = fieldDefinition;
|
|
144
|
+
} else {
|
|
145
|
+
for (const [tabName, tabFields] of Object.entries(model.json)) {
|
|
146
|
+
if (tabFields[fieldId]) {
|
|
147
|
+
console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
|
|
148
|
+
process.exitCode = 1;
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
for (const [groupFieldId, groupField] of Object.entries(tabFields)) {
|
|
152
|
+
if (isGroupField(groupField) && groupField.config.fields[fieldId]) {
|
|
153
|
+
console.error(`Field "${fieldId}" already exists in group "${groupFieldId}" in tab "${tabName}"`);
|
|
154
|
+
process.exitCode = 1;
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
model.json[targetTab][fieldId] = fieldDefinition;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Write updated model
|
|
163
|
+
try {
|
|
164
|
+
await framework.updateCustomType(model);
|
|
165
|
+
} catch (error) {
|
|
166
|
+
if (error instanceof Error) {
|
|
167
|
+
console.error(`Failed to update custom type: ${error.message}`);
|
|
168
|
+
} else {
|
|
169
|
+
console.error("Failed to update custom type");
|
|
170
|
+
}
|
|
171
|
+
process.exitCode = 1;
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (fieldPath.type === "nested") {
|
|
176
|
+
console.info(`Added field "${fieldPath.nestedFieldId}" (Link) to group "${fieldPath.groupId}" in ${typeId}`);
|
|
177
|
+
} else {
|
|
178
|
+
console.info(`Added field "${fieldId}" (Link) to "${targetTab}" tab in ${typeId}`);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
try {
|
|
182
|
+
await buildTypes({ output: types });
|
|
183
|
+
console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
|
|
184
|
+
} catch (error) {
|
|
185
|
+
console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
console.info();
|
|
189
|
+
console.info("Next: Add more fields with `prismic custom-type add-field`");
|
|
190
|
+
console.info(" Run `prismic status` when done to find next steps");
|
|
191
|
+
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import type { CustomType, Number as NumberField } from "@prismicio/types-internal/lib/customtypes";
|
|
2
|
+
|
|
3
|
+
import { parseArgs } from "node:util";
|
|
4
|
+
|
|
5
|
+
import { buildTypes } from "./codegen-types";
|
|
6
|
+
import { findGroupInTab, isGroupField, parseFieldPath, validateNestedFieldPath } from "./lib/field-path";
|
|
7
|
+
import { requireFramework } from "./framework";
|
|
8
|
+
import { humanReadable } from "./lib/string";
|
|
9
|
+
|
|
10
|
+
const HELP = `
|
|
11
|
+
Add a number field to an existing custom type.
|
|
12
|
+
|
|
13
|
+
USAGE
|
|
14
|
+
prismic custom-type add-field number <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 (inferred from field-id if omitted)
|
|
23
|
+
-p, --placeholder string Placeholder text
|
|
24
|
+
--min number Minimum value
|
|
25
|
+
--max number Maximum value
|
|
26
|
+
--step number Step increment
|
|
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 number homepage price
|
|
32
|
+
prismic custom-type add-field number product quantity --min 0 --max 100
|
|
33
|
+
prismic custom-type add-field number settings rating --min 1 --max 5 --step 1
|
|
34
|
+
`.trim();
|
|
35
|
+
|
|
36
|
+
export async function customTypeAddFieldNumber(): Promise<void> {
|
|
37
|
+
const {
|
|
38
|
+
values: { help, tab, label, placeholder, min, max, step, types },
|
|
39
|
+
positionals: [typeId, fieldId],
|
|
40
|
+
} = parseArgs({
|
|
41
|
+
args: process.argv.slice(5), // skip: node, script, "custom-type", "add-field", "number"
|
|
42
|
+
options: {
|
|
43
|
+
tab: { type: "string", short: "t" },
|
|
44
|
+
label: { type: "string", short: "l" },
|
|
45
|
+
placeholder: { type: "string", short: "p" },
|
|
46
|
+
min: { type: "string" },
|
|
47
|
+
max: { type: "string" },
|
|
48
|
+
step: { type: "string" },
|
|
49
|
+
types: { type: "string" },
|
|
50
|
+
help: { type: "boolean", short: "h" },
|
|
51
|
+
},
|
|
52
|
+
allowPositionals: true,
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
if (help) {
|
|
56
|
+
console.info(HELP);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (!typeId) {
|
|
61
|
+
console.error("Missing required argument: type-id\n");
|
|
62
|
+
console.error("Usage: prismic custom-type add-field number <type-id> <field-id>");
|
|
63
|
+
process.exitCode = 1;
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (!fieldId) {
|
|
68
|
+
console.error("Missing required argument: field-id\n");
|
|
69
|
+
console.error("Usage: prismic custom-type add-field number <type-id> <field-id>");
|
|
70
|
+
process.exitCode = 1;
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Parse and validate field path
|
|
75
|
+
const fieldPath = parseFieldPath(fieldId);
|
|
76
|
+
const pathValidation = validateNestedFieldPath(fieldPath);
|
|
77
|
+
if (!pathValidation.ok) {
|
|
78
|
+
console.error(pathValidation.error);
|
|
79
|
+
process.exitCode = 1;
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Parse numeric values
|
|
84
|
+
const minValue = min !== undefined ? Number(min) : undefined;
|
|
85
|
+
const maxValue = max !== undefined ? Number(max) : undefined;
|
|
86
|
+
const stepValue = step !== undefined ? Number(step) : undefined;
|
|
87
|
+
|
|
88
|
+
if (min !== undefined && Number.isNaN(minValue)) {
|
|
89
|
+
console.error("Invalid --min value: must be a number");
|
|
90
|
+
process.exitCode = 1;
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (max !== undefined && Number.isNaN(maxValue)) {
|
|
95
|
+
console.error("Invalid --max value: must be a number");
|
|
96
|
+
process.exitCode = 1;
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (step !== undefined && Number.isNaN(stepValue)) {
|
|
101
|
+
console.error("Invalid --step value: must be a number");
|
|
102
|
+
process.exitCode = 1;
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const framework = await requireFramework();
|
|
107
|
+
if (!framework) return;
|
|
108
|
+
|
|
109
|
+
let model: CustomType;
|
|
110
|
+
try {
|
|
111
|
+
model = await framework.readCustomType(typeId);
|
|
112
|
+
} catch {
|
|
113
|
+
console.error(`Custom type not found: ${typeId}\n\nCreate it first with: prismic custom-type create ${typeId}`);
|
|
114
|
+
process.exitCode = 1;
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Determine target tab
|
|
119
|
+
const existingTabs = Object.keys(model.json);
|
|
120
|
+
const targetTab = tab ?? existingTabs[0] ?? "Main";
|
|
121
|
+
|
|
122
|
+
// Initialize tab if it doesn't exist
|
|
123
|
+
if (!model.json[targetTab]) {
|
|
124
|
+
model.json[targetTab] = {};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Build field definition
|
|
128
|
+
const fieldDefinition: NumberField = {
|
|
129
|
+
type: "Number",
|
|
130
|
+
config: {
|
|
131
|
+
label: label ?? humanReadable(fieldPath.type === "nested" ? fieldPath.nestedFieldId : fieldId),
|
|
132
|
+
...(placeholder && { placeholder }),
|
|
133
|
+
...(minValue !== undefined && { min: minValue }),
|
|
134
|
+
...(maxValue !== undefined && { max: maxValue }),
|
|
135
|
+
...(stepValue !== undefined && { step: stepValue }),
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
// Add field to model
|
|
140
|
+
if (fieldPath.type === "nested") {
|
|
141
|
+
const groupResult = findGroupInTab(model.json[targetTab], fieldPath.groupId, targetTab);
|
|
142
|
+
if (!groupResult.ok) {
|
|
143
|
+
console.error(groupResult.error);
|
|
144
|
+
process.exitCode = 1;
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
if (groupResult.group.config.fields[fieldPath.nestedFieldId]) {
|
|
148
|
+
console.error(`Field "${fieldPath.nestedFieldId}" already exists in group "${fieldPath.groupId}"`);
|
|
149
|
+
process.exitCode = 1;
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
groupResult.group.config.fields[fieldPath.nestedFieldId] = fieldDefinition;
|
|
153
|
+
} else {
|
|
154
|
+
for (const [tabName, tabFields] of Object.entries(model.json)) {
|
|
155
|
+
if (tabFields[fieldId]) {
|
|
156
|
+
console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
|
|
157
|
+
process.exitCode = 1;
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
for (const [groupFieldId, groupField] of Object.entries(tabFields)) {
|
|
161
|
+
if (isGroupField(groupField) && groupField.config.fields[fieldId]) {
|
|
162
|
+
console.error(`Field "${fieldId}" already exists in group "${groupFieldId}" in tab "${tabName}"`);
|
|
163
|
+
process.exitCode = 1;
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
model.json[targetTab][fieldId] = fieldDefinition;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Write updated model
|
|
172
|
+
try {
|
|
173
|
+
await framework.updateCustomType(model);
|
|
174
|
+
} catch (error) {
|
|
175
|
+
if (error instanceof Error) {
|
|
176
|
+
console.error(`Failed to update custom type: ${error.message}`);
|
|
177
|
+
} else {
|
|
178
|
+
console.error("Failed to update custom type");
|
|
179
|
+
}
|
|
180
|
+
process.exitCode = 1;
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (fieldPath.type === "nested") {
|
|
185
|
+
console.info(`Added field "${fieldPath.nestedFieldId}" (Number) to group "${fieldPath.groupId}" in ${typeId}`);
|
|
186
|
+
} else {
|
|
187
|
+
console.info(`Added field "${fieldId}" (Number) to "${targetTab}" tab in ${typeId}`);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
try {
|
|
191
|
+
await buildTypes({ output: types });
|
|
192
|
+
console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
|
|
193
|
+
} catch (error) {
|
|
194
|
+
console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
console.info();
|
|
198
|
+
console.info("Next: Add more fields with `prismic custom-type add-field`");
|
|
199
|
+
console.info(" Run `prismic status` when done to find next steps");
|
|
200
|
+
}
|