-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
298 lines (234 loc) · 8.32 KB
/
index.js
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
'use strict';
const topLogPrefix = 'larvitfs: index.js: ';
const LUtils = require('larvitutils');
const path = require('path');
/**
* Lfs constructor
*
* @param {obj} options {
* 'basePath': process.cwd(), // OPTIONAL
* 'cacheMaxSize': 10000, // OPTIONAL
* 'log': new (new (require('larvitutils'))()).Log(), // OPTIONAL
* 'fs': require('fs')
* }
*/
function Lfs(options) {
const that = this;
that.options = options || {};
if (! that.options.log) {
const lUtils = new LUtils();
that.options.log = new lUtils.Log();
}
that.log = that.options.log;
if (! that.options.basePath) {
that.options.basePath = process.cwd();
}
that.basePath = that.options.basePath;
if (! that.options.cacheMaxSize) {
that.options.cacheMaxSize = 10000;
}
that.cacheMaxSize = that.options.cacheMaxSize;
if (! that.options.fs) {
that.options.fs = require('fs');
}
that.fs = that.options.fs;
that.cache = new Map();
that.paths = [that.options.basePath];
that.loadPaths();
}
Lfs.prototype.getPathSync = function getPathSync(pathToResolve) {
const logPrefix = topLogPrefix + 'Lfs.getPathSync() - pathToResolve: "' + pathToResolve + '" - ';
const that = this;
if (that.cache.get(pathToResolve) !== undefined) {
that.log.silly(logPrefix + 'Found in cache');
return that.cache.get(pathToResolve);
}
// Make sure we do not use up all the memory with caching violent amount of files
if (that.cache.size >= that.options.cacheMaxSize) {
that.cache.clear();
}
if (pathToResolve[0] === '/') {
that.log.debug(logPrefix + 'starts with "/", only check absolute path');
if (that.fs.existsSync(pathToResolve)) {
that.log.debug(logPrefix + '"' + pathToResolve + '" found - loading to cache');
that.cache.set(pathToResolve, pathToResolve);
} else {
that.log.debug(logPrefix + '"' + pathToResolve + '" not found - setting false in cache');
that.cache.set(pathToResolve, false);
}
return that.cache.get(pathToResolve);
} else {
that.log.debug(logPrefix + 'is relative, look in all the paths');
for (let i = 0; that.paths[i] !== undefined; i ++) {
let testPath = path.join(that.paths[i], pathToResolve);
that.log.silly(logPrefix + 'Checking for ' + testPath);
// Lookup if this file exists
if (that.fs.existsSync(testPath)) {
that.log.debug(logPrefix + '"' + testPath + '" found - loading to cache');
that.cache.set(pathToResolve, testPath);
return testPath;
} else {
that.log.silly(logPrefix + '"' + testPath + '" does not exist');
}
}
// If we arrive here, no file have been found.
that.cache.set(pathToResolve, false);
return false;
}
};
Lfs.prototype.getPathsSync = function getPathsSync(target, refreshCache) {
const logPrefix = topLogPrefix + 'getPathsSync() - ';
const that = this;
let result = [];
let modules_result;
let package_json;
if (! target) {
that.log.warn(logPrefix + 'Invalid target');
return false;
}
if (! refreshCache && that.getPathsCache && that.getPathsCache[target]) {
return that.getPathsCache[target];
}
/**
* Search for paths recursively
*
* @param {str} thisPath - the path to search for
* @param {arr} pathsToIgnore - array of paths to ignore
* @returns {str} - absolute path
*/
function searchPathsRec(thisPath, pathsToIgnore) {
const subLogPrefix = logPrefix + 'searchPathsRec() - ';
let result = [];
let thisPaths;
if (! pathsToIgnore) pathsToIgnore = [];
try {
if (that.fs.existsSync(thisPath + '/' + target) && result.indexOf(path.normalize(thisPath + '/' + target)) === - 1 && pathsToIgnore.indexOf(thisPath) === - 1) {
result.push(path.normalize(thisPath + '/' + target));
}
if (! that.fs.existsSync(thisPath)) {
return result;
}
thisPaths = that.fs.readdirSync(thisPath);
} catch (err) {
that.log.error(subLogPrefix + 'throwed fs error: ' + err.message);
return result;
}
for (let i = 0; thisPaths[i] !== undefined; i ++) {
try {
const subStat = that.fs.statSync(thisPath + '/' + thisPaths[i]);
if (subStat.isDirectory()) {
if (thisPaths[i] !== target && pathsToIgnore.indexOf(thisPaths[i]) === - 1) {
// If we've found a target dir, we do not wish to scan it
result = result.concat(searchPathsRec(thisPath + '/' + thisPaths[i], pathsToIgnore));
}
}
} catch (err) {
that.log.error(subLogPrefix + 'Could not read "' + thisPaths[i] + '": ' + err.message);
}
}
return result;
}
// First scan for local controllers
result = searchPathsRec(that.options.basePath, ['node_modules']);
try {
package_json = require(that.options.basePath + '/package.json');
} catch (err) {
that.log.info(logPrefix + 'Could not load package.json, err: ' + err.message);
}
// Then go through the dependencies in the package file
try {
if (package_json && package_json.dependencies) {
for (let depPath of Object.keys(package_json.dependencies)) {
const modPath = path.normalize(that.options.basePath + '/node_modules/' + depPath);
if (that.fs.existsSync(modPath)) {
const stats = that.fs.statSync(modPath);
if (stats && stats.isDirectory()) {
for (const dir of that.fs.readdirSync(modPath)) {
if (dir === target && result.indexOf(path.normalize(modPath + '/' + dir)) === - 1) {
result.push(path.normalize(modPath + '/' + dir));
break;
}
}
}
}
}
}
} catch (err) {
that.log.error(logPrefix + 'Could not fetch info about dependencies. err: ' + err.message);
return false;
}
// Add all other paths, recursively, starting in basePath
modules_result = searchPathsRec(that.options.basePath + '/node_modules');
// The lower in the tree of node modules, the farther back in the array
modules_result.sort(function (a, b) {
return a.lastIndexOf('node_modules') - b.lastIndexOf('node_modules');
});
for (let i = 0; modules_result[i] !== undefined; i ++) {
if (result.indexOf(modules_result[i]) === - 1) {
result.push(modules_result[i]);
}
}
if (! that.getPathsCache) {
that.getPathsCache = {};
}
that.getPathsCache[target] = result;
return result;
};
// Load paths to local cache
Lfs.prototype.loadPaths = function loadPaths() {
const logPrefix = topLogPrefix + 'Lfs.prototype.loadPaths() - ';
const that = this;
let package_json;
/**
* Add all other paths, recursively
*
* @param {str} thisPath - the path to search for
*/
function loadPathsRec(thisPath) {
const subLogPrefix = logPrefix + 'loadPathsRec() - ';
let thisPaths;
if (that.paths.indexOf(thisPath) === - 1) {
that.log.debug(subLogPrefix + 'Adding ' + path.basename(thisPath) + ' to paths with full path ' + thisPath);
that.paths.push(thisPath);
}
thisPaths = that.fs.readdirSync(thisPath + '/node_modules');
for (let i = 0; thisPaths[i] !== undefined; i ++) {
try {
const subStat = that.fs.statSync(thisPath + '/node_modules/' + thisPaths[i]);
if (subStat.isDirectory()) {
loadPathsRec(thisPath + '/node_modules/' + thisPaths[i]);
}
} catch (err) {
that.log.silly(subLogPrefix + 'Could not read "' + thisPaths[i] + '": ' + err.message);
}
}
}
that.log.verbose(logPrefix + 'Loading paths cache relative to ' + that.options.basePath);
try {
package_json = require(that.basePath + '/package.json');
} catch (err) {
that.log.info(logPrefix + 'Could not load package.json, err: ' + err.message);
}
// First go through the dependencies in the package file
if (package_json && package_json.dependencies) {
for (let depPath of Object.keys(package_json.dependencies)) {
const modPath = path.normalize(that.options.basePath + '/node_modules/' + depPath);
if (that.fs.existsSync(modPath)) {
const stats = that.fs.statSync(modPath);
if (! stats || ! stats.isDirectory()) {
that.log.info(logPrefix + 'Module "' + depPath + '" not found at ' + modPath);
} else {
that.log.debug(logPrefix + 'Adding "' + depPath + '" to paths with full path ' + modPath);
that.paths.push(modPath);
}
}
}
}
// Start in basePath
try {
loadPathsRec(that.options.basePath);
} catch (err) {
that.log.info(logPrefix + 'Could not find node_modules folder in "' + that.options.basePath + '". If you have modules installed, make sure options.basePath is set correctly when instancing larvitfs. err: ' + err.message);
}
};
exports = module.exports = Lfs;