"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/npm/lib/install/action/finalize.js" (7 Feb 2017, 2804 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 path = require('path')
    3 var rimraf = require('rimraf')
    4 var fs = require('graceful-fs')
    5 var mkdirp = require('mkdirp')
    6 var asyncMap = require('slide').asyncMap
    7 var rename = require('../../utils/rename.js')
    8 
    9 module.exports = function (top, buildpath, pkg, log, next) {
   10   log.silly('finalize', pkg.path)
   11 
   12   var delpath = path.join(path.dirname(pkg.path), '.' + path.basename(pkg.path) + '.DELETE')
   13 
   14   mkdirp(path.resolve(pkg.path, '..'), whenParentExists)
   15 
   16   function whenParentExists (mkdirEr) {
   17     if (mkdirEr) return next(mkdirEr)
   18     // We stat first, because we can't rely on ENOTEMPTY from Windows.
   19     // Windows, by contrast, gives the generic EPERM of a folder already exists.
   20     fs.lstat(pkg.path, destStatted)
   21   }
   22 
   23   function destStatted (doesNotExist) {
   24     if (doesNotExist) {
   25       rename(buildpath, pkg.path, whenMoved)
   26     } else {
   27       moveAway()
   28     }
   29   }
   30 
   31   function whenMoved (renameEr) {
   32     if (!renameEr) return next()
   33     if (renameEr.code !== 'ENOTEMPTY') return next(renameEr)
   34     moveAway()
   35   }
   36 
   37   function moveAway () {
   38     rename(pkg.path, delpath, whenOldMovedAway)
   39   }
   40 
   41   function whenOldMovedAway (renameEr) {
   42     if (renameEr) return next(renameEr)
   43     rename(buildpath, pkg.path, whenConflictMoved)
   44   }
   45 
   46   function whenConflictMoved (renameEr) {
   47     // if we got an error we'll try to put back the original module back,
   48     // succeed or fail though we want the original error that caused this
   49     if (renameEr) return rename(delpath, pkg.path, function () { next(renameEr) })
   50     fs.readdir(path.join(delpath, 'node_modules'), makeTarget)
   51   }
   52 
   53   function makeTarget (readdirEr, files) {
   54     if (readdirEr) return cleanup()
   55     if (!files.length) return cleanup()
   56     mkdirp(path.join(pkg.path, 'node_modules'), function (mkdirEr) { moveModules(mkdirEr, files) })
   57   }
   58 
   59   function moveModules (mkdirEr, files) {
   60     if (mkdirEr) return next(mkdirEr)
   61     asyncMap(files, function (file, done) {
   62       var from = path.join(delpath, 'node_modules', file)
   63       var to = path.join(pkg.path, 'node_modules', file)
   64       rename(from, to, done)
   65     }, cleanup)
   66   }
   67 
   68   function cleanup (moveEr) {
   69     if (moveEr) return next(moveEr)
   70     rimraf(delpath, afterCleanup)
   71   }
   72 
   73   function afterCleanup (rimrafEr) {
   74     if (rimrafEr) log.warn('finalize', rimrafEr)
   75     next()
   76   }
   77 }
   78 
   79 module.exports.rollback = function (buildpath, pkg, next) {
   80   var top = path.resolve(buildpath, '..')
   81   rimraf(pkg.path, function () {
   82     removeEmptyParents(pkg.path)
   83   })
   84   function removeEmptyParents (pkgdir) {
   85     if (path.relative(top, pkgdir)[0] === '.') return next()
   86     fs.rmdir(pkgdir, function (er) {
   87       // FIXME: Make sure windows does what we want here
   88       if (er && er.code !== 'ENOENT') return next()
   89       removeEmptyParents(path.resolve(pkgdir, '..'))
   90     })
   91   }
   92 }