Skip to content

Comments

feat: calculate directory sizes#1540

Open
wojtekmaj wants to merge 1 commit intonpmx-dev:mainfrom
wojtekmaj:dir-sizes
Open

feat: calculate directory sizes#1540
wojtekmaj wants to merge 1 commit intonpmx-dev:mainfrom
wojtekmaj:dir-sizes

Conversation

@wojtekmaj
Copy link
Contributor

Before/After:

Zrzut ekranu 2026-02-18 o 10 21 43

npm reference:

Zrzut ekranu 2026-02-18 o 10 22 56

Copilot AI review requested due to automatic review settings February 18, 2026 09:23
@vercel
Copy link

vercel bot commented Feb 18, 2026

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

Project Deployment Actions Updated (UTC)
npmx.dev Ready Ready Preview, Comment Feb 18, 2026 9:25am
2 Skipped Deployments
Project Deployment Actions Updated (UTC)
docs.npmx.dev Ignored Ignored Preview Feb 18, 2026 9:25am
npmx-lunaria Ignored Ignored Feb 18, 2026 9:25am

Request Review

@codecov
Copy link

codecov bot commented Feb 18, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request adds support for calculating and displaying directory sizes in the package file browser. Previously, only individual file sizes were shown; now directories display the recursive total of all files they contain, matching the behavior of npm's package explorer.

Changes:

  • Implemented recursive directory size calculation in the file tree conversion logic
  • Updated type documentation to clarify that size applies to both files and directories
  • Bumped API cache key version to invalidate old cached responses
  • Modified UI to display sizes for both files and directories
  • Added comprehensive test coverage for directory size calculations

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
server/utils/file-tree.ts Added recursive directory size calculation using reduce to sum all child node sizes
shared/types/npm-registry.ts Updated JSDoc comment to document that size field applies to directories (as recursive totals) and files
server/api/registry/files/[...pkg].get.ts Incremented cache key from v1 to v2 to invalidate stale cached responses with old structure
app/components/Code/DirectoryListing.vue Changed condition from checking file type and size to type-safe number check to display sizes for both files and directories
test/unit/server/utils/file-tree.spec.ts Added assertions to verify directory sizes are calculated correctly including nested directories and empty directories

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 18, 2026

📝 Walkthrough

Walkthrough

This change extends the file tree structure to include size information for directory nodes. The convertToFileTree utility now computes and exposes a size field for directories, representing the recursive total of child sizes. The component rendering logic is updated to display sizes for any node with a numeric size value, rather than only files. The cache key for file-tree API responses is incremented to invalidate previously cached data. Supporting documentation is updated to reflect that size can represent either individual file sizes or directory totals. Tests are added to verify size calculations for directory nodes.

🚥 Pre-merge checks | ✅ 1
✅ Passed checks (1 passed)
Check name Status Explanation
Description check ✅ Passed The pull request description includes before/after screenshots comparing directory listing views with file size calculations, directly relating to the changeset which adds directory size computation.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord.


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 and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
app/components/Code/DirectoryListing.vue (1)

123-125: typeof node.size === 'number' is the right guard – minor UX note for the 0 case.

The check correctly supersedes a truthiness test and handles both size === 0 and type-narrowing for bytesFormatter.format. One consequence: empty directories (whose computed size is always 0) will now render "0 B" in the listing. If that's undesirable, a simple additional guard would suppress it:

✨ Optional: suppress "0 B" for empty directories
-<span v-if="typeof node.size === 'number'" class="text-end text-xs text-fg-subtle">
+<span v-if="typeof node.size === 'number' && node.size > 0" class="text-end text-xs text-fg-subtle">

@alex-key
Copy link

Hi @wojtekmaj , very nice feature)
What is the reason for having a difference between the sum of file sizes (and it seems that npmjs calculates it correctly) and your implementation?

image image

@wojtekmaj
Copy link
Contributor Author

wojtekmaj commented Feb 18, 2026

Hi @wojtekmaj , very nice feature) What is the reason for having a difference between the sum of file sizes (and it seems that npmjs calculates it correctly) and your implementation?

@alex-key Heh, looks like you've found a bug - but not in my PR, but in npmx in general. Taking /fast-xml-parser/lib/fxbuilder.min.js as an example, which is 8414 bytes long, npm reports its size as 8.41 kB, we as 8.2 kB. So we give size as KiB while we say it's kB. Will fix in a separate PR cause it has nothing to do with that one.

There you go: #1541

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