From 1ac2df458abe9c85f7fe79d6286cb7e5af5fb782 Mon Sep 17 00:00:00 2001 From: Vadim Dalecky Date: Sun, 18 Jun 2023 23:59:47 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20add=20mkdtempSync()=20me?= =?UTF-8?q?thod?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/fsa/main.ts | 7 ++++++- src/fsa-to-node/FsaNodeFs.ts | 11 +++++++++-- src/fsa-to-node/types.ts | 1 + src/fsa-to-node/worker/FsaNodeSyncWorker.ts | 3 +++ src/node/types/FsSynchronousApi.ts | 1 - 5 files changed, 19 insertions(+), 4 deletions(-) diff --git a/demo/fsa/main.ts b/demo/fsa/main.ts index 4808e3428..1a016b76e 100644 --- a/demo/fsa/main.ts +++ b/demo/fsa/main.ts @@ -61,11 +61,16 @@ const demo = async (dir: fsa.IFileSystemDirectoryHandle) => { console.log('mkdirSync() - can create a nested directory'); fs.mkdirSync('/public/site/assets/img', {recursive: true}); strictEqual(fs.statSync('/public/site/assets/img').isDirectory(), true); + + console.log('mkdtempSync() - can create a temporary directory'); + await fs.promises.mkdir('/tmp'); + const tmpDirName = fs.mkdtempSync('/tmp/temporary-'); + strictEqual(fs.statSync(tmpDirName).isDirectory(), true); }; const main = async () => { const button = document.createElement("button"); - button.textContent = "Select folder"; + button.textContent = "Select an empty folder"; document.body.appendChild(button); button.onclick = async () => { const dir = await (window as any).showDirectoryPicker({id: 'demo', mode: 'readwrite'}); diff --git a/src/fsa-to-node/FsaNodeFs.ts b/src/fsa-to-node/FsaNodeFs.ts index 438653e31..d2bf8f601 100644 --- a/src/fsa-to-node/FsaNodeFs.ts +++ b/src/fsa-to-node/FsaNodeFs.ts @@ -15,6 +15,7 @@ import { getOptions, getStatOptions, getAppendFileOpts, + getDefaultOpts, } from '../node/options'; import { bufToUint8, @@ -875,10 +876,16 @@ export class FsaNodeFs extends FsaNodeCore implements FsCallbackApi, FsSynchrono return this.getSyncAdapter().call('mkdir', [filename, options]); }; + public readonly mkdtempSync: FsSynchronousApi['mkdtempSync'] = (prefix: string, options?: opts.IOptions): misc.TDataOut => { + const {encoding} = getDefaultOpts(options); + if (!prefix || typeof prefix !== 'string') throw new TypeError('filename prefix is required'); + nullCheck(prefix); + const result = this.getSyncAdapter().call('mkdtemp', [prefix, options]); + return strToEncoding(result, encoding); + }; + public readonly ftruncateSync: FsSynchronousApi['ftruncateSync'] = notSupported; public readonly linkSync: FsSynchronousApi['linkSync'] = 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; diff --git a/src/fsa-to-node/types.ts b/src/fsa-to-node/types.ts index de228468b..24db7d05e 100644 --- a/src/fsa-to-node/types.ts +++ b/src/fsa-to-node/types.ts @@ -17,6 +17,7 @@ export interface FsaNodeSyncAdapterApi { rmdir(req: [filename: string, opts?: opts.IRmdirOptions]): void; rm(req: [filename: string, opts?: opts.IRmOptions]): void; mkdir(req: [filename: string, opts?: misc.TMode | opts.IMkdirOptions]): string | undefined; + mkdtemp(req: [filename: string, opts?: misc.TMode | opts.IOptions]): string; } export interface FsaNodeSyncAdapter { diff --git a/src/fsa-to-node/worker/FsaNodeSyncWorker.ts b/src/fsa-to-node/worker/FsaNodeSyncWorker.ts index 803e0a9eb..70f2f8f39 100644 --- a/src/fsa-to-node/worker/FsaNodeSyncWorker.ts +++ b/src/fsa-to-node/worker/FsaNodeSyncWorker.ts @@ -150,5 +150,8 @@ export class FsaNodeSyncWorker { mkdir: async ([filename, options]): Promise => { return await this.fs.promises.mkdir(filename, options); }, + mkdtemp: async ([filename]): Promise => { + return await this.fs.promises.mkdtemp(filename, {encoding: 'utf8'}) as string; + }, }; } diff --git a/src/node/types/FsSynchronousApi.ts b/src/node/types/FsSynchronousApi.ts index 01d4a5d53..3a4fc2d28 100644 --- a/src/node/types/FsSynchronousApi.ts +++ b/src/node/types/FsSynchronousApi.ts @@ -58,7 +58,6 @@ export interface FsSynchronousApi { mkdirSync(path: misc.PathLike, options: opts.IMkdirOptions & { recursive: true }): string | undefined; mkdirSync(path: misc.PathLike, options?: misc.TMode | (opts.IMkdirOptions & { recursive?: false })): void; mkdirSync(path: misc.PathLike, options?: misc.TMode | opts.IMkdirOptions): string | undefined; - mkdirpSync(path: misc.PathLike, mode?: misc.TMode): void; mkdtempSync(prefix: string, options?: opts.IOptions): misc.TDataOut; rmdirSync(path: misc.PathLike, options?: opts.IRmdirOptions): void; rmSync(path: misc.PathLike, options?: opts.IRmOptions): void;