ExplorerBase.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getExtensionDescription = getExtensionDescription;
  6. exports.ExplorerBase = void 0;
  7. var _path = _interopRequireDefault(require("path"));
  8. var _loaders = require("./loaders");
  9. var _getPropertyByPath = require("./getPropertyByPath");
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. class ExplorerBase {
  12. constructor(options) {
  13. if (options.cache === true) {
  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. if (result.isEmpty && this.config.ignoreEmptySearchPlaces) return false;
  50. return true;
  51. }
  52. nextDirectoryToSearch(currentDir, currentResult) {
  53. if (this.shouldSearchStopWithResult(currentResult)) {
  54. return null;
  55. }
  56. const nextDir = nextDirUp(currentDir);
  57. if (nextDir === currentDir || currentDir === this.config.stopDir) {
  58. return null;
  59. }
  60. return nextDir;
  61. }
  62. loadPackageProp(filepath, content) {
  63. const parsedContent = _loaders.loaders.loadJson(filepath, content);
  64. const packagePropValue = (0, _getPropertyByPath.getPropertyByPath)(parsedContent, this.config.packageProp);
  65. return packagePropValue || null;
  66. }
  67. getLoaderEntryForFile(filepath) {
  68. if (_path.default.basename(filepath) === 'package.json') {
  69. const loader = this.loadPackageProp.bind(this);
  70. return loader;
  71. }
  72. const loaderKey = _path.default.extname(filepath) || 'noExt';
  73. const loader = this.config.loaders[loaderKey];
  74. if (!loader) {
  75. throw new Error(`No loader specified for ${getExtensionDescription(filepath)}`);
  76. }
  77. return loader;
  78. }
  79. loadedContentToCosmiconfigResult(filepath, loadedContent) {
  80. if (loadedContent === null) {
  81. return null;
  82. }
  83. if (loadedContent === undefined) {
  84. return {
  85. filepath,
  86. config: undefined,
  87. isEmpty: true
  88. };
  89. }
  90. return {
  91. config: loadedContent,
  92. filepath
  93. };
  94. }
  95. validateFilePath(filepath) {
  96. if (!filepath) {
  97. throw new Error('load must pass a non-empty string');
  98. }
  99. }
  100. }
  101. exports.ExplorerBase = ExplorerBase;
  102. function nextDirUp(dir) {
  103. return _path.default.dirname(dir);
  104. }
  105. function getExtensionDescription(filepath) {
  106. const ext = _path.default.extname(filepath);
  107. return ext ? `extension "${ext}"` : 'files without extensions';
  108. }
  109. //# sourceMappingURL=ExplorerBase.js.map