wipe-node-cache
Advanced tools
Comparing version 1.0.2 to 1.1.0
@@ -1,3 +0,3 @@ | ||
var path = require('path'); | ||
var wipe = require("../index"); | ||
var path = require('path'), | ||
wipe = require("../index"); | ||
@@ -14,5 +14,2 @@ | ||
return !resolver(null, moduleName); | ||
}); | ||
wipe.allButNodeModules(); | ||
wipe.all(); | ||
}); |
72
index.js
@@ -6,2 +6,6 @@ var Module = require('module'); | ||
} | ||
function removeFromCache(moduleName) { | ||
delete require.cache[moduleName]; | ||
} | ||
/** | ||
@@ -15,5 +19,2 @@ * Wipes node.js module cache. | ||
* @param {Function} [waveCallback] function(moduleName) which shall return false, if parent module must not be wiped. | ||
* | ||
* @name wipeCache | ||
* @function | ||
*/ | ||
@@ -25,3 +26,3 @@ function wipeCache(stubs, resolver, waveCallback) { | ||
var cache = Object.assign({}, require.cache); | ||
var cache = require.cache; | ||
@@ -34,3 +35,3 @@ // First wave | ||
removedList.push(moduleName); | ||
delete cache[moduleName]; | ||
removeFromCache(moduleName); | ||
} | ||
@@ -51,3 +52,3 @@ }); | ||
removedList.push(moduleName); | ||
delete cache[moduleName]; | ||
removeFromCache(moduleName); | ||
} | ||
@@ -58,63 +59,4 @@ }); | ||
} | ||
// Replace module cache by wiped one | ||
require.cache = Module._cache = cache; | ||
} | ||
/** | ||
* Wipes everything | ||
* @name wipeCache.all | ||
* @function | ||
*/ | ||
wipeCache.all = function () { | ||
var returnTrue = function () { | ||
return true; | ||
}; | ||
wipeCache(null, returnTrue, returnTrue); | ||
}; | ||
/** | ||
* Wipes everything except node_modules | ||
* @name wipeCache.allButNodeModules | ||
* @function | ||
*/ | ||
wipeCache.allButNodeModules = function () { | ||
function resolver(stubs, fileName, module) { | ||
return !fileName.indexOf('node_modules') > 0 | ||
} | ||
// wipe everything, except node_modules | ||
wipeCache(null, resolver, function (moduleName) { | ||
return !resolver(null, moduleName); | ||
}); | ||
}; | ||
/** | ||
* Wipes modules by list of matches | ||
* @param {String[]} matches - list of expressions | ||
* @name wipeCache.match | ||
* @function | ||
*/ | ||
wipeCache.match = function (matches) { | ||
function resolver(stubs, fileName, module) { | ||
var dirname = module ? path.dirname(module) : ''; | ||
var requireName = fileName; | ||
if (dirname) { | ||
requireName = fileName.charAt(0) == '.' ? path.normalize(dirname + '/' + fileName) : fileName; | ||
} | ||
for (var i in stubs) { | ||
if (requireName.match(stubs[i])) { | ||
return stubs[i]; | ||
} | ||
} | ||
} | ||
wipeCache(matches, resolver, function () { | ||
return true; | ||
}); | ||
}; | ||
module.exports = wipeCache; |
{ | ||
"name": "wipe-node-cache", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Wipes node.js cache in a controled way.", | ||
@@ -28,4 +28,3 @@ "main": "index.js", | ||
}, | ||
"dependencies": { | ||
} | ||
"dependencies": {} | ||
} |
@@ -17,11 +17,2 @@ **Keep your cache clear** – as my mom always says. | ||
```js | ||
wipe.all(); | ||
wipe.allButNodeModules(); | ||
wipe(stubs, resolver, waveCallback); // see API below | ||
``` | ||
Little example | ||
```js | ||
//or, you can | ||
// foo.js | ||
@@ -43,3 +34,3 @@ var i = 0; | ||
wipe(null, function(){return true;}); // will wipe everythinh | ||
wipe(null, function(){return true;}) | ||
@@ -59,3 +50,3 @@ require('./foo')(); | ||
After first pass wipe will enter bubbling stage, and will wipe all modules, which use already wiped ones. | ||
After first pass wipe will enter bubbling stage, and will wipe all modules, which use first ones. | ||
Each time _bubbleCallback_ will be called with 1 argument - moduleName. | ||
@@ -79,4 +70,2 @@ And you can decide - purge it, or not. | ||
```js | ||
// create custom resolver | ||
function resolver(stubs, fileName, module) { | ||
@@ -97,3 +86,3 @@ var dirname = module ? path.dirname(module) : ''; | ||
// wipe anything from helpers, and app.js. | ||
// but keep hands off everything else, for example – node_modules and core during bubbling. | ||
// but keep hands off node_modules and core during bubbling. | ||
wipe({ | ||
@@ -107,6 +96,4 @@ 'helpers/*': true, | ||
Wiping everying is bad idea, wiping node_modules is bad idea, you should wipe only your own things, or even just selected ones. | ||
## Related | ||
- [proxyquire](https://github.com/thlorenz/proxyquire) - Usefull testing tool, but with bad caching politics. |
@@ -62,44 +62,2 @@ /*jshint asi:true*/ | ||
it('should reset everything', function () { | ||
//wipe all | ||
wipe(null, function () { | ||
return true | ||
}); | ||
var a = require('./src/a.js'); | ||
assert.equal(a.fn(), 1); | ||
var a1 = require('./src/a.js'); | ||
assert.equal(a1.fn(), 2); | ||
assert.equal(a.b_fn(), 1); | ||
wipe.all(); | ||
assert.equal(a.b_fn(), 2); | ||
var b = require('./src/b.js'); | ||
assert.equal(b.fn(), 1); | ||
}); | ||
it('should reset by list', function () { | ||
//wipe all | ||
wipe(null, function () { | ||
return true | ||
}); | ||
var a = require('./src/a.js'); | ||
assert.equal(a.fn(), 1); | ||
var a1 = require('./src/a.js'); | ||
assert.equal(a1.fn(), 2); | ||
assert.equal(a.b_fn(), 1); | ||
wipe.match([ | ||
/wipeNodeCache\/test\/src/ | ||
]); | ||
assert.equal(a.b_fn(), 2); | ||
var b = require('./src/b.js'); | ||
assert.equal(b.fn(), 1); | ||
}); | ||
if (1) { | ||
@@ -106,0 +64,0 @@ it('should reset deep module module', function () { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9432
10
193
94