"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/npm/lib/install/action/remove.js" (11 Apr 2017, 2475 Bytes) of package /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 fs = require('graceful-fs')
    4 var rimraf = require('rimraf')
    5 var asyncMap = require('slide').asyncMap
    6 var mkdirp = require('mkdirp')
    7 var npm = require('../../npm.js')
    8 var andIgnoreErrors = require('../and-ignore-errors.js')
    9 var rename = require('../../utils/rename.js')
   10 
   11 // This is weird because we want to remove the module but not it's node_modules folder
   12 // allowing for this allows us to not worry about the order of operations
   13 module.exports = function (top, buildpath, pkg, log, next) {
   14   log.silly('remove', pkg.path)
   15   if (pkg.target) {
   16     removeLink(pkg, next)
   17   } else {
   18     removeDir(pkg, log, next)
   19   }
   20 }
   21 
   22 function removeLink (pkg, next) {
   23   npm.commands.unbuild(pkg.path, true, next)
   24 }
   25 
   26 function removeDir (pkg, log, next) {
   27   var modpath = path.join(path.dirname(pkg.path), '.' + path.basename(pkg.path) + '.MODULES')
   28 
   29   rename(path.join(pkg.path, 'node_modules'), modpath, unbuildPackage)
   30 
   31   function unbuildPackage (renameEr) {
   32     npm.commands.unbuild(pkg.path, true, function () {
   33       rimraf(pkg.path, renameEr ? andRemoveEmptyParents(pkg.path) : moveModulesBack)
   34     })
   35   }
   36 
   37   function andRemoveEmptyParents (path) {
   38     return function (er) {
   39       if (er) return next(er)
   40       removeEmptyParents(pkg.path)
   41     }
   42   }
   43 
   44   function moveModulesBack () {
   45     fs.readdir(modpath, makeTarget)
   46   }
   47 
   48   function makeTarget (readdirEr, files) {
   49     if (readdirEr) return cleanup()
   50     if (!files.length) return cleanup()
   51     mkdirp(path.join(pkg.path, 'node_modules'), function (mkdirEr) { moveModules(mkdirEr, files) })
   52   }
   53 
   54   function moveModules (mkdirEr, files) {
   55     if (mkdirEr) return next(mkdirEr)
   56     asyncMap(files, function (file, done) {
   57       var from = path.join(modpath, file)
   58       var to = path.join(pkg.path, 'node_modules', file)
   59       // we ignore errors here, because they can legitimately happen, for instance,
   60       // bundled modules will be in both node_modules folders
   61       rename(from, to, andIgnoreErrors(done))
   62     }, cleanup)
   63   }
   64 
   65   function cleanup () {
   66     rimraf(modpath, afterCleanup)
   67   }
   68 
   69   function afterCleanup (rimrafEr) {
   70     if (rimrafEr) log.warn('remove', rimrafEr)
   71     removeEmptyParents(path.resolve(pkg.path, '..'))
   72   }
   73 
   74   function removeEmptyParents (pkgdir) {
   75     fs.rmdir(pkgdir, function (er) {
   76       // FIXME: Make sure windows does what we want here
   77       if (er && er.code !== 'ENOENT') return next()
   78       removeEmptyParents(path.resolve(pkgdir, '..'))
   79     })
   80   }
   81 }