ExplorerSync.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.ExplorerSync = 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 ExplorerSync extends _ExplorerBase.ExplorerBase {
  13. constructor(options) {
  14. super(options);
  15. }
  16. searchSync(searchFrom = process.cwd()) {
  17. const startDirectory = (0, _getDirectory.getDirectorySync)(searchFrom);
  18. const result = this.searchFromDirectorySync(startDirectory);
  19. return result;
  20. }
  21. searchFromDirectorySync(dir) {
  22. const absoluteDir = _path.default.resolve(process.cwd(), dir);
  23. const run = () => {
  24. const result = this.searchDirectorySync(absoluteDir);
  25. const nextDir = this.nextDirectoryToSearch(absoluteDir, result);
  26. if (nextDir) {
  27. return this.searchFromDirectorySync(nextDir);
  28. }
  29. const transformResult = this.config.transform(result);
  30. return transformResult;
  31. };
  32. if (this.searchCache) {
  33. return (0, _cacheWrapper.cacheWrapperSync)(this.searchCache, absoluteDir, run);
  34. }
  35. return run();
  36. }
  37. searchDirectorySync(dir) {
  38. for (const place of this.config.searchPlaces) {
  39. const placeResult = this.loadSearchPlaceSync(dir, place);
  40. if (this.shouldSearchStopWithResult(placeResult) === true) {
  41. return placeResult;
  42. }
  43. } // config not found
  44. return null;
  45. }
  46. loadSearchPlaceSync(dir, place) {
  47. const filepath = _path.default.join(dir, place);
  48. const content = (0, _readFile.readFileSync)(filepath);
  49. const result = this.createCosmiconfigResultSync(filepath, content);
  50. return result;
  51. }
  52. loadFileContentSync(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 = loader(filepath, content);
  61. return loaderResult;
  62. }
  63. createCosmiconfigResultSync(filepath, content) {
  64. const fileContent = this.loadFileContentSync(filepath, content);
  65. const result = this.loadedContentToCosmiconfigResult(filepath, fileContent);
  66. return result;
  67. }
  68. loadSync(filepath) {
  69. this.validateFilePath(filepath);
  70. const absoluteFilePath = _path.default.resolve(process.cwd(), filepath);
  71. const runLoadSync = () => {
  72. const content = (0, _readFile.readFileSync)(absoluteFilePath, {
  73. throwNotFound: true
  74. });
  75. const cosmiconfigResult = this.createCosmiconfigResultSync(absoluteFilePath, content);
  76. const transformResult = this.config.transform(cosmiconfigResult);
  77. return transformResult;
  78. };
  79. if (this.loadCache) {
  80. return (0, _cacheWrapper.cacheWrapperSync)(this.loadCache, absoluteFilePath, runLoadSync);
  81. }
  82. return runLoadSync();
  83. }
  84. }
  85. exports.ExplorerSync = ExplorerSync;
  86. //# sourceMappingURL=ExplorerSync.js.map