Skip to content

Commit

Permalink
move: refactor to closure-based callbacks
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 8ea142f commit 66cca25
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions lib/install/action/move.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,35 @@ function moveModuleOnly (from, to, log, done) {
var temp_modules = from + '.node_modules'

log.silly('move', 'remove existing destination', to)
rimraf(to, iferr(done, makeDestination))
rimraf(to, iferr(done, makeDestination(done)))

function makeDestination () {
log.silly('move', 'make sure destination parent exists', path.resolve(to, '..'))
mkdirp(path.resolve(to, '..'), iferr(done, moveNodeModules))
function makeDestination (next) {
return function () {
log.silly('move', 'make sure destination parent exists', path.resolve(to, '..'))
mkdirp(path.resolve(to, '..'), iferr(done, moveNodeModules(next)))
}
}

function moveNodeModules () {
log.silly('move', 'move source node_modules away', from_modules)
fs.rename(from_modules, temp_modules, function (er) {
doMove(er ? done : moveNodeModulesBack)
})
function moveNodeModules (next) {
return function () {
log.silly('move', 'move source node_modules away', from_modules)
fs.rename(from_modules, temp_modules, iferr(doMove(next), doMove(moveNodeModulesBack(next))))
}
}

function doMove (next) {
log.silly('move', 'move module dir to final dest', from, to)
fs.rename(from, to, iferr(done, next))
return function () {
log.silly('move', 'move module dir to final dest', from, to)
fs.rename(from, to, iferr(done, next))
}
}

function moveNodeModulesBack () {
mkdirp(from, iferr(done, function () {
log.silly('move', 'put source node_modules back', from_modules)
fs.rename(temp_modules, from_modules, done)
}))
function moveNodeModulesBack (next) {
return function () {
mkdirp(from, iferr(done, function () {
log.silly('move', 'put source node_modules back', from_modules)
fs.rename(temp_modules, from_modules, iferr(done, next))
}))
}
}
}

0 comments on commit 66cca25

Please sign in to comment.