Skip to content

Commit

Permalink
feat: 🎸 add .rmSync(), .rm(), and .promises.rm() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Sep 19, 2021
1 parent 05b2a47 commit 2414fb6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/__tests__/volume/rmSync.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { create } from '../util';

describe('rmSync', () => {
it('remove directory with two files', () => {
const vol = create({
'/foo/bar': 'baz',
'/foo/baz': 'qux',
'/oof': 'zab',
});

vol.rmSync('/foo', {force: true, recursive: true});

expect(vol.toJSON()).toEqual({
'/oof': 'zab',
});
});
});
6 changes: 6 additions & 0 deletions src/promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
IRealpathOptions,
IWriteFileOptions,
IStatOptions,
IRmOptions,
} from './volume';
import Stats from './Stats';
import Dirent from './Dirent';
Expand Down Expand Up @@ -86,6 +87,7 @@ export interface IPromisesAPI {
realpath(path: PathLike, options?: IRealpathOptions | string): Promise<TDataOut>;
rename(oldPath: PathLike, newPath: PathLike): Promise<void>;
rmdir(path: PathLike): Promise<void>;
rm(path: PathLike, options?: IRmOptions): Promise<void>;
stat(path: PathLike, options?: IStatOptions): Promise<Stats>;
symlink(target: PathLike, path: PathLike, type?: symlink.Type): Promise<void>;
truncate(path: PathLike, len?: number): Promise<void>;
Expand Down Expand Up @@ -245,6 +247,10 @@ export default function createPromisesApi(vol: Volume): null | IPromisesAPI {
return promisify(vol, 'rmdir')(path);
},

rm(path: PathLike, options?: IRmOptions): Promise<void> {
return promisify(vol, 'rm')(path, options);
},

stat(path: PathLike, options?: IStatOptions): Promise<Stats> {
return promisify(vol, 'stat')(path, options);
},
Expand Down
27 changes: 27 additions & 0 deletions src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,15 @@ const getRmdirOptions = (options): IRmdirOptions => {
return Object.assign({}, rmdirDefaults, options);
};

export interface IRmOptions {
force?: boolean;
maxRetries?: number;
recursive?: boolean;
retryDelay?: number;
}
const getRmOpts = optsGenerator<IOptions>(optsDefaults);
const getRmOptsAndCb = optsAndCbGenerator<IRmOptions, any>(getRmOpts);

// Options for `fs.readdir` and `fs.readdirSync`
export interface IReaddirOptions extends IOptions {
withFileTypes?: boolean;
Expand Down Expand Up @@ -1976,6 +1985,24 @@ export class Volume {
this.wrapAsync(this.rmdirBase, [pathToFilename(path), opts], callback);
}

private rmBase(filename: string, options: IRmOptions = {}): void {
const force = !!options.force;
const recursive = !!options.recursive;
const link = this.getLinkAsDirOrThrow(filename, 'rm');
this.deleteLink(link);
}

public rmSync(path: PathLike, options?: IRmOptions): void {
this.rmBase(pathToFilename(path), options);
}

public rm(path: PathLike, callback: TCallback<void>): void;
public rm(path: PathLike, options: IRmOptions, callback: TCallback<void>): void;
public rm(path: PathLike, a: TCallback<void> | IRmOptions, b?: TCallback<void>): void {
const [opts, callback] = getRmOptsAndCb(a, b);
this.wrapAsync(this.rmBase, [pathToFilename(path), opts], callback);
}

private fchmodBase(fd: number, modeNum: number) {
const file = this.getFileByFdOrThrow(fd, 'fchmod');
file.chmod(modeNum);
Expand Down

0 comments on commit 2414fb6

Please sign in to comment.