Skip to content

Commit

Permalink
move: preserve dest node_modules when moving
Browse files Browse the repository at this point in the history
Credit: @iarna
Reviewed-By: @zkat
PR-URL: npm#10655
  • Loading branch information
iarna authored and othiym23 committed Dec 3, 2015
1 parent 0c4d158 commit f4385d8
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions lib/install/action/move.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ var rmStuff = require('../../unbuild.js').rmStuff
var lifecycle = require('../../utils/lifecycle.js')
var updatePackageJson = require('../update-package-json')

/*
Move a module from one point in the node_modules tree to another.
Do not disturb either the source or target location's node_modules
folders.
*/

module.exports = function (top, buildpath, pkg, log, next) {
log.silly('move', pkg.fromPath, pkg.path)
chain([
Expand All @@ -33,10 +39,31 @@ function removeEmptyParents (pkgdir, next) {

function moveModuleOnly (from, to, log, done) {
var fromModules = path.join(from, 'node_modules')
var tempModules = from + '.node_modules'
var tempFromModules = from + '.node_modules'
var toModules = path.join(to, 'node_modules')
var tempToModules = to + '.node_modules'

log.silly('move', 'move existing destination node_modules away', toModules)

fs.rename(toModules, tempToModules, removeDestination(done))

log.silly('move', 'remove existing destination', to)
rimraf(to, iferr(done, makeDestination(done)))
function removeDestination (next) {
return function (er) {
log.silly('move', 'remove existing destination', to)
if (er) {
rimraf(to, iferr(next, makeDestination(next)))
} else {
rimraf(to, iferr(next, makeDestination(iferr(next, moveToModulesBack(next)))))
}
}
}

function moveToModulesBack (next) {
return function () {
log.silly('move', 'move existing destination node_modules back', toModules)
fs.rename(tempToModules, toModules, iferr(done, next))
}
}

function makeDestination (next) {
return function () {
Expand All @@ -48,7 +75,7 @@ function moveModuleOnly (from, to, log, done) {
function moveNodeModules (next) {
return function () {
log.silly('move', 'move source node_modules away', fromModules)
fs.rename(fromModules, tempModules, iferr(doMove(next), doMove(moveNodeModulesBack(next))))
fs.rename(fromModules, tempFromModules, iferr(doMove(next), doMove(moveNodeModulesBack(next))))
}
}

Expand All @@ -63,7 +90,7 @@ function moveModuleOnly (from, to, log, done) {
return function () {
mkdirp(from, iferr(done, function () {
log.silly('move', 'put source node_modules back', fromModules)
fs.rename(tempModules, fromModules, iferr(done, next))
fs.rename(tempFromModules, fromModules, iferr(done, next))
}))
}
}
Expand Down

0 comments on commit f4385d8

Please sign in to comment.