team.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* eslint-disable standard/no-callback-literal */
  2. const columns = require('cli-columns')
  3. const figgyPudding = require('figgy-pudding')
  4. const libteam = require('libnpm/team')
  5. const npmConfig = require('./config/figgy-config.js')
  6. const output = require('./utils/output.js')
  7. const otplease = require('./utils/otplease.js')
  8. const usage = require('./utils/usage')
  9. module.exports = team
  10. team.subcommands = ['create', 'destroy', 'add', 'rm', 'ls', 'edit']
  11. team.usage = usage(
  12. 'team',
  13. 'npm team create <scope:team> [--otp <otpcode>]\n' +
  14. 'npm team destroy <scope:team> [--otp <otpcode>]\n' +
  15. 'npm team add <scope:team> <user> [--otp <otpcode>]\n' +
  16. 'npm team rm <scope:team> <user> [--otp <otpcode>]\n' +
  17. 'npm team ls <scope>|<scope:team>\n' +
  18. 'npm team edit <scope:team>'
  19. )
  20. const TeamConfig = figgyPudding({
  21. json: {},
  22. loglevel: {},
  23. parseable: {},
  24. silent: {}
  25. })
  26. function UsageError () {
  27. throw Object.assign(new Error(team.usage), {code: 'EUSAGE'})
  28. }
  29. team.completion = function (opts, cb) {
  30. var argv = opts.conf.argv.remain
  31. if (argv.length === 2) {
  32. return cb(null, team.subcommands)
  33. }
  34. switch (argv[2]) {
  35. case 'ls':
  36. case 'create':
  37. case 'destroy':
  38. case 'add':
  39. case 'rm':
  40. case 'edit':
  41. return cb(null, [])
  42. default:
  43. return cb(new Error(argv[2] + ' not recognized'))
  44. }
  45. }
  46. function team ([cmd, entity = '', user = ''], cb) {
  47. // Entities are in the format <scope>:<team>
  48. otplease(npmConfig(), opts => {
  49. opts = TeamConfig(opts).concat({description: null})
  50. entity = entity.replace(/^@/, '')
  51. switch (cmd) {
  52. case 'create': return teamCreate(entity, opts)
  53. case 'destroy': return teamDestroy(entity, opts)
  54. case 'add': return teamAdd(entity, user, opts)
  55. case 'rm': return teamRm(entity, user, opts)
  56. case 'ls': {
  57. const match = entity.match(/[^:]+:.+/)
  58. if (match) {
  59. return teamListUsers(entity, opts)
  60. } else {
  61. return teamListTeams(entity, opts)
  62. }
  63. }
  64. case 'edit':
  65. throw new Error('`npm team edit` is not implemented yet.')
  66. default:
  67. UsageError()
  68. }
  69. }).then(
  70. data => cb(null, data),
  71. err => err.code === 'EUSAGE' ? cb(err.message) : cb(err)
  72. )
  73. }
  74. function teamCreate (entity, opts) {
  75. return libteam.create(entity, opts).then(() => {
  76. if (opts.json) {
  77. output(JSON.stringify({
  78. created: true,
  79. team: entity
  80. }))
  81. } else if (opts.parseable) {
  82. output(`${entity}\tcreated`)
  83. } else if (!opts.silent && opts.loglevel !== 'silent') {
  84. output(`+@${entity}`)
  85. }
  86. })
  87. }
  88. function teamDestroy (entity, opts) {
  89. return libteam.destroy(entity, opts).then(() => {
  90. if (opts.json) {
  91. output(JSON.stringify({
  92. deleted: true,
  93. team: entity
  94. }))
  95. } else if (opts.parseable) {
  96. output(`${entity}\tdeleted`)
  97. } else if (!opts.silent && opts.loglevel !== 'silent') {
  98. output(`-@${entity}`)
  99. }
  100. })
  101. }
  102. function teamAdd (entity, user, opts) {
  103. return libteam.add(user, entity, opts).then(() => {
  104. if (opts.json) {
  105. output(JSON.stringify({
  106. added: true,
  107. team: entity,
  108. user
  109. }))
  110. } else if (opts.parseable) {
  111. output(`${user}\t${entity}\tadded`)
  112. } else if (!opts.silent && opts.loglevel !== 'silent') {
  113. output(`${user} added to @${entity}`)
  114. }
  115. })
  116. }
  117. function teamRm (entity, user, opts) {
  118. return libteam.rm(user, entity, opts).then(() => {
  119. if (opts.json) {
  120. output(JSON.stringify({
  121. removed: true,
  122. team: entity,
  123. user
  124. }))
  125. } else if (opts.parseable) {
  126. output(`${user}\t${entity}\tremoved`)
  127. } else if (!opts.silent && opts.loglevel !== 'silent') {
  128. output(`${user} removed from @${entity}`)
  129. }
  130. })
  131. }
  132. function teamListUsers (entity, opts) {
  133. return libteam.lsUsers(entity, opts).then(users => {
  134. users = users.sort()
  135. if (opts.json) {
  136. output(JSON.stringify(users, null, 2))
  137. } else if (opts.parseable) {
  138. output(users.join('\n'))
  139. } else if (!opts.silent && opts.loglevel !== 'silent') {
  140. output(`\n@${entity} has ${users.length} user${users.length === 1 ? '' : 's'}:\n`)
  141. output(columns(users, {padding: 1}))
  142. }
  143. })
  144. }
  145. function teamListTeams (entity, opts) {
  146. return libteam.lsTeams(entity, opts).then(teams => {
  147. teams = teams.sort()
  148. if (opts.json) {
  149. output(JSON.stringify(teams, null, 2))
  150. } else if (opts.parseable) {
  151. output(teams.join('\n'))
  152. } else if (!opts.silent && opts.loglevel !== 'silent') {
  153. output(`\n@${entity} has ${teams.length} team${teams.length === 1 ? '' : 's'}:\n`)
  154. output(columns(teams.map(t => `@${t}`), {padding: 1}))
  155. }
  156. })
  157. }