all files / src/configLoader/ findup.js

88.24% Statements 15/17
75% Branches 3/4
100% Functions 2/2
88.24% Lines 15/17
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               14× 14×   14× 14× 14×   14× 14× 42×   42× 14×       14× 14×            
import path from 'path';
import glob from 'glob';
 
export default findup;
 
// Before, "findup-sync" package was used,
// but it does not provide filter callback
function findup(patterns, options, fn) {
    /* jshint -W083 */
 
    var lastpath;
    var file;
 
    options = Object.create(options);
    options.maxDepth = 1;
    options.cwd = path.resolve(options.cwd);
 
    do {
        file = patterns.filter(function(pattern) {
            var configPath = glob.sync(pattern, options)[0];
 
            if (configPath) {
                return fn(path.join(options.cwd, configPath));
            }
        })[0];
 
        Eif (file) {
            return path.join(options.cwd, file);
        }
 
        lastpath = options.cwd;
        options.cwd = path.resolve(options.cwd, '..');
    } while (options.cwd !== lastpath);
}