Skip to content

Commit e97dd6c

Browse files
Update examples: file exists, folder exists (#1424)
1 parent c61fcd5 commit e97dd6c

File tree

3 files changed

+58
-26
lines changed

3 files changed

+58
-26
lines changed

examples/_data.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,11 @@ export const sidebar = [
658658
id: "/examples/checking_file_existence",
659659
type: "example",
660660
},
661+
{
662+
label: "Checking for directory existence",
663+
id: "/examples/checking_directory_existence",
664+
type: "example",
665+
},
661666
{
662667
label: "Moving/Renaming files",
663668
id: "/examples/moving_renaming_files",
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @title Checking for directory existence
3+
* @difficulty beginner
4+
* @tags cli, deploy
5+
* @run --allow-read --allow-write <url>
6+
* @group File System
7+
*
8+
* When creating directories it can be useful to first ensure that
9+
* such a directory doesn't already exist.
10+
* There are a number of ways to do this.
11+
*/
12+
13+
// Use the `exists` utility from the std library to check for existence of a file or folder.
14+
// Note: Can create a race condition if followed by file operation.
15+
// Consider the alternative below.
16+
import { exists } from "jsr:@std/fs/exists";
17+
await exists("./this_file_or_folder_exists"); // true
18+
await exists("./this_file_or_folder_does_not_exist"); // false
19+
20+
// We can also use this function to check if the item on a path is a file or a directory
21+
await exists("./directory", { isDirectory: true }); // true
22+
await exists("./file", { isDirectory: true }); // false
23+
24+
// Do not use the above function if performing a check directly before another operation on that folder.
25+
// Doing so creates a race condition. The `exists` function is not recommended for that usecase.
26+
// Consider this alternative which checks for existence of a folder without doing any other filesystem operations.
27+
try {
28+
await Deno.lstat("./example_directory");
29+
console.log("Folder exists");
30+
} catch (err) {
31+
if (!(err instanceof Deno.errors.NotFound)) {
32+
throw err;
33+
}
34+
console.log("Folder does not exist");
35+
}

examples/scripts/checking_file_existence.ts

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,30 @@
55
* @run --allow-read --allow-write <url>
66
* @group File System
77
*
8-
* Sometimes we as developers think that we need to check
9-
* if a file exists or not. More often than not, we are
10-
* entirely wrong.
8+
* When creating files it can be useful to first ensure that
9+
* such a file doesn't already exist.
10+
* There are a number of ways to do this.
1111
*/
1212

13-
// Let's say we wanted to create a folder if one doesn't
14-
// already exist. Logically it makes sense to first verify
15-
// that the folder exists, then try to create it right?
16-
// Wrong. This will create a race condition where if a folder
17-
// gets created in between when you check if the folder exists
18-
// and when you create a folder, your program will crash.
19-
// Instead, you should just create a folder and try to catch
20-
// errors like so.
21-
try {
22-
await Deno.mkdir("new_dir");
23-
} catch (err) {
24-
if (!(err instanceof Deno.errors.AlreadyExists)) {
25-
throw err;
26-
}
27-
}
13+
// Use the `exists` utility from the std library to check for existence of a file or folder.
14+
// Note: Can create a race condition if followed by file operation.
15+
// Consider the alternative below.
16+
import { exists } from "jsr:@std/fs/exists";
17+
await exists("./this_file_or_folder_exists"); // true
18+
await exists("./this_file_or_folder_does_not_exist"); // false
19+
20+
// We can also use this function to check if the item on a path is a file or a directory
21+
await exists("./file", { isFile: true }); // true
22+
await exists("./directory", { isFile: true }); // false
2823

29-
// This applies to almost every usecase. If you have a niche
30-
// usecase that requires you to check for existence of a file
31-
// without doing an filesystem operations other than that
32-
// (which is quite rare), then you can simply lstat the file
33-
// and catch the error.
24+
// Do not use the above function if performing a check directly before another operation on that file.
25+
// Doing so creates a race condition. The `exists` function is not recommended for that usecase.
26+
// Consider this alternative which checks for existence of a file without doing any other filesystem operations.
3427
try {
35-
await Deno.lstat("example.txt");
36-
console.log("exists!");
28+
const stats = await Deno.lstat("example.txt");
3729
} catch (err) {
3830
if (!(err instanceof Deno.errors.NotFound)) {
3931
throw err;
4032
}
41-
console.log("not exists!");
33+
console.log("File does not exist");
4234
}

0 commit comments

Comments
 (0)