Skip to content

Commit

Permalink
feat: PathRef - readDir() and readDirSync return a WalkEntry
Browse files Browse the repository at this point in the history
…(includes path) (#112)
  • Loading branch information
dsherret authored Feb 16, 2023
1 parent b419af3 commit f69085e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
15 changes: 8 additions & 7 deletions src/path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,16 +258,16 @@ Deno.test("readDir", async () => {

const entries1 = [];
for await (const entry of dir.readDir()) {
entries1.push(entry.name);
entries1.push(entry);
}
entries1.sort();
const entries2 = Array.from(dir.readDirSync()).map((e) => e.name);
entries2.sort();
const entries2 = Array.from(dir.readDirSync());
entries1.sort((a, b) => a.name.localeCompare(b.name));
entries2.sort((a, b) => a.name.localeCompare(b.name));

for (const entries of [entries1, entries2]) {
assertEquals(entries.length, 2);
assertEquals(entries[0], "file1");
assertEquals(entries[1], "file2");
assertEquals(entries[0].name, "file1");
assertEquals(entries[1].name, "file2");
}

const filePaths1 = [];
Expand All @@ -278,7 +278,8 @@ Deno.test("readDir", async () => {
filePaths1.sort();
filePaths2.sort();
assertEquals(filePaths1, filePaths2);
assertEquals(filePaths1, entries1.map((dirName) => dir.join(dirName).toString()));
assertEquals(filePaths1, entries1.map((entry) => entry.path.toString()));
assertEquals(filePaths1, entries2.map((entry) => entry.path.toString()));
});
});

Expand Down
34 changes: 24 additions & 10 deletions src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,29 +434,43 @@ export class PathRef {
}

/** Reads the entries in the directory. */
readDir(): AsyncIterable<Deno.DirEntry> {
return Deno.readDir(this.#path);
async *readDir(): AsyncIterable<WalkEntry> {
const dir = this.resolve();
for await (const entry of Deno.readDir(dir.#path)) {
yield {
...entry,
path: dir.join(entry.name),
};
}
}

/** Synchronously reads the entries in the directory. */
readDirSync(): Iterable<Deno.DirEntry> {
return Deno.readDirSync(this.#path);
*readDirSync(): Iterable<WalkEntry> {
const dir = this.resolve();
for (const entry of Deno.readDirSync(dir.#path)) {
yield {
...entry,
path: dir.join(entry.name),
};
}
}

/** Reads the directory file paths, not including symlinks. */
/** Reads only the directory file paths, not including symlinks. */
async *readDirFilePaths(): AsyncIterable<PathRef> {
for await (const entry of Deno.readDir(this.#path)) {
const dir = this.resolve();
for await (const entry of Deno.readDir(dir.#path)) {
if (entry.isFile) {
yield this.join(entry.name);
yield dir.join(entry.name);
}
}
}

/** Synchronously reads the directory file paths, not including symlinks. */
/** Synchronously reads only the directory file paths, not including symlinks. */
*readDirFilePathsSync(): Iterable<PathRef> {
for (const entry of Deno.readDirSync(this.#path)) {
const dir = this.resolve();
for (const entry of Deno.readDirSync(dir.#path)) {
if (entry.isFile) {
yield this.join(entry.name);
yield dir.join(entry.name);
}
}
}
Expand Down

0 comments on commit f69085e

Please sign in to comment.