-
Notifications
You must be signed in to change notification settings - Fork 633
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fs/unstable): add readDir (#6338)
- Loading branch information
Showing
7 changed files
with
117 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright 2018-2025 the Deno authors. MIT license. | ||
|
||
import type { DirEntry } from "./unstable_types.ts"; | ||
|
||
export function toDirEntry(s: import("node:fs").Dirent): DirEntry { | ||
return { | ||
name: s.name, | ||
isFile: s.isFile(), | ||
isDirectory: s.isDirectory(), | ||
isSymlink: s.isSymbolicLink(), | ||
}; | ||
} |
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,40 @@ | ||
// Copyright 2018-2025 the Deno authors. MIT license. | ||
|
||
import { getNodeFs, isDeno } from "./_utils.ts"; | ||
import { mapError } from "./_map_error.ts"; | ||
import { toDirEntry } from "./_to_dir_entry.ts"; | ||
import type { DirEntry } from "./unstable_types.ts"; | ||
|
||
/** Reads the directory given by `path` and returns an async iterable of | ||
* {@linkcode DirEntry}. The order of entries is not guaranteed. | ||
* | ||
* @example Usage | ||
* ```ts | ||
* import { readDir } from "@std/fs/unstable-read-dir"; | ||
* | ||
* for await (const dirEntry of readDir("/")) { | ||
* console.log(dirEntry.name); | ||
* } | ||
* ``` | ||
* | ||
* Throws error if `path` is not a directory. | ||
* | ||
* Requires `allow-read` permission. | ||
* | ||
* @tags allow-read | ||
* @category File System | ||
*/ | ||
export async function* readDir(path: string | URL): AsyncIterable<DirEntry> { | ||
if (isDeno) { | ||
yield* Deno.readDir(path); | ||
} else { | ||
try { | ||
const dir = await getNodeFs().promises.opendir(path); | ||
for await (const entry of dir) { | ||
yield toDirEntry(entry); | ||
} | ||
} catch (error) { | ||
throw mapError(error); | ||
} | ||
} | ||
} |
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,41 @@ | ||
// Copyright 2018-2025 the Deno authors. MIT license. | ||
|
||
import { assert, assertEquals, assertRejects } from "@std/assert"; | ||
import { fromFileUrl, join, resolve } from "@std/path"; | ||
import { readDir } from "./unstable_read_dir.ts"; | ||
import { NotFound } from "./unstable_errors.js"; | ||
|
||
const testdataDir = resolve(fromFileUrl(import.meta.url), "../testdata"); | ||
|
||
Deno.test("readDir() reads from the directory and its subdirectories", async () => { | ||
const files = []; | ||
for await (const e of readDir(testdataDir)) { | ||
files.push(e); | ||
} | ||
|
||
let counter = 0; | ||
for (const f of files) { | ||
if (f.name === "walk") { | ||
assert(f.isDirectory); | ||
counter++; | ||
} | ||
} | ||
|
||
assertEquals(counter, 1); | ||
}); | ||
|
||
Deno.test("readDir() rejects when the path is not a directory", async () => { | ||
await assertRejects(async () => { | ||
const testFile = join(testdataDir, "0.ts"); | ||
await readDir(testFile)[Symbol.asyncIterator]().next(); | ||
}, Error); | ||
}); | ||
|
||
Deno.test("readDir() rejects when the directory does not exist", async () => { | ||
await assertRejects( | ||
async () => { | ||
await readDir("non_existent_dir")[Symbol.asyncIterator]().next(); | ||
}, | ||
NotFound, | ||
); | ||
}); |
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