diff --git a/src/commands/field-add-group.ts b/src/commands/field-add-group.ts index 8c9351e8..e3309489 100644 --- a/src/commands/field-add-group.ts +++ b/src/commands/field-add-group.ts @@ -2,7 +2,7 @@ import type { GroupModel } from "@prismicio/types-internal"; import { capitalCase } from "change-case"; import { getNewFieldTarget, TARGET_OPTIONS } from "../fields"; -import { createCommand, type CommandConfig } from "../lib/command"; +import { CommandError, createCommand, type CommandConfig } from "../lib/command"; import { addField } from "../lib/prismic/models"; const config = { @@ -25,6 +25,10 @@ export default createCommand(config, async ({ positionals, values }) => { const [id] = positionals; const { label } = values; + if (values["legacy-items-zone"]) { + throw new CommandError("Group fields cannot be added to the legacy items zone."); + } + const { fields, fieldId, save } = await getNewFieldTarget(id, values); const field: GroupModel = { diff --git a/src/commands/slice-view.ts b/src/commands/slice-view.ts index c905217e..d8bfafec 100644 --- a/src/commands/slice-view.ts +++ b/src/commands/slice-view.ts @@ -33,5 +33,10 @@ export default createCommand(config, async ({ positionals, values }) => { console.info(""); console.info(`${variation.id}:`); console.info(formatFieldTable(variation.primary ?? {})); + if (variation.items && Object.keys(variation.items).length > 0) { + console.info(""); + console.info(`${variation.id} (legacy items zone):`); + console.info(formatFieldTable(variation.items)); + } } }); diff --git a/src/fields.ts b/src/fields.ts index 44686516..85fb86ce 100644 --- a/src/fields.ts +++ b/src/fields.ts @@ -1,4 +1,4 @@ -import type { DynamicWidgetModel } from "@prismicio/types-internal"; +import type { DynamicWidgetModel, SharedSliceModel } from "@prismicio/types-internal"; import { getAdapter } from "./adapters"; import { CommandError, exactlyOneOption, type CommandConfig } from "./lib/command"; @@ -6,6 +6,7 @@ import type { ContentRelationshipFieldSelection } from "./lib/prismic/models"; import { FieldExistsError, FieldNotFoundError, + SliceItemsNotFoundError, resolveCustomTypeFieldContainer, resolveContentRelationshipFieldSelection, resolveSliceFieldContainer, @@ -26,6 +27,12 @@ export const TARGET_OPTIONS = { description: 'Slice variation ID (default: "default")', dependsOn: "to-slice", }, + "legacy-items-zone": { + type: "boolean", + description: + "Edit the legacy repeatable zone of a slice upgraded from the Legacy Builder. New slices use a repeatable group instead.", + dependsOn: "to-slice", + }, tab: { type: "string", description: 'Content type tab name (default: "Main")', @@ -47,6 +54,12 @@ export const SOURCE_OPTIONS = { description: 'Slice variation ID (default: "default")', dependsOn: "from-slice", }, + "legacy-items-zone": { + type: "boolean", + description: + "Edit the legacy repeatable zone of a slice upgraded from the Legacy Builder. New slices use a repeatable group instead.", + dependsOn: "from-slice", + }, } satisfies CommandConfig["options"]; type ResolvedFieldTarget = { @@ -64,13 +77,14 @@ export async function getFieldReorderTargets( "from-slice"?: string; "from-type"?: string; variation?: string; + "legacy-items-zone"?: boolean; }, ): Promise<{ source: ResolvedFieldContainer; anchor: ResolvedFieldContainer; save: () => Promise; }> { - const { variation = "default" } = values; + const { variation = "default", "legacy-items-zone": items } = values; const { key, value } = exactlyOneOption(values, ["from-slice", "from-type"]); const adapter = await getAdapter(); @@ -79,8 +93,8 @@ export async function getFieldReorderTargets( let save: () => Promise; if (key === "from-slice") { const { model } = await adapter.getSlice(value); - source = resolveSliceFieldContainer(sourcePath, model, variation); - anchor = resolveSliceFieldContainer(anchorPath, model, variation); + source = resolveSliceFields(sourcePath, model, variation, items); + anchor = resolveSliceFields(anchorPath, model, variation, items); save = async () => { await adapter.updateSlice(model); await adapter.generateTypes(); @@ -107,6 +121,7 @@ export async function getNewFieldTarget( "to-slice"?: string; "to-type"?: string; variation?: string; + "legacy-items-zone"?: boolean; tab?: string; }, ): Promise { @@ -114,7 +129,7 @@ export async function getNewFieldTarget( const { key, value } = exactlyOneOption(values, ["to-slice", "to-type"]); const target = key === "to-slice" - ? await getSliceFieldTarget(path, value, variation) + ? await getSliceFieldTarget(path, value, variation, values["legacy-items-zone"]) : await getCustomTypeFieldTarget(path, value, tab); if (target.fieldId in target.fields) throw new FieldExistsError(path); return target; @@ -126,13 +141,14 @@ export async function getExistingField( "from-slice"?: string; "from-type"?: string; variation?: string; + "legacy-items-zone"?: boolean; }, ): Promise { const { variation = "default" } = values; const { key, value } = exactlyOneOption(values, ["from-slice", "from-type"]); const target = key === "from-slice" - ? await getSliceFieldTarget(path, value, variation) + ? await getSliceFieldTarget(path, value, variation, values["legacy-items-zone"]) : await getCustomTypeFieldTarget(path, value); const field = target.fields[target.fieldId]; if (!field) throw new FieldNotFoundError(path); @@ -157,10 +173,11 @@ async function getSliceFieldTarget( path: string, sliceId: string, variationId: string, + items?: boolean, ): Promise { const adapter = await getAdapter(); const { model } = await adapter.getSlice(sliceId); - const fieldContainer = resolveSliceFieldContainer(path, model, variationId); + const fieldContainer = resolveSliceFields(path, model, variationId, items); return { ...fieldContainer, @@ -171,6 +188,24 @@ async function getSliceFieldTarget( }; } +function resolveSliceFields( + path: string, + slice: SharedSliceModel, + variationId: string, + items?: boolean, +): ResolvedFieldContainer { + try { + return resolveSliceFieldContainer(path, slice, variationId, items); + } catch (error) { + if (!(error instanceof SliceItemsNotFoundError)) throw error; + let target = `--to-slice ${slice.id}`; + if (variationId !== "default") target += ` --variation ${variationId}`; + throw new CommandError( + `${error.message}\nHint: prismic field add group items ${target}\nThen add fields to the group: prismic field add text items. ${target}`, + ); + } +} + async function getCustomTypeFieldTarget( path: string, customTypeId: string, diff --git a/src/index.ts b/src/index.ts index 97a0155f..c4590219 100644 --- a/src/index.ts +++ b/src/index.ts @@ -26,6 +26,7 @@ import { FieldExistsError, FieldNotFoundError, FieldSelectionError, + SliceItemsNotFoundError, SliceVariationNotFoundError, TabNotFoundError, UnsupportedNestedFieldError, @@ -71,6 +72,7 @@ const KNOWN_ERRORS = [ FieldSelectionError, TabNotFoundError, SliceVariationNotFoundError, + SliceItemsNotFoundError, UnsupportedFileTypeError, NoSupportedFrameworkError, ModelExistsError, diff --git a/src/lib/prismic/models.ts b/src/lib/prismic/models.ts index 7e8e9128..0167ebbb 100644 --- a/src/lib/prismic/models.ts +++ b/src/lib/prismic/models.ts @@ -167,9 +167,16 @@ export function resolveSliceFieldContainer( path: string, slice: SharedSliceModel, variationId: string, + items = false, ): { fields: Fields; fieldId: string } { const variation = slice.variations.find((variation) => variation.id === variationId); if (!variation) throw new SliceVariationNotFoundError(variationId, slice.id); + if (items) { + if (!variation.items || Object.keys(variation.items).length === 0) { + throw new SliceItemsNotFoundError(variationId, slice.id); + } + return resolveNestedFieldContainer(path, variation.items); + } variation.primary ??= {}; return resolveNestedFieldContainer(path, variation.primary); } @@ -378,3 +385,13 @@ export class SliceVariationNotFoundError extends Error { super(`Variation "${id}" does not exist on slice "${sliceId}".`); } } + +export class SliceItemsNotFoundError extends Error { + name = "SliceItemsNotFoundError"; + + constructor(variationId: string, sliceId: string) { + super( + `Variation "${variationId}" of slice "${sliceId}" has no legacy items zone. To add repeatable fields, add a repeatable group to the slice.`, + ); + } +} diff --git a/test/field-add-group.test.ts b/test/field-add-group.test.ts index 0afbdf58..6f9c3091 100644 --- a/test/field-add-group.test.ts +++ b/test/field-add-group.test.ts @@ -111,3 +111,20 @@ it("errors when dot syntax targets a field that does not support nesting", async expect(exitCode).toBe(1); expect(stderr).toContain('Field "my_text" does not support nested fields.'); }); + +it("errors for the legacy items zone", async ({ expect, prismic, project }) => { + const slice = buildSlice(); + slice.variations[0].items = { label: { type: "Text", config: { label: "Label" } } }; + await writeLocalSlice(project, slice); + + const { stderr, exitCode } = await prismic("field", [ + "add", + "group", + "my_group", + "--to-slice", + slice.id, + "--legacy-items-zone", + ]); + expect(exitCode).toBe(1); + expect(stderr).toContain("Group fields cannot be added to the legacy items zone."); +}); diff --git a/test/field-add-text.test.ts b/test/field-add-text.test.ts index a99f2de3..f59c48ab 100644 --- a/test/field-add-text.test.ts +++ b/test/field-add-text.test.ts @@ -83,3 +83,43 @@ it("errors for an unknown slice", async ({ expect, prismic }) => { expect(exitCode).toBe(1); expect(stderr).toContain('Slice "CallToAction" does not exist.'); }); + +it("adds a text field to a slice's legacy items zone", async ({ expect, prismic, project }) => { + const slice = buildSlice(); + slice.variations[0].items = { label: { type: "Text", config: { label: "Label" } } }; + await writeLocalSlice(project, slice); + + const { stderr, exitCode } = await prismic("field", [ + "add", + "text", + "subtitle", + "--to-slice", + slice.id, + "--legacy-items-zone", + ]); + expect(exitCode, stderr).toBe(0); + + const updated = await readLocalSlice(project, slice.id); + expect(updated!.variations[0].items!.subtitle).toMatchObject({ type: "Text" }); + expect(updated!.variations[0].primary).toEqual({}); +}); + +it("errors when the variation has no legacy items zone", async ({ expect, prismic, project }) => { + const slice = buildSlice(); + await writeLocalSlice(project, slice); + + const { stderr, exitCode } = await prismic("field", [ + "add", + "text", + "subtitle", + "--to-slice", + slice.id, + "--legacy-items-zone", + ]); + expect(exitCode).toBe(1); + expect(stderr).toContain( + `Variation "default" of slice "${slice.id}" has no legacy items zone. To add repeatable fields, add a repeatable group to the slice.`, + ); + expect(stderr).toContain(`Hint: prismic field add group items --to-slice ${slice.id}`); + expect(stderr).toContain(`prismic field add text items. --to-slice ${slice.id}`); +}); diff --git a/test/field-edit.test.ts b/test/field-edit.test.ts index 7ae78f61..58a56054 100644 --- a/test/field-edit.test.ts +++ b/test/field-edit.test.ts @@ -337,3 +337,23 @@ it("edits link field options", async ({ expect, prismic, project }) => { config: { allowTargetBlank: true }, }); }); + +it("edits a field in a slice's legacy items zone", async ({ expect, prismic, project }) => { + const slice = buildSlice(); + slice.variations[0].items = { label: { type: "Text", config: { label: "Old Label" } } }; + await writeLocalSlice(project, slice); + + const { stderr, exitCode } = await prismic("field", [ + "edit", + "label", + "--from-slice", + slice.id, + "--legacy-items-zone", + "--label", + "New Label", + ]); + expect(exitCode, stderr).toBe(0); + + const updated = await readLocalSlice(project, slice.id); + expect(updated!.variations[0].items!.label).toMatchObject({ config: { label: "New Label" } }); +}); diff --git a/test/field-remove.test.ts b/test/field-remove.test.ts index 1646c0d8..ddec98fc 100644 --- a/test/field-remove.test.ts +++ b/test/field-remove.test.ts @@ -110,3 +110,21 @@ it("removes a nested field using dot notation", async ({ expect, prismic, projec expect(group.config?.fields).toMatchObject({}); expect(group.config?.fields?.subtitle).toBeUndefined(); }); + +it("removes a field from a slice's legacy items zone", async ({ expect, prismic, project }) => { + const slice = buildSlice(); + slice.variations[0].items = { label: { type: "Text", config: { label: "Label" } } }; + await writeLocalSlice(project, slice); + + const { stderr, exitCode } = await prismic("field", [ + "remove", + "label", + "--from-slice", + slice.id, + "--legacy-items-zone", + ]); + expect(exitCode, stderr).toBe(0); + + const updated = await readLocalSlice(project, slice.id); + expect(updated!.variations[0].items).toEqual({}); +}); diff --git a/test/field-reorder.test.ts b/test/field-reorder.test.ts index 24630766..604ba963 100644 --- a/test/field-reorder.test.ts +++ b/test/field-reorder.test.ts @@ -133,3 +133,26 @@ it("errors when the field does not exist", async ({ expect, prismic, project }) expect(exitCode).toBe(1); expect(stderr).toContain('"missing"'); }); + +it("reorders a field in a slice's legacy items zone", async ({ expect, prismic, project }) => { + const slice = buildSlice(); + slice.variations[0].items = { + field_a: { type: "Boolean", config: { label: "A" } }, + field_b: { type: "Boolean", config: { label: "B" } }, + }; + await writeLocalSlice(project, slice); + + const { stderr, exitCode } = await prismic("field", [ + "reorder", + "field_a", + "--after", + "field_b", + "--from-slice", + slice.id, + "--legacy-items-zone", + ]); + expect(exitCode, stderr).toBe(0); + + const updated = await readLocalSlice(project, slice.id); + expect(Object.keys(updated!.variations[0].items!)).toEqual(["field_b", "field_a"]); +}); diff --git a/test/slice-view.test.ts b/test/slice-view.test.ts index 2c360704..cf6b55cc 100644 --- a/test/slice-view.test.ts +++ b/test/slice-view.test.ts @@ -76,3 +76,13 @@ it("points at the upgrade for a legacy slice", async ({ expect, prismic, project `"hero" is a legacy slice in "${customType.id}". Upgrade it first: \`prismic slice upgrade-legacy hero --from ${customType.id}\`.`, ); }); + +it("shows a variation's legacy items zone fields", async ({ expect, prismic, project }) => { + const slice = buildSlice(); + slice.variations[0].items = { label: { type: "Text", config: { label: "Label" } } }; + await writeLocalSlice(project, slice); + + const { stdout, stderr, exitCode } = await prismic("slice", ["view", slice.id]); + expect(exitCode, stderr).toBe(0); + expect(stdout).toMatch(/default \(legacy items zone\):\n\s+label\s+Text\s+Label/); +});