From 6d773e4a7ba8f1beb9f48612950e8c59b7a908ae Mon Sep 17 00:00:00 2001 From: David Sherret Date: Sat, 11 Feb 2023 23:13:03 -0500 Subject: [PATCH] refactor: `PathReference` -> `PathRef` --- README.md | 6 +-- mod.ts | 8 ++-- src/command.ts | 6 +-- src/deps.test.ts | 6 +-- src/path.test.ts | 120 +++++++++++++++++++++++------------------------ src/path.ts | 90 +++++++++++++++++------------------ src/request.ts | 28 +++++------ 7 files changed, 132 insertions(+), 132 deletions(-) diff --git a/README.md b/README.md index 236da56..04d09b4 100644 --- a/README.md +++ b/README.md @@ -472,12 +472,12 @@ pb.with(() => { ## Path API -The path API offers an immutable `PathReference` class, which is a similar concept to Rust's `PathBuf` struct. +The path API offers an immutable `PathRef` class, which is a similar concept to Rust's `PathBuf` struct. To create a `PathReference`, do the following: ```ts -// create a `PathReference` +// create a `PathRef` let srcDir = $.path("src"); // get information about the path srcDir.isDir(); // false @@ -511,7 +511,7 @@ const srcDir = $.path("src").resolve(); await $`echo ${srcDir}`; ``` -There are a lot of helper methods here, so check the [documentation on PathReference](https://deno.land/x/dax/src/path.ts/~/PathReference) for more details. +There are a lot of helper methods here, so check the [documentation on PathRef](https://deno.land/x/dax/src/path.ts/~/PathRef) for more details. ## Helper functions diff --git a/mod.ts b/mod.ts index 59e51b4..9eddd29 100644 --- a/mod.ts +++ b/mod.ts @@ -28,9 +28,9 @@ import { import { colors, fs, outdent, path as stdPath, which, whichSync } from "./src/deps.ts"; import { wasmInstance } from "./src/lib/mod.ts"; import { RequestBuilder, withProgressBarFactorySymbol } from "./src/request.ts"; -import { createPathReference } from "./src/path.ts"; +import { createPathRef } from "./src/path.ts"; -export { FsFileWrapper, PathReference } from "./src/path.ts"; +export { FsFileWrapper, PathRef } from "./src/path.ts"; export { CommandBuilder, CommandResult } from "./src/command.ts"; export type { CommandContext, CommandHandler, CommandPipeReader, CommandPipeWriter } from "./src/command_handler.ts"; export type { @@ -221,7 +221,7 @@ export interface $BuiltInProperties { /** Helper function for creating path references, which provide an easier way for * working with paths, directories, and files on the file system. Also, a re-export * of deno_std's `path` module as properties on this object. */ - path: typeof createPathReference & typeof stdPath; + path: typeof createPathRef & typeof stdPath; /** * Logs with potential indentation (`$.logIndent`) * and output of commands or request responses. @@ -531,7 +531,7 @@ function buildInitial$State( const helperObject = { fs, - path: Object.assign(createPathReference, stdPath), + path: Object.assign(createPathRef, stdPath), cd, escapeArg, stripAnsi(text: string) { diff --git a/src/command.ts b/src/command.ts index 1bc6225..bcd8c8a 100644 --- a/src/command.ts +++ b/src/command.ts @@ -24,7 +24,7 @@ import { parseCommand, spawn } from "./shell.ts"; import { cpCommand, mvCommand } from "./commands/cp_mv.ts"; import { isShowingProgressBars } from "./console/progress/interval.ts"; import { touchCommand } from "./commands/touch.ts"; -import { PathReference } from "./path.ts"; +import { PathRef } from "./path.ts"; type BufferStdio = "inherit" | "null" | "streamed" | Buffer; @@ -293,11 +293,11 @@ export class CommandBuilder implements PromiseLike { } /** Sets the current working directory to use when executing this command. */ - cwd(dirPath: string | URL | PathReference) { + cwd(dirPath: string | URL | PathRef) { return this.#newWithState((state) => { state.cwd = dirPath instanceof URL ? path.fromFileUrl(dirPath) - : dirPath instanceof PathReference + : dirPath instanceof PathRef ? dirPath.resolve().toString() : path.resolve(dirPath); }); diff --git a/src/deps.test.ts b/src/deps.test.ts index 5cff8bf..61e7364 100644 --- a/src/deps.test.ts +++ b/src/deps.test.ts @@ -1,4 +1,4 @@ -import { createPathReference, PathReference } from "./path.ts"; +import { createPathRef, PathRef } from "./path.ts"; export { assert, @@ -14,12 +14,12 @@ export { serve } from "https://deno.land/std@0.171.0/http/server.ts"; * Creates a temporary directory, changes the cwd to this directory, * then cleans up and restores the cwd when complete. */ -export async function withTempDir(action: (path: PathReference) => Promise | void) { +export async function withTempDir(action: (path: PathRef) => Promise | void) { const originalDirPath = Deno.cwd(); const dirPath = Deno.makeTempDirSync(); Deno.chdir(dirPath); try { - await action(createPathReference(dirPath).resolve()); + await action(createPathRef(dirPath).resolve()); } finally { try { await Deno.remove(dirPath, { recursive: true }); diff --git a/src/path.test.ts b/src/path.test.ts index 3c3b035..193bd3c 100644 --- a/src/path.test.ts +++ b/src/path.test.ts @@ -1,39 +1,39 @@ import { assert, assertEquals, assertRejects, assertThrows, withTempDir } from "./deps.test.ts"; -import { createPathReference } from "./path.ts"; +import { createPathRef } from "./path.ts"; import { path as stdPath } from "./deps.ts"; Deno.test("join", () => { - const path = createPathReference("src"); + const path = createPathRef("src"); const newPath = path.join("other", "test"); assertEquals(path.toString(), "src"); assertEquals(newPath.toString(), stdPath.join("src", "other", "test")); }); Deno.test("resolve", () => { - const path = createPathReference("src").resolve(); + const path = createPathRef("src").resolve(); assertEquals(path.toString(), stdPath.resolve("src")); }); Deno.test("normalize", () => { - const path = createPathReference("src").normalize(); + const path = createPathRef("src").normalize(); assertEquals(path.toString(), stdPath.normalize("src")); }); Deno.test("isDir", () => { - assert(createPathReference("src").isDir()); - assert(!createPathReference("mod.ts").isDir()); - assert(!createPathReference("nonExistent").isDir()); + assert(createPathRef("src").isDir()); + assert(!createPathRef("mod.ts").isDir()); + assert(!createPathRef("nonExistent").isDir()); }); Deno.test("isFile", () => { - assert(!createPathReference("src").isFile()); - assert(createPathReference("mod.ts").isFile()); - assert(!createPathReference("nonExistent").isFile()); + assert(!createPathRef("src").isFile()); + assert(createPathRef("mod.ts").isFile()); + assert(!createPathRef("nonExistent").isFile()); }); Deno.test("isSymlink", async () => { await withTempDir(() => { - const path = createPathReference("file.txt").writeTextSync(""); + const path = createPathRef("file.txt").writeTextSync(""); const newPath = path.createAbsoluteSymlinkAtSync("test.txt"); assert(newPath.isSymlink()); assert(!path.isSymlink()); @@ -41,31 +41,31 @@ Deno.test("isSymlink", async () => { }); Deno.test("isAbsolute", () => { - assert(!createPathReference("src").isAbsolute()); - assert(createPathReference("src").resolve().isAbsolute()); + assert(!createPathRef("src").isAbsolute()); + assert(createPathRef("src").resolve().isAbsolute()); }); Deno.test("isRelative", () => { - assert(createPathReference("src").isRelative()); - assert(!createPathReference("src").resolve().isRelative()); + assert(createPathRef("src").isRelative()); + assert(!createPathRef("src").resolve().isRelative()); }); Deno.test("parent", () => { - const parent = createPathReference("src").parent()!; + const parent = createPathRef("src").parent()!; assertEquals(parent.toString(), Deno.cwd()); const lastParent = Array.from(parent.ancestors()).at(-1)!; assertEquals(lastParent.parent(), undefined); }); Deno.test("parentOrThrow", () => { - const parent = createPathReference("src").parentOrThrow(); + const parent = createPathRef("src").parentOrThrow(); assertEquals(parent.toString(), Deno.cwd()); const lastParent = Array.from(parent.ancestors()).at(-1)!; assertThrows(() => lastParent.parentOrThrow(), Error); }); Deno.test("ancestors", () => { - const srcDir = createPathReference("src").resolve(); + const srcDir = createPathRef("src").resolve(); let lastDir = srcDir; for (const ancestor of srcDir.ancestors()) { assert(ancestor.toString().length < lastDir.toString().length); @@ -74,13 +74,13 @@ Deno.test("ancestors", () => { }); Deno.test("stat", async () => { - const stat1 = await createPathReference("src").stat(); + const stat1 = await createPathRef("src").stat(); assertEquals(stat1?.isDirectory, true); - const stat2 = await createPathReference("nonExistent").stat(); + const stat2 = await createPathRef("nonExistent").stat(); assertEquals(stat2, undefined); await withTempDir(async () => { - const dir = createPathReference("temp.txt").writeTextSync(""); + const dir = createPathRef("temp.txt").writeTextSync(""); const destinationPath = await dir.createAbsoluteSymlinkAt("other.txt"); const stat3 = await destinationPath.stat(); assertEquals(stat3!.isFile, true); @@ -89,13 +89,13 @@ Deno.test("stat", async () => { }); Deno.test("statSync", async () => { - const stat1 = createPathReference("src").statSync(); + const stat1 = createPathRef("src").statSync(); assertEquals(stat1?.isDirectory, true); - const stat2 = createPathReference("nonExistent").statSync(); + const stat2 = createPathRef("nonExistent").statSync(); assertEquals(stat2, undefined); await withTempDir(() => { - const dir = createPathReference("temp.txt").writeTextSync(""); + const dir = createPathRef("temp.txt").writeTextSync(""); const destinationPath = dir.createAbsoluteSymlinkAtSync("other.txt"); const stat3 = destinationPath.statSync(); assertEquals(stat3!.isFile, true); @@ -104,13 +104,13 @@ Deno.test("statSync", async () => { }); Deno.test("lstat", async () => { - const stat1 = await createPathReference("src").lstat(); + const stat1 = await createPathRef("src").lstat(); assertEquals(stat1?.isDirectory, true); - const stat2 = await createPathReference("nonExistent").lstat(); + const stat2 = await createPathRef("nonExistent").lstat(); assertEquals(stat2, undefined); await withTempDir(async () => { - const dir = createPathReference("temp.txt").writeTextSync(""); + const dir = createPathRef("temp.txt").writeTextSync(""); const destinationPath = await dir.createRelativeSymlinkAt("other.txt"); const stat3 = await destinationPath.lstat(); assertEquals(stat3!.isSymlink, true); @@ -118,13 +118,13 @@ Deno.test("lstat", async () => { }); Deno.test("lstatSync", async () => { - const stat1 = createPathReference("src").lstatSync(); + const stat1 = createPathRef("src").lstatSync(); assertEquals(stat1?.isDirectory, true); - const stat2 = createPathReference("nonExistent").lstatSync(); + const stat2 = createPathRef("nonExistent").lstatSync(); assertEquals(stat2, undefined); await withTempDir(() => { - const dir = createPathReference("temp.txt").writeTextSync(""); + const dir = createPathRef("temp.txt").writeTextSync(""); const destinationPath = dir.createRelativeSymlinkAtSync("other.txt"); const stat3 = destinationPath.lstatSync(); assertEquals(stat3!.isSymlink, true); @@ -132,7 +132,7 @@ Deno.test("lstatSync", async () => { }); Deno.test("withExtname", () => { - let path = createPathReference("src").resolve(); + let path = createPathRef("src").resolve(); path = path.join("temp", "other"); assertEquals(path.basename(), "other"); assertEquals(path.extname(), undefined); @@ -147,7 +147,7 @@ Deno.test("withExtname", () => { }); Deno.test("withBasename", () => { - let path = createPathReference("src").resolve(); + let path = createPathRef("src").resolve(); path = path.join("temp", "other"); assertEquals(path.basename(), "other"); path = path.withBasename("test"); @@ -157,8 +157,8 @@ Deno.test("withBasename", () => { }); Deno.test("relative", () => { - const path1 = createPathReference("src"); - const path2 = createPathReference(".github"); + const path1 = createPathRef("src"); + const path2 = createPathRef(".github"); assertEquals( path1.relative(path2), Deno.build.os === "windows" ? "..\\.github" : "../.github", @@ -167,7 +167,7 @@ Deno.test("relative", () => { Deno.test("exists", async () => { await withTempDir(async () => { - const file = createPathReference("file"); + const file = createPathRef("file"); assert(!await file.exists()); assert(!file.existsSync()); file.writeTextSync(""); @@ -178,7 +178,7 @@ Deno.test("exists", async () => { Deno.test("realpath", async () => { await withTempDir(async () => { - let file = createPathReference("file").resolve(); + let file = createPathRef("file").resolve(); file.writeTextSync(""); // need to do realPathSync for GH actions CI file = file.realPathSync(); @@ -196,7 +196,7 @@ Deno.test("realpath", async () => { Deno.test("mkdir", async () => { await withTempDir(async () => { - const path = createPathReference("dir"); + const path = createPathRef("dir"); await path.mkdir(); assert(path.isDir()); path.removeSync(); @@ -218,7 +218,7 @@ Deno.test("mkdir", async () => { Deno.test("createAbsoluteSymlinkTo", async () => { await withTempDir(async () => { - const destFile = createPathReference("temp.txt").writeTextSync(""); + const destFile = createPathRef("temp.txt").writeTextSync(""); const otherFile = destFile.parentOrThrow().join("other.txt"); await otherFile.createAbsoluteSymlinkTo(destFile); const stat = await otherFile.stat(); @@ -230,7 +230,7 @@ Deno.test("createAbsoluteSymlinkTo", async () => { Deno.test("createAbsoluteSymlinkToSync", async () => { await withTempDir(() => { - const destFile = createPathReference("temp.txt").writeTextSync(""); + const destFile = createPathRef("temp.txt").writeTextSync(""); const otherFile = destFile.parentOrThrow().join("other.txt"); otherFile.createAbsoluteSymlinkToSync(destFile); const stat = otherFile.statSync(); @@ -242,7 +242,7 @@ Deno.test("createAbsoluteSymlinkToSync", async () => { Deno.test("readDir", async () => { await withTempDir(async () => { - const dir = createPathReference(".").resolve(); + const dir = createPathRef(".").resolve(); dir.join("file1").writeTextSync(""); dir.join("file2").writeTextSync(""); @@ -267,12 +267,12 @@ Deno.test("readDir", async () => { Deno.test("bytes", async () => { await withTempDir(async () => { - const file = createPathReference("file.txt"); + const file = createPathRef("file.txt"); const bytes = new TextEncoder().encode("asdf"); file.writeSync(bytes); assertEquals(file.bytesSync(), bytes); assertEquals(await file.bytes(), bytes); - const nonExistent = createPathReference("not-exists"); + const nonExistent = createPathRef("not-exists"); assertThrows(() => nonExistent.bytesSync()); await assertRejects(() => nonExistent.bytes()); }); @@ -280,12 +280,12 @@ Deno.test("bytes", async () => { Deno.test("maybeBytes", async () => { await withTempDir(async () => { - const file = createPathReference("file.txt"); + const file = createPathRef("file.txt"); const bytes = new TextEncoder().encode("asdf"); await file.write(bytes); assertEquals(file.maybeBytesSync(), bytes); assertEquals(await file.maybeBytes(), bytes); - const nonExistent = createPathReference("not-exists"); + const nonExistent = createPathRef("not-exists"); assertEquals(await nonExistent.maybeText(), undefined); assertEquals(nonExistent.maybeTextSync(), undefined); }); @@ -293,11 +293,11 @@ Deno.test("maybeBytes", async () => { Deno.test("text", async () => { await withTempDir(async () => { - const file = createPathReference("file.txt"); + const file = createPathRef("file.txt"); file.writeTextSync("asdf"); assertEquals(file.maybeTextSync(), "asdf"); assertEquals(await file.maybeText(), "asdf"); - const nonExistent = createPathReference("not-exists"); + const nonExistent = createPathRef("not-exists"); assertThrows(() => nonExistent.textSync()); await assertRejects(() => nonExistent.text()); }); @@ -305,11 +305,11 @@ Deno.test("text", async () => { Deno.test("maybeText", async () => { await withTempDir(async () => { - const file = createPathReference("file.txt"); + const file = createPathRef("file.txt"); file.writeTextSync("asdf"); assertEquals(file.maybeTextSync(), "asdf"); assertEquals(await file.maybeText(), "asdf"); - const nonExistent = createPathReference("not-exists"); + const nonExistent = createPathRef("not-exists"); assertEquals(await nonExistent.maybeText(), undefined); assertEquals(nonExistent.maybeTextSync(), undefined); }); @@ -317,7 +317,7 @@ Deno.test("maybeText", async () => { Deno.test("json", async () => { await withTempDir(async () => { - const file = createPathReference("file.txt"); + const file = createPathRef("file.txt"); file.writeJsonSync({ test: 123 }); let data = file.jsonSync(); assertEquals(data, { test: 123 }); @@ -328,13 +328,13 @@ Deno.test("json", async () => { Deno.test("maybeJson", async () => { await withTempDir(async () => { - const file = createPathReference("file.json"); + const file = createPathRef("file.json"); file.writeJsonSync({ test: 123 }); let data = file.maybeJsonSync(); assertEquals(data, { test: 123 }); data = await file.maybeJson(); assertEquals(data, { test: 123 }); - const nonExistent = createPathReference("not-exists"); + const nonExistent = createPathRef("not-exists"); data = nonExistent.maybeJsonSync(); assertEquals(data, undefined); data = await nonExistent.maybeJson(); @@ -348,7 +348,7 @@ Deno.test("maybeJson", async () => { Deno.test("writeJson", async () => { await withTempDir(async () => { - const path = createPathReference("file.json"); + const path = createPathRef("file.json"); await path.writeJson({ prop: "test", }); @@ -373,7 +373,7 @@ Deno.test("writeJson", async () => { Deno.test("writeJsonPretty", async () => { await withTempDir(async () => { - const path = createPathReference("file.json"); + const path = createPathRef("file.json"); await path.writeJsonPretty({ prop: "test", }); @@ -398,7 +398,7 @@ Deno.test("writeJsonPretty", async () => { Deno.test("create", async () => { await withTempDir(async () => { - const path = createPathReference("file.txt").writeTextSync("text"); + const path = createPathRef("file.txt").writeTextSync("text"); let file = await path.create(); file.writeTextSync("asdf"); file.close(); @@ -416,7 +416,7 @@ Deno.test("create", async () => { Deno.test("createNew", async () => { await withTempDir(async () => { - const path = createPathReference("file.txt").writeTextSync("text"); + const path = createPathRef("file.txt").writeTextSync("text"); await assertRejects(() => path.createNew()); path.removeSync(); let file = await path.createNew(); @@ -430,7 +430,7 @@ Deno.test("createNew", async () => { Deno.test("open", async () => { await withTempDir(async () => { - const path = createPathReference("file.txt").writeTextSync("text"); + const path = createPathRef("file.txt").writeTextSync("text"); let file = await path.open({ write: true }); await file.writeText("1"); file.writeTextSync("2"); @@ -444,7 +444,7 @@ Deno.test("open", async () => { Deno.test("remove", async () => { await withTempDir(async () => { - const path = createPathReference("file.txt").writeTextSync("text"); + const path = createPathRef("file.txt").writeTextSync("text"); assert(path.existsSync()); assert(!path.removeSync().existsSync()); path.writeTextSync("asdf"); @@ -455,7 +455,7 @@ Deno.test("remove", async () => { Deno.test("copyFile", async () => { await withTempDir(async () => { - const path = createPathReference("file.txt").writeTextSync("text"); + const path = createPathRef("file.txt").writeTextSync("text"); const newPath = await path.copyFile("other.txt"); assert(path.existsSync()); assert(newPath.existsSync()); @@ -468,7 +468,7 @@ Deno.test("copyFile", async () => { Deno.test("rename", async () => { await withTempDir(async () => { - const path = createPathReference("file.txt").writeTextSync(""); + const path = createPathRef("file.txt").writeTextSync(""); const newPath = path.renameSync("other.txt"); assert(!path.existsSync()); assert(newPath.existsSync()); @@ -482,7 +482,7 @@ Deno.test("rename", async () => { Deno.test("pipeTo", async () => { await withTempDir(async () => { const largeText = "asdf".repeat(100_000); - const textFile = createPathReference("file.txt").writeTextSync(largeText); + const textFile = createPathRef("file.txt").writeTextSync(largeText); const otherFilePath = textFile.parentOrThrow().join("other.txt"); const otherFile = otherFilePath.openSync({ write: true, create: true }); await textFile.pipeTo(otherFile.writable); diff --git a/src/path.ts b/src/path.ts index 13ff426..718367a 100644 --- a/src/path.ts +++ b/src/path.ts @@ -2,14 +2,14 @@ import { path as stdPath, writeAll, writeAllSync } from "./deps.ts"; const PERIOD_CHAR_CODE = ".".charCodeAt(0); -export function createPathReference(path: string | URL): PathReference { - return new PathReference(path); +export function createPathRef(path: string | URL): PathRef { + return new PathRef(path); } /** * Holds a reference to a path providing helper methods. */ -export class PathReference { +export class PathRef { readonly #path: string; constructor(path: string | URL) { @@ -22,13 +22,13 @@ export class PathReference { } /** Joins the provided path segments onto this path. */ - join(...pathSegments: string[]): PathReference { - return new PathReference(stdPath.join(this.#path, ...pathSegments)); + join(...pathSegments: string[]): PathRef { + return new PathRef(stdPath.join(this.#path, ...pathSegments)); } /** Resolves this path to an absolute path along with the provided path segments. */ - resolve(...pathSegments: string[]): PathReference { - return new PathReference(stdPath.resolve(this.#path, ...pathSegments)); + resolve(...pathSegments: string[]): PathRef { + return new PathRef(stdPath.resolve(this.#path, ...pathSegments)); } /** @@ -36,8 +36,8 @@ export class PathReference { * 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(): PathReference { - return new PathReference(stdPath.normalize(this.#path)); + normalize(): PathRef { + return new PathRef(stdPath.normalize(this.#path)); } /** Follows symlinks and gets if this path is a directory. */ @@ -122,7 +122,7 @@ export class PathReference { /** * Gets the directory path. In most cases, it is recommended - * to use `.parent()` instead since it will give you a `PathReference`. + * to use `.parent()` instead since it will give you a `PathRef`. */ dirname(): string { return stdPath.dirname(this.#path); @@ -134,7 +134,7 @@ export class PathReference { } /** Resolves the path getting all its ancestor directories in order. */ - *ancestors(): Generator { + *ancestors(): Generator { let ancestor = this.parent(); while (ancestor != null) { yield ancestor; @@ -143,18 +143,18 @@ export class PathReference { } /** Gets the parent directory or returns undefined if the parent is the root directory. */ - parent(): PathReference | undefined { + parent(): PathRef | undefined { const resolvedPath = this.resolve(); const dirname = resolvedPath.dirname(); if (dirname === resolvedPath.#path) { return undefined; } else { - return new PathReference(dirname); + return new PathRef(dirname); } } /** Gets the parent or throws if the current directory was the root. */ - parentOrThrow(): PathReference { + parentOrThrow(): PathRef { const parent = this.parent(); if (parent == null) { throw new Error(`Cannot get the parent directory of '${this.#path}'.`); @@ -172,23 +172,23 @@ export class PathReference { } /** Gets a new path reference with the provided extension. */ - withExtname(ext: string): PathReference { + withExtname(ext: string): PathRef { const currentExt = this.extname(); const hasLeadingPeriod = ext.charCodeAt(0) === PERIOD_CHAR_CODE; if (!hasLeadingPeriod) { ext = "." + ext; } - return new PathReference(this.#path.substring(0, this.#path.length - (currentExt?.length ?? 0)) + ext); + return new PathRef(this.#path.substring(0, this.#path.length - (currentExt?.length ?? 0)) + ext); } /** Gets a new path reference with the provided file or directory name. */ - withBasename(basename: string): PathReference { + withBasename(basename: string): PathRef { const currentBaseName = this.basename(); - return new PathReference(this.#path.substring(0, this.#path.length - currentBaseName.length) + basename); + return new PathRef(this.#path.substring(0, this.#path.length - currentBaseName.length) + basename); } /** Gets the relative path from this path to the specified path. */ - relative(to: string | URL | PathReference): string { + relative(to: string | URL | PathRef): string { const toPathRef = ensurePathRef(to); console.log(this.resolve().#path, toPathRef.resolve().#path); console.log(stdPath.relative(this.resolve().#path, toPathRef.resolve().#path)); @@ -206,13 +206,13 @@ export class PathReference { } /** Resolves to the absolute normalized path, with symbolic links resolved. */ - realPath(): Promise { - return Deno.realPath(this.#path).then((path) => new PathReference(path)); + realPath(): Promise { + return Deno.realPath(this.#path).then((path) => new PathRef(path)); } /** Synchronously resolves to the absolute normalized path, with symbolic links resolved. */ - realPathSync(): PathReference { - return new PathReference(Deno.realPathSync(this.#path)); + realPathSync(): PathRef { + return new PathRef(Deno.realPathSync(this.#path)); } /** Creates a directory at this path. */ @@ -231,9 +231,9 @@ export class PathReference { * Creates a symlink at the provided path to the provided target returning the target path. */ async createAbsoluteSymlinkTo( - target: string | URL | PathReference, + target: string | URL | PathRef, opts?: Deno.SymlinkOptions, - ): Promise { + ): Promise { const from = this.resolve(); const to = ensurePathRef(target).resolve(); await createSymlink({ @@ -249,7 +249,7 @@ export class PathReference { * Synchronously creates a symlink at the provided path to the provided target returning the target path. * @returns The resolved target path. */ - createAbsoluteSymlinkToSync(target: string | URL | PathReference, opts?: Deno.SymlinkOptions): PathReference { + createAbsoluteSymlinkToSync(target: string | URL | PathRef, opts?: Deno.SymlinkOptions): PathRef { const from = this.resolve(); const to = ensurePathRef(target).resolve(); createSymlinkSync({ @@ -268,9 +268,9 @@ export class PathReference { * @returns The destination path. */ async createAbsoluteSymlinkAt( - linkPath: string | URL | PathReference, + linkPath: string | URL | PathRef, opts?: Deno.SymlinkOptions, - ): Promise { + ): Promise { const linkPathRef = ensurePathRef(linkPath).resolve(); const thisResolved = this.resolve(); await createSymlink({ @@ -289,9 +289,9 @@ export class PathReference { * @returns The destination path. */ createAbsoluteSymlinkAtSync( - linkPath: string | URL | PathReference, + linkPath: string | URL | PathRef, opts?: Deno.SymlinkOptions, - ): PathReference { + ): PathRef { const linkPathRef = ensurePathRef(linkPath).resolve(); const thisResolved = this.resolve(); createSymlinkSync({ @@ -310,9 +310,9 @@ export class PathReference { * @returns The destination path. */ async createRelativeSymlinkAt( - linkPath: string | URL | PathReference, + linkPath: string | URL | PathRef, opts?: Deno.SymlinkOptions, - ): Promise { + ): Promise { const { linkPathRef, thisResolved, @@ -334,9 +334,9 @@ export class PathReference { * @returns The destination path. */ createRelativeSymlinkAtSync( - linkPath: string | URL | PathReference, + linkPath: string | URL | PathRef, opts?: Deno.SymlinkOptions, - ): PathReference { + ): PathRef { const { linkPathRef, thisResolved, @@ -351,7 +351,7 @@ export class PathReference { return linkPathRef; } - #getRelativeSymlinkAtParts(linkPath: string | URL | PathReference) { + #getRelativeSymlinkAtParts(linkPath: string | URL | PathRef) { const linkPathRef = ensurePathRef(linkPath).resolve(); const thisResolved = this.resolve(); let relativePath: string; @@ -619,7 +619,7 @@ export class PathReference { * Copies the file returning a promise that resolves to * the destination path. */ - copyFile(destinationPath: string | URL | PathReference): Promise { + copyFile(destinationPath: string | URL | PathRef): Promise { const pathRef = ensurePathRef(destinationPath); return Deno.copyFile(this.#path, pathRef.#path) .then(() => pathRef); @@ -629,7 +629,7 @@ export class PathReference { * Copies the file returning a promise that resolves to * the destination path synchronously. */ - copyFileSync(destinationPath: string | URL | PathReference): PathReference { + copyFileSync(destinationPath: string | URL | PathRef): PathRef { const pathRef = ensurePathRef(destinationPath); Deno.copyFileSync(this.#path, pathRef.#path); return pathRef; @@ -639,7 +639,7 @@ export class PathReference { * Renames the file or directory returning a promise that resolves to * the renamed path. */ - rename(newPath: string | URL | PathReference): Promise { + rename(newPath: string | URL | PathRef): Promise { const pathRef = ensurePathRef(newPath); return Deno.rename(this.#path, pathRef.#path).then(() => pathRef); } @@ -648,7 +648,7 @@ export class PathReference { * Renames the file or directory returning a promise that resolves to * the renamed path synchronously. */ - renameSync(newPath: string | URL | PathReference): PathReference { + renameSync(newPath: string | URL | PathRef): PathRef { const pathRef = ensurePathRef(newPath); Deno.renameSync(this.#path, pathRef.#path); return pathRef; @@ -670,13 +670,13 @@ export class PathReference { } } -function ensurePathRef(path: string | URL | PathReference) { - return path instanceof PathReference ? path : new PathReference(path); +function ensurePathRef(path: string | URL | PathRef) { + return path instanceof PathRef ? path : new PathRef(path); } async function createSymlink(opts: { - fromPath: PathReference; - toPath: PathReference; + fromPath: PathRef; + toPath: PathRef; text: string; type: "file" | "dir" | undefined; }) { @@ -705,8 +705,8 @@ async function createSymlink(opts: { } function createSymlinkSync(opts: { - fromPath: PathReference; - toPath: PathReference; + fromPath: PathRef; + toPath: PathRef; text: string; type: "file" | "dir" | undefined; }) { diff --git a/src/request.ts b/src/request.ts index d68ef9e..14980d2 100644 --- a/src/request.ts +++ b/src/request.ts @@ -1,6 +1,6 @@ import { Delay, delayToMs, filterEmptyRecordValues, getFileNameFromUrl } from "./common.ts"; import { ProgressBar } from "./console/mod.ts"; -import { PathReference } from "./path.ts"; +import { PathRef } from "./path.ts"; interface RequestBuilderState { noThrow: boolean | number[]; @@ -299,7 +299,7 @@ export class RequestBuilder implements PromiseLike { * * @returns The path reference of the downloaded file. */ - async pipeToPath(options?: Deno.WriteFileOptions): Promise; + async pipeToPath(options?: Deno.WriteFileOptions): Promise; /** * Pipes the response body to a file. * @@ -309,11 +309,11 @@ export class RequestBuilder implements PromiseLike { * @returns The path reference of the downloaded file. */ async pipeToPath( - path?: string | URL | PathReference | undefined, + path?: string | URL | PathRef | undefined, options?: Deno.WriteFileOptions, - ): Promise; + ): Promise; async pipeToPath( - filePathOrOptions?: string | URL | PathReference | Deno.WriteFileOptions, + filePathOrOptions?: string | URL | PathRef | Deno.WriteFileOptions, maybeOptions?: Deno.WriteFileOptions, ) { // Do not derive from the response url because that could cause the server @@ -513,7 +513,7 @@ export class RequestResult { * * @returns The path reference of the downloaded file */ - async pipeToPath(options?: Deno.WriteFileOptions): Promise; + async pipeToPath(options?: Deno.WriteFileOptions): Promise; /** * Pipes the response body to a file. * @@ -526,11 +526,11 @@ export class RequestResult { * @returns The path reference of the downloaded file */ async pipeToPath( - path?: string | URL | PathReference | undefined, + path?: string | URL | PathRef | undefined, options?: Deno.WriteFileOptions, - ): Promise; + ): Promise; async pipeToPath( - filePathOrOptions?: string | URL | PathReference | Deno.WriteFileOptions, + filePathOrOptions?: string | URL | PathRef | Deno.WriteFileOptions, maybeOptions?: Deno.WriteFileOptions, ) { // resolve the file path using the original url because it would be a security issue @@ -648,16 +648,16 @@ export async function makeRequest(state: RequestBuilderState) { } function resolvePipeToPathParams( - pathOrOptions: string | URL | PathReference | Deno.WriteFileOptions | undefined, + pathOrOptions: string | URL | PathRef | Deno.WriteFileOptions | undefined, maybeOptions: Deno.WriteFileOptions | undefined, originalUrl: string | URL | undefined, ) { - let filePath: PathReference | undefined; + let filePath: PathRef | undefined; let options: Deno.WriteFileOptions | undefined; if (typeof pathOrOptions === "string" || pathOrOptions instanceof URL) { - filePath = new PathReference(pathOrOptions).resolve(); + filePath = new PathRef(pathOrOptions).resolve(); options = maybeOptions; - } else if (pathOrOptions instanceof PathReference) { + } else if (pathOrOptions instanceof PathRef) { filePath = pathOrOptions.resolve(); options = maybeOptions; } else if (typeof pathOrOptions === "object") { @@ -666,7 +666,7 @@ function resolvePipeToPathParams( options = maybeOptions; } if (filePath === undefined) { - filePath = new PathReference(getFileNameFromUrlOrThrow(originalUrl)); + filePath = new PathRef(getFileNameFromUrlOrThrow(originalUrl)); } else if (filePath.isDir()) { filePath = filePath.join(getFileNameFromUrlOrThrow(originalUrl)); }