resolveConfigPath.js 3.3 KB

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