access.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. 'use strict'
  2. /* eslint-disable standard/no-callback-literal */
  3. const BB = require('bluebird')
  4. const figgyPudding = require('figgy-pudding')
  5. const libaccess = require('libnpm/access')
  6. const npmConfig = require('./config/figgy-config.js')
  7. const output = require('./utils/output.js')
  8. const otplease = require('./utils/otplease.js')
  9. const path = require('path')
  10. const prefix = require('./npm.js').prefix
  11. const readPackageJson = BB.promisify(require('read-package-json'))
  12. const usage = require('./utils/usage.js')
  13. const whoami = require('./whoami.js')
  14. module.exports = access
  15. access.usage = usage(
  16. 'npm access',
  17. 'npm access public [<package>]\n' +
  18. 'npm access restricted [<package>]\n' +
  19. 'npm access grant <read-only|read-write> <scope:team> [<package>]\n' +
  20. 'npm access revoke <scope:team> [<package>]\n' +
  21. 'npm access 2fa-required [<package>]\n' +
  22. 'npm access 2fa-not-required [<package>]\n' +
  23. 'npm access ls-packages [<user>|<scope>|<scope:team>]\n' +
  24. 'npm access ls-collaborators [<package> [<user>]]\n' +
  25. 'npm access edit [<package>]'
  26. )
  27. access.subcommands = [
  28. 'public', 'restricted', 'grant', 'revoke',
  29. 'ls-packages', 'ls-collaborators', 'edit',
  30. '2fa-required', '2fa-not-required'
  31. ]
  32. const AccessConfig = figgyPudding({
  33. json: {}
  34. })
  35. function UsageError (msg = '') {
  36. throw Object.assign(new Error(
  37. (msg ? `\nUsage: ${msg}\n\n` : '') +
  38. access.usage
  39. ), {code: 'EUSAGE'})
  40. }
  41. access.completion = function (opts, cb) {
  42. var argv = opts.conf.argv.remain
  43. if (argv.length === 2) {
  44. return cb(null, access.subcommands)
  45. }
  46. switch (argv[2]) {
  47. case 'grant':
  48. if (argv.length === 3) {
  49. return cb(null, ['read-only', 'read-write'])
  50. } else {
  51. return cb(null, [])
  52. }
  53. case 'public':
  54. case 'restricted':
  55. case 'ls-packages':
  56. case 'ls-collaborators':
  57. case 'edit':
  58. case '2fa-required':
  59. case '2fa-not-required':
  60. return cb(null, [])
  61. case 'revoke':
  62. return cb(null, [])
  63. default:
  64. return cb(new Error(argv[2] + ' not recognized'))
  65. }
  66. }
  67. function access ([cmd, ...args], cb) {
  68. return BB.try(() => {
  69. const fn = access.subcommands.includes(cmd) && access[cmd]
  70. if (!cmd) { UsageError('Subcommand is required.') }
  71. if (!fn) { UsageError(`${cmd} is not a recognized subcommand.`) }
  72. return fn(args, AccessConfig(npmConfig()))
  73. }).then(
  74. x => cb(null, x),
  75. err => err.code === 'EUSAGE' ? cb(err.message) : cb(err)
  76. )
  77. }
  78. access.public = ([pkg], opts) => {
  79. return modifyPackage(pkg, opts, libaccess.public)
  80. }
  81. access.restricted = ([pkg], opts) => {
  82. return modifyPackage(pkg, opts, libaccess.restricted)
  83. }
  84. access.grant = ([perms, scopeteam, pkg], opts) => {
  85. return BB.try(() => {
  86. if (!perms || (perms !== 'read-only' && perms !== 'read-write')) {
  87. UsageError('First argument must be either `read-only` or `read-write.`')
  88. }
  89. if (!scopeteam) {
  90. UsageError('`<scope:team>` argument is required.')
  91. }
  92. const [, scope, team] = scopeteam.match(/^@?([^:]+):(.*)$/) || []
  93. if (!scope && !team) {
  94. UsageError(
  95. 'Second argument used incorrect format.\n' +
  96. 'Example: @example:developers'
  97. )
  98. }
  99. return modifyPackage(pkg, opts, (pkgName, opts) => {
  100. return libaccess.grant(pkgName, scopeteam, perms, opts)
  101. }, false)
  102. })
  103. }
  104. access.revoke = ([scopeteam, pkg], opts) => {
  105. return BB.try(() => {
  106. if (!scopeteam) {
  107. UsageError('`<scope:team>` argument is required.')
  108. }
  109. const [, scope, team] = scopeteam.match(/^@?([^:]+):(.*)$/) || []
  110. if (!scope || !team) {
  111. UsageError(
  112. 'First argument used incorrect format.\n' +
  113. 'Example: @example:developers'
  114. )
  115. }
  116. return modifyPackage(pkg, opts, (pkgName, opts) => {
  117. return libaccess.revoke(pkgName, scopeteam, opts)
  118. })
  119. })
  120. }
  121. access['2fa-required'] = access.tfaRequired = ([pkg], opts) => {
  122. return modifyPackage(pkg, opts, libaccess.tfaRequired, false)
  123. }
  124. access['2fa-not-required'] = access.tfaNotRequired = ([pkg], opts) => {
  125. return modifyPackage(pkg, opts, libaccess.tfaNotRequired, false)
  126. }
  127. access['ls-packages'] = access.lsPackages = ([owner], opts) => {
  128. return (
  129. owner ? BB.resolve(owner) : BB.fromNode(cb => whoami([], true, cb))
  130. ).then(owner => {
  131. return libaccess.lsPackages(owner, opts)
  132. }).then(pkgs => {
  133. // TODO - print these out nicely (breaking change)
  134. output(JSON.stringify(pkgs, null, 2))
  135. })
  136. }
  137. access['ls-collaborators'] = access.lsCollaborators = ([pkg, usr], opts) => {
  138. return getPackage(pkg, false).then(pkgName =>
  139. libaccess.lsCollaborators(pkgName, usr, opts)
  140. ).then(collabs => {
  141. // TODO - print these out nicely (breaking change)
  142. output(JSON.stringify(collabs, null, 2))
  143. })
  144. }
  145. access['edit'] = () => BB.reject(new Error('edit subcommand is not implemented yet'))
  146. function modifyPackage (pkg, opts, fn, requireScope = true) {
  147. return getPackage(pkg, requireScope).then(pkgName =>
  148. otplease(opts, opts => fn(pkgName, opts))
  149. )
  150. }
  151. function getPackage (name, requireScope = true) {
  152. return BB.try(() => {
  153. if (name && name.trim()) {
  154. return name.trim()
  155. } else {
  156. return readPackageJson(
  157. path.resolve(prefix, 'package.json')
  158. ).then(
  159. data => data.name,
  160. err => {
  161. if (err.code === 'ENOENT') {
  162. throw new Error('no package name passed to command and no package.json found')
  163. } else {
  164. throw err
  165. }
  166. }
  167. )
  168. }
  169. }).then(name => {
  170. if (requireScope && !name.match(/^@[^/]+\/.*$/)) {
  171. UsageError('This command is only available for scoped packages.')
  172. } else {
  173. return name
  174. }
  175. })
  176. }