From 6043d4d72ff97b0cec4b4b2175bcedd39a8e26c8 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 21:01:58 +0000 Subject: [PATCH 1/5] feat: let slice field commands target the items zone Slices upgraded from the Legacy Builder keep their repeatable fields in the deprecated items zone. The field commands could not reach them. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01L1dGD8SwrKjWRKbjezoBWe --- src/commands/field-add-group.ts | 4 ++- src/commands/slice-view.ts | 5 +++ src/fields.ts | 34 +++++++++++++++++--- src/index.ts | 2 ++ src/lib/prismic/models.ts | 18 +++++++++-- test/field-add-group.test.ts | 18 +++++++++++ test/field-add-text.test.ts | 57 +++++++++++++++++++++++++++++++++ test/field-edit.test.ts | 21 ++++++++++++ test/field-remove.test.ts | 19 +++++++++++ test/field-reorder.test.ts | 24 ++++++++++++++ test/slice-view.test.ts | 10 ++++++ 11 files changed, 204 insertions(+), 8 deletions(-) diff --git a/src/commands/field-add-group.ts b/src/commands/field-add-group.ts index 8c9351e8..d1c49434 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,8 @@ export default createCommand(config, async ({ positionals, values }) => { const [id] = positionals; const { label } = values; + if (values.zone === "items") throw new CommandError("Group fields cannot be added to items."); + 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..740c76ed 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} items:`); + console.info(formatFieldTable(variation.items)); + } } }); diff --git a/src/fields.ts b/src/fields.ts index 44686516..58613468 100644 --- a/src/fields.ts +++ b/src/fields.ts @@ -26,6 +26,12 @@ export const TARGET_OPTIONS = { description: 'Slice variation ID (default: "default")', dependsOn: "to-slice", }, + zone: { + type: "string", + description: + 'Slice variation zone: "primary" or "items", the legacy repeatable zone of upgraded slices (default: "primary")', + dependsOn: "to-slice", + }, tab: { type: "string", description: 'Content type tab name (default: "Main")', @@ -47,6 +53,12 @@ export const SOURCE_OPTIONS = { description: 'Slice variation ID (default: "default")', dependsOn: "from-slice", }, + zone: { + type: "string", + description: + 'Slice variation zone: "primary" or "items", the legacy repeatable zone of upgraded slices (default: "primary")', + dependsOn: "from-slice", + }, } satisfies CommandConfig["options"]; type ResolvedFieldTarget = { @@ -64,6 +76,7 @@ export async function getFieldReorderTargets( "from-slice"?: string; "from-type"?: string; variation?: string; + zone?: string; }, ): Promise<{ source: ResolvedFieldContainer; @@ -71,6 +84,7 @@ export async function getFieldReorderTargets( save: () => Promise; }> { const { variation = "default" } = values; + const zone = parseZone(values.zone); 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 = resolveSliceFieldContainer(sourcePath, model, variation, zone); + anchor = resolveSliceFieldContainer(anchorPath, model, variation, zone); 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; + zone?: string; 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.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; + zone?: string; }, ): 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.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, + zone?: string, ): Promise { const adapter = await getAdapter(); const { model } = await adapter.getSlice(sliceId); - const fieldContainer = resolveSliceFieldContainer(path, model, variationId); + const fieldContainer = resolveSliceFieldContainer(path, model, variationId, parseZone(zone)); return { ...fieldContainer, @@ -189,6 +206,13 @@ async function getCustomTypeFieldTarget( }; } +function parseZone(zone = "primary"): "primary" | "items" { + if (zone !== "primary" && zone !== "items") { + throw new CommandError('--zone must be "primary" or "items".'); + } + return zone; +} + export function parseNumber(value: string | undefined, optionName: string): number | undefined { if (value === undefined) return undefined; const number = Number(value); diff --git a/src/index.ts b/src/index.ts index 56451e55..4c152ece 100644 --- a/src/index.ts +++ b/src/index.ts @@ -26,6 +26,7 @@ import { FieldExistsError, FieldNotFoundError, FieldSelectionError, + SliceItemsNotFoundError, SliceVariationNotFoundError, TabNotFoundError, UnsupportedNestedFieldError, @@ -70,6 +71,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 694903f9..9d2ba4b5 100644 --- a/src/lib/prismic/models.ts +++ b/src/lib/prismic/models.ts @@ -167,11 +167,15 @@ export function resolveSliceFieldContainer( path: string, slice: SharedSliceModel, variationId: string, + zone: "primary" | "items", ): { fields: Fields; fieldId: string } { const variation = slice.variations.find((variation) => variation.id === variationId); if (!variation) throw new SliceVariationNotFoundError(variationId, slice.id); - variation.primary ??= {}; - return resolveNestedFieldContainer(path, variation.primary); + if (zone === "primary") return resolveNestedFieldContainer(path, (variation.primary ??= {})); + if (!variation.items || Object.keys(variation.items).length === 0) { + throw new SliceItemsNotFoundError(variationId, slice.id); + } + return resolveNestedFieldContainer(path, variation.items); } export async function getRemoteModels(config: CustomTypesConfig): Promise { @@ -378,3 +382,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 items. Add fields to primary instead.`, + ); + } +} diff --git a/test/field-add-group.test.ts b/test/field-add-group.test.ts index 0afbdf58..5d408f94 100644 --- a/test/field-add-group.test.ts +++ b/test/field-add-group.test.ts @@ -111,3 +111,21 @@ 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 when adding a group to a slice's items", 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, + "--zone", + "items", + ]); + expect(exitCode).toBe(1); + expect(stderr).toContain("Group fields cannot be added to items."); +}); diff --git a/test/field-add-text.test.ts b/test/field-add-text.test.ts index a99f2de3..d9e140bb 100644 --- a/test/field-add-text.test.ts +++ b/test/field-add-text.test.ts @@ -83,3 +83,60 @@ 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 items", 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, + "--zone", + "items", + ]); + 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 for an unknown 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, + "--zone", + "repeat", + ]); + expect(exitCode).toBe(1); + expect(stderr).toContain('--zone must be "primary" or "items".'); +}); + +it("errors when the variation has no items", async ({ expect, prismic, project }) => { + const slice = buildSlice(); + await writeLocalSlice(project, slice); + + const { stderr, exitCode } = await prismic("field", [ + "add", + "text", + "subtitle", + "--to-slice", + slice.id, + "--zone", + "items", + ]); + expect(exitCode).toBe(1); + expect(stderr).toContain( + `Variation "default" of slice "${slice.id}" has no items. Add fields to primary instead.`, + ); +}); diff --git a/test/field-edit.test.ts b/test/field-edit.test.ts index 7ae78f61..ddd2c2e4 100644 --- a/test/field-edit.test.ts +++ b/test/field-edit.test.ts @@ -337,3 +337,24 @@ it("edits link field options", async ({ expect, prismic, project }) => { config: { allowTargetBlank: true }, }); }); + +it("edits a field in a slice's items", 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, + "--zone", + "items", + "--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..3ff41bdb 100644 --- a/test/field-remove.test.ts +++ b/test/field-remove.test.ts @@ -110,3 +110,22 @@ 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 items", 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, + "--zone", + "items", + ]); + 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..a9296221 100644 --- a/test/field-reorder.test.ts +++ b/test/field-reorder.test.ts @@ -133,3 +133,27 @@ 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 items", 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, + "--zone", + "items", + ]); + 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..e5c80d34 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 items 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 items:\n\s+label\s+Text\s+Label/); +}); From ca9f53db67c3e11f5322ca926a86c64cae8ab62d Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 21:10:45 +0000 Subject: [PATCH 2/5] refactor: validate --zone with an enumOption helper Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01L1dGD8SwrKjWRKbjezoBWe --- src/fields.ts | 20 +++++++------------- src/lib/command.ts | 12 ++++++++++++ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/fields.ts b/src/fields.ts index 58613468..4b0b643f 100644 --- a/src/fields.ts +++ b/src/fields.ts @@ -1,7 +1,7 @@ import type { DynamicWidgetModel } from "@prismicio/types-internal"; import { getAdapter } from "./adapters"; -import { CommandError, exactlyOneOption, type CommandConfig } from "./lib/command"; +import { CommandError, enumOption, exactlyOneOption, type CommandConfig } from "./lib/command"; import type { ContentRelationshipFieldSelection } from "./lib/prismic/models"; import { FieldExistsError, @@ -84,7 +84,7 @@ export async function getFieldReorderTargets( save: () => Promise; }> { const { variation = "default" } = values; - const zone = parseZone(values.zone); + const zone = enumOption(values, "zone", ["primary", "items"]) ?? "primary"; const { key, value } = exactlyOneOption(values, ["from-slice", "from-type"]); const adapter = await getAdapter(); @@ -129,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, values.zone) + ? await getSliceFieldTarget(path, value, variation, values) : await getCustomTypeFieldTarget(path, value, tab); if (target.fieldId in target.fields) throw new FieldExistsError(path); return target; @@ -148,7 +148,7 @@ export async function getExistingField( const { key, value } = exactlyOneOption(values, ["from-slice", "from-type"]); const target = key === "from-slice" - ? await getSliceFieldTarget(path, value, variation, values.zone) + ? await getSliceFieldTarget(path, value, variation, values) : await getCustomTypeFieldTarget(path, value); const field = target.fields[target.fieldId]; if (!field) throw new FieldNotFoundError(path); @@ -173,11 +173,12 @@ async function getSliceFieldTarget( path: string, sliceId: string, variationId: string, - zone?: string, + values: { zone?: string }, ): Promise { + const zone = enumOption(values, "zone", ["primary", "items"]) ?? "primary"; const adapter = await getAdapter(); const { model } = await adapter.getSlice(sliceId); - const fieldContainer = resolveSliceFieldContainer(path, model, variationId, parseZone(zone)); + const fieldContainer = resolveSliceFieldContainer(path, model, variationId, zone); return { ...fieldContainer, @@ -206,13 +207,6 @@ async function getCustomTypeFieldTarget( }; } -function parseZone(zone = "primary"): "primary" | "items" { - if (zone !== "primary" && zone !== "items") { - throw new CommandError('--zone must be "primary" or "items".'); - } - return zone; -} - export function parseNumber(value: string | undefined, optionName: string): number | undefined { if (value === undefined) return undefined; const number = Number(value); diff --git a/src/lib/command.ts b/src/lib/command.ts index 15f81603..4172bb6b 100644 --- a/src/lib/command.ts +++ b/src/lib/command.ts @@ -239,6 +239,18 @@ export function exclusiveOptions>( if (provided.length > 1) throw new CommandError(`Only one of ${list} can be specified.`); } +export function enumOption, const C extends readonly string[]>( + values: T, + name: keyof T, + choices: C, +): C[number] | undefined { + const value = values[name]; + if (value === undefined || choices.includes(value as string)) + return value as C[number] | undefined; + const list = choices.map((choice) => `"${choice}"`).join(" or "); + throw new CommandError(`--${String(name)} must be ${list}.`); +} + export class CommandError extends Error { name = "CommandError"; } From 48859ac3615a7fdc13e81d61f94df0be505b180f Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 21:17:57 +0000 Subject: [PATCH 3/5] refactor: replace --zone with --legacy-items-zone Slice field commands now take a boolean --legacy-items-zone flag instead of --zone. Without the flag, commands edit the slice fields as on main. Remove the enumOption helper because nothing else uses it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01L1dGD8SwrKjWRKbjezoBWe --- src/commands/field-add-group.ts | 4 +++- src/commands/slice-view.ts | 2 +- src/fields.ts | 36 ++++++++++++++++----------------- src/lib/command.ts | 12 ----------- src/lib/prismic/models.ts | 15 ++++++++------ test/field-add-group.test.ts | 7 +++---- test/field-add-text.test.ts | 29 +++++--------------------- test/field-edit.test.ts | 5 ++--- test/field-remove.test.ts | 5 ++--- test/field-reorder.test.ts | 5 ++--- test/slice-view.test.ts | 4 ++-- 11 files changed, 46 insertions(+), 78 deletions(-) diff --git a/src/commands/field-add-group.ts b/src/commands/field-add-group.ts index d1c49434..e3309489 100644 --- a/src/commands/field-add-group.ts +++ b/src/commands/field-add-group.ts @@ -25,7 +25,9 @@ export default createCommand(config, async ({ positionals, values }) => { const [id] = positionals; const { label } = values; - if (values.zone === "items") throw new CommandError("Group fields cannot be added to items."); + 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); diff --git a/src/commands/slice-view.ts b/src/commands/slice-view.ts index 740c76ed..d8bfafec 100644 --- a/src/commands/slice-view.ts +++ b/src/commands/slice-view.ts @@ -35,7 +35,7 @@ export default createCommand(config, async ({ positionals, values }) => { console.info(formatFieldTable(variation.primary ?? {})); if (variation.items && Object.keys(variation.items).length > 0) { console.info(""); - console.info(`${variation.id} items:`); + console.info(`${variation.id} (legacy items zone):`); console.info(formatFieldTable(variation.items)); } } diff --git a/src/fields.ts b/src/fields.ts index 4b0b643f..b625513b 100644 --- a/src/fields.ts +++ b/src/fields.ts @@ -1,7 +1,7 @@ import type { DynamicWidgetModel } from "@prismicio/types-internal"; import { getAdapter } from "./adapters"; -import { CommandError, enumOption, exactlyOneOption, type CommandConfig } from "./lib/command"; +import { CommandError, exactlyOneOption, type CommandConfig } from "./lib/command"; import type { ContentRelationshipFieldSelection } from "./lib/prismic/models"; import { FieldExistsError, @@ -26,10 +26,10 @@ export const TARGET_OPTIONS = { description: 'Slice variation ID (default: "default")', dependsOn: "to-slice", }, - zone: { - type: "string", + "legacy-items-zone": { + type: "boolean", description: - 'Slice variation zone: "primary" or "items", the legacy repeatable zone of upgraded slices (default: "primary")', + "Edit the legacy repeatable zone of a slice upgraded from the Legacy Builder. New slices use a repeatable group instead.", dependsOn: "to-slice", }, tab: { @@ -53,10 +53,10 @@ export const SOURCE_OPTIONS = { description: 'Slice variation ID (default: "default")', dependsOn: "from-slice", }, - zone: { - type: "string", + "legacy-items-zone": { + type: "boolean", description: - 'Slice variation zone: "primary" or "items", the legacy repeatable zone of upgraded slices (default: "primary")', + "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"]; @@ -76,15 +76,14 @@ export async function getFieldReorderTargets( "from-slice"?: string; "from-type"?: string; variation?: string; - zone?: string; + "legacy-items-zone"?: boolean; }, ): Promise<{ source: ResolvedFieldContainer; anchor: ResolvedFieldContainer; save: () => Promise; }> { - const { variation = "default" } = values; - const zone = enumOption(values, "zone", ["primary", "items"]) ?? "primary"; + const { variation = "default", "legacy-items-zone": items } = values; const { key, value } = exactlyOneOption(values, ["from-slice", "from-type"]); const adapter = await getAdapter(); @@ -93,8 +92,8 @@ export async function getFieldReorderTargets( let save: () => Promise; if (key === "from-slice") { const { model } = await adapter.getSlice(value); - source = resolveSliceFieldContainer(sourcePath, model, variation, zone); - anchor = resolveSliceFieldContainer(anchorPath, model, variation, zone); + source = resolveSliceFieldContainer(sourcePath, model, variation, items); + anchor = resolveSliceFieldContainer(anchorPath, model, variation, items); save = async () => { await adapter.updateSlice(model); await adapter.generateTypes(); @@ -121,7 +120,7 @@ export async function getNewFieldTarget( "to-slice"?: string; "to-type"?: string; variation?: string; - zone?: string; + "legacy-items-zone"?: boolean; tab?: string; }, ): Promise { @@ -129,7 +128,7 @@ export async function getNewFieldTarget( const { key, value } = exactlyOneOption(values, ["to-slice", "to-type"]); const target = key === "to-slice" - ? await getSliceFieldTarget(path, value, variation, values) + ? 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; @@ -141,14 +140,14 @@ export async function getExistingField( "from-slice"?: string; "from-type"?: string; variation?: string; - zone?: 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, values) + ? 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); @@ -173,12 +172,11 @@ async function getSliceFieldTarget( path: string, sliceId: string, variationId: string, - values: { zone?: string }, + items?: boolean, ): Promise { - const zone = enumOption(values, "zone", ["primary", "items"]) ?? "primary"; const adapter = await getAdapter(); const { model } = await adapter.getSlice(sliceId); - const fieldContainer = resolveSliceFieldContainer(path, model, variationId, zone); + const fieldContainer = resolveSliceFieldContainer(path, model, variationId, items); return { ...fieldContainer, diff --git a/src/lib/command.ts b/src/lib/command.ts index 4172bb6b..15f81603 100644 --- a/src/lib/command.ts +++ b/src/lib/command.ts @@ -239,18 +239,6 @@ export function exclusiveOptions>( if (provided.length > 1) throw new CommandError(`Only one of ${list} can be specified.`); } -export function enumOption, const C extends readonly string[]>( - values: T, - name: keyof T, - choices: C, -): C[number] | undefined { - const value = values[name]; - if (value === undefined || choices.includes(value as string)) - return value as C[number] | undefined; - const list = choices.map((choice) => `"${choice}"`).join(" or "); - throw new CommandError(`--${String(name)} must be ${list}.`); -} - export class CommandError extends Error { name = "CommandError"; } diff --git a/src/lib/prismic/models.ts b/src/lib/prismic/models.ts index 9d2ba4b5..560d11db 100644 --- a/src/lib/prismic/models.ts +++ b/src/lib/prismic/models.ts @@ -167,15 +167,18 @@ export function resolveSliceFieldContainer( path: string, slice: SharedSliceModel, variationId: string, - zone: "primary" | "items", + items = false, ): { fields: Fields; fieldId: string } { const variation = slice.variations.find((variation) => variation.id === variationId); if (!variation) throw new SliceVariationNotFoundError(variationId, slice.id); - if (zone === "primary") return resolveNestedFieldContainer(path, (variation.primary ??= {})); - if (!variation.items || Object.keys(variation.items).length === 0) { - throw new SliceItemsNotFoundError(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); } - return resolveNestedFieldContainer(path, variation.items); + variation.primary ??= {}; + return resolveNestedFieldContainer(path, variation.primary); } export async function getRemoteModels(config: CustomTypesConfig): Promise { @@ -388,7 +391,7 @@ export class SliceItemsNotFoundError extends Error { constructor(variationId: string, sliceId: string) { super( - `Variation "${variationId}" of slice "${sliceId}" has no items. Add fields to primary instead.`, + `Variation "${variationId}" of slice "${sliceId}" has no legacy items zone. Add fields to the slice instead.`, ); } } diff --git a/test/field-add-group.test.ts b/test/field-add-group.test.ts index 5d408f94..6f9c3091 100644 --- a/test/field-add-group.test.ts +++ b/test/field-add-group.test.ts @@ -112,7 +112,7 @@ it("errors when dot syntax targets a field that does not support nesting", async expect(stderr).toContain('Field "my_text" does not support nested fields.'); }); -it("errors when adding a group to a slice's items", async ({ expect, prismic, project }) => { +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); @@ -123,9 +123,8 @@ it("errors when adding a group to a slice's items", async ({ expect, prismic, pr "my_group", "--to-slice", slice.id, - "--zone", - "items", + "--legacy-items-zone", ]); expect(exitCode).toBe(1); - expect(stderr).toContain("Group fields cannot be added to items."); + 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 d9e140bb..abee21a7 100644 --- a/test/field-add-text.test.ts +++ b/test/field-add-text.test.ts @@ -84,7 +84,7 @@ it("errors for an unknown slice", async ({ expect, prismic }) => { expect(stderr).toContain('Slice "CallToAction" does not exist.'); }); -it("adds a text field to a slice's items", async ({ expect, prismic, project }) => { +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); @@ -95,8 +95,7 @@ it("adds a text field to a slice's items", async ({ expect, prismic, project }) "subtitle", "--to-slice", slice.id, - "--zone", - "items", + "--legacy-items-zone", ]); expect(exitCode, stderr).toBe(0); @@ -105,7 +104,7 @@ it("adds a text field to a slice's items", async ({ expect, prismic, project }) expect(updated!.variations[0].primary).toEqual({}); }); -it("errors for an unknown zone", async ({ expect, prismic, project }) => { +it("errors when the variation has no legacy items zone", async ({ expect, prismic, project }) => { const slice = buildSlice(); await writeLocalSlice(project, slice); @@ -115,28 +114,10 @@ it("errors for an unknown zone", async ({ expect, prismic, project }) => { "subtitle", "--to-slice", slice.id, - "--zone", - "repeat", - ]); - expect(exitCode).toBe(1); - expect(stderr).toContain('--zone must be "primary" or "items".'); -}); - -it("errors when the variation has no items", async ({ expect, prismic, project }) => { - const slice = buildSlice(); - await writeLocalSlice(project, slice); - - const { stderr, exitCode } = await prismic("field", [ - "add", - "text", - "subtitle", - "--to-slice", - slice.id, - "--zone", - "items", + "--legacy-items-zone", ]); expect(exitCode).toBe(1); expect(stderr).toContain( - `Variation "default" of slice "${slice.id}" has no items. Add fields to primary instead.`, + `Variation "default" of slice "${slice.id}" has no legacy items zone. Add fields to the slice instead.`, ); }); diff --git a/test/field-edit.test.ts b/test/field-edit.test.ts index ddd2c2e4..58a56054 100644 --- a/test/field-edit.test.ts +++ b/test/field-edit.test.ts @@ -338,7 +338,7 @@ it("edits link field options", async ({ expect, prismic, project }) => { }); }); -it("edits a field in a slice's items", async ({ expect, prismic, project }) => { +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); @@ -348,8 +348,7 @@ it("edits a field in a slice's items", async ({ expect, prismic, project }) => { "label", "--from-slice", slice.id, - "--zone", - "items", + "--legacy-items-zone", "--label", "New Label", ]); diff --git a/test/field-remove.test.ts b/test/field-remove.test.ts index 3ff41bdb..ddec98fc 100644 --- a/test/field-remove.test.ts +++ b/test/field-remove.test.ts @@ -111,7 +111,7 @@ it("removes a nested field using dot notation", async ({ expect, prismic, projec expect(group.config?.fields?.subtitle).toBeUndefined(); }); -it("removes a field from a slice's items", async ({ expect, prismic, project }) => { +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); @@ -121,8 +121,7 @@ it("removes a field from a slice's items", async ({ expect, prismic, project }) "label", "--from-slice", slice.id, - "--zone", - "items", + "--legacy-items-zone", ]); expect(exitCode, stderr).toBe(0); diff --git a/test/field-reorder.test.ts b/test/field-reorder.test.ts index a9296221..604ba963 100644 --- a/test/field-reorder.test.ts +++ b/test/field-reorder.test.ts @@ -134,7 +134,7 @@ it("errors when the field does not exist", async ({ expect, prismic, project }) expect(stderr).toContain('"missing"'); }); -it("reorders a field in a slice's items", async ({ expect, prismic, project }) => { +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" } }, @@ -149,8 +149,7 @@ it("reorders a field in a slice's items", async ({ expect, prismic, project }) = "field_b", "--from-slice", slice.id, - "--zone", - "items", + "--legacy-items-zone", ]); expect(exitCode, stderr).toBe(0); diff --git a/test/slice-view.test.ts b/test/slice-view.test.ts index e5c80d34..cf6b55cc 100644 --- a/test/slice-view.test.ts +++ b/test/slice-view.test.ts @@ -77,12 +77,12 @@ it("points at the upgrade for a legacy slice", async ({ expect, prismic, project ); }); -it("shows a variation's items fields", async ({ expect, prismic, project }) => { +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 items:\n\s+label\s+Text\s+Label/); + expect(stdout).toMatch(/default \(legacy items zone\):\n\s+label\s+Text\s+Label/); }); From 40b424c2f95b48abb51ccbce2cd085778bf09f65 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 01:03:03 +0000 Subject: [PATCH 4/5] fix: point legacy items zone error to repeatable groups The error for a missing legacy items zone told users to add fields to the slice. That did not tell them how to keep repeatable content. The message now tells them to add a repeatable group to the slice. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01L1dGD8k3F268SphC2JdpP7 --- src/lib/prismic/models.ts | 2 +- test/field-add-text.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/prismic/models.ts b/src/lib/prismic/models.ts index 560d11db..e4f8fb3c 100644 --- a/src/lib/prismic/models.ts +++ b/src/lib/prismic/models.ts @@ -391,7 +391,7 @@ export class SliceItemsNotFoundError extends Error { constructor(variationId: string, sliceId: string) { super( - `Variation "${variationId}" of slice "${sliceId}" has no legacy items zone. Add fields to the slice instead.`, + `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-text.test.ts b/test/field-add-text.test.ts index abee21a7..e2eaefd8 100644 --- a/test/field-add-text.test.ts +++ b/test/field-add-text.test.ts @@ -118,6 +118,6 @@ it("errors when the variation has no legacy items zone", async ({ expect, prismi ]); expect(exitCode).toBe(1); expect(stderr).toContain( - `Variation "default" of slice "${slice.id}" has no legacy items zone. Add fields to the slice instead.`, + `Variation "default" of slice "${slice.id}" has no legacy items zone. To add repeatable fields, add a repeatable group to the slice.`, ); }); From 6c5d6c31731416ff0501594ac78a57b003092f9a Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 01:04:55 +0000 Subject: [PATCH 5/5] fix: suggest group command when legacy items zone is missing When a field command uses --legacy-items-zone on a slice variation that has no legacy items zone, the error now shows the commands to run. The hint tells the user to add a group to the slice, and then to add fields to that group with dot notation. The slice ID is filled in. The --variation flag is added when the variation is not "default". The hint is in src/fields.ts, which all field commands use to find slice fields. The error class in src/lib stays free of CLI flag names. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01L1dGD8k3F268SphC2JdpP7 --- src/fields.ts | 27 +++++++++++++++++++++++---- test/field-add-text.test.ts | 2 ++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/fields.ts b/src/fields.ts index b625513b..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, @@ -92,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, items); - anchor = resolveSliceFieldContainer(anchorPath, model, variation, items); + source = resolveSliceFields(sourcePath, model, variation, items); + anchor = resolveSliceFields(anchorPath, model, variation, items); save = async () => { await adapter.updateSlice(model); await adapter.generateTypes(); @@ -176,7 +177,7 @@ async function getSliceFieldTarget( ): Promise { const adapter = await getAdapter(); const { model } = await adapter.getSlice(sliceId); - const fieldContainer = resolveSliceFieldContainer(path, model, variationId, items); + const fieldContainer = resolveSliceFields(path, model, variationId, items); return { ...fieldContainer, @@ -187,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/test/field-add-text.test.ts b/test/field-add-text.test.ts index e2eaefd8..f59c48ab 100644 --- a/test/field-add-text.test.ts +++ b/test/field-add-text.test.ts @@ -120,4 +120,6 @@ it("errors when the variation has no legacy items zone", async ({ expect, prismi 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}`); });