From e294af945cf44baa6c755268ee9d2c970d722750 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Mon, 3 Jul 2023 20:46:33 -0400 Subject: [PATCH] feat: PathRef.prototype.copyFileToDir (#154) --- src/path.test.ts | 18 ++++++++++++++++++ src/path.ts | 28 ++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/path.test.ts b/src/path.test.ts index 6c39920..48ec764 100644 --- a/src/path.test.ts +++ b/src/path.test.ts @@ -677,6 +677,24 @@ Deno.test("copyFile", async () => { }); }); +Deno.test("copyFileToDir", async () => { + await withTempDir(async () => { + const path = createPathRef("file.txt") + .writeTextSync("text"); + const dir = createPathRef("dir").mkdirSync(); + const newPath = await path.copyFileToDir(dir); + assert(path.existsSync()); + assert(newPath.existsSync()); + assertEquals(dir.join("file.txt").toString(), newPath.toString()); + assertEquals(newPath.readTextSync(), "text"); + const dir2 = createPathRef("dir2").mkdirSync(); + const newPath2 = path.copyFileToDir(dir2); + assert(newPath2.existsSync()); + assertEquals(newPath2.readTextSync(), "text"); + assertEquals(newPath2.toString(), dir2.join("file.txt").toString()); + }); +}); + Deno.test("rename", async () => { await withTempDir(async () => { const path = createPathRef("file.txt").writeTextSync(""); diff --git a/src/path.ts b/src/path.ts index bf85440..ee4b33c 100644 --- a/src/path.ts +++ b/src/path.ts @@ -802,8 +802,8 @@ export class PathRef { } /** - * Copies the file returning a promise that resolves to - * the destination path. + * Copies the file to the specified destination path. + * @returns The destination file path. */ copyFile(destinationPath: string | URL | PathRef): Promise { const pathRef = ensurePathRef(destinationPath); @@ -812,8 +812,8 @@ export class PathRef { } /** - * Copies the file returning a promise that resolves to - * the destination path synchronously. + * Copies the file to the destination path synchronously. + * @returns The destination file path. */ copyFileSync(destinationPath: string | URL | PathRef): PathRef { const pathRef = ensurePathRef(destinationPath); @@ -821,6 +821,26 @@ export class PathRef { return pathRef; } + /** + * Copies the file to the specified directory. + * @returns The destination file path. + */ + copyFileToDir(destinationDirPath: string | URL | PathRef): PathRef { + const destinationPath = ensurePathRef(destinationDirPath) + .join(this.basename()); + return this.copyFileSync(destinationPath); + } + + /** + * Copies the file to the specified directory synchronously. + * @returns The destination file path. + */ + copyFileToDirSync(destinationDirPath: string | URL | PathRef): PathRef { + const destinationPath = ensurePathRef(destinationDirPath) + .join(this.basename()); + return this.copyFileSync(destinationPath); + } + /** * Renames the file or directory returning a promise that resolves to * the renamed path.