]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/lib/access.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / lib / access.js
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
9 var whoami = require('./whoami')
10
11 module.exports = access
12
13 access.usage =
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>]'
21
22 access.subcommands = ['public', 'restricted', 'grant', 'revoke',
23                       'ls-packages', 'ls-collaborators', 'edit']
24
25 access.completion = function (opts, cb) {
26   var argv = opts.conf.argv.remain
27   if (argv.length === 2) {
28     return cb(null, access.subcommands)
29   }
30
31   switch (argv[2]) {
32     case 'grant':
33       if (argv.length === 3) {
34         return cb(null, ['read-only', 'read-write'])
35       } else {
36         return cb(null, [])
37       }
38       break
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         !err && data && console.log(JSON.stringify(data, undefined, 2))
67         cb(err, data)
68       })
69     } catch (e) {
70       cb(e.message + '\n\nUsage:\n' + access.usage)
71     }
72   }
73 }
74
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()
81   }
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]
86   }
87
88   if (cmd === 'ls-packages') {
89     if (!params.scope) {
90       whoami([], true, function (err, scope) {
91         params.scope = scope
92         cb(err, params)
93       })
94     } else {
95       cb(null, params)
96     }
97   } else {
98     getPackage(args.shift(), function (err, pkg) {
99       if (err) return cb(err)
100       params.package = pkg
101
102       if (cmd === 'ls-collaborators') params.user = args.shift()
103       cb(null, params)
104     })
105   }
106 }
107
108 function getPackage (name, cb) {
109   if (name && name.trim()) {
110     cb(null, name.trim())
111   } else {
112     readPackageJson(
113       resolve(npm.prefix, 'package.json'),
114       function (err, data) {
115         if (err) {
116           if (err.code === 'ENOENT') {
117             cb(new Error('no package name passed to command and no package.json found'))
118           } else {
119             cb(err)
120           }
121         } else {
122           cb(null, data.name)
123         }
124       }
125     )
126   }
127 }