diff options
author | Gar <gar+gh@danger.computer> | 2021-02-25 02:54:50 +0300 |
---|---|---|
committer | Ruy Adorno <ruyadorno@hotmail.com> | 2021-03-05 00:05:08 +0300 |
commit | 4a5dd3a5a200b3f4f7b47168497d8e03dca3a2ca (patch) | |
tree | d34a1ea229b719c3cfbdce85899ceaf67b43e7ab /lib/owner.js | |
parent | b33c760cea7fe2696d35b5530abc1b455980fef1 (diff) |
fix(npm) pass npm context everywhere
Instead of files randomly requiring the npm singleton,
we pass it where it needs to go so that tests don't need
to do so much require mocking everywhere
PR-URL: https://github.com/npm/cli/pull/2772
Credit: @wraithgar
Close: #2772
Reviewed-by: @ruyadorno
Diffstat (limited to 'lib/owner.js')
-rw-r--r-- | lib/owner.js | 212 |
1 files changed, 110 insertions, 102 deletions
diff --git a/lib/owner.js b/lib/owner.js index 6dce3ec70..6cb990488 100644 --- a/lib/owner.js +++ b/lib/owner.js @@ -3,94 +3,138 @@ const npa = require('npm-package-arg') const npmFetch = require('npm-registry-fetch') const pacote = require('pacote') -const npm = require('./npm.js') const output = require('./utils/output.js') const otplease = require('./utils/otplease.js') const readLocalPkg = require('./utils/read-local-package.js') const usageUtil = require('./utils/usage.js') -const usage = usageUtil( - 'owner', - 'npm owner add <user> [<@scope>/]<pkg>' + - '\nnpm owner rm <user> [<@scope>/]<pkg>' + - '\nnpm owner ls [<@scope>/]<pkg>' -) +class Owner { + constructor (npm) { + this.npm = npm + } -const completion = async (opts) => { - const argv = opts.conf.argv.remain - if (argv.length > 3) - return [] + get usage () { + return usageUtil( + 'owner', + 'npm owner add <user> [<@scope>/]<pkg>' + + '\nnpm owner rm <user> [<@scope>/]<pkg>' + + '\nnpm owner ls [<@scope>/]<pkg>' + ) + } - if (argv[1] !== 'owner') - argv.unshift('owner') + get usageError () { + return Object.assign(new Error(this.usage), { code: 'EUSAGE' }) + } - if (argv.length === 2) - return ['add', 'rm', 'ls'] + async completion (opts) { + const argv = opts.conf.argv.remain + if (argv.length > 3) + return [] - // reaches registry in order to autocomplete rm - if (argv[2] === 'rm') { - const opts = { - ...npm.flatOptions, - fullMetadata: true, + if (argv[1] !== 'owner') + argv.unshift('owner') + + if (argv.length === 2) + return ['add', 'rm', 'ls'] + + // reaches registry in order to autocomplete rm + if (argv[2] === 'rm') { + const pkgName = await readLocalPkg(this.npm) + if (!pkgName) + return [] + + const spec = npa(pkgName) + const data = await pacote.packument(spec, { + ...this.npm.flatOptions, + fullMetadata: true, + }) + if (data && data.maintainers && data.maintainers.length) + return data.maintainers.map(m => m.name) } - const pkgName = await readLocalPkg() - if (!pkgName) - return [] + return [] + } - const spec = npa(pkgName) - const data = await pacote.packument(spec, opts) - if (data && data.maintainers && data.maintainers.length) - return data.maintainers.map(m => m.name) + exec (args, cb) { + this.owner(args).then(() => cb()).catch(cb) } - return [] -} -const UsageError = () => - Object.assign(new Error(usage), { code: 'EUSAGE' }) - -const cmd = (args, cb) => owner(args).then(() => cb()).catch(cb) - -const owner = async ([action, ...args]) => { - const opts = npm.flatOptions - 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: - throw UsageError() + async owner ([action, ...args]) { + const opts = this.npm.flatOptions + switch (action) { + case 'ls': + case 'list': + return this.ls(args[0], opts) + case 'add': + return this.add(args[0], args[1], opts) + case 'rm': + case 'remove': + return this.rm(args[0], args[1], opts) + default: + throw this.usageError + } } -} -const ls = async (pkg, opts) => { - if (!pkg) { - const pkgName = await readLocalPkg() - if (!pkgName) - throw UsageError() + async ls (pkg, opts) { + if (!pkg) { + const pkgName = await readLocalPkg(this.npm) + if (!pkgName) + throw this.usageError + + pkg = pkgName + } + + const spec = npa(pkg) + + try { + const packumentOpts = { ...opts, fullMetadata: true } + const { maintainers } = await pacote.packument(spec, packumentOpts) + if (!maintainers || !maintainers.length) + output('no admin found') + else + output(maintainers.map(o => `${o.name} <${o.email}>`).join('\n')) - pkg = pkgName + return maintainers + } catch (err) { + log.error('owner ls', "Couldn't get owner data", pkg) + throw err + } } - const spec = npa(pkg) + async add (user, pkg, opts) { + if (!user) + throw this.usageError - try { - const packumentOpts = { ...opts, fullMetadata: true } - const { maintainers } = await pacote.packument(spec, packumentOpts) - if (!maintainers || !maintainers.length) - output('no admin found') - else - output(maintainers.map(o => `${o.name} <${o.email}>`).join('\n')) + if (!pkg) { + const pkgName = await readLocalPkg(this.npm) + if (!pkgName) + throw this.usageError - return maintainers - } catch (err) { - log.error('owner ls', "Couldn't get owner data", pkg) - throw err + pkg = pkgName + } + log.verbose('owner add', '%s to %s', user, pkg) + + const spec = npa(pkg) + return putOwners(spec, user, opts, validateAddOwner) + } + + async rm (user, pkg, opts) { + if (!user) + throw this.usageError + + if (!pkg) { + const pkgName = await readLocalPkg(this.npm) + if (!pkgName) + throw this.usageError + + pkg = pkgName + } + log.verbose('owner rm', '%s from %s', user, pkg) + + const spec = npa(pkg) + return putOwners(spec, user, opts, validateRmOwner) } } +module.exports = Owner const validateAddOwner = (newOwner, owners) => { owners = owners || [] @@ -109,23 +153,6 @@ const validateAddOwner = (newOwner, owners) => { ] } -const add = async (user, pkg, opts) => { - if (!user) - throw UsageError() - - if (!pkg) { - const pkgName = await readLocalPkg() - if (!pkgName) - throw UsageError() - - pkg = pkgName - } - log.verbose('owner add', '%s to %s', user, pkg) - - const spec = npa(pkg) - return putOwners(spec, user, opts, validateAddOwner) -} - const validateRmOwner = (rmOwner, owners) => { let found = false const m = owners.filter(function (o) { @@ -151,23 +178,6 @@ const validateRmOwner = (rmOwner, owners) => { return m } -const rm = async (user, pkg, opts) => { - if (!user) - throw UsageError() - - if (!pkg) { - const pkgName = await readLocalPkg() - if (!pkgName) - throw UsageError() - - pkg = pkgName - } - log.verbose('owner rm', '%s from %s', user, pkg) - - const spec = npa(pkg) - return putOwners(spec, user, opts, validateRmOwner) -} - const putOwners = async (spec, user, opts, validation) => { const uri = `/-/user/org.couchdb.user:${encodeURIComponent(user)}` let u = '' @@ -227,5 +237,3 @@ const putOwners = async (spec, user, opts, validation) => { } return res } - -module.exports = Object.assign(cmd, { usage, completion }) |