"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/npm/lib/shrinkwrap.js" (7 Feb 2017, 4992 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 // emit JSON describing versions of all packages currently installed (for later
    2 // use with shrinkwrap install)
    3 
    4 module.exports = exports = shrinkwrap
    5 
    6 var path = require('path')
    7 var log = require('npmlog')
    8 var writeFileAtomic = require('write-file-atomic')
    9 var iferr = require('iferr')
   10 var readPackageJson = require('read-package-json')
   11 var readPackageTree = require('read-package-tree')
   12 var validate = require('aproba')
   13 var chain = require('slide').chain
   14 var npm = require('./npm.js')
   15 var recalculateMetadata = require('./install/deps.js').recalculateMetadata
   16 var validatePeerDeps = require('./install/deps.js').validatePeerDeps
   17 var isExtraneous = require('./install/is-extraneous.js')
   18 var isOnlyDev = require('./install/is-dev.js').isOnlyDev
   19 var packageId = require('./utils/package-id.js')
   20 var moduleName = require('./utils/module-name.js')
   21 var output = require('./utils/output.js')
   22 var lifecycle = require('./utils/lifecycle.js')
   23 
   24 shrinkwrap.usage = 'npm shrinkwrap'
   25 
   26 function shrinkwrap (args, silent, cb) {
   27   if (typeof cb !== 'function') {
   28     cb = silent
   29     silent = false
   30   }
   31 
   32   if (args.length) {
   33     log.warn('shrinkwrap', "doesn't take positional args")
   34   }
   35 
   36   var dir = path.resolve(npm.dir, '..')
   37   var packagePath = path.join(npm.localPrefix, 'package.json')
   38   npm.config.set('production', true)
   39 
   40   readPackageJson(packagePath, iferr(cb, function (data) {
   41     lifecycle(data, 'preshrinkwrap', function () {
   42       readPackageTree(dir, andRecalculateMetadata(iferr(cb, function (tree) {
   43         var pkginfo = treeToShrinkwrap(tree, !!npm.config.get('dev') || /^dev(elopment)?$/.test(npm.config.get('also')))
   44 
   45         chain([
   46           [lifecycle, tree.package, 'shrinkwrap'],
   47           [shrinkwrap_, pkginfo, silent],
   48           [lifecycle, tree.package, 'postshrinkwrap']
   49         ], iferr(cb, function (data) {
   50           cb(null, data[0])
   51         }))
   52       })))
   53     })
   54   }))
   55 }
   56 
   57 function andRecalculateMetadata (next) {
   58   validate('F', arguments)
   59   return function (er, tree) {
   60     validate('EO', arguments)
   61     if (er) return next(er)
   62     recalculateMetadata(tree, log, next)
   63   }
   64 }
   65 
   66 function treeToShrinkwrap (tree, dev) {
   67   validate('OB', arguments)
   68   var pkginfo = {}
   69   if (tree.package.name) pkginfo.name = tree.package.name
   70   if (tree.package.version) pkginfo.version = tree.package.version
   71   var problems = []
   72   if (tree.children.length) {
   73     shrinkwrapDeps(dev, problems, pkginfo.dependencies = {}, tree)
   74   }
   75   if (problems.length) pkginfo.problems = problems
   76   return pkginfo
   77 }
   78 
   79 function shrinkwrapDeps (dev, problems, deps, tree, seen) {
   80   validate('BAOO', [dev, problems, deps, tree])
   81   if (!seen) seen = {}
   82   if (seen[tree.path]) return
   83   seen[tree.path] = true
   84   Object.keys(tree.missingDeps).forEach(function (name) {
   85     var invalid = tree.children.filter(function (dep) { return moduleName(dep) === name })[0]
   86     if (invalid) {
   87       problems.push('invalid: have ' + invalid.package._id + ' (expected: ' + tree.missingDeps[name] + ') ' + invalid.path)
   88     } else if (!tree.package.optionalDependencies || !tree.package.optionalDependencies[name]) {
   89       var topname = packageId(tree)
   90       problems.push('missing: ' + name + '@' + tree.package.dependencies[name] +
   91         (topname ? ', required by ' + topname : ''))
   92     }
   93   })
   94   tree.children.sort(function (aa, bb) { return moduleName(aa).localeCompare(moduleName(bb)) }).forEach(function (child) {
   95     if (!dev && isOnlyDev(child)) {
   96       log.warn('shrinkwrap', 'Excluding devDependency: %s', packageId(child), child.parent.package.dependencies)
   97       return
   98     }
   99     var pkginfo = deps[moduleName(child)] = {}
  100     pkginfo.version = child.package.version
  101     pkginfo.from = child.package._from
  102     pkginfo.resolved = child.package._resolved
  103     if (isExtraneous(child)) {
  104       problems.push('extraneous: ' + child.package._id + ' ' + child.path)
  105     }
  106     validatePeerDeps(child, function (tree, pkgname, version) {
  107       problems.push('peer invalid: ' + pkgname + '@' + version +
  108         ', required by ' + child.package._id)
  109     })
  110     if (child.children.length) {
  111       shrinkwrapDeps(dev, problems, pkginfo.dependencies = {}, child, seen)
  112     }
  113   })
  114 }
  115 
  116 function shrinkwrap_ (pkginfo, silent, cb) {
  117   if (pkginfo.problems) {
  118     return cb(new Error('Problems were encountered\n' +
  119                         'Please correct and try again.\n' +
  120                         pkginfo.problems.join('\n')))
  121   }
  122 
  123   save(pkginfo, silent, cb)
  124 }
  125 
  126 function save (pkginfo, silent, cb) {
  127   // copy the keys over in a well defined order
  128   // because javascript objects serialize arbitrarily
  129   var swdata
  130   try {
  131     swdata = JSON.stringify(pkginfo, null, 2) + '\n'
  132   } catch (er) {
  133     log.error('shrinkwrap', 'Error converting package info to json')
  134     return cb(er)
  135   }
  136 
  137   var file = path.resolve(npm.prefix, 'npm-shrinkwrap.json')
  138 
  139   writeFileAtomic(file, swdata, function (er) {
  140     if (er) return cb(er)
  141     if (silent) return cb(null, pkginfo)
  142     output('wrote npm-shrinkwrap.json')
  143     cb(null, pkginfo)
  144   })
  145 }