3 * https://github.com/cowboy/node-findup-sync
5 * Copyright (c) 2013 "Cowboy" Ben Alman
6 * Licensed under the MIT license.
12 var path = require('path');
15 var glob = require('glob');
17 // Search for a filename in the given directory or all parent directories.
18 module.exports = function(patterns, options) {
19 // Normalize patterns to an array.
20 if (!Array.isArray(patterns)) { patterns = [patterns]; }
21 // Create globOptions so that it can be modified without mutating the
23 var globOptions = Object.create(options || {});
24 globOptions.maxDepth = 1;
25 globOptions.cwd = path.resolve(globOptions.cwd || '.');
29 // Search for files matching patterns.
30 files = patterns.map(function(pattern) {
31 return glob.sync(pattern, globOptions);
32 }).reduce(function(a, b) {
34 }).filter(function(entry, index, arr) {
35 return index === arr.indexOf(entry);
37 // Return file if found.
38 if (files.length > 0) {
39 return path.resolve(path.join(globOptions.cwd, files[0]));
42 lastpath = globOptions.cwd;
43 globOptions.cwd = path.resolve(globOptions.cwd, '..');
44 // If parentpath is the same as basedir, we can't go any higher.
45 } while (globOptions.cwd !== lastpath);
47 // No files were found!