org.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. 'use strict'
  2. const figgyPudding = require('figgy-pudding')
  3. const liborg = require('libnpm/org')
  4. const npmConfig = require('./config/figgy-config.js')
  5. const output = require('./utils/output.js')
  6. const otplease = require('./utils/otplease.js')
  7. const Table = require('cli-table3')
  8. module.exports = org
  9. org.subcommands = ['set', 'rm', 'ls']
  10. org.usage =
  11. 'npm org set orgname username [developer | admin | owner]\n' +
  12. 'npm org rm orgname username\n' +
  13. 'npm org ls orgname [<username>]'
  14. const OrgConfig = figgyPudding({
  15. json: {},
  16. loglevel: {},
  17. parseable: {},
  18. silent: {}
  19. })
  20. org.completion = function (opts, cb) {
  21. var argv = opts.conf.argv.remain
  22. if (argv.length === 2) {
  23. return cb(null, org.subcommands)
  24. }
  25. switch (argv[2]) {
  26. case 'ls':
  27. case 'add':
  28. case 'rm':
  29. case 'set':
  30. return cb(null, [])
  31. default:
  32. return cb(new Error(argv[2] + ' not recognized'))
  33. }
  34. }
  35. function UsageError () {
  36. throw Object.assign(new Error(org.usage), {code: 'EUSAGE'})
  37. }
  38. function org ([cmd, orgname, username, role], cb) {
  39. otplease(npmConfig(), opts => {
  40. opts = OrgConfig(opts)
  41. switch (cmd) {
  42. case 'add':
  43. case 'set':
  44. return orgSet(orgname, username, role, opts)
  45. case 'rm':
  46. return orgRm(orgname, username, opts)
  47. case 'ls':
  48. return orgList(orgname, username, opts)
  49. default:
  50. UsageError()
  51. }
  52. }).then(
  53. x => cb(null, x),
  54. err => cb(err.code === 'EUSAGE' ? err.message : err)
  55. )
  56. }
  57. function orgSet (org, user, role, opts) {
  58. role = role || 'developer'
  59. if (!org) {
  60. throw new Error('First argument `orgname` is required.')
  61. }
  62. if (!user) {
  63. throw new Error('Second argument `username` is required.')
  64. }
  65. if (!['owner', 'admin', 'developer'].find(x => x === role)) {
  66. throw new Error('Third argument `role` must be one of `owner`, `admin`, or `developer`, with `developer` being the default value if omitted.')
  67. }
  68. return liborg.set(org, user, role, opts).then(memDeets => {
  69. if (opts.json) {
  70. output(JSON.stringify(memDeets, null, 2))
  71. } else if (opts.parseable) {
  72. output(['org', 'orgsize', 'user', 'role'].join('\t'))
  73. output([
  74. memDeets.org.name,
  75. memDeets.org.size,
  76. memDeets.user,
  77. memDeets.role
  78. ])
  79. } else if (!opts.silent && opts.loglevel !== 'silent') {
  80. output(`Added ${memDeets.user} as ${memDeets.role} to ${memDeets.org.name}. You now ${memDeets.org.size} member${memDeets.org.size === 1 ? '' : 's'} in this org.`)
  81. }
  82. return memDeets
  83. })
  84. }
  85. function orgRm (org, user, opts) {
  86. if (!org) {
  87. throw new Error('First argument `orgname` is required.')
  88. }
  89. if (!user) {
  90. throw new Error('Second argument `username` is required.')
  91. }
  92. return liborg.rm(org, user, opts).then(() => {
  93. return liborg.ls(org, opts)
  94. }).then(roster => {
  95. user = user.replace(/^[~@]?/, '')
  96. org = org.replace(/^[~@]?/, '')
  97. const userCount = Object.keys(roster).length
  98. if (opts.json) {
  99. output(JSON.stringify({
  100. user,
  101. org,
  102. userCount,
  103. deleted: true
  104. }))
  105. } else if (opts.parseable) {
  106. output(['user', 'org', 'userCount', 'deleted'].join('\t'))
  107. output([user, org, userCount, true].join('\t'))
  108. } else if (!opts.silent && opts.loglevel !== 'silent') {
  109. output(`Successfully removed ${user} from ${org}. You now have ${userCount} member${userCount === 1 ? '' : 's'} in this org.`)
  110. }
  111. })
  112. }
  113. function orgList (org, user, opts) {
  114. if (!org) {
  115. throw new Error('First argument `orgname` is required.')
  116. }
  117. return liborg.ls(org, opts).then(roster => {
  118. if (user) {
  119. const newRoster = {}
  120. if (roster[user]) {
  121. newRoster[user] = roster[user]
  122. }
  123. roster = newRoster
  124. }
  125. if (opts.json) {
  126. output(JSON.stringify(roster, null, 2))
  127. } else if (opts.parseable) {
  128. output(['user', 'role'].join('\t'))
  129. Object.keys(roster).forEach(user => {
  130. output([user, roster[user]].join('\t'))
  131. })
  132. } else if (!opts.silent && opts.loglevel !== 'silent') {
  133. const table = new Table({head: ['user', 'role']})
  134. Object.keys(roster).sort().forEach(user => {
  135. table.push([user, roster[user]])
  136. })
  137. output(table.toString())
  138. }
  139. })
  140. }