]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/lib/unbuild.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / lib / unbuild.js
1 module.exports = unbuild
2 unbuild.usage = "npm unbuild <folder>\n(this is plumbing)"
3
4 var readJson = require("read-package-json")
5   , gentlyRm = require("./utils/gently-rm.js")
6   , npm = require("./npm.js")
7   , path = require("path")
8   , isInside = require("path-is-inside")
9   , lifecycle = require("./utils/lifecycle.js")
10   , asyncMap = require("slide").asyncMap
11   , chain = require("slide").chain
12   , log = require("npmlog")
13   , build = require("./build.js")
14
15 // args is a list of folders.
16 // remove any bins/etc, and then delete the folder.
17 function unbuild (args, silent, cb) {
18   if (typeof silent === "function") cb = silent, silent = false
19   asyncMap(args, unbuild_(silent), cb)
20 }
21
22 function unbuild_ (silent) { return function (folder, cb_) {
23   function cb (er) {
24     cb_(er, path.relative(npm.root, folder))
25   }
26   folder = path.resolve(folder)
27   var base = isInside(folder, npm.prefix) ? npm.prefix : folder
28   delete build._didBuild[folder]
29   log.verbose("unbuild", folder.substr(npm.prefix.length + 1))
30   readJson(path.resolve(folder, "package.json"), function (er, pkg) {
31     // if no json, then just trash it, but no scripts or whatever.
32     if (er) return gentlyRm(folder, false, base, cb)
33     chain
34       ( [ [lifecycle, pkg, "preuninstall", folder, false, true]
35         , [lifecycle, pkg, "uninstall", folder, false, true]
36         , !silent && function(cb) {
37             console.log("unbuild " + pkg._id)
38             cb()
39           }
40         , [rmStuff, pkg, folder]
41         , [lifecycle, pkg, "postuninstall", folder, false, true]
42         , [gentlyRm, folder, false, base] ]
43       , cb )
44   })
45 }}
46
47 function rmStuff (pkg, folder, cb) {
48   // if it's global, and folder is in {prefix}/node_modules,
49   // then bins are in {prefix}/bin
50   // otherwise, then bins are in folder/../.bin
51   var parent = path.dirname(folder)
52     , gnm = npm.dir
53     , top = gnm === parent
54
55   log.verbose("unbuild rmStuff", pkg._id, "from", gnm)
56   if (!top) log.verbose("unbuild rmStuff", "in", parent)
57   asyncMap([rmBins, rmMans], function (fn, cb) {
58     fn(pkg, folder, parent, top, cb)
59   }, cb)
60 }
61
62 function rmBins (pkg, folder, parent, top, cb) {
63   if (!pkg.bin) return cb()
64   var binRoot = top ? npm.bin : path.resolve(parent, ".bin")
65   asyncMap(Object.keys(pkg.bin), function (b, cb) {
66     if (process.platform === "win32") {
67       chain([ [gentlyRm, path.resolve(binRoot, b) + ".cmd", true]
68             , [gentlyRm, path.resolve(binRoot, b), true] ], cb)
69     } else {
70       gentlyRm(path.resolve(binRoot, b), true, cb)
71     }
72   }, cb)
73 }
74
75 function rmMans (pkg, folder, parent, top, cb) {
76   if (!pkg.man
77       || !top
78       || process.platform === "win32"
79       || !npm.config.get("global")) {
80     return cb()
81   }
82   var manRoot = path.resolve(npm.config.get("prefix"), "share", "man")
83   log.verbose("rmMans", "man files are", pkg.man, "in", manRoot)
84   asyncMap(pkg.man, function (man, cb) {
85     if (Array.isArray(man)) {
86       man.forEach(rmMan)
87     } else {
88       rmMan(man)
89     }
90
91     function rmMan (man) {
92       log.silly("rmMan", "preparing to remove", man)
93       var parseMan = man.match(/(.*\.([0-9]+)(\.gz)?)$/)
94       if (!parseMan) {
95         log.error(
96           "rmMan", man, "is not a valid name for a man file.",
97           "Man files must end with a number, " +
98           "and optionally a .gz suffix if they are compressed."
99         )
100         return cb()
101       }
102
103       var stem = parseMan[1]
104       var sxn = parseMan[2]
105       var gz = parseMan[3] || ""
106       var bn = path.basename(stem)
107       var manDest = path.join(
108         manRoot,
109         "man"+sxn,
110         (bn.indexOf(pkg.name) === 0 ? bn : pkg.name+"-"+bn)+"."+sxn+gz
111       )
112       gentlyRm(manDest, true, cb)
113     }
114   }, cb)
115 }