summaryrefslogtreecommitdiff
path: root/std/node/_fs
diff options
context:
space:
mode:
authorAli Hasani <a.hassssani@gmail.com>2020-04-12 23:04:16 +0430
committerGitHub <noreply@github.com>2020-04-12 14:34:16 -0400
commite23f33de7bdf2a20aa72ad222dfbea75c7332a7c (patch)
tree1d1ccd647d274eab44a44fd8ee70d2c8571c6fae /std/node/_fs
parent6e0c9a0c32ebdbe66301a11f106ba848ee96b8bd (diff)
add copyFile & copyFileSync to std/node/fs (#4726)
Diffstat (limited to 'std/node/_fs')
-rw-r--r--std/node/_fs/_fs_copy.ts32
-rw-r--r--std/node/_fs/_fs_copy_test.ts33
2 files changed, 65 insertions, 0 deletions
diff --git a/std/node/_fs/_fs_copy.ts b/std/node/_fs/_fs_copy.ts
new file mode 100644
index 000000000..4fdc63008
--- /dev/null
+++ b/std/node/_fs/_fs_copy.ts
@@ -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;
+ }
+}
diff --git a/std/node/_fs/_fs_copy_test.ts b/std/node/_fs/_fs_copy_test.ts
new file mode 100644
index 000000000..45e7e9372
--- /dev/null
+++ b/std/node/_fs/_fs_copy_test.ts
@@ -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);
+ },
+});