"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/npm/lib/cache/add-local.js" (8 Mar 2017, 4282 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 var assert = require('assert')
    2 var path = require('path')
    3 var mkdir = require('mkdirp')
    4 var chownr = require('chownr')
    5 var pathIsInside = require('path-is-inside')
    6 var readJson = require('read-package-json')
    7 var log = require('npmlog')
    8 var npm = require('../npm.js')
    9 var tar = require('../utils/tar.js')
   10 var deprCheck = require('../utils/depr-check.js')
   11 var getCacheStat = require('./get-stat.js')
   12 var cachedPackageRoot = require('./cached-package-root.js')
   13 var addLocalTarball = require('./add-local-tarball.js')
   14 var sha = require('sha')
   15 var inflight = require('inflight')
   16 var lifecycle = require('../utils/lifecycle.js')
   17 var iferr = require('iferr')
   18 
   19 module.exports = addLocal
   20 
   21 function addLocal (p, pkgData, cb_) {
   22   assert(typeof p === 'object', 'must have spec info')
   23   assert(typeof cb_ === 'function', 'must have callback')
   24 
   25   pkgData = pkgData || {}
   26 
   27   function cb (er, data) {
   28     if (er) {
   29       log.error('addLocal', 'Could not install %s', p.spec)
   30       return cb_(er)
   31     }
   32     if (data && !data._fromHosted) {
   33       data._from = path.relative(npm.prefix, p.spec) || '.'
   34       var resolved = path.relative(npm.prefix, p.spec)
   35       if (resolved) data._resolved = 'file:' + resolved
   36     }
   37     return cb_(er, data)
   38   }
   39 
   40   if (p.type === 'directory') {
   41     addLocalDirectory(p.spec, pkgData, null, cb)
   42   } else {
   43     addLocalTarball(p.spec, pkgData, null, cb)
   44   }
   45 }
   46 
   47 // At this point, if shasum is set, it's something that we've already
   48 // read and checked.  Just stashing it in the data at this point.
   49 function addLocalDirectory (p, pkgData, shasum, cb) {
   50   assert(pkgData, 'must pass package data')
   51   assert(typeof cb === 'function', 'must have callback')
   52 
   53   // if it's a folder, then read the package.json,
   54   // tar it to the proper place, and add the cache tar
   55   if (pathIsInside(p, npm.cache)) {
   56     return cb(new Error(
   57     'Adding a cache directory to the cache will make the world implode.'
   58     ))
   59   }
   60 
   61   readJson(path.join(p, 'package.json'), false, function (er, data) {
   62     if (er) return cb(er)
   63 
   64     if (!data.name) {
   65       return cb(new Error('No name provided in package.json'))
   66     } else if (pkgData.name && pkgData.name !== data.name) {
   67       return cb(new Error(
   68         'Invalid package: expected ' + pkgData.name + ' but found ' + data.name
   69       ))
   70     }
   71 
   72     if (!data.version) {
   73       return cb(new Error('No version provided in package.json'))
   74     } else if (pkgData.version && pkgData.version !== data.version) {
   75       return cb(new Error(
   76         'Invalid package: expected ' + pkgData.name + '@' + pkgData.version +
   77           ' but found ' + data.name + '@' + data.version
   78       ))
   79     }
   80 
   81     deprCheck(data)
   82 
   83     // pack to {cache}/name/ver/package.tgz
   84     var root = cachedPackageRoot(data)
   85     var tgz = path.resolve(root, 'package.tgz')
   86     var pj = path.resolve(root, 'package/package.json')
   87 
   88     var wrapped = inflight(tgz, next)
   89     if (!wrapped) return log.verbose('addLocalDirectory', tgz, 'already in flight; waiting')
   90     log.verbose('addLocalDirectory', tgz, 'not in flight; packing')
   91 
   92     getCacheStat(function (er, cs) {
   93       mkdir(path.dirname(pj), function (er, made) {
   94         if (er) return wrapped(er)
   95         var doPrePublish = !pathIsInside(p, npm.tmp)
   96         if (doPrePublish) {
   97           lifecycle(data, 'prepublish', p, iferr(wrapped, thenPack))
   98         } else {
   99           thenPack()
  100         }
  101         function thenPack () {
  102           tar.pack(tgz, p, data, function (er) {
  103             if (er) {
  104               log.error('addLocalDirectory', 'Could not pack', p, 'to', tgz)
  105               return wrapped(er)
  106             }
  107 
  108             if (!cs || isNaN(cs.uid) || isNaN(cs.gid)) return wrapped()
  109 
  110             chownr(made || tgz, cs.uid, cs.gid, function (er) {
  111               if (er && er.code === 'ENOENT') return wrapped()
  112               wrapped(er)
  113             })
  114           })
  115         }
  116       })
  117     })
  118 
  119     function next (er) {
  120       if (er) return cb(er)
  121       // if we have the shasum already, just add it
  122       if (shasum) {
  123         return addLocalTarball(tgz, data, shasum, cb)
  124       } else {
  125         sha.get(tgz, function (er, shasum) {
  126           if (er) {
  127             return cb(er)
  128           }
  129           data._shasum = shasum
  130           return addLocalTarball(tgz, data, shasum, cb)
  131         })
  132       }
  133     }
  134   })
  135 }