"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/npm/lib/unpublish.js" (8 Mar 2017, 3636 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 = unpublish
    3 
    4 var log = require('npmlog')
    5 var npm = require('./npm.js')
    6 var readJson = require('read-package-json')
    7 var path = require('path')
    8 var mapToRegistry = require('./utils/map-to-registry.js')
    9 var npa = require('npm-package-arg')
   10 var getPublishConfig = require('./utils/get-publish-config.js')
   11 var output = require('./utils/output.js')
   12 
   13 unpublish.usage = 'npm unpublish [<@scope>/]<pkg>[@<version>]'
   14 
   15 unpublish.completion = function (opts, cb) {
   16   if (opts.conf.argv.remain.length >= 3) return cb()
   17   npm.commands.whoami([], true, function (er, username) {
   18     if (er) return cb()
   19 
   20     var un = encodeURIComponent(username)
   21     if (!un) return cb()
   22     var byUser = '-/by-user/' + un
   23     mapToRegistry(byUser, npm.config, function (er, uri, auth) {
   24       if (er) return cb(er)
   25 
   26       npm.registry.get(uri, { auth: auth }, function (er, pkgs) {
   27         // do a bit of filtering at this point, so that we don't need
   28         // to fetch versions for more than one thing, but also don't
   29         // accidentally a whole project.
   30         pkgs = pkgs[un]
   31         if (!pkgs || !pkgs.length) return cb()
   32         var pp = npa(opts.partialWord).name
   33         pkgs = pkgs.filter(function (p) {
   34           return p.indexOf(pp) === 0
   35         })
   36         if (pkgs.length > 1) return cb(null, pkgs)
   37         mapToRegistry(pkgs[0], npm.config, function (er, uri, auth) {
   38           if (er) return cb(er)
   39 
   40           npm.registry.get(uri, { auth: auth }, function (er, d) {
   41             if (er) return cb(er)
   42             var vers = Object.keys(d.versions)
   43             if (!vers.length) return cb(null, pkgs)
   44             return cb(null, vers.map(function (v) {
   45               return pkgs[0] + '@' + v
   46             }))
   47           })
   48         })
   49       })
   50     })
   51   })
   52 }
   53 
   54 function unpublish (args, cb) {
   55   if (args.length > 1) return cb(unpublish.usage)
   56 
   57   var thing = args.length ? npa(args[0]) : {}
   58   var project = thing.name
   59   var version = thing.rawSpec
   60 
   61   log.silly('unpublish', 'args[0]', args[0])
   62   log.silly('unpublish', 'thing', thing)
   63   if (!version && !npm.config.get('force')) {
   64     return cb(
   65       'Refusing to delete entire project.\n' +
   66       'Run with --force to do this.\n' +
   67       unpublish.usage
   68     )
   69   }
   70 
   71   if (!project || path.resolve(project) === npm.localPrefix) {
   72     // if there's a package.json in the current folder, then
   73     // read the package name and version out of that.
   74     var cwdJson = path.join(npm.localPrefix, 'package.json')
   75     return readJson(cwdJson, function (er, data) {
   76       if (er && er.code !== 'ENOENT' && er.code !== 'ENOTDIR') return cb(er)
   77       if (er) return cb('Usage:\n' + unpublish.usage)
   78       log.verbose('unpublish', data)
   79       gotProject(data.name, data.version, data.publishConfig, cb)
   80     })
   81   }
   82   return gotProject(project, version, cb)
   83 }
   84 
   85 function gotProject (project, version, publishConfig, cb_) {
   86   if (typeof cb_ !== 'function') {
   87     cb_ = publishConfig
   88     publishConfig = null
   89   }
   90 
   91   function cb (er) {
   92     if (er) return cb_(er)
   93     output('- ' + project + (version ? '@' + version : ''))
   94     cb_()
   95   }
   96 
   97   var mappedConfig = getPublishConfig(publishConfig, npm.config, npm.registry)
   98   var config = mappedConfig.config
   99   var registry = mappedConfig.client
  100 
  101   // remove from the cache first
  102   npm.commands.cache(['clean', project, version], function (er) {
  103     if (er) {
  104       log.error('unpublish', 'Failed to clean cache')
  105       return cb(er)
  106     }
  107 
  108     mapToRegistry(project, config, function (er, uri, auth) {
  109       if (er) return cb(er)
  110 
  111       var params = {
  112         version: version,
  113         auth: auth
  114       }
  115       registry.unpublish(uri, params, cb)
  116     })
  117   })
  118 }