Add exists and existsSync to std/node (#4655)

This commit is contained in:
Ali Hasani 2020-04-07 08:13:14 +04:30 committed by GitHub
parent f5d505332e
commit 47a580293e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 0 deletions

View 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;
}
}

View File

@ -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,
};