forked from denoland/std
-
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.
feat(fs/unstable): added readDir function and test denoland#6255
- Loading branch information
Showing
2 changed files
with
91 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2018-2025 the Deno authors. MIT license. | ||
|
||
import { getNodeFsPromises, isDeno } from "./_utils.ts"; | ||
import { mapError } from "./_map_error.ts"; | ||
import { toDirEntry } from "./_to_dir_entry.ts"; | ||
import type { DirEntry } from "./unstable_types.ts"; | ||
|
||
async function* getDirEntries(path: string | URL): AsyncIterable<DirEntry> { | ||
const fsPromises = getNodeFsPromises(); | ||
try { | ||
const dir = await fsPromises.opendir(path); | ||
for await (const entry of dir) { | ||
const dirEntry = toDirEntry(entry); | ||
yield dirEntry; | ||
} | ||
} catch (error) { | ||
throw error; | ||
} | ||
} | ||
|
||
/** Reads the directory given by `path` and returns an async iterable of | ||
* {@linkcode DirEntry}. The order of entries is not guaranteed. | ||
* | ||
* ```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 function readDir(path: string | URL): AsyncIterable<DirEntry> { | ||
if (isDeno) { | ||
return Deno.readDir(path); | ||
} else { | ||
try { | ||
const dirEntries = getDirEntries(path); | ||
return dirEntries; | ||
} 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.txt"); | ||
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, | ||
); | ||
}); |