1 module.exports = unbuild
2 unbuild.usage = "npm unbuild <folder>\n(this is plumbing)"
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")
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)
22 function unbuild_ (silent) { return function (folder, cb_) {
24 cb_(er, path.relative(npm.root, folder))
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)
34 ( [ [lifecycle, pkg, "preuninstall", folder, false, true]
35 , [lifecycle, pkg, "uninstall", folder, false, true]
36 , !silent && function(cb) {
37 console.log("unbuild " + pkg._id)
40 , [rmStuff, pkg, folder]
41 , [lifecycle, pkg, "postuninstall", folder, false, true]
42 , [gentlyRm, folder, false, base] ]
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)
53 , top = gnm === parent
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)
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)
70 gentlyRm(path.resolve(binRoot, b), true, cb)
75 function rmMans (pkg, folder, parent, top, cb) {
78 || process.platform === "win32"
79 || !npm.config.get("global")) {
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)) {
91 function rmMan (man) {
92 log.silly("rmMan", "preparing to remove", man)
93 var parseMan = man.match(/(.*\.([0-9]+)(\.gz)?)$/)
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."
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(
110 (bn.indexOf(pkg.name) === 0 ? bn : pkg.name+"-"+bn)+"."+sxn+gz
112 gentlyRm(manDest, true, cb)