0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/deps/npm/lib/whoami.js

48 lines
1.4 KiB
JavaScript
Raw Normal View History

var npm = require('./npm.js')
var output = require('./utils/output.js')
2014-09-24 23:41:07 +02:00
module.exports = whoami
whoami.usage = 'npm whoami [--registry <registry>]\n(just prints username according to given registry)'
2012-07-17 20:37:39 +02:00
function whoami (args, silent, cb) {
2014-09-24 23:41:07 +02:00
// FIXME: need tighter checking on this, but is a breaking change
if (typeof cb !== 'function') {
2014-09-24 23:41:07 +02:00
cb = silent
silent = false
}
var registry = npm.config.get('registry')
if (!registry) return cb(new Error('no default registry set'))
2014-09-24 23:41:07 +02:00
var auth = npm.config.getCredentialsByURI(registry)
if (auth) {
if (auth.username) {
if (!silent) output(auth.username)
return process.nextTick(cb.bind(this, null, auth.username))
} else if (auth.token) {
return npm.registry.whoami(registry, { auth: auth }, function (er, username) {
2014-09-24 23:41:07 +02:00
if (er) return cb(er)
if (!username) {
var needNewSession = new Error(
'Your auth token is no longer valid. Please log in again.'
)
needNewSession.code = 'ENEEDAUTH'
return cb(needNewSession)
}
2014-09-24 23:41:07 +02:00
if (!silent) output(username)
2014-09-24 23:41:07 +02:00
cb(null, username)
})
}
}
// At this point, if they have a credentials object, it doesn't have a token
// or auth in it. Probably just the default registry.
var needAuth = new Error(
'this command requires you to be logged in.'
)
needAuth.code = 'ENEEDAUTH'
process.nextTick(cb.bind(this, needAuth))
}