Skip to main content

@david/path@0.2.0
Built and signed on GitHub Actions

Path class for JavaScript built on top of Deno's standard library.

This package works with Deno
This package works with Deno
JSR Score
100%
Published
8 months ago (0.2.0)

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

class Path

Represents a path on the file system.

Constructors

new
Path(path: )

Creates a new path from the provided string, URL, or another Path.

Methods

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.

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.

Iterates over the components of a path.

copy(
destinationPath: ,
options?: { overwrite?: boolean; },
): Promise<Path>

Copies a file or directory to the provided destination.

copyFile(destinationPath: ): Promise<Path>

Copies the file to the specified destination path.

copyFileSync(destinationPath: ): Path

Copies the file to the destination path synchronously.

copyFileToDir(destinationDirPath: ): Promise<Path>

Copies the file to the specified directory.

copyFileToDirSync(destinationDirPath: ): Path

Copies the file to the specified directory synchronously.

copySync(
destinationPath: ,
options?: { overwrite?: boolean; },
): Path

Copies a file or directory to the provided destination synchronously.

copyToDir(
destinationDirPath: ,
options?: { overwrite?: boolean; },
): Promise<Path>

Copies the file or directory to the specified directory.

copyToDirSync(
destinationDirPath: ,
options?: { overwrite?: boolean; },
): Path

Copies the file or directory to the specified directory synchronously.

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.

Synchronously creates a new file or opens the existing one.

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

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.

Synchronous version of emptyDir()

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"));

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.

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.

Removes the file or directory from the file system, but doesn't throw when the file doesn't exist.

Removes the file or directory from the file system, but doesn't throw when the file doesn't exist.

equals(otherPath: Path): boolean

If this path reference is the same as another one.

Gets if the path exists. Beware of TOCTOU issues.

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

extname(): string | undefined

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

Gets if this path is an absolute path.

Follows symlinks and gets if this path is a directory.

Follows symlinks and gets if this path is a file.

Gets if this path is relative.

Gets if this path is a symlink.

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

Joins the provided path segments onto this path.

linkTo(targetPath: ): Promise<void>

Creates a hardlink to the provided target path.

linkToSync(targetPath: ): void

Synchronously creates a hardlink to the provided target 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.

Creates a directory at this path.

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

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(
options?: StreamPipeOptions,
): Promise<this>

Opens the file and pipes it to the writable stream.

Reads the bytes from the file.

Synchronously reads the bytes from the file.

readDir(): AsyncIterable<DirEntry>

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

Synchronously reads the entries in the directory.

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

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

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

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.

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

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

Reads the text from the file.

Synchronously reads the text from the file.

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

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

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

Removes the file or directory from the file system.

Removes the file or directory from the file system synchronously.

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

Moves the file or directory returning the renamed path synchronously.

renameToDir(destinationDirPath: ): Promise<Path>

Moves the file or directory to the specified directory.

renameToDirSync(destinationDirPath: ): 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.

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"));
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.

symlinkTo(
targetPath: URL | Path,
): Promise<void>

Creates a symlink to the provided target path.

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

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

symlinkToSync(
targetPath: URL | Path,
): void

Synchronously creates a symlink to the provided target path.

symlinkToSync(
target: string,
opts?: Partial<SymlinkOptions>,
): void

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

Resolves the path and gets the file URL.

Gets the string representation of this path.

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

Gets a new path reference with the provided extension.

Writes out the provided bytes or text to the file.

writeJson(
obj: unknown,
): Promise<this>

Writes out the provided object as compact JSON.

writeJsonPretty(
obj: unknown,
): Promise<this>

Writes out the provided object as formatted JSON.

writeJsonPrettySync(
obj: unknown,
): this

Synchronously writes out the provided object as formatted JSON.

writeJsonSync(
obj: unknown,
): this

Synchronously writes out the provided object as compact JSON.

Synchronously writes out the provided bytes or text to the file.

Writes the provided text to this file.

Synchronously writes the provided text to this file.

Static Properties

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.

Static Methods

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

Add Package

deno add jsr:@david/path

Import symbol

import { Path } from "@david/path";

---- OR ----

Import directly with a jsr specifier

import { Path } from "jsr:@david/path";