Skip to content

Commit

Permalink
feat: 🎸 add ability to remove all files
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 20, 2023
1 parent 99c915f commit 566e29b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/fsa-to-node/FsaNodeFs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,16 @@ export class FsaNodeFs extends FsaNodeCore implements FsCallbackApi, FsSynchrono
});
};

private rmAll(callback: (err: Error | null) => void): void {
(async () => {
const root = await this.root;
for await (const name of root.keys()) {
await root.removeEntry(name, {recursive: true});
}
})()
.then(() => callback(null), error => callback(error));
}

public readonly rmdir: FsCallbackApi['rmdir'] = (
path: misc.PathLike,
a: misc.TCallback<void> | opts.IRmdirOptions,
Expand All @@ -597,6 +607,7 @@ export class FsaNodeFs extends FsaNodeCore implements FsCallbackApi, FsSynchrono
const options: opts.IRmdirOptions = optHelpers.getRmdirOptions(a);
const callback: misc.TCallback<void> = util.validateCallback(typeof a === 'function' ? a : b);
const [folder, name] = pathToLocation(util.pathToFilename(path));
if (!name && options.recursive) return this.rmAll(callback);
this.getDir(folder, false, 'rmdir')
.then(dir => dir.getDirectoryHandle(name).then(() => dir))
.then(dir => dir.removeEntry(name, { recursive: options.recursive ?? false }))
Expand Down Expand Up @@ -629,6 +640,7 @@ export class FsaNodeFs extends FsaNodeCore implements FsCallbackApi, FsSynchrono
): void => {
const [options, callback] = optHelpers.getRmOptsAndCb(a, b);
const [folder, name] = pathToLocation(util.pathToFilename(path));
if (!name && options.recursive) return this.rmAll(callback);
this.getDir(folder, false, 'rmdir')
.then(dir => dir.removeEntry(name, { recursive: options.recursive ?? false }))
.then(
Expand Down
16 changes: 16 additions & 0 deletions src/fsa-to-node/__tests__/FsaNodeFs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ onlyOnNode20('FsaNodeFs', () => {
'/mountpoint/empty-folder': null,
});
});

test('can remove starting from root folder', async () => {
const { fs, mfs } = setup({ folder: { subfolder: { file: 'test' } }, 'empty-folder': null });
await fs.promises.rmdir('/', { recursive: true });
expect(mfs.__vol.toJSON()).toStrictEqual({
'/mountpoint': null,
});
});
});

describe('.rm()', () => {
Expand Down Expand Up @@ -164,6 +172,14 @@ onlyOnNode20('FsaNodeFs', () => {
'/mountpoint/empty-folder': null,
});
});

test('can remove starting from root folder', async () => {
const { fs, mfs } = setup({ folder: { subfolder: { file: 'test' } }, 'empty-folder': null });
await fs.promises.rm('/', { recursive: true });
expect(mfs.__vol.toJSON()).toStrictEqual({
'/mountpoint': null,
});
});
});

describe('.unlink()', () => {
Expand Down
1 change: 0 additions & 1 deletion src/node/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export interface IMkdirOptions {
}

export interface IRmdirOptions {
/** @deprecated */
recursive?: boolean;
maxRetries?: number;
retryDelay?: number;
Expand Down

0 comments on commit 566e29b

Please sign in to comment.