diff options
-rw-r--r-- | std/node/_fs/_fs_exists.ts | 34 | ||||
-rwxr-xr-x | std/node/fs.ts | 3 |
2 files changed, 37 insertions, 0 deletions
diff --git a/std/node/_fs/_fs_exists.ts b/std/node/_fs/_fs_exists.ts new file mode 100644 index 000000000..cc4f65736 --- /dev/null +++ b/std/node/_fs/_fs_exists.ts @@ -0,0 +1,34 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +type ExitsCallback = (exists: boolean) => void; + +/* Deprecated in node api */ + +export function exists(path: string, callback: ExitsCallback): void { + new Promise(async (resolve, reject) => { + try { + await Deno.lstat(path); + resolve(); + } catch (err) { + reject(err); + } + }) + .then(() => { + callback(true); + }) + .catch(() => { + callback(false); + }); +} + +export function existsSync(path: string): boolean { + try { + Deno.lstatSync(path); + return true; + } catch (err) { + if (err instanceof Deno.errors.NotFound) { + return false; + } + throw err; + } +} diff --git a/std/node/fs.ts b/std/node/fs.ts index 0681ee5a1..fc65a2637 100755 --- a/std/node/fs.ts +++ b/std/node/fs.ts @@ -8,6 +8,7 @@ import { close, closeSync } from "./_fs/_fs_close.ts"; import * as constants from "./_fs/_fs_constants.ts"; import { readFile, readFileSync } from "./_fs/_fs_readFile.ts"; import { readlink, readlinkSync } from "./_fs/_fs_readlink.ts"; +import { exists, existsSync } from "./_fs/_fs_exists.ts"; export { access, @@ -25,4 +26,6 @@ export { readFileSync, readlink, readlinkSync, + exists, + existsSync, }; |