Skip to content

Commit

Permalink
feat: 🎸 add mkdirSync() method
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 18, 2023
1 parent aa9acb3 commit bcad970
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 3 deletions.
4 changes: 4 additions & 0 deletions demo/fsa/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
7 changes: 6 additions & 1 deletion src/fsa-to-node/FsaNodeFs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/fsa-to-node/types.ts
Original file line number Diff line number Diff line change
@@ -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];

Expand All @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions src/fsa-to-node/worker/FsaNodeSyncWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,8 @@ export class FsaNodeSyncWorker {
rm: async ([filename, options]): Promise<void> => {
await this.fs.promises.rm(filename, options);
},
mkdir: async ([filename, options]): Promise<string | undefined> => {
return await this.fs.promises.mkdir(filename, options);
},
};
}
2 changes: 1 addition & 1 deletion src/node/promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
mkdir(path: misc.PathLike, options?: misc.TMode | opts.IMkdirOptions): Promise<string | undefined> {
return promisify(vol, 'mkdir')(path, options);
},

Expand Down
2 changes: 1 addition & 1 deletion src/node/types/promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface FsPromisesApi {
lchown(path: misc.PathLike, uid: number, gid: number): Promise<void>;
link(existingPath: misc.PathLike, newPath: misc.PathLike): Promise<void>;
lstat(path: misc.PathLike, options?: opts.IStatOptions): Promise<misc.IStats>;
mkdir(path: misc.PathLike, options?: misc.TMode | opts.IMkdirOptions): Promise<void>;
mkdir(path: misc.PathLike, options?: misc.TMode | opts.IMkdirOptions): Promise<string | undefined>;
mkdtemp(prefix: string, options?: opts.IOptions): Promise<misc.TDataOut>;
open(path: misc.PathLike, flags?: misc.TFlags, mode?: misc.TMode): Promise<misc.IFileHandle>;
readdir(path: misc.PathLike, options?: opts.IReaddirOptions | string): Promise<misc.TDataOut[] | misc.IDirent[]>;
Expand Down

0 comments on commit bcad970

Please sign in to comment.