|
1 | 1 | import type { Command, CommandContext, ExecResult } from "../../types.js"; |
2 | 2 | import { parseArgs } from "../../utils/args.js"; |
| 3 | +import { DEFAULT_BATCH_SIZE } from "../../utils/constants.js"; |
3 | 4 | import { hasHelpFlag, showHelp } from "../help.js"; |
4 | 5 |
|
5 | 6 | const duHelp = { |
@@ -120,49 +121,123 @@ async function calculateSize( |
120 | 121 | return result; |
121 | 122 | } |
122 | 123 |
|
123 | | - // Directory |
124 | | - const entries = await ctx.fs.readdir(fullPath); |
| 124 | + // Directory - use readdirWithFileTypes if available for better performance |
125 | 125 | let dirSize = 0; |
126 | 126 |
|
127 | | - for (const entry of entries) { |
128 | | - const entryPath = fullPath === "/" ? `/${entry}` : `${fullPath}/${entry}`; |
129 | | - const entryDisplayPath = |
130 | | - displayPath === "." ? entry : `${displayPath}/${entry}`; |
| 127 | + // Get entries with type info if possible |
| 128 | + interface EntryInfo { |
| 129 | + name: string; |
| 130 | + isDirectory: boolean; |
| 131 | + size?: number; |
| 132 | + } |
| 133 | + const entryInfos: EntryInfo[] = []; |
131 | 134 |
|
132 | | - try { |
133 | | - const entryStat = await ctx.fs.stat(entryPath); |
134 | | - |
135 | | - if (entryStat.isDirectory) { |
136 | | - const subResult = await calculateSize( |
137 | | - ctx, |
138 | | - entryPath, |
139 | | - entryDisplayPath, |
140 | | - options, |
141 | | - depth + 1, |
142 | | - ); |
143 | | - dirSize += subResult.totalSize; |
144 | | - |
145 | | - // Only output subdirectories if not summarizing and within depth limit |
146 | | - if (!options.summarize) { |
147 | | - if (options.maxDepth === null || depth + 1 <= options.maxDepth) { |
148 | | - result.output += subResult.output; |
149 | | - } else { |
150 | | - // Still need to count the size even if not displaying |
151 | | - dirSize += 0; // Size already counted |
| 135 | + if (ctx.fs.readdirWithFileTypes) { |
| 136 | + const entriesWithTypes = await ctx.fs.readdirWithFileTypes(fullPath); |
| 137 | + // For files, we still need stat to get size, but we know directories |
| 138 | + const fileEntries = entriesWithTypes.filter((e) => e.isFile); |
| 139 | + const dirEntries = entriesWithTypes.filter((e) => e.isDirectory); |
| 140 | + |
| 141 | + // Parallel stat for files to get sizes |
| 142 | + for (let i = 0; i < fileEntries.length; i += DEFAULT_BATCH_SIZE) { |
| 143 | + const batch = fileEntries.slice(i, i + DEFAULT_BATCH_SIZE); |
| 144 | + const stats = await Promise.all( |
| 145 | + batch.map(async (e) => { |
| 146 | + const entryPath = |
| 147 | + fullPath === "/" ? `/${e.name}` : `${fullPath}/${e.name}`; |
| 148 | + try { |
| 149 | + const s = await ctx.fs.stat(entryPath); |
| 150 | + return { name: e.name, isDirectory: false, size: s.size }; |
| 151 | + } catch { |
| 152 | + return { name: e.name, isDirectory: false, size: 0 }; |
152 | 153 | } |
153 | | - } |
154 | | - } else { |
155 | | - dirSize += entryStat.size; |
156 | | - if (options.allFiles && !options.summarize) { |
157 | | - result.output += |
158 | | - formatSize(entryStat.size, options.humanReadable) + |
159 | | - "\t" + |
160 | | - entryDisplayPath + |
161 | | - "\n"; |
| 154 | + }), |
| 155 | + ); |
| 156 | + entryInfos.push(...stats); |
| 157 | + } |
| 158 | + |
| 159 | + // Add directory entries (size will be calculated recursively) |
| 160 | + entryInfos.push( |
| 161 | + ...dirEntries.map((e) => ({ name: e.name, isDirectory: true })), |
| 162 | + ); |
| 163 | + } else { |
| 164 | + // Fall back to readdir + parallel stat |
| 165 | + const entries = await ctx.fs.readdir(fullPath); |
| 166 | + for (let i = 0; i < entries.length; i += DEFAULT_BATCH_SIZE) { |
| 167 | + const batch = entries.slice(i, i + DEFAULT_BATCH_SIZE); |
| 168 | + const stats = await Promise.all( |
| 169 | + batch.map(async (entry) => { |
| 170 | + const entryPath = |
| 171 | + fullPath === "/" ? `/${entry}` : `${fullPath}/${entry}`; |
| 172 | + try { |
| 173 | + const s = await ctx.fs.stat(entryPath); |
| 174 | + return { |
| 175 | + name: entry, |
| 176 | + isDirectory: s.isDirectory, |
| 177 | + size: s.isDirectory ? undefined : s.size, |
| 178 | + }; |
| 179 | + } catch { |
| 180 | + return { name: entry, isDirectory: false, size: 0 }; |
| 181 | + } |
| 182 | + }), |
| 183 | + ); |
| 184 | + entryInfos.push(...stats); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + // Sort entries for consistent output |
| 189 | + entryInfos.sort((a, b) => a.name.localeCompare(b.name)); |
| 190 | + |
| 191 | + // Process files first (simple size addition) |
| 192 | + const fileInfos = entryInfos.filter((e) => !e.isDirectory); |
| 193 | + for (const file of fileInfos) { |
| 194 | + const size = file.size ?? 0; |
| 195 | + dirSize += size; |
| 196 | + if (options.allFiles && !options.summarize) { |
| 197 | + const entryDisplayPath = |
| 198 | + displayPath === "." ? file.name : `${displayPath}/${file.name}`; |
| 199 | + result.output += |
| 200 | + formatSize(size, options.humanReadable) + |
| 201 | + "\t" + |
| 202 | + entryDisplayPath + |
| 203 | + "\n"; |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + // Process directories in parallel batches |
| 208 | + const dirInfos = entryInfos.filter((e) => e.isDirectory); |
| 209 | + for (let i = 0; i < dirInfos.length; i += DEFAULT_BATCH_SIZE) { |
| 210 | + const batch = dirInfos.slice(i, i + DEFAULT_BATCH_SIZE); |
| 211 | + const subResults = await Promise.all( |
| 212 | + batch.map(async (dir) => { |
| 213 | + const entryPath = |
| 214 | + fullPath === "/" ? `/${dir.name}` : `${fullPath}/${dir.name}`; |
| 215 | + const entryDisplayPath = |
| 216 | + displayPath === "." ? dir.name : `${displayPath}/${dir.name}`; |
| 217 | + return { |
| 218 | + name: dir.name, |
| 219 | + result: await calculateSize( |
| 220 | + ctx, |
| 221 | + entryPath, |
| 222 | + entryDisplayPath, |
| 223 | + options, |
| 224 | + depth + 1, |
| 225 | + ), |
| 226 | + }; |
| 227 | + }), |
| 228 | + ); |
| 229 | + |
| 230 | + // Sort results for consistent order |
| 231 | + subResults.sort((a, b) => a.name.localeCompare(b.name)); |
| 232 | + |
| 233 | + for (const { result: subResult } of subResults) { |
| 234 | + dirSize += subResult.totalSize; |
| 235 | + // Only output subdirectories if not summarizing and within depth limit |
| 236 | + if (!options.summarize) { |
| 237 | + if (options.maxDepth === null || depth + 1 <= options.maxDepth) { |
| 238 | + result.output += subResult.output; |
162 | 239 | } |
163 | 240 | } |
164 | | - } catch { |
165 | | - // Skip entries we can't read |
166 | 241 | } |
167 | 242 | } |
168 | 243 |
|
|
0 commit comments