Skip to content

Commit 50463f5

Browse files
committed
org: improve usage errors and add org ls filtering
1 parent cfea6ea commit 50463f5

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

lib/org.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ org.subcommands = ['set', 'rm', 'ls']
1414
org.usage =
1515
'npm org set orgname username [developer | admin | owner]\n' +
1616
'npm org rm orgname username\n' +
17-
'npm org ls orgname'
17+
'npm org ls orgname [<username>]'
1818

1919
const OrgConfig = figgyPudding({
2020
json: {},
@@ -53,17 +53,27 @@ function org ([cmd, orgname, username, role], cb) {
5353
case 'rm':
5454
return orgRm(orgname, username, opts)
5555
case 'ls':
56-
return orgList(orgname, opts)
56+
return orgList(orgname, username, opts)
5757
default:
5858
UsageError()
5959
}
6060
}).then(
6161
x => cb(null, x),
62-
err => err.code === 'EUSAGE' ? err.message : err
63-
).catch(cb)
62+
err => cb(err.code === 'EUSAGE' ? err.message : err)
63+
)
6464
}
6565

6666
function orgSet (org, user, role, opts) {
67+
role = role || 'developer'
68+
if (!org) {
69+
throw new Error('First argument `orgname` is required.')
70+
}
71+
if (!user) {
72+
throw new Error('Second argument `username` is required.')
73+
}
74+
if (!['owner', 'admin', 'developer'].find(role)) {
75+
throw new Error('Third argument `role` must be one of `owner`, `admin`, or `developer`, with `developer` being the default value if omitted.')
76+
}
6777
return liborg.set(org, user, role, opts).then(memDeets => {
6878
if (opts.json) {
6979
output(JSON.stringify(memDeets, null, 2))
@@ -83,6 +93,12 @@ function orgSet (org, user, role, opts) {
8393
}
8494

8595
function orgRm (org, user, opts) {
96+
if (!org) {
97+
throw new Error('First argument `orgname` is required.')
98+
}
99+
if (!user) {
100+
throw new Error('Second argument `username` is required.')
101+
}
86102
return liborg.rm(org, user, opts).then(() => {
87103
return liborg.ls(org, opts)
88104
}).then(roster => {
@@ -105,8 +121,18 @@ function orgRm (org, user, opts) {
105121
})
106122
}
107123

108-
function orgList (org, opts) {
124+
function orgList (org, user, opts) {
125+
if (!org) {
126+
throw new Error('First argument `orgname` is required.')
127+
}
109128
return liborg.ls(org, opts).then(roster => {
129+
if (user) {
130+
const newRoster = {}
131+
if (roster[user]) {
132+
newRoster[user] = roster[user]
133+
}
134+
roster = newRoster
135+
}
110136
if (opts.json) {
111137
output(JSON.stringify(roster, null, 2))
112138
} else if (opts.parseable) {

0 commit comments

Comments
 (0)