Skip to content

cli(refactor): split project.ts into cohesive modules - #596

Merged
Ducksss merged 1 commit into
mainfrom
claude/split-project-ts
Oct 4, 2026
Merged

Ducksss merged 1 commit into
mainfrom
claude/split-project-ts

Conversation

@Ducksss

@Ducksss Ducksss commented Oct 3, 2026

Copy link
Copy Markdown
Owner

Summary

tools/payload-components/project.ts had grown to 2,106 lines. The September audit put it this way: it "mixes detection, scanning, wiring and localization". This PR moves the code into separate modules along those four lines. It moves code without rewriting it, so behavior does not change. project.ts stays as the facade, so every existing import … from './project' still works. No importer, test or mock changed.

Module map

File Responsibility Lines
tools/payload-components/project.ts Facade and project-level checks: detectProject (support-matrix target matching, resolveHostFile), flattenRequiredFiles / findExistingRequiredFile, resolveRecoveryPatchedFiles, assertManifestSupport, the manifest prerequisite checks (checkManifestProjectRequirements / assertManifestProjectRequirements), verifyInstalledManifestFiles and setBaseCollections. Also re-exports the fragment and localization APIs. 466
tools/payload-components/payload-fragments.ts Fragment wiring for RenderBlocks and Pages: applyPayloadFragments, preparePayloadFragmentRemoval, removePayloadFragments and verifyInstalledPayloadFragments. Also holds the anchors, the dedup checks (hasDirectObjectEntry, hasDirectArrayIdentifier), the removal helpers and the describeFragmentFailure hand-edit message. 650
tools/payload-components/payload-localization.ts Both halves of localization. Field level: localizeBlockConfigSource, applyLocalizedFields, isBlockConfigFile, LOCALIZE_HELPER_FILE. Config level: setPayloadLocalization / readPayloadLocalization and their private readers. 613
tools/payload-components/source-scanner.ts The masking scanner (maskIgnoredSource) plus the structural primitives built on it: delimiter matching, top-level object and anchor lookup, named-import lookup, insertLineBeforeAnchor, and the object-literal readers (findBuildConfigObject, findDirectProperty, findDirectShorthand, findLastDirectSpread, findDirectArrayEntries, …). These are pure functions of source text and the module imports nothing. 482
tools/payload-components/project-paths.ts CANONICAL_HOST_FILES and the two one-line helpers (getAbsolutePath, normalizeFileList) that detection, fragments and localization all use. It exists so none of those modules has to import another sideways. 22

Dependency graph (no cycles among the 52 CLI modules):

  • project-paths is a leaf.
  • source-scanner is a leaf.
  • payload-fragments and payload-localization depend on project-paths and source-scanner. They do not depend on each other.
  • project depends on both leaves and re-exports payload-fragments and payload-localization.

Why these boundaries

  • The scanner is its own module. Fragments, localization and the project checks all use the same primitives. That sharing used to be implicit because everything lived in one file; now each module imports what it uses.
  • The buildConfig object readers moved to the scanner. They were written for config-level localization, but missingCollectionRequirements and setBaseCollections now use them too. Leaving them in the localization module would make the project checks import from localization.
  • Single-consumer helpers stay with their consumer. For example, findRenderBlocksObject, findPagesLayoutBlocks and expandToOwnLine stay in fragments, and detectObjectIndent and readDirectString stay in localization.

Deliberately left in project.ts

  • Detection and host-file resolution, the manifest support and prerequisite checks, and verifyInstalledManifestFiles. These are the "project" concerns the file name describes, and leaving them in place keeps that part of the diff at zero.
  • setBaseCollections. It is the init --scaffold patch that registers Pages/Media in buildConfig.collections. It stays next to missingCollectionRequirements, which reads the same collections array for the prerequisite check, so the read/write pair stays together. It is not localization, and it is not a PayloadFragment.

Behavior is unchanged

Every function body is byte-for-byte identical. The only new lines are the five module header comments, the import blocks, one export list per new module, and the facade re-exports. Previously private helpers are exported through that one export list (as project.ts already did for LOCALIZE_HELPER_FILE), so no moved declaration line was edited. The only lines removed outright are the two import names project.ts no longer uses (PayloadFragment, commitFileChanges) and three stray blank lines.

How I verified it:

  • Line multiset. I took every non-blank line of the old file and compared it against the union of the five new files. Nothing is lost except the two dropped import names, and every added line is a header, import, export list or re-export.
  • Export surface. Object.keys(await import('./project')) returns the same 20 runtime names before and after. The three type exports also remain: LocalizationConfigPatch and ReadLocalization are re-exported, and ProjectRequirementFailure is unchanged in place.
  • Published bundle. I built dist/cli.js with tsup from main and from this branch and compared them line by line. The only differences are esbuild's pathN import-alias numbering (one more node:path import shifts the counter) and its // tools/payload-components/<file>.ts module markers. The code is the same; only its order in the bundle changed. That reordering is safe because every top-level statement in these modules only initializes a constant or declares a function.
  • Mocks. tests/int/payload-components-add-command.int.spec.ts and payload-components-doctor-orchestration.int.spec.ts vi.doMock the project module. All consumers still import through the facade, so those mocks still intercept the same calls.

Checks (run locally):

  • pnpm lint: pass, 0 warnings. eslint --max-warnings 0 also passes on the five modules.
  • pnpm format:check: pass.
  • pnpm exec tsc --noEmit: pass.
  • pnpm run test:int: 64 files passed, 1 skipped (the gated pack test); 803 tests passed, 1 skipped.
  • pnpm test:pack: pass. It packs the tarball, installs it, and runs the bundled bin.

Reviewing

git diff --color-moved=dimmed-zebra --diff-algorithm=histogram origin/main...

With histogram (or patience), git colours 1,674 of the 1,679 lines removed from project.ts as moved. The 39 lines added to project.ts are just the new imports and the facade. The new files' uncoloured lines are their headers, imports and export lists. The default Myers algorithm also works, but it shows about 90 kept lines of project.ts as moved within the file.

Docs and blog figures

  • Blog catalog and figure. tools/blog/visual-system/catalog.ts: the two renderer excerpts (how-renderblocks-works, anchored at const propertyLine, and text-anchors-vs-ast, anchored at applyRenderBlocksFragment) now point at payload-fragments.ts. pnpm blog:visuals:figures regenerated all 27 figures. Only public/blog/how-renderblocks-works/figure-01-renderer-dispatch.svg changed, and only its provenance line; the excerpt text is identical.
  • Design essay. In content/docs/design.mdx, decision 03 ("Patch by anchor, refuse when unsure") now points its GitHub receipt at payload-fragments.ts, where the anchor refusal and dedup logic now live.
  • AGENTS.md. Core flows now names payload-fragments.ts, source-scanner.ts and payload-localization.ts where it described project.ts.
  • Left as is. The dated records in docs/reviews/ and docs/superpowers/plans/ are history and were not changed. tests/int/ci-change-selection.int.spec.ts uses project.ts only as a sample path, and that file still exists.

Notes for reviewers

  • Blog covers not regenerated. The committed cover.webp images for how-renderblocks-works and text-anchors-vs-ast still caption their primary provenance as tools/payload-components/project.ts. Covers are Chromium renders and are not byte-gated. Re-rendering those two from unmodified main on my machine (render-covers.ts --slug …) already differs from the committed files in 18–22% of pixels across the whole canvas. Regenerating them here would therefore ship unrelated visual changes along with the one-line caption fix. They should be refreshed with the rest of the covers, or the stale caption accepted.
  • Prettier does not check this directory. .prettierignore's bare payload-components entry matches at any depth, so it also excludes tools/payload-components/. That is why missingCollectionRequirements and setBaseCollections are not Prettier-shaped. I kept them verbatim rather than reformat during a move. Anchoring the entry as /payload-components would bring the CLI under the gate, but as a separate change with its own formatting diff.
  • CI. CI will run the fresh-Payload smoke because tools/ changed. I did not run pnpm test:fresh locally.

🤖 Generated with Claude Code

project.ts had grown to 2,106 lines mixing target detection, the masking
scanner, fragment wiring and Payload localization. Move the code into
modules along those seams, verbatim, and keep project.ts as the facade so
every existing import of './project' keeps working:

- source-scanner.ts: the masking scanner and structural primitives
  (pure functions of source text; no imports)
- payload-fragments.ts: RenderBlocks/Pages fragment apply, remove, verify
- payload-localization.ts: the localizeFields wrap and the buildConfig
  localization block (set/read)
- project-paths.ts: CANONICAL_HOST_FILES and the two path helpers the
  other modules share, so none of them imports another sideways
- project.ts: detection, host-file resolution, manifest support and
  prerequisite checks, installed-file checks, base-collection
  registration, and re-exports of the moved public API

Every function body moves byte-for-byte; the only new lines are module
headers, import blocks, export lists and the facade re-exports. Previously
private helpers are exported through one export list per module instead
of editing their declarations. Behavior is unchanged: the runtime export
surface of project.ts is identical, and the bundled CLI differs from
main's only in esbuild's pathN alias numbering and module comments.

The blog catalog's two renderer excerpts now point at payload-fragments.ts
(the regenerated figure changes only its provenance line), the design
essay's "patch by anchor" receipt follows the code, and AGENTS.md names
the new modules.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 3, 2026 12:31
@vercel

vercel Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
payload-components Ready Ready Preview Oct 3, 2026 12:33pm UTC
payload-components-97bf Ready Ready Preview Oct 3, 2026 12:33pm UTC

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@coderabbitai

coderabbitai Bot commented Oct 3, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 42 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: Repository: Ducksss/payload-components/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3da1b7c5-f837-4c41-a955-baf7d09476e7
📥 Commits

Reviewing files that changed from the base of the PR and between 599e179 and 6ceeaa1.

⛔ Files ignored due to path filters (1)
  • public/blog/how-renderblocks-works/figure-01-renderer-dispatch.svg is excluded by !**/*.svg
📒 Files selected for processing (8)
  • AGENTS.md
  • content/docs/design.mdx
  • tools/blog/visual-system/catalog.ts
  • tools/payload-components/payload-fragments.ts
  • tools/payload-components/payload-localization.ts
  • tools/payload-components/project-paths.ts
  • tools/payload-components/project.ts
  • tools/payload-components/source-scanner.ts
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Ducksss
Ducksss merged commit 4949208 into main Oct 4, 2026
17 checks passed
@Ducksss
Ducksss deleted the claude/split-project-ts branch October 4, 2026 07:38
@Ducksss Ducksss mentioned this pull request Oct 9, 2026

This branch was successfully deployed

2 active deployments
Preview – payload-components — 6ceeaa10 Deployed Oct 3, 2026 by vercel[bot]
Preview – payload-components-97bf — 6ceeaa10 Deployed Oct 3, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants