Skip to content

Commit

Permalink
feat(path): copyFile -> copy and support copying directories (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Apr 6, 2024
1 parent 20ec3c2 commit 2e884cc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
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

0 comments on commit 2e884cc

Please sign in to comment.