"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/npm/lib/install/save.js" (8 Mar 2017, 6620 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 fs = require('graceful-fs')
    3 var path = require('path')
    4 var url = require('url')
    5 var writeFileAtomic = require('write-file-atomic')
    6 var log = require('npmlog')
    7 var semver = require('semver')
    8 var iferr = require('iferr')
    9 var validate = require('aproba')
   10 var without = require('lodash.without')
   11 var npm = require('../npm.js')
   12 var deepSortObject = require('../utils/deep-sort-object.js')
   13 var parseJSON = require('../utils/parse-json.js')
   14 var moduleName = require('../utils/module-name.js')
   15 var isOnlyDev = require('./is-dev.js').isOnlyDev
   16 
   17 // if the -S|--save option is specified, then write installed packages
   18 // as dependencies to a package.json file.
   19 
   20 exports.saveRequested = function (args, tree, andReturn) {
   21   validate('AOF', arguments)
   22   savePackageJson(args, tree, andWarnErrors(andSaveShrinkwrap(tree, andReturn)))
   23 }
   24 
   25 function andSaveShrinkwrap (tree, andReturn) {
   26   validate('OF', arguments)
   27   return function (er) {
   28     validate('E', arguments)
   29     saveShrinkwrap(tree, andWarnErrors(andReturn))
   30   }
   31 }
   32 
   33 function andWarnErrors (cb) {
   34   validate('F', arguments)
   35   return function (er) {
   36     if (er) log.warn('saveError', er.message)
   37     arguments[0] = null
   38     cb.apply(null, arguments)
   39   }
   40 }
   41 
   42 function saveShrinkwrap (tree, next) {
   43   validate('OF', arguments)
   44   var saveTarget = path.resolve(tree.path, 'npm-shrinkwrap.json')
   45   fs.stat(saveTarget, function (er, stat) {
   46     if (er) return next()
   47     var save = npm.config.get('save')
   48     var saveDev = npm.config.get('save-dev')
   49     var saveOptional = npm.config.get('save-optional')
   50 
   51     var shrinkwrap = tree.package._shrinkwrap || {dependencies: {}}
   52     var hasDevOnlyDeps = tree.requires.filter(function (dep) {
   53       return isOnlyDev(dep)
   54     }).some(function (dep) {
   55       return shrinkwrap.dependencies[dep.package.name] != null
   56     })
   57 
   58     if (!saveOptional && saveDev && !hasDevOnlyDeps) return next()
   59     if (saveOptional || !save) return next()
   60 
   61     if (hasDevOnlyDeps) {
   62       var dev = npm.config.get('dev')
   63       npm.config.set('dev', true)
   64       npm.commands.shrinkwrap([], true, function () {
   65         npm.config.set('dev', dev)
   66         next.apply(this, arguments)
   67       })
   68     } else {
   69       npm.commands.shrinkwrap([], true, next)
   70     }
   71   })
   72 }
   73 
   74 function savePackageJson (args, tree, next) {
   75   validate('AOF', arguments)
   76   var saveBundle = npm.config.get('save-bundle')
   77 
   78   // each item in the tree is a top-level thing that should be saved
   79   // to the package.json file.
   80   // The relevant tree shape is { <folder>: {what:<pkg>} }
   81   var saveTarget = path.resolve(tree.path, 'package.json')
   82   // don't use readJson, because we don't want to do all the other
   83   // tricky npm-specific stuff that's in there.
   84   fs.readFile(saveTarget, iferr(next, function (packagejson) {
   85     try {
   86       packagejson = parseJSON(packagejson)
   87     } catch (ex) {
   88       return next(ex)
   89     }
   90 
   91     // If we're saving bundled deps, normalize the key before we start
   92     if (saveBundle) {
   93       var bundle = packagejson.bundleDependencies || packagejson.bundledDependencies
   94       delete packagejson.bundledDependencies
   95       if (!Array.isArray(bundle)) bundle = []
   96     }
   97 
   98     var toSave = getThingsToSave(tree)
   99     var toRemove = getThingsToRemove(args, tree)
  100     var savingTo = {}
  101     toSave.forEach(function (pkg) { savingTo[pkg.save] = true })
  102     toRemove.forEach(function (pkg) { savingTo[pkg.save] = true })
  103 
  104     Object.keys(savingTo).forEach(function (save) {
  105       if (!packagejson[save]) packagejson[save] = {}
  106     })
  107 
  108     log.verbose('saving', toSave)
  109     toSave.forEach(function (pkg) {
  110       packagejson[pkg.save][pkg.name] = pkg.spec
  111       if (saveBundle) {
  112         var ii = bundle.indexOf(pkg.name)
  113         if (ii === -1) bundle.push(pkg.name)
  114       }
  115     })
  116 
  117     toRemove.forEach(function (pkg) {
  118       delete packagejson[pkg.save][pkg.name]
  119       if (saveBundle) {
  120         bundle = without(bundle, pkg.name)
  121       }
  122     })
  123 
  124     Object.keys(savingTo).forEach(function (key) {
  125       packagejson[key] = deepSortObject(packagejson[key])
  126     })
  127     if (saveBundle) {
  128       packagejson.bundledDependencies = deepSortObject(bundle)
  129     }
  130 
  131     var json = JSON.stringify(packagejson, null, 2) + '\n'
  132     writeFileAtomic(saveTarget, json, next)
  133   }))
  134 }
  135 
  136 var getSaveType = exports.getSaveType = function (args) {
  137   validate('A', arguments)
  138   var nothingToSave = !args.length
  139   var globalInstall = npm.config.get('global')
  140   var noSaveFlags = !npm.config.get('save') &&
  141                     !npm.config.get('save-dev') &&
  142                     !npm.config.get('save-optional')
  143   if (nothingToSave || globalInstall || noSaveFlags) return null
  144 
  145   if (npm.config.get('save-optional')) return 'optionalDependencies'
  146   else if (npm.config.get('save-dev')) return 'devDependencies'
  147   else return 'dependencies'
  148 }
  149 
  150 function computeVersionSpec (child) {
  151   validate('O', arguments)
  152   var requested = child.package._requested
  153   if (!requested || requested.type === 'tag') {
  154     requested = {
  155       type: 'version',
  156       spec: child.package.version
  157     }
  158   }
  159   if (requested.type === 'version' || requested.type === 'range') {
  160     var version = child.package.version
  161     var rangeDescriptor = ''
  162     if (semver.valid(version, true) &&
  163         semver.gte(version, '0.1.0', true) &&
  164         !npm.config.get('save-exact')) {
  165       rangeDescriptor = npm.config.get('save-prefix')
  166     }
  167     return rangeDescriptor + version
  168   } else if (requested.type === 'directory' || requested.type === 'local') {
  169     var relativePath = path.relative(child.parent.path, requested.spec)
  170     if (/^[.][.]/.test(relativePath)) {
  171       return url.format({
  172         protocol: 'file',
  173         slashes: true,
  174         pathname: requested.spec
  175       })
  176     } else {
  177       return url.format({
  178         protocol: 'file',
  179         slashes: false,
  180         pathname: relativePath
  181       })
  182     }
  183   } else if (requested.type === 'hosted') {
  184     return requested.spec
  185   } else {
  186     return requested.rawSpec
  187   }
  188 }
  189 
  190 function getThingsToSave (tree) {
  191   validate('O', arguments)
  192   var toSave = tree.children.filter(function (child) {
  193     return child.save
  194   }).map(function (child) {
  195     return {
  196       name: moduleName(child),
  197       spec: computeVersionSpec(child),
  198       save: child.save
  199     }
  200   })
  201   return toSave
  202 }
  203 
  204 function getThingsToRemove (args, tree) {
  205   validate('AO', arguments)
  206   if (!tree.removed) return []
  207   var toRemove = tree.removed.map(function (child) {
  208     return {
  209       name: moduleName(child),
  210       save: child.save
  211     }
  212   })
  213   var saveType = getSaveType(args)
  214   args.forEach(function (arg) {
  215     toRemove.push({
  216       name: arg,
  217       save: saveType
  218     })
  219   })
  220   return toRemove
  221 }