Skip to content

Commit

Permalink
Add size and diskusage
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Mar 28, 2024
1 parent 877c1d3 commit e86c3a6
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Methods:
| isDir | X | X | X | custom native |
| isFile | X | X | X | custom native |
| isSymlink | X | X | X | custom native |
| size | X | X | X | custom native |
| diskusage | X | X | X | custom native |

### Io

Expand Down
4 changes: 2 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"@cross/dir": "jsr:@cross/dir@^1.1.0",
"@cross/runtime": "jsr:@cross/runtime@^1.0.0",
"@cross/test": "jsr:@cross/test@^0.0.9",
"@std/assert": "jsr:@std/assert@^0.220.1",
"@std/path": "jsr:@std/path@^0.220.1"
"@std/assert": "jsr:@std/assert@^0.221.0",
"@std/path": "jsr:@std/path@^0.221.0"
},
"publish": {
"exclude": [".github", "*.test.ts"]
Expand Down
1 change: 1 addition & 0 deletions src/stat/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,4 @@ export { statWrap as stat };
export { lstat } from "node:fs/promises";
export * from "./is.ts";
export * from "./exists.ts";
export * from "./size.ts";
52 changes: 52 additions & 0 deletions src/stat/size.ts
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;
}

0 comments on commit e86c3a6

Please sign in to comment.