/// /// /// /// /// /// /// /*! * path.js * Copyright (C) 2021-2023 Sebastian Riedel * MIT Licensed */ import type EventEmitter from 'node:events'; import type stream from 'node:stream'; import fs from 'node:fs'; import fsPromises from 'node:fs/promises'; import path from 'node:path'; import readline from 'node:readline'; interface StreamOptions { flags?: string; encoding?: BufferEncoding; fd?: number | fsPromises.FileHandle; mode?: number; autoClose?: boolean; emitClose?: boolean; start?: number; highWaterMark?: number; } interface ReadStreamOptions extends StreamOptions { end?: number; } export default class Path { _path: string; /** * Create a `Path` instance for the given path or the current working directory. * @example * // Relative file * const file = new Path('work', 'notes.txt'); * * // Current working directory * const dir = new Path(); */ constructor(...parts: string[]); /** * Asynchronously tests a user's permissions for the file or directory. * @see https://nodejs.org/api/fs.html#fs_fspromises_access_path_mode */ access(mode: number): Promise; /** * Synchronously tests a user's permissions for the file or directory. * @see https://nodejs.org/api/fs.html#fs_fs_accesssync_path_mode */ accessSync(mode: number): boolean; /** * Asynchronously append data to a file, creating the file if it does not yet exist. * @see https://nodejs.org/api/fs.html#filehandleappendfiledata-options */ appendFile(data: string | Uint8Array, options?: BufferEncoding | (fs.ObjectEncodingOptions & fs.promises.FlagAndOpenMode)): Promise; /** * Synchronously append data to a file, creating the file if it does not yet exist. * @see https://nodejs.org/api/fs.html#fsappendfilesyncpath-data-options */ appendFileSync(data: string | Uint8Array, options?: fs.WriteFileOptions): this; /** * Returns the last portion of a path, similar to the Unix `basename` command. * @see https://nodejs.org/api/path.html#path_path_basename_path_ext */ basename(ext?: string): string; /** * Create a new `Path` object for the caller source file. */ static callerFile(): Path; /** * Create a new `Path` object relative to the current path. * @example * // "/home/kraih/notes.txt" * const home = new Path('/home/kraih'); * const file = home.child('notes.txt'); */ child(...parts: string[]): Path; /** * Asynchronously changes the permissions of a file. * @see https://nodejs.org/api/fs.html#fs_fspromises_chmod_path_mode */ chmod(mode: string | number): Promise; /** * Synchronously changes the permissions of a file. * @see https://nodejs.org/api/fs.html#fs_fs_chmodsync_path_mode */ chmodSync(mode: string | number): this; /** * Asynchronously change the ownership of a file. * @see https://nodejs.org/api/fs.html#fspromiseschownpath-uid-gid */ chown(uid: number, gid: number): Promise; /** * Synchronously change the ownership of a file. * @see https://nodejs.org/api/fs.html#fschownsyncpath-uid-gid */ chownSync(uid: number, gid: number): this; /** * Returns an object containing commonly used constants for file system operations. * @see https://nodejs.org/api/fs.html#fs_fs_constants */ static get constants(): typeof fs.constants; /** * Asynchronously copies file to destination. * @see https://nodejs.org/api/fs.html#fs_fspromises_copyfile_src_dest_mode */ copyFile(destination: Path | string, flags?: number): Promise; /** * Synchronously copies file to destination. * @see https://nodejs.org/api/fs.html#fs_fs_copyfilesync_src_dest_mode */ copyFileSync(destination: Path | string, flags?: number): this; /** * Create a readable stream for file. * @see https://nodejs.org/api/fs.html#fs_fs_createreadstream_path_options */ createReadStream(options?: BufferEncoding | ReadStreamOptions | undefined): fs.ReadStream; /** * Create a writable stream for file. * @see https://nodejs.org/api/fs.html#fs_fs_createwritestream_path_options */ createWriteStream(options?: BufferEncoding | StreamOptions | undefined): fs.WriteStream; /** * Create a new `Path` object for the current source file. */ static currentFile(): Path; /** * Returns the directory name of a path, similar to the Unix `dirname` command. * @see https://nodejs.org/api/path.html#path_path_dirname_path */ dirname(): Path; /** * Asynchronously check if file or directory exists. */ exists(): Promise; /** * Synchronously check if file or directory exists. */ existsSync(): boolean; /** * Returns the extension of the path, from the last occurrence of the `.` (period) character to end of string in the * last portion of the path. * @see https://nodejs.org/api/path.html#path_path_extname_path */ extname(): string; /** * Create a new `Path` object from a `file://` URL. */ static fromFileURL(file: string): Path; /** * Determine if path is an absolute path. * @see https://nodejs.org/api/path.html#path_path_isabsolute_path */ isAbsolute(): boolean; /** * Asynchronously check if file is readable. */ isReadable(): Promise; /** * Synchronously check if file is readable. */ isReadableSync(): boolean; /** * Asynchronously check if file is writable. */ isWritable(): Promise; /** * Synchronously check if file is writable. */ isWritableSync(): boolean; /** * List files in directory. * @example * // List files recursively * const dir = new Path('/tmp'); * for await (const file of dir.list({recursive: true})) { * console.log(file.toString()); * } */ list(options?: { dir?: boolean; hidden?: boolean; maxDepth?: number; recursive?: boolean; }): AsyncIterable; /** * Read file one line at a time. * @example * // Decode UTF-8 file * const file = new Path('notes.txt'); * for await (const line of file.lines({encoding: 'utf8'})) { * console.log(line); * } */ lines(options?: stream.ReadableOptions): readline.Interface; /** * Equivalent to `stat` unless path refers to a symbolic link, in which case the link itself is stat-ed, not the file * that it refers to. * @see https://nodejs.org/api/fs.html#fs_fspromises_lstat_path_options */ lstat(options?: fs.StatOptions): Promise; /** * Equivalent to `statSync` unless path refers to a symbolic link, in which case the link itself is stat-ed, not the * file that it refers to. * @see https://nodejs.org/api/fs.html#fs_fs_lstatsync_path_options */ lstatSync(options?: fs.StatOptions): fs.Stats | fs.BigIntStats | undefined; /** * Asynchronously creates a directory. * @see https://nodejs.org/api/fs.html#fs_fspromises_mkdir_path_options */ mkdir(options?: fs.MakeDirectoryOptions & { recursive: true; }): Promise; /** * Synchronously creates a directory. * @see https://nodejs.org/api/fs.html#fs_fs_mkdirsync_path_options */ mkdirSync(options?: fs.MakeDirectoryOptions & { recursive: true; }): this; /** * Normalizes the given path, resolving `..` and `.` segments. * @see https://nodejs.org/api/path.html#path_path_normalize_path */ normalize(): Path; /** * Asynchronously open file. * @see https://nodejs.org/api/fs.html#fs_fspromises_open_path_flags_mode */ open(flags: string | number, mode?: string | number): Promise; /** * Asynchronously reads the entire contents of a file. * @see https://nodejs.org/api/fs.html#fs_fspromises_readfile_path_options */ readFile(options?: BufferEncoding | (fs.ObjectEncodingOptions & EventEmitter.Abortable & { flag?: fs.OpenMode; })): Promise; /** * Synchronously reads the entire contents of a file. * @see https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options */ readFileSync(options?: BufferEncoding | (fs.ObjectEncodingOptions & { flag?: string; })): string | Buffer; /** * Returns the relative path from path to `to` based on the current working directory. * @see https://nodejs.org/api/path.html#path_path_relative_from_to */ relative(to: Path | string): Path; /** * Asynchronously renames path to `newPath`. * @see https://nodejs.org/api/fs.html#fs_fspromises_rename_oldpath_newpath */ rename(newPath: Path | string): Promise; /** * Synchronously renames path to `newPath`. * @see https://nodejs.org/api/fs.html#fs_fs_renamesync_oldpath_newpath */ renameSync(newPath: Path | string): void; /** * Asynchronously computes the canonical pathname. * @see https://nodejs.org/api/fs.html#fs_fspromises_realpath_path_options */ realpath(options?: fs.ObjectEncodingOptions): Promise; /** * Synchronously computes the canonical pathname. * @see https://nodejs.org/api/fs.html#fs_fs_realpathsync_path_options */ realpathSync(options?: fs.ObjectEncodingOptions): Path; /** * Asynchronously removes files and directories (modeled on the standard POSIX `rm` utility). * @see https://nodejs.org/api/fs.html#fs_fspromises_rm_path_options */ rm(options?: fs.RmOptions): Promise; /** * Synchronously removes files and directories (modeled on the standard POSIX `rm` utility). * @see https://nodejs.org/api/fs.html#fs_fs_rmsync_path_options */ rmSync(options?: fs.RmOptions): void; /** * Create a new `Path` object relative to the parent directory. * @example * // "/home/kraih/users.txt" * const notes = new Path('/home/kraih/notes.txt'); * const users = notes.sibling('users.txt'); */ sibling(...parts: string[]): Path; /** * Asynchronously retrieves stat information for the path. * @see https://nodejs.org/api/fs.html#fs_fspromises_stat_path_options */ stat(options?: fs.StatOptions): Promise; /** * Synchronously retrieves stat information for the path. * @see https://nodejs.org/api/fs.html#fs_fs_statsync_path_options */ statSync(options?: fs.StatOptions): fs.Stats | fs.BigIntStats | undefined; /** * Asynchronously creates a symbolic link. * @see https://nodejs.org/api/fs.html#fs_fspromises_symlink_target_path_type */ symlink(link: Path | string, type?: fs.symlink.Type): Promise; /** * Synchronously creates a symbolic link. * @see https://nodejs.org/api/fs.html#fs_fs_symlinksync_target_path_type */ symlinkSync(link: Path | string, type?: fs.symlink.Type): this; /** * Asynchronously truncates (shortens or extends the length) of the file. * @see https://nodejs.org/api/fs.html#fs_fspromises_truncate_path_len */ truncate(len?: number): Promise; /** * Synchronously truncates (shortens or extends the length) of the file. * @see https://nodejs.org/api/fs.html#fs_fs_truncatesync_path_len */ truncateSync(len?: number): this; /** * Create a new `TempDir` object (`Path` subclass with `destroy` and `destroySync` methods) for a temporary directory. */ static tempDir(options?: fs.ObjectEncodingOptions & { dir?: Path; name?: string; }): Promise; /** * Create a new `TempDir` object (`Path` subclass with `destroy` and `destroySync` methods) for a temporary directory. */ static tempDirSync(options?: fs.ObjectEncodingOptions & { dir?: Path; name?: string; }): TempDir; /** * Create file if it does not exist or change the modification and access time to the current time. */ touch(): Promise; /** * Create file if it does not exist or change the modification and access time to the current time. */ touchSync(): this; /** * Split the path. */ toArray(): string[]; /** * Convert path into a `file://` `URL` object. */ toFileURL(): URL; /** * Returns an object whose properties represent significant elements of the path. */ toObject(): path.ParsedPath; /** * Convert path into a string. */ toString(): string; /** * Change the file system timestamps of the object referenced by path. * @see https://nodejs.org/api/fs.html#fs_fspromises_utimes_path_atime_mtime */ utimes(atime: string | number | Date, mtime: string | number | Date): Promise; /** * Change the file system timestamps of the object referenced by path. * @see https://nodejs.org/api/fs.html#fs_fs_utimessync_path_atime_mtime */ utimesSync(atime: string | number | Date, mtime: string | number | Date): this; /** * Asynchronously writes data to a file, replacing the file if it already exists. * @see https://nodejs.org/api/fs.html#fs_fspromises_writefile_file_data_options */ writeFile(data: string | Uint8Array, options?: fs.ObjectEncodingOptions & { mode?: fs.Mode; flag?: fs.OpenMode; } & EventEmitter.Abortable): Promise; /** * Synchronously writes data to a file, replacing the file if it already exists. * @see https://nodejs.org/api/fs.html#fs_fs_writefilesync_file_data_options */ writeFileSync(data: string | Uint8Array, options?: fs.WriteFileOptions): this; } declare class TempDir extends Path { [Symbol.asyncDispose](): Promise; [Symbol.dispose](): void; /** * Asynchronously remove temporary directory. */ destroy(): Promise; /** * Synchronously remove temporary directory. */ destroySync(): void; } export {};