ExplorerBase.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.ExplorerBase = void 0;
  6. exports.getExtensionDescription = getExtensionDescription;
  7. var _path = _interopRequireDefault(require("path"));
  8. var _getPropertyByPath = require("./getPropertyByPath");
  9. var _loaders = require("./loaders");
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. class ExplorerBase {
  12. constructor(options) {
  13. if (options.cache) {
  14. this.loadCache = new Map();
  15. this.searchCache = new Map();
  16. }
  17. this.config = options;
  18. this.validateConfig();
  19. }
  20. clearLoadCache() {
  21. if (this.loadCache) {
  22. this.loadCache.clear();
  23. }
  24. }
  25. clearSearchCache() {
  26. if (this.searchCache) {
  27. this.searchCache.clear();
  28. }
  29. }
  30. clearCaches() {
  31. this.clearLoadCache();
  32. this.clearSearchCache();
  33. }
  34. validateConfig() {
  35. const config = this.config;
  36. config.searchPlaces.forEach(place => {
  37. const loaderKey = _path.default.extname(place) || 'noExt';
  38. const loader = config.loaders[loaderKey];
  39. if (!loader) {
  40. throw new Error(`No loader specified for ${getExtensionDescription(place)}, so searchPlaces item "${place}" is invalid`);
  41. }
  42. if (typeof loader !== 'function') {
  43. throw new Error(`loader for ${getExtensionDescription(place)} is not a function (type provided: "${typeof loader}"), so searchPlaces item "${place}" is invalid`);
  44. }
  45. });
  46. }
  47. shouldSearchStopWithResult(result) {
  48. if (result === null) return false;
  49. return !(result.isEmpty && this.config.ignoreEmptySearchPlaces);
  50. }
  51. nextDirectoryToSearch(currentDir, currentResult) {
  52. if (this.shouldSearchStopWithResult(currentResult)) {
  53. return null;
  54. }
  55. const nextDir = nextDirUp(currentDir);
  56. if (nextDir === currentDir || currentDir === this.config.stopDir) {
  57. return null;
  58. }
  59. return nextDir;
  60. }
  61. loadPackageProp(filepath, content) {
  62. const parsedContent = _loaders.loaders.loadJson(filepath, content);
  63. const packagePropValue = (0, _getPropertyByPath.getPropertyByPath)(parsedContent, this.config.packageProp);
  64. return packagePropValue || null;
  65. }
  66. getLoaderEntryForFile(filepath) {
  67. if (_path.default.basename(filepath) === 'package.json') {
  68. return this.loadPackageProp.bind(this);
  69. }
  70. const loaderKey = _path.default.extname(filepath) || 'noExt';
  71. const loader = this.config.loaders[loaderKey];
  72. if (!loader) {
  73. throw new Error(`No loader specified for ${getExtensionDescription(filepath)}`);
  74. }
  75. return loader;
  76. }
  77. loadedContentToCosmiconfigResult(filepath, loadedContent, forceProp) {
  78. if (loadedContent === null) {
  79. return null;
  80. }
  81. if (loadedContent === undefined) {
  82. return {
  83. filepath,
  84. config: undefined,
  85. isEmpty: true
  86. };
  87. }
  88. if (this.config.usePackagePropInConfigFiles || forceProp) {
  89. loadedContent = (0, _getPropertyByPath.getPropertyByPath)(loadedContent, this.config.packageProp);
  90. }
  91. if (loadedContent === undefined) {
  92. return {
  93. filepath,
  94. config: undefined,
  95. isEmpty: true
  96. };
  97. }
  98. return {
  99. config: loadedContent,
  100. filepath
  101. };
  102. }
  103. validateFilePath(filepath) {
  104. if (!filepath) {
  105. throw new Error('load must pass a non-empty string');
  106. }
  107. }
  108. }
  109. exports.ExplorerBase = ExplorerBase;
  110. function nextDirUp(dir) {
  111. return _path.default.dirname(dir);
  112. }
  113. function getExtensionDescription(filepath) {
  114. const ext = _path.default.extname(filepath);
  115. return ext ? `extension "${ext}"` : 'files without extensions';
  116. }
  117. //# sourceMappingURL=ExplorerBase.js.map