Skip to content

Instantly share code, notes, and snippets.

@akhoury
Forked from sbellone/node_cache_test.js
Created September 3, 2014 15:05
Show Gist options
  • Save akhoury/85a4a84ed244c9c2053a to your computer and use it in GitHub Desktop.
Save akhoury/85a4a84ed244c9c2053a to your computer and use it in GitHub Desktop.
var npm = require('npm');
var fs = require('fs');
var path = require('path');
module.constructor.new_packageMainCache = {}; // own cache
// copied from node src
function statPath(path) {
try {
return fs.statSync(path);
} catch (ex) {}
return false;
}
function hasOwnProperty(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}
// modified, added the nocache param
function new_tryExtensions(p, exts, nocache) {
for (var i = 0, EL = exts.length; i < EL; i++) {
var filename = new_tryFile(p + exts[i], nocache);
if (filename) {
return filename;
}
}
return false;
}
// modified, added the nocache param
function new_tryFile(requestPath, nocache) {
var stats = statPath(requestPath);
if (stats && !stats.isDirectory()) {
return fs.realpathSync(requestPath, nocache ? {} : module.constructor._realpathCache);
}
return false;
}
// modified, added the nocache param
function new_readPackage (requestPath, nocache) {
if (hasOwnProperty(module.constructor.new_packageMainCache, requestPath) && ! nocache) {
return module.constructor.new_packageMainCache[requestPath];
}
try {
var jsonPath = path.resolve(requestPath, 'package.json');
var json = fs.readFileSync(jsonPath, 'utf8');
} catch (e) {
return false;
}
try {
var pkg = module.constructor.new_packageMainCache[requestPath] = JSON.parse(json).main;
} catch (e) {
e.path = jsonPath;
e.message = 'Error parsing ' + jsonPath + ': ' + e.message;
throw e;
}
return pkg;
}
// modified, added the nocache param
function new_tryPackage_again (requestPath, exts, nocache) {
var pkg = new_readPackage(requestPath, nocache);
if (!pkg) return false;
var filename = path.resolve(requestPath, pkg);
return new_tryFile(filename, nocache) || new_tryExtensions(filename, exts, nocache) ||
new_tryExtensions(path.resolve(filename, 'index'), exts, nocache);
}
var old_findPath = module.constructor._findPath;
module.constructor._findPath = function(request, paths) {
var filename = old_findPath(request, paths);
if (! filename) {
var cacheKey = JSON.stringify({request: request, paths: paths});
var exts = Object.keys(module.constructor._extensions);
for (var i = 0, PL = paths.length; i < PL; i++) {
var basePath = path.resolve(paths[i], request);
filename = new_tryPackage_again(basePath, exts, true);
if (filename) {
module.constructor._pathCache[cacheKey] = filename;
return filename;
}
}
return false;
}
return filename;
};
npm.load({}, function (er) {
if (er) console.log(er);
npm.commands.install(["[email protected]"], function (er, data) {
if (er) {
console.log("npm install failed: " + er);
}
else {
var winston = require('winston');
console.log("Winston version: " + winston.version);
upgradeWinstonVersion();
}
});
npm.on("log", function (message) { console.log(message) });
});
function cleanModuleCache(moduleName) {
var mod = require.resolve(moduleName);
if (mod && ((mod = require.cache[mod]) !== undefined)) {
(function run(mod) {
mod.children.forEach(function (child) {
run(child);
});
delete require.cache[mod.id];
})(mod);
}
Object.keys(module.constructor._pathCache).forEach(function(cacheKey) {
if (cacheKey.indexOf(moduleName) > 0) {
delete module.constructor._pathCache[cacheKey];
}
});
}
function upgradeWinstonVersion() {
cleanModuleCache('winston');
npm.commands.install(["[email protected]"], function (er, data) {
if (er) {
console.log("npm install failed: " + er);
}
else {
var winston = require('winston');
console.log("Winston version: " + winston.version);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment