"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/npm/lib/publish.js" (8 Mar 2017, 5136 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 
    2 module.exports = publish
    3 
    4 var npm = require('./npm.js')
    5 var log = require('npmlog')
    6 var path = require('path')
    7 var readJson = require('read-package-json')
    8 var lifecycle = require('./utils/lifecycle.js')
    9 var chain = require('slide').chain
   10 var mapToRegistry = require('./utils/map-to-registry.js')
   11 var cachedPackageRoot = require('./cache/cached-package-root.js')
   12 var createReadStream = require('graceful-fs').createReadStream
   13 var npa = require('npm-package-arg')
   14 var semver = require('semver')
   15 var getPublishConfig = require('./utils/get-publish-config.js')
   16 var output = require('./utils/output.js')
   17 
   18 publish.usage = 'npm publish [<tarball>|<folder>] [--tag <tag>] [--access <public|restricted>]' +
   19                 "\n\nPublishes '.' if no argument supplied" +
   20                 '\n\nSets tag `latest` if no --tag specified'
   21 
   22 publish.completion = function (opts, cb) {
   23   // publish can complete to a folder with a package.json
   24   // or a tarball, or a tarball url.
   25   // for now, not yet implemented.
   26   return cb()
   27 }
   28 
   29 function publish (args, isRetry, cb) {
   30   if (typeof cb !== 'function') {
   31     cb = isRetry
   32     isRetry = false
   33   }
   34   if (args.length === 0) args = ['.']
   35   if (args.length !== 1) return cb(publish.usage)
   36 
   37   log.verbose('publish', args)
   38 
   39   var t = npm.config.get('tag').trim()
   40   if (semver.validRange(t)) {
   41     var er = new Error('Tag name must not be a valid SemVer range: ' + t)
   42     return cb(er)
   43   }
   44 
   45   var arg = args[0]
   46   // if it's a local folder, then run the prepublish there, first.
   47   readJson(path.resolve(arg, 'package.json'), function (er, data) {
   48     if (er && er.code !== 'ENOENT' && er.code !== 'ENOTDIR') return cb(er)
   49 
   50     if (data) {
   51       if (!data.name) return cb(new Error('No name provided'))
   52       if (!data.version) return cb(new Error('No version provided'))
   53     }
   54 
   55     // Error is OK. Could be publishing a URL or tarball, however, that means
   56     // that we will not have automatically run the prepublish script, since
   57     // that gets run when adding a folder to the cache.
   58     if (er) return cacheAddPublish(arg, false, isRetry, cb)
   59     else cacheAddPublish(arg, true, isRetry, cb)
   60   })
   61 }
   62 
   63 // didPre in this case means that we already ran the prepublish script,
   64 // and that the 'dir' is an actual directory, and not something silly
   65 // like a tarball or name@version thing.
   66 // That means that we can run publish/postpublish in the dir, rather than
   67 // in the cache dir.
   68 function cacheAddPublish (dir, didPre, isRetry, cb) {
   69   npm.commands.cache.add(dir, null, null, false, function (er, data) {
   70     if (er) return cb(er)
   71     log.silly('publish', data)
   72     var cachedir = path.resolve(cachedPackageRoot(data), 'package')
   73     chain(
   74       [
   75         !didPre && [lifecycle, data, 'prepublish', cachedir],
   76         [publish_, dir, data, isRetry, cachedir],
   77         [lifecycle, data, 'publish', didPre ? dir : cachedir],
   78         [lifecycle, data, 'postpublish', didPre ? dir : cachedir]
   79       ],
   80       cb
   81     )
   82   })
   83 }
   84 
   85 function publish_ (arg, data, isRetry, cachedir, cb) {
   86   if (!data) return cb(new Error('no package.json file found'))
   87 
   88   var mappedConfig = getPublishConfig(
   89     data.publishConfig,
   90     npm.config,
   91     npm.registry
   92   )
   93   var config = mappedConfig.config
   94   var registry = mappedConfig.client
   95 
   96   data._npmVersion = npm.version
   97   data._nodeVersion = process.versions.node
   98 
   99   delete data.modules
  100   if (data.private) {
  101     return cb(new Error(
  102       'This package has been marked as private\n' +
  103       "Remove the 'private' field from the package.json to publish it."
  104     ))
  105   }
  106 
  107   mapToRegistry(data.name, config, function (er, registryURI, auth, registryBase) {
  108     if (er) return cb(er)
  109 
  110     var tarballPath = cachedir + '.tgz'
  111 
  112     // we just want the base registry URL in this case
  113     log.verbose('publish', 'registryBase', registryBase)
  114     log.silly('publish', 'uploading', tarballPath)
  115 
  116     data._npmUser = {
  117       name: auth.username,
  118       email: auth.email
  119     }
  120 
  121     var params = {
  122       metadata: data,
  123       body: createReadStream(tarballPath),
  124       auth: auth
  125     }
  126 
  127     // registry-frontdoor cares about the access level, which is only
  128     // configurable for scoped packages
  129     if (config.get('access')) {
  130       if (!npa(data.name).scope && config.get('access') === 'restricted') {
  131         return cb(new Error("Can't restrict access to unscoped packages."))
  132       }
  133 
  134       params.access = config.get('access')
  135     }
  136 
  137     log.showProgress('publish:' + data._id)
  138     registry.publish(registryBase, params, function (er) {
  139       if (er && er.code === 'EPUBLISHCONFLICT' &&
  140           npm.config.get('force') && !isRetry) {
  141         log.warn('publish', 'Forced publish over ' + data._id)
  142         return npm.commands.unpublish([data._id], function (er) {
  143           // ignore errors.  Use the force.  Reach out with your feelings.
  144           // but if it fails again, then report the first error.
  145           publish([arg], er || true, cb)
  146         })
  147       }
  148       // report the unpublish error if this was a retry and unpublish failed
  149       if (er && isRetry && isRetry !== true) return cb(isRetry)
  150       if (er) return cb(er)
  151       output('+ ' + data._id)
  152       cb()
  153     })
  154   })
  155 }