UNPKG

3.31 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.Explorer = 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 Explorer extends _ExplorerBase.ExplorerBase {
21 constructor(options) {
22 super(options);
23 }
24
25 async search(searchFrom = process.cwd()) {
26 if (this.config.metaConfigFilePath) {
27 const config = await this._loadFile(this.config.metaConfigFilePath, true);
28
29 if (config && !config.isEmpty) {
30 return config;
31 }
32 }
33
34 return await this.searchFromDirectory(await (0, _getDirectory.getDirectory)(searchFrom));
35 }
36
37 async searchFromDirectory(dir) {
38 const absoluteDir = _path.default.resolve(process.cwd(), dir);
39
40 const run = async () => {
41 const result = await this.searchDirectory(absoluteDir);
42 const nextDir = this.nextDirectoryToSearch(absoluteDir, result);
43
44 if (nextDir) {
45 return this.searchFromDirectory(nextDir);
46 }
47
48 return await this.config.transform(result);
49 };
50
51 if (this.searchCache) {
52 return (0, _cacheWrapper.cacheWrapper)(this.searchCache, absoluteDir, run);
53 }
54
55 return run();
56 }
57
58 async searchDirectory(dir) {
59 for await (const place of this.config.searchPlaces) {
60 const placeResult = await this.loadSearchPlace(dir, place);
61
62 if (this.shouldSearchStopWithResult(placeResult)) {
63 return placeResult;
64 }
65 } // config not found
66
67
68 return null;
69 }
70
71 async loadSearchPlace(dir, place) {
72 const filepath = _path.default.join(dir, place);
73
74 const fileContents = await (0, _readFile.readFile)(filepath);
75 return await this.createCosmiconfigResult(filepath, fileContents, false);
76 }
77
78 async loadFileContent(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 await loader(filepath, content);
91 } catch (e) {
92 e.filepath = filepath;
93 throw e;
94 }
95 }
96
97 async createCosmiconfigResult(filepath, content, forceProp) {
98 const fileContent = await this.loadFileContent(filepath, content);
99 return this.loadedContentToCosmiconfigResult(filepath, fileContent, forceProp);
100 }
101
102 async load(filepath) {
103 return this._loadFile(filepath, false);
104 }
105
106 async _loadFile(filepath, forceProp) {
107 this.validateFilePath(filepath);
108
109 const absoluteFilePath = _path.default.resolve(process.cwd(), filepath);
110
111 const runLoad = async () => {
112 const fileContents = await (0, _readFile.readFile)(absoluteFilePath, {
113 throwNotFound: true
114 });
115 const result = await this.createCosmiconfigResult(absoluteFilePath, fileContents, forceProp);
116 return await this.config.transform(result);
117 };
118
119 if (this.loadCache) {
120 return (0, _cacheWrapper.cacheWrapper)(this.loadCache, absoluteFilePath, runLoad);
121 }
122
123 return runLoad();
124 }
125
126}
127
128exports.Explorer = Explorer;
129//# sourceMappingURL=Explorer.js.map
\No newline at end of file