3 var resolve = require('path').resolve
5 var readPackageJson = require('read-package-json')
6 var mapToRegistry = require('./utils/map-to-registry.js')
7 var npm = require('./npm.js')
9 var whoami = require('./whoami')
11 module.exports = access
14 'npm access public [<package>]\n' +
15 'npm access restricted [<package>]\n' +
16 'npm access grant <read-only|read-write> <scope:team> [<package>]\n' +
17 'npm access revoke <scope:team> [<package>]\n' +
18 'npm access ls-packages [<user>|<scope>|<scope:team>]\n' +
19 'npm access ls-collaborators [<package> [<user>]]\n' +
20 'npm access edit [<package>]'
22 access.subcommands = ['public', 'restricted', 'grant', 'revoke',
23 'ls-packages', 'ls-collaborators', 'edit']
25 access.completion = function (opts, cb) {
26 var argv = opts.conf.argv.remain
27 if (argv.length === 2) {
28 return cb(null, access.subcommands)
33 if (argv.length === 3) {
34 return cb(null, ['read-only', 'read-write'])
42 case 'ls-collaborators':
48 return cb(new Error(argv[2] + ' not recognized'))
52 function access (args, cb) {
53 var cmd = args.shift()
55 return parseParams(cmd, args, function (err, p) {
56 if (err) { return cb(err) }
58 return mapToRegistry(params.package, npm.config, invokeCmd)
61 function invokeCmd (err, uri, auth, base) {
62 if (err) { return cb(err) }
65 return npm.registry.access(cmd, uri, params, function (err, data) {
66 !err && data && console.log(JSON.stringify(data, undefined, 2))
70 cb(e.message + '\n\nUsage:\n' + access.usage)
75 function parseParams (cmd, args, cb) {
76 // mapToRegistry will complain if package is undefined,
77 // but it's not needed for ls-packages
78 var params = { 'package': '' }
79 if (cmd === 'grant') {
80 params.permissions = args.shift()
82 if (['grant', 'revoke', 'ls-packages'].indexOf(cmd) !== -1) {
83 var entity = (args.shift() || '').split(':')
84 params.scope = entity[0]
85 params.team = entity[1]
88 if (cmd === 'ls-packages') {
90 whoami([], true, function (err, scope) {
98 getPackage(args.shift(), function (err, pkg) {
99 if (err) return cb(err)
102 if (cmd === 'ls-collaborators') params.user = args.shift()
108 function getPackage (name, cb) {
109 if (name && name.trim()) {
110 cb(null, name.trim())
113 resolve(npm.prefix, 'package.json'),
114 function (err, data) {
116 if (err.code === 'ENOENT') {
117 cb(new Error('no package name passed to command and no package.json found'))