-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfield-add-color.ts
More file actions
45 lines (37 loc) · 1.4 KB
/
field-add-color.ts
File metadata and controls
45 lines (37 loc) · 1.4 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
45
import type { Color } from "@prismicio/types-internal/lib/customtypes";
import { capitalCase } from "change-case";
import { getHost, getToken } from "../auth";
import { CommandError, createCommand, type CommandConfig } from "../lib/command";
import { resolveFieldTarget, resolveModel, TARGET_OPTIONS } from "../models";
import { getRepositoryName } from "../project";
const config = {
name: "prismic field add color",
description: "Add a color 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, repo = await getRepositoryName() } = values;
const token = await getToken();
const host = await getHost();
const [fields, saveModel] = await resolveModel(values, { repo, token, host });
const [targetFields, fieldId] = resolveFieldTarget(fields, id);
const field: Color = {
type: "Color",
config: {
label: label ?? capitalCase(fieldId),
placeholder,
},
};
if (fieldId in targetFields) throw new CommandError(`Field "${id}" already exists.`);
targetFields[fieldId] = field;
await saveModel();
console.info(`Field added: ${id}`);
});