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