ExplorerBase.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.getExtensionDescription = exports.ExplorerBase = void 0;
  7. const env_paths_1 = __importDefault(require("env-paths"));
  8. const os_1 = __importDefault(require("os"));
  9. const path_1 = __importDefault(require("path"));
  10. const util_js_1 = require("./util.js");
  11. /**
  12. * @internal
  13. */
  14. class ExplorerBase {
  15. #loadingMetaConfig = false;
  16. config;
  17. loadCache;
  18. searchCache;
  19. constructor(options) {
  20. this.config = options;
  21. if (options.cache) {
  22. this.loadCache = new Map();
  23. this.searchCache = new Map();
  24. }
  25. this.#validateConfig();
  26. }
  27. set loadingMetaConfig(value) {
  28. this.#loadingMetaConfig = value;
  29. }
  30. #validateConfig() {
  31. const config = this.config;
  32. for (const place of config.searchPlaces) {
  33. const extension = path_1.default.extname(place);
  34. const loader = this.config.loaders[extension || 'noExt'] ??
  35. this.config.loaders['default'];
  36. if (loader === undefined) {
  37. throw new Error(`Missing loader for ${getExtensionDescription(place)}.`);
  38. }
  39. if (typeof loader !== 'function') {
  40. throw new Error(`Loader for ${getExtensionDescription(place)} is not a function: Received ${typeof loader}.`);
  41. }
  42. }
  43. }
  44. clearLoadCache() {
  45. if (this.loadCache) {
  46. this.loadCache.clear();
  47. }
  48. }
  49. clearSearchCache() {
  50. if (this.searchCache) {
  51. this.searchCache.clear();
  52. }
  53. }
  54. clearCaches() {
  55. this.clearLoadCache();
  56. this.clearSearchCache();
  57. }
  58. toCosmiconfigResult(filepath, config) {
  59. if (config === null) {
  60. return null;
  61. }
  62. if (config === undefined) {
  63. return { filepath, config: undefined, isEmpty: true };
  64. }
  65. if (this.config.applyPackagePropertyPathToConfiguration ||
  66. this.#loadingMetaConfig) {
  67. const packageProp = this.config.packageProp ?? this.config.moduleName;
  68. config = (0, util_js_1.getPropertyByPath)(config, packageProp);
  69. }
  70. if (config === undefined) {
  71. return { filepath, config: undefined, isEmpty: true };
  72. }
  73. return { config, filepath };
  74. }
  75. validateImports(containingFilePath, imports, importStack) {
  76. const fileDirectory = path_1.default.dirname(containingFilePath);
  77. for (const importPath of imports) {
  78. if (typeof importPath !== 'string') {
  79. throw new Error(`${containingFilePath}: Key $import must contain a string or a list of strings`);
  80. }
  81. const fullPath = path_1.default.resolve(fileDirectory, importPath);
  82. if (fullPath === containingFilePath) {
  83. throw new Error(`Self-import detected in ${containingFilePath}`);
  84. }
  85. const idx = importStack.indexOf(fullPath);
  86. if (idx !== -1) {
  87. throw new Error(`Circular import detected:
  88. ${[...importStack, fullPath]
  89. .map((path, i) => `${i + 1}. ${path}`)
  90. .join('\n')} (same as ${idx + 1}.)`);
  91. }
  92. }
  93. }
  94. getSearchPlacesForDir(dir, globalConfigPlaces) {
  95. return (dir.isGlobalConfig ? globalConfigPlaces : this.config.searchPlaces).map((place) => path_1.default.join(dir.path, place));
  96. }
  97. getGlobalConfigDir() {
  98. return (0, env_paths_1.default)(this.config.moduleName, { suffix: '' }).config;
  99. }
  100. *getGlobalDirs(startDir) {
  101. const stopDir = path_1.default.resolve(this.config.stopDir ?? os_1.default.homedir());
  102. yield { path: startDir, isGlobalConfig: false };
  103. let currentDir = startDir;
  104. while (currentDir !== stopDir) {
  105. const parentDir = path_1.default.dirname(currentDir);
  106. /* istanbul ignore if -- @preserve */
  107. if (parentDir === currentDir) {
  108. // we're probably at the root of the directory structure
  109. break;
  110. }
  111. yield { path: parentDir, isGlobalConfig: false };
  112. currentDir = parentDir;
  113. }
  114. yield { path: this.getGlobalConfigDir(), isGlobalConfig: true };
  115. }
  116. }
  117. exports.ExplorerBase = ExplorerBase;
  118. /**
  119. * @internal
  120. */
  121. function getExtensionDescription(extension) {
  122. /* istanbul ignore next -- @preserve */
  123. return extension ? `extension "${extension}"` : 'files without extensions';
  124. }
  125. exports.getExtensionDescription = getExtensionDescription;
  126. //# sourceMappingURL=ExplorerBase.js.map