nodeModulesPaths.js
  1  'use strict';
  2  
  3  Object.defineProperty(exports, '__esModule', {
  4    value: true
  5  });
  6  exports.default = nodeModulesPaths;
  7  
  8  function path() {
  9    const data = _interopRequireWildcard(require('path'));
 10  
 11    path = function () {
 12      return data;
 13    };
 14  
 15    return data;
 16  }
 17  
 18  function _jestUtil() {
 19    const data = require('jest-util');
 20  
 21    _jestUtil = function () {
 22      return data;
 23    };
 24  
 25    return data;
 26  }
 27  
 28  function _getRequireWildcardCache() {
 29    if (typeof WeakMap !== 'function') return null;
 30    var cache = new WeakMap();
 31    _getRequireWildcardCache = function () {
 32      return cache;
 33    };
 34    return cache;
 35  }
 36  
 37  function _interopRequireWildcard(obj) {
 38    if (obj && obj.__esModule) {
 39      return obj;
 40    }
 41    if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
 42      return {default: obj};
 43    }
 44    var cache = _getRequireWildcardCache();
 45    if (cache && cache.has(obj)) {
 46      return cache.get(obj);
 47    }
 48    var newObj = {};
 49    var hasPropertyDescriptor =
 50      Object.defineProperty && Object.getOwnPropertyDescriptor;
 51    for (var key in obj) {
 52      if (Object.prototype.hasOwnProperty.call(obj, key)) {
 53        var desc = hasPropertyDescriptor
 54          ? Object.getOwnPropertyDescriptor(obj, key)
 55          : null;
 56        if (desc && (desc.get || desc.set)) {
 57          Object.defineProperty(newObj, key, desc);
 58        } else {
 59          newObj[key] = obj[key];
 60        }
 61      }
 62    }
 63    newObj.default = obj;
 64    if (cache) {
 65      cache.set(obj, newObj);
 66    }
 67    return newObj;
 68  }
 69  
 70  /**
 71   * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
 72   *
 73   * This source code is licensed under the MIT license found in the
 74   * LICENSE file in the root directory of this source tree.
 75   *
 76   * Adapted from: https://github.com/substack/node-resolve
 77   */
 78  function nodeModulesPaths(basedir, options) {
 79    const modules =
 80      options && options.moduleDirectory
 81        ? Array.from(options.moduleDirectory)
 82        : ['node_modules']; // ensure that `basedir` is an absolute path at this point,
 83    // resolving against the process' current working directory
 84  
 85    const basedirAbs = path().resolve(basedir);
 86    let prefix = '/';
 87  
 88    if (/^([A-Za-z]:)/.test(basedirAbs)) {
 89      prefix = '';
 90    } else if (/^\\\\/.test(basedirAbs)) {
 91      prefix = '\\\\';
 92    } // The node resolution algorithm (as implemented by NodeJS and TypeScript)
 93    // traverses parents of the physical path, not the symlinked path
 94  
 95    let physicalBasedir;
 96  
 97    try {
 98      physicalBasedir = (0, _jestUtil().tryRealpath)(basedirAbs);
 99    } catch {
100      // realpath can throw, e.g. on mapped drives
101      physicalBasedir = basedirAbs;
102    }
103  
104    const paths = [physicalBasedir];
105    let parsed = path().parse(physicalBasedir);
106  
107    while (parsed.dir !== paths[paths.length - 1]) {
108      paths.push(parsed.dir);
109      parsed = path().parse(parsed.dir);
110    }
111  
112    const dirs = paths
113      .reduce(
114        (dirs, aPath) =>
115          dirs.concat(
116            modules.map(moduleDir =>
117              path().isAbsolute(moduleDir)
118                ? aPath === basedirAbs
119                  ? moduleDir
120                  : ''
121                : path().join(prefix, aPath, moduleDir)
122            )
123          ),
124        []
125      )
126      .filter(dir => dir !== '');
127    return options.paths ? dirs.concat(options.paths) : dirs;
128  }