UNPKG

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