2011-11-21 18:48:45 +01:00
|
|
|
var npm = require("./npm.js")
|
|
|
|
|
2014-09-24 23:41:07 +02:00
|
|
|
module.exports = whoami
|
|
|
|
|
|
|
|
whoami.usage = "npm whoami\n(just prints username according to given registry)"
|
2011-11-21 18:48:45 +01:00
|
|
|
|
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") {
|
|
|
|
cb = silent
|
|
|
|
silent = false
|
|
|
|
}
|
|
|
|
|
|
|
|
var registry = npm.config.get("registry")
|
|
|
|
if (!registry) return cb(new Error("no default registry set"))
|
|
|
|
|
2015-01-23 15:56:30 +01:00
|
|
|
function noUser () {
|
|
|
|
// 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 msg = "Not authed. Run 'npm adduser'"
|
|
|
|
if (!silent) console.log(msg)
|
|
|
|
cb(null, msg)
|
|
|
|
}
|
|
|
|
|
2015-01-08 23:37:26 +01:00
|
|
|
var auth = npm.config.getCredentialsByURI(registry)
|
|
|
|
if (auth) {
|
|
|
|
if (auth.username) {
|
|
|
|
if (!silent) console.log(auth.username)
|
|
|
|
return process.nextTick(cb.bind(this, null, auth.username))
|
2014-09-24 23:41:07 +02:00
|
|
|
}
|
2015-01-08 23:37:26 +01:00
|
|
|
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)
|
2015-01-23 15:56:30 +01:00
|
|
|
if (!username) return noUser()
|
2014-09-24 23:41:07 +02:00
|
|
|
|
|
|
|
if (!silent) console.log(username)
|
|
|
|
cb(null, username)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 15:56:30 +01:00
|
|
|
process.nextTick(noUser)
|
2011-11-21 18:48:45 +01:00
|
|
|
}
|