summaryrefslogtreecommitdiff
path: root/std/node/_fs/_fs_exists.ts
blob: e6e6e4f2e6135bd1a1ef4a04fd1073077c60b8b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { fromFileUrl } from "../path.ts";

type ExitsCallback = (exists: boolean) => void;

/**
 * TODO: Also accept 'path' parameter as a Node polyfill Buffer type once these
 * are implemented. See https://github.com/denoland/deno/issues/3403
 * Deprecated in node api
 */
export function exists(path: string | URL, callback: ExitsCallback): void {
  path = path instanceof URL ? fromFileUrl(path) : path;
  Deno.lstat(path)
    .then(() => {
      callback(true);
    })
    .catch(() => callback(false));
}

/**
 * TODO: Also accept 'path' parameter as a Node polyfill Buffer or URL type once these
 * are implemented. See https://github.com/denoland/deno/issues/3403
 */
export function existsSync(path: string | URL): boolean {
  path = path instanceof URL ? fromFileUrl(path) : path;
  try {
    Deno.lstatSync(path);
    return true;
  } catch (err) {
    if (err instanceof Deno.errors.NotFound) {
      return false;
    }
    throw err;
  }
}