From 50463f58b4b70180a85d6d8c10fcf50d8970ef5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Fri, 18 Jan 2019 09:05:41 -0800 Subject: [PATCH] org: improve usage errors and add org ls filtering --- lib/org.js | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/lib/org.js b/lib/org.js index 9947bcb5663ae..38b7dbd27c798 100644 --- a/lib/org.js +++ b/lib/org.js @@ -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 []' const OrgConfig = figgyPudding({ json: {}, @@ -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)) @@ -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 => { @@ -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) {