UNPKG

3.87 kBJavaScriptView Raw
1
2// remove a package.
3
4module.exports = uninstall
5
6uninstall.usage = "npm uninstall <name>[@<version> [<name>[@<version>] ...]"
7 + "\nnpm rm <name>[@<version> [<name>[@<version>] ...]"
8
9uninstall.completion = require("./utils/completion/installed-shallow.js")
10
11var fs = require("graceful-fs")
12 , log = require("npmlog")
13 , readJson = require("read-package-json")
14 , path = require("path")
15 , npm = require("./npm.js")
16 , asyncMap = require("slide").asyncMap
17
18function uninstall (args, cb) {
19 // this is super easy
20 // get the list of args that correspond to package names in either
21 // the global npm.dir,
22 // then call unbuild on all those folders to pull out their bins
23 // and mans and whatnot, and then delete the folder.
24
25 var nm = npm.dir
26 if (args.length === 1 && args[0] === ".") args = []
27 if (args.length) return uninstall_(args, nm, cb)
28
29 // remove this package from the global space, if it's installed there
30 if (npm.config.get("global")) return cb(uninstall.usage)
31 readJson(path.resolve(npm.prefix, "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]
35 , npm.dir
36 , cb )
37 })
38
39}
40
41function 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')
47 if (s || d || o) {
48 cb = saver(args, nm, cb)
49 }
50
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)
56 return cb(null, [])
57 }
58 fs.lstat(p, function (er) {
59 if (er) {
60 log.warn("uninstall", "not installed in %s: %j", nm, arg)
61 return cb(null, [])
62 }
63 cb(null, p)
64 })
65 }, function (er, folders) {
66 if (er) return cb(er)
67 asyncMap(folders, npm.commands.unbuild, cb)
68 })
69}
70
71function saver (args, nm, cb_) {
72 return 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) {
82 try {
83 var pkg = JSON.parse(json)
84 } catch (_) {}
85 if (!pkg) return cb_(null, data)
86
87 var bundle
88 if (npm.config.get('save-bundle')) {
89 var bundle = pkg.bundleDependencies || pkg.bundledDependencies
90 if (!Array.isArray(bundle)) bundle = undefined
91 }
92
93 var changed = false
94 args.forEach(function (a) {
95 ; [ [s, 'dependencies']
96 , [o, 'optionalDependencies']
97 , [d, 'devDependencies'] ].forEach(function (f) {
98 var flag = f[0]
99 , field = f[1]
100 if (!flag || !pkg[field] || !pkg[field].hasOwnProperty(a)) return
101 changed = true
102
103 if (bundle) {
104 var i = bundle.indexOf(a)
105 if (i !== -1) bundle.splice(i, 1)
106 }
107
108 delete pkg[field][a]
109 })
110 })
111 if (!changed) return cb_(null, data)
112
113 if (bundle) {
114 delete pkg.bundledDependencies
115 if (bundle.length) {
116 pkg.bundleDependencies = bundle
117 } else {
118 delete pkg.bundleDependencies
119 }
120 }
121
122 fs.writeFile(pj, JSON.stringify(pkg, null, 2) + "\n", function (er) {
123 return cb_(er, data)
124 })
125 })
126 }
127}