"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/npm/lib/unbuild.js" (8 Mar 2017, 4061 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 module.exports = unbuild
    2 module.exports.rmStuff = rmStuff
    3 unbuild.usage = 'npm unbuild <folder>\n(this is plumbing)'
    4 
    5 var readJson = require('read-package-json')
    6 var gentlyRm = require('./utils/gently-rm.js')
    7 var npm = require('./npm.js')
    8 var path = require('path')
    9 var isInside = require('path-is-inside')
   10 var lifecycle = require('./utils/lifecycle.js')
   11 var asyncMap = require('slide').asyncMap
   12 var chain = require('slide').chain
   13 var log = require('npmlog')
   14 var build = require('./build.js')
   15 var output = require('./utils/output.js')
   16 
   17 // args is a list of folders.
   18 // remove any bins/etc, and then delete the folder.
   19 function unbuild (args, silent, cb) {
   20   if (typeof silent === 'function') {
   21     cb = silent
   22     silent = false
   23   }
   24   asyncMap(args, unbuild_(silent), cb)
   25 }
   26 
   27 function unbuild_ (silent) {
   28   return function (folder, cb_) {
   29     function cb (er) {
   30       cb_(er, path.relative(npm.root, folder))
   31     }
   32     folder = path.resolve(folder)
   33     var base = isInside(folder, npm.prefix) ? npm.prefix : folder
   34     delete build._didBuild[folder]
   35     log.verbose('unbuild', folder.substr(npm.prefix.length + 1))
   36     readJson(path.resolve(folder, 'package.json'), function (er, pkg) {
   37       // if no json, then just trash it, but no scripts or whatever.
   38       if (er) return gentlyRm(folder, false, base, cb)
   39       chain(
   40         [
   41           [lifecycle, pkg, 'preuninstall', folder, false, true],
   42           [lifecycle, pkg, 'uninstall', folder, false, true],
   43           !silent && function (cb) {
   44             output('unbuild ' + pkg._id)
   45             cb()
   46           },
   47           [rmStuff, pkg, folder],
   48           [lifecycle, pkg, 'postuninstall', folder, false, true],
   49           [gentlyRm, folder, false, base]
   50         ],
   51         cb
   52       )
   53     })
   54   }
   55 }
   56 
   57 function rmStuff (pkg, folder, cb) {
   58   // if it's global, and folder is in {prefix}/node_modules,
   59   // then bins are in {prefix}/bin
   60   // otherwise, then bins are in folder/../.bin
   61   var parent = pkg.name[0] === '@' ? path.dirname(path.dirname(folder)) : path.dirname(folder)
   62   var gnm = npm.dir
   63   var top = gnm === parent
   64 
   65   log.verbose('unbuild rmStuff', pkg._id, 'from', gnm)
   66   if (!top) log.verbose('unbuild rmStuff', 'in', parent)
   67   asyncMap([rmBins, rmMans], function (fn, cb) {
   68     fn(pkg, folder, parent, top, cb)
   69   }, cb)
   70 }
   71 
   72 function rmBins (pkg, folder, parent, top, cb) {
   73   if (!pkg.bin) return cb()
   74   var binRoot = top ? npm.bin : path.resolve(parent, '.bin')
   75   asyncMap(Object.keys(pkg.bin), function (b, cb) {
   76     if (process.platform === 'win32') {
   77       chain([ [gentlyRm, path.resolve(binRoot, b) + '.cmd', true, folder],
   78               [gentlyRm, path.resolve(binRoot, b), true, folder] ], cb)
   79     } else {
   80       gentlyRm(path.resolve(binRoot, b), true, folder, cb)
   81     }
   82   }, gentlyRmBinRoot)
   83 
   84   function gentlyRmBinRoot (err) {
   85     if (err || top) return cb(err)
   86     return gentlyRm(binRoot, true, parent, cb)
   87   }
   88 }
   89 
   90 function rmMans (pkg, folder, parent, top, cb) {
   91   if (!pkg.man ||
   92       !top ||
   93       process.platform === 'win32' ||
   94       !npm.config.get('global')) {
   95     return cb()
   96   }
   97   var manRoot = path.resolve(npm.config.get('prefix'), 'share', 'man')
   98   log.verbose('rmMans', 'man files are', pkg.man, 'in', manRoot)
   99   asyncMap(pkg.man, function (man, cb) {
  100     if (Array.isArray(man)) {
  101       man.forEach(rmMan)
  102     } else {
  103       rmMan(man)
  104     }
  105 
  106     function rmMan (man) {
  107       log.silly('rmMan', 'preparing to remove', man)
  108       var parseMan = man.match(/(.*\.([0-9]+)(\.gz)?)$/)
  109       if (!parseMan) {
  110         log.error(
  111           'rmMan', man, 'is not a valid name for a man file.',
  112           'Man files must end with a number, ' +
  113           'and optionally a .gz suffix if they are compressed.'
  114         )
  115         return cb()
  116       }
  117 
  118       var stem = parseMan[1]
  119       var sxn = parseMan[2]
  120       var gz = parseMan[3] || ''
  121       var bn = path.basename(stem)
  122       var manDest = path.join(
  123         manRoot,
  124         'man' + sxn,
  125         (bn.indexOf(pkg.name) === 0 ? bn : pkg.name + '-' + bn) + '.' + sxn + gz
  126       )
  127       gentlyRm(manDest, true, cb)
  128     }
  129   }, cb)
  130 }