1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.ExplorerBase = void 0;
|
7 | exports.getExtensionDescription = getExtensionDescription;
|
8 |
|
9 | var _path = _interopRequireDefault(require("path"));
|
10 |
|
11 | var _getPropertyByPath = require("./getPropertyByPath");
|
12 |
|
13 | var _loaders = require("./loaders");
|
14 |
|
15 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
16 |
|
17 | class ExplorerBase {
|
18 | constructor(options) {
|
19 | if (options.cache) {
|
20 | this.loadCache = new Map();
|
21 | this.searchCache = new Map();
|
22 | }
|
23 |
|
24 | this.config = options;
|
25 | this.validateConfig();
|
26 | }
|
27 |
|
28 | clearLoadCache() {
|
29 | if (this.loadCache) {
|
30 | this.loadCache.clear();
|
31 | }
|
32 | }
|
33 |
|
34 | clearSearchCache() {
|
35 | if (this.searchCache) {
|
36 | this.searchCache.clear();
|
37 | }
|
38 | }
|
39 |
|
40 | clearCaches() {
|
41 | this.clearLoadCache();
|
42 | this.clearSearchCache();
|
43 | }
|
44 |
|
45 | validateConfig() {
|
46 | const config = this.config;
|
47 | config.searchPlaces.forEach(place => {
|
48 | const loaderKey = _path.default.extname(place) || 'noExt';
|
49 | const loader = config.loaders[loaderKey];
|
50 |
|
51 | if (!loader) {
|
52 | throw new Error(`No loader specified for ${getExtensionDescription(place)}, so searchPlaces item "${place}" is invalid`);
|
53 | }
|
54 |
|
55 | if (typeof loader !== 'function') {
|
56 | throw new Error(`loader for ${getExtensionDescription(place)} is not a function (type provided: "${typeof loader}"), so searchPlaces item "${place}" is invalid`);
|
57 | }
|
58 | });
|
59 | }
|
60 |
|
61 | shouldSearchStopWithResult(result) {
|
62 | if (result === null) return false;
|
63 | return !(result.isEmpty && this.config.ignoreEmptySearchPlaces);
|
64 | }
|
65 |
|
66 | nextDirectoryToSearch(currentDir, currentResult) {
|
67 | if (this.shouldSearchStopWithResult(currentResult)) {
|
68 | return null;
|
69 | }
|
70 |
|
71 | const nextDir = nextDirUp(currentDir);
|
72 |
|
73 | if (nextDir === currentDir || currentDir === this.config.stopDir) {
|
74 | return null;
|
75 | }
|
76 |
|
77 | return nextDir;
|
78 | }
|
79 |
|
80 | loadPackageProp(filepath, content) {
|
81 | const parsedContent = _loaders.loaders.loadJson(filepath, content);
|
82 |
|
83 | const packagePropValue = (0, _getPropertyByPath.getPropertyByPath)(parsedContent, this.config.packageProp);
|
84 | return packagePropValue || null;
|
85 | }
|
86 |
|
87 | getLoaderEntryForFile(filepath) {
|
88 | if (_path.default.basename(filepath) === 'package.json') {
|
89 | return this.loadPackageProp.bind(this);
|
90 | }
|
91 |
|
92 | const loaderKey = _path.default.extname(filepath) || 'noExt';
|
93 | const loader = this.config.loaders[loaderKey];
|
94 |
|
95 | if (!loader) {
|
96 | throw new Error(`No loader specified for ${getExtensionDescription(filepath)}`);
|
97 | }
|
98 |
|
99 | return loader;
|
100 | }
|
101 |
|
102 | loadedContentToCosmiconfigResult(filepath, loadedContent, forceProp) {
|
103 | if (loadedContent === null) {
|
104 | return null;
|
105 | }
|
106 |
|
107 | if (loadedContent === undefined) {
|
108 | return {
|
109 | filepath,
|
110 | config: undefined,
|
111 | isEmpty: true
|
112 | };
|
113 | }
|
114 |
|
115 | if (this.config.usePackagePropInConfigFiles || forceProp) {
|
116 | loadedContent = (0, _getPropertyByPath.getPropertyByPath)(loadedContent, this.config.packageProp);
|
117 | }
|
118 |
|
119 | if (loadedContent === undefined) {
|
120 | return {
|
121 | filepath,
|
122 | config: undefined,
|
123 | isEmpty: true
|
124 | };
|
125 | }
|
126 |
|
127 | return {
|
128 | config: loadedContent,
|
129 | filepath
|
130 | };
|
131 | }
|
132 |
|
133 | validateFilePath(filepath) {
|
134 | if (!filepath) {
|
135 | throw new Error('load must pass a non-empty string');
|
136 | }
|
137 | }
|
138 |
|
139 | }
|
140 |
|
141 | exports.ExplorerBase = ExplorerBase;
|
142 |
|
143 | function nextDirUp(dir) {
|
144 | return _path.default.dirname(dir);
|
145 | }
|
146 |
|
147 | function getExtensionDescription(filepath) {
|
148 | const ext = _path.default.extname(filepath);
|
149 |
|
150 | return ext ? `extension "${ext}"` : 'files without extensions';
|
151 | }
|
152 |
|
\ | No newline at end of file |