dax
Version:
Cross platform shell tools inspired by zx.
404 lines • 18.4 kB
TypeScript
import * as dntShim from "../../../../../_dnt.shims.js";
import * as _fs from "./_fs.js";
export type { DirEntryInfo, FileInfo, MkdirOptions, OpenOptions, ReadFileOptions, RemoveOptions, WriteFileOptions, } from "./_fs.js";
export { FsFile } from "./_fs.js";
/** Directory entry when reading a directory. */
export interface DirEntry extends _fs.DirEntryInfo {
/** Path of this directory entry. */
path: Path;
}
/** Options for creating a symlink. */
export interface SymlinkOptions {
/** Creates the symlink as absolute or relative. */
kind: "absolute" | "relative";
/** Kind of symlink to create. Required on Windows when the target doesn't exist. */
type?: "file" | "dir" | "junction";
}
/** Creates a new {@linkcode Path}. Shorthand for `new Path(...)`. */
export declare function path(path: string | URL | Path): Path;
export default path;
/** Represents a path on the file system. */
export declare class Path {
#private;
/** This is a special symbol that allows different versions of
* `Path` API to match on `instanceof` checks. Ideally
* people shouldn't be mixing versions, but if it happens then
* this will maybe reduce some bugs.
* @internal
*/
private static instanceofSymbol;
/** Creates a new path from the provided string, URL, or another Path. */
constructor(path: string | URL | Path);
/** @internal */
static [Symbol.hasInstance](instance: unknown): boolean;
/** Gets the string representation of this path. */
toString(): string;
/** Resolves the path and gets the file URL. */
toFileUrl(): URL;
/** If this path reference is the same as another one. */
equals(otherPath: Path): boolean;
/** Follows symlinks and gets if this path is a directory. */
isDirSync(): boolean;
/** Follows symlinks and gets if this path is a file. */
isFileSync(): boolean;
/** Gets if this path is a symlink. */
isSymlinkSync(): boolean;
/** Gets if this path is an absolute path. */
isAbsolute(): boolean;
/** Gets if this path is relative. */
isRelative(): boolean;
/** Joins the provided path segments onto this path. */
join(...pathSegments: string[]): Path;
/** Resolves this path to an absolute path along with the provided path segments. */
resolve(...pathSegments: string[]): Path;
/**
* Normalizes the `path`, resolving `'..'` and `'.'` segments.
* Note that resolving these segments does not necessarily mean that all will be eliminated.
* A `'..'` at the top-level will be preserved, and an empty path is canonically `'.'`.
*/
normalize(): Path;
/** Resolves the file info of this path following symlinks. */
stat(): Promise<_fs.FileInfo | undefined>;
/** Synchronously resolves the file info of this path following symlinks. */
statSync(): _fs.FileInfo | undefined;
/** Resolves the file info of this path without following symlinks. */
lstat(): Promise<_fs.FileInfo | undefined>;
/** Synchronously resolves the file info of this path without following symlinks. */
lstatSync(): _fs.FileInfo | undefined;
/**
* Gets the directory path. In most cases, it is recommended
* to use `.parent()` instead since it will give you a `PathRef`.
*/
dirname(): string;
/** Gets the file or directory name of the path. */
basename(): string;
/** Resolves the path getting all its ancestor directories in order. */
ancestors(): Generator<Path>;
/** Iterates over the components of a path. */
components(): Generator<string>;
/** Gets if the provided path starts with the specified Path, URL, or string.
*
* This verifies based on matching the components.
*
* ```
* assert(new Path("/a/b/c").startsWith("/a/b"));
* assert(!new Path("/example").endsWith("/exam"));
* ```
*/
startsWith(path: Path | URL | string): boolean;
/** Gets if the provided path ends with the specified Path, URL, or string.
*
* This verifies based on matching the components.
*
* ```
* assert(new Path("/a/b/c").endsWith("b/c"));
* assert(!new Path("/a/b/example").endsWith("ple"));
* ```
*/
endsWith(path: Path | URL | string): boolean;
/** Gets the parent directory or returns undefined if the parent is the root directory. */
parent(): Path | undefined;
/** Gets the parent or throws if the current directory was the root. */
parentOrThrow(): Path;
/**
* Returns the extension of the path with leading period or undefined
* if there is no extension.
*/
extname(): string | undefined;
/** Gets a new path reference with the provided extension. */
withExtname(ext: string): Path;
/** Gets a new path reference with the provided file or directory name. */
withBasename(basename: string): Path;
/** Gets the relative path from this path to the specified path. */
relative(to: string | URL | Path): string;
/** Gets if the path exists. Beware of TOCTOU issues. */
exists(): Promise<boolean>;
/** Synchronously gets if the path exists. Beware of TOCTOU issues. */
existsSync(): boolean;
/** Resolves to the absolute normalized path, with symbolic links resolved. */
realPath(): Promise<Path>;
/** Synchronously resolves to the absolute normalized path, with symbolic links resolved. */
realPathSync(): Path;
/** Creates a directory at this path.
* @remarks By default, this is recursive.
*/
mkdir(options?: _fs.MkdirOptions): Promise<this>;
/** Synchronously creates a directory at this path.
* @remarks By default, this is recursive.
*/
mkdirSync(options?: _fs.MkdirOptions): this;
/**
* Creates a symlink to the provided target path.
*/
symlinkTo(targetPath: URL | Path, opts: SymlinkOptions): Promise<void>;
/**
* Creates a symlink at the provided path with the provided target text.
*/
symlinkTo(target: string, opts?: Partial<SymlinkOptions>): Promise<void>;
/**
* Synchronously creates a symlink to the provided target path.
*/
symlinkToSync(targetPath: URL | Path, opts: SymlinkOptions): void;
/**
* Synchronously creates a symlink at the provided path with the provided target text.
*/
symlinkToSync(target: string, opts?: Partial<SymlinkOptions>): void;
/**
* Creates a hardlink to the provided target path.
*/
linkTo(targetPath: string | URL | Path): Promise<void>;
/**
* Synchronously creates a hardlink to the provided target path.
*/
linkToSync(targetPath: string | URL | Path): void;
/** Reads the entries in the directory. */
readDir(): AsyncIterable<DirEntry>;
/** Synchronously reads the entries in the directory. */
readDirSync(): Iterable<DirEntry>;
/** Reads only the directory file paths, not including symlinks. */
readDirFilePaths(): AsyncIterable<Path>;
/** Synchronously reads only the directory file paths, not including symlinks. */
readDirFilePathsSync(): Iterable<Path>;
/** Reads the bytes from the file. */
readBytes(options?: _fs.ReadFileOptions): Promise<Uint8Array>;
/** Synchronously reads the bytes from the file. */
readBytesSync(): Uint8Array;
/** Calls `.readBytes()`, but returns undefined if the path doesn't exist. */
readMaybeBytes(options?: _fs.ReadFileOptions): Promise<Uint8Array | undefined>;
/** Calls `.readBytesSync()`, but returns undefined if the path doesn't exist. */
readMaybeBytesSync(): Uint8Array | undefined;
/** Reads the text from the file. */
readText(options?: _fs.ReadFileOptions): Promise<string>;
/** Synchronously reads the text from the file. */
readTextSync(): string;
/** Calls `.readText()`, but returns undefined when the path doesn't exist.
* @remarks This still errors for other kinds of errors reading a file.
*/
readMaybeText(options?: _fs.ReadFileOptions): Promise<string | undefined>;
/** Calls `.readTextSync()`, but returns undefined when the path doesn't exist.
* @remarks This still errors for other kinds of errors reading a file.
*/
readMaybeTextSync(): string | undefined;
/** Reads the file's text and returns an array of its lines.
*
* Lines are split at `\n` or `\r\n`. Line terminators are not included.
* A trailing blank line caused by a final line ending is excluded (matches
* [Rust's `str::lines`](https://doc.rust-lang.org/std/primitive.str.html#method.lines)).
*/
lines(options?: _fs.ReadFileOptions): Promise<string[]>;
/** Synchronously reads the file's text and returns an array of its lines.
*
* See `.lines()` for the splitting semantics.
*/
linesSync(): string[];
/** Streams the file and iterates over its lines without loading it all into memory.
*
* See `.lines()` for the splitting semantics.
*/
linesIter(options?: _fs.ReadFileOptions): AsyncIterableIterator<string>;
/** Synchronously streams the file and iterates over its lines without
* loading it all into memory.
*
* See `.lines()` for the splitting semantics.
*/
linesIterSync(): IterableIterator<string>;
/** Reads and parses the file as JSON, throwing if it doesn't exist or is not valid JSON. */
readJson<T>(options?: _fs.ReadFileOptions): Promise<T>;
/** Synchronously reads and parses the file as JSON, throwing if it doesn't
* exist or is not valid JSON. */
readJsonSync<T>(): T;
/**
* Calls `.readJson()`, but returns undefined if the file doesn't exist.
* @remarks This method will still throw if the file cannot be parsed as JSON.
*/
readMaybeJson<T>(options?: _fs.ReadFileOptions): Promise<T | undefined>;
/**
* Calls `.readJsonSync()`, but returns undefined if the file doesn't exist.
* @remarks This method will still throw if the file cannot be parsed as JSON.
*/
readMaybeJsonSync<T>(): T | undefined;
/** Writes out the provided bytes or text to the file.
*
* Strings are encoded as UTF-8.
*/
write(data: string | Uint8Array, options?: _fs.WriteFileOptions): Promise<this>;
/** Synchronously writes out the provided bytes or text to the file.
*
* Strings are encoded as UTF-8.
*/
writeSync(data: string | Uint8Array, options?: _fs.WriteFileOptions): this;
/** Writes the provided text to the file.
* @deprecated Use `.write(text)` instead — `write` now accepts strings.
*/
writeText(text: string, options?: _fs.WriteFileOptions): Promise<this>;
/** Synchronously writes the provided text to the file.
* @deprecated Use `.writeSync(text)` instead — `writeSync` now accepts strings.
*/
writeTextSync(text: string, options?: _fs.WriteFileOptions): this;
/** Writes out the provided object as compact JSON. */
writeJson(obj: unknown, options?: _fs.WriteFileOptions): Promise<this>;
/** Synchronously writes out the provided object as compact JSON. */
writeJsonSync(obj: unknown, options?: _fs.WriteFileOptions): this;
/** Writes out the provided object as formatted JSON. */
writeJsonPretty(obj: unknown, options?: _fs.WriteFileOptions): Promise<this>;
/** Synchronously writes out the provided object as formatted JSON. */
writeJsonPrettySync(obj: unknown, options?: _fs.WriteFileOptions): this;
/** Appends the provided bytes or text to the file.
*
* Strings are encoded as UTF-8.
*/
append(data: string | Uint8Array, options?: Omit<_fs.WriteFileOptions, "append">): Promise<this>;
/** Synchronously appends the provided bytes or text to the file.
*
* Strings are encoded as UTF-8.
*/
appendSync(data: string | Uint8Array, options?: Omit<_fs.WriteFileOptions, "append">): this;
/** Changes the permissions of the file or directory. */
chmod(mode: number): Promise<this>;
/** Synchronously changes the permissions of the file or directory. */
chmodSync(mode: number): this;
/** Changes the ownership permissions of the file. */
chown(uid: number | null, gid: number | null): Promise<this>;
/** Synchronously changes the ownership permissions of the file. */
chownSync(uid: number | null, gid: number | null): this;
/** Creates a new file or opens the existing one. */
create(): Promise<FsFileWrapper>;
/** Synchronously creates a new file or opens the existing one. */
createSync(): FsFileWrapper;
/** Creates a file throwing if a file previously existed. */
createNew(): Promise<FsFileWrapper>;
/** Synchronously creates a file throwing if a file previously existed. */
createNewSync(): FsFileWrapper;
/** Opens a file. */
open(options?: _fs.OpenOptions): Promise<FsFileWrapper>;
/** Opens a file synchronously. */
openSync(options?: _fs.OpenOptions): FsFileWrapper;
/** Removes the file or directory from the file system. */
remove(options?: _fs.RemoveOptions): Promise<this>;
/** Removes the file or directory from the file system synchronously. */
removeSync(options?: _fs.RemoveOptions): this;
/** Removes the file or directory from the file system, but doesn't throw
* when the file doesn't exist.
*/
ensureRemove(options?: _fs.RemoveOptions): Promise<this>;
/** Removes the file or directory from the file system, but doesn't throw
* when the file doesn't exist.
*/
ensureRemoveSync(options?: _fs.RemoveOptions): this;
/**
* Ensures that a directory is empty.
* Deletes directory contents if the directory is not empty.
* If the directory does not exist, it is created.
* The directory itself is not deleted.
*/
emptyDir(): Promise<this>;
/** Synchronous version of `emptyDir()` */
emptyDirSync(): this;
/** Ensures that the directory exists.
* If the directory structure does not exist, it is created. Like mkdir -p.
*/
ensureDir(): Promise<this>;
/** Synchronously ensures that the directory exists.
* If the directory structure does not exist, it is created. Like mkdir -p.
*/
ensureDirSync(): this;
/**
* Ensures that the file exists.
* If the file that is requested to be created is in directories that do
* not exist these directories are created. If the file already exists,
* it is NOTMODIFIED.
*/
ensureFile(): Promise<this>;
/**
* Synchronously ensures that the file exists.
* If the file that is requested to be created is in directories that do
* not exist these directories are created. If the file already exists,
* it is NOTMODIFIED.
*/
ensureFileSync(): this;
/** Copies a file or directory to the provided destination.
* @returns The destination path.
*/
copy(destinationPath: string | URL | Path, options?: {
overwrite?: boolean;
}): Promise<Path>;
/** Copies a file or directory to the provided destination synchronously.
* @returns The destination path.
*/
copySync(destinationPath: string | URL | Path, options?: {
overwrite?: boolean;
}): Path;
/**
* Copies the file or directory to the specified directory.
* @returns The destination path.
*/
copyToDir(destinationDirPath: string | URL | Path, options?: {
overwrite?: boolean;
}): Promise<Path>;
/**
* Copies the file or directory to the specified directory synchronously.
* @returns The destination path.
*/
copyToDirSync(destinationDirPath: string | URL | Path, options?: {
overwrite?: boolean;
}): Path;
/**
* Copies the file to the specified destination path.
* @returns The destination path.
*/
copyFile(destinationPath: string | URL | Path): Promise<Path>;
/**
* Copies the file to the destination path synchronously.
* @returns The destination path.
*/
copyFileSync(destinationPath: string | URL | Path): Path;
/**
* Copies the file to the specified directory.
* @returns The destination path.
*/
copyFileToDir(destinationDirPath: string | URL | Path): Promise<Path>;
/**
* Copies the file to the specified directory synchronously.
* @returns The destination path.
*/
copyFileToDirSync(destinationDirPath: string | URL | Path): Path;
/**
* Moves the file or directory returning a promise that resolves to
* the renamed path.
* @returns The destination path.
*/
rename(newPath: string | URL | Path): Promise<Path>;
/**
* Moves the file or directory returning the renamed path synchronously.
* @returns The destination path.
*/
renameSync(newPath: string | URL | Path): Path;
/**
* Moves the file or directory to the specified directory.
* @returns The destination path.
*/
renameToDir(destinationDirPath: string | URL | Path): Promise<Path>;
/**
* Moves the file or directory to the specified directory synchronously.
* @returns The destination path.
*/
renameToDirSync(destinationDirPath: string | URL | Path): Path;
/** Opens the file and pipes it to the writable stream. */
pipeTo(dest: dntShim.WritableStream<Uint8Array>, options?: dntShim.StreamPipeOptions): Promise<this>;
}
/** A handle to an open file. */
export declare class FsFileWrapper extends _fs.FsFile {
/** Writes the provided text to this file, looping to handle partial writes. */
writeText(text: string): Promise<this>;
/** Synchronously writes the provided text to this file, looping to handle partial writes. */
writeTextSync(text: string): this;
/** Writes all the provided bytes to this file, looping to handle partial writes. */
writeBytes(bytes: Uint8Array): Promise<this>;
/** Synchronously writes all the provided bytes to this file, looping to handle partial writes. */
writeBytesSync(bytes: Uint8Array): this;
/** Writes all the provided data to this file, dispatching to `.writeText` or `.writeBytes` based on input type. */
writeAll(data: string | Uint8Array): Promise<this>;
/** Synchronously writes all the provided data to this file, dispatching to `.writeTextSync` or `.writeBytesSync` based on input type. */
writeAllSync(data: string | Uint8Array): this;
}
//# sourceMappingURL=mod.d.ts.map