diff --git a/src/promises.ts b/src/promises.ts index 3b4a2d5b..96a12032 100644 --- a/src/promises.ts +++ b/src/promises.ts @@ -1,6 +1,5 @@ import { Volume, - TFilePath, TData, TMode, TFlags, @@ -20,6 +19,7 @@ import { Buffer } from './internal/buffer'; import Stats from './Stats'; import Dirent from './Dirent'; import { TDataOut } from './encoding'; +import { PathLike } from 'fs'; function promisify( vol: Volume, @@ -66,33 +66,33 @@ export interface IFileHandle { writeFile(data: TData, options?: IWriteFileOptions): Promise; } -export type TFileHandle = TFilePath | IFileHandle; +export type TFileHandle = PathLike | IFileHandle; export interface IPromisesAPI { FileHandle; - access(path: TFilePath, mode?: number): Promise; + access(path: PathLike, mode?: number): Promise; appendFile(path: TFileHandle, data: TData, options?: IAppendFileOptions | string): Promise; - chmod(path: TFilePath, mode: TMode): Promise; - chown(path: TFilePath, uid: number, gid: number): Promise; - copyFile(src: TFilePath, dest: TFilePath, flags?: TFlagsCopy): Promise; - lchmod(path: TFilePath, mode: TMode): Promise; - lchown(path: TFilePath, uid: number, gid: number): Promise; - link(existingPath: TFilePath, newPath: TFilePath): Promise; - lstat(path: TFilePath, options?: IStatOptions): Promise; - mkdir(path: TFilePath, options?: TMode | IMkdirOptions): Promise; + chmod(path: PathLike, mode: TMode): Promise; + chown(path: PathLike, uid: number, gid: number): Promise; + copyFile(src: PathLike, dest: PathLike, flags?: TFlagsCopy): Promise; + lchmod(path: PathLike, mode: TMode): Promise; + lchown(path: PathLike, uid: number, gid: number): Promise; + link(existingPath: PathLike, newPath: PathLike): Promise; + lstat(path: PathLike, options?: IStatOptions): Promise; + mkdir(path: PathLike, options?: TMode | IMkdirOptions): Promise; mkdtemp(prefix: string, options?: IOptions): Promise; - open(path: TFilePath, flags: TFlags, mode?: TMode): Promise; - readdir(path: TFilePath, options?: IReaddirOptions | string): Promise; + open(path: PathLike, flags: TFlags, mode?: TMode): Promise; + readdir(path: PathLike, options?: IReaddirOptions | string): Promise; readFile(id: TFileHandle, options?: IReadFileOptions | string): Promise; - readlink(path: TFilePath, options?: IOptions): Promise; - realpath(path: TFilePath, options?: IRealpathOptions | string): Promise; - rename(oldPath: TFilePath, newPath: TFilePath): Promise; - rmdir(path: TFilePath): Promise; - stat(path: TFilePath, options?: IStatOptions): Promise; - symlink(target: TFilePath, path: TFilePath, type?: TSymlinkType): Promise; - truncate(path: TFilePath, len?: number): Promise; - unlink(path: TFilePath): Promise; - utimes(path: TFilePath, atime: TTime, mtime: TTime): Promise; + readlink(path: PathLike, options?: IOptions): Promise; + realpath(path: PathLike, options?: IRealpathOptions | string): Promise; + rename(oldPath: PathLike, newPath: PathLike): Promise; + rmdir(path: PathLike): Promise; + stat(path: PathLike, options?: IStatOptions): Promise; + symlink(target: PathLike, path: PathLike, type?: TSymlinkType): Promise; + truncate(path: PathLike, len?: number): Promise; + unlink(path: PathLike): Promise; + utimes(path: PathLike, atime: TTime, mtime: TTime): Promise; writeFile(id: TFileHandle, data: TData, options?: IWriteFileOptions): Promise; } @@ -175,43 +175,43 @@ export default function createPromisesApi(vol: Volume): null | IPromisesAPI { return { FileHandle, - access(path: TFilePath, mode?: number): Promise { + access(path: PathLike, mode?: number): Promise { return promisify(vol, 'access')(path, mode); }, appendFile(path: TFileHandle, data: TData, options?: IAppendFileOptions | string): Promise { - return promisify(vol, 'appendFile')(path instanceof FileHandle ? path.fd : (path as TFilePath), data, options); + return promisify(vol, 'appendFile')(path instanceof FileHandle ? path.fd : (path as PathLike), data, options); }, - chmod(path: TFilePath, mode: TMode): Promise { + chmod(path: PathLike, mode: TMode): Promise { return promisify(vol, 'chmod')(path, mode); }, - chown(path: TFilePath, uid: number, gid: number): Promise { + chown(path: PathLike, uid: number, gid: number): Promise { return promisify(vol, 'chown')(path, uid, gid); }, - copyFile(src: TFilePath, dest: TFilePath, flags?: TFlagsCopy): Promise { + copyFile(src: PathLike, dest: PathLike, flags?: TFlagsCopy): Promise { return promisify(vol, 'copyFile')(src, dest, flags); }, - lchmod(path: TFilePath, mode: TMode): Promise { + lchmod(path: PathLike, mode: TMode): Promise { return promisify(vol, 'lchmod')(path, mode); }, - lchown(path: TFilePath, uid: number, gid: number): Promise { + lchown(path: PathLike, uid: number, gid: number): Promise { return promisify(vol, 'lchown')(path, uid, gid); }, - link(existingPath: TFilePath, newPath: TFilePath): Promise { + link(existingPath: PathLike, newPath: PathLike): Promise { return promisify(vol, 'link')(existingPath, newPath); }, - lstat(path: TFilePath, options?: IStatOptions): Promise { + lstat(path: PathLike, options?: IStatOptions): Promise { return promisify(vol, 'lstat')(path, options); }, - mkdir(path: TFilePath, options?: TMode | IMkdirOptions): Promise { + mkdir(path: PathLike, options?: TMode | IMkdirOptions): Promise { return promisify(vol, 'mkdir')(path, options); }, @@ -219,56 +219,56 @@ export default function createPromisesApi(vol: Volume): null | IPromisesAPI { return promisify(vol, 'mkdtemp')(prefix, options); }, - open(path: TFilePath, flags: TFlags, mode?: TMode): Promise { + open(path: PathLike, flags: TFlags, mode?: TMode): Promise { return promisify(vol, 'open', fd => new FileHandle(vol, fd))(path, flags, mode); }, - readdir(path: TFilePath, options?: IReaddirOptions | string): Promise { + readdir(path: PathLike, options?: IReaddirOptions | string): Promise { return promisify(vol, 'readdir')(path, options); }, readFile(id: TFileHandle, options?: IReadFileOptions | string): Promise { - return promisify(vol, 'readFile')(id instanceof FileHandle ? id.fd : (id as TFilePath), options); + return promisify(vol, 'readFile')(id instanceof FileHandle ? id.fd : (id as PathLike), options); }, - readlink(path: TFilePath, options?: IOptions): Promise { + readlink(path: PathLike, options?: IOptions): Promise { return promisify(vol, 'readlink')(path, options); }, - realpath(path: TFilePath, options?: IRealpathOptions | string): Promise { + realpath(path: PathLike, options?: IRealpathOptions | string): Promise { return promisify(vol, 'realpath')(path, options); }, - rename(oldPath: TFilePath, newPath: TFilePath): Promise { + rename(oldPath: PathLike, newPath: PathLike): Promise { return promisify(vol, 'rename')(oldPath, newPath); }, - rmdir(path: TFilePath): Promise { + rmdir(path: PathLike): Promise { return promisify(vol, 'rmdir')(path); }, - stat(path: TFilePath, options?: IStatOptions): Promise { + stat(path: PathLike, options?: IStatOptions): Promise { return promisify(vol, 'stat')(path, options); }, - symlink(target: TFilePath, path: TFilePath, type?: TSymlinkType): Promise { + symlink(target: PathLike, path: PathLike, type?: TSymlinkType): Promise { return promisify(vol, 'symlink')(target, path, type); }, - truncate(path: TFilePath, len?: number): Promise { + truncate(path: PathLike, len?: number): Promise { return promisify(vol, 'truncate')(path, len); }, - unlink(path: TFilePath): Promise { + unlink(path: PathLike): Promise { return promisify(vol, 'unlink')(path); }, - utimes(path: TFilePath, atime: TTime, mtime: TTime): Promise { + utimes(path: PathLike, atime: TTime, mtime: TTime): Promise { return promisify(vol, 'utimes')(path, atime, mtime); }, writeFile(id: TFileHandle, data: TData, options?: IWriteFileOptions): Promise { - return promisify(vol, 'writeFile')(id instanceof FileHandle ? id.fd : (id as TFilePath), data, options); + return promisify(vol, 'writeFile')(id instanceof FileHandle ? id.fd : (id as PathLike), data, options); }, }; } diff --git a/src/volume.ts b/src/volume.ts index 77409c61..f4b016f2 100644 --- a/src/volume.ts +++ b/src/volume.ts @@ -1,4 +1,5 @@ import * as pathModule from 'path'; +import { PathLike } from 'fs'; import { Node, Link, File } from './node'; import Stats, { TStatNumber } from './Stats'; import Dirent from './Dirent'; @@ -52,8 +53,7 @@ export interface IError extends Error { code?: string; } -export type TFilePath = string | Buffer | URL; -export type TFileId = TFilePath | number; // Number is used as a file descriptor. +export type TFileId = PathLike | number; // Number is used as a file descriptor. export type TData = TDataOut | Uint8Array; // Data formats users can give us. export type TFlags = string | number; export type TMode = string | number; // Mode can be a String, although docs say it should be a Number. @@ -397,7 +397,7 @@ function getPathFromURLPosix(url): string { return decodeURIComponent(pathname); } -export function pathToFilename(path: TFilePath): string { +export function pathToFilename(path: PathLike): string { if (typeof path !== 'string' && !Buffer.isBuffer(path)) { try { if (!(path instanceof require('url').URL)) throw new TypeError(ERRSTR.PATH_STR); @@ -429,7 +429,7 @@ export function filenameToSteps(filename: string, base?: string): string[] { return fullPathSansSlash.split(sep); } -export function pathToSteps(path: TFilePath): string[] { +export function pathToSteps(path: PathLike): string[] { return filenameToSteps(pathToFilename(path)); } @@ -777,7 +777,7 @@ export class Volume { if (!file) throw Error('File nto found'); return file.node; } else { - const steps = pathToSteps(id as TFilePath); + const steps = pathToSteps(id as PathLike); let link = this.getLink(steps); if (link) return link.getNode(); @@ -845,7 +845,7 @@ export class Volume { return json; } - toJSON(paths?: TFilePath | TFilePath[], json = {}, isRelative = false): DirectoryJSON { + toJSON(paths?: PathLike | PathLike[], json = {}, isRelative = false): DirectoryJSON { const links: Link[] = []; if (paths) { @@ -970,7 +970,7 @@ export class Volume { return file.fd; } - openSync(path: TFilePath, flags: TFlags, mode: TMode = MODE.DEFAULT): number { + openSync(path: PathLike, flags: TFlags, mode: TMode = MODE.DEFAULT): number { // Validate (1) mode; (2) path; (3) flags - in that order. const modeNum = modeToNumber(mode); const fileName = pathToFilename(path); @@ -978,9 +978,9 @@ export class Volume { return this.openBase(fileName, flagsNum, modeNum); } - open(path: TFilePath, flags: TFlags, /* ... */ callback: TCallback); - open(path: TFilePath, flags: TFlags, mode: TMode, callback: TCallback); - open(path: TFilePath, flags: TFlags, a: TMode | TCallback, b?: TCallback) { + open(path: PathLike, flags: TFlags, /* ... */ callback: TCallback); + open(path: PathLike, flags: TFlags, mode: TMode, callback: TCallback); + open(path: PathLike, flags: TFlags, a: TMode | TCallback, b?: TCallback) { let mode: TMode = a as TMode; let callback: TCallback = b as TCallback; @@ -1072,7 +1072,7 @@ export class Volume { if (userOwnsFd) fd = id as number; else { - const filename = pathToFilename(id as TFilePath); + const filename = pathToFilename(id as PathLike); const steps = filenameToSteps(filename); const link: Link | null = this.getResolvedLink(steps); @@ -1081,7 +1081,7 @@ export class Volume { if (node.isDirectory()) throw createError(EISDIR, 'open', link.getPath()); } - fd = this.openSync(id as TFilePath, flagsNum); + fd = this.openSync(id as PathLike, flagsNum); } try { @@ -1240,8 +1240,8 @@ export class Volume { if (isUserFd) fd = id as number; else { - fd = this.openBase(pathToFilename(id as TFilePath), flagsNum, modeNum); - // fd = this.openSync(id as TFilePath, flagsNum, modeNum); + fd = this.openBase(pathToFilename(id as PathLike), flagsNum, modeNum); + // fd = this.openSync(id as PathLike, flagsNum, modeNum); } let offset = 0; @@ -1324,16 +1324,16 @@ export class Volume { this.writeFileBase(dest, buf, FLAGS.w, MODE.DEFAULT); } - copyFileSync(src: TFilePath, dest: TFilePath, flags?: TFlagsCopy) { + copyFileSync(src: PathLike, dest: PathLike, flags?: TFlagsCopy) { const srcFilename = pathToFilename(src); const destFilename = pathToFilename(dest); return this.copyFileBase(srcFilename, destFilename, (flags || 0) | 0); } - copyFile(src: TFilePath, dest: TFilePath, callback: TCallback); - copyFile(src: TFilePath, dest: TFilePath, flags: TFlagsCopy, callback: TCallback); - copyFile(src: TFilePath, dest: TFilePath, a, b?) { + copyFile(src: PathLike, dest: PathLike, callback: TCallback); + copyFile(src: PathLike, dest: PathLike, flags: TFlagsCopy, callback: TCallback); + copyFile(src: PathLike, dest: PathLike, a, b?) { const srcFilename = pathToFilename(src); const destFilename = pathToFilename(dest); @@ -1353,13 +1353,13 @@ export class Volume { this.wrapAsync(this.copyFileBase, [srcFilename, destFilename, flags], callback); } - linkSync(existingPath: TFilePath, newPath: TFilePath) { + linkSync(existingPath: PathLike, newPath: PathLike) { const existingPathFilename = pathToFilename(existingPath); const newPathFilename = pathToFilename(newPath); this.linkBase(existingPathFilename, newPathFilename); } - link(existingPath: TFilePath, newPath: TFilePath, callback: TCallback) { + link(existingPath: PathLike, newPath: PathLike, callback: TCallback) { const existingPathFilename = pathToFilename(existingPath); const newPathFilename = pathToFilename(newPath); this.wrapAsync(this.linkBase, [existingPathFilename, newPathFilename], callback); @@ -1385,12 +1385,12 @@ export class Volume { } } - unlinkSync(path: TFilePath) { + unlinkSync(path: PathLike) { const filename = pathToFilename(path); this.unlinkBase(filename); } - unlink(path: TFilePath, callback: TCallback) { + unlink(path: PathLike, callback: TCallback) { const filename = pathToFilename(path); this.wrapAsync(this.unlinkBase, [filename], callback); } @@ -1414,17 +1414,16 @@ export class Volume { } // `type` argument works only on Windows. - symlinkSync(target: TFilePath, path: TFilePath, type?: TSymlinkType) { + symlinkSync(target: PathLike, path: PathLike, type?: TSymlinkType) { const targetFilename = pathToFilename(target); const pathFilename = pathToFilename(path); this.symlinkBase(targetFilename, pathFilename); } - symlink(target: TFilePath, path: TFilePath, callback: TCallback); - symlink(target: TFilePath, path: TFilePath, type: TSymlinkType, callback: TCallback); - symlink(target: TFilePath, path: TFilePath, a: TSymlinkType | TCallback, b?: TCallback) { + symlink(target: PathLike, path: PathLike, callback: TCallback); + symlink(target: PathLike, path: PathLike, type: TSymlinkType, callback: TCallback); + symlink(target: PathLike, path: PathLike, a: TSymlinkType | TCallback, b?: TCallback) { const callback: TCallback = validateCallback(typeof a === 'function' ? a : b); - const targetFilename = pathToFilename(target); const pathFilename = pathToFilename(path); this.wrapAsync(this.symlinkBase, [targetFilename, pathFilename], callback); @@ -1438,13 +1437,13 @@ export class Volume { return strToEncoding(realLink.getPath(), encoding); } - realpathSync(path: TFilePath, options?: IRealpathOptions | string): TDataOut { + realpathSync(path: PathLike, options?: IRealpathOptions | string): TDataOut { return this.realpathBase(pathToFilename(path), getRealpathOptions(options).encoding); } - realpath(path: TFilePath, callback: TCallback); - realpath(path: TFilePath, options: IRealpathOptions | string, callback: TCallback); - realpath(path: TFilePath, a: TCallback | IRealpathOptions | string, b?: TCallback) { + realpath(path: PathLike, callback: TCallback); + realpath(path: PathLike, options: IRealpathOptions | string, callback: TCallback); + realpath(path: PathLike, a: TCallback | IRealpathOptions | string, b?: TCallback) { const [opts, callback] = getRealpathOptsAndCb(a, b); const pathFilename = pathToFilename(path); this.wrapAsync(this.realpathBase, [pathFilename, opts.encoding], callback); @@ -1458,16 +1457,16 @@ export class Volume { return Stats.build(link.getNode(), bigint); } - lstatSync(path: TFilePath): Stats; - lstatSync(path: TFilePath, options: { bigint: false }): Stats; - lstatSync(path: TFilePath, options: { bigint: true }): Stats; - lstatSync(path: TFilePath, options?: IStatOptions): Stats { + lstatSync(path: PathLike): Stats; + lstatSync(path: PathLike, options: { bigint: false }): Stats; + lstatSync(path: PathLike, options: { bigint: true }): Stats; + lstatSync(path: PathLike, options?: IStatOptions): Stats { return this.lstatBase(pathToFilename(path), getStatOptions(options).bigint as any); } - lstat(path: TFilePath, callback: TCallback); - lstat(path: TFilePath, options: IStatOptions, callback: TCallback); - lstat(path: TFilePath, a: TCallback | IStatOptions, b?: TCallback) { + lstat(path: PathLike, callback: TCallback); + lstat(path: PathLike, options: IStatOptions, callback: TCallback); + lstat(path: PathLike, a: TCallback | IStatOptions, b?: TCallback) { const [opts, callback] = getStatOptsAndCb(a, b); this.wrapAsync(this.lstatBase, [pathToFilename(path), opts.bigint], callback); } @@ -1482,16 +1481,16 @@ export class Volume { return Stats.build(link.getNode(), bigint); } - statSync(path: TFilePath): Stats; - statSync(path: TFilePath, options: { bigint: false }): Stats; - statSync(path: TFilePath, options: { bigint: true }): Stats; - statSync(path: TFilePath, options?: IStatOptions): Stats { + statSync(path: PathLike): Stats; + statSync(path: PathLike, options: { bigint: false }): Stats; + statSync(path: PathLike, options: { bigint: true }): Stats; + statSync(path: PathLike, options?: IStatOptions): Stats { return this.statBase(pathToFilename(path), getStatOptions(options).bigint as any); } - stat(path: TFilePath, callback: TCallback); - stat(path: TFilePath, options: IStatOptions, callback: TCallback); - stat(path: TFilePath, a: TCallback | IStatOptions, b?: TCallback) { + stat(path: PathLike, callback: TCallback); + stat(path: PathLike, options: IStatOptions, callback: TCallback); + stat(path: PathLike, a: TCallback | IStatOptions, b?: TCallback) { const [opts, callback] = getStatOptsAndCb(a, b); this.wrapAsync(this.statBase, [pathToFilename(path), opts.bigint], callback); } @@ -1546,13 +1545,13 @@ export class Volume { newPathDirLink.setChild(link.getName(), link); } - renameSync(oldPath: TFilePath, newPath: TFilePath) { + renameSync(oldPath: PathLike, newPath: PathLike) { const oldPathFilename = pathToFilename(oldPath); const newPathFilename = pathToFilename(newPath); this.renameBase(oldPathFilename, newPathFilename); } - rename(oldPath: TFilePath, newPath: TFilePath, callback: TCallback) { + rename(oldPath: PathLike, newPath: PathLike, callback: TCallback) { const oldPathFilename = pathToFilename(oldPath); const newPathFilename = pathToFilename(newPath); this.wrapAsync(this.renameBase, [oldPathFilename, newPathFilename], callback); @@ -1562,7 +1561,7 @@ export class Volume { return !!this.statBase(filename); } - existsSync(path: TFilePath): boolean { + existsSync(path: PathLike): boolean { try { return this.existsBase(pathToFilename(path)); } catch (err) { @@ -1570,7 +1569,7 @@ export class Volume { } } - exists(path: TFilePath, callback: (exists: boolean) => void) { + exists(path: PathLike, callback: (exists: boolean) => void) { const filename = pathToFilename(path); if (typeof callback !== 'function') throw Error(ERRSTR.CB); @@ -1590,15 +1589,15 @@ export class Volume { // TODO: Verify permissions } - accessSync(path: TFilePath, mode: number = F_OK) { + accessSync(path: PathLike, mode: number = F_OK) { const filename = pathToFilename(path); mode = mode | 0; this.accessBase(filename, mode); } - access(path: TFilePath, callback: TCallback); - access(path: TFilePath, mode: number, callback: TCallback); - access(path: TFilePath, a: TCallback | number, b?: TCallback) { + access(path: PathLike, callback: TCallback); + access(path: PathLike, mode: number, callback: TCallback); + access(path: PathLike, a: TCallback | number, b?: TCallback) { let mode: number = F_OK; let callback: TCallback; @@ -1672,15 +1671,15 @@ export class Volume { return list; } - readdirSync(path: TFilePath, options?: IReaddirOptions | string): TDataOut[] | Dirent[] { + readdirSync(path: PathLike, options?: IReaddirOptions | string): TDataOut[] | Dirent[] { const opts = getReaddirOptions(options); const filename = pathToFilename(path); return this.readdirBase(filename, opts); } - readdir(path: TFilePath, callback: TCallback); - readdir(path: TFilePath, options: IReaddirOptions | string, callback: TCallback); - readdir(path: TFilePath, a?, b?) { + readdir(path: PathLike, callback: TCallback); + readdir(path: PathLike, options: IReaddirOptions | string, callback: TCallback); + readdir(path: PathLike, a?, b?) { const [options, callback] = getReaddirOptsAndCb(a, b); const filename = pathToFilename(path); this.wrapAsync(this.readdirBase, [filename, options], callback); @@ -1696,15 +1695,15 @@ export class Volume { return strToEncoding(str, encoding); } - readlinkSync(path: TFilePath, options?: IOptions): TDataOut { + readlinkSync(path: PathLike, options?: IOptions): TDataOut { const opts = getDefaultOpts(options); const filename = pathToFilename(path); return this.readlinkBase(filename, opts.encoding); } - readlink(path: TFilePath, callback: TCallback); - readlink(path: TFilePath, options: IOptions, callback: TCallback); - readlink(path: TFilePath, a: TCallback | IOptions, b?: TCallback) { + readlink(path: PathLike, callback: TCallback); + readlink(path: PathLike, options: IOptions, callback: TCallback); + readlink(path: PathLike, a: TCallback | IOptions, b?: TCallback) { const [opts, callback] = getDefaultOptsAndCb(a, b); const filename = pathToFilename(path); this.wrapAsync(this.readlinkBase, [filename, opts.encoding], callback); @@ -1752,7 +1751,7 @@ export class Volume { this.wrapAsync(this.ftruncateBase, [fd, len], callback); } - private truncateBase(path: TFilePath, len?: number) { + private truncateBase(path: PathLike, len?: number) { const fd = this.openSync(path, 'r+'); try { this.ftruncateSync(fd, len); @@ -1764,7 +1763,7 @@ export class Volume { truncateSync(id: TFileId, len?: number) { if (isFd(id)) return this.ftruncateSync(id as number, len); - this.truncateBase(id as TFilePath, len); + this.truncateBase(id as PathLike, len); } truncate(id: TFileId, callback: TCallback); @@ -1802,11 +1801,11 @@ export class Volume { } } - utimesSync(path: TFilePath, atime: TTime, mtime: TTime) { + utimesSync(path: PathLike, atime: TTime, mtime: TTime) { this.utimesBase(pathToFilename(path), toUnixTimestamp(atime), toUnixTimestamp(mtime)); } - utimes(path: TFilePath, atime: TTime, mtime: TTime, callback: TCallback) { + utimes(path: PathLike, atime: TTime, mtime: TTime, callback: TCallback) { this.wrapAsync(this.utimesBase, [pathToFilename(path), toUnixTimestamp(atime), toUnixTimestamp(mtime)], callback); } @@ -1850,7 +1849,7 @@ export class Volume { } } - mkdirSync(path: TFilePath, options?: TMode | IMkdirOptions) { + mkdirSync(path: PathLike, options?: TMode | IMkdirOptions) { const opts = getMkdirOptions(options); const modeNum = modeToNumber(opts.mode, 0o777); const filename = pathToFilename(path); @@ -1858,12 +1857,11 @@ export class Volume { else this.mkdirBase(filename, modeNum); } - mkdir(path: TFilePath, callback: TCallback); - mkdir(path: TFilePath, mode: TMode | IMkdirOptions, callback: TCallback); - mkdir(path: TFilePath, a: TCallback | TMode | IMkdirOptions, b?: TCallback) { + mkdir(path: PathLike, callback: TCallback); + mkdir(path: PathLike, mode: TMode | IMkdirOptions, callback: TCallback); + mkdir(path: PathLike, a: TCallback | TMode | IMkdirOptions, b?: TCallback) { const opts: TMode | IMkdirOptions = getMkdirOptions(a); const callback: TCallback = validateCallback(typeof a === 'function' ? a : b); - const modeNum = modeToNumber(opts.mode, 0o777); const filename = pathToFilename(path); if (opts.recursive) this.wrapAsync(this.mkdirpBase, [filename, modeNum], callback); @@ -1871,16 +1869,15 @@ export class Volume { } // legacy interface - mkdirpSync(path: TFilePath, mode?: TMode) { + mkdirpSync(path: PathLike, mode?: TMode) { this.mkdirSync(path, { mode, recursive: true }); } - mkdirp(path: TFilePath, callback: TCallback); - mkdirp(path: TFilePath, mode: TMode, callback: TCallback); - mkdirp(path: TFilePath, a: TCallback | TMode, b?: TCallback) { + mkdirp(path: PathLike, callback: TCallback); + mkdirp(path: PathLike, mode: TMode, callback: TCallback); + mkdirp(path: PathLike, a: TCallback | TMode, b?: TCallback) { const mode: TMode | undefined = typeof a === 'function' ? undefined : a; const callback: TCallback = validateCallback(typeof a === 'function' ? a : b); - this.mkdir(path, { mode, recursive: true }, callback); } @@ -1929,13 +1926,13 @@ export class Volume { this.deleteLink(link); } - rmdirSync(path: TFilePath, options?: IRmdirOptions) { + rmdirSync(path: PathLike, options?: IRmdirOptions) { this.rmdirBase(pathToFilename(path), options); } - rmdir(path: TFilePath, callback: TCallback); - rmdir(path: TFilePath, options: IRmdirOptions, callback: TCallback); - rmdir(path: TFilePath, a: TCallback | IRmdirOptions, b?: TCallback) { + rmdir(path: PathLike, callback: TCallback); + rmdir(path: PathLike, options: IRmdirOptions, callback: TCallback); + rmdir(path: PathLike, a: TCallback | IRmdirOptions, b?: TCallback) { const opts: IRmdirOptions = getRmdirOptions(a); const callback: TCallback = validateCallback(typeof a === 'function' ? a : b); this.wrapAsync(this.rmdirBase, [pathToFilename(path), opts], callback); @@ -1963,13 +1960,13 @@ export class Volume { } } - chmodSync(path: TFilePath, mode: TMode) { + chmodSync(path: PathLike, mode: TMode) { const modeNum = modeToNumber(mode); const filename = pathToFilename(path); this.chmodBase(filename, modeNum); } - chmod(path: TFilePath, mode: TMode, callback: TCallback) { + chmod(path: PathLike, mode: TMode, callback: TCallback) { const modeNum = modeToNumber(mode); const filename = pathToFilename(path); this.wrapAsync(this.chmodBase, [filename, modeNum], callback); @@ -1984,13 +1981,13 @@ export class Volume { } } - lchmodSync(path: TFilePath, mode: TMode) { + lchmodSync(path: PathLike, mode: TMode) { const modeNum = modeToNumber(mode); const filename = pathToFilename(path); this.lchmodBase(filename, modeNum); } - lchmod(path: TFilePath, mode: TMode, callback: TCallback) { + lchmod(path: PathLike, mode: TMode, callback: TCallback) { const modeNum = modeToNumber(mode); const filename = pathToFilename(path); this.wrapAsync(this.lchmodBase, [filename, modeNum], callback); @@ -2026,13 +2023,13 @@ export class Volume { // } } - chownSync(path: TFilePath, uid: number, gid: number) { + chownSync(path: PathLike, uid: number, gid: number) { validateUid(uid); validateGid(gid); this.chownBase(pathToFilename(path), uid, gid); } - chown(path: TFilePath, uid: number, gid: number, callback: TCallback) { + chown(path: PathLike, uid: number, gid: number, callback: TCallback) { validateUid(uid); validateGid(gid); this.wrapAsync(this.chownBase, [pathToFilename(path), uid, gid], callback); @@ -2044,13 +2041,13 @@ export class Volume { .chown(uid, gid); } - lchownSync(path: TFilePath, uid: number, gid: number) { + lchownSync(path: PathLike, uid: number, gid: number) { validateUid(uid); validateGid(gid); this.lchownBase(pathToFilename(path), uid, gid); } - lchown(path: TFilePath, uid: number, gid: number, callback: TCallback) { + lchown(path: PathLike, uid: number, gid: number, callback: TCallback) { validateUid(uid); validateGid(gid); this.wrapAsync(this.lchownBase, [pathToFilename(path), uid, gid], callback); @@ -2058,9 +2055,9 @@ export class Volume { private statWatchers = {}; - watchFile(path: TFilePath, listener: (curr: Stats, prev: Stats) => void): StatWatcher; - watchFile(path: TFilePath, options: IWatchFileOptions, listener: (curr: Stats, prev: Stats) => void): StatWatcher; - watchFile(path: TFilePath, a, b?): StatWatcher { + watchFile(path: PathLike, listener: (curr: Stats, prev: Stats) => void): StatWatcher; + watchFile(path: PathLike, options: IWatchFileOptions, listener: (curr: Stats, prev: Stats) => void): StatWatcher; + watchFile(path: PathLike, a, b?): StatWatcher { const filename = pathToFilename(path); let options: IWatchFileOptions | null = a; @@ -2095,7 +2092,7 @@ export class Volume { return watcher; } - unwatchFile(path: TFilePath, listener?: (curr: Stats, prev: Stats) => void) { + unwatchFile(path: PathLike, listener?: (curr: Stats, prev: Stats) => void) { const filename = pathToFilename(path); const watcher = this.statWatchers[filename]; if (!watcher) return; @@ -2112,18 +2109,18 @@ export class Volume { } } - createReadStream(path: TFilePath, options?: IReadStreamOptions | string): IReadStream { + createReadStream(path: PathLike, options?: IReadStreamOptions | string): IReadStream { return new this.ReadStream(path, options); } - createWriteStream(path: TFilePath, options?: IWriteStreamOptions | string): IWriteStream { + createWriteStream(path: PathLike, options?: IWriteStreamOptions | string): IWriteStream { return new this.WriteStream(path, options); } - // watch(path: TFilePath): FSWatcher; - // watch(path: TFilePath, options?: IWatchOptions | string): FSWatcher; + // watch(path: PathLike): FSWatcher; + // watch(path: PathLike, options?: IWatchOptions | string): FSWatcher; watch( - path: TFilePath, + path: PathLike, options?: IWatchOptions | string, listener?: (eventType: string, filename: string) => void, ): FSWatcher { @@ -2209,7 +2206,7 @@ export class StatWatcher extends EventEmitter { // ---------------------------------------- ReadStream export interface IReadStream extends Readable { - new (path: TFilePath, options: IReadStreamOptions); + new (path: PathLike, options: IReadStreamOptions); open(); close(callback: TCallback); bytesRead: number; @@ -2382,7 +2379,7 @@ function closeOnOpen(fd) { export interface IWriteStream extends Writable { bytesWritten: number; path: string; - new (path: TFilePath, options: IWriteStreamOptions); + new (path: PathLike, options: IWriteStreamOptions); open(); close(); } @@ -2567,7 +2564,7 @@ export class FSWatcher extends EventEmitter { this._timer = setTimeout(this._persist, 1e6); }; - start(path: TFilePath, persistent: boolean = true, recursive: boolean = false, encoding: TEncoding = ENCODING_UTF8) { + start(path: PathLike, persistent: boolean = true, recursive: boolean = false, encoding: TEncoding = ENCODING_UTF8) { this._filename = pathToFilename(path); this._steps = filenameToSteps(this._filename); this._filenameEncoded = strToEncoding(this._filename);