From bcad970b0b9cf704abf2e7ffd55d72078a54a79d Mon Sep 17 00:00:00 2001 From: Vadim Dalecky Date: Sun, 18 Jun 2023 23:47:36 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20add=20mkdirSync()=20meth?= =?UTF-8?q?od?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/fsa/main.ts | 4 ++++ src/fsa-to-node/FsaNodeFs.ts | 7 ++++++- src/fsa-to-node/types.ts | 2 ++ src/fsa-to-node/worker/FsaNodeSyncWorker.ts | 3 +++ src/node/promises.ts | 2 +- src/node/types/promises.ts | 2 +- 6 files changed, 17 insertions(+), 3 deletions(-) diff --git a/demo/fsa/main.ts b/demo/fsa/main.ts index c0c0d728b..4808e3428 100644 --- a/demo/fsa/main.ts +++ b/demo/fsa/main.ts @@ -57,6 +57,10 @@ const demo = async (dir: fsa.IFileSystemDirectoryHandle) => { strictEqual(fs.existsSync('/dir/tmp'), true); fs.rmSync('/dir/tmp'); strictEqual(fs.existsSync('/dir/tmp'), false); + + 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); }; const main = async () => { diff --git a/src/fsa-to-node/FsaNodeFs.ts b/src/fsa-to-node/FsaNodeFs.ts index 0a5e5b72c..438653e31 100644 --- a/src/fsa-to-node/FsaNodeFs.ts +++ b/src/fsa-to-node/FsaNodeFs.ts @@ -868,7 +868,12 @@ export class FsaNodeFs extends FsaNodeCore implements FsCallbackApi, FsSynchrono adapter.call('rm', [filename, options]); }; - public readonly mkdirSync: FsSynchronousApi['mkdirSync'] = notSupported; + public readonly mkdirSync: FsSynchronousApi['mkdirSync'] = (path: misc.PathLike, options?: misc.TMode | opts.IMkdirOptions): string | undefined => { + const opts = getMkdirOptions(options); + const modeNum = modeToNumber(opts.mode, 0o777); + const filename = pathToFilename(path); + return this.getSyncAdapter().call('mkdir', [filename, options]); + }; public readonly ftruncateSync: FsSynchronousApi['ftruncateSync'] = notSupported; public readonly linkSync: FsSynchronousApi['linkSync'] = notSupported; diff --git a/src/fsa-to-node/types.ts b/src/fsa-to-node/types.ts index d7e08313d..de228468b 100644 --- a/src/fsa-to-node/types.ts +++ b/src/fsa-to-node/types.ts @@ -1,4 +1,5 @@ import type * as opts from '../node/types/options'; +import type * as misc from '../node/types/misc'; export type FsLocation = [folder: string[], file: string]; @@ -15,6 +16,7 @@ export interface FsaNodeSyncAdapterApi { move(req: { src: string; dst: string }): void; 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; } export interface FsaNodeSyncAdapter { diff --git a/src/fsa-to-node/worker/FsaNodeSyncWorker.ts b/src/fsa-to-node/worker/FsaNodeSyncWorker.ts index 860bdd3e8..803e0a9eb 100644 --- a/src/fsa-to-node/worker/FsaNodeSyncWorker.ts +++ b/src/fsa-to-node/worker/FsaNodeSyncWorker.ts @@ -147,5 +147,8 @@ export class FsaNodeSyncWorker { rm: async ([filename, options]): Promise => { await this.fs.promises.rm(filename, options); }, + mkdir: async ([filename, options]): Promise => { + return await this.fs.promises.mkdir(filename, options); + }, }; } diff --git a/src/node/promises.ts b/src/node/promises.ts index d75143ec5..657de0cb2 100644 --- a/src/node/promises.ts +++ b/src/node/promises.ts @@ -48,7 +48,7 @@ export function createPromisesApi(vol: FsCallbackApi): FsPromisesApi { return promisify(vol, 'lstat')(path, options); }, - mkdir(path: misc.PathLike, options?: misc.TMode | opts.IMkdirOptions): Promise { + mkdir(path: misc.PathLike, options?: misc.TMode | opts.IMkdirOptions): Promise { return promisify(vol, 'mkdir')(path, options); }, diff --git a/src/node/types/promises.ts b/src/node/types/promises.ts index 091559cca..3b3ba85b4 100644 --- a/src/node/types/promises.ts +++ b/src/node/types/promises.ts @@ -12,7 +12,7 @@ export interface FsPromisesApi { lchown(path: misc.PathLike, uid: number, gid: number): Promise; link(existingPath: misc.PathLike, newPath: misc.PathLike): Promise; lstat(path: misc.PathLike, options?: opts.IStatOptions): Promise; - mkdir(path: misc.PathLike, options?: misc.TMode | opts.IMkdirOptions): Promise; + mkdir(path: misc.PathLike, options?: misc.TMode | opts.IMkdirOptions): Promise; mkdtemp(prefix: string, options?: opts.IOptions): Promise; open(path: misc.PathLike, flags?: misc.TFlags, mode?: misc.TMode): Promise; readdir(path: misc.PathLike, options?: opts.IReaddirOptions | string): Promise;