UNPKG

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