Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/commands/field-add-boolean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type { BooleanField } 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 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,
} = values;

const adapter = await getAdapter();
const [fields, saveModel] = await resolveModel(values, { adapter });
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();
await adapter.generateTypes();

console.info(`Field added: ${id}`);
});
44 changes: 44 additions & 0 deletions src/commands/field-add-color.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { Color } 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 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 } = values;

const adapter = await getAdapter();
const [fields, saveModel] = await resolveModel(values, { adapter });
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();
await adapter.generateTypes();

console.info(`Field added: ${id}`);
});
51 changes: 51 additions & 0 deletions src/commands/field-add-content-relationship.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import type { Link } 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 content-relationship",
description: "Add a content relationship field to a slice or custom type.",
positionals: {
id: { description: "Field ID", required: true },
},
options: {
...TARGET_OPTIONS,
label: { type: "string", description: "Field label" },
tag: { type: "string", multiple: true, description: "Allowed tag (can be repeated)" },
"custom-type": { type: "string", multiple: true, description: "Allowed custom type (can be repeated)" },
},
} satisfies CommandConfig;

export default createCommand(config, async ({ positionals, values }) => {
const [id] = positionals;
const {
label,
tag: tags,
"custom-type": customtypes,
} = values;

const adapter = await getAdapter();
const [fields, saveModel] = await resolveModel(values, { adapter });
const [targetFields, fieldId] = resolveFieldTarget(fields, id);

const field: Link = {
type: "Link",
config: {
label: label ?? capitalCase(fieldId),
select: "document",
tags,
customtypes,
},
};

if (fieldId in targetFields) throw new CommandError(`Field "${id}" already exists.`);
targetFields[fieldId] = field;
await saveModel();
await adapter.generateTypes();

console.info(`Field added: ${id}`);
});
46 changes: 46 additions & 0 deletions src/commands/field-add-date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import type { Date as DateField } 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 date",
description: "Add a date 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" },
default: { type: "string", description: "Default value" },
},
} satisfies CommandConfig;

export default createCommand(config, async ({ positionals, values }) => {
const [id] = positionals;
const { label, placeholder, default: defaultValue } = values;

const adapter = await getAdapter();
const [fields, saveModel] = await resolveModel(values, { adapter });
const [targetFields, fieldId] = resolveFieldTarget(fields, id);

const field: DateField = {
type: "Date",
config: {
label: label ?? capitalCase(fieldId),
placeholder,
default: defaultValue,
},
};

if (fieldId in targetFields) throw new CommandError(`Field "${id}" already exists.`);
targetFields[fieldId] = field;
await saveModel();
await adapter.generateTypes();

console.info(`Field added: ${id}`);
});
44 changes: 44 additions & 0 deletions src/commands/field-add-embed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,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}`);
});
42 changes: 42 additions & 0 deletions src/commands/field-add-geopoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { GeoPoint } 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 geopoint",
description: "Add a geopoint field to a slice or custom type.",
positionals: {
id: { description: "Field ID", required: true },
},
options: {
...TARGET_OPTIONS,
label: { type: "string", description: "Field label" },
},
} satisfies CommandConfig;

export default createCommand(config, async ({ positionals, values }) => {
const [id] = positionals;
const { label } = values;

const adapter = await getAdapter();
const [fields, saveModel] = await resolveModel(values, { adapter });
const [targetFields, fieldId] = resolveFieldTarget(fields, id);

const field: GeoPoint = {
type: "GeoPoint",
config: {
label: label ?? capitalCase(fieldId),
},
};

if (fieldId in targetFields) throw new CommandError(`Field "${id}" already exists.`);
targetFields[fieldId] = field;
await saveModel();
await adapter.generateTypes();

console.info(`Field added: ${id}`);
});
42 changes: 42 additions & 0 deletions src/commands/field-add-group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { Group } 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 group",
description: "Add a group field to a slice or custom type.",
positionals: {
id: { description: "Field ID", required: true },
},
options: {
...TARGET_OPTIONS,
label: { type: "string", description: "Field label" },
},
} satisfies CommandConfig;

export default createCommand(config, async ({ positionals, values }) => {
const [id] = positionals;
const { label } = values;

const adapter = await getAdapter();
const [fields, saveModel] = await resolveModel(values, { adapter });
const [targetFields, fieldId] = resolveFieldTarget(fields, id);

const field: Group = {
type: "Group",
config: {
label: label ?? capitalCase(fieldId),
},
};

if (fieldId in targetFields) throw new CommandError(`Field "${id}" already exists.`);
targetFields[fieldId] = field;
await saveModel();
await adapter.generateTypes();

console.info(`Field added: ${id}`);
});
44 changes: 44 additions & 0 deletions src/commands/field-add-image.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { Image } 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 image",
description: "Add an image 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: Image = {
type: "Image",
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}`);
});
Loading