Projekt

Obecné

Profil

Stáhnout (1.98 KB) Statistiky
| Větev: | Revize:
1
'use strict';
2

    
3
/**
4
 * Module dependencies
5
 */
6

    
7
var fs = require('fs');
8
var path = require('path');
9
var isGlob = require('is-glob');
10
var resolveDir = require('resolve-dir');
11
var detect = require('detect-file');
12
var mm = require('micromatch');
13

    
14
/**
15
 * @param  {String|Array} `pattern` Glob pattern or file path(s) to match against.
16
 * @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.
17
 * @return {String} Returns the first matching file.
18
 * @api public
19
 */
20

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

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

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

    
33
  return lookup(cwd, patterns, options);
34
};
35

    
36
function lookup(cwd, patterns, options) {
37
  var len = patterns.length;
38
  var idx = -1;
39
  var res;
40

    
41
  while (++idx < len) {
42
    if (isGlob(patterns[idx])) {
43
      res = matchFile(cwd, patterns[idx], options);
44
    } else {
45
      res = findFile(cwd, patterns[idx], options);
46
    }
47
    if (res) {
48
      return res;
49
    }
50
  }
51

    
52
  var dir = path.dirname(cwd);
53
  if (dir === cwd) {
54
    return null;
55
  }
56
  return lookup(dir, patterns, options);
57
}
58

    
59
function matchFile(cwd, pattern, opts) {
60
  var isMatch = mm.matcher(pattern, opts);
61
  var files = tryReaddirSync(cwd);
62
  var len = files.length;
63
  var idx = -1;
64

    
65
  while (++idx < len) {
66
    var name = files[idx];
67
    var fp = path.join(cwd, name);
68
    if (isMatch(name) || isMatch(fp)) {
69
      return fp;
70
    }
71
  }
72
  return null;
73
}
74

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

    
80
function tryReaddirSync(fp) {
81
  try {
82
    return fs.readdirSync(fp);
83
  } catch (err) {
84
    // Ignore error
85
  }
86
  return [];
87
}
(3-3/4)