From 8971ba1b953d4f05ff5094f1822b91526282edd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Thu, 16 Aug 2018 16:59:48 -0700 Subject: [PATCH] owner: stop using npm-registry-client --- lib/owner.js | 340 +++++++++++++++++++++++---------------------------- 1 file changed, 152 insertions(+), 188 deletions(-) diff --git a/lib/owner.js b/lib/owner.js index 3c2660ace113d..a64cb5e14ccef 100644 --- a/lib/owner.js +++ b/lib/owner.js @@ -1,12 +1,17 @@ -/* eslint-disable standard/no-callback-literal */ module.exports = owner -var npm = require('./npm.js') -var log = require('npmlog') -var mapToRegistry = require('./utils/map-to-registry.js') -var readLocalPkg = require('./utils/read-local-package.js') -var usage = require('./utils/usage') -var output = require('./utils/output.js') +const BB = require('bluebird') + +const log = require('npmlog') +const npa = require('libnpm/parse-arg') +const npmConfig = require('./config/figgy-config.js') +const npmFetch = require('libnpm/fetch') +const output = require('./utils/output.js') +const otplease = require('./utils/otplease.js') +const packument = require('libnpm/packument') +const readLocalPkg = BB.promisify(require('./utils/read-local-package.js')) +const usage = require('./utils/usage') +const whoami = BB.promisify(require('./whoami.js')) owner.usage = usage( 'owner', @@ -14,8 +19,9 @@ owner.usage = usage( '\nnpm owner rm [<@scope>/]' + '\nnpm owner ls [<@scope>/]' ) + owner.completion = function (opts, cb) { - var argv = opts.conf.argv.remain + const argv = opts.conf.argv.remain if (argv.length > 4) return cb() if (argv.length <= 2) { var subs = ['add', 'rm'] @@ -23,130 +29,109 @@ owner.completion = function (opts, cb) { else subs.push('ls', 'list') return cb(null, subs) } - - npm.commands.whoami([], true, function (er, username) { - if (er) return cb() - - var un = encodeURIComponent(username) - var byUser, theUser - switch (argv[2]) { - case 'ls': - // FIXME: there used to be registry completion here, but it stopped - // making sense somewhere around 50,000 packages on the registry - return cb() - - case 'rm': - if (argv.length > 3) { - theUser = encodeURIComponent(argv[3]) - byUser = '-/by-user/' + theUser + '|' + un - return mapToRegistry(byUser, npm.config, function (er, uri, auth) { - if (er) return cb(er) - - console.error(uri) - npm.registry.get(uri, { auth: auth }, function (er, d) { - if (er) return cb(er) - // return the intersection - return cb(null, d[theUser].filter(function (p) { + BB.try(() => { + const opts = npmConfig() + return whoami([], true).then(username => { + const un = encodeURIComponent(username) + let byUser, theUser + switch (argv[2]) { + case 'ls': + // FIXME: there used to be registry completion here, but it stopped + // making sense somewhere around 50,000 packages on the registry + return + case 'rm': + if (argv.length > 3) { + theUser = encodeURIComponent(argv[3]) + byUser = `/-/by-user/${theUser}|${un}` + return npmFetch.json(byUser, opts).then(d => { + return d[theUser].filter( // kludge for server adminery. - return un === 'isaacs' || d[un].indexOf(p) === -1 - })) + p => un === 'isaacs' || d[un].indexOf(p) === -1 + ) }) - }) - } - // else fallthrough - /* eslint no-fallthrough:0 */ - case 'add': - if (argv.length > 3) { - theUser = encodeURIComponent(argv[3]) - byUser = '-/by-user/' + theUser + '|' + un - return mapToRegistry(byUser, npm.config, function (er, uri, auth) { - if (er) return cb(er) - - console.error(uri) - npm.registry.get(uri, { auth: auth }, function (er, d) { - console.error(uri, er || d) - // return mine that they're not already on. - if (er) return cb(er) + } + // else fallthrough + /* eslint no-fallthrough:0 */ + case 'add': + if (argv.length > 3) { + theUser = encodeURIComponent(argv[3]) + byUser = `/-/by-user/${theUser}|${un}` + return npmFetch.json(byUser, opts).then(d => { var mine = d[un] || [] var theirs = d[theUser] || [] - return cb(null, mine.filter(function (p) { - return theirs.indexOf(p) === -1 - })) + return mine.filter(p => theirs.indexOf(p) === -1) }) - }) - } - // just list all users who aren't me. - return mapToRegistry('-/users', npm.config, function (er, uri, auth) { - if (er) return cb(er) + } else { + // just list all users who aren't me. + return npmFetch.json('/-/users', opts).then(list => { + return Object.keys(list).filter(n => n !== un) + }) + } - npm.registry.get(uri, { auth: auth }, function (er, list) { - if (er) return cb() - return cb(null, Object.keys(list).filter(function (n) { - return n !== un - })) - }) - }) + default: + return cb() + } + }) + }).nodeify(cb) +} - default: - return cb() - } - }) +function UsageError () { + throw Object.assign(new Error(owner.usage), {code: 'EUSAGE'}) } -function owner (args, cb) { - var action = args.shift() - switch (action) { - case 'ls': case 'list': return ls(args[0], cb) - case 'add': return add(args[0], args[1], cb) - case 'rm': case 'remove': return rm(args[0], args[1], cb) - default: return unknown(action, cb) - } +function owner ([action, ...args], cb) { + const opts = npmConfig() + BB.try(() => { + switch (action) { + case 'ls': case 'list': return ls(args[0], opts) + case 'add': return add(args[0], args[1], opts) + case 'rm': case 'remove': return rm(args[0], args[1], opts) + default: UsageError() + } + }).then( + data => cb(null, data), + err => err.code === 'EUSAGE' ? cb(err.message) : cb(err) + ) } -function ls (pkg, cb) { +function ls (pkg, opts) { if (!pkg) { - return readLocalPkg(function (er, pkg) { - if (er) return cb(er) - if (!pkg) return cb(owner.usage) - ls(pkg, cb) + return readLocalPkg().then(pkg => { + if (!pkg) { UsageError() } + return ls(pkg, opts) }) } - mapToRegistry(pkg, npm.config, function (er, uri, auth) { - if (er) return cb(er) - - npm.registry.get(uri, { auth: auth }, function (er, data) { - var msg = '' - if (er) { - log.error('owner ls', "Couldn't get owner data", pkg) - return cb(er) - } + const spec = npa(pkg) + return packument(spec, opts.concat({fullMetadata: true})).then( + data => { var owners = data.maintainers if (!owners || !owners.length) { - msg = 'admin party!' + output('admin party!') } else { - msg = owners.map(function (o) { - return o.name + ' <' + o.email + '>' - }).join('\n') + output(owners.map(o => `${o.name} <${o.email}>`).join('\n')) } - output(msg) - cb(er, owners) - }) - }) + return owners + }, + err => { + log.error('owner ls', "Couldn't get owner data", pkg) + throw err + } + ) } -function add (user, pkg, cb) { - if (!user) return cb(owner.usage) +function add (user, pkg, opts) { + if (!user) { UsageError() } if (!pkg) { - return readLocalPkg(function (er, pkg) { - if (er) return cb(er) - if (!pkg) return cb(new Error(owner.usage)) - add(user, pkg, cb) + return readLocalPkg().then(pkg => { + if (!pkg) { UsageError() } + return add(user, pkg, opts) }) } - log.verbose('owner add', '%s to %s', user, pkg) - mutate(pkg, user, function (u, owners) { + + const spec = npa(pkg) + return withMutation(spec, user, opts, (u, owners) => { if (!owners) owners = [] for (var i = 0, l = owners.length; i < l; i++) { var o = owners[i] @@ -160,22 +145,23 @@ function add (user, pkg, cb) { } owners.push(u) return owners - }, cb) + }) } -function rm (user, pkg, cb) { +function rm (user, pkg, opts) { + if (!user) { UsageError() } if (!pkg) { - return readLocalPkg(function (er, pkg) { - if (er) return cb(er) - if (!pkg) return cb(new Error(owner.usage)) - rm(user, pkg, cb) + return readLocalPkg().then(pkg => { + if (!pkg) { UsageError() } + return add(user, pkg, opts) }) } - log.verbose('owner rm', '%s from %s', user, pkg) - mutate(pkg, user, function (u, owners) { - var found = false - var m = owners.filter(function (o) { + + const spec = npa(pkg) + return withMutation(spec, user, opts, function (u, owners) { + let found = false + const m = owners.filter(function (o) { var match = (o.name === user) found = found || match return !match @@ -187,92 +173,70 @@ function rm (user, pkg, cb) { } if (!m.length) { - return new Error( + throw new Error( 'Cannot remove all owners of a package. Add someone else first.' ) } return m - }, cb) + }) } -function mutate (pkg, user, mutation, cb) { - if (user) { - var byUser = '-/user/org.couchdb.user:' + user - mapToRegistry(byUser, npm.config, function (er, uri, auth) { - if (er) return cb(er) - - npm.registry.get(uri, { auth: auth }, mutate_) - }) - } else { - mutate_(null, null) - } +function withMutation (spec, user, opts, mutation) { + return BB.try(() => { + if (user) { + const uri = `/-/user/org.couchdb.user:${encodeURIComponent(user)}` + return npmFetch.json(uri, opts).then(mutate_, err => { + log.error('owner mutate', 'Error getting user data for %s', user) + throw err + }) + } else { + return mutate_(null) + } + }) - function mutate_ (er, u) { - if (!er && user && (!u || u.error)) { - er = new Error( + function mutate_ (u) { + if (user && (!u || u.error)) { + throw new Error( "Couldn't get user data for " + user + ': ' + JSON.stringify(u) ) } - if (er) { - log.error('owner mutate', 'Error getting user data for %s', user) - return cb(er) - } - if (u) u = { name: u.name, email: u.email } - mapToRegistry(pkg, npm.config, function (er, uri, auth) { - if (er) return cb(er) - - npm.registry.get(uri, { auth: auth }, function (er, data) { - if (er) { - log.error('owner mutate', 'Error getting package data for %s', pkg) - return cb(er) - } - - // save the number of maintainers before mutation so that we can figure - // out if maintainers were added or removed - var beforeMutation = data.maintainers.length - - var m = mutation(u, data.maintainers) - if (!m) return cb() // handled - if (m instanceof Error) return cb(m) // error - - data = { - _id: data._id, - _rev: data._rev, - maintainers: m - } - var dataPath = pkg.replace('/', '%2f') + '/-rev/' + data._rev - mapToRegistry(dataPath, npm.config, function (er, uri, auth) { - if (er) return cb(er) - - var params = { - method: 'PUT', - body: data, - auth: auth - } - npm.registry.request(uri, params, function (er, data) { - if (!er && data.error) { - er = new Error('Failed to update package metadata: ' + JSON.stringify(data)) - } - - if (er) { - log.error('owner mutate', 'Failed to update package metadata') - } else if (m.length > beforeMutation) { - output('+ %s (%s)', user, pkg) - } else if (m.length < beforeMutation) { - output('- %s (%s)', user, pkg) - } - - cb(er, data) - }) + return packument(spec, opts.concat({ + fullMetadata: true + })).then(data => { + // save the number of maintainers before mutation so that we can figure + // out if maintainers were added or removed + const beforeMutation = data.maintainers.length + + const m = mutation(u, data.maintainers) + if (!m) return // handled + if (m instanceof Error) throw m // error + + data = { + _id: data._id, + _rev: data._rev, + maintainers: m + } + const dataPath = `/${spec.escapedName}/-rev/${encodeURIComponent(data._rev)}` + return otplease(opts, opts => { + const reqOpts = opts.concat({ + method: 'PUT', + body: data, + spec }) + return npmFetch.json(dataPath, reqOpts) + }).then(data => { + if (data.error) { + throw new Error('Failed to update package metadata: ' + JSON.stringify(data)) + } else if (m.length > beforeMutation) { + output('+ %s (%s)', user, spec.name) + } else if (m.length < beforeMutation) { + output('- %s (%s)', user, spec.name) + } + return data }) }) } } - -function unknown (action, cb) { - cb('Usage: \n' + owner.usage) -}