summaryrefslogtreecommitdiff
path: root/std/node/_fs/_fs_exists.ts
diff options
context:
space:
mode:
Diffstat (limited to 'std/node/_fs/_fs_exists.ts')
-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;
+ }
+}