1'use strict' 2 3const eu = encodeURIComponent 4const figgyPudding = require('figgy-pudding') 5const getStream = require('get-stream') 6const npmFetch = require('npm-registry-fetch') 7const validate = require('aproba') 8 9const TeamConfig = figgyPudding({ 10 description: {}, 11 Promise: { default: () => Promise } 12}) 13 14const cmd = module.exports = {} 15 16cmd.create = (entity, opts) => { 17 opts = TeamConfig(opts) 18 return pwrap(opts, () => { 19 const { scope, team } = splitEntity(entity) 20 validate('SSO', [scope, team, opts]) 21 return npmFetch.json(`/-/org/${eu(scope)}/team`, opts.concat({ 22 method: 'PUT', 23 scope, 24 body: { name: team, description: opts.description } 25 })) 26 }) 27} 28 29cmd.destroy = (entity, opts) => { 30 opts = TeamConfig(opts) 31 return pwrap(opts, () => { 32 const { scope, team } = splitEntity(entity) 33 validate('SSO', [scope, team, opts]) 34 return npmFetch.json(`/-/team/${eu(scope)}/${eu(team)}`, opts.concat({ 35 method: 'DELETE', 36 scope 37 })) 38 }) 39} 40 41cmd.add = (user, entity, opts) => { 42 opts = TeamConfig(opts) 43 return pwrap(opts, () => { 44 const { scope, team } = splitEntity(entity) 45 validate('SSO', [scope, team, opts]) 46 return npmFetch.json(`/-/team/${eu(scope)}/${eu(team)}/user`, opts.concat({ 47 method: 'PUT', 48 scope, 49 body: { user } 50 })) 51 }) 52} 53 54cmd.rm = (user, entity, opts) => { 55 opts = TeamConfig(opts) 56 return pwrap(opts, () => { 57 const { scope, team } = splitEntity(entity) 58 validate('SSO', [scope, team, opts]) 59 return npmFetch.json(`/-/team/${eu(scope)}/${eu(team)}/user`, opts.concat({ 60 method: 'DELETE', 61 scope, 62 body: { user } 63 })) 64 }) 65} 66 67cmd.lsTeams = (scope, opts) => { 68 opts = TeamConfig(opts) 69 return pwrap(opts, () => getStream.array(cmd.lsTeams.stream(scope, opts))) 70} 71cmd.lsTeams.stream = (scope, opts) => { 72 opts = TeamConfig(opts) 73 validate('SO', [scope, opts]) 74 return npmFetch.json.stream(`/-/org/${eu(scope)}/team`, '.*', opts.concat({ 75 query: { format: 'cli' } 76 })) 77} 78 79cmd.lsUsers = (entity, opts) => { 80 opts = TeamConfig(opts) 81 return pwrap(opts, () => getStream.array(cmd.lsUsers.stream(entity, opts))) 82} 83cmd.lsUsers.stream = (entity, opts) => { 84 opts = TeamConfig(opts) 85 const { scope, team } = splitEntity(entity) 86 validate('SSO', [scope, team, opts]) 87 const uri = `/-/team/${eu(scope)}/${eu(team)}/user` 88 return npmFetch.json.stream(uri, '.*', opts.concat({ 89 query: { format: 'cli' } 90 })) 91} 92 93cmd.edit = () => { 94 throw new Error('edit is not implemented yet') 95} 96 97function splitEntity (entity = '') { 98 let [, scope, team] = entity.match(/^@?([^:]+):(.*)$/) || [] 99 return { scope, team } 100} 101 102function pwrap (opts, fn) { 103 return new opts.Promise((resolve, reject) => { 104 fn().then(resolve, reject) 105 }) 106} 107