UNPKG

3.14 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.ExplorerSync = void 0;
7
8var _path = _interopRequireDefault(require("path"));
9
10var _ExplorerBase = require("./ExplorerBase");
11
12var _readFile = require("./readFile");
13
14var _cacheWrapper = require("./cacheWrapper");
15
16var _getDirectory = require("./getDirectory");
17
18function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
20class ExplorerSync extends _ExplorerBase.ExplorerBase {
21 constructor(options) {
22 super(options);
23 }
24
25 searchSync(searchFrom = process.cwd()) {
26 const startDirectory = (0, _getDirectory.getDirectorySync)(searchFrom);
27 const result = this.searchFromDirectorySync(startDirectory);
28 return result;
29 }
30
31 searchFromDirectorySync(dir) {
32 const absoluteDir = _path.default.resolve(process.cwd(), dir);
33
34 const run = () => {
35 const result = this.searchDirectorySync(absoluteDir);
36 const nextDir = this.nextDirectoryToSearch(absoluteDir, result);
37
38 if (nextDir) {
39 return this.searchFromDirectorySync(nextDir);
40 }
41
42 const transformResult = this.config.transform(result);
43 return transformResult;
44 };
45
46 if (this.searchCache) {
47 return (0, _cacheWrapper.cacheWrapperSync)(this.searchCache, absoluteDir, run);
48 }
49
50 return run();
51 }
52
53 searchDirectorySync(dir) {
54 for (const place of this.config.searchPlaces) {
55 const placeResult = this.loadSearchPlaceSync(dir, place);
56
57 if (this.shouldSearchStopWithResult(placeResult) === true) {
58 return placeResult;
59 }
60 } // config not found
61
62
63 return null;
64 }
65
66 loadSearchPlaceSync(dir, place) {
67 const filepath = _path.default.join(dir, place);
68
69 const content = (0, _readFile.readFileSync)(filepath);
70 const result = this.createCosmiconfigResultSync(filepath, content);
71 return result;
72 }
73
74 loadFileContentSync(filepath, content) {
75 if (content === null) {
76 return null;
77 }
78
79 if (content.trim() === '') {
80 return undefined;
81 }
82
83 const loader = this.getLoaderEntryForFile(filepath);
84 const loaderResult = loader(filepath, content);
85 return loaderResult;
86 }
87
88 createCosmiconfigResultSync(filepath, content) {
89 const fileContent = this.loadFileContentSync(filepath, content);
90 const result = this.loadedContentToCosmiconfigResult(filepath, fileContent);
91 return result;
92 }
93
94 loadSync(filepath) {
95 this.validateFilePath(filepath);
96
97 const absoluteFilePath = _path.default.resolve(process.cwd(), filepath);
98
99 const runLoadSync = () => {
100 const content = (0, _readFile.readFileSync)(absoluteFilePath, {
101 throwNotFound: true
102 });
103 const cosmiconfigResult = this.createCosmiconfigResultSync(absoluteFilePath, content);
104 const transformResult = this.config.transform(cosmiconfigResult);
105 return transformResult;
106 };
107
108 if (this.loadCache) {
109 return (0, _cacheWrapper.cacheWrapperSync)(this.loadCache, absoluteFilePath, runLoadSync);
110 }
111
112 return runLoadSync();
113 }
114
115}
116
117exports.ExplorerSync = ExplorerSync;
118//# sourceMappingURL=ExplorerSync.js.map
\No newline at end of file