-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
57 lines (49 loc) · 1.31 KB
/
index.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
56
57
const fs = require('fs')
const path = require('path')
function fileExists (filepath, options, done) {
if (typeof options === 'function') {
done = options
options = {}
}
if (!done) {
return new Promise((resolve, reject) => {
fs.stat(fullPath(filepath, options), (err, stats) => {
if (err) {
return err.code === 'ENOENT'
? resolve(false)
: reject(err)
}
resolve(stats.isFile())
})
})
}
fs.stat(fullPath(filepath, options), (err, stats) => {
if (err) {
return err.code === 'ENOENT'
? done(null, false)
: done(err)
}
done(null, stats.isFile())
})
}
fileExists.sync = function fileExistsSync (filepath, options) {
const _filepath = filepath || '';
const _options = options || {};
try {
return fs.statSync(fullPath(_filepath, _options)).isFile()
}
catch (e) {
// Check exception. If ENOENT - no such file or directory ok, file doesn't exist.
// Otherwise something else went wrong, we don't have rights to access the file, ...
if (e.code != 'ENOENT') {
throw e
}
return false
}
}
function fullPath (filepath, options) {
const _options = options || {};
const root = _options.root;
return (root) ? path.join(root, filepath) : filepath
}
module.exports = fileExists