Skip to content

Commit

Permalink
style: 💄 run Prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 16, 2023
1 parent 5d6f976 commit f6bc998
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/Dirent.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Link } from './node';
import { constants } from './constants';
import { TEncodingExtended, strToEncoding, TDataOut } from './encoding';
import type {IDirent} from './node/types/misc';
import type { IDirent } from './node/types/misc';

const { S_IFMT, S_IFDIR, S_IFREG, S_IFBLK, S_IFCHR, S_IFLNK, S_IFIFO, S_IFSOCK } = constants;

Expand Down
8 changes: 4 additions & 4 deletions src/fsa-to-node/FsaNodeDirent.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {NodeFileSystemDirectoryHandle, NodeFileSystemFileHandle} from "../node-to-fsa";
import type {IFileSystemHandle} from "../fsa/types";
import type {IDirent, TDataOut} from "../node/types/misc";
import { NodeFileSystemDirectoryHandle, NodeFileSystemFileHandle } from '../node-to-fsa';
import type { IFileSystemHandle } from '../fsa/types';
import type { IDirent, TDataOut } from '../node/types/misc';

export class FsaNodeDirent implements IDirent {
public constructor(public readonly name: TDataOut, protected readonly handle: IFileSystemHandle) {}

isDirectory(): boolean {
return this.handle instanceof NodeFileSystemDirectoryHandle;
}

isFile(): boolean {
return this.handle instanceof NodeFileSystemFileHandle;
}
Expand Down
88 changes: 59 additions & 29 deletions src/fsa-to-node/FsaNodeFs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { strToEncoding } from '../encoding';
import { FsaToNodeConstants } from './constants';
import { bufferToEncoding } from '../volume';
import { FsaNodeFsOpenFile } from './FsaNodeFsOpenFile';
import {FsaNodeDirent} from './FsaNodeDirent';
import { FsaNodeDirent } from './FsaNodeDirent';
import type { FsCallbackApi, FsPromisesApi } from '../node/types';
import type * as misc from '../node/types/misc';
import type * as opts from '../node/types/options';
Expand Down Expand Up @@ -297,31 +297,40 @@ export class FsaNodeFs implements FsCallbackApi {
const [folder, name] = pathToLocation(filename);
folder.push(name);
this.getDir(folder, false, 'readdir')
.then(dir => (async () => {
if (options.withFileTypes) {
const list: misc.IDirent[] = [];
for await (const [name, handle] of dir.entries()) {
const dirent = new FsaNodeDirent(name, handle);
list.push(dirent);
.then(dir =>
(async () => {
if (options.withFileTypes) {
const list: misc.IDirent[] = [];
for await (const [name, handle] of dir.entries()) {
const dirent = new FsaNodeDirent(name, handle);
list.push(dirent);
}
if (!isWin && options.encoding !== 'buffer')
list.sort((a, b) => {
if (a.name < b.name) return -1;
if (a.name > b.name) return 1;
return 0;
});
return list;
} else {
const list: string[] = [];
for await (const key of dir.keys()) list.push(key);
if (!isWin && options.encoding !== 'buffer') list.sort();
return list;
}
if (!isWin && options.encoding !== 'buffer')
list.sort((a, b) => {
if (a.name < b.name) return -1;
if (a.name > b.name) return 1;
return 0;
});
return list;
} else {
const list: string[] = [];
for await (const key of dir.keys()) list.push(key);
if (!isWin && options.encoding !== 'buffer') list.sort();
return list;
}
})())
.then(res => callback(null, res), err => callback(err));
})(),
)
.then(
res => callback(null, res),
err => callback(err),
);
};

public readonly readlink: FsCallbackApi['readlink'] = (path: misc.PathLike, a: misc.TCallback<misc.TDataOut> | opts.IOptions, b?: misc.TCallback<misc.TDataOut>) => {
public readonly readlink: FsCallbackApi['readlink'] = (
path: misc.PathLike,
a: misc.TCallback<misc.TDataOut> | opts.IOptions,
b?: misc.TCallback<misc.TDataOut>,
) => {
const [opts, callback] = getDefaultOptsAndCb(a, b);
const filename = pathToFilename(path);
const buffer = Buffer.from(filename);
Expand All @@ -336,16 +345,27 @@ export class FsaNodeFs implements FsCallbackApi {
callback(null);
};

public readonly ftruncate: FsCallbackApi['ftruncate'] = (fd: number, a: misc.TCallback<void> | number, b?: misc.TCallback<void>): void => {
public readonly ftruncate: FsCallbackApi['ftruncate'] = (
fd: number,
a: misc.TCallback<void> | number,
b?: misc.TCallback<void>,
): void => {
const len: number = typeof a === 'number' ? a : 0;
const callback: misc.TCallback<void> = validateCallback(typeof a === 'number' ? b : a);
this.getFileByFd(fd)
.then(file => file.file.createWritable({keepExistingData: true}))
.then(file => file.file.createWritable({ keepExistingData: true }))
.then(writable => writable.truncate(len).then(() => writable.close()))
.then(() => callback(null), error => callback(error));
.then(
() => callback(null),
error => callback(error),
);
};

public readonly truncate: FsCallbackApi['truncate'] = (path: misc.PathLike, a: misc.TCallback<void> | number, b?: misc.TCallback<void>) => {
public readonly truncate: FsCallbackApi['truncate'] = (
path: misc.PathLike,
a: misc.TCallback<void> | number,
b?: misc.TCallback<void>,
) => {
const len: number = typeof a === 'number' ? a : 0;
const callback: misc.TCallback<void> = validateCallback(typeof a === 'number' ? b : a);
this.open(path, 'r+', (error, fd) => {
Expand All @@ -359,11 +379,21 @@ export class FsaNodeFs implements FsCallbackApi {
});
};

public readonly futimes: FsCallbackApi['futimes'] = (fd: number, atime: misc.TTime, mtime: misc.TTime, callback: misc.TCallback<void>): void => {
public readonly futimes: FsCallbackApi['futimes'] = (
fd: number,
atime: misc.TTime,
mtime: misc.TTime,
callback: misc.TCallback<void>,
): void => {
callback(null);
};

public readonly utimes: FsCallbackApi['utimes'] = (path: misc.PathLike, atime: misc.TTime, mtime: misc.TTime, callback: misc.TCallback<void>): void => {
public readonly utimes: FsCallbackApi['utimes'] = (
path: misc.PathLike,
atime: misc.TTime,
mtime: misc.TTime,
callback: misc.TCallback<void>,
): void => {
callback(null);
};

Expand Down
12 changes: 6 additions & 6 deletions src/fsa-to-node/__tests__/FsaNodeFs.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IFsWithVolume, NestedDirectoryJSON, memfs } from '../..';
import { nodeToFsa } from '../../node-to-fsa';
import {IDirent} from '../../node/types/misc';
import { IDirent } from '../../node/types/misc';
import { FsaNodeFs } from '../FsaNodeFs';

const setup = (json: NestedDirectoryJSON | null = null) => {
Expand Down Expand Up @@ -245,7 +245,7 @@ describe('.truncate()', () => {
test('can truncate a file', async () => {
const { fs, mfs } = setup({ folder: { file: 'test' }, 'empty-folder': null });
const res = await new Promise<unknown>((resolve, reject) => {
fs.truncate('/folder/file', 2, (err, res) => err ? reject(err) : resolve(res));
fs.truncate('/folder/file', 2, (err, res) => (err ? reject(err) : resolve(res)));
});
expect(res).toBe(undefined);
expect(mfs.readFileSync('/mountpoint/folder/file', 'utf8')).toBe('te');
Expand All @@ -257,7 +257,7 @@ describe('.ftruncate()', () => {
const { fs, mfs } = setup({ folder: { file: 'test' }, 'empty-folder': null });
const handle = await fs.promises.open('/folder/file');
const res = await new Promise<unknown>((resolve, reject) => {
fs.ftruncate(handle.fd, 3, (err, res) => err ? reject(err) : resolve(res));
fs.ftruncate(handle.fd, 3, (err, res) => (err ? reject(err) : resolve(res)));
});
expect(res).toBe(undefined);
expect(mfs.readFileSync('/mountpoint/folder/file', 'utf8')).toBe('tes');
Expand All @@ -267,7 +267,7 @@ describe('.ftruncate()', () => {
describe('.readdir()', () => {
test('can read directory contents as strings', async () => {
const { fs, mfs } = setup({ folder: { file: 'test' }, 'empty-folder': null, 'f.html': 'test' });
const res = await fs.promises.readdir('/') as string[];
const res = (await fs.promises.readdir('/')) as string[];
expect(res.length).toBe(3);
expect(res.includes('folder')).toBe(true);
expect(res.includes('empty-folder')).toBe(true);
Expand All @@ -276,9 +276,9 @@ describe('.readdir()', () => {

test('can read directory contents with "withFileTypes" flag set', async () => {
const { fs, mfs } = setup({ folder: { file: 'test' }, 'empty-folder': null, 'f.html': 'test' });
const list = await fs.promises.readdir('/', {withFileTypes: true}) as IDirent[];
const list = (await fs.promises.readdir('/', { withFileTypes: true })) as IDirent[];
expect(list.length).toBe(3);
const names = list.map((item) => item.name);
const names = list.map(item => item.name);
expect(names).toStrictEqual(['empty-folder', 'f.html', 'folder']);
expect(list.find(item => item.name === 'folder')?.isDirectory()).toBe(true);
expect(list.find(item => item.name === 'empty-folder')?.isDirectory()).toBe(true);
Expand Down
4 changes: 3 additions & 1 deletion src/node/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,6 @@ const readdirDefaults: opts.IReaddirOptions = {
withFileTypes: false,
};
export const getReaddirOptions = optsGenerator<opts.IReaddirOptions>(readdirDefaults);
export const getReaddirOptsAndCb = optsAndCbGenerator<opts.IReaddirOptions, misc.TDataOut[] | misc.IDirent[]>(getReaddirOptions);
export const getReaddirOptsAndCb = optsAndCbGenerator<opts.IReaddirOptions, misc.TDataOut[] | misc.IDirent[]>(
getReaddirOptions,
);

0 comments on commit f6bc998

Please sign in to comment.