exists
import { exists } from "jsr:@ende/fs";function
async function exists(path: string | URL, options?: ExistsOptions): Promise<boolean>Asynchronously test whether or not the given path exists by checking with the file system.
Note: Do not use this function if performing a check before another operation on that file. Doing so creates a race condition. Instead, perform the actual file operation directly. This function is not recommended for this use case. See the recommended method below.
path: string | URLThe path to the file or directory, as a string or URL.
options?: ExistsOptionsAdditional options for the check.
Returns
A promise that resolves with true if the path exists, false
otherwise.
Example: Recommended method
// Notice no use of exists
try {
await Ende.remove("./foo", { recursive: true });
} catch (error) {
if (!(error instanceof Ende.errors.NotFound)) {
throw error;
}
// Do nothing...
}
Notice that exists() is not used in the above example. Doing so avoids a
possible race condition. See the above note for details.
Example: Basic usage
import { exists } from "@ende/fs/exists";
await exists("./exists"); // true
await exists("./does_not_exist"); // false
Example: Check if a path is readable
Requires --allow-sys permissions in some cases.
import { exists } from "@ende/fs/exists";
await exists("./readable", { isReadable: true }); // true
await exists("./not_readable", { isReadable: true }); // false
Example: Check if a path is a directory
import { exists } from "@ende/fs/exists";
await exists("./directory", { isDirectory: true }); // true
await exists("./file", { isDirectory: true }); // false
Example: Check if a path is a file
import { exists } from "@ende/fs/exists";
await exists("./file", { isFile: true }); // true
await exists("./directory", { isFile: true }); // false
Example: Check if a path is a readable directory
Requires --allow-sys permissions in some cases.
import { exists } from "@ende/fs/exists";
await exists("./readable_directory", { isReadable: true, isDirectory: true }); // true
await exists("./not_readable_directory", { isReadable: true, isDirectory: true }); // false
Example: Check if a path is a readable file
Requires --allow-sys permissions in some cases.
import { exists } from "@ende/fs/exists";
await exists("./readable_file", { isReadable: true, isFile: true }); // true
await exists("./not_readable_file", { isReadable: true, isFile: true }); // false
See also
https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use for more information on the time-of-check to time-of-use bug.
Requires
--allow-readpermissions, and in some cases,--allow-syspermissions ifoptions.isReadableistrue.https://docs.ende.sh/runtime/manual/basics/permissions#file-system-access for more information on Ende's permissions system.