existsSync
import { existsSync } from "jsr:@ende/fs";function
function existsSync(path: string | URL, options?: ExistsOptions): booleanSynchronously 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
true if the path exists, false otherwise.
Example: Recommended method
// Notice no use of exists
try {
Ende.removeSync("./foo", { recursive: true });
} catch (error) {
if (!(error instanceof Ende.errors.NotFound)) {
throw error;
}
// Do nothing...
}
Notice that existsSync() is not used in the above example. Doing so avoids
a possible race condition. See the above note for details.
Example: Basic usage
import { existsSync } from "@ende/fs/exists";
existsSync("./exists"); // true
existsSync("./does_not_exist"); // false
Example: Check if a path is readable
Requires --allow-sys permissions in some cases.
import { existsSync } from "@ende/fs/exists";
existsSync("./readable", { isReadable: true }); // true
existsSync("./not_readable", { isReadable: true }); // false
Example: Check if a path is a directory
import { existsSync } from "@ende/fs/exists";
existsSync("./directory", { isDirectory: true }); // true
existsSync("./file", { isDirectory: true }); // false
Example: Check if a path is a file
import { existsSync } from "@ende/fs/exists";
existsSync("./file", { isFile: true }); // true
existsSync("./directory", { isFile: true }); // false
Example: Check if a path is a readable directory
Requires --allow-sys permissions in some cases.
import { existsSync } from "@ende/fs/exists";
existsSync("./readable_directory", { isReadable: true, isDirectory: true }); // true
existsSync("./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 { existsSync } from "@ende/fs/exists";
existsSync("./readable_file", { isReadable: true, isFile: true }); // true
existsSync("./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.