ExplorerSync.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 _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 ExplorerSync extends _ExplorerBase.ExplorerBase {
  13. constructor(options) {
  14. super(options);
  15. }
  16. searchSync(searchFrom = process.cwd()) {
  17. if (this.config.metaConfigFilePath) {
  18. const config = this._loadFileSync(this.config.metaConfigFilePath, true);
  19. if (config && !config.isEmpty) {
  20. return config;
  21. }
  22. }
  23. return this.searchFromDirectorySync((0, _getDirectory.getDirectorySync)(searchFrom));
  24. }
  25. searchFromDirectorySync(dir) {
  26. const absoluteDir = _path.default.resolve(process.cwd(), dir);
  27. const run = () => {
  28. const result = this.searchDirectorySync(absoluteDir);
  29. const nextDir = this.nextDirectoryToSearch(absoluteDir, result);
  30. if (nextDir) {
  31. return this.searchFromDirectorySync(nextDir);
  32. }
  33. return this.config.transform(result);
  34. };
  35. if (this.searchCache) {
  36. return (0, _cacheWrapper.cacheWrapperSync)(this.searchCache, absoluteDir, run);
  37. }
  38. return run();
  39. }
  40. searchDirectorySync(dir) {
  41. for (const place of this.config.searchPlaces) {
  42. const placeResult = this.loadSearchPlaceSync(dir, place);
  43. if (this.shouldSearchStopWithResult(placeResult)) {
  44. return placeResult;
  45. }
  46. } // config not found
  47. return null;
  48. }
  49. loadSearchPlaceSync(dir, place) {
  50. const filepath = _path.default.join(dir, place);
  51. const content = (0, _readFile.readFileSync)(filepath);
  52. return this.createCosmiconfigResultSync(filepath, content, false);
  53. }
  54. loadFileContentSync(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 loader(filepath, content);
  64. } catch (e) {
  65. e.filepath = filepath;
  66. throw e;
  67. }
  68. }
  69. createCosmiconfigResultSync(filepath, content, forceProp) {
  70. const fileContent = this.loadFileContentSync(filepath, content);
  71. return this.loadedContentToCosmiconfigResult(filepath, fileContent, forceProp);
  72. }
  73. loadSync(filepath) {
  74. return this._loadFileSync(filepath, false);
  75. }
  76. _loadFileSync(filepath, forceProp) {
  77. this.validateFilePath(filepath);
  78. const absoluteFilePath = _path.default.resolve(process.cwd(), filepath);
  79. const runLoadSync = () => {
  80. const content = (0, _readFile.readFileSync)(absoluteFilePath, {
  81. throwNotFound: true
  82. });
  83. const cosmiconfigResult = this.createCosmiconfigResultSync(absoluteFilePath, content, forceProp);
  84. return this.config.transform(cosmiconfigResult);
  85. };
  86. if (this.loadCache) {
  87. return (0, _cacheWrapper.cacheWrapperSync)(this.loadCache, absoluteFilePath, runLoadSync);
  88. }
  89. return runLoadSync();
  90. }
  91. }
  92. exports.ExplorerSync = ExplorerSync;
  93. //# sourceMappingURL=ExplorerSync.js.map