readConfigFileAndSetRootDir.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 _jsonlint;
  14. function _load_jsonlint() {
  15. return (_jsonlint = _interopRequireDefault(require('./vendor/jsonlint')));
  16. }
  17. var _constants;
  18. function _load_constants() {
  19. return (_constants = require('./constants'));
  20. }
  21. function _interopRequireDefault(obj) {
  22. return obj && obj.__esModule ? obj : {default: obj};
  23. }
  24. // Read the configuration and set its `rootDir`
  25. // 1. If it's a `package.json` file, we look into its "jest" property
  26. // 2. For any other file, we just require it.
  27. exports.default = configPath => {
  28. const isJSON = configPath.endsWith('.json');
  29. let configObject;
  30. try {
  31. // $FlowFixMe dynamic require
  32. configObject = require(configPath);
  33. } catch (error) {
  34. if (isJSON) {
  35. throw new Error(
  36. `Jest: Failed to parse config file ${configPath}\n` +
  37. ` ${(_jsonlint || _load_jsonlint()).default.errors(
  38. (_fs || _load_fs()).default.readFileSync(configPath, 'utf8')
  39. )}`
  40. );
  41. } else {
  42. throw error;
  43. }
  44. }
  45. if (configPath.endsWith((_constants || _load_constants()).PACKAGE_JSON)) {
  46. // Event if there's no "jest" property in package.json we will still use
  47. // an empty object.
  48. configObject = configObject.jest || {};
  49. }
  50. if (configObject.rootDir) {
  51. // We don't touch it if it has an absolute path specified
  52. if (!(_path || _load_path()).default.isAbsolute(configObject.rootDir)) {
  53. // otherwise, we'll resolve it relative to the file's __dirname
  54. configObject.rootDir = (_path || _load_path()).default.resolve(
  55. (_path || _load_path()).default.dirname(configPath),
  56. configObject.rootDir
  57. );
  58. }
  59. } else {
  60. // If rootDir is not there, we'll set it to this file's __dirname
  61. configObject.rootDir = (_path || _load_path()).default.dirname(configPath);
  62. }
  63. return configObject;
  64. };
  65. /**
  66. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  67. *
  68. * This source code is licensed under the MIT license found in the
  69. * LICENSE file in the root directory of this source tree.
  70. *
  71. *
  72. */