Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

Commit

Permalink
Use new npmconf module
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Aug 15, 2012
1 parent 671d314 commit d20eb0f
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 148 deletions.
4 changes: 2 additions & 2 deletions bin/npm-cli.js
Expand Up @@ -22,10 +22,10 @@ log.info("it worked if it ends with", "ok")
var fs = require("graceful-fs") var fs = require("graceful-fs")
, path = require("path") , path = require("path")
, npm = require("../lib/npm.js") , npm = require("../lib/npm.js")
, ini = require("../lib/utils/ini.js") , npmconf = require("npmconf")
, errorHandler = require("../lib/utils/error-handler.js") , errorHandler = require("../lib/utils/error-handler.js")


, configDefs = require("../lib/utils/config-defs.js") , configDefs = npmconf.defs
, shorthands = configDefs.shorthands , shorthands = configDefs.shorthands
, types = configDefs.types , types = configDefs.types
, nopt = require("nopt") , nopt = require("nopt")
Expand Down
11 changes: 5 additions & 6 deletions lib/adduser.js
@@ -1,8 +1,7 @@


module.exports = adduser module.exports = adduser


var ini = require("./utils/ini.js") var log = require("npmlog")
, log = require("npmlog")
, npm = require("./npm.js") , npm = require("./npm.js")
, registry = npm.registry , registry = npm.registry
, read = require("read") , read = require("read")
Expand Down Expand Up @@ -130,10 +129,10 @@ function save (c, u, cb) {
registry.username = u.u registry.username = u.u
registry.password = u.p registry.password = u.p
registry.email = u.e registry.email = u.e
ini.set("username", u.u, "user") npm.config.set("username", u.u, "user")
ini.set("_password", u.p, "user") npm.config.set("_password", u.p, "user")
ini.set("email", u.e, "user") npm.config.set("email", u.e, "user")
log.info("adduser", "Authorized user %s", u.u) log.info("adduser", "Authorized user %s", u.u)
ini.save("user", cb) npm.config.save("user", cb)
}) })
} }
8 changes: 5 additions & 3 deletions lib/build.js
@@ -1,4 +1,3 @@

// npm build command // npm build command


// everything about the installation after the creation of // everything about the installation after the creation of
Expand Down Expand Up @@ -62,8 +61,11 @@ function build_ (global, didPre, didRB) { return function (folder, cb) {
function writeBuiltinConf (folder, cb) { function writeBuiltinConf (folder, cb) {
// the builtin config is "sticky". Any time npm installs itself, // the builtin config is "sticky". Any time npm installs itself,
// it puts its builtin config file there, as well. // it puts its builtin config file there, as well.
var ini = require("./utils/ini.js") if (!npm.config.usingBuiltin
ini.saveConfig("builtin", path.resolve(folder, "npmrc"), cb) || folder !== path.dirname(__dirname)) {
return cb()
}
npm.config.save("builtin", cb)
} }


function linkStuff (pkg, folder, global, didRB, cb) { function linkStuff (pkg, folder, global, didRB, cb) {
Expand Down
183 changes: 82 additions & 101 deletions lib/config.js
Expand Up @@ -9,13 +9,13 @@ config.usage = "npm config set <key> <value>"
+ "\nnpm set <key> <value>" + "\nnpm set <key> <value>"
+ "\nnpm get [<key>]" + "\nnpm get [<key>]"


var ini = require("./utils/ini.js") var log = require("npmlog")
, log = require("npmlog")
, npm = require("./npm.js") , npm = require("./npm.js")
, exec = require("./utils/exec.js") , exec = require("./utils/exec.js")
, fs = require("graceful-fs") , fs = require("graceful-fs")
, dc , npmconf = require("npmconf")
, types = require("./utils/config-defs.js").types , types = npmconf.defs.types
, ini = require("ini")


config.completion = function (opts, cb) { config.completion = function (opts, cb) {
var argv = opts.conf.argv.remain var argv = opts.conf.argv.remain
Expand Down Expand Up @@ -59,18 +59,17 @@ function config (args, cb) {
} }


function edit (cb) { function edit (cb) {
var e = ini.get("editor") var e = npm.config.get("editor")
, which = ini.get("global") ? "global" : "user" , which = npm.config.get("global") ? "global" : "user"
, f = ini.get(which + "config") , f = npm.config.get(which + "config")
, eol = process.platform === "win32" ? "\r\n" : "\n" , eol = process.platform === "win32" ? "\r\n" : "\n"
if (!e) return cb(new Error("No EDITOR config or environ set.")) if (!e) return cb(new Error("No EDITOR config or environ set."))
ini.save(which, function (er) { npm.config.save(which, function (er) {
if (er) return cb(er) if (er) return cb(er)
fs.readFile(f, "utf8", function (er, data) { fs.readFile(f, "utf8", function (er, data) {
if (er) data = "" if (er) data = ""
dc = dc || require("./utils/config-defs.js").defaults
data = [ ";;;;" data = [ ";;;;"
, "; npm "+(ini.get("global") ? , "; npm "+(npm.config.get("global") ?
"globalconfig" : "userconfig")+" file" "globalconfig" : "userconfig")+" file"
, "; this is a simple ini-formatted file" , "; this is a simple ini-formatted file"
, "; lines that start with semi-colons are comments." , "; lines that start with semi-colons are comments."
Expand All @@ -83,8 +82,8 @@ function edit (cb) {
, ";;;;" , ";;;;"
] ]
) )
.concat(Object.keys(dc).map(function (k) { .concat(Object.keys(npmconf.defaults).map(function (k) {
return "; " + k + " = " + ini.unParseField(dc[k],k) return "; " + k + " = " + npmconf.defaults[k]
})) }))
.concat([""]) .concat([""])
.join(eol) .join(eol)
Expand All @@ -94,13 +93,7 @@ function edit (cb) {
, "utf8" , "utf8"
, function (er) { , function (er) {
if (er) return cb(er) if (er) return cb(er)
exec(e, [f], function (er) { exec(e, [f], cb)
if (er) return cb(er)
ini.resolveConfigs(function (er) {
if (er) return cb(er)
ini.save(which, cb)
})
})
} }
) )
}) })
Expand All @@ -109,8 +102,9 @@ function edit (cb) {


function del (key, cb) { function del (key, cb) {
if (!key) return cb(new Error("no key provided")) if (!key) return cb(new Error("no key provided"))
ini.del(key) var where = npm.config.get("global") ? "global" : "user"
ini.save(ini.get("global") ? "global" : "user", cb) npm.config.del(key, where)
npm.config.save(where, cb)
} }


function set (key, val, cb) { function set (key, val, cb) {
Expand All @@ -129,9 +123,9 @@ function set (key, val, cb) {
key = key.trim() key = key.trim()
val = val.trim() val = val.trim()
log.info("config", "set %j %j", key, val) log.info("config", "set %j %j", key, val)
var where = ini.get("global") ? "global" : "user" var where = npm.config.get("global") ? "global" : "user"
ini.set(key, val, where) npm.config.set(key, val, where)
ini.save(where, cb) npm.config.save(where, cb)
} }


function get (key, cb) { function get (key, cb) {
Expand All @@ -151,140 +145,127 @@ function reverse (a, b) {
return a > b ? -1 : 1 return a > b ? -1 : 1
} }


function public (k) {
return !(k.charAt(0) === "_" || types[k] !== types[k])
}

function getKeys (data) {
return Object.keys(data).filter(public).sort(sort)
}

function list (cb) { function list (cb) {
var msg = "" var msg = ""
, long = npm.config.get("long") , long = npm.config.get("long")


// cli configs. var cli = npm.config.sources.cli.data
// show any that aren't secret , cliKeys = getKeys(cli)
var cli = ini.configList.list[ini.TRANS.cli]
, eol = process.platform === "win32" ? "\r\n" : "\n"
, cliKeys = Object.keys(cli).filter(function (k) {
return !(k.charAt(0) === "_" || types[k] !== types[k])
}).sort(function (a, b) {
return a > b ? 1 : -1
})
if (cliKeys.length) { if (cliKeys.length) {
msg += "; cli configs" + eol msg += "; cli configs\n"
cliKeys.forEach(function (k) { 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 (k === "argv") return
msg += k + " = " + JSON.stringify(cli[k]) + eol msg += k + " = " + JSON.stringify(cli[k]) + "\n"
}) })
msg += eol msg += "\n"
} }


// env configs // env configs
var env = ini.configList.list[ini.TRANS.env] var env = npm.config.sources.env.data
, envKeys = Object.keys(env).filter(function (k) { , envKeys = getKeys(env)
return !(k.charAt(0) === "_" || types[k] !== types[k])
}).sort(function (a, b) {
return a > b ? 1 : -1
})
if (envKeys.length) { if (envKeys.length) {
msg += "; environment configs" + eol msg += "; environment configs\n"
envKeys.forEach(function (k) { envKeys.forEach(function (k) {
if (env[k] !== ini.get(k)) { if (env[k] !== npm.config.get(k)) {
if (!long) return if (!long) return
msg += "; " + k + " = " + JSON.stringify(env[k]) msg += "; " + k + " = " + JSON.stringify(env[k])
+ " (overridden)" + eol + " (overridden)\n"
} else msg += k + " = " + JSON.stringify(env[k]) + eol } else msg += k + " = " + JSON.stringify(env[k]) + "\n"
}) })
msg += eol msg += "\n"
} }


// user config file // user config file
var uconf = ini.configList.list[ini.TRANS.user] var uconf = npm.config.sources.user.data
, uconfKeys = Object.keys(uconf).filter(function (k) { , uconfKeys = getKeys(uconf)
return types[k] === types[k]
}).sort(function (a, b) {
return a > b ? 1 : -1
})
if (uconfKeys.length) { if (uconfKeys.length) {
msg += "; userconfig " + ini.get("userconfig") + eol msg += "; userconfig " + npm.config.get("userconfig") + "\n"
uconfKeys.forEach(function (k) { uconfKeys.forEach(function (k) {
var val = (k.charAt(0) === "_") var val = (k.charAt(0) === "_")
? "---sekretz---" ? "---sekretz---"
: JSON.stringify(uconf[k]) : JSON.stringify(uconf[k])
if (uconf[k] !== ini.get(k)) { if (uconf[k] !== npm.config.get(k)) {
if (!long) return if (!long) return
msg += "; " + k + " = " + val msg += "; " + k + " = " + val
+ " (overridden)" + eol + " (overridden)\n"
} else msg += k + " = " + val + eol } else msg += k + " = " + val + "\n"
}) })
msg += eol msg += "\n"
} }


// global config file // global config file
var gconf = ini.configList.list[ini.TRANS.global] var gconf = npm.config.sources.global.data
, gconfKeys = Object.keys(gconf).filter(function (k) { , gconfKeys = getKeys(gconf)
return types[k] === types[k]
}).sort(function (a, b) {
return a > b ? 1 : -1
})
if (gconfKeys.length) { if (gconfKeys.length) {
msg += "; globalconfig " + ini.get("globalconfig") + eol msg += "; globalconfig " + npm.config.get("globalconfig") + "\n"
gconfKeys.forEach(function (k) { gconfKeys.forEach(function (k) {
var val = (k.charAt(0) === "_") var val = (k.charAt(0) === "_")
? "---sekretz---" ? "---sekretz---"
: JSON.stringify(gconf[k]) : JSON.stringify(gconf[k])
if (gconf[k] !== ini.get(k)) { if (gconf[k] !== npm.config.get(k)) {
if (!long) return if (!long) return
msg += "; " + k + " = " + val msg += "; " + k + " = " + val
+ " (overridden)" + eol + " (overridden)\n"
} else msg += k + " = " + val + eol } else msg += k + " = " + val + "\n"
}) })
msg += eol msg += "\n"
} }


// builtin config file // builtin config file
var bconf = ini.configList.list[ini.TRANS.builtin] var builtin = npm.config.sources.builtin || {}
, bconfKeys = Object.keys(bconf).filter(function (k) { if (builtin && builtin.data) {
return types[k] === types[k] var bconf = builtin.data
}).sort(function (a, b) { , bpath = builtin.path
return a > b ? 1 : -1 , bconfKeys = getKeys(bconf)
if (bconfKeys.length) {
var path = require("path")
msg += "; builtin config " + bpath + "\n"
bconfKeys.forEach(function (k) {
var val = (k.charAt(0) === "_")
? "---sekretz---"
: JSON.stringify(bconf[k])
if (bconf[k] !== npm.config.get(k)) {
if (!long) return
msg += "; " + k + " = " + val
+ " (overridden)\n"
} else msg += k + " = " + val + "\n"
}) })
if (bconfKeys.length) { msg += "\n"
var path = require("path") }
msg += "; builtin config " + path.resolve(__dirname, "../npmrc") + eol
bconfKeys.forEach(function (k) {
var val = (k.charAt(0) === "_")
? "---sekretz---"
: JSON.stringify(bconf[k])
if (bconf[k] !== ini.get(k)) {
if (!long) return
msg += "; " + k + " = " + val
+ " (overridden)" + eol
} else msg += k + " = " + val + eol
})
msg += eol
} }


// only show defaults if --long // only show defaults if --long
if (!long) { if (!long) {
msg += "; node install prefix = " + process.installPrefix + eol msg += "; node bin location = " + process.execPath + "\n"
+ "; node bin location = " + process.execPath + eol + "; cwd = " + process.cwd() + "\n"
+ "; cwd = " + process.cwd() + eol + "; HOME = " + process.env.HOME + "\n"
+ "; HOME = " + process.env.HOME + eol + "; 'npm config ls -l' to show all defaults.\n"
+ "; 'npm config ls -l' to show all defaults." + eol


console.log(msg) console.log(msg)
return cb() return cb()
} }


var defaults = ini.defaultConfig var defaults = npmconf.defaults
, defKeys = Object.keys(defaults) , defKeys = getKeys(defaults)
msg += "; default values" + eol msg += "; default values\n"
defKeys.forEach(function (k) { 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]) var val = JSON.stringify(defaults[k])
if (defaults[k] !== ini.get(k)) { if (defaults[k] !== npm.config.get(k)) {
if (!long) return
msg += "; " + k + " = " + val msg += "; " + k + " = " + val
+ " (overridden)" + eol + " (overridden)\n"
} else msg += k + " = " + val + eol } else msg += k + " = " + val + "\n"
}) })
msg += eol msg += "\n"


console.log(msg) console.log(msg)
return cb() return cb()
Expand Down

0 comments on commit d20eb0f

Please sign in to comment.