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(path): copyFile -> copy and support copying directories #260

Merged
merged 1 commit into from
Apr 6, 2024
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
26 changes: 20 additions & 6 deletions src/path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -810,31 +810,45 @@ Deno.test("ensureFile", async () => {
});
});

Deno.test("copyFile", async () => {
Deno.test("copy", async () => {
// file
await withTempDir(async () => {
const path = createPath("file.txt").writeTextSync("text");
const newPath = await path.copyFile("other.txt");
const newPath = await path.copy("other.txt");
assert(path.existsSync());
assert(newPath.existsSync());
assertEquals(newPath.readTextSync(), "text");
const newPath2 = path.copyFileSync("other2.txt");
const newPath2 = path.copySync("other2.txt");
assert(newPath2.existsSync());
assertEquals(newPath2.readTextSync(), "text");
});
// directory
await withTempDir(async () => {
const dir = createPath("dir").mkdirSync();
dir.join("file.txt").writeTextSync("text");
const dir2 = createPath("dir2");
await dir.copy(dir2);
assertEquals(dir2.join("file.txt").readTextSync(), "text");
await assertRejects(() => dir.copy(dir2));
assertEquals(dir2.join("file.txt").readTextSync(), "text");
dir.join("file.txt").writeTextSync("text2");
await dir.copy(dir2, { overwrite: true });
assertEquals(dir2.join("file.txt").readTextSync(), "text2");
});
});

Deno.test("copyFileToDir", async () => {
Deno.test("copyToDir", async () => {
await withTempDir(async () => {
const path = createPath("file.txt")
.writeTextSync("text");
const dir = createPath("dir").mkdirSync();
const newPath = await path.copyFileToDir(dir);
const newPath = await path.copyToDir(dir);
assert(path.existsSync());
assert(newPath.existsSync());
assertEquals(dir.join("file.txt").toString(), newPath.toString());
assertEquals(newPath.readTextSync(), "text");
const dir2 = createPath("dir2").mkdirSync();
const newPath2 = path.copyFileToDirSync(dir2);
const newPath2 = path.copyToDirSync(dir2);
assert(newPath2.existsSync());
assertEquals(newPath2.readTextSync(), "text");
assertEquals(newPath2.toString(), dir2.join("file.txt").toString());
Expand Down
29 changes: 14 additions & 15 deletions src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ensureFileSync,
expandGlob,
expandGlobSync,
fs,
path as stdPath,
walk,
walkSync,
Expand Down Expand Up @@ -1038,44 +1039,42 @@ export class Path {
return this;
}

/**
* Copies the file to the specified destination path.
/** Copies a file or directory to the provided destination.
* @returns The destination file path.
*/
copyFile(destinationPath: string | URL | Path): Promise<Path> {
async copy(destinationPath: string | URL | Path, options?: { overwrite?: boolean }): Promise<Path> {
const pathRef = ensurePath(destinationPath);
return Deno.copyFile(this.#path, pathRef.#path)
.then(() => pathRef);
await fs.copy(this.#path, pathRef.#path, options);
return pathRef;
}

/**
* Copies the file to the destination path synchronously.
/** Copies a file or directory to the provided destination synchronously.
* @returns The destination file path.
*/
copyFileSync(destinationPath: string | URL | Path): Path {
copySync(destinationPath: string | URL | Path, options?: { overwrite?: boolean }): Path {
const pathRef = ensurePath(destinationPath);
Deno.copyFileSync(this.#path, pathRef.#path);
fs.copySync(this.#path, pathRef.#path, options);
return pathRef;
}

/**
* Copies the file to the specified directory.
* Copies the file or directory to the specified directory.
* @returns The destination file path.
*/
copyFileToDir(destinationDirPath: string | URL | Path): Promise<Path> {
copyToDir(destinationDirPath: string | URL | Path, options?: { overwrite?: boolean }): Promise<Path> {
const destinationPath = ensurePath(destinationDirPath)
.join(this.basename());
return this.copyFile(destinationPath);
return this.copy(destinationPath, options);
}

/**
* Copies the file to the specified directory synchronously.
* Copies the file or directory to the specified directory synchronously.
* @returns The destination file path.
*/
copyFileToDirSync(destinationDirPath: string | URL | Path): Path {
copyToDirSync(destinationDirPath: string | URL | Path, options?: { overwrite?: boolean }): Path {
const destinationPath = ensurePath(destinationDirPath)
.join(this.basename());
return this.copyFileSync(destinationPath);
return this.copySync(destinationPath, options);
}

/**
Expand Down