Skip to content

Commit

Permalink
fix: remove dir, should skip using file
Browse files Browse the repository at this point in the history
  • Loading branch information
hughfenghen committed Aug 2, 2024
1 parent c3b1ad2 commit af2bc11
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/calm-actors-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'opfs-tools': patch
---

fix: remove dir, should skip using file
15 changes: 15 additions & 0 deletions src/__tests__/directory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,18 @@ test('move dir, dest is exists', async () => {

await trushDir.remove();
});

test('remove dir', async () => {
const f = file(filePath + '/a');
await write(f, 'aaa');
await write(filePath + '/b/c', 'bbbccc');
const d = dir(filePath);
const reader = await f.createReader();
expect((await d.children()).length).toBe(2);
await d.remove();
expect((await d.children()).length).toBe(1);

await reader.close();
await d.remove();
expect(await d.exists()).toBe(false);
});
13 changes: 12 additions & 1 deletion src/directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,18 @@ export class OPFSDirWrap {
* return A promise that resolves when the directory is removed.
*/
async remove() {
await remove(this.#path);
for (const it of await this.children()) {
try {
await it.remove();
} catch (err) {
console.warn(err);
}
}
try {
await remove(this.#path);
} catch (err) {
console.warn(err);
}
}

/**
Expand Down

0 comments on commit af2bc11

Please sign in to comment.