"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/npm/lib/dist-tag.js" (11 Apr 2017, 3753 Bytes) of package /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 module.exports = distTag
    2 
    3 var log = require('npmlog')
    4 var npa = require('npm-package-arg')
    5 var semver = require('semver')
    6 
    7 var npm = require('./npm.js')
    8 var mapToRegistry = require('./utils/map-to-registry.js')
    9 var readLocalPkg = require('./utils/read-local-package.js')
   10 var usage = require('./utils/usage')
   11 var output = require('./utils/output.js')
   12 
   13 distTag.usage = usage(
   14   'dist-tag',
   15   'npm dist-tag add <pkg>@<version> [<tag>]' +
   16   '\nnpm dist-tag rm <pkg> <tag>' +
   17   '\nnpm dist-tag ls [<pkg>]'
   18 )
   19 
   20 distTag.completion = function (opts, cb) {
   21   var argv = opts.conf.argv.remain
   22   if (argv.length === 2) {
   23     return cb(null, ['add', 'rm', 'ls'])
   24   }
   25 
   26   switch (argv[2]) {
   27     default:
   28       return cb()
   29   }
   30 }
   31 
   32 function distTag (args, cb) {
   33   var cmd = args.shift()
   34   switch (cmd) {
   35     case 'add': case 'a': case 'set': case 's':
   36       return add(args[0], args[1], cb)
   37     case 'rm': case 'r': case 'del': case 'd': case 'remove':
   38       return remove(args[1], args[0], cb)
   39     case 'ls': case 'l': case 'sl': case 'list':
   40       return list(args[0], cb)
   41     default:
   42       return cb('Usage:\n' + distTag.usage)
   43   }
   44 }
   45 
   46 function add (spec, tag, cb) {
   47   var thing = npa(spec || '')
   48   var pkg = thing.name
   49   var version = thing.rawSpec
   50   var t = (tag || npm.config.get('tag')).trim()
   51 
   52   log.verbose('dist-tag add', t, 'to', pkg + '@' + version)
   53 
   54   if (!pkg || !version || !t) return cb('Usage:\n' + distTag.usage)
   55 
   56   if (semver.validRange(t)) {
   57     var er = new Error('Tag name must not be a valid SemVer range: ' + t)
   58     return cb(er)
   59   }
   60 
   61   fetchTags(pkg, function (er, tags) {
   62     if (er) return cb(er)
   63 
   64     if (tags[t] === version) {
   65       log.warn('dist-tag add', t, 'is already set to version', version)
   66       return cb()
   67     }
   68     tags[t] = version
   69 
   70     mapToRegistry(pkg, npm.config, function (er, uri, auth, base) {
   71       var params = {
   72         'package': pkg,
   73         distTag: t,
   74         version: version,
   75         auth: auth
   76       }
   77 
   78       npm.registry.distTags.add(base, params, function (er) {
   79         if (er) return cb(er)
   80 
   81         output('+' + t + ': ' + pkg + '@' + version)
   82         cb()
   83       })
   84     })
   85   })
   86 }
   87 
   88 function remove (tag, pkg, cb) {
   89   log.verbose('dist-tag del', tag, 'from', pkg)
   90 
   91   fetchTags(pkg, function (er, tags) {
   92     if (er) return cb(er)
   93 
   94     if (!tags[tag]) {
   95       log.info('dist-tag del', tag, 'is not a dist-tag on', pkg)
   96       return cb(new Error(tag + ' is not a dist-tag on ' + pkg))
   97     }
   98 
   99     var version = tags[tag]
  100     delete tags[tag]
  101 
  102     mapToRegistry(pkg, npm.config, function (er, uri, auth, base) {
  103       var params = {
  104         'package': pkg,
  105         distTag: tag,
  106         auth: auth
  107       }
  108 
  109       npm.registry.distTags.rm(base, params, function (er) {
  110         if (er) return cb(er)
  111 
  112         output('-' + tag + ': ' + pkg + '@' + version)
  113         cb()
  114       })
  115     })
  116   })
  117 }
  118 
  119 function list (pkg, cb) {
  120   if (!pkg) {
  121     return readLocalPkg(function (er, pkg) {
  122       if (er) return cb(er)
  123       if (!pkg) return cb(distTag.usage)
  124       list(pkg, cb)
  125     })
  126   }
  127 
  128   fetchTags(pkg, function (er, tags) {
  129     if (er) {
  130       log.error('dist-tag ls', "Couldn't get dist-tag data for", pkg)
  131       return cb(er)
  132     }
  133     var msg = Object.keys(tags).map(function (k) {
  134       return k + ': ' + tags[k]
  135     }).sort().join('\n')
  136     output(msg)
  137     cb(er, tags)
  138   })
  139 }
  140 
  141 function fetchTags (pkg, cb) {
  142   mapToRegistry(pkg, npm.config, function (er, uri, auth, base) {
  143     if (er) return cb(er)
  144 
  145     var params = {
  146       'package': pkg,
  147       auth: auth
  148     }
  149     npm.registry.distTags.fetch(base, params, function (er, tags) {
  150       if (er) return cb(er)
  151       if (!tags || !Object.keys(tags).length) {
  152         return cb(new Error('No dist-tags found for ' + pkg))
  153       }
  154 
  155       cb(null, tags)
  156     })
  157   })
  158 }