normalizeOptions.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var _fs = _interopRequireDefault(require("fs"));
  5. var _path = _interopRequireDefault(require("path"));
  6. var _reselect = require("reselect");
  7. var _findBabelConfig = _interopRequireDefault(require("find-babel-config"));
  8. var _glob = _interopRequireDefault(require("glob"));
  9. var _pkgUp = _interopRequireDefault(require("pkg-up"));
  10. var _utils = require("./utils");
  11. var _resolvePath = _interopRequireDefault(require("./resolvePath"));
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. const defaultExtensions = ['.js', '.jsx', '.es', '.es6', '.mjs'];
  14. const defaultTransformedFunctions = ['require', 'require.resolve', 'System.import', // Jest methods
  15. 'jest.genMockFromModule', 'jest.mock', 'jest.unmock', 'jest.doMock', 'jest.dontMock', 'jest.setMock', 'require.requireActual', 'require.requireMock'];
  16. function isRegExp(string) {
  17. return string.startsWith('^') || string.endsWith('$');
  18. }
  19. const specialCwd = {
  20. babelrc: startPath => _findBabelConfig.default.sync(startPath).file,
  21. packagejson: startPath => _pkgUp.default.sync(startPath)
  22. };
  23. function normalizeCwd(optsCwd, currentFile) {
  24. let cwd;
  25. if (optsCwd in specialCwd) {
  26. const startPath = currentFile === 'unknown' ? './' : currentFile;
  27. const computedCwd = specialCwd[optsCwd](startPath);
  28. cwd = computedCwd ? _path.default.dirname(computedCwd) : null;
  29. } else {
  30. cwd = optsCwd;
  31. }
  32. return cwd || process.cwd();
  33. }
  34. function normalizeRoot(optsRoot, cwd) {
  35. if (!optsRoot) {
  36. return [];
  37. }
  38. const rootArray = Array.isArray(optsRoot) ? optsRoot : [optsRoot];
  39. return rootArray.map(dirPath => _path.default.resolve(cwd, dirPath)).reduce((resolvedDirs, absDirPath) => {
  40. if (_glob.default.hasMagic(absDirPath)) {
  41. const roots = _glob.default.sync(absDirPath).filter(resolvedPath => _fs.default.lstatSync(resolvedPath).isDirectory());
  42. return [...resolvedDirs, ...roots];
  43. }
  44. return [...resolvedDirs, absDirPath];
  45. }, []);
  46. }
  47. function getAliasTarget(key, isKeyRegExp) {
  48. const regExpPattern = isKeyRegExp ? key : `^${(0, _utils.escapeRegExp)(key)}(/.*|)$`;
  49. return new RegExp(regExpPattern);
  50. }
  51. function getAliasSubstitute(value, isKeyRegExp) {
  52. if (typeof value === 'function') {
  53. return value;
  54. }
  55. if (!isKeyRegExp) {
  56. return ([, match]) => `${value}${match}`;
  57. }
  58. const parts = value.split('\\\\');
  59. return execResult => parts.map(part => part.replace(/\\\d+/g, number => execResult[number.slice(1)] || '')).join('\\');
  60. }
  61. function normalizeAlias(optsAlias) {
  62. if (!optsAlias) {
  63. return [];
  64. }
  65. const aliasArray = Array.isArray(optsAlias) ? optsAlias : [optsAlias];
  66. return aliasArray.reduce((aliasPairs, alias) => {
  67. const aliasKeys = Object.keys(alias);
  68. aliasKeys.forEach(key => {
  69. const isKeyRegExp = isRegExp(key);
  70. aliasPairs.push([getAliasTarget(key, isKeyRegExp), getAliasSubstitute(alias[key], isKeyRegExp)]);
  71. });
  72. return aliasPairs;
  73. }, []);
  74. }
  75. function normalizeTransformedFunctions(optsTransformFunctions) {
  76. if (!optsTransformFunctions) {
  77. return defaultTransformedFunctions;
  78. }
  79. return [...defaultTransformedFunctions, ...optsTransformFunctions];
  80. }
  81. function normalizeLoglevel(optsLoglevel) {
  82. return optsLoglevel || 'warn';
  83. }
  84. var _default = (0, _reselect.createSelector)( // The currentFile should have an extension; otherwise it's considered a special value
  85. currentFile => currentFile.includes('.') ? _path.default.dirname(currentFile) : currentFile, (_, opts) => opts, (currentFile, opts) => {
  86. const cwd = normalizeCwd(opts.cwd, currentFile);
  87. const root = normalizeRoot(opts.root, cwd);
  88. const alias = normalizeAlias(opts.alias);
  89. const loglevel = normalizeLoglevel(opts.loglevel);
  90. const transformFunctions = normalizeTransformedFunctions(opts.transformFunctions);
  91. const extensions = opts.extensions || defaultExtensions;
  92. const stripExtensions = opts.stripExtensions || extensions;
  93. const resolvePath = opts.resolvePath || _resolvePath.default;
  94. return {
  95. cwd,
  96. root,
  97. alias,
  98. loglevel,
  99. transformFunctions,
  100. extensions,
  101. stripExtensions,
  102. resolvePath
  103. };
  104. });
  105. exports.default = _default;