‘liusuyi’
2023-08-09 161b9318e345c8a0c9cdc133b33a1c759495f323
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
'use strict';
var path = require('path');
var pathExists = require('path-exists');
var Promise = require('pinkie-promise');
 
function splitPath(x) {
    return path.resolve(x || '').split(path.sep);
}
 
function join(parts, filename) {
    return path.resolve(parts.join(path.sep) + path.sep, filename);
}
 
module.exports = function (filename, opts) {
    opts = opts || {};
 
    var parts = splitPath(opts.cwd);
 
    return new Promise(function (resolve) {
        (function find() {
            var fp = join(parts, filename);
 
            pathExists(fp).then(function (exists) {
                if (exists) {
                    resolve(fp);
                } else if (parts.pop()) {
                    find();
                } else {
                    resolve(null);
                }
            });
        })();
    });
};
 
module.exports.sync = function (filename, opts) {
    opts = opts || {};
 
    var parts = splitPath(opts.cwd);
    var len = parts.length;
 
    while (len--) {
        var fp = join(parts, filename);
 
        if (pathExists.sync(fp)) {
            return fp;
        }
 
        parts.pop();
    }
 
    return null;
};