forked from denoland/std
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add copyFile & copyFileSync to node/fs (denoland/deno#4726)
- Loading branch information
1 parent
179e123
commit cae381e
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters