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 Dec 10, 2018
1 parent 8971ba1 commit 99156e0
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 36 deletions.
157 changes: 136 additions & 21 deletions lib/team.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
/* 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 columns = require('cli-columns')
const figgyPudding = require('figgy-pudding')
const libteam = require('libnpm/team')
const npmConfig = require('./config/figgy-config.js')
const output = require('./utils/output.js')
const otplease = require('./utils/otplease.js')
const usage = require('./utils/usage')

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>'
)

const TeamConfig = figgyPudding({
json: {},
loglevel: {},
parseable: {},
silent: {}
})

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 +51,121 @@ team.completion = function (opts, cb) {
}
}

function team (args, cb) {
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)
otplease(npmConfig(), opts => {
opts = TeamConfig(opts).concat({description: null})
entity = entity.replace(/^@/, '')
switch (cmd) {
case 'create': return teamCreate(entity, opts)
case 'destroy': return teamDestroy(entity, opts)
case 'add': return teamAdd(entity, user, opts)
case 'rm': return teamRm(entity, user, opts)
case 'ls': {
const match = entity.match(/[^:]+:.+/)
if (match) {
return teamListUsers(entity, opts)
} else {
return teamListTeams(entity, opts)
}
}
case 'edit':
throw new Error('`npm team edit` is not implemented yet.')
default:
UsageError()
}
}).then(
data => cb(null, data),
err => err.code === 'EUSAGE' ? cb(err.message) : cb(err)
)
}

function teamCreate (entity, opts) {
return libteam.create(entity, opts).then(() => {
if (opts.json) {
output(JSON.stringify({
created: true,
team: entity
}))
} else if (opts.parseable) {
output(`${entity}\tcreated`)
} else if (!opts.silent && opts.loglevel !== 'silent') {
output(`+@${entity}`)
}
})
}

function teamDestroy (entity, opts) {
return libteam.destroy(entity, opts).then(() => {
if (opts.json) {
output(JSON.stringify({
deleted: true,
team: entity
}))
} else if (opts.parseable) {
output(`${entity}\tdeleted`)
} else if (!opts.silent && opts.loglevel !== 'silent') {
output(`-@${entity}`)
}
})
}

function teamAdd (entity, user, opts) {
return libteam.add(user, entity, opts).then(() => {
if (opts.json) {
output(JSON.stringify({
added: true,
team: entity,
user
}))
} else if (opts.parseable) {
output(`${user}\t${entity}\tadded`)
} else if (!opts.silent && opts.loglevel !== 'silent') {
output(`${user} added to @${entity}`)
}
})
}

function teamRm (entity, user, opts) {
return libteam.rm(user, entity, opts).then(() => {
if (opts.json) {
output(JSON.stringify({
removed: true,
team: entity,
user
}))
} else if (opts.parseable) {
output(`${user}\t${entity}\tremoved`)
} else if (!opts.silent && opts.loglevel !== 'silent') {
output(`${user} removed from @${entity}`)
}
})
}

function teamListUsers (entity, opts) {
return libteam.lsUsers(entity, opts).then(users => {
users = users.sort()
if (opts.json) {
output(JSON.stringify(users, null, 2))
} else if (opts.parseable) {
output(users.join('\n'))
} else if (!opts.silent && opts.loglevel !== 'silent') {
output(`\n@${entity} has ${users.length} user${users.length === 1 ? '' : 's'}:\n`)
output(columns(users, {padding: 1}))
}
})
}

function teamListTeams (entity, opts) {
return libteam.lsTeams(entity, opts).then(teams => {
teams = teams.sort()
if (opts.json) {
output(JSON.stringify(teams, null, 2))
} else if (opts.parseable) {
output(teams.join('\n'))
} else if (!opts.silent && opts.loglevel !== 'silent') {
output(`\n@${entity} has ${teams.length} team${teams.length === 1 ? '' : 's'}:\n`)
output(columns(teams.map(t => `@${t}`), {padding: 1}))
}
})
}
39 changes: 24 additions & 15 deletions test/tap/team.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ test('team create basic', function (t) {
deleted: null
}
server.put('/-/org/myorg/team', JSON.stringify({
name: teamData.name
name: teamData.name,
description: null
})).reply(200, teamData)
common.npm([
'team', 'create', 'myorg:' + teamData.name,
'--registry', common.registry,
'--loglevel', 'silent'
'--loglevel', 'error',
'--json'
], {}, function (err, code, stdout, stderr) {
t.ifError(err, 'npm team')
t.equal(code, 0, 'exited OK')
t.equal(stderr, '', 'no error output')
t.same(JSON.parse(stdout), teamData)
t.same(JSON.parse(stdout), {created: true, team: `myorg:${teamData.name}`})
t.end()
})
})
Expand All @@ -46,17 +48,19 @@ test('team create (allow optional @ prefix on scope)', function (t) {
deleted: null
}
server.put('/-/org/myorg/team', JSON.stringify({
name: teamData.name
name: teamData.name,
description: null
})).reply(200, teamData)
common.npm([
'team', 'create', '@myorg:' + teamData.name,
'--registry', common.registry,
'--loglevel', 'silent'
'--loglevel', 'silent',
'--json'
], {}, function (err, code, stdout, stderr) {
t.ifError(err, 'npm team')
t.equal(code, 0, 'exited OK')
t.equal(stderr, '', 'no error output')
t.same(JSON.parse(stdout), teamData)
t.same(JSON.parse(stdout), {created: true, team: `myorg:${teamData.name}`})
t.end()
})
})
Expand All @@ -73,12 +77,13 @@ test('team destroy', function (t) {
common.npm([
'team', 'destroy', 'myorg:' + teamData.name,
'--registry', common.registry,
'--loglevel', 'silent'
'--loglevel', 'silent',
'--json'
], {}, function (err, code, stdout, stderr) {
t.ifError(err, 'npm team')
t.equal(code, 0, 'exited OK')
t.equal(stderr, '', 'no error output')
t.same(JSON.parse(stdout), teamData)
t.same(JSON.parse(stdout), {deleted: true, team: `myorg:${teamData.name}`})
t.end()
})
})
Expand All @@ -87,11 +92,12 @@ test('team add', function (t) {
var user = 'zkat'
server.put('/-/team/myorg/myteam/user', JSON.stringify({
user: user
})).reply(200)
})).reply(200, {})
common.npm([
'team', 'add', 'myorg:myteam', user,
'--registry', common.registry,
'--loglevel', 'silent'
'--loglevel', 'error',
'--json'
], {}, function (err, code, stdout, stderr) {
t.ifError(err, 'npm team')
t.equal(code, 0, 'exited OK')
Expand All @@ -104,11 +110,12 @@ test('team rm', function (t) {
var user = 'zkat'
server.delete('/-/team/myorg/myteam/user', JSON.stringify({
user: user
})).reply(200)
})).reply(200, {})
common.npm([
'team', 'rm', 'myorg:myteam', user,
'--registry', common.registry,
'--loglevel', 'silent'
'--loglevel', 'silent',
'--json'
], {}, function (err, code, stdout, stderr) {
t.ifError(err, 'npm team')
t.equal(code, 0, 'exited OK')
Expand All @@ -123,7 +130,8 @@ test('team ls (on org)', function (t) {
common.npm([
'team', 'ls', 'myorg',
'--registry', common.registry,
'--loglevel', 'silent'
'--loglevel', 'silent',
'--json'
], {}, function (err, code, stdout, stderr) {
t.ifError(err, 'npm team')
t.equal(code, 0, 'exited OK')
Expand All @@ -139,12 +147,13 @@ test('team ls (on team)', function (t) {
common.npm([
'team', 'ls', 'myorg:myteam',
'--registry', common.registry,
'--loglevel', 'silent'
'--loglevel', 'silent',
'--json'
], {}, function (err, code, stdout, stderr) {
t.ifError(err, 'npm team')
t.equal(code, 0, 'exited OK')
t.equal(stderr, '', 'no error output')
t.same(JSON.parse(stdout), users)
t.same(JSON.parse(stdout).sort(), users.sort())
t.end()
})
})
Expand Down

0 comments on commit 99156e0

Please sign in to comment.