Explorer.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. class Explorer extends _ExplorerBase.ExplorerBase {
  13. constructor(options) {
  14. super(options);
  15. }
  16. async search(searchFrom = process.cwd()) {
  17. const startDirectory = await (0, _getDirectory.getDirectory)(searchFrom);
  18. const result = await this.searchFromDirectory(startDirectory);
  19. return result;
  20. }
  21. async searchFromDirectory(dir) {
  22. const absoluteDir = _path.default.resolve(process.cwd(), dir);
  23. const run = async () => {
  24. const result = await this.searchDirectory(absoluteDir);
  25. const nextDir = this.nextDirectoryToSearch(absoluteDir, result);
  26. if (nextDir) {
  27. return this.searchFromDirectory(nextDir);
  28. }
  29. const transformResult = await this.config.transform(result);
  30. return transformResult;
  31. };
  32. if (this.searchCache) {
  33. return (0, _cacheWrapper.cacheWrapper)(this.searchCache, absoluteDir, run);
  34. }
  35. return run();
  36. }
  37. async searchDirectory(dir) {
  38. for await (const place of this.config.searchPlaces) {
  39. const placeResult = await this.loadSearchPlace(dir, place);
  40. if (this.shouldSearchStopWithResult(placeResult) === true) {
  41. return placeResult;
  42. }
  43. } // config not found
  44. return null;
  45. }
  46. async loadSearchPlace(dir, place) {
  47. const filepath = _path.default.join(dir, place);
  48. const fileContents = await (0, _readFile.readFile)(filepath);
  49. const result = await this.createCosmiconfigResult(filepath, fileContents);
  50. return result;
  51. }
  52. async loadFileContent(filepath, content) {
  53. if (content === null) {
  54. return null;
  55. }
  56. if (content.trim() === '') {
  57. return undefined;
  58. }
  59. const loader = this.getLoaderEntryForFile(filepath);
  60. const loaderResult = await loader(filepath, content);
  61. return loaderResult;
  62. }
  63. async createCosmiconfigResult(filepath, content) {
  64. const fileContent = await this.loadFileContent(filepath, content);
  65. const result = this.loadedContentToCosmiconfigResult(filepath, fileContent);
  66. return result;
  67. }
  68. async load(filepath) {
  69. this.validateFilePath(filepath);
  70. const absoluteFilePath = _path.default.resolve(process.cwd(), filepath);
  71. const runLoad = async () => {
  72. const fileContents = await (0, _readFile.readFile)(absoluteFilePath, {
  73. throwNotFound: true
  74. });
  75. const result = await this.createCosmiconfigResult(absoluteFilePath, fileContents);
  76. const transformResult = await this.config.transform(result);
  77. return transformResult;
  78. };
  79. if (this.loadCache) {
  80. return (0, _cacheWrapper.cacheWrapper)(this.loadCache, absoluteFilePath, runLoad);
  81. }
  82. return runLoad();
  83. }
  84. }
  85. exports.Explorer = Explorer;
  86. //# sourceMappingURL=Explorer.js.map