resolve_config_path.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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) { return obj && obj.__esModule ? obj : { default: obj }; }
  18. /**
  19. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  20. *
  21. * This source code is licensed under the MIT license found in the
  22. * LICENSE file in the root directory of this source tree.
  23. *
  24. *
  25. */
  26. const isFile = filePath => (_fs || _load_fs()).default.existsSync(filePath) && !(_fs || _load_fs()).default.lstatSync(filePath).isDirectory();
  27. exports.default = (pathToResolve, cwd) => {
  28. if (!(_path || _load_path()).default.isAbsolute(cwd)) {
  29. throw new Error(`"cwd" must be an absolute path. cwd: ${cwd}`);
  30. }
  31. const absolutePath = (_path || _load_path()).default.isAbsolute(pathToResolve) ? pathToResolve : (_path || _load_path()).default.resolve(cwd, pathToResolve);
  32. if (isFile(absolutePath)) {
  33. return absolutePath;
  34. }
  35. // This is a guard against passing non existing path as a project/config,
  36. // that will otherwise result in a very confusing situation.
  37. // e.g.
  38. // With a directory structure like this:
  39. // my_project/
  40. // packcage.json
  41. //
  42. // Passing a `my_project/some_directory_that_doesnt_exist` as a project
  43. // name will resolve into a (possibly empty) `my_project/package.json` and
  44. // try to run all tests it finds under `my_project` directory.
  45. if (!(_fs || _load_fs()).default.existsSync(absolutePath)) {
  46. throw new Error(`Can't find a root directory while resolving a config file path.\n` + `Provided path to resolve: ${pathToResolve}\n` + `cwd: ${cwd}`);
  47. }
  48. return resolveConfigPathByTraversing(absolutePath, pathToResolve, cwd);
  49. };
  50. const resolveConfigPathByTraversing = (pathToResolve, initialPath, cwd) => {
  51. const jestConfig = (_path || _load_path()).default.resolve(pathToResolve, (_constants || _load_constants()).JEST_CONFIG);
  52. if (isFile(jestConfig)) {
  53. return jestConfig;
  54. }
  55. const packageJson = (_path || _load_path()).default.resolve(pathToResolve, (_constants || _load_constants()).PACKAGE_JSON);
  56. if (isFile(packageJson)) {
  57. return packageJson;
  58. }
  59. // This is the system root.
  60. // We tried everything, config is nowhere to be found ¯\_(ツ)_/¯
  61. if (pathToResolve === (_path || _load_path()).default.dirname(pathToResolve)) {
  62. throw new Error(makeResolutionErrorMessage(initialPath, cwd));
  63. }
  64. // go up a level and try it again
  65. return resolveConfigPathByTraversing((_path || _load_path()).default.dirname(pathToResolve), initialPath, cwd);
  66. };
  67. const makeResolutionErrorMessage = (initialPath, cwd) => {
  68. return 'Could not find a config file based on provided values:\n' + `path: "${initialPath}"\n` + `cwd: "${cwd}"\n` + 'Config paths must be specified by either a direct path to a config\n' + 'file, or a path to a directory. If directory is given, Jest will try to\n' + `traverse directory tree up, until it finds either "${(_constants || _load_constants()).JEST_CONFIG}" or\n` + `"${(_constants || _load_constants()).PACKAGE_JSON}".`;
  69. };