-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
57 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { readdir, stat } from "node:fs/promises"; | ||
import { join } from "@std/path"; | ||
|
||
export async function diskusage( | ||
inPath: string, | ||
recursive?: boolean, | ||
): Promise<number> { | ||
const statSelf = await stat(inPath); | ||
const blockSize = statSelf.blksize; | ||
const actualSize = Math.max(blockSize, statSelf.size); | ||
if (statSelf.isFile()) { | ||
return actualSize; | ||
} | ||
if (statSelf.isDirectory()) { | ||
const files = await readdir(inPath, { withFileTypes: true }); | ||
const paths: Promise<number>[] = files.map((file) => { | ||
const path = join(inPath, file.name); | ||
return size(path, recursive); | ||
}); | ||
const results = await Promise.allSettled(paths); | ||
const sizes = results | ||
.map((result) => result.status === "fulfilled" ? result.value : 0) | ||
.flat(Infinity) | ||
.reduce((i, size) => i + size, 0); | ||
return sizes + actualSize; | ||
} | ||
return actualSize; | ||
} | ||
|
||
export async function size( | ||
inPath: string, | ||
recursive?: boolean, | ||
): Promise<number> { | ||
const statSelf = await stat(inPath); | ||
if (statSelf.isFile()) { | ||
return statSelf.size; | ||
} | ||
if (statSelf.isDirectory()) { | ||
const files = await readdir(inPath, { withFileTypes: true }); | ||
const paths: Promise<number>[] = files.map((file) => { | ||
const path = join(inPath, file.name); | ||
return size(path, recursive); | ||
}); | ||
const results = await Promise.allSettled(paths); | ||
const sizes = results | ||
.map((result) => result.status === "fulfilled" ? result.value : 0) | ||
.flat(Infinity) | ||
.reduce((i, size) => i + size, 0); | ||
return sizes + statSelf.size; | ||
} | ||
return statSelf.size; | ||
} |