resolveConfigPath.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _path() {
  7. const data = _interopRequireDefault(require('path'));
  8. _path = function _path() {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _fs() {
  14. const data = _interopRequireDefault(require('fs'));
  15. _fs = function _fs() {
  16. return data;
  17. };
  18. return data;
  19. }
  20. var _constants = require('./constants');
  21. function _interopRequireDefault(obj) {
  22. return obj && obj.__esModule ? obj : {default: obj};
  23. }
  24. /**
  25. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  26. *
  27. * This source code is licensed under the MIT license found in the
  28. * LICENSE file in the root directory of this source tree.
  29. */
  30. const isFile = filePath =>
  31. _fs().default.existsSync(filePath) &&
  32. !_fs()
  33. .default.lstatSync(filePath)
  34. .isDirectory();
  35. var _default = (pathToResolve, cwd) => {
  36. if (!_path().default.isAbsolute(cwd)) {
  37. throw new Error(`"cwd" must be an absolute path. cwd: ${cwd}`);
  38. }
  39. const absolutePath = _path().default.isAbsolute(pathToResolve)
  40. ? pathToResolve
  41. : _path().default.resolve(cwd, pathToResolve);
  42. if (isFile(absolutePath)) {
  43. return absolutePath;
  44. } // This is a guard against passing non existing path as a project/config,
  45. // that will otherwise result in a very confusing situation.
  46. // e.g.
  47. // With a directory structure like this:
  48. // my_project/
  49. // packcage.json
  50. //
  51. // Passing a `my_project/some_directory_that_doesnt_exist` as a project
  52. // name will resolve into a (possibly empty) `my_project/package.json` and
  53. // try to run all tests it finds under `my_project` directory.
  54. if (!_fs().default.existsSync(absolutePath)) {
  55. throw new Error(
  56. `Can't find a root directory while resolving a config file path.\n` +
  57. `Provided path to resolve: ${pathToResolve}\n` +
  58. `cwd: ${cwd}`
  59. );
  60. }
  61. return resolveConfigPathByTraversing(absolutePath, pathToResolve, cwd);
  62. };
  63. exports.default = _default;
  64. const resolveConfigPathByTraversing = (pathToResolve, initialPath, cwd) => {
  65. const jestConfig = _path().default.resolve(
  66. pathToResolve,
  67. _constants.JEST_CONFIG
  68. );
  69. if (isFile(jestConfig)) {
  70. return jestConfig;
  71. }
  72. const packageJson = _path().default.resolve(
  73. pathToResolve,
  74. _constants.PACKAGE_JSON
  75. );
  76. if (isFile(packageJson)) {
  77. return packageJson;
  78. } // This is the system root.
  79. // We tried everything, config is nowhere to be found ¯\_(ツ)_/¯
  80. if (pathToResolve === _path().default.dirname(pathToResolve)) {
  81. throw new Error(makeResolutionErrorMessage(initialPath, cwd));
  82. } // go up a level and try it again
  83. return resolveConfigPathByTraversing(
  84. _path().default.dirname(pathToResolve),
  85. initialPath,
  86. cwd
  87. );
  88. };
  89. const makeResolutionErrorMessage = (initialPath, cwd) =>
  90. 'Could not find a config file based on provided values:\n' +
  91. `path: "${initialPath}"\n` +
  92. `cwd: "${cwd}"\n` +
  93. 'Config paths must be specified by either a direct path to a config\n' +
  94. 'file, or a path to a directory. If directory is given, Jest will try to\n' +
  95. `traverse directory tree up, until it finds either "${_constants.JEST_CONFIG}" or\n` +
  96. `"${_constants.PACKAGE_JSON}".`;