Skip to content

Commit

Permalink
feat: 🎸 add rmSync() method
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 20, 2023
1 parent 59ccf3c commit a39e9a2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
6 changes: 6 additions & 0 deletions demo/fsa/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ const demo = async (dir: fsa.IFileSystemDirectoryHandle) => {
strictEqual(fs.existsSync('/to-be-deleted'), true);
fs.rmdirSync('/to-be-deleted');
strictEqual(fs.existsSync('/to-be-deleted'), false);

console.log('rmSync() - can delete a file');
await fs.promises.writeFile('/dir/tmp', '...');
strictEqual(fs.existsSync('/dir/tmp'), true);
fs.rmSync('/dir/tmp');
strictEqual(fs.existsSync('/dir/tmp'), false);
};

const main = async () => {
Expand Down
13 changes: 9 additions & 4 deletions src/fsa-to-node/FsaNodeFs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,6 @@ export class FsaNodeFs extends FsaNodeCore implements FsCallbackApi, FsSynchrono
const [options, callback] = getRmOptsAndCb(a, b);
const [folder, name] = pathToLocation(pathToFilename(path));
this.getDir(folder, false, 'rmdir')
.then(dir => dir.getDirectoryHandle(name).then(() => dir))
.then(dir => dir.removeEntry(name, { recursive: options.recursive ?? false }))
.then(
() => callback(null),
Expand Down Expand Up @@ -860,20 +859,26 @@ export class FsaNodeFs extends FsaNodeCore implements FsCallbackApi, FsSynchrono
public readonly rmdirSync: FsSynchronousApi['rmdirSync'] = (path: misc.PathLike, opts?: opts.IRmdirOptions): void => {
const filename = pathToFilename(path);
const adapter = this.getSyncAdapter();
adapter.call('rmdir', {path: filename, opts});
adapter.call('rmdir', [filename, opts]);
};

public readonly rmSync: FsSynchronousApi['rmSync'] = (path: misc.PathLike, options?: opts.IRmOptions): void => {
const filename = pathToFilename(path);
const adapter = this.getSyncAdapter();
adapter.call('rm', [filename, options]);
};

public readonly mkdirSync: FsSynchronousApi['mkdirSync'] = notSupported;

public readonly ftruncateSync: FsSynchronousApi['ftruncateSync'] = notSupported;
public readonly linkSync: FsSynchronousApi['linkSync'] = notSupported;
public readonly mkdirSync: FsSynchronousApi['mkdirSync'] = notSupported;
public readonly mkdirpSync: FsSynchronousApi['mkdirpSync'] = notSupported;
public readonly mkdtempSync: FsSynchronousApi['mkdtempSync'] = notSupported;
public readonly openSync: FsSynchronousApi['openSync'] = notSupported;
public readonly readdirSync: FsSynchronousApi['readdirSync'] = notSupported;
public readonly readlinkSync: FsSynchronousApi['readlinkSync'] = notSupported;
public readonly readSync: FsSynchronousApi['readSync'] = notSupported;
public readonly realpathSync: FsSynchronousApi['realpathSync'] = notSupported;
public readonly rmSync: FsSynchronousApi['rmSync'] = notSupported;
public readonly symlinkSync: FsSynchronousApi['symlinkSync'] = notSupported;
public readonly truncateSync: FsSynchronousApi['truncateSync'] = notSupported;
public readonly unlinkSync: FsSynchronousApi['unlinkSync'] = notSupported;
Expand Down
9 changes: 9 additions & 0 deletions src/fsa-to-node/__tests__/FsaNodeFs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,15 @@ onlyOnNode20('FsaNodeFs', () => {
const { fs } = setup({ folder: { subfolder: { file: 'test' } }, 'empty-folder': null });
await fs.promises.rm('/lala/lulu', { recursive: true, force: true });
});

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

describe('.unlink()', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/fsa-to-node/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export interface FsaNodeSyncAdapterApi {
appendFile(req: { filename: string; data: Uint8Array; opts?: opts.IAppendFileOptions }): void;
copy(req: { src: string; dst: string, flags?: number }): void;
move(req: { src: string; dst: string }): void;
rmdir(req: { path: string; opts?: opts.IRmdirOptions }): void;
rmdir(req: [filename: string, opts?: opts.IRmdirOptions]): void;
rm(req: [filename: string, opts?: opts.IRmOptions]): void;
}

export interface FsaNodeSyncAdapter {
Expand Down
7 changes: 5 additions & 2 deletions src/fsa-to-node/worker/FsaNodeSyncWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,11 @@ export class FsaNodeSyncWorker {
move: async ({ src, dst }): Promise<void> => {
await this.fs.promises.rename(src, dst);
},
rmdir: async ({ path, opts }): Promise<void> => {
await this.fs.promises.rmdir(path, opts);
rmdir: async ([filename, options]): Promise<void> => {
await this.fs.promises.rmdir(filename, options);
},
rm: async ([filename, options]): Promise<void> => {
await this.fs.promises.rm(filename, options);
},
};
}

0 comments on commit a39e9a2

Please sign in to comment.