Explorer.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.Explorer = void 0;
  6. var _path = _interopRequireDefault(require("path"));
  7. var _cacheWrapper = require("./cacheWrapper");
  8. var _ExplorerBase = require("./ExplorerBase");
  9. var _getDirectory = require("./getDirectory");
  10. var _readFile = require("./readFile");
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. class Explorer extends _ExplorerBase.ExplorerBase {
  13. constructor(options) {
  14. super(options);
  15. }
  16. async search(searchFrom = process.cwd()) {
  17. if (this.config.metaConfigFilePath) {
  18. const config = await this._loadFile(this.config.metaConfigFilePath, true);
  19. if (config && !config.isEmpty) {
  20. return config;
  21. }
  22. }
  23. return await this.searchFromDirectory(await (0, _getDirectory.getDirectory)(searchFrom));
  24. }
  25. async searchFromDirectory(dir) {
  26. const absoluteDir = _path.default.resolve(process.cwd(), dir);
  27. const run = async () => {
  28. const result = await this.searchDirectory(absoluteDir);
  29. const nextDir = this.nextDirectoryToSearch(absoluteDir, result);
  30. if (nextDir) {
  31. return this.searchFromDirectory(nextDir);
  32. }
  33. return await this.config.transform(result);
  34. };
  35. if (this.searchCache) {
  36. return (0, _cacheWrapper.cacheWrapper)(this.searchCache, absoluteDir, run);
  37. }
  38. return run();
  39. }
  40. async searchDirectory(dir) {
  41. for await (const place of this.config.searchPlaces) {
  42. const placeResult = await this.loadSearchPlace(dir, place);
  43. if (this.shouldSearchStopWithResult(placeResult)) {
  44. return placeResult;
  45. }
  46. } // config not found
  47. return null;
  48. }
  49. async loadSearchPlace(dir, place) {
  50. const filepath = _path.default.join(dir, place);
  51. const fileContents = await (0, _readFile.readFile)(filepath);
  52. return await this.createCosmiconfigResult(filepath, fileContents, false);
  53. }
  54. async loadFileContent(filepath, content) {
  55. if (content === null) {
  56. return null;
  57. }
  58. if (content.trim() === '') {
  59. return undefined;
  60. }
  61. const loader = this.getLoaderEntryForFile(filepath);
  62. try {
  63. return await loader(filepath, content);
  64. } catch (e) {
  65. e.filepath = filepath;
  66. throw e;
  67. }
  68. }
  69. async createCosmiconfigResult(filepath, content, forceProp) {
  70. const fileContent = await this.loadFileContent(filepath, content);
  71. return this.loadedContentToCosmiconfigResult(filepath, fileContent, forceProp);
  72. }
  73. async load(filepath) {
  74. return this._loadFile(filepath, false);
  75. }
  76. async _loadFile(filepath, forceProp) {
  77. this.validateFilePath(filepath);
  78. const absoluteFilePath = _path.default.resolve(process.cwd(), filepath);
  79. const runLoad = async () => {
  80. const fileContents = await (0, _readFile.readFile)(absoluteFilePath, {
  81. throwNotFound: true
  82. });
  83. const result = await this.createCosmiconfigResult(absoluteFilePath, fileContents, forceProp);
  84. return await this.config.transform(result);
  85. };
  86. if (this.loadCache) {
  87. return (0, _cacheWrapper.cacheWrapper)(this.loadCache, absoluteFilePath, runLoad);
  88. }
  89. return runLoad();
  90. }
  91. }
  92. exports.Explorer = Explorer;
  93. //# sourceMappingURL=Explorer.js.map