/// /// import fs from 'fs'; import fse from 'fs-extra'; import { PathyStatic } from './pathy.static.js'; import type { FileRetryOptions, PathyCopyOptions, PathyFindParentOptions, PathyInfix, PathyListChildrenOptions, PathyOrString, PathyReadOptions, PathyReadOutput, PathyRemoveOptions, PathyWriteOptions } from './pathy.types.js'; export interface PathyOptions { cwd?: PathyOrString; /** * Optionally provide a Zod-compatible schema/validator, * which will be used on read/write to file. */ validator?: { parse: (v: unknown) => T; }; } /** * An **immutable** utility class for reducing the cognitive load of * working with file paths. * * Any operation that would return a new path will will return a new * {@link Pathy} instance. */ export declare class Pathy extends PathyStatic { /** * The normalized path, with whatever * absolute/relative status the original input * path had. * * Use {@link absolute} or * {@link relative} for * guaranteed-absolute/relative variants. */ readonly normalized: string; /** * The normalized absolute path, as determined * by the cwd (provided or inferred) at instantiation * (or by the path itself if it was already absolute.) */ readonly absolute: string; /** * The normalized relative path, as determined * by the cwd (provided or inferred) at instantiation. */ readonly relative: string; /** * The working directory from when the instance * was created. */ readonly workingDirectory: string; /** * A zod schema or other compatible validator, which will be used * during file read/write if provided. */ readonly validator?: { parse: (v: unknown) => FileContent; }; constructor(path?: PathyOrString | string[], cwd?: PathyOrString); constructor(path?: PathyOrString | string[], options?: PathyOptions); get directory(): string; get name(): string; get basename(): string; get extname(): string; withValidator(validator: { parse: (v: unknown) => T; }): Pathy; /** * Check if a path has the given extension, ignoring case * and ensuring the `.` is present even if not provided in * the args (since that's always confusing). */ hasExtension(ext: string | string[]): boolean; parseInfix(): PathyInfix; /** * Whether this path is the root path. * This returns `true` when there is no parent directory. * * @example * new Pathy('/').isRoot; // true * new Pathy('c:/').isRoot; // true * new Pathy('c:/hello').isRoot; // false * new Pathy('c:\\').isRoot; // true * new Pathy('').isRoot; // false * new Pathy('/a/b').isRoot; // false * new Pathy('a').isRoot; // false */ get isRoot(): boolean; /** * Get a new {@link Pathy} instance that contains the relative * bath from the current location to another. */ relativeTo(to?: string | Pathy): string; relativeFrom(from?: string | Pathy): string; /** * Change the path by moving up the tree some number of * levels. * * @example * const path = new Path('/a/b/c'); * path.up(); // `/a/b` */ up(levels?: number): Pathy; join(...paths: PathyOrString[]): Pathy; /** * Change to another path by jumping through a number * of relative or absolute paths, using `path.resolve` logic. * * @example * const path = new Path('/a/b'); * path.resolveTo('c'); // `/a/b/c` * path.resolveTo('..'); // `/a/b` * path.resolveTo('c/d','e'); // `/a/b/c/d/e` * path.resolveTo('irrelevant','/new-root','../z'); // `/z` */ resolveTo(...paths: PathyOrString[]): Pathy; isDirectory(options?: { assert?: boolean; }): Promise; isDirectorySync(): boolean; isFile(): Promise; isFileSync(): boolean; isParentOf(otherPath: PathyOrString): boolean; isChildOf(otherPath: PathyOrString): boolean; /** * Read the file at the current path automatically parsing * it if the extension is supported (see {@link Pathy.read}). */ read Parsed; } | never = never>(options?: PathyReadOptions): Promise>; /** * Write to file at the current path, automatically serializing * if the extension is supported (see {@link Pathy.write}). */ write(data: T, options?: PathyWriteOptions): Promise; /** * Whether or not a file/directory exists at this path. */ exists(options?: { assert?: boolean; } & FileRetryOptions): Promise; existsSync(options?: { assert?: boolean; }): boolean; stat(options?: FileRetryOptions): Promise; statSync(): fs.Stats; /** * Check if this path leads to an empty directory. * * (If the path does not exist, will throw an error.) */ isEmptyDirectory(options?: { assert?: boolean; /** * If `false`, may contain other directories, nested ad nauseam, * so long as no directory includes any *files*. * * If `true`, this directory must be *completely* * empty (no files *or* directories). * * @default false */ strict?: boolean; /** * If `true`, if this path does not map onto * anything on disk the path will still be considered * an "empty directory" (i.e. returns `true`). */ allowNotFound?: boolean; }): Promise; /** * Treat the path as a directory, and ensure * that it exists (creating any needed parent * directories as along the way). */ ensureDirectory(): Promise; /** * @alias {@link Pathy.ensureDirectory} */ readonly ensureDir: () => Promise; /** * @alias {@link Pathy.ensureDirectory} */ readonly mkdir: () => Promise; /** * List all immediate children of this path. * * (The path must be folder, and the children * can be files or folders.) */ listChildren(): Promise; /** * @alias {@link Pathy.listChildren} */ readonly ls: () => Promise; listChildrenSync(): Pathy[]; /** * Copy this file/directory to another location. */ copy(destination: PathyOrString, options?: fse.CopyOptions): Promise; /** * @alias {@link Pathy.copy} */ readonly cp: (destination: PathyOrString, options?: fse.CopyOptions) => Promise; /** * Shorthand for performing a copy to destination, followed by deletion */ move(destination: PathyOrString, options?: fse.CopyOptions & PathyCopyOptions): Promise; /** * @alias {@link Pathy.move} */ readonly mv: (destination: PathyOrString, options?: fse.CopyOptions & PathyCopyOptions) => Promise; /** * Delete this path. */ delete(options?: PathyRemoveOptions): Promise; /** * @alias {@link Pathy.delete} */ readonly rm: (options?: PathyRemoveOptions) => Promise; listSiblings(): Promise[]>; findChild(basename: string | RegExp, options?: { assert?: boolean; recursive?: boolean; }): Promise | undefined>; listChildrenRecursively(options?: PathyListChildrenOptions): Promise; findInParents(basename: string, options?: PathyFindParentOptions): Promise | undefined>; findInParentsSync(basename: string, options?: PathyFindParentOptions): Pathy | undefined; /** * Create a new Pathy instance that has the same path * as this one, guaranteed to be absolute. */ toAbsolute(cwd?: string | Pathy): Pathy; /** * Add additional path segments (uses `path.join`). */ append(...paths: string[]): Pathy; changeExtension(to: string): Pathy; changeExtension(from: string[] | string, to: string): Pathy; /** * Check if this path matches another path after both * are normalized. */ equals(other: Pathy | string): boolean; /** * Add a file to this path, assuming it's a directory * (else will throw an error). */ addFileToDirectory(fileName: string, content: any, options?: PathyWriteOptions): Promise>; /** * Get the normalized path as an array of * path segments. */ explode(): string[]; /** * Get all parent paths eventually leading to a given * path. */ lineage(): string[]; /** * Serialize this Pathy instance as a plain string. * * Uses the absolute path, since that's generally * the safest choice. */ toString(options?: { format: 'win32' | 'posix'; relative?: boolean; }): string; /** * This method causes {@link Pathy} instances to automatically * JSON-serialize as their normalized string values. * * If the path is absolute, its cwd-relative path is returned * since that's usually what is desired in a JSON context. * * @example * const pathContainer = { * title: "My path", * path: new Path('/foo/bar') * } * JSON.stringify(pathContainer); // {"title":"My path","path":"/foo/bar"} */ toJSON(): string; /** * The current working directory as a Pathy instance. */ static cwd(): Pathy; /** * Convenience function for ensuring that a path is a * Pathy instance, useful for cases where a function * allows either a string or Pathy instance to be passed. * * If `stringOrInstance` is a Pathy instance, returns * that same instance. If it is undefined, * `process.cwd()` is used. * * See {@link PathyStatic.asString} for the opposite case. */ static asInstance(stringOrInstance: string | Pathy | undefined): Pathy; static isString(thing: any): thing is string; static isPathy(thing: any): thing is Pathy; static isStringOrPathy(thing: any): thing is PathyOrString; } //# sourceMappingURL=pathy.d.ts.map