diff --git a/src/Dirent.ts b/src/Dirent.ts index 362e139a1..be157a7ad 100644 --- a/src/Dirent.ts +++ b/src/Dirent.ts @@ -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; diff --git a/src/fsa-to-node/FsaNodeDirent.ts b/src/fsa-to-node/FsaNodeDirent.ts index 62317f093..85b501798 100644 --- a/src/fsa-to-node/FsaNodeDirent.ts +++ b/src/fsa-to-node/FsaNodeDirent.ts @@ -1,6 +1,6 @@ -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) {} @@ -8,7 +8,7 @@ export class FsaNodeDirent implements IDirent { isDirectory(): boolean { return this.handle instanceof NodeFileSystemDirectoryHandle; } - + isFile(): boolean { return this.handle instanceof NodeFileSystemFileHandle; } diff --git a/src/fsa-to-node/FsaNodeFs.ts b/src/fsa-to-node/FsaNodeFs.ts index c43bb3b60..db916e278 100644 --- a/src/fsa-to-node/FsaNodeFs.ts +++ b/src/fsa-to-node/FsaNodeFs.ts @@ -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'; @@ -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 | opts.IOptions, b?: misc.TCallback) => { + public readonly readlink: FsCallbackApi['readlink'] = ( + path: misc.PathLike, + a: misc.TCallback | opts.IOptions, + b?: misc.TCallback, + ) => { const [opts, callback] = getDefaultOptsAndCb(a, b); const filename = pathToFilename(path); const buffer = Buffer.from(filename); @@ -336,16 +345,27 @@ export class FsaNodeFs implements FsCallbackApi { callback(null); }; - public readonly ftruncate: FsCallbackApi['ftruncate'] = (fd: number, a: misc.TCallback | number, b?: misc.TCallback): void => { + public readonly ftruncate: FsCallbackApi['ftruncate'] = ( + fd: number, + a: misc.TCallback | number, + b?: misc.TCallback, + ): void => { const len: number = typeof a === 'number' ? a : 0; const callback: misc.TCallback = 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 | number, b?: misc.TCallback) => { + public readonly truncate: FsCallbackApi['truncate'] = ( + path: misc.PathLike, + a: misc.TCallback | number, + b?: misc.TCallback, + ) => { const len: number = typeof a === 'number' ? a : 0; const callback: misc.TCallback = validateCallback(typeof a === 'number' ? b : a); this.open(path, 'r+', (error, fd) => { @@ -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 => { + public readonly futimes: FsCallbackApi['futimes'] = ( + fd: number, + atime: misc.TTime, + mtime: misc.TTime, + callback: misc.TCallback, + ): void => { callback(null); }; - public readonly utimes: FsCallbackApi['utimes'] = (path: misc.PathLike, atime: misc.TTime, mtime: misc.TTime, callback: misc.TCallback): void => { + public readonly utimes: FsCallbackApi['utimes'] = ( + path: misc.PathLike, + atime: misc.TTime, + mtime: misc.TTime, + callback: misc.TCallback, + ): void => { callback(null); }; diff --git a/src/fsa-to-node/__tests__/FsaNodeFs.test.ts b/src/fsa-to-node/__tests__/FsaNodeFs.test.ts index d082a0d53..622356094 100644 --- a/src/fsa-to-node/__tests__/FsaNodeFs.test.ts +++ b/src/fsa-to-node/__tests__/FsaNodeFs.test.ts @@ -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) => { @@ -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((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'); @@ -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((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'); @@ -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); @@ -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); diff --git a/src/node/options.ts b/src/node/options.ts index 3e7b08302..1d9a347d7 100644 --- a/src/node/options.ts +++ b/src/node/options.ts @@ -75,4 +75,6 @@ const readdirDefaults: opts.IReaddirOptions = { withFileTypes: false, }; export const getReaddirOptions = optsGenerator(readdirDefaults); -export const getReaddirOptsAndCb = optsAndCbGenerator(getReaddirOptions); +export const getReaddirOptsAndCb = optsAndCbGenerator( + getReaddirOptions, +);