summaryrefslogtreecommitdiff
path: root/std/node/_fs
diff options
context:
space:
mode:
authorAli Hasani <a.hassssani@gmail.com>2020-04-07 08:13:14 +0430
committerGitHub <noreply@github.com>2020-04-06 23:43:14 -0400
commit47a580293eb5c176497264f1c3f108bf6b2c480d (patch)
treee874b58f940a27d412fcc6c51d03ef78b4ff575a /std/node/_fs
parentf5d505332e862b21e650324e7013c8f6f27e06c4 (diff)
Add exists and existsSync to std/node (#4655)
Diffstat (limited to 'std/node/_fs')
-rw-r--r--std/node/_fs/_fs_exists.ts34
1 files changed, 34 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;
+ }
+}