1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-25 08:39:09 -05:00
denoland-deno/std/node/_fs/_fs_exists.ts

32 lines
868 B
TypeScript

// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
type ExitsCallback = (exists: boolean) => void;
/**
* 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
* Deprecated in node api
*/
export function exists(path: string, callback: ExitsCallback): void {
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): boolean {
try {
Deno.lstatSync(path);
return true;
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
return false;
}
throw err;
}
}