-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfield-add-embed.ts
More file actions
44 lines (36 loc) · 1.31 KB
/
field-add-embed.ts
File metadata and controls
44 lines (36 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import type { Embed } from "@prismicio/types-internal/lib/customtypes";
import { capitalCase } from "change-case";
import { getAdapter } from "../adapters";
import { CommandError, createCommand, type CommandConfig } from "../lib/command";
import { resolveFieldTarget, resolveModel, TARGET_OPTIONS } from "../models";
const config = {
name: "prismic field add embed",
description: "Add an embed field to a slice or custom type.",
positionals: {
id: { description: "Field ID", required: true },
},
options: {
...TARGET_OPTIONS,
label: { type: "string", description: "Field label" },
placeholder: { type: "string", description: "Placeholder text" },
},
} satisfies CommandConfig;
export default createCommand(config, async ({ positionals, values }) => {
const [id] = positionals;
const { label, placeholder } = values;
const adapter = await getAdapter();
const [fields, saveModel] = await resolveModel(values, { adapter });
const [targetFields, fieldId] = resolveFieldTarget(fields, id);
const field: Embed = {
type: "Embed",
config: {
label: label ?? capitalCase(fieldId),
placeholder,
},
};
if (fieldId in targetFields) throw new CommandError(`Field "${id}" already exists.`);
targetFields[fieldId] = field;
await saveModel();
await adapter.generateTypes();
console.info(`Field added: ${id}`);
});