1 | var npm = require('./npm.js')
|
2 | var output = require('./utils/output.js')
|
3 |
|
4 | module.exports = whoami
|
5 |
|
6 | whoami.usage = 'npm whoami [--registry <registry>]\n(just prints username according to given registry)'
|
7 |
|
8 | function whoami (args, silent, cb) {
|
9 |
|
10 | if (typeof cb !== 'function') {
|
11 | cb = silent
|
12 | silent = false
|
13 | }
|
14 |
|
15 | var registry = npm.config.get('registry')
|
16 | if (!registry) return cb(new Error('no default registry set'))
|
17 |
|
18 | var auth = npm.config.getCredentialsByURI(registry)
|
19 | if (auth) {
|
20 | if (auth.username) {
|
21 | if (!silent) output(auth.username)
|
22 | return process.nextTick(cb.bind(this, null, auth.username))
|
23 | } else if (auth.token) {
|
24 | return npm.registry.whoami(registry, { auth: auth }, function (er, username) {
|
25 | if (er) return cb(er)
|
26 | if (!username) {
|
27 | var needNewSession = new Error(
|
28 | 'Your auth token is no longer valid. Please log in again.'
|
29 | )
|
30 | needNewSession.code = 'ENEEDAUTH'
|
31 | return cb(needNewSession)
|
32 | }
|
33 |
|
34 | if (!silent) output(username)
|
35 | cb(null, username)
|
36 | })
|
37 | }
|
38 | }
|
39 |
|
40 |
|
41 |
|
42 | var needAuth = new Error(
|
43 | 'this command requires you to be logged in.'
|
44 | )
|
45 | needAuth.code = 'ENEEDAUTH'
|
46 | process.nextTick(cb.bind(this, needAuth))
|
47 | }
|