4 module.exports = uninstall
6 uninstall.usage = "npm uninstall <name>[@<version> [<name>[@<version>] ...]"
7 + "\nnpm rm <name>[@<version> [<name>[@<version>] ...]"
9 uninstall.completion = require("./utils/completion/installed-shallow.js")
11 var fs = require("graceful-fs")
12 , writeFileAtomic = require("write-file-atomic")
13 , log = require("npmlog")
14 , readJson = require("read-package-json")
15 , path = require("path")
16 , npm = require("./npm.js")
17 , asyncMap = require("slide").asyncMap
19 function uninstall (args, cb) {
21 // get the list of args that correspond to package names in either
22 // the global npm.dir,
23 // then call unbuild on all those folders to pull out their bins
24 // and mans and whatnot, and then delete the folder.
27 if (args.length === 1 && args[0] === ".") args = []
28 if (args.length) return uninstall_(args, nm, cb)
30 // remove this package from the global space, if it's installed there
31 readJson(path.resolve(npm.localPrefix, "package.json"), function (er, pkg) {
32 if (er && er.code !== "ENOENT" && er.code !== "ENOTDIR") return cb(er)
33 if (er) return cb(uninstall.usage)
34 uninstall_( [pkg.name]
41 function uninstall_ (args, nm, cb) {
42 // if we've been asked to --save or --save-dev or --save-optional,
43 // then also remove it from the associated dependencies hash.
44 var s = npm.config.get('save')
45 , d = npm.config.get('save-dev')
46 , o = npm.config.get('save-optional')
48 cb = saver(args, nm, cb)
51 asyncMap(args, function (arg, cb) {
52 // uninstall .. should not delete /usr/local/lib/node_modules/..
53 var p = path.join(path.resolve(nm), path.join("/", arg))
54 if (path.resolve(p) === nm) {
55 log.warn("uninstall", "invalid argument: %j", arg)
58 fs.lstat(p, function (er) {
60 log.warn("uninstall", "not installed in %s: %j", nm, arg)
65 }, function (er, folders) {
67 asyncMap(folders, npm.commands.unbuild, cb)
71 function saver (args, nm, cb_) {
73 function cb (er, data) {
74 var s = npm.config.get('save')
75 , d = npm.config.get('save-dev')
76 , o = npm.config.get('save-optional')
77 if (er || !(s || d || o)) return cb_(er, data)
78 var pj = path.resolve(nm, '..', 'package.json')
79 // don't use readJson here, because we don't want all the defaults
80 // filled in, for mans and other bs.
81 fs.readFile(pj, 'utf8', function (er, json) {
84 pkg = JSON.parse(json)
86 if (!pkg) return cb_(null, data)
89 if (npm.config.get('save-bundle')) {
90 bundle = pkg.bundleDependencies || pkg.bundledDependencies
91 if (!Array.isArray(bundle)) bundle = undefined
95 args.forEach(function (a) {
96 ; [ [s, 'dependencies']
97 , [o, 'optionalDependencies']
98 , [d, 'devDependencies'] ].forEach(function (f) {
101 if (!flag || !pkg[field] || !pkg[field].hasOwnProperty(a)) return
105 var i = bundle.indexOf(a)
106 if (i !== -1) bundle.splice(i, 1)
112 if (!changed) return cb_(null, data)
115 delete pkg.bundledDependencies
117 pkg.bundleDependencies = bundle
119 delete pkg.bundleDependencies
123 writeFileAtomic(pj, JSON.stringify(pkg, null, 2) + "\n", function (er) {