feat(codemod): scaffold @payloadcms/codemod for v4 migrations#16385
Merged
feat(codemod): scaffold @payloadcms/codemod for v4 migrations#16385
Conversation
…onfig comment, maintainers)
… import TransformRunResult, simpler globs)
Contributor
📦 esbuild Bundle Analysis for payloadThis analysis was generated by esbuild-bundle-analyzer. 🤖
Largest pathsThese visualization shows top 20 largest paths in the bundle.Meta file: packages/next/meta_index.json, Out file: esbuild/index.js
Meta file: packages/payload/meta_index.json, Out file: esbuild/index.js
Meta file: packages/payload/meta_shared.json, Out file: esbuild/exports/shared.js
Meta file: packages/richtext-lexical/meta_client.json, Out file: esbuild/exports/client_optimized/index.js
Meta file: packages/ui/meta_client.json, Out file: esbuild/exports/client_optimized/index.js
Meta file: packages/ui/meta_shared.json, Out file: esbuild/exports/shared_optimized/index.js
DetailsNext to the size is how much the size has increased or decreased compared with the base branch of this PR.
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Overview
Scaffolds a new
@payloadcms/codemodpackage atpackages/codemod/. It provides a CLI that will host codemods for migrating user projects from Payload v3 to v4. This PR ships infrastructure only; real migration transforms land alongside the PRs that introduce their deprecations.Key Changes
New package:
@payloadcms/codemodpayload-codemodbin, built via SWC. Package shape mirrorscreate-payload-app.npx @payloadcms/codemod [path]runs every registered transform in order;--transform <name>,--list,--dry(alias--dry-run), and--printare available as escape hatches.ts-morph-based runner and transform contract
Transformexposes{ name, description, apply(ctx) }. The runner applies transforms against a shared ts-morphProject, isolates errors per transform, and aggregates results. A failing transform setsprocess.exitCode = 1but does not abort the rest of the run.--printonly emits files whose content changed and the default path only writes files that actually differ from the loaded source.Testing infrastructure
runTransform()helper drives fixture-pair comparisons against in-memory ts-morph projects. Each transform lives in its own folder with co-locatedindex.ts,index.test.ts, and<case>.input.ts/<case>.output.tssiblings. Idempotency is a per-transform test requirement.Reference transform:
example-noopBuild wiring
build:codemodscript added; the package is picked up bybuild:all.packages/eslint-configgains a single entry allowing the newbin/cli.jsthrough typescript-eslint'sprojectService, matching the existing entry forcreate-payload-app.Design Decisions
Engine: ts-morph. Payload is TS-first and most expected v3 to v4 deprecations are TS-shaped (renamed types, moved exports,
import typeupdates, generic signatures). jscodeshift is the industry default but fights TS specifics; ast-grep is too limited for structural transforms.Scope: v3 to v4 only. No version metadata, no
--sinceflag, no back-catalog migrations. Transforms are a flat list in the registry, authored as part of the PR that introduces the deprecation. Future major versions are a separate decision, not a feature flag on this package."Run everything" as the primary UX. Users point the CLI at their project; it applies every registered transform. Transforms are required to be idempotent and safe on non-matching code, so running the full set against a partially migrated project is safe. Per-transform selection exists for debugging, not as the common path.
Dry-run is purely a CLI concern.
TransformContextintentionally does not exposedry. Transforms always mutate the in-memory project; the CLI decides whether to persist. This prevents transforms from branching on a flag they cannot meaningfully honor.--dryis the canonical flag (matching jscodeshift and the broader codemod ecosystem);--dry-runis accepted as an alias for users reaching for the general Unix form.Error isolation over atomicity. One transform throwing does not stop the run; failures surface in the summary and via exit code. Users get maximum-progress-per-invocation, recoverable via idempotent re-runs.
Overall Flow
sequenceDiagram participant User participant CLI as payload-codemod participant Runner participant Registry participant Project as ts-morph Project User->>CLI: payload-codemod [path] [flags] CLI->>Registry: load transforms CLI->>Project: load from tsconfig or glob CLI->>Project: snapshot source texts CLI->>Runner: runTransforms({ project, transforms }) loop per transform Runner->>Project: transform.apply({ project }) Project-->>Runner: mutations applied end Runner-->>CLI: { results, failed } CLI->>Project: diff against snapshot alt --print CLI->>User: print changed files only else --dry CLI->>User: summary only else CLI->>Project: save changed files end CLI->>User: summary + exit codeReferences / Links
Resolves https://app.asana.com/1/10497086658021/project/1214231632891339/task/1214259839775107