-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add prismic slice upgrade-legacy
#388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c43cb6a
feat(slice): add `prismic slice migrate` to convert legacy slices
claude d7e9225
feat(init): turn on the Type Builder for Legacy Builder repositories
claude 03fdb91
refactor(slice): simplify `prismic slice migrate` and init
claude c63bb86
refactor(init): use the repository service to enable the Type Builder
claude 9e6303e
test(evals): check that agents convert legacy slices with the CLI
claude 32704e1
feat: add `slice upgrade-legacy` and show legacy slices across commands
claude ca2f0f7
refactor: simplify legacy slice upgrade code and tests
claude 9bbdc9c
refactor: move the `prismic init` Type Builder change to its own PR
claude 0355bc5
refactor: move `getLegacySlices` into the models module
claude 1ec60ac
feat: show legacy slices in `slice list` and `type view`
claude b372f40
Merge branch 'claude/focused-pascal-2fffvf-legacy' into claude/focuse…
claude 5d5a697
refactor: simplify `slice list --json` and the legacy test fixture
claude 082fe74
Merge branch 'claude/focused-pascal-2fffvf-legacy' into claude/focuse…
claude 0a828f6
test: merge upgrade tests that share setup
claude d19eff7
fix: refuse to upgrade onto another legacy slice in the same zone
claude 72e1936
Merge remote-tracking branch 'origin/main' into claude/focused-pascal…
claude c47782a
fix: word upgrade choices for both people and agents
claude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,237 @@ | ||
| import { isDeepStrictEqual } from "node:util"; | ||
|
|
||
| import type { SharedSliceModel, SharedSliceModelVariation } from "@prismicio/types-internal"; | ||
| import { camelCase, pascalCase, snakeCase } from "change-case"; | ||
|
|
||
| import { getAdapter } from "../adapters"; | ||
| import { CommandError, createCommand, type CommandConfig } from "../lib/command"; | ||
| import { getLegacySlices, type LegacySlice } from "../lib/prismic/models"; | ||
| import { relativePathname } from "../lib/url"; | ||
| import { findProjectRoot } from "../project"; | ||
|
|
||
| const config = { | ||
| name: "prismic slice upgrade-legacy", | ||
| description: ` | ||
| Upgrade a legacy slice from the Legacy Builder. | ||
|
|
||
| The CLI and the Type Builder cannot edit legacy slices. Run | ||
| \`prismic slice list --legacy\` to list them. | ||
|
|
||
| Each legacy slice becomes its own slice unless --to names an existing | ||
| slice, which gets it as a new variation. Legacy slices with the same ID | ||
| in several types are often the same component. Combining them is a content | ||
| modeling decision for the project's owner, since it changes how editors pick | ||
| slices. Get their choice before upgrading them. | ||
|
|
||
| Only local models change. After \`prismic push\`, Prismic returns the | ||
| slice's content in its upgraded shape, so deploy the updated component | ||
| with the push. | ||
| `, | ||
| sections: { | ||
| EXAMPLES: ` | ||
| Upgrade a legacy slice: | ||
| prismic slice upgrade-legacy hero --from blog_post | ||
|
|
||
| Upgrade a legacy slice that is not in the "body" slice zone: | ||
| prismic slice upgrade-legacy hero --from blog_post --slice-zone page_slices | ||
|
|
||
| Add a legacy slice to an existing slice as a new variation: | ||
| prismic slice upgrade-legacy hero --from landing_page --to hero --variation landing | ||
|
|
||
| Merge a legacy slice into an existing variation with the same fields: | ||
| prismic slice upgrade-legacy hero --from landing_page --to hero --variation default | ||
| `, | ||
| }, | ||
| positionals: { | ||
| id: { description: "ID of the legacy slice", required: true }, | ||
| }, | ||
| options: { | ||
| from: { | ||
| type: "string", | ||
| required: true, | ||
| description: "ID of the type that contains the legacy slice", | ||
| }, | ||
| "slice-zone": { | ||
| type: "string", | ||
| description: 'Slice zone field ID (default: "body")', | ||
| }, | ||
| to: { | ||
| type: "string", | ||
| description: | ||
| "ID of the slice to upgrade to, created if missing (default: the legacy slice ID)", | ||
| }, | ||
| variation: { | ||
| type: "string", | ||
| description: | ||
| 'Variation to create, or to merge into if it has the same fields (default: "default" for a new slice, the legacy slice ID otherwise)', | ||
| }, | ||
| }, | ||
| } satisfies CommandConfig; | ||
|
|
||
| export default createCommand(config, async ({ positionals, values }) => { | ||
| const [id] = positionals; | ||
| const { from, "slice-zone": sliceZoneId = "body", to, variation } = values; | ||
|
|
||
| const adapter = await getAdapter(); | ||
| const customTypes = (await adapter.getCustomTypes()).map((customType) => customType.model); | ||
| const slices = (await adapter.getSlices()).map((slice) => slice.model); | ||
| const legacySlices = getLegacySlices(customTypes); | ||
|
|
||
| const customType = customTypes.find((ct) => ct.id === from); | ||
| if (!customType) { | ||
| throw new CommandError(`Type "${from}" not found.`); | ||
| } | ||
| const legacySlice = legacySlices.find( | ||
| (s) => s.customTypeId === from && s.sliceZoneId === sliceZoneId && s.id === id, | ||
| ); | ||
| if (!legacySlice) { | ||
| throw new CommandError( | ||
| `Legacy slice "${id}" not found in the "${sliceZoneId}" slice zone of "${from}". Use --slice-zone to choose the slice zone.`, | ||
| ); | ||
| } | ||
| const legacyPath = `${from}::${sliceZoneId}::${id}`; | ||
|
|
||
| const sliceId = to ?? snakeCase(id); | ||
| const zoneChoice = legacySlice.sliceZone.config?.choices?.[sliceId]; | ||
| if (sliceId !== id && zoneChoice && zoneChoice.type !== "SharedSlice") { | ||
| throw new CommandError( | ||
| `The "${sliceZoneId}" slice zone of "${from}" also has legacy slice "${sliceId}". Upgrade it first.`, | ||
| ); | ||
| } | ||
| let slice = slices.find((s) => s.id === sliceId); | ||
| const variationId = camelCase(variation ?? (slice ? id : "default")); | ||
| const upgraded = toVariation(legacySlice, variationId); | ||
| let summary: string; | ||
| if (slice) { | ||
| if (!to) { | ||
| throw new CommandError(getSliceExistsMessage(slice, legacySlice, upgraded)); | ||
| } | ||
| const target = slice.variations.find((v) => v.id === variationId); | ||
| if (target && !hasSameFields(target, upgraded)) { | ||
| throw new CommandError( | ||
| `Variation "${variationId}" of slice "${sliceId}" has different fields than legacy slice "${id}". Use another ID with --variation.`, | ||
| ); | ||
| } | ||
| if (target) { | ||
| summary = `Merged legacy slice "${id}" into variation "${variationId}" of slice "${sliceId}"`; | ||
| } else { | ||
| slice.variations.push(upgraded); | ||
| summary = `Added legacy slice "${id}" to slice "${sliceId}" as variation "${variationId}"`; | ||
| } | ||
| slice.legacyPaths = { ...slice.legacyPaths, [legacyPath]: variationId }; | ||
| await adapter.updateSlice(slice); | ||
| } else { | ||
| const { model } = legacySlice; | ||
| const name = "fieldset" in model ? model.fieldset : undefined; | ||
| slice = { | ||
| id: sliceId, | ||
| type: "SharedSlice", | ||
| name: pascalCase(to ?? name ?? sliceId), | ||
| legacyPaths: { [legacyPath]: variationId }, | ||
| variations: [upgraded], | ||
| }; | ||
| summary = `Upgraded legacy slice "${id}" to slice "${sliceId}"`; | ||
| await adapter.createSlice(slice); | ||
| } | ||
|
|
||
| replaceChoice(legacySlice, slice.id); | ||
| await adapter.updateCustomType(customType); | ||
| await adapter.generateTypes(); | ||
|
|
||
| const { directory } = await adapter.getSlice(slice.id); | ||
| const componentPath = relativePathname(await findProjectRoot(), directory); | ||
| const remaining = legacySlices.length - 1; | ||
|
|
||
| console.info(summary); | ||
| console.info(`After \`prismic push\`: ${getContentChange(legacySlice, sliceId, variationId)}`); | ||
| console.info( | ||
| `Update the component in ${componentPath}, then deploy it with \`prismic push\`. Documents use the new shape after the next publish.`, | ||
| ); | ||
| if (remaining > 0) { | ||
| console.info( | ||
| `\n${remaining} legacy ${remaining === 1 ? "slice remains" : "slices remain"}. Run \`prismic slice list --legacy\` to list them.`, | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| // Upgrading into an existing slice changes how editors use it, so list the choices instead of picking one. | ||
| function getSliceExistsMessage( | ||
| slice: SharedSliceModel, | ||
| legacySlice: LegacySlice, | ||
| upgraded: SharedSliceModelVariation, | ||
| ) { | ||
| const { id, customTypeId, sliceZoneId } = legacySlice; | ||
| const zoneOption = sliceZoneId === "body" ? "" : ` --slice-zone ${sliceZoneId}`; | ||
| const command = `prismic slice upgrade-legacy ${id} --from ${customTypeId}${zoneOption}`; | ||
| const sameFields = slice.variations.find((v) => hasSameFields(v, upgraded)); | ||
| return [ | ||
| `Slice "${slice.id}" already exists. How do you want to upgrade legacy slice "${id}" of "${customTypeId}"? This changes how editors pick slices, so the project's owner should choose:`, | ||
| ` Add it to slice "${slice.id}" as a new variation: ${command} --to ${slice.id}`, | ||
| ...(sameFields | ||
| ? [ | ||
| ` Merge it into variation "${sameFields.id}", which has the same fields: ${command} --to ${slice.id} --variation ${sameFields.id}`, | ||
| ] | ||
| : []), | ||
| ` Upgrade it to a separate slice: ${command} --to <new-slice-id>`, | ||
| ].join("\n"); | ||
| } | ||
|
|
||
| function getContentChange(legacySlice: LegacySlice, sliceId: string, variationId: string) { | ||
| const { model, id } = legacySlice; | ||
| const changes = []; | ||
| if (sliceId !== id || variationId !== "default") { | ||
| changes.push(`\`slice_type\` becomes "${sliceId}" and \`variation\` becomes "${variationId}".`); | ||
| } | ||
| if (model.type === "Group") changes.push("`slice.value` moves to `slice.items`."); | ||
| else if (model.type !== "Slice") | ||
| changes.push(`\`slice.value\` moves to \`slice.primary.${id}\`.`); | ||
| return changes.join(" ") || "`slice.primary` and `slice.items` keep their shape."; | ||
| } | ||
|
|
||
| // Mirrors Slice Machine's legacy slice upgrader so Prismic reads existing | ||
| // content the same way through `legacyPaths`. | ||
| function toVariation(legacySlice: LegacySlice, id: string): SharedSliceModelVariation { | ||
| const { model } = legacySlice; | ||
| const name = id === "default" ? "Default" : pascalCase(id); | ||
| const variation: SharedSliceModelVariation = { | ||
| id, | ||
| name, | ||
| description: name, | ||
| docURL: "", | ||
| imageUrl: "", | ||
| version: "initial", | ||
| primary: {}, | ||
| items: {}, | ||
| }; | ||
|
|
||
| switch (model.type) { | ||
| case "Slice": | ||
| variation.primary = model["non-repeat"] ?? {}; | ||
| variation.items = model.repeat ?? {}; | ||
| break; | ||
| case "Group": | ||
| variation.items = model.config?.fields ?? {}; | ||
| break; | ||
| default: | ||
| variation.primary = { [legacySlice.id]: model }; | ||
| break; | ||
| } | ||
|
|
||
| return variation; | ||
| } | ||
|
|
||
| function hasSameFields(a: SharedSliceModelVariation, b: SharedSliceModelVariation): boolean { | ||
| return ( | ||
| isDeepStrictEqual(a.primary ?? {}, b.primary ?? {}) && | ||
| isDeepStrictEqual(a.items ?? {}, b.items ?? {}) | ||
| ); | ||
| } | ||
|
|
||
| // Keeps the slice at the same position in the zone. Choice order is slice order. | ||
| function replaceChoice({ id, sliceZone }: LegacySlice, sliceId: string) { | ||
| sliceZone.config!.choices = Object.fromEntries( | ||
| Object.entries(sliceZone.config!.choices!) | ||
| .filter(([key]) => key === id || key !== sliceId) | ||
| .map(([key, choice]) => (key === id ? [sliceId, { type: "SharedSlice" }] : [key, choice])), | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.