Skip to content

Commit

Permalink
org: improve usage errors and add org ls filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Jan 18, 2019
1 parent cfea6ea commit 50463f5
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions lib/org.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ org.subcommands = ['set', 'rm', 'ls']
org.usage =
'npm org set orgname username [developer | admin | owner]\n' +
'npm org rm orgname username\n' +
'npm org ls orgname'
'npm org ls orgname [<username>]'

const OrgConfig = figgyPudding({
json: {},
Expand Down Expand Up @@ -53,17 +53,27 @@ function org ([cmd, orgname, username, role], cb) {
case 'rm':
return orgRm(orgname, username, opts)
case 'ls':
return orgList(orgname, opts)
return orgList(orgname, username, opts)
default:
UsageError()
}
}).then(
x => cb(null, x),
err => err.code === 'EUSAGE' ? err.message : err
).catch(cb)
err => cb(err.code === 'EUSAGE' ? err.message : err)
)
}

function orgSet (org, user, role, opts) {
role = role || 'developer'
if (!org) {
throw new Error('First argument `orgname` is required.')
}
if (!user) {
throw new Error('Second argument `username` is required.')
}
if (!['owner', 'admin', 'developer'].find(role)) {
throw new Error('Third argument `role` must be one of `owner`, `admin`, or `developer`, with `developer` being the default value if omitted.')
}
return liborg.set(org, user, role, opts).then(memDeets => {
if (opts.json) {
output(JSON.stringify(memDeets, null, 2))
Expand All @@ -83,6 +93,12 @@ function orgSet (org, user, role, opts) {
}

function orgRm (org, user, opts) {
if (!org) {
throw new Error('First argument `orgname` is required.')
}
if (!user) {
throw new Error('Second argument `username` is required.')
}
return liborg.rm(org, user, opts).then(() => {
return liborg.ls(org, opts)
}).then(roster => {
Expand All @@ -105,8 +121,18 @@ function orgRm (org, user, opts) {
})
}

function orgList (org, opts) {
function orgList (org, user, opts) {
if (!org) {
throw new Error('First argument `orgname` is required.')
}
return liborg.ls(org, opts).then(roster => {
if (user) {
const newRoster = {}
if (roster[user]) {
newRoster[user] = roster[user]
}
roster = newRoster
}
if (opts.json) {
output(JSON.stringify(roster, null, 2))
} else if (opts.parseable) {
Expand Down

0 comments on commit 50463f5

Please sign in to comment.