Skip to main content
Module

x/dax/src/path.ts>Path

Cross-platform shell tools for Deno and Node.js inspired by zx.
Very Popular
Latest
class Path
import { Path } from "https://deno.land/x/dax@0.39.2/src/path.ts";

Holds a reference to a path providing helper methods.

Create one via $: const srcDir = $.path("src");

Constructors

new
Path(path:
| string
| URL
| Path
)

Methods

ancestors(): Generator<Path>

Resolves the path getting all its ancestor directories in order.

append(data: Uint8Array, options?: Omit<Deno.WriteFileOptions, "append">): Promise<this>

Appends the provided bytes to the file.

appendSync(data: Uint8Array, options?: Omit<Deno.WriteFileOptions, "append">): this

Synchronously appends the provided bytes to the file.

appendText(text: string, options?: Omit<Deno.WriteFileOptions, "append">): Promise<this>

Appends the provided text to the file.

appendTextSync(text: string, options?: Omit<Deno.WriteFileOptions, "append">): this

Synchronously appends the provided text to the file.

basename(): string

Gets the file or directory name of the path.

chmod(mode: number): Promise<this>

Changes the permissions of the file or directory.

chmodSync(mode: number): this

Synchronously changes the permissions of the file or directory.

chown(uid: number | null, gid: number | null): Promise<this>

Changes the ownership permissions of the file.

chownSync(uid: number | null, gid: number | null): this

Synchronously changes the ownership permissions of the file.

components(): Generator<string>
copyFile(destinationPath: string | URL | Path): Promise<Path>

Copies the file to the specified destination path.

copyFileSync(destinationPath: string | URL | Path): Path

Copies the file to the destination path synchronously.

copyFileToDir(destinationDirPath: string | URL | Path): Promise<Path>

Copies the file to the specified directory.

copyFileToDirSync(destinationDirPath: string | URL | Path): Path

Copies the file to the specified directory synchronously.

create(): Promise<FsFileWrapper>

Creates a new file or opens the existing one.

Creates a file throwing if a file previously existed.

Synchronously creates a file throwing if a file previously existed.

createSymlinkTo(targetPath: URL | Path, opts: Partial<Deno.SymlinkOptions> & PathSymlinkOptions): Promise<void>

Creates a symlink to the provided target path.

createSymlinkTo(target: string, opts?: SymlinkOptions): Promise<void>

Creates a symlink at the provided path with the provided target text.

createSymlinkToSync(targetPath: URL | Path, opts: Partial<Deno.SymlinkOptions> & PathSymlinkOptions): void

Synchronously creates a symlink to the provided target path.

createSymlinkToSync(target: string, opts?: SymlinkOptions): void

Synchronously creates a symlink at the provided path with the provided target text.

Synchronously creates a new file or opens the existing one.

dirname(): string

Gets the directory path. In most cases, it is recommended to use .parent() instead since it will give you a Path.

emptyDir(): Promise<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.

emptyDirSync(): this

Synchronous version of emptyDir()

endsWith(path: Path | URL | string): boolean
ensureDir(): Promise<this>

Ensures that the directory exists. If the directory structure does not exist, it is created. Like mkdir -p.

Synchronously ensures that the directory exists. If the directory structure does not exist, it is created. Like mkdir -p.

ensureFile(): Promise<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.

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.

equals(otherPath: Path): boolean

If this path reference is the same as another one.

exists(): Promise<boolean>

Gets if the path exists. Beware of TOCTOU issues.

existsSync(): boolean

Synchronously gets if the path exists. Beware of TOCTOU issues.

expandGlob(glob: string | URL, options?: Omit<ExpandGlobOptions, "root">): AsyncGenerator<WalkEntry, void, unknown>

Expands the glob using the current path as the root.

expandGlobSync(glob: string | URL, options?: Omit<ExpandGlobOptions, "root">): Generator<WalkEntry, void, unknown>

Synchronously expands the glob using the current path as the root.

extname(): string | undefined

Returns the extension of the path with leading period or undefined if there is no extension.

isAbsolute(): boolean

Gets if this path is an absolute path.

isDirSync(): boolean

Follows symlinks and gets if this path is a directory.

isFileSync(): boolean

Follows symlinks and gets if this path is a file.

isRelative(): boolean

Gets if this path is relative.

isSymlinkSync(): boolean

Gets if this path is a symlink.

join(...pathSegments: string[]): Path

Joins the provided path segments onto this path.

lstat(): Promise<Deno.FileInfo | undefined>

Resolves the Deno.FileInfo of this path without following symlinks.

lstatSync(): Deno.FileInfo | undefined

Synchronously resolves the Deno.FileInfo of this path without following symlinks.

mkdir(options?: Deno.MkdirOptions): Promise<this>

Creates a directory at this path.

mkdirSync(options?: Deno.MkdirOptions): this

Synchronously creates a directory at this 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 '.'.

open(options?: Deno.OpenOptions): Promise<FsFileWrapper>

Opens a file.

Opens a file synchronously.

parent(): Path | undefined

Gets the parent directory or returns undefined if the parent is the root directory.

Gets the parent or throws if the current directory was the root.

pipeTo(dest: WritableStream<Uint8Array>, options?: PipeOptions): Promise<this>

Opens the file and pipes it to the writable stream.

readBytes(options?: Deno.ReadFileOptions): Promise<Uint8Array>

Reads the bytes from the file.

readBytesSync(): Uint8Array

Synchronously reads the bytes from the file.

readDir(): AsyncIterable<WalkEntry>

Reads the entries in the directory.

readDirFilePaths(): AsyncIterable<Path>

Reads only the directory file paths, not including symlinks.

Synchronously reads only the directory file paths, not including symlinks.

readDirSync(): Iterable<WalkEntry>

Synchronously reads the entries in the directory.

readJson<T>(options?: Deno.ReadFileOptions): Promise<T>

Reads and parses the file as JSON, throwing if it doesn't exist or is not valid JSON.

readJsonSync<T>(): T

Synchronously reads and parses the file as JSON, throwing if it doesn't exist or is not valid JSON.

readMaybeBytes(options?: Deno.ReadFileOptions): Promise<Uint8Array | undefined>

Calls .readBytes(), but returns undefined if the path doesn't exist.

readMaybeBytesSync(): Uint8Array | undefined

Calls .readBytesSync(), but returns undefined if the path doesn't exist.

readMaybeJson<T>(options?: Deno.ReadFileOptions): Promise<T | undefined>

Calls .readJson(), but returns undefined if the file doesn't exist.

readMaybeJsonSync<T>(): T | undefined

Calls .readJsonSync(), but returns undefined if the file doesn't exist.

readMaybeText(options?: Deno.ReadFileOptions): Promise<string | undefined>

Calls .readText(), but returns undefined when the path doesn't exist.

readMaybeTextSync(): string | undefined

Calls .readTextSync(), but returns undefined when the path doesn't exist.

readText(options?: Deno.ReadFileOptions): Promise<string>

Reads the text from the file.

readTextSync(): string

Synchronously reads the text from the file.

realPath(): Promise<Path>

Resolves to the absolute normalized path, with symbolic links resolved.

Synchronously resolves to the absolute normalized path, with symbolic links resolved.

relative(to: string | URL | Path): string

Gets the relative path from this path to the specified path.

remove(options?: Deno.RemoveOptions): Promise<this>

Removes the file or directory from the file system.

Removes the file or directory from the file system synchronously.

rename(newPath: string | URL | Path): Promise<Path>

Moves the file or directory returning a promise that resolves to the renamed path.

renameSync(newPath: string | URL | Path): Path

Moves the file or directory returning the renamed path synchronously.

renameToDir(destinationDirPath: string | URL | Path): Promise<Path>

Moves the file or directory to the specified directory.

renameToDirSync(destinationDirPath: string | URL | Path): Path

Moves the file or directory to the specified directory synchronously.

resolve(...pathSegments: string[]): Path

Resolves this path to an absolute path along with the provided path segments.

startsWith(path: Path | URL | string): boolean
stat(): Promise<Deno.FileInfo | undefined>

Resolves the Deno.FileInfo of this path following symlinks.

statSync(): Deno.FileInfo | undefined

Synchronously resolves the Deno.FileInfo of this path following symlinks.

Resolves the path and gets the file URL.

toString(): string

Gets the string representation of this path.

walk(options?: WalkOptions): AsyncIterableIterator<WalkEntry>

Walks the file tree rooted at the current path, yielding each file or directory in the tree filtered according to the given options.

walkSync(options?: WalkOptions): Iterable<WalkEntry>

Synchronously walks the file tree rooted at the current path, yielding each file or directory in the tree filtered according to the given options.

withBasename(basename: string): Path

Gets a new path reference with the provided file or directory name.

withExtname(ext: string): Path

Gets a new path reference with the provided extension.

write(data: Uint8Array, options?: Deno.WriteFileOptions): Promise<this>

Writes out the provided bytes to the file.

writeJson(obj: unknown, options?: Deno.WriteFileOptions): Promise<this>

Writes out the provided object as compact JSON.

writeJsonPretty(obj: unknown, options?: Deno.WriteFileOptions): Promise<this>

Writes out the provided object as formatted JSON.

writeJsonPrettySync(obj: unknown, options?: Deno.WriteFileOptions): this

Synchronously writes out the provided object as formatted JSON.

writeJsonSync(obj: unknown, options?: Deno.WriteFileOptions): this

Synchronously writes out the provided object as compact JSON.

writeSync(data: Uint8Array, options?: Deno.WriteFileOptions): this

Synchronously writes out the provided bytes to the file.

writeText(text: string, options?: Deno.WriteFileOptions): Promise<this>

Writes out the provided text to the file.

writeTextSync(text: string, options?: Deno.WriteFileOptions): this

Synchronously writes out the provided text to the file.

Static Properties

private
instanceofSymbol

This is a special symbol that allows different versions of Dax's 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 (or cause some... tbd).

Static Methods

[Symbol.hasInstance](instance: any): boolean