1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
'use strict'
var path = require('path')
var rimraf = require('rimraf')
var fs = require('graceful-fs')
var mkdirp = require('mkdirp')
var asyncMap = require('slide').asyncMap
var iferr = require('iferr')
function getTree (pkg) {
while (pkg.parent) pkg = pkg.parent
return pkg
}
function warn (pkg, code, msg) {
var tree = getTree(pkg)
var err = new Error(msg)
err.code = code
tree.warnings.push(err)
}
function pathToShortname (modpath) {
return modpath.replace(/node_modules[/]/g, '').replace(/[/]/g, ' > ')
}
module.exports = function (top, buildpath, pkg, log, next) {
log.silly('finalize', pkg.path)
var delpath = path.join(path.dirname(pkg.path), '.' + path.basename(pkg.path) + '.DELETE')
mkdirp(path.resolve(pkg.path, '..'), whenParentExists)
function whenParentExists (mkdirEr) {
if (mkdirEr) return next(mkdirEr)
// We stat first, because we can't rely on ENOTEMPTY from Windows.
// Windows, by contrast, gives the generic EPERM of a folder already exists.
fs.lstat(pkg.path, destStated)
}
function destStated (doesNotExist) {
if (doesNotExist) {
fs.rename(buildpath, pkg.path, whenMoved)
} else {
moveAway()
}
}
function whenMoved (renameEr) {
if (!renameEr) return next()
if (renameEr.code !== 'ENOTEMPTY') return next(renameEr)
moveAway()
}
function moveAway () {
fs.rename(pkg.path, delpath, whenOldMovedAway)
}
function whenOldMovedAway (renameEr) {
if (renameEr) return next(renameEr)
fs.rename(buildpath, pkg.path, whenConflictMoved)
}
function whenConflictMoved (renameEr) {
// if we got an error we'll try to put back the original module back,
// succeed or fail though we want the original error that caused this
if (renameEr) return fs.rename(delpath, pkg.path, function () { next(renameEr) })
fs.readdir(path.join(delpath, 'node_modules'), makeTarget)
}
function makeTarget (readdirEr, files) {
if (readdirEr) return cleanup()
if (!files.length) return cleanup()
mkdirp(path.join(pkg.path, 'node_modules'), function (mkdirEr) { moveModules(mkdirEr, files) })
}
function moveModules (mkdirEr, files) {
if (mkdirEr) return next(mkdirEr)
asyncMap(files, function (file, done) {
// `from` wins over `to`, because if `from` was there it's because the
// module installer wanted it to be there. By contrast, `to` is just
// whatever was bundled in this module. And the intentions of npm's
// installer should always beat out random module contents.
var from = path.join(delpath, 'node_modules', file)
var to = path.join(pkg.path, 'node_modules', file)
fs.stat(to, function (er, info) {
if (er) return fs.rename(from, to, done)
var shortname = pathToShortname(path.relative(getTree(pkg).path, to))
warn(pkg, 'EBUNDLEOVERRIDE', 'Replacing bundled ' + shortname + ' with new installed version')
rimraf(to, iferr(done, function () {
fs.rename(from, to, done)
}))
})
}, cleanup)
}
function cleanup (moveEr) {
if (moveEr) return next(moveEr)
rimraf(delpath, afterCleanup)
}
function afterCleanup (rimrafEr) {
if (rimrafEr) log.warn('finalize', rimrafEr)
next()
}
}
module.exports.rollback = function (buildpath, pkg, next) {
var top = path.resolve(buildpath, '..')
rimraf(pkg.path, function () {
removeEmptyParents(pkg.path)
})
function removeEmptyParents (pkgdir) {
if (path.relative(top, pkgdir)[0] === '.') return next()
fs.rmdir(pkgdir, function (er) {
// FIXME: Make sure windows does what we want here
if (er && er.code !== 'ENOENT') return next()
removeEmptyParents(path.resolve(pkgdir, '..'))
})
}
}
|