1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | ![is-there](http://i.imgur.com/ZHzpvvE.png)
|
17 |
|
18 | # is-there [![Donate now][donate-now]][paypal-donations]
|
19 |
|
20 | Check if a file or directory exists in a given path.
|
21 |
|
22 | ## Why? `fs.exists` already does the job!
|
23 | Because `fs.exists` and `fs.existsSync` will be deprecated and I still like and need them!
|
24 |
|
25 | > `fs.exists()` is an anachronism and exists only for historical reasons. There should almost never be a reason to use it in your own code.
|
26 |
|
27 | > In particular, checking if a file exists before opening it is an anti-pattern that leaves you vulnerable to race conditions: another process may remove the file between the calls to `fs.exists()` and `fs.open()`. Just open the file and handle the error when it's not there.
|
28 |
|
29 | > **`fs.exists()` will be deprecated.**
|
30 |
|
31 | > <sup>([Source](http://nodejs.org/api/fs.html#fs_fs_exists_path_callback), emphasis added)</sup>
|
32 |
|
33 | ## Installation
|
34 |
|
35 | ```sh
|
36 | $ npm i is-there
|
37 | ```
|
38 |
|
39 | ## Example
|
40 |
|
41 | ```js
|
42 | // Dependencies
|
43 | var IsThere = require("is-there");
|
44 |
|
45 | // Paths to test
|
46 | var paths = [
|
47 | // exist
|
48 | "dir"
|
49 | , "dir/another"
|
50 | , "dir/another/file"
|
51 | , "dir/file"
|
52 | , "file"
|
53 | , "file.ext"
|
54 | // don't exist
|
55 | , "foo"
|
56 | , "foo/bar"
|
57 | , "foo.bar"
|
58 | , "foo/bar.foo"
|
59 | ].map(function (c) {
|
60 | return __dirname + "/contents/" + c;
|
61 | });
|
62 |
|
63 | // Sync
|
64 | console.log("> Testing sync method.");
|
65 | paths.forEach(function (c) {
|
66 | console.log("> %s %s", c, IsThere(c) ? "exists" : "doesn't exist");
|
67 | });
|
68 |
|
69 | console.log("> Testing async method.");
|
70 | function doSeq(i) {
|
71 | i = i || 0;
|
72 | var cPath = paths[i];
|
73 | if (!cPath) { return; }
|
74 | IsThere(cPath, function (exists) {
|
75 | console.log("> %s %s", cPath, exists ? "exists" : "doesn't exist");
|
76 | doSeq(i + 1);
|
77 | });
|
78 | }
|
79 |
|
80 | doSeq();
|
81 |
|
82 | ```
|
83 |
|
84 | ## Documentation
|
85 |
|
86 | ### `IsThere(path, callback)`
|
87 | Checks if a file or directory exists on given path.
|
88 |
|
89 | #### Params
|
90 | - **String** `path`: The path to the file or directory.
|
91 | - **Function** `callback`: The callback function called with a boolean value representing if the file or directory exists. If this parameter is not a
|
92 | function, the function will run the synchronously and return the value.
|
93 |
|
94 | #### Return
|
95 | - **IsThere|Boolean** The `IsThere` function if the `callback` parameter was provided, otherwise a boolean value indicating if the file/directory
|
96 | exists or not.
|
97 |
|
98 | ## How to contribute
|
99 | Have an idea? Found a bug? See [how to contribute][contributing].
|
100 |
|
101 | ## License
|
102 | [KINDLY][license] © [Ionică Bizău][website]–The [LICENSE](/LICENSE) file contains
|
103 | a copy of the license.
|
104 |
|
105 | [license]: http://ionicabizau.github.io/kindly-license/?author=Ionic%C4%83%20Biz%C4%83u%20%3Cbizauionica@gmail.com%3E&year=2015
|
106 | [contributing]: /CONTRIBUTING.md
|
107 | [website]: http://ionicabizau.net
|
108 | [docs]: /DOCUMENTATION.md
|
109 | [paypal-donations]: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=MG98D7NPFZ3MG
|
110 | [donate-now]: http://i.imgur.com/jioicaN.png |
\ | No newline at end of file |