From 476931487f4105982f4825f808bbc8d9cd4c2df7 Mon Sep 17 00:00:00 2001 From: streamich Date: Sat, 17 Jun 2023 18:38:02 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20implement=20basic=20rena?= =?UTF-8?q?me()=20method,=20only=20for=20files?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/fsa-to-node/FsaNodeFs.ts | 13 +++++++++++++ src/fsa-to-node/__tests__/FsaNodeFs.test.ts | 12 ++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/fsa-to-node/FsaNodeFs.ts b/src/fsa-to-node/FsaNodeFs.ts index 3e90014c1..dfbe70c76 100644 --- a/src/fsa-to-node/FsaNodeFs.ts +++ b/src/fsa-to-node/FsaNodeFs.ts @@ -320,6 +320,19 @@ export class FsaNodeFs implements FsCallbackApi, FsCommonObjects { public readonly rename: FsCallbackApi['rename'] = (oldPath: misc.PathLike, newPath: misc.PathLike, callback: misc.TCallback): void => { const oldPathFilename = pathToFilename(oldPath); const newPathFilename = pathToFilename(newPath); + const [oldFolder, oldName] = pathToLocation(oldPathFilename); + const [newFolder, newName] = pathToLocation(newPathFilename); + (async () => { + const oldFile = await this.getFile(oldFolder, oldName, 'rename'); + const newDir = await this.getDir(newFolder, false, 'rename'); + const newFile = await newDir.getFileHandle(newName, { create: true }); + const writable = await newFile.createWritable({ keepExistingData: false }); + const oldData = await oldFile.getFile(); + await writable.write(await oldData.arrayBuffer()); + await writable.close(); + const oldDir = await this.getDir(oldFolder, false, 'rename'); + await oldDir.removeEntry(oldName); + })().then(() => callback(null), error => callback(error)); }; public readonly exists: FsCallbackApi['exists'] = ( diff --git a/src/fsa-to-node/__tests__/FsaNodeFs.test.ts b/src/fsa-to-node/__tests__/FsaNodeFs.test.ts index 9db5bdb75..0bcef711c 100644 --- a/src/fsa-to-node/__tests__/FsaNodeFs.test.ts +++ b/src/fsa-to-node/__tests__/FsaNodeFs.test.ts @@ -451,3 +451,15 @@ describe('.access()', () => { }); }); }); + +describe('.rename()', () => { + test('can rename a file', async () => { + const { fs, mfs } = setup({ folder: { file: 'test' }, 'empty-folder': null, 'f.html': 'test' }); + await fs.promises.rename('/folder/file', '/folder/file2'); + expect(mfs.__vol.toJSON()).toStrictEqual({ + '/mountpoint/folder/file2': 'test', + '/mountpoint/empty-folder': null, + '/mountpoint/f.html': 'test', + }); + }); +});