Sha256: c28a7d7d3cea40468a54ce118614d69f21f89d70b72af5b799ed20e8061f88ab

Contents?: true

Size: 1.98 KB

Versions: 26

Compression:

Stored size: 1.98 KB

Contents

'use strict';

/**
 * Module dependencies
 */

var fs = require('fs');
var path = require('path');
var isGlob = require('is-glob');
var resolveDir = require('resolve-dir');
var detect = require('detect-file');
var mm = require('micromatch');

/**
 * @param  {String|Array} `pattern` Glob pattern or file path(s) to match against.
 * @param  {Object} `options` Options to pass to [micromatch]. Note that if you want to start in a different directory than the current working directory, specify the `options.cwd` property here.
 * @return {String} Returns the first matching file.
 * @api public
 */

module.exports = function(patterns, options) {
  options = options || {};
  var cwd = path.resolve(resolveDir(options.cwd || ''));

  if (typeof patterns === 'string') {
    return lookup(cwd, [patterns], options);
  }

  if (!Array.isArray(patterns)) {
    throw new TypeError('findup-sync expects a string or array as the first argument.');
  }

  return lookup(cwd, patterns, options);
};

function lookup(cwd, patterns, options) {
  var len = patterns.length;
  var idx = -1;
  var res;

  while (++idx < len) {
    if (isGlob(patterns[idx])) {
      res = matchFile(cwd, patterns[idx], options);
    } else {
      res = findFile(cwd, patterns[idx], options);
    }
    if (res) {
      return res;
    }
  }

  var dir = path.dirname(cwd);
  if (dir === cwd) {
    return null;
  }
  return lookup(dir, patterns, options);
}

function matchFile(cwd, pattern, opts) {
  var isMatch = mm.matcher(pattern, opts);
  var files = tryReaddirSync(cwd);
  var len = files.length;
  var idx = -1;

  while (++idx < len) {
    var name = files[idx];
    var fp = path.join(cwd, name);
    if (isMatch(name) || isMatch(fp)) {
      return fp;
    }
  }
  return null;
}

function findFile(cwd, filename, options) {
  var fp = cwd ? path.resolve(cwd, filename) : filename;
  return detect(fp, options);
}

function tryReaddirSync(fp) {
  try {
    return fs.readdirSync(fp);
  } catch (err) {
    // Ignore error
  }
  return [];
}

Version data entries

26 entries across 25 versions & 8 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/findup-sync/index.js
disco_app-0.18.0 test/dummy/node_modules/findup-sync/index.js
disco_app-0.18.2 test/dummy/node_modules/findup-sync/index.js
disco_app-0.16.1 test/dummy/node_modules/findup-sync/index.js
disco_app-0.15.2 test/dummy/node_modules/findup-sync/index.js
disco_app-0.18.4 test/dummy/node_modules/findup-sync/index.js
disco_app-0.18.1 test/dummy/node_modules/findup-sync/index.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/findup-sync/index.js
disco_app-0.14.0 test/dummy/node_modules/findup-sync/index.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/findup-sync/index.js
tang-0.2.1 spec/tang_app/node_modules/findup-sync/index.js
groonga-client-model-6.0.0 test/apps/rails6.0.3.5/node_modules/findup-sync/index.js
groonga-client-model-6.0.0 test/apps/rails6.1.3/node_modules/findup-sync/index.js
ruby2js-4.0.4 lib/tasks/testrails/node_modules/findup-sync/index.js
ruby2js-4.0.3 lib/tasks/testrails/node_modules/findup-sync/index.js
tang-0.2.0 spec/tang_app/node_modules/findup-sync/index.js
tang-0.1.0 spec/tang_app/node_modules/findup-sync/index.js
tang-0.0.9 spec/tang_app/node_modules/findup-sync/index.js
enju_library-0.3.8 spec/dummy/node_modules/findup-sync/index.js
jester-data-8.0.0 node_modules/findup-sync/index.js