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,82 @@
|
|
|
1
|
+
import { writeFile } from "node:fs/promises";
|
|
2
|
+
import { parseArgs } from "node:util";
|
|
3
|
+
import { generateTypes, NON_EDITABLE_FILE_HEADER } from "prismic-ts-codegen";
|
|
4
|
+
|
|
5
|
+
import type { FrameworkAdapter } from "./framework";
|
|
6
|
+
|
|
7
|
+
import { requireFramework } from "./framework";
|
|
8
|
+
|
|
9
|
+
const HELP = `
|
|
10
|
+
Generate TypeScript types from local custom type and slice models.
|
|
11
|
+
|
|
12
|
+
USAGE
|
|
13
|
+
prismic codegen types [flags]
|
|
14
|
+
|
|
15
|
+
FLAGS
|
|
16
|
+
-o, --output string Output file path (default: "prismicio-types.d.ts")
|
|
17
|
+
-h, --help Show help for command
|
|
18
|
+
|
|
19
|
+
EXAMPLES
|
|
20
|
+
prismic codegen types
|
|
21
|
+
prismic codegen types --output custom.d.ts
|
|
22
|
+
`.trim();
|
|
23
|
+
|
|
24
|
+
export async function codegenTypes(): Promise<void> {
|
|
25
|
+
const {
|
|
26
|
+
values: { help, output },
|
|
27
|
+
} = parseArgs({
|
|
28
|
+
args: process.argv.slice(4), // skip: node, script, "codegen", "types"
|
|
29
|
+
options: {
|
|
30
|
+
output: { type: "string", short: "o" },
|
|
31
|
+
help: { type: "boolean", short: "h" },
|
|
32
|
+
},
|
|
33
|
+
allowPositionals: false,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
if (help) {
|
|
37
|
+
console.info(HELP);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
try {
|
|
42
|
+
await buildTypes({ output });
|
|
43
|
+
console.info(`Generated types written to ${output ?? "prismicio-types.d.ts"}`);
|
|
44
|
+
} catch (error) {
|
|
45
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
46
|
+
process.exitCode = 1;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export async function buildTypes(args?: {
|
|
51
|
+
output?: string;
|
|
52
|
+
framework?: FrameworkAdapter;
|
|
53
|
+
}): Promise<void> {
|
|
54
|
+
const output = args?.output ?? "prismicio-types.d.ts";
|
|
55
|
+
|
|
56
|
+
const framework = args?.framework ?? (await requireFramework());
|
|
57
|
+
if (!framework) {
|
|
58
|
+
throw new Error("No supported framework found");
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const [customTypeResults, sliceResults] = await Promise.all([
|
|
62
|
+
framework.getCustomTypes(),
|
|
63
|
+
framework.getSlices(),
|
|
64
|
+
]);
|
|
65
|
+
|
|
66
|
+
const customTypes = customTypeResults.map((ct) => ct.model);
|
|
67
|
+
const slices = sliceResults.map((s) => s.model);
|
|
68
|
+
|
|
69
|
+
const types = generateTypes({
|
|
70
|
+
customTypeModels: customTypes,
|
|
71
|
+
sharedSliceModels: slices,
|
|
72
|
+
typesProvider: "@prismicio/client",
|
|
73
|
+
clientIntegration: {
|
|
74
|
+
includeCreateClientInterface: customTypes.length > 0 || slices.length > 0,
|
|
75
|
+
includeContentNamespace: true,
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
const content = NON_EDITABLE_FILE_HEADER + "\n\n" + types;
|
|
80
|
+
|
|
81
|
+
await writeFile(output, content);
|
|
82
|
+
}
|
package/src/codegen.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
|
|
3
|
+
import { codegenTypes } from "./codegen-types";
|
|
4
|
+
|
|
5
|
+
const HELP = `
|
|
6
|
+
Generate code from Prismic models.
|
|
7
|
+
|
|
8
|
+
USAGE
|
|
9
|
+
prismic codegen <command> [flags]
|
|
10
|
+
|
|
11
|
+
COMMANDS
|
|
12
|
+
types Generate TypeScript types from models pushed to Prismic
|
|
13
|
+
|
|
14
|
+
FLAGS
|
|
15
|
+
-h, --help Show help for command
|
|
16
|
+
|
|
17
|
+
LEARN MORE
|
|
18
|
+
Use \`prismic codegen <command> --help\` for more information about a command.
|
|
19
|
+
`.trim();
|
|
20
|
+
|
|
21
|
+
export async function codegen(): Promise<void> {
|
|
22
|
+
const {
|
|
23
|
+
positionals: [subcommand],
|
|
24
|
+
} = parseArgs({
|
|
25
|
+
args: process.argv.slice(3), // skip: node, script, "codegen"
|
|
26
|
+
options: {
|
|
27
|
+
help: { type: "boolean", short: "h" },
|
|
28
|
+
},
|
|
29
|
+
allowPositionals: true,
|
|
30
|
+
strict: false,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
switch (subcommand) {
|
|
34
|
+
case "types":
|
|
35
|
+
await codegenTypes();
|
|
36
|
+
break;
|
|
37
|
+
default: {
|
|
38
|
+
if (subcommand) {
|
|
39
|
+
console.error(`Unknown codegen subcommand: ${subcommand}\n`);
|
|
40
|
+
process.exitCode = 1;
|
|
41
|
+
}
|
|
42
|
+
console.info(HELP);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import type { BooleanField, CustomType } 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 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 (inferred from field-id if omitted)
|
|
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
|
+
--types string Output file for generated types (default: "prismicio-types.d.ts")
|
|
27
|
+
-h, --help Show help for command
|
|
28
|
+
|
|
29
|
+
EXAMPLES
|
|
30
|
+
prismic custom-type add-field boolean homepage featured
|
|
31
|
+
prismic custom-type add-field boolean article published --default
|
|
32
|
+
prismic custom-type add-field boolean product available --true-label "In Stock" --false-label "Out of Stock"
|
|
33
|
+
`.trim();
|
|
34
|
+
|
|
35
|
+
export async function customTypeAddFieldBoolean(): Promise<void> {
|
|
36
|
+
const {
|
|
37
|
+
values: {
|
|
38
|
+
help,
|
|
39
|
+
tab,
|
|
40
|
+
label,
|
|
41
|
+
default: defaultValue,
|
|
42
|
+
"true-label": trueLabel,
|
|
43
|
+
"false-label": falseLabel,
|
|
44
|
+
types,
|
|
45
|
+
},
|
|
46
|
+
positionals: [typeId, fieldId],
|
|
47
|
+
} = parseArgs({
|
|
48
|
+
args: process.argv.slice(5), // skip: node, script, "custom-type", "add-field", "boolean"
|
|
49
|
+
options: {
|
|
50
|
+
tab: { type: "string", short: "t" },
|
|
51
|
+
label: { type: "string", short: "l" },
|
|
52
|
+
default: { type: "boolean" },
|
|
53
|
+
"true-label": { type: "string" },
|
|
54
|
+
"false-label": { type: "string" },
|
|
55
|
+
types: { type: "string" },
|
|
56
|
+
help: { type: "boolean", short: "h" },
|
|
57
|
+
},
|
|
58
|
+
allowPositionals: true,
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
if (help) {
|
|
62
|
+
console.info(HELP);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (!typeId) {
|
|
67
|
+
console.error("Missing required argument: type-id\n");
|
|
68
|
+
console.error("Usage: prismic custom-type add-field boolean <type-id> <field-id>");
|
|
69
|
+
process.exitCode = 1;
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (!fieldId) {
|
|
74
|
+
console.error("Missing required argument: field-id\n");
|
|
75
|
+
console.error("Usage: prismic custom-type add-field boolean <type-id> <field-id>");
|
|
76
|
+
process.exitCode = 1;
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Parse and validate field path
|
|
81
|
+
const fieldPath = parseFieldPath(fieldId);
|
|
82
|
+
const pathValidation = validateNestedFieldPath(fieldPath);
|
|
83
|
+
if (!pathValidation.ok) {
|
|
84
|
+
console.error(pathValidation.error);
|
|
85
|
+
process.exitCode = 1;
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const framework = await requireFramework();
|
|
90
|
+
if (!framework) return;
|
|
91
|
+
|
|
92
|
+
let model: CustomType;
|
|
93
|
+
try {
|
|
94
|
+
model = await framework.readCustomType(typeId);
|
|
95
|
+
} catch {
|
|
96
|
+
console.error(`Custom type not found: ${typeId}\n\nCreate it first with: prismic custom-type create ${typeId}`);
|
|
97
|
+
process.exitCode = 1;
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Determine target tab
|
|
102
|
+
const existingTabs = Object.keys(model.json);
|
|
103
|
+
const targetTab = tab ?? existingTabs[0] ?? "Main";
|
|
104
|
+
|
|
105
|
+
// Initialize tab if it doesn't exist
|
|
106
|
+
if (!model.json[targetTab]) {
|
|
107
|
+
model.json[targetTab] = {};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Build field definition
|
|
111
|
+
const fieldDefinition: BooleanField = {
|
|
112
|
+
type: "Boolean",
|
|
113
|
+
config: {
|
|
114
|
+
label: label ?? humanReadable(fieldPath.type === "nested" ? fieldPath.nestedFieldId : fieldId),
|
|
115
|
+
...(defaultValue && { default_value: true }),
|
|
116
|
+
...(trueLabel && { placeholder_true: trueLabel }),
|
|
117
|
+
...(falseLabel && { placeholder_false: falseLabel }),
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
// Add field to model
|
|
122
|
+
if (fieldPath.type === "nested") {
|
|
123
|
+
const groupResult = findGroupInTab(model.json[targetTab], fieldPath.groupId, targetTab);
|
|
124
|
+
if (!groupResult.ok) {
|
|
125
|
+
console.error(groupResult.error);
|
|
126
|
+
process.exitCode = 1;
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
// Check if field already exists in the group
|
|
130
|
+
if (groupResult.group.config.fields[fieldPath.nestedFieldId]) {
|
|
131
|
+
console.error(`Field "${fieldPath.nestedFieldId}" already exists in group "${fieldPath.groupId}"`);
|
|
132
|
+
process.exitCode = 1;
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
groupResult.group.config.fields[fieldPath.nestedFieldId] = fieldDefinition;
|
|
136
|
+
} else {
|
|
137
|
+
// Check if field already exists in any tab
|
|
138
|
+
for (const [tabName, tabFields] of Object.entries(model.json)) {
|
|
139
|
+
if (tabFields[fieldId]) {
|
|
140
|
+
console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
|
|
141
|
+
process.exitCode = 1;
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
// Also check inside groups
|
|
145
|
+
for (const [groupFieldId, groupField] of Object.entries(tabFields)) {
|
|
146
|
+
if (isGroupField(groupField) && groupField.config.fields[fieldId]) {
|
|
147
|
+
console.error(`Field "${fieldId}" already exists in group "${groupFieldId}" in tab "${tabName}"`);
|
|
148
|
+
process.exitCode = 1;
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
model.json[targetTab][fieldId] = fieldDefinition;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Write updated model
|
|
157
|
+
try {
|
|
158
|
+
await framework.updateCustomType(model);
|
|
159
|
+
} catch (error) {
|
|
160
|
+
if (error instanceof Error) {
|
|
161
|
+
console.error(`Failed to update custom type: ${error.message}`);
|
|
162
|
+
} else {
|
|
163
|
+
console.error("Failed to update custom type");
|
|
164
|
+
}
|
|
165
|
+
process.exitCode = 1;
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (fieldPath.type === "nested") {
|
|
170
|
+
console.info(`Added field "${fieldPath.nestedFieldId}" (Boolean) to group "${fieldPath.groupId}" in ${typeId}`);
|
|
171
|
+
} else {
|
|
172
|
+
console.info(`Added field "${fieldId}" (Boolean) to "${targetTab}" tab in ${typeId}`);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
try {
|
|
176
|
+
await buildTypes({ output: types });
|
|
177
|
+
console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
|
|
178
|
+
} catch (error) {
|
|
179
|
+
console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
console.info();
|
|
183
|
+
console.info("Next: Add more fields with `prismic custom-type add-field`");
|
|
184
|
+
console.info(" Run `prismic status` when done to find next steps");
|
|
185
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import type { Color, CustomType } 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 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 (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 color homepage bg_color
|
|
29
|
+
prismic custom-type add-field color homepage accent --tab "Design"
|
|
30
|
+
prismic custom-type add-field color homepage text_color --label "Text Color"
|
|
31
|
+
`.trim();
|
|
32
|
+
|
|
33
|
+
export async function customTypeAddFieldColor(): 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", "color"
|
|
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 color <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 color <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: Color = {
|
|
100
|
+
type: "Color",
|
|
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}" (Color) to group "${fieldPath.groupId}" in ${typeId}`);
|
|
154
|
+
} else {
|
|
155
|
+
console.info(`Added field "${fieldId}" (Color) 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,171 @@
|
|
|
1
|
+
import type { CustomType, Date as DateField } 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 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 (inferred from field-id if omitted)
|
|
23
|
+
-p, --placeholder string Placeholder text
|
|
24
|
+
--default string Default date value (YYYY-MM-DD format)
|
|
25
|
+
--types string Output file for generated types (default: "prismicio-types.d.ts")
|
|
26
|
+
-h, --help Show help for command
|
|
27
|
+
|
|
28
|
+
EXAMPLES
|
|
29
|
+
prismic custom-type add-field date homepage publish_date
|
|
30
|
+
prismic custom-type add-field date event start_date --tab "Schedule"
|
|
31
|
+
prismic custom-type add-field date article date --label "Publication Date" --default "2024-01-01"
|
|
32
|
+
`.trim();
|
|
33
|
+
|
|
34
|
+
export async function customTypeAddFieldDate(): Promise<void> {
|
|
35
|
+
const {
|
|
36
|
+
values: { help, tab, label, placeholder, default: defaultValue, types },
|
|
37
|
+
positionals: [typeId, fieldId],
|
|
38
|
+
} = parseArgs({
|
|
39
|
+
args: process.argv.slice(5), // skip: node, script, "custom-type", "add-field", "date"
|
|
40
|
+
options: {
|
|
41
|
+
tab: { type: "string", short: "t" },
|
|
42
|
+
label: { type: "string", short: "l" },
|
|
43
|
+
placeholder: { type: "string", short: "p" },
|
|
44
|
+
default: { type: "string" },
|
|
45
|
+
types: { type: "string" },
|
|
46
|
+
help: { type: "boolean", short: "h" },
|
|
47
|
+
},
|
|
48
|
+
allowPositionals: true,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
if (help) {
|
|
52
|
+
console.info(HELP);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (!typeId) {
|
|
57
|
+
console.error("Missing required argument: type-id\n");
|
|
58
|
+
console.error("Usage: prismic custom-type add-field date <type-id> <field-id>");
|
|
59
|
+
process.exitCode = 1;
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (!fieldId) {
|
|
64
|
+
console.error("Missing required argument: field-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
|
+
// Parse and validate field path
|
|
71
|
+
const fieldPath = parseFieldPath(fieldId);
|
|
72
|
+
const pathValidation = validateNestedFieldPath(fieldPath);
|
|
73
|
+
if (!pathValidation.ok) {
|
|
74
|
+
console.error(pathValidation.error);
|
|
75
|
+
process.exitCode = 1;
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const framework = await requireFramework();
|
|
80
|
+
if (!framework) return;
|
|
81
|
+
|
|
82
|
+
let model: CustomType;
|
|
83
|
+
try {
|
|
84
|
+
model = await framework.readCustomType(typeId);
|
|
85
|
+
} catch {
|
|
86
|
+
console.error(`Custom type not found: ${typeId}\n\nCreate it first with: prismic custom-type create ${typeId}`);
|
|
87
|
+
process.exitCode = 1;
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Determine target tab
|
|
92
|
+
const existingTabs = Object.keys(model.json);
|
|
93
|
+
const targetTab = tab ?? existingTabs[0] ?? "Main";
|
|
94
|
+
|
|
95
|
+
// Initialize tab if it doesn't exist
|
|
96
|
+
if (!model.json[targetTab]) {
|
|
97
|
+
model.json[targetTab] = {};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Build field definition
|
|
101
|
+
const fieldDefinition: DateField = {
|
|
102
|
+
type: "Date",
|
|
103
|
+
config: {
|
|
104
|
+
label: label ?? humanReadable(fieldPath.type === "nested" ? fieldPath.nestedFieldId : fieldId),
|
|
105
|
+
...(placeholder && { placeholder }),
|
|
106
|
+
...(defaultValue && { default: defaultValue }),
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
// Add field to model
|
|
111
|
+
if (fieldPath.type === "nested") {
|
|
112
|
+
const groupResult = findGroupInTab(model.json[targetTab], fieldPath.groupId, targetTab);
|
|
113
|
+
if (!groupResult.ok) {
|
|
114
|
+
console.error(groupResult.error);
|
|
115
|
+
process.exitCode = 1;
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (groupResult.group.config.fields[fieldPath.nestedFieldId]) {
|
|
119
|
+
console.error(`Field "${fieldPath.nestedFieldId}" already exists in group "${fieldPath.groupId}"`);
|
|
120
|
+
process.exitCode = 1;
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
groupResult.group.config.fields[fieldPath.nestedFieldId] = fieldDefinition;
|
|
124
|
+
} else {
|
|
125
|
+
for (const [tabName, tabFields] of Object.entries(model.json)) {
|
|
126
|
+
if (tabFields[fieldId]) {
|
|
127
|
+
console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
|
|
128
|
+
process.exitCode = 1;
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
for (const [groupFieldId, groupField] of Object.entries(tabFields)) {
|
|
132
|
+
if (isGroupField(groupField) && groupField.config.fields[fieldId]) {
|
|
133
|
+
console.error(`Field "${fieldId}" already exists in group "${groupFieldId}" in tab "${tabName}"`);
|
|
134
|
+
process.exitCode = 1;
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
model.json[targetTab][fieldId] = fieldDefinition;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Write updated model
|
|
143
|
+
try {
|
|
144
|
+
await framework.updateCustomType(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
|
+
if (fieldPath.type === "nested") {
|
|
156
|
+
console.info(`Added field "${fieldPath.nestedFieldId}" (Date) to group "${fieldPath.groupId}" in ${typeId}`);
|
|
157
|
+
} else {
|
|
158
|
+
console.info(`Added field "${fieldId}" (Date) to "${targetTab}" tab in ${typeId}`);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
try {
|
|
162
|
+
await buildTypes({ output: types });
|
|
163
|
+
console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
|
|
164
|
+
} catch (error) {
|
|
165
|
+
console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
console.info();
|
|
169
|
+
console.info("Next: Add more fields with `prismic custom-type add-field`");
|
|
170
|
+
console.info(" Run `prismic status` when done to find next steps");
|
|
171
|
+
}
|