1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.getExtensionDescription = getExtensionDescription;
|
7 | exports.ExplorerBase = void 0;
|
8 |
|
9 | var _path = _interopRequireDefault(require("path"));
|
10 |
|
11 | var _loaders = require("./loaders");
|
12 |
|
13 | var _getPropertyByPath = require("./getPropertyByPath");
|
14 |
|
15 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
16 |
|
17 | class ExplorerBase {
|
18 | constructor(options) {
|
19 | if (options.cache === true) {
|
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 | if (result.isEmpty && this.config.ignoreEmptySearchPlaces) return false;
|
64 | return true;
|
65 | }
|
66 |
|
67 | nextDirectoryToSearch(currentDir, currentResult) {
|
68 | if (this.shouldSearchStopWithResult(currentResult)) {
|
69 | return null;
|
70 | }
|
71 |
|
72 | const nextDir = nextDirUp(currentDir);
|
73 |
|
74 | if (nextDir === currentDir || currentDir === this.config.stopDir) {
|
75 | return null;
|
76 | }
|
77 |
|
78 | return nextDir;
|
79 | }
|
80 |
|
81 | loadPackageProp(filepath, content) {
|
82 | const parsedContent = _loaders.loaders.loadJson(filepath, content);
|
83 |
|
84 | const packagePropValue = (0, _getPropertyByPath.getPropertyByPath)(parsedContent, this.config.packageProp);
|
85 | return packagePropValue || null;
|
86 | }
|
87 |
|
88 | getLoaderEntryForFile(filepath) {
|
89 | if (_path.default.basename(filepath) === 'package.json') {
|
90 | const loader = this.loadPackageProp.bind(this);
|
91 | return loader;
|
92 | }
|
93 |
|
94 | const loaderKey = _path.default.extname(filepath) || 'noExt';
|
95 | const loader = this.config.loaders[loaderKey];
|
96 |
|
97 | if (!loader) {
|
98 | throw new Error(`No loader specified for ${getExtensionDescription(filepath)}`);
|
99 | }
|
100 |
|
101 | return loader;
|
102 | }
|
103 |
|
104 | loadedContentToCosmiconfigResult(filepath, loadedContent) {
|
105 | if (loadedContent === null) {
|
106 | return null;
|
107 | }
|
108 |
|
109 | if (loadedContent === undefined) {
|
110 | return {
|
111 | filepath,
|
112 | config: undefined,
|
113 | isEmpty: true
|
114 | };
|
115 | }
|
116 |
|
117 | return {
|
118 | config: loadedContent,
|
119 | filepath
|
120 | };
|
121 | }
|
122 |
|
123 | validateFilePath(filepath) {
|
124 | if (!filepath) {
|
125 | throw new Error('load must pass a non-empty string');
|
126 | }
|
127 | }
|
128 |
|
129 | }
|
130 |
|
131 | exports.ExplorerBase = ExplorerBase;
|
132 |
|
133 | function nextDirUp(dir) {
|
134 | return _path.default.dirname(dir);
|
135 | }
|
136 |
|
137 | function getExtensionDescription(filepath) {
|
138 | const ext = _path.default.extname(filepath);
|
139 |
|
140 | return ext ? `extension "${ext}"` : 'files without extensions';
|
141 | }
|
142 |
|
\ | No newline at end of file |