-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfield-add-boolean.ts
More file actions
55 lines (47 loc) · 1.7 KB
/
field-add-boolean.ts
File metadata and controls
55 lines (47 loc) · 1.7 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
46
47
48
49
50
51
52
53
54
55
import type { BooleanField } 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 boolean",
description: "Add a boolean field to a slice or custom type.",
positionals: {
id: { description: "Field ID", required: true },
},
options: {
...TARGET_OPTIONS,
label: { type: "string", description: "Field label" },
"default-value": { type: "boolean", description: "Default value" },
"true-label": { type: "string", description: "Label for true value" },
"false-label": { type: "string", description: "Label for false value" },
},
} satisfies CommandConfig;
export default createCommand(config, async ({ positionals, values }) => {
const [id] = positionals;
const {
label,
"default-value": default_value,
"true-label": placeholder_true,
"false-label": placeholder_false,
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: BooleanField = {
type: "Boolean",
config: {
label: label ?? capitalCase(fieldId),
default_value,
placeholder_true,
placeholder_false,
},
};
if (fieldId in targetFields) throw new CommandError(`Field "${id}" already exists.`);
targetFields[fieldId] = field;
await saveModel();
console.info(`Field added: ${id}`);
});