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