Skip to content

Commit 250fcf1

Browse files
authored
feat: calculate directory sizes in code view (#1540)
1 parent f5aca2e commit 250fcf1

File tree

5 files changed

+10
-7
lines changed

5 files changed

+10
-7
lines changed

app/components/Code/DirectoryListing.vue

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,7 @@ const bytesFormatter = useBytesFormatter()
120120
</svg>
121121
<span class="w-full flex justify-self-stretch items-center gap-2">
122122
<span class="flex-1">{{ node.name }}</span>
123-
<span
124-
v-if="node.type === 'file' && node.size"
125-
class="text-end text-xs text-fg-subtle"
126-
>
123+
<span v-if="typeof node.size === 'number'" class="text-end text-xs text-fg-subtle">
127124
{{ bytesFormatter.format(node.size) }}
128125
</span>
129126
</span>

server/api/registry/files/[...pkg].get.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export default defineCachedEventHandler(
4646
swr: true,
4747
getKey: event => {
4848
const pkg = getRouterParam(event, 'pkg') ?? ''
49-
return `files:v1:${pkg.replace(/\/+$/, '').trim()}`
49+
return `files:v2:${pkg.replace(/\/+$/, '').trim()}`
5050
},
5151
},
5252
)

server/utils/file-tree.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,14 @@ export function convertToFileTree(
3939
const path = parentPath ? `${parentPath}/${node.name}` : node.name
4040

4141
if (node.type === 'directory') {
42+
const children = node.files ? convertToFileTree(node.files, path) : []
43+
4244
result.push({
4345
name: node.name,
4446
path,
4547
type: 'directory',
46-
children: node.files ? convertToFileTree(node.files, path) : [],
48+
size: children.reduce((total, child) => total + (child.size ?? 0), 0),
49+
children,
4750
})
4851
} else {
4952
result.push({

shared/types/npm-registry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ export interface PackageFileTree {
355355
path: string
356356
/** Node type */
357357
type: 'file' | 'directory'
358-
/** File size in bytes (only for files) */
358+
/** Node size in bytes (file size or recursive directory total) */
359359
size?: number
360360
/** Child nodes (only for directories) */
361361
children?: PackageFileTree[]

test/unit/server/utils/file-tree.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ describe('convertToFileTree', () => {
9393
const formatFile = getChildren(utilsDir).find(child => child.name === 'format.ts')
9494
expect(formatFile?.path).toBe('src/utils/format.ts')
9595
expect(formatFile?.size).toBe(22)
96+
expect(utilsDir?.size).toBe(22)
97+
expect(src?.size).toBe(122)
9698
})
9799

98100
it('returns an empty tree for empty input', () => {
@@ -112,6 +114,7 @@ describe('convertToFileTree', () => {
112114
const tree = convertToFileTree(input)
113115

114116
expect(tree[0]?.type).toBe('directory')
117+
expect(tree[0]?.size).toBe(0)
115118
expect(tree[0]?.children).toEqual([])
116119
})
117120
})

0 commit comments

Comments
 (0)