1
0

Explorer.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 _ExplorerBase = require("./ExplorerBase");
  8. var _readFile = require("./readFile");
  9. var _cacheWrapper = require("./cacheWrapper");
  10. var _getDirectory = require("./getDirectory");
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. function _asyncIterator(iterable) { var method; if (typeof Symbol !== "undefined") { if (Symbol.asyncIterator) { method = iterable[Symbol.asyncIterator]; if (method != null) return method.call(iterable); } if (Symbol.iterator) { method = iterable[Symbol.iterator]; if (method != null) return method.call(iterable); } } throw new TypeError("Object is not async iterable"); }
  13. class Explorer extends _ExplorerBase.ExplorerBase {
  14. constructor(options) {
  15. super(options);
  16. }
  17. async search(searchFrom = process.cwd()) {
  18. const startDirectory = await (0, _getDirectory.getDirectory)(searchFrom);
  19. const result = await this.searchFromDirectory(startDirectory);
  20. return result;
  21. }
  22. async searchFromDirectory(dir) {
  23. const absoluteDir = _path.default.resolve(process.cwd(), dir);
  24. const run = async () => {
  25. const result = await this.searchDirectory(absoluteDir);
  26. const nextDir = this.nextDirectoryToSearch(absoluteDir, result);
  27. if (nextDir) {
  28. return this.searchFromDirectory(nextDir);
  29. }
  30. const transformResult = await this.config.transform(result);
  31. return transformResult;
  32. };
  33. if (this.searchCache) {
  34. return (0, _cacheWrapper.cacheWrapper)(this.searchCache, absoluteDir, run);
  35. }
  36. return run();
  37. }
  38. async searchDirectory(dir) {
  39. var _iteratorNormalCompletion = true;
  40. var _didIteratorError = false;
  41. var _iteratorError;
  42. try {
  43. for (var _iterator = _asyncIterator(this.config.searchPlaces), _step, _value; _step = await _iterator.next(), _iteratorNormalCompletion = _step.done, _value = await _step.value, !_iteratorNormalCompletion; _iteratorNormalCompletion = true) {
  44. const place = _value;
  45. const placeResult = await this.loadSearchPlace(dir, place);
  46. if (this.shouldSearchStopWithResult(placeResult) === true) {
  47. return placeResult;
  48. }
  49. } // config not found
  50. } catch (err) {
  51. _didIteratorError = true;
  52. _iteratorError = err;
  53. } finally {
  54. try {
  55. if (!_iteratorNormalCompletion && _iterator.return != null) {
  56. await _iterator.return();
  57. }
  58. } finally {
  59. if (_didIteratorError) {
  60. throw _iteratorError;
  61. }
  62. }
  63. }
  64. return null;
  65. }
  66. async loadSearchPlace(dir, place) {
  67. const filepath = _path.default.join(dir, place);
  68. const fileContents = await (0, _readFile.readFile)(filepath);
  69. const result = await this.createCosmiconfigResult(filepath, fileContents);
  70. return result;
  71. }
  72. async loadFileContent(filepath, content) {
  73. if (content === null) {
  74. return null;
  75. }
  76. if (content.trim() === '') {
  77. return undefined;
  78. }
  79. const loader = this.getLoaderEntryForFile(filepath);
  80. const loaderResult = await loader(filepath, content);
  81. return loaderResult;
  82. }
  83. async createCosmiconfigResult(filepath, content) {
  84. const fileContent = await this.loadFileContent(filepath, content);
  85. const result = this.loadedContentToCosmiconfigResult(filepath, fileContent);
  86. return result;
  87. }
  88. async load(filepath) {
  89. this.validateFilePath(filepath);
  90. const absoluteFilePath = _path.default.resolve(process.cwd(), filepath);
  91. const runLoad = async () => {
  92. const fileContents = await (0, _readFile.readFile)(absoluteFilePath, {
  93. throwNotFound: true
  94. });
  95. const result = await this.createCosmiconfigResult(absoluteFilePath, fileContents);
  96. const transformResult = await this.config.transform(result);
  97. return transformResult;
  98. };
  99. if (this.loadCache) {
  100. return (0, _cacheWrapper.cacheWrapper)(this.loadCache, absoluteFilePath, runLoad);
  101. }
  102. return runLoad();
  103. }
  104. }
  105. exports.Explorer = Explorer;
  106. //# sourceMappingURL=Explorer.js.map