mirror of
https://github.com/denoland/deno.git
synced 2024-12-23 07:44:48 -05:00
Add exists and existsSync to std/node (#4655)
This commit is contained in:
parent
f5d505332e
commit
47a580293e
2 changed files with 37 additions and 0 deletions
34
std/node/_fs/_fs_exists.ts
Normal file
34
std/node/_fs/_fs_exists.ts
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -8,6 +8,7 @@ import { close, closeSync } from "./_fs/_fs_close.ts";
|
|||
import * as constants from "./_fs/_fs_constants.ts";
|
||||
import { readFile, readFileSync } from "./_fs/_fs_readFile.ts";
|
||||
import { readlink, readlinkSync } from "./_fs/_fs_readlink.ts";
|
||||
import { exists, existsSync } from "./_fs/_fs_exists.ts";
|
||||
|
||||
export {
|
||||
access,
|
||||
|
@ -25,4 +26,6 @@ export {
|
|||
readFileSync,
|
||||
readlink,
|
||||
readlinkSync,
|
||||
exists,
|
||||
existsSync,
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue