1 | 'use strict'
|
2 |
|
3 | var resolve = require('path').resolve
|
4 |
|
5 | var readPackageJson = require('read-package-json')
|
6 | var mapToRegistry = require('./utils/map-to-registry.js')
|
7 | var npm = require('./npm.js')
|
8 | var output = require('./utils/output.js')
|
9 |
|
10 | var whoami = require('./whoami')
|
11 |
|
12 | module.exports = access
|
13 |
|
14 | access.usage =
|
15 | 'npm access public [<package>]\n' +
|
16 | 'npm access restricted [<package>]\n' +
|
17 | 'npm access grant <read-only|read-write> <scope:team> [<package>]\n' +
|
18 | 'npm access revoke <scope:team> [<package>]\n' +
|
19 | 'npm access ls-packages [<user>|<scope>|<scope:team>]\n' +
|
20 | 'npm access ls-collaborators [<package> [<user>]]\n' +
|
21 | 'npm access edit [<package>]'
|
22 |
|
23 | access.subcommands = ['public', 'restricted', 'grant', 'revoke',
|
24 | 'ls-packages', 'ls-collaborators', 'edit']
|
25 |
|
26 | access.completion = function (opts, cb) {
|
27 | var argv = opts.conf.argv.remain
|
28 | if (argv.length === 2) {
|
29 | return cb(null, access.subcommands)
|
30 | }
|
31 |
|
32 | switch (argv[2]) {
|
33 | case 'grant':
|
34 | if (argv.length === 3) {
|
35 | return cb(null, ['read-only', 'read-write'])
|
36 | } else {
|
37 | return cb(null, [])
|
38 | }
|
39 | case 'public':
|
40 | case 'restricted':
|
41 | case 'ls-packages':
|
42 | case 'ls-collaborators':
|
43 | case 'edit':
|
44 | return cb(null, [])
|
45 | case 'revoke':
|
46 | return cb(null, [])
|
47 | default:
|
48 | return cb(new Error(argv[2] + ' not recognized'))
|
49 | }
|
50 | }
|
51 |
|
52 | function access (args, cb) {
|
53 | var cmd = args.shift()
|
54 | var params
|
55 | return parseParams(cmd, args, function (err, p) {
|
56 | if (err) { return cb(err) }
|
57 | params = p
|
58 | return mapToRegistry(params.package, npm.config, invokeCmd)
|
59 | })
|
60 |
|
61 | function invokeCmd (err, uri, auth, base) {
|
62 | if (err) { return cb(err) }
|
63 | params.auth = auth
|
64 | try {
|
65 | return npm.registry.access(cmd, uri, params, function (err, data) {
|
66 | if (!err && data) {
|
67 | output(JSON.stringify(data, undefined, 2))
|
68 | }
|
69 | cb(err, data)
|
70 | })
|
71 | } catch (e) {
|
72 | cb(e.message + '\n\nUsage:\n' + access.usage)
|
73 | }
|
74 | }
|
75 | }
|
76 |
|
77 | function parseParams (cmd, args, cb) {
|
78 |
|
79 |
|
80 | var params = { 'package': '' }
|
81 | if (cmd === 'grant') {
|
82 | params.permissions = args.shift()
|
83 | }
|
84 | if (['grant', 'revoke', 'ls-packages'].indexOf(cmd) !== -1) {
|
85 | var entity = (args.shift() || '').split(':')
|
86 | params.scope = entity[0]
|
87 | params.team = entity[1]
|
88 | }
|
89 |
|
90 | if (cmd === 'ls-packages') {
|
91 | if (!params.scope) {
|
92 | whoami([], true, function (err, scope) {
|
93 | params.scope = scope
|
94 | cb(err, params)
|
95 | })
|
96 | } else {
|
97 | cb(null, params)
|
98 | }
|
99 | } else {
|
100 | getPackage(args.shift(), function (err, pkg) {
|
101 | if (err) return cb(err)
|
102 | params.package = pkg
|
103 |
|
104 | if (cmd === 'ls-collaborators') params.user = args.shift()
|
105 | cb(null, params)
|
106 | })
|
107 | }
|
108 | }
|
109 |
|
110 | function getPackage (name, cb) {
|
111 | if (name && name.trim()) {
|
112 | cb(null, name.trim())
|
113 | } else {
|
114 | readPackageJson(
|
115 | resolve(npm.prefix, 'package.json'),
|
116 | function (err, data) {
|
117 | if (err) {
|
118 | if (err.code === 'ENOENT') {
|
119 | cb(new Error('no package name passed to command and no package.json found'))
|
120 | } else {
|
121 | cb(err)
|
122 | }
|
123 | } else {
|
124 | cb(null, data.name)
|
125 | }
|
126 | }
|
127 | )
|
128 | }
|
129 | }
|