1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.Explorer = void 0;
|
---|
7 |
|
---|
8 | var _path = _interopRequireDefault(require("path"));
|
---|
9 |
|
---|
10 | var _ExplorerBase = require("./ExplorerBase");
|
---|
11 |
|
---|
12 | var _readFile = require("./readFile");
|
---|
13 |
|
---|
14 | var _cacheWrapper = require("./cacheWrapper");
|
---|
15 |
|
---|
16 | var _getDirectory = require("./getDirectory");
|
---|
17 |
|
---|
18 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
19 |
|
---|
20 | class Explorer extends _ExplorerBase.ExplorerBase {
|
---|
21 | constructor(options) {
|
---|
22 | super(options);
|
---|
23 | }
|
---|
24 |
|
---|
25 | async search(searchFrom = process.cwd()) {
|
---|
26 | const startDirectory = await (0, _getDirectory.getDirectory)(searchFrom);
|
---|
27 | const result = await this.searchFromDirectory(startDirectory);
|
---|
28 | return result;
|
---|
29 | }
|
---|
30 |
|
---|
31 | async searchFromDirectory(dir) {
|
---|
32 | const absoluteDir = _path.default.resolve(process.cwd(), dir);
|
---|
33 |
|
---|
34 | const run = async () => {
|
---|
35 | const result = await this.searchDirectory(absoluteDir);
|
---|
36 | const nextDir = this.nextDirectoryToSearch(absoluteDir, result);
|
---|
37 |
|
---|
38 | if (nextDir) {
|
---|
39 | return this.searchFromDirectory(nextDir);
|
---|
40 | }
|
---|
41 |
|
---|
42 | const transformResult = await this.config.transform(result);
|
---|
43 | return transformResult;
|
---|
44 | };
|
---|
45 |
|
---|
46 | if (this.searchCache) {
|
---|
47 | return (0, _cacheWrapper.cacheWrapper)(this.searchCache, absoluteDir, run);
|
---|
48 | }
|
---|
49 |
|
---|
50 | return run();
|
---|
51 | }
|
---|
52 |
|
---|
53 | async searchDirectory(dir) {
|
---|
54 | for await (const place of this.config.searchPlaces) {
|
---|
55 | const placeResult = await this.loadSearchPlace(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 | async loadSearchPlace(dir, place) {
|
---|
67 | const filepath = _path.default.join(dir, place);
|
---|
68 |
|
---|
69 | const fileContents = await (0, _readFile.readFile)(filepath);
|
---|
70 | const result = await this.createCosmiconfigResult(filepath, fileContents);
|
---|
71 | return result;
|
---|
72 | }
|
---|
73 |
|
---|
74 | async loadFileContent(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 = await loader(filepath, content);
|
---|
85 | return loaderResult;
|
---|
86 | }
|
---|
87 |
|
---|
88 | async createCosmiconfigResult(filepath, content) {
|
---|
89 | const fileContent = await this.loadFileContent(filepath, content);
|
---|
90 | const result = this.loadedContentToCosmiconfigResult(filepath, fileContent);
|
---|
91 | return result;
|
---|
92 | }
|
---|
93 |
|
---|
94 | async load(filepath) {
|
---|
95 | this.validateFilePath(filepath);
|
---|
96 |
|
---|
97 | const absoluteFilePath = _path.default.resolve(process.cwd(), filepath);
|
---|
98 |
|
---|
99 | const runLoad = async () => {
|
---|
100 | const fileContents = await (0, _readFile.readFile)(absoluteFilePath, {
|
---|
101 | throwNotFound: true
|
---|
102 | });
|
---|
103 | const result = await this.createCosmiconfigResult(absoluteFilePath, fileContents);
|
---|
104 | const transformResult = await this.config.transform(result);
|
---|
105 | return transformResult;
|
---|
106 | };
|
---|
107 |
|
---|
108 | if (this.loadCache) {
|
---|
109 | return (0, _cacheWrapper.cacheWrapper)(this.loadCache, absoluteFilePath, runLoad);
|
---|
110 | }
|
---|
111 |
|
---|
112 | return runLoad();
|
---|
113 | }
|
---|
114 |
|
---|
115 | }
|
---|
116 |
|
---|
117 | exports.Explorer = Explorer;
|
---|
118 | //# sourceMappingURL=Explorer.js.map |
---|