UNPKG

3.28 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 _cacheWrapper = require("./cacheWrapper");
11
12var _ExplorerBase = require("./ExplorerBase");
13
14var _getDirectory = require("./getDirectory");
15
16var _readFile = require("./readFile");
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 if (this.config.metaConfigFilePath) {
27 const config = this._loadFileSync(this.config.metaConfigFilePath, true);
28
29 if (config && !config.isEmpty) {
30 return config;
31 }
32 }
33
34 return this.searchFromDirectorySync((0, _getDirectory.getDirectorySync)(searchFrom));
35 }
36
37 searchFromDirectorySync(dir) {
38 const absoluteDir = _path.default.resolve(process.cwd(), dir);
39
40 const run = () => {
41 const result = this.searchDirectorySync(absoluteDir);
42 const nextDir = this.nextDirectoryToSearch(absoluteDir, result);
43
44 if (nextDir) {
45 return this.searchFromDirectorySync(nextDir);
46 }
47
48 return this.config.transform(result);
49 };
50
51 if (this.searchCache) {
52 return (0, _cacheWrapper.cacheWrapperSync)(this.searchCache, absoluteDir, run);
53 }
54
55 return run();
56 }
57
58 searchDirectorySync(dir) {
59 for (const place of this.config.searchPlaces) {
60 const placeResult = this.loadSearchPlaceSync(dir, place);
61
62 if (this.shouldSearchStopWithResult(placeResult)) {
63 return placeResult;
64 }
65 } // config not found
66
67
68 return null;
69 }
70
71 loadSearchPlaceSync(dir, place) {
72 const filepath = _path.default.join(dir, place);
73
74 const content = (0, _readFile.readFileSync)(filepath);
75 return this.createCosmiconfigResultSync(filepath, content, false);
76 }
77
78 loadFileContentSync(filepath, content) {
79 if (content === null) {
80 return null;
81 }
82
83 if (content.trim() === '') {
84 return undefined;
85 }
86
87 const loader = this.getLoaderEntryForFile(filepath);
88
89 try {
90 return loader(filepath, content);
91 } catch (e) {
92 e.filepath = filepath;
93 throw e;
94 }
95 }
96
97 createCosmiconfigResultSync(filepath, content, forceProp) {
98 const fileContent = this.loadFileContentSync(filepath, content);
99 return this.loadedContentToCosmiconfigResult(filepath, fileContent, forceProp);
100 }
101
102 loadSync(filepath) {
103 return this._loadFileSync(filepath, false);
104 }
105
106 _loadFileSync(filepath, forceProp) {
107 this.validateFilePath(filepath);
108
109 const absoluteFilePath = _path.default.resolve(process.cwd(), filepath);
110
111 const runLoadSync = () => {
112 const content = (0, _readFile.readFileSync)(absoluteFilePath, {
113 throwNotFound: true
114 });
115 const cosmiconfigResult = this.createCosmiconfigResultSync(absoluteFilePath, content, forceProp);
116 return this.config.transform(cosmiconfigResult);
117 };
118
119 if (this.loadCache) {
120 return (0, _cacheWrapper.cacheWrapperSync)(this.loadCache, absoluteFilePath, runLoadSync);
121 }
122
123 return runLoadSync();
124 }
125
126}
127
128exports.ExplorerSync = ExplorerSync;
129//# sourceMappingURL=ExplorerSync.js.map
\No newline at end of file