createExplorer.js
4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
'use strict';
const path = require('path');
const loadPackageProp = require('./loadPackageProp');
const loadRc = require('./loadRc');
const loadJs = require('./loadJs');
const loadDefinedFile = require('./loadDefinedFile');
const funcRunner = require('./funcRunner');
const getDirectory = require('./getDirectory');
module.exports = function createExplorer(options
) {
// When `options.sync` is `false` (default),
// these cache Promises that resolve with results, not the results themselves.
const fileCache = options.cache ? new Map() : null;
const directoryCache = options.cache ? new Map() : null;
const transform = options.transform || identity;
const packageProp = options.packageProp;
function clearFileCache() {
if (fileCache) fileCache.clear();
}
function clearDirectoryCache() {
if (directoryCache) directoryCache.clear();
}
function clearCaches() {
clearFileCache();
clearDirectoryCache();
}
function throwError(error) {
if (options.sync) {
throw error;
} else {
return Promise.reject(error);
}
}
function load(
searchPath ,
configPath
) {
if (!searchPath) searchPath = process.cwd();
if (!configPath && options.configPath) configPath = options.configPath;
if (configPath) {
const absoluteConfigPath = path.resolve(process.cwd(), configPath);
if (fileCache && fileCache.has(absoluteConfigPath)) {
return fileCache.get(absoluteConfigPath);
}
let load;
if (path.basename(absoluteConfigPath) === 'package.json') {
if (!packageProp) {
return throwError(
new Error(
'Please specify the packageProp option. The configPath argument cannot point to a package.json file if packageProp is false.'
)
);
}
load = () =>
loadPackageProp(path.dirname(absoluteConfigPath), {
packageProp,
sync: options.sync,
});
} else {
load = () =>
loadDefinedFile(absoluteConfigPath, {
sync: options.sync,
format: options.format,
});
}
const loadResult = load();
const result =
loadResult instanceof Promise
? loadResult.then(transform)
: transform(loadResult);
if (fileCache) fileCache.set(absoluteConfigPath, result);
return result;
}
const absoluteSearchPath = path.resolve(process.cwd(), searchPath);
const searchPathDir = getDirectory(absoluteSearchPath, options.sync);
return searchPathDir instanceof Promise
? searchPathDir.then(searchDirectory)
: searchDirectory(searchPathDir);
}
function searchDirectory(
directory
) {
if (directoryCache && directoryCache.has(directory)) {
return directoryCache.get(directory);
}
const result = funcRunner(!options.sync ? Promise.resolve() : undefined, [
() => {
if (!packageProp) return;
return loadPackageProp(directory, {
packageProp,
sync: options.sync,
});
},
result => {
if (result || !options.rc) return result;
return loadRc(path.join(directory, options.rc), {
sync: options.sync,
rcStrictJson: options.rcStrictJson,
rcExtensions: options.rcExtensions,
});
},
result => {
if (result || !options.js) return result;
return loadJs(path.join(directory, options.js), { sync: options.sync });
},
result => {
if (result) return result;
const nextDirectory = path.dirname(directory);
if (nextDirectory === directory || directory === options.stopDir)
return null;
return searchDirectory(nextDirectory);
},
transform,
]);
if (directoryCache) directoryCache.set(directory, result);
return result;
}
return {
load,
clearFileCache,
clearDirectoryCache,
clearCaches,
};
};
function identity(x) {
return x;
}