# all files / lib/ index.js

100% Statements 55/55
91.67% Branches 22/24
100% Functions 8/8
100% Lines 55/55
3 statements, 1 branch Ignored

    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
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    961×
    1×

    1×
    1×
    1×

    1×
    22×
    2×

    22×
    22×
    22×
    3×
    3×

    19×
    19×
    1×
    1×
    1×
    1×

    21×
    3×
    3×

    18×
    18×

    1×
    27×
    3×

    27×
    27×

    27×
    27×
    27×
    27×

    5×
    5×

    22×

    27×
    3×
    3×

    24×
    2×
    2×
    2×
    2×

    22×
    22×

    1×
    62×
    62×

    1×

    62×
    62×

    1×
    1×
    1×

     module.exports = resolvePkg;
    module.exports.sync = sync;

    var fs = require('then-fs');
    var path = require('path');
    var debug = require('debug')('snyk:resolve');

    function resolvePkg(name, basedir) {
      if (!basedir) {
        basedir = process.cwd();
      }

      var filename = path.resolve(basedir, 'node_modules', name, 'package.json');
      debug('%s: %s', name, filename);
      return fs.stat(filename).then(function (stat) {
        Eif (stat.isFile()) {
          return path.dirname(filename);
        }
      }).catch(function (error) {
        debug('%s: not found on %s (root? %s)', name, basedir, isRoot(basedir));
        if (isRoot(basedir)) {
          debug('at root');
          error = new Error('package not found ' + name);
          error.code = 'NO_PACKAGE_FOUND';
          throw error;
        }
      }).then(function (dir) {
        if (dir) {
          debug('%s: FOUND AT %s', name, dir);
          return dir;
        }

        debug('%s: cycling down', name);
        return resolvePkg(name, path.resolve(basedir, '..'));
      });
    }

    function sync(name, basedir) {
      if (!basedir) {
        basedir = process.cwd();
      }

      var filename = path.resolve(basedir, 'node_modules', name, 'package.json');
      debug('%s: %s', name, filename);

      var isFile = function (file) {
        var stat;
        try {
          stat = fs.statSync(file);
        } catch (error) {
          Eif (error && error.code === 'ENOENT') {
            return false;
          }
        }
        return stat.isFile() || stat.isFIFO();
      };

      if (isFile(filename)) {
        debug('%s: FOUND AT %s', name, filename);
        return path.dirname(filename);
      }

      if (isRoot(basedir)) {
        debug('%s: not found on %s (now at root)', name, filename);
        var error = new Error('package not found ' + name);
        error.code = 'NO_PACKAGE_FOUND';
        throw error;
      }

      debug('%s: cycling down', name);
      return sync(name, path.resolve(basedir, '..'));
    }

    function isRoot(dir) {
      var parsed = parse(dir);
      return parsed.root === parsed.dir && !parsed.base;
    }

    // FIXME determine whether this would work properly on windows in 0.10
    function parse(dir) {
      /* istanbul ignore else  */
      // jscs:disable requireEarlyReturn
      Eif (path.parse) {
        return path.parse(dir);
      } else {
        var split = dir.split(path.sep);
        var root = split[0] + path.sep;
        return {
          base: split[1],
          root: root,
          dir: dir,
        };
      }
      // jscs:enable requireEarlyReturn
    }