Last active
August 29, 2015 14:16
-
-
Save ybootin/2fe1e36324ec4cd6dd51 to your computer and use it in GitHub Desktop.
global npm unlinker
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
// retrieve all global links, and unlink all | |
// USAGE : | |
// node unlink.js module ==> remove module from node cache | |
// node unlink.js ==> remove all modules from cache | |
var execSync = require('exec-sync'); | |
var readJson = require('read-package-json') | |
var output = execSync('ls -l /usr/local/lib/node_modules | grep "\\\->"') | |
var m; | |
process.argv.forEach(function (val, index, array) { | |
switch(index) { | |
case 2: | |
m = val; | |
break; | |
} | |
}); | |
if (String(output).trim().length > 0) { | |
console.log("try to unlink " + (m ? m : "all modules") + " globally") | |
output.split("\n").forEach(function(val) { | |
var dir = String(val.split('->')[1]).trim() | |
readJson(dir + '/package.json', console.error, false, function (er, data) { | |
if (er) { | |
// if we got an error, this mean we should totaly remove link from node_modules, the package may have change name | |
console.error("WARNING, file " + dir + "/package.json, doesn't exist or throw an error (error : " + er.errno + " - " + er.code + ")") | |
return | |
} | |
if (!m || (m === data.name)) { | |
var unlink = execSync("npm r " + data.name + " -g", true) | |
if (unlink.stderr) { | |
console.log('unable to unlink ' + data.name + ' module in ' + dir + ', does the module exists ?') | |
} | |
else { | |
console.log('module "' + data.name + '" removed from global cache') | |
} | |
} | |
}); | |
}); | |
} else { | |
console.log('nothing to unlink for ' + (m ? m : "all modules")); | |
} |
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
{ | |
"name": "npm-unlinker", | |
"description": "Unlink all dependencies globally", | |
"version": "0.0.1", | |
"dependencies": { | |
}, | |
"devDependencies": { | |
"exec-sync": "0.1.6", | |
"read-package-json": "1.3.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment