Skip to content

Commit

Permalink
team: stop using npm-registry-client
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Aug 21, 2018
1 parent 21730d3 commit c4dfe41
Showing 1 changed file with 71 additions and 22 deletions.
93 changes: 71 additions & 22 deletions lib/team.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
/* eslint-disable standard/no-callback-literal */
var mapToRegistry = require('./utils/map-to-registry.js')
var npm = require('./npm')
var output = require('./utils/output.js')

const BB = require('bluebird')

const npmConfig = require('./config/figgy-config.js')
const npmFetch = require('npm-registry-fetch')
const output = require('./utils/output.js')
const usage = require('./utils/usage')
const validate = require('aproba')

module.exports = team

team.subcommands = ['create', 'destroy', 'add', 'rm', 'ls', 'edit']

team.usage =
team.usage = usage(
'team',
'npm team create <scope:team>\n' +
'npm team destroy <scope:team>\n' +
'npm team add <scope:team> <user>\n' +
'npm team rm <scope:team> <user>\n' +
'npm team ls <scope>|<scope:team>\n' +
'npm team edit <scope:team>'
)

function UsageError () {
throw Object.assign(new Error(team.usage), {code: 'EUSAGE'})
}

team.completion = function (opts, cb) {
var argv = opts.conf.argv.remain
Expand All @@ -33,24 +44,62 @@ team.completion = function (opts, cb) {
}
}

function team (args, cb) {
const eu = encodeURIComponent

function team ([cmd, entity = '', user = ''], cb) {
// Entities are in the format <scope>:<team>
var cmd = args.shift()
var entity = (args.shift() || '').split(':')
return mapToRegistry('/', npm.config, function (err, uri, auth) {
if (err) { return cb(err) }
try {
return npm.registry.team(cmd, uri, {
auth: auth,
scope: entity[0].replace(/^@/, ''), // '@' prefix on scope is optional.
team: entity[1],
user: args.shift()
}, function (err, data) {
!err && data && output(JSON.stringify(data, undefined, 2))
cb(err, data)
})
} catch (e) {
cb(e.message + '\n\nUsage:\n' + team.usage)
return BB.try(() => {
let [scope, team] = entity.split(':')
scope = scope.replace(/^@/, '')
const opts = npmConfig().concat({
scope: `@${scope}`
})
let uri
switch (cmd) {
case 'create':
validate('SS', [scope, team])
uri = `/-/org/${eu(scope)}/team`
return npmFetch.json(uri, opts.concat({
method: 'PUT',
body: {name: team}
}))
case 'ls':
if (team) {
validate('SS', [scope, team])
uri = `/-/team/${eu(scope)}/${eu(team)}/user`
} else {
validate('S', [scope])
uri = `/-/org/${eu(scope)}/team`
}
return npmFetch.json(uri, opts.concat({query: {format: 'cli'}}))
case 'destroy':
validate('SS', [scope, team])
uri = `/-/team/${eu(scope)}/${eu(team)}`
return npmFetch.json(uri, opts.concat({method: 'DELETE'}))
case 'add':
validate('SSS', [scope, team, user])
uri = `/-/team/${eu(scope)}/${eu(team)}/user`
return npmFetch(uri, opts.concat({
method: 'PUT',
body: {user}
})).then(() => ({}))
case 'rm':
validate('SSS', [scope, team, user])
uri = `/-/team/${eu(scope)}/${eu(team)}/user`
return npmFetch(uri, opts.concat({
method: 'DELETE',
body: {user}
})).then(() => ({}))
case 'edit':
throw new Error('`npm team edit` is not implemented yet.')
default:
UsageError()
}
})
}).then(
data => {
data && output(JSON.stringify(data, undefined, 2))
cb(null, data)
},
err => err.code === 'EUSAGE' ? cb(err.message) : cb(err)
)
}

0 comments on commit c4dfe41

Please sign in to comment.