-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
55 lines (51 loc) · 1.16 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const fs = require('fs')
/**
* Function which will check if a file exists
*
* @param {string} file - The filepath as a string
* @param {string} callback - The callback function which takes the boolean result
* @return {void} Nothing
*
* @example
*
* exists('myfile.txt', (result) => {
* console.log(result)
* })
*/
function exists (file, callback) {
fs.stat(file, function (err) {
if(err == null) {
callback(true)
} else if(err.code === 'ENOENT') {
callback(false)
} else {
callback(false)
}
})
}
/**
* Function which will asynchronously check if a file exists
*
* @param {string} file - The filepath as a string
* @return {Promise<boolean>} A promise which resolves to a boolean
*
* @example
*
* const result = await existsAsync('myfile.txt')
*/
function existsAsync (file) {
return new Promise((resolve, reject) => {
exists(file, (res) => {
if (res) {
resolve(true)
} else {
resolve(false)
}
})
})
}
const enoent = {
exists,
existsAsync
}
module.exports = enoent