Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: PathRef.prototype.copyFileToDir #154

Merged
merged 1 commit into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #155 to fix

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