Skip to content

Commit

Permalink
feat: PathRef.prototype.copyFileToDir (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Jul 4, 2023
1 parent 7b5b33e commit e294af9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
18 changes: 18 additions & 0 deletions src/path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
Expand Down
28 changes: 24 additions & 4 deletions src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathRef> {
const pathRef = ensurePathRef(destinationPath);
Expand All @@ -812,15 +812,35 @@ 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);
Deno.copyFileSync(this.#path, pathRef.#path);
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);

This comment has been minimized.

Copy link
@dsherret

dsherret Jul 4, 2023

Author Owner

Oops

}

/**
* 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.
Expand Down

0 comments on commit e294af9

Please sign in to comment.