From 4b3444dd5ef16039cb48c02ebfd73f096aa92e8e Mon Sep 17 00:00:00 2001 From: Vadim Dalecky Date: Mon, 19 Jun 2023 00:16:12 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20add=20unlinkSync()=20met?= =?UTF-8?q?hod?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/fsa/main.ts | 5 +++++ src/fsa-to-node/FsaNodeFs.ts | 6 +++++- src/fsa-to-node/types.ts | 1 + src/fsa-to-node/worker/FsaNodeSyncWorker.ts | 3 +++ 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/demo/fsa/main.ts b/demo/fsa/main.ts index dceeee119..bc359d66f 100644 --- a/demo/fsa/main.ts +++ b/demo/fsa/main.ts @@ -71,6 +71,11 @@ const demo = async (dir: fsa.IFileSystemDirectoryHandle) => { await fs.promises.writeFile('/truncated.txt', 'truncate here: abcdefghijklmnopqrstuvwxyz'); fs.truncateSync('/truncated.txt', 14); strictEqual(fs.readFileSync('/truncated.txt', 'utf8'), 'truncate here:'); + + console.log('unlinkSync() - can delete a file'); + await fs.promises.writeFile('/delete-me.txt', 'abc'); + fs.unlinkSync('/delete-me.txt'); + strictEqual(fs.existsSync('/delete-me.txt'), false); }; const main = async () => { diff --git a/src/fsa-to-node/FsaNodeFs.ts b/src/fsa-to-node/FsaNodeFs.ts index 96f03428e..bb6e57bb8 100644 --- a/src/fsa-to-node/FsaNodeFs.ts +++ b/src/fsa-to-node/FsaNodeFs.ts @@ -902,11 +902,15 @@ export class FsaNodeFs extends FsaNodeCore implements FsCallbackApi, FsSynchrono this.truncateSync(filename, len); }; + public readonly unlinkSync: FsSynchronousApi['unlinkSync'] = (path: misc.PathLike): void => { + const filename = pathToFilename(path); + this.getSyncAdapter().call('unlink', [filename]); + }; + public readonly openSync: FsSynchronousApi['openSync'] = notSupported; public readonly readdirSync: FsSynchronousApi['readdirSync'] = notSupported; public readonly readSync: FsSynchronousApi['readSync'] = notSupported; public readonly realpathSync: FsSynchronousApi['realpathSync'] = notSupported; - public readonly unlinkSync: FsSynchronousApi['unlinkSync'] = notSupported; public readonly writeSync: FsSynchronousApi['writeSync'] = notSupported; public readonly chmodSync: FsSynchronousApi['chmodSync'] = noop; diff --git a/src/fsa-to-node/types.ts b/src/fsa-to-node/types.ts index 50de9e687..1f51cd47f 100644 --- a/src/fsa-to-node/types.ts +++ b/src/fsa-to-node/types.ts @@ -19,6 +19,7 @@ export interface FsaNodeSyncAdapterApi { mkdir(req: [filename: string, opts?: misc.TMode | opts.IMkdirOptions]): string | undefined; mkdtemp(req: [filename: string, opts?: misc.TMode | opts.IOptions]): string; trunc(req: [filename: string, len: number]): void; + unlink(req: [filename: string]): void; } export interface FsaNodeSyncAdapter { diff --git a/src/fsa-to-node/worker/FsaNodeSyncWorker.ts b/src/fsa-to-node/worker/FsaNodeSyncWorker.ts index 69ef8f9b4..e72da6e1c 100644 --- a/src/fsa-to-node/worker/FsaNodeSyncWorker.ts +++ b/src/fsa-to-node/worker/FsaNodeSyncWorker.ts @@ -156,5 +156,8 @@ export class FsaNodeSyncWorker { trunc: async ([filename, len]): Promise => { await this.fs.promises.truncate(filename, len); }, + unlink: async ([filename]): Promise => { + await this.fs.promises.unlink(filename); + }, }; }