From 47a580293eb5c176497264f1c3f108bf6b2c480d Mon Sep 17 00:00:00 2001 From: Ali Hasani Date: Tue, 7 Apr 2020 08:13:14 +0430 Subject: Add exists and existsSync to std/node (#4655) --- std/node/_fs/_fs_exists.ts | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 std/node/_fs/_fs_exists.ts (limited to 'std/node/_fs') 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; + } +} -- cgit v1.2.3