]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/lib/uninstall.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / lib / uninstall.js
1
2 // remove a package.
3
4 module.exports = uninstall
5
6 uninstall.usage = "npm uninstall <name>[@<version> [<name>[@<version>] ...]"
7                 + "\nnpm rm <name>[@<version> [<name>[@<version>] ...]"
8
9 uninstall.completion = require("./utils/completion/installed-shallow.js")
10
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
18
19 function uninstall (args, cb) {
20   // this is super easy
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.
25
26   var nm = npm.dir
27   if (args.length === 1 && args[0] === ".") args = []
28   if (args.length) return uninstall_(args, nm, cb)
29
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]
35               , npm.globalDir
36               , cb )
37   })
38
39 }
40
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')
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
71 function 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       var pkg
83       try {
84         pkg = JSON.parse(json)
85       } catch (_) {}
86       if (!pkg) return cb_(null, data)
87
88       var bundle
89       if (npm.config.get('save-bundle')) {
90         bundle = pkg.bundleDependencies || pkg.bundledDependencies
91         if (!Array.isArray(bundle)) bundle = undefined
92       }
93
94       var changed = false
95       args.forEach(function (a) {
96         ; [ [s, 'dependencies']
97           , [o, 'optionalDependencies']
98           , [d, 'devDependencies'] ].forEach(function (f) {
99             var flag = f[0]
100               , field = f[1]
101             if (!flag || !pkg[field] || !pkg[field].hasOwnProperty(a)) return
102             changed = true
103
104             if (bundle) {
105               var i = bundle.indexOf(a)
106               if (i !== -1) bundle.splice(i, 1)
107             }
108
109             delete pkg[field][a]
110           })
111       })
112       if (!changed) return cb_(null, data)
113
114       if (bundle) {
115         delete pkg.bundledDependencies
116         if (bundle.length) {
117           pkg.bundleDependencies = bundle
118         } else {
119           delete pkg.bundleDependencies
120         }
121       }
122
123       writeFileAtomic(pj, JSON.stringify(pkg, null, 2) + "\n", function (er) {
124         return cb_(er, data)
125       })
126     })
127   }
128 }