Skip to content

Commit

Permalink
feat: PathRef - walk and emptyDir (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Mar 14, 2023
1 parent 9347044 commit ceb61f1
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,41 @@ Deno.test("expandGlob", async () => {
});
});

Deno.test("walk", async () => {
await withTempDir(async () => {
const dir = createPathRef("rootDir").mkdirSync().resolve();
dir.join("file1").writeTextSync("");
dir.join("file2").writeTextSync("");
const subDir = dir.join("dir").join("subDir");
subDir.join("file.txt").writeTextSync("");

const entries1 = [];
for await (const entry of dir.walk()) {
entries1.push(entry.path);
}
const entries2 = Array.from(dir.walkSync()).map((e) => e.path);

assertEquals(entries1, entries2);
assertEquals(entries1.length, 6);
const entryNames = entries1.map((e) => e.basename());
assertEquals(entryNames.sort(), [
"dir",
"file.txt",
"file1",
"file2",
"rootDir",
"subDir",
]);

const subDir2 = dir.join("dir2");
subDir2.join("other.txt").writeTextSync("");
const entries3 = Array.from(subDir2.walkSync()).map((e) => e.path);
assertEquals(entries3.length, 2);
assertEquals(entries3[0].basename(), "dir2");
assertEquals(entries3[1].basename(), "other.txt");
});
});

Deno.test("readBytes", async () => {
await withTempDir(async () => {
const file = createPathRef("file.txt");
Expand Down Expand Up @@ -592,6 +627,32 @@ Deno.test("remove", async () => {
});
});

Deno.test("emptyDir", async () => {
await withTempDir(async (path) => {
const dir = path.join("subDir").mkdirSync();
const file = dir.join("file.txt").writeTextSync("text");
const subDir = dir.join("subDir").mkdirSync();
subDir.join("test").writeTextSync("");
assert((await dir.emptyDir()).existsSync());
assert(!file.existsSync());
assert(!subDir.existsSync());
assert(dir.existsSync());
});
});

Deno.test("emptyDirSync", async () => {
await withTempDir((path) => {
const dir = path.join("subDir").mkdirSync();
const file = dir.join("file.txt").writeTextSync("text");
const subDir = dir.join("subDir").mkdirSync();
subDir.join("test").writeTextSync("");
assert(dir.emptyDirSync().existsSync());
assert(!file.existsSync());
assert(!subDir.existsSync());
assert(dir.existsSync());
});
});

Deno.test("copyFile", async () => {
await withTempDir(async () => {
const path = createPathRef("file.txt").writeTextSync("text");
Expand Down
33 changes: 33 additions & 0 deletions src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,22 @@ export class PathRef {
}
}

/** Walks the file tree rooted at the current path, yielding each file or
* directory in the tree filtered according to the given options. */
async *walk(options?: fs.WalkOptions): AsyncIterableIterator<WalkEntry> {
for await (const entry of fs.walk(this.toString(), options)) {
yield this.#stdWalkEntryToDax(entry);
}
}

/** Synchronously walks the file tree rooted at the current path, yielding each
* file or directory in the tree filtered according to the given options. */
*walkSync(options?: fs.WalkOptions): Iterable<WalkEntry> {
for (const entry of fs.walkSync(this.toString(), options)) {
yield this.#stdWalkEntryToDax(entry);
}
}

#stdWalkEntryToDax(entry: fs.WalkEntry): WalkEntry {
return {
...entry,
Expand Down Expand Up @@ -752,6 +768,23 @@ export class PathRef {
return this;
}

/**
* Ensures that a directory is empty.
* Deletes directory contents if the directory is not empty.
* If the directory does not exist, it is created.
* The directory itself is not deleted.
*/
async emptyDir(): Promise<this> {
await fs.emptyDir(this.toString());
return this;
}

/** Synchronous version of `emptyDir()` */
emptyDirSync(): this {
fs.emptyDirSync(this.toString());
return this;
}

/**
* Copies the file returning a promise that resolves to
* the destination path.
Expand Down

0 comments on commit ceb61f1

Please sign in to comment.