mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 09:31:22 -05:00
add copyFile & copyFileSync to std/node/fs (#4726)
This commit is contained in:
parent
6e0c9a0c32
commit
e23f33de7b
3 changed files with 68 additions and 0 deletions
32
std/node/_fs/_fs_copy.ts
Normal file
32
std/node/_fs/_fs_copy.ts
Normal file
|
@ -0,0 +1,32 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
import { CallbackWithError } from "./_fs_common.ts";
|
||||
|
||||
export function copyFile(
|
||||
source: string,
|
||||
destination: string,
|
||||
callback: CallbackWithError
|
||||
): void {
|
||||
new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
await Deno.copyFile(source, destination);
|
||||
resolve();
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
})
|
||||
.then(() => {
|
||||
callback();
|
||||
})
|
||||
.catch((err) => {
|
||||
callback(err);
|
||||
});
|
||||
}
|
||||
|
||||
export function copyFileSync(source: string, destination: string): void {
|
||||
try {
|
||||
Deno.copyFileSync(source, destination);
|
||||
} catch (err) {
|
||||
throw err;
|
||||
}
|
||||
}
|
33
std/node/_fs/_fs_copy_test.ts
Normal file
33
std/node/_fs/_fs_copy_test.ts
Normal file
|
@ -0,0 +1,33 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
import { copyFile, copyFileSync } from "./_fs_copy.ts";
|
||||
import { existsSync } from "./_fs_exists.ts";
|
||||
|
||||
import { assert } from "../../testing/asserts.ts";
|
||||
const { test } = Deno;
|
||||
|
||||
const destFile = "./destination.txt";
|
||||
|
||||
test({
|
||||
name: "[std/node/fs] copy file",
|
||||
fn: async () => {
|
||||
const srouceFile = Deno.makeTempFileSync();
|
||||
const err = await new Promise((resolve) => {
|
||||
copyFile(srouceFile, destFile, (err: Error | undefined) => resolve(err));
|
||||
});
|
||||
assert(!err);
|
||||
assert(existsSync(destFile));
|
||||
Deno.removeSync(srouceFile);
|
||||
Deno.removeSync(destFile);
|
||||
},
|
||||
});
|
||||
|
||||
test({
|
||||
name: "[std/node/fs] copy file sync",
|
||||
fn: () => {
|
||||
const srouceFile = Deno.makeTempFileSync();
|
||||
copyFileSync(srouceFile, destFile);
|
||||
assert(existsSync(destFile));
|
||||
Deno.removeSync(srouceFile);
|
||||
Deno.removeSync(destFile);
|
||||
},
|
||||
});
|
|
@ -10,6 +10,7 @@ import { readFile, readFileSync } from "./_fs/_fs_readFile.ts";
|
|||
import { readlink, readlinkSync } from "./_fs/_fs_readlink.ts";
|
||||
import { exists, existsSync } from "./_fs/_fs_exists.ts";
|
||||
import { mkdir, mkdirSync } from "./_fs/_fs_mkdir.ts";
|
||||
import { copyFile, copyFileSync } from "./_fs/_fs_copy.ts";
|
||||
|
||||
export {
|
||||
access,
|
||||
|
@ -23,6 +24,8 @@ export {
|
|||
close,
|
||||
closeSync,
|
||||
constants,
|
||||
copyFile,
|
||||
copyFileSync,
|
||||
exists,
|
||||
existsSync,
|
||||
readFile,
|
||||
|
|
Loading…
Add table
Reference in a new issue