Last active
August 29, 2015 14:05
-
-
Save akhoury/c6104082c06749895535 to your computer and use it in GitHub Desktop.
node v0.10.3, npm v1.4.4 A test script to install module via npm, that when attempting to uninstall a module (which was installed from the npm registry) then installing the same module from a URL (github.com), the 2nd call to require attempts to execute the old main script which does not execute, even after clearing require's cache.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var getModuleShortName = function(giturlORname) { | |
if (giturlORname.indexOf('git://github.com') > -1) { | |
return giturlORname.split('/').pop().split('#')[0]; | |
} | |
return giturlORname.split('@')[0]; | |
}; | |
var searchModulesCache = function(moduleName, callback) { | |
var mod = require.resolve(moduleName); | |
if (mod && ((mod = require.cache[mod]) !== undefined)) { | |
(function run(mod) { | |
mod.children.forEach(function (child) { | |
run(child); | |
}); | |
callback(mod); | |
})(mod); | |
} | |
}; | |
var reloadModule = function(moduleName) { | |
searchModulesCache(moduleName, function(mod) { | |
delete require.cache[mod.id]; | |
}); | |
return require(moduleName); | |
}; | |
var isCompatible = function(myModule) { | |
return typeof myModule.teardown == 'function' && typeof myModule.setup === 'function'; | |
}; | |
var install = function(moduleName, config, callback) { | |
var npm = require('npm'); | |
if (typeof config === 'function') { | |
callback = config; | |
config = {}; | |
} | |
callback = typeof callback === 'function' ? callback : function(){}; | |
npm.load(config || {}, function(err) { | |
if (err) { | |
callback(err); | |
} | |
npm.config.set('spin', false); | |
npm.config.set('force', true); | |
npm.config.set('verbose', false); | |
npm.commands.install([moduleName], function(err) { | |
if (err) { | |
callback(err); | |
} | |
// just in case it's a URL, i.e. git://github.com/someone/something#master | |
var moduleShortName = getModuleShortName(moduleName); | |
console.log('\n\n\n\n\nMODULE: ' + moduleName); | |
console.log('\nRESOLVED PATH OF THE MAIN SCRIPT: >>>> ' + require.resolve(moduleShortName)); | |
var packageJson = JSON.parse(fs.readFileSync( __dirname + '/node_modules/' + moduleShortName + '/package.json')); | |
console.log('\npackage.json ==> { main: ' + packageJson.main + ', version: ' + packageJson.version + ' }\n\n\n\n\n'); | |
try { | |
var myModule = reloadModule(moduleShortName); | |
} catch (e) { | |
console.log('\n\nWhy is it trying to execute the old main script?? even though it was replaced by a new module, scroll up the logs compare both attempts.\n\n'); | |
throw e; | |
} | |
if (! isCompatible(myModule) ) { | |
// not compatible? let's try the latest on github.com, it might be compatible | |
// if it has the github url in it, then we already tried that | |
if (moduleName.indexOf('github.com/akhoury') === -1) { | |
// uninstall them first | |
npm.commands.uninstall([moduleShortName], function(err) { | |
if(err) { | |
callback(err); | |
} | |
install('git://github.com/akhoury/' + moduleShortName + '#master', {}, callback); | |
}); | |
} else { | |
callback({error: moduleName + ' is not compatible.'}); | |
} | |
} else { | |
callback(); | |
} | |
}); | |
}); | |
}; | |
install('[email protected]', {force: true}, function(err) { | |
if (err) { | |
console.error(err); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
SAMPLE OUTPUT