Permalink
Browse files

The token is an object. Treat it as one.

Allow objects in config files, and leverage the ini format's ability
to handle them intelligently.
  • Loading branch information...
1 parent 9b0b53a commit 400763888f1f8321ef7c5b2d1928752391b20dba @isaacs isaacs committed Jul 30, 2012
Showing with 16 additions and 15 deletions.
  1. +2 −7 lib/config.js
  2. +11 −6 lib/npm.js
  3. +3 −2 lib/utils/ini.js
View
@@ -167,9 +167,8 @@ function list (cb) {
if (cliKeys.length) {
msg += "; cli configs" + eol
cliKeys.forEach(function (k) {
- if (cli[k] && typeof cli[k] === 'object') return
+ if (cli[k] && typeof cli[k] === "object") return
if (k === "argv") return
- if (typeof cli[k] === 'object') return
msg += k + " = " + JSON.stringify(cli[k]) + eol
})
msg += eol
@@ -185,7 +184,6 @@ function list (cb) {
if (envKeys.length) {
msg += "; environment configs" + eol
envKeys.forEach(function (k) {
- if (env[k] && typeof env[k] === 'object') return
if (env[k] !== ini.get(k)) {
if (!long) return
msg += "; " + k + " = " + JSON.stringify(env[k])
@@ -205,7 +203,6 @@ function list (cb) {
if (uconfKeys.length) {
msg += "; userconfig " + ini.get("userconfig") + eol
uconfKeys.forEach(function (k) {
- if (uconf[k] && typeof uconf[k] === 'object') return
var val = (k.charAt(0) === "_")
? "---sekretz---"
: JSON.stringify(uconf[k])
@@ -228,7 +225,6 @@ function list (cb) {
if (gconfKeys.length) {
msg += "; globalconfig " + ini.get("globalconfig") + eol
gconfKeys.forEach(function (k) {
- if (gconf[k] && typeof gconf[k] === 'object') return
var val = (k.charAt(0) === "_")
? "---sekretz---"
: JSON.stringify(gconf[k])
@@ -252,7 +248,6 @@ function list (cb) {
var path = require("path")
msg += "; builtin config " + path.resolve(__dirname, "../npmrc") + eol
bconfKeys.forEach(function (k) {
- if (bconf[k] && typeof bconf[k] === 'object') return
var val = (k.charAt(0) === "_")
? "---sekretz---"
: JSON.stringify(bconf[k])
@@ -281,7 +276,7 @@ function list (cb) {
, defKeys = Object.keys(defaults)
msg += "; default values" + eol
defKeys.forEach(function (k) {
- if (defaults[k] && typeof defaults[k] === 'object') return
+ if (defaults[k] && typeof defaults[k] === "object") return
var val = JSON.stringify(defaults[k])
if (defaults[k] !== ini.get(k)) {
if (!long) return
View
@@ -294,9 +294,14 @@ function load (npm, conf, cb) {
// at this point the configs are all set.
// go ahead and spin up the registry client.
- var token
- try { token = JSON.parse(npm.config.get("_token")) }
- catch (er) { token = null }
+ var token = npm.config.get("_token")
+ if (typeof token === "string") {
+ try {
+ token = JSON.parse(token)
+ npm.config.set("_token", token, "user")
+ ini.save("user", function () {})
+ } catch (e) { token = null }
+ }
npm.registry = new RegClient(
{ registry: npm.config.get("registry")
@@ -324,7 +329,7 @@ function load (npm, conf, cb) {
// save the token cookie in the config file
if (npm.registry.couchLogin) {
npm.registry.couchLogin.tokenSet = function (tok) {
- ini.set("_token", JSON.stringify(tok), "user")
+ ini.set("_token", tok, "user")
// ignore save error. best effort.
ini.save("user", function () {})
}
@@ -424,8 +429,8 @@ function setUser (cl, dc, cb) {
npm.config =
{ get : function (key) { return ini.get(key) }
- , set : function (key, val) { return ini.set(key, val, "cli") }
- , del : function (key, val) { return ini.del(key, val, "cli") }
+ , set : function (key, val, which) { return ini.set(key, val, which) }
+ , del : function (key, val, which) { return ini.del(key, val, which) }
}
Object.defineProperty(npm, "prefix",
View
@@ -163,6 +163,7 @@ function unParseField (f, k) {
}
}
}
+ return (!f || typeof f !== "object") ? ini.safe(f) : f
return ini.safe(f)
}
@@ -220,7 +221,6 @@ function encryptAuth (config, cb) {
}
function parseAuth (config) {
- //console.error("parsing config %j", config)
if (!config._auth) return config
var b = new Buffer(config._auth, "base64")
, unpw = b.toString().split(":")
@@ -347,5 +347,6 @@ function set (key, value, which) {
if (configList.length === 1) {
return new Error("trying to set before loading")
}
- return configList.list[TRANS[which]][key] = value
+ configList.list[TRANS[which]][key] = value
+ return value
}

0 comments on commit 4007638

Please sign in to comment.