"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/npm/lib/install/action/move.js" (8 Mar 2017, 3239 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 'use strict'
    2 var fs = require('graceful-fs')
    3 var path = require('path')
    4 var chain = require('slide').chain
    5 var iferr = require('iferr')
    6 var rimraf = require('rimraf')
    7 var mkdirp = require('mkdirp')
    8 var rmStuff = require('../../unbuild.js').rmStuff
    9 var lifecycle = require('../../utils/lifecycle.js')
   10 var updatePackageJson = require('../update-package-json.js')
   11 var rename = require('../../utils/rename.js')
   12 
   13 /*
   14   Move a module from one point in the node_modules tree to another.
   15   Do not disturb either the source or target location's node_modules
   16   folders.
   17 */
   18 
   19 module.exports = function (top, buildpath, pkg, log, next) {
   20   log.silly('move', pkg.fromPath, pkg.path)
   21   chain([
   22     [lifecycle, pkg.package, 'preuninstall', pkg.fromPath, false, true],
   23     [lifecycle, pkg.package, 'uninstall', pkg.fromPath, false, true],
   24     [rmStuff, pkg.package, pkg.fromPath],
   25     [lifecycle, pkg.package, 'postuninstall', pkg.fromPath, false, true],
   26     [moveModuleOnly, pkg.fromPath, pkg.path, log],
   27     [lifecycle, pkg.package, 'preinstall', pkg.path, false, true],
   28     [removeEmptyParents, path.resolve(pkg.fromPath, '..')],
   29     [updatePackageJson, pkg, pkg.path]
   30   ], next)
   31 }
   32 
   33 function removeEmptyParents (pkgdir, next) {
   34   fs.rmdir(pkgdir, function (er) {
   35     // FIXME: Make sure windows does what we want here
   36     if (er && er.code !== 'ENOENT') return next()
   37     removeEmptyParents(path.resolve(pkgdir, '..'), next)
   38   })
   39 }
   40 
   41 function moveModuleOnly (from, to, log, done) {
   42   var fromModules = path.join(from, 'node_modules')
   43   var tempFromModules = from + '.node_modules'
   44   var toModules = path.join(to, 'node_modules')
   45   var tempToModules = to + '.node_modules'
   46 
   47   log.silly('move', 'move existing destination node_modules away', toModules)
   48 
   49   rename(toModules, tempToModules, removeDestination(done))
   50 
   51   function removeDestination (next) {
   52     return function (er) {
   53       log.silly('move', 'remove existing destination', to)
   54       if (er) {
   55         rimraf(to, iferr(next, makeDestination(next)))
   56       } else {
   57         rimraf(to, iferr(next, makeDestination(iferr(next, moveToModulesBack(next)))))
   58       }
   59     }
   60   }
   61 
   62   function moveToModulesBack (next) {
   63     return function () {
   64       log.silly('move', 'move existing destination node_modules back', toModules)
   65       rename(tempToModules, toModules, iferr(done, next))
   66     }
   67   }
   68 
   69   function makeDestination (next) {
   70     return function () {
   71       log.silly('move', 'make sure destination parent exists', path.resolve(to, '..'))
   72       mkdirp(path.resolve(to, '..'), iferr(done, moveNodeModules(next)))
   73     }
   74   }
   75 
   76   function moveNodeModules (next) {
   77     return function () {
   78       log.silly('move', 'move source node_modules away', fromModules)
   79       rename(fromModules, tempFromModules, iferr(doMove(next), doMove(moveNodeModulesBack(next))))
   80     }
   81   }
   82 
   83   function doMove (next) {
   84     return function () {
   85       log.silly('move', 'move module dir to final dest', from, to)
   86       rename(from, to, iferr(done, next))
   87     }
   88   }
   89 
   90   function moveNodeModulesBack (next) {
   91     return function () {
   92       mkdirp(from, iferr(done, function () {
   93         log.silly('move', 'put source node_modules back', fromModules)
   94         rename(tempFromModules, fromModules, iferr(done, next))
   95       }))
   96     }
   97   }
   98 }