UNPKG

3.23 kBJavaScriptView Raw
1var path = require('path'); // if module is locally defined we path.resolve it
2var find = require('find'); // https://www.npmjs.com/package/find
3
4require.getRootDir = function () {
5 var keys = Object.keys(require.cache);
6 var parent = keys[0]; // the module that required decache
7 /* istanbul ignore else */
8 if(parent.indexOf('node_modules') > -1) {
9 var end = parent.indexOf('node_modules');
10 parent = parent.substring(0, end);
11 } else {
12 parent = path.resolve(path.normalize(parent +'../../..')); // resolve up!
13 }
14 return parent;
15}
16
17require.find = function (moduleName) {
18 var parent = require.getRootDir();
19 // a locally defined module
20 if(moduleName.indexOf('/') > -1) {
21 // console.log("BEFORE: "+moduleName);
22 var last = moduleName.lastIndexOf('/');
23 // strip everything before the forward slash
24 moduleName = moduleName.substring(last, moduleName.length).replace('/', '');
25 // console.log('AFTER: '+moduleName);
26
27 var mod;
28 var ext = path.extname(moduleName);
29 if(ext === '') {
30 mod = moduleName + '\.js'; // append .js to file type by default
31 }
32 else {
33 mod = moduleName.replace(ext, '\\' + ext); // escape extension for regex
34 }
35
36 var re = new RegExp(mod,"g"); // regex to use when finding the file
37 var files = find.fileSync(re, parent);
38 var file;
39 var keys = Object.keys(require.cache);
40 for (var i = 0; i < files.length; i++) {
41 var f = files[i];
42 if(keys.indexOf(f) > -1) { // won't this *always* be true?
43 file = f;
44 break;
45 }
46 }
47 return file;
48 } else {
49 return moduleName;
50 }
51}
52
53/**
54 * Removes a module from the cache. We need this to re-load our http_request !
55 * see: http://stackoverflow.com/a/14801711/1148249
56 */
57require.decache = function (moduleName) {
58
59 moduleName = require.find(moduleName);
60
61 if(!moduleName) {return;}
62
63 // Run over the cache looking for the files
64 // loaded by the specified module name
65 require.searchCache(moduleName, function (mod) {
66 delete require.cache[mod.id];
67 });
68
69 // Remove cached paths to the module.
70 // Thanks to @bentael for pointing this out.
71 Object.keys(module.constructor._pathCache).forEach(function(cacheKey) {
72 if (cacheKey.indexOf(moduleName)>0) {
73 delete module.constructor._pathCache[cacheKey];
74 }
75 });
76};
77
78/**
79 * Runs over the cache to search for all the cached
80 * files
81 */
82require.searchCache = function (moduleName, callback) {
83 // Resolve the module identified by the specified name
84 var mod = require.resolve(moduleName);
85
86 // Check if the module has been resolved and found within
87 // the cache no else so #ignore else http://git.io/vtgMI
88 /* istanbul ignore else */
89 if (mod && ((mod = require.cache[mod]) !== undefined)) {
90 // Recursively go over the results
91 (function run(mod) {
92 // Go over each of the module's children and
93 // run over it
94 mod.children.forEach(function (child) {
95 run(child);
96 });
97
98 // Call the specified callback providing the
99 // found module
100 callback(mod);
101 })(mod);
102 }
103};
104
105module.exports = require.decache;