"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/npm/lib/owner.js" (8 Mar 2017, 7728 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 module.exports = owner
    2 
    3 var npm = require('./npm.js')
    4 var log = require('npmlog')
    5 var mapToRegistry = require('./utils/map-to-registry.js')
    6 var readLocalPkg = require('./utils/read-local-package.js')
    7 var usage = require('./utils/usage')
    8 var output = require('./utils/output.js')
    9 
   10 owner.usage = usage(
   11   'owner',
   12   'npm owner add <user> [<@scope>/]<pkg>' +
   13   '\nnpm owner rm <user> [<@scope>/]<pkg>' +
   14   '\nnpm owner ls [<@scope>/]<pkg>'
   15 )
   16 owner.completion = function (opts, cb) {
   17   var argv = opts.conf.argv.remain
   18   if (argv.length > 4) return cb()
   19   if (argv.length <= 2) {
   20     var subs = ['add', 'rm']
   21     if (opts.partialWord === 'l') subs.push('ls')
   22     else subs.push('ls', 'list')
   23     return cb(null, subs)
   24   }
   25 
   26   npm.commands.whoami([], true, function (er, username) {
   27     if (er) return cb()
   28 
   29     var un = encodeURIComponent(username)
   30     var byUser, theUser
   31     switch (argv[2]) {
   32       case 'ls':
   33         // FIXME: there used to be registry completion here, but it stopped
   34         // making sense somewhere around 50,000 packages on the registry
   35         return cb()
   36 
   37       case 'rm':
   38         if (argv.length > 3) {
   39           theUser = encodeURIComponent(argv[3])
   40           byUser = '-/by-user/' + theUser + '|' + un
   41           return mapToRegistry(byUser, npm.config, function (er, uri, auth) {
   42             if (er) return cb(er)
   43 
   44             console.error(uri)
   45             npm.registry.get(uri, { auth: auth }, function (er, d) {
   46               if (er) return cb(er)
   47               // return the intersection
   48               return cb(null, d[theUser].filter(function (p) {
   49                 // kludge for server adminery.
   50                 return un === 'isaacs' || d[un].indexOf(p) === -1
   51               }))
   52             })
   53           })
   54         }
   55         // else fallthrough
   56         /*eslint no-fallthrough:0*/
   57       case 'add':
   58         if (argv.length > 3) {
   59           theUser = encodeURIComponent(argv[3])
   60           byUser = '-/by-user/' + theUser + '|' + un
   61           return mapToRegistry(byUser, npm.config, function (er, uri, auth) {
   62             if (er) return cb(er)
   63 
   64             console.error(uri)
   65             npm.registry.get(uri, { auth: auth }, function (er, d) {
   66               console.error(uri, er || d)
   67               // return mine that they're not already on.
   68               if (er) return cb(er)
   69               var mine = d[un] || []
   70               var theirs = d[theUser] || []
   71               return cb(null, mine.filter(function (p) {
   72                 return theirs.indexOf(p) === -1
   73               }))
   74             })
   75           })
   76         }
   77         // just list all users who aren't me.
   78         return mapToRegistry('-/users', npm.config, function (er, uri, auth) {
   79           if (er) return cb(er)
   80 
   81           npm.registry.get(uri, { auth: auth }, function (er, list) {
   82             if (er) return cb()
   83             return cb(null, Object.keys(list).filter(function (n) {
   84               return n !== un
   85             }))
   86           })
   87         })
   88 
   89       default:
   90         return cb()
   91     }
   92   })
   93 }
   94 
   95 function owner (args, cb) {
   96   var action = args.shift()
   97   switch (action) {
   98     case 'ls': case 'list': return ls(args[0], cb)
   99     case 'add': return add(args[0], args[1], cb)
  100     case 'rm': case 'remove': return rm(args[0], args[1], cb)
  101     default: return unknown(action, cb)
  102   }
  103 }
  104 
  105 function ls (pkg, cb) {
  106   if (!pkg) {
  107     return readLocalPkg(function (er, pkg) {
  108       if (er) return cb(er)
  109       if (!pkg) return cb(owner.usage)
  110       ls(pkg, cb)
  111     })
  112   }
  113 
  114   mapToRegistry(pkg, npm.config, function (er, uri, auth) {
  115     if (er) return cb(er)
  116 
  117     npm.registry.get(uri, { auth: auth }, function (er, data) {
  118       var msg = ''
  119       if (er) {
  120         log.error('owner ls', "Couldn't get owner data", pkg)
  121         return cb(er)
  122       }
  123       var owners = data.maintainers
  124       if (!owners || !owners.length) {
  125         msg = 'admin party!'
  126       } else {
  127         msg = owners.map(function (o) {
  128           return o.name + ' <' + o.email + '>'
  129         }).join('\n')
  130       }
  131       output(msg)
  132       cb(er, owners)
  133     })
  134   })
  135 }
  136 
  137 function add (user, pkg, cb) {
  138   if (!user) return cb(owner.usage)
  139   if (!pkg) {
  140     return readLocalPkg(function (er, pkg) {
  141       if (er) return cb(er)
  142       if (!pkg) return cb(new Error(owner.usage))
  143       add(user, pkg, cb)
  144     })
  145   }
  146 
  147   log.verbose('owner add', '%s to %s', user, pkg)
  148   mutate(pkg, user, function (u, owners) {
  149     if (!owners) owners = []
  150     for (var i = 0, l = owners.length; i < l; i++) {
  151       var o = owners[i]
  152       if (o.name === u.name) {
  153         log.info(
  154           'owner add',
  155           'Already a package owner: ' + o.name + ' <' + o.email + '>'
  156         )
  157         return false
  158       }
  159     }
  160     owners.push(u)
  161     return owners
  162   }, cb)
  163 }
  164 
  165 function rm (user, pkg, cb) {
  166   if (!pkg) {
  167     return readLocalPkg(function (er, pkg) {
  168       if (er) return cb(er)
  169       if (!pkg) return cb(new Error(owner.usage))
  170       rm(user, pkg, cb)
  171     })
  172   }
  173 
  174   log.verbose('owner rm', '%s from %s', user, pkg)
  175   mutate(pkg, user, function (u, owners) {
  176     var found = false
  177     var m = owners.filter(function (o) {
  178       var match = (o.name === user)
  179       found = found || match
  180       return !match
  181     })
  182 
  183     if (!found) {
  184       log.info('owner rm', 'Not a package owner: ' + user)
  185       return false
  186     }
  187 
  188     if (!m.length) {
  189       return new Error(
  190         'Cannot remove all owners of a package.  Add someone else first.'
  191       )
  192     }
  193 
  194     return m
  195   }, cb)
  196 }
  197 
  198 function mutate (pkg, user, mutation, cb) {
  199   if (user) {
  200     var byUser = '-/user/org.couchdb.user:' + user
  201     mapToRegistry(byUser, npm.config, function (er, uri, auth) {
  202       if (er) return cb(er)
  203 
  204       npm.registry.get(uri, { auth: auth }, mutate_)
  205     })
  206   } else {
  207     mutate_(null, null)
  208   }
  209 
  210   function mutate_ (er, u) {
  211     if (!er && user && (!u || u.error)) {
  212       er = new Error(
  213         "Couldn't get user data for " + user + ': ' + JSON.stringify(u)
  214       )
  215     }
  216 
  217     if (er) {
  218       log.error('owner mutate', 'Error getting user data for %s', user)
  219       return cb(er)
  220     }
  221 
  222     if (u) u = { name: u.name, email: u.email }
  223     mapToRegistry(pkg, npm.config, function (er, uri, auth) {
  224       if (er) return cb(er)
  225 
  226       npm.registry.get(uri, { auth: auth }, function (er, data) {
  227         if (er) {
  228           log.error('owner mutate', 'Error getting package data for %s', pkg)
  229           return cb(er)
  230         }
  231 
  232         // save the number of maintainers before mutation so that we can figure
  233         // out if maintainers were added or removed
  234         var beforeMutation = data.maintainers.length
  235 
  236         var m = mutation(u, data.maintainers)
  237         if (!m) return cb() // handled
  238         if (m instanceof Error) return cb(m) // error
  239 
  240         data = {
  241           _id: data._id,
  242           _rev: data._rev,
  243           maintainers: m
  244         }
  245         var dataPath = pkg.replace('/', '%2f') + '/-rev/' + data._rev
  246         mapToRegistry(dataPath, npm.config, function (er, uri, auth) {
  247           if (er) return cb(er)
  248 
  249           var params = {
  250             method: 'PUT',
  251             body: data,
  252             auth: auth
  253           }
  254           npm.registry.request(uri, params, function (er, data) {
  255             if (!er && data.error) {
  256               er = new Error('Failed to update package metadata: ' + JSON.stringify(data))
  257             }
  258 
  259             if (er) {
  260               log.error('owner mutate', 'Failed to update package metadata')
  261             } else if (m.length > beforeMutation) {
  262               output('+ %s (%s)', user, pkg)
  263             } else if (m.length < beforeMutation) {
  264               output('- %s (%s)', user, pkg)
  265             }
  266 
  267             cb(er, data)
  268           })
  269         })
  270       })
  271     })
  272   }
  273 }
  274 
  275 function unknown (action, cb) {
  276   cb('Usage: \n' + owner.usage)
  277 }