Skip to content

Commit

Permalink
feat: 🎸 improve mkdir method
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 20, 2023
1 parent 06f7a37 commit e6dd59e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 26 deletions.
30 changes: 13 additions & 17 deletions src/fsa-to-node/FsaNodeFs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,29 +229,25 @@ export class FsaNodeFs implements FsCallbackApi {
.then(
() => callback(null),
error => {
if (!error || typeof error !== 'object') {
callback(createError('', 'mkdir'));
return;
}
switch (error.name) {
case 'NotFoundError': {
const err = createError('ENOTDIR', 'mkdir', folder.join('/'));
callback(err);
}
default: {
callback(createError('', 'mkdir'));
if (error && typeof error === 'object') {
switch (error.name) {
case 'NotFoundError': {
const err = createError('ENOENT', 'mkdir', folder.join('/'));
callback(err);
return;
}
case 'TypeMismatchError': {
const err = createError('ENOTDIR', 'mkdir', folder.join('/'));
callback(err);
return;
}
}
}
callback(error);
},
);
};

mkdirp(path: misc.PathLike, callback: misc.TCallback<string>);
mkdirp(path: misc.PathLike, mode: misc.TMode, callback: misc.TCallback<string>);
mkdirp(path: misc.PathLike, a: misc.TCallback<string> | misc.TMode, b?: misc.TCallback<string>) {
throw new Error('Not implemented');
}

mkdtemp(prefix: string, callback: misc.TCallback<void>): void;
mkdtemp(prefix: string, options: opts.IOptions, callback: misc.TCallback<void>);
mkdtemp(prefix: string, a: misc.TCallback<void> | opts.IOptions, b?: misc.TCallback<void>) {
Expand Down
18 changes: 14 additions & 4 deletions src/fsa-to-node/__tests__/FsaNodeFs.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { IFsWithVolume, memfs } from '../..';
import { IFsWithVolume, NestedDirectoryJSON, memfs } from '../..';
import { nodeToFsa } from '../../node-to-fsa';
import { FsaNodeFs } from '../FsaNodeFs';

const setup = () => {
const mfs = memfs({ mountpoint: null }) as IFsWithVolume;
const setup = (json: NestedDirectoryJSON | null = null) => {
const mfs = memfs({ mountpoint: json }) as IFsWithVolume;
const dir = nodeToFsa(mfs, '/mountpoint', { mode: 'readwrite' });
const fs = new FsaNodeFs(dir);
return { fs, mfs, dir };
Expand Down Expand Up @@ -32,7 +32,7 @@ describe('.mkdir()', () => {
);
throw new Error('Expected error');
} catch (error) {
expect(error.code).toBe('ENOTDIR');
expect(error.code).toBe('ENOENT');
}
});

Expand All @@ -52,4 +52,14 @@ describe('.mkdir()', () => {
await fs.promises.mkdir('/test/subtest', { recursive: true });
expect(mfs.statSync('/mountpoint/test/subtest').isDirectory()).toBe(true);
});

test('cannot create a folder over a file', async () => {
const { fs } = setup({file: 'test'});
try {
await fs.promises.mkdir('/file/folder', { recursive: true });
throw new Error('Expected error');
} catch (error) {
expect(error.code).toBe('ENOTDIR');
}
});
});
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import {
IReadStream,
IWriteStream,
DirectoryJSON,
NestedDirectoryJSON,
} from './volume';
const { fsSyncMethods, fsAsyncMethods } = require('fs-monkey/lib/util/lists');
import { constants } from './constants';
import type { FsPromisesApi } from './node/types';
const { F_OK, R_OK, W_OK, X_OK } = constants;

export { DirectoryJSON };
export { DirectoryJSON, NestedDirectoryJSON };
export const Volume = _Volume;

// Default volume.
Expand Down Expand Up @@ -63,8 +64,8 @@ export const fs: IFs = createFsFromVolume(vol);
* @returns A `memfs` file system instance, which is a drop-in replacement for
* the `fs` module.
*/
export const memfs = (json: DirectoryJSON = {}, cwd: string = '/'): IFs => {
const volume = Volume.fromJSON(json, cwd);
export const memfs = (json: NestedDirectoryJSON = {}, cwd: string = '/'): IFs => {
const volume = Volume.fromNestedJSON(json, cwd);
const fs = createFsFromVolume(volume);
return fs;
};
Expand Down
2 changes: 0 additions & 2 deletions src/node/types/callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ export interface FsCallbackApi {
);
mkdir(path: misc.PathLike, mode: opts.IMkdirOptions & { recursive: true }, callback: misc.TCallback<string>);
mkdir(path: misc.PathLike, mode: misc.TMode | opts.IMkdirOptions, callback: misc.TCallback<string>);
mkdirp(path: misc.PathLike, callback: misc.TCallback<string>);
mkdirp(path: misc.PathLike, mode: misc.TMode, callback: misc.TCallback<string>);
mkdtemp(prefix: string, callback: misc.TCallback<void>): void;
mkdtemp(prefix: string, options: opts.IOptions, callback: misc.TCallback<void>);
rmdir(path: misc.PathLike, callback: misc.TCallback<void>);
Expand Down

0 comments on commit e6dd59e

Please sign in to comment.