UNPKG

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