Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/dax/mod.ts>PathRef

Cross platform shell tools for Deno inspired by zx.
Very Popular
Go to Latest
class PathRef
Re-export
import { PathRef } from "https://deno.land/x/dax@0.33.0/mod.ts";

Holds a reference to a path providing helper methods.

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

Constructors

new
PathRef(path:
| string
| URL
)

Methods

ancestors(): Generator<PathRef>

Resolves the path getting all its ancestor directories in order.

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.

copyFile(destinationPath: string | URL | PathRef): Promise<PathRef>

Copies the file to the specified destination path.

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

Copies the file to the destination path synchronously.

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

Copies the file to the specified directory.

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

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 | PathRef, 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.

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 PathRef.

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()

equals(otherPath: PathRef): boolean

If this path reference is the same as another one.

exists(): Promise<boolean>

Gets if the path exists. Beaware of TOCTOU issues.

existsSync(): boolean

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

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

Expands the glob using the current path as the root.

expandGlobSync(glob: string | URL, options?: Omit<fs.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.

isDir(): boolean

Follows symlinks and gets if this path is a directory.

isFile(): boolean

Follows symlinks and gets if this path is a file.

isRelative(): boolean

Gets if this path is relative.

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

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(): PathRef | 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<PathRef>

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<PathRef>

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 | PathRef): 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 | PathRef): Promise<PathRef>

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

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

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

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

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

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?: fs.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?: fs.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): PathRef

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

withExtname(ext: string): PathRef

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 PathRef 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).