1
|
/*!
|
2
|
* detect-file <https://github.com/doowb/detect-file>
|
3
|
*
|
4
|
* Copyright (c) 2016-2017, Brian Woodward.
|
5
|
* Released under the MIT License.
|
6
|
*/
|
7
|
|
8
|
'use strict';
|
9
|
|
10
|
var fs = require('fs');
|
11
|
var path = require('path');
|
12
|
|
13
|
/**
|
14
|
* Detect the given `filepath` if it exists.
|
15
|
*
|
16
|
* ```js
|
17
|
* var res = detect('package.json');
|
18
|
* console.log(res);
|
19
|
* //=> "package.json"
|
20
|
*
|
21
|
* var res = detect('fake-file.json');
|
22
|
* console.log(res)
|
23
|
* //=> null
|
24
|
* ```
|
25
|
*
|
26
|
* @param {String} `filepath` filepath to detect.
|
27
|
* @param {Object} `options` Additional options.
|
28
|
* @param {Boolean} `options.nocase` Set this to `true` to force case-insensitive filename checks. This is useful on case sensitive file systems.
|
29
|
* @return {String} Returns the detected filepath if it exists, otherwise returns `null`.
|
30
|
* @api public
|
31
|
*/
|
32
|
|
33
|
module.exports = function detect(filepath, options) {
|
34
|
if (!filepath || (typeof filepath !== 'string')) {
|
35
|
return null;
|
36
|
}
|
37
|
if (fs.existsSync(filepath)) {
|
38
|
return path.resolve(filepath);
|
39
|
}
|
40
|
|
41
|
options = options || {};
|
42
|
if (options.nocase === true) {
|
43
|
return nocase(filepath);
|
44
|
}
|
45
|
return null;
|
46
|
};
|
47
|
|
48
|
/**
|
49
|
* Check if the filepath exists by falling back to reading in the entire directory.
|
50
|
* Returns the real filepath (for case sensitive file systems) if found.
|
51
|
*
|
52
|
* @param {String} `filepath` filepath to check.
|
53
|
* @return {String} Returns found filepath if exists, otherwise null.
|
54
|
*/
|
55
|
|
56
|
function nocase(filepath) {
|
57
|
filepath = path.resolve(filepath);
|
58
|
var res = tryReaddir(filepath);
|
59
|
if (res === null) {
|
60
|
return null;
|
61
|
}
|
62
|
|
63
|
// "filepath" is a directory, an error would be
|
64
|
// thrown if it doesn't exist. if we're here, it exists
|
65
|
if (res.path === filepath) {
|
66
|
return res.path;
|
67
|
}
|
68
|
|
69
|
// "filepath" is not a directory
|
70
|
// compare against upper case later
|
71
|
// see https://nodejs.org/en/docs/guides/working-with-different-filesystems/
|
72
|
var upper = filepath.toUpperCase();
|
73
|
var len = res.files.length;
|
74
|
var idx = -1;
|
75
|
|
76
|
while (++idx < len) {
|
77
|
var fp = path.resolve(res.path, res.files[idx]);
|
78
|
if (filepath === fp || upper === fp) {
|
79
|
return fp;
|
80
|
}
|
81
|
var fpUpper = fp.toUpperCase();
|
82
|
if (filepath === fpUpper || upper === fpUpper) {
|
83
|
return fp;
|
84
|
}
|
85
|
}
|
86
|
|
87
|
return null;
|
88
|
}
|
89
|
|
90
|
/**
|
91
|
* Try to read the filepath as a directory first, then fallback to the filepath's dirname.
|
92
|
*
|
93
|
* @param {String} `filepath` path of the directory to read.
|
94
|
* @return {Object} Object containing `path` and `files` if succesful. Otherwise, null.
|
95
|
*/
|
96
|
|
97
|
function tryReaddir(filepath) {
|
98
|
var ctx = { path: filepath, files: [] };
|
99
|
try {
|
100
|
ctx.files = fs.readdirSync(filepath);
|
101
|
return ctx;
|
102
|
} catch (err) {}
|
103
|
try {
|
104
|
ctx.path = path.dirname(filepath);
|
105
|
ctx.files = fs.readdirSync(ctx.path);
|
106
|
return ctx;
|
107
|
} catch (err) {}
|
108
|
return null;
|
109
|
}
|