Use new npmconf module

This commit is contained in:
isaacs
2012-08-14 19:24:19 -07:00
parent 671d314e7e
commit d20eb0fcdd
8 changed files with 126 additions and 148 deletions

View File

@@ -22,10 +22,10 @@ log.info("it worked if it ends with", "ok")
var fs = require("graceful-fs")
, path = require("path")
, npm = require("../lib/npm.js")
, ini = require("../lib/utils/ini.js")
, npmconf = require("npmconf")
, errorHandler = require("../lib/utils/error-handler.js")
, configDefs = require("../lib/utils/config-defs.js")
, configDefs = npmconf.defs
, shorthands = configDefs.shorthands
, types = configDefs.types
, nopt = require("nopt")

View File

@@ -1,8 +1,7 @@
module.exports = adduser
var ini = require("./utils/ini.js")
, log = require("npmlog")
var log = require("npmlog")
, npm = require("./npm.js")
, registry = npm.registry
, read = require("read")
@@ -130,10 +129,10 @@ function save (c, u, cb) {
registry.username = u.u
registry.password = u.p
registry.email = u.e
ini.set("username", u.u, "user")
ini.set("_password", u.p, "user")
ini.set("email", u.e, "user")
npm.config.set("username", u.u, "user")
npm.config.set("_password", u.p, "user")
npm.config.set("email", u.e, "user")
log.info("adduser", "Authorized user %s", u.u)
ini.save("user", cb)
npm.config.save("user", cb)
})
}

View File

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

View File

@@ -9,13 +9,13 @@ config.usage = "npm config set <key> <value>"
+ "\nnpm set <key> <value>"
+ "\nnpm get [<key>]"
var ini = require("./utils/ini.js")
, log = require("npmlog")
var log = require("npmlog")
, npm = require("./npm.js")
, exec = require("./utils/exec.js")
, fs = require("graceful-fs")
, dc
, types = require("./utils/config-defs.js").types
, npmconf = require("npmconf")
, types = npmconf.defs.types
, ini = require("ini")
config.completion = function (opts, cb) {
var argv = opts.conf.argv.remain
@@ -59,18 +59,17 @@ function config (args, cb) {
}
function edit (cb) {
var e = ini.get("editor")
, which = ini.get("global") ? "global" : "user"
, f = ini.get(which + "config")
var e = npm.config.get("editor")
, which = npm.config.get("global") ? "global" : "user"
, f = npm.config.get(which + "config")
, eol = process.platform === "win32" ? "\r\n" : "\n"
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)
fs.readFile(f, "utf8", function (er, data) {
if (er) data = ""
dc = dc || require("./utils/config-defs.js").defaults
data = [ ";;;;"
, "; npm "+(ini.get("global") ?
, "; npm "+(npm.config.get("global") ?
"globalconfig" : "userconfig")+" file"
, "; this is a simple ini-formatted file"
, "; lines that start with semi-colons are comments."
@@ -83,8 +82,8 @@ function edit (cb) {
, ";;;;"
]
)
.concat(Object.keys(dc).map(function (k) {
return "; " + k + " = " + ini.unParseField(dc[k],k)
.concat(Object.keys(npmconf.defaults).map(function (k) {
return "; " + k + " = " + npmconf.defaults[k]
}))
.concat([""])
.join(eol)
@@ -94,13 +93,7 @@ function edit (cb) {
, "utf8"
, function (er) {
if (er) return cb(er)
exec(e, [f], function (er) {
if (er) return cb(er)
ini.resolveConfigs(function (er) {
if (er) return cb(er)
ini.save(which, cb)
})
})
exec(e, [f], cb)
}
)
})
@@ -109,8 +102,9 @@ function edit (cb) {
function del (key, cb) {
if (!key) return cb(new Error("no key provided"))
ini.del(key)
ini.save(ini.get("global") ? "global" : "user", cb)
var where = npm.config.get("global") ? "global" : "user"
npm.config.del(key, where)
npm.config.save(where, cb)
}
function set (key, val, cb) {
@@ -129,9 +123,9 @@ function set (key, val, cb) {
key = key.trim()
val = val.trim()
log.info("config", "set %j %j", key, val)
var where = ini.get("global") ? "global" : "user"
ini.set(key, val, where)
ini.save(where, cb)
var where = npm.config.get("global") ? "global" : "user"
npm.config.set(key, val, where)
npm.config.save(where, cb)
}
function get (key, cb) {
@@ -151,140 +145,127 @@ function reverse (a, b) {
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) {
var msg = ""
, long = npm.config.get("long")
// cli configs.
// show any that aren't secret
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
})
var cli = npm.config.sources.cli.data
, cliKeys = getKeys(cli)
if (cliKeys.length) {
msg += "; cli configs" + eol
msg += "; cli configs\n"
cliKeys.forEach(function (k) {
if (cli[k] && typeof cli[k] === "object") 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
var env = ini.configList.list[ini.TRANS.env]
, envKeys = Object.keys(env).filter(function (k) {
return !(k.charAt(0) === "_" || types[k] !== types[k])
}).sort(function (a, b) {
return a > b ? 1 : -1
})
var env = npm.config.sources.env.data
, envKeys = getKeys(env)
if (envKeys.length) {
msg += "; environment configs" + eol
msg += "; environment configs\n"
envKeys.forEach(function (k) {
if (env[k] !== ini.get(k)) {
if (env[k] !== npm.config.get(k)) {
if (!long) return
msg += "; " + k + " = " + JSON.stringify(env[k])
+ " (overridden)" + eol
} else msg += k + " = " + JSON.stringify(env[k]) + eol
+ " (overridden)\n"
} else msg += k + " = " + JSON.stringify(env[k]) + "\n"
})
msg += eol
msg += "\n"
}
// user config file
var uconf = ini.configList.list[ini.TRANS.user]
, uconfKeys = Object.keys(uconf).filter(function (k) {
return types[k] === types[k]
}).sort(function (a, b) {
return a > b ? 1 : -1
})
var uconf = npm.config.sources.user.data
, uconfKeys = getKeys(uconf)
if (uconfKeys.length) {
msg += "; userconfig " + ini.get("userconfig") + eol
msg += "; userconfig " + npm.config.get("userconfig") + "\n"
uconfKeys.forEach(function (k) {
var val = (k.charAt(0) === "_")
? "---sekretz---"
: JSON.stringify(uconf[k])
if (uconf[k] !== ini.get(k)) {
if (uconf[k] !== npm.config.get(k)) {
if (!long) return
msg += "; " + k + " = " + val
+ " (overridden)" + eol
} else msg += k + " = " + val + eol
+ " (overridden)\n"
} else msg += k + " = " + val + "\n"
})
msg += eol
msg += "\n"
}
// global config file
var gconf = ini.configList.list[ini.TRANS.global]
, gconfKeys = Object.keys(gconf).filter(function (k) {
return types[k] === types[k]
}).sort(function (a, b) {
return a > b ? 1 : -1
})
var gconf = npm.config.sources.global.data
, gconfKeys = getKeys(gconf)
if (gconfKeys.length) {
msg += "; globalconfig " + ini.get("globalconfig") + eol
msg += "; globalconfig " + npm.config.get("globalconfig") + "\n"
gconfKeys.forEach(function (k) {
var val = (k.charAt(0) === "_")
? "---sekretz---"
: JSON.stringify(gconf[k])
if (gconf[k] !== ini.get(k)) {
if (gconf[k] !== npm.config.get(k)) {
if (!long) return
msg += "; " + k + " = " + val
+ " (overridden)" + eol
} else msg += k + " = " + val + eol
+ " (overridden)\n"
} else msg += k + " = " + val + "\n"
})
msg += eol
msg += "\n"
}
// builtin config file
var bconf = ini.configList.list[ini.TRANS.builtin]
, bconfKeys = Object.keys(bconf).filter(function (k) {
return types[k] === types[k]
}).sort(function (a, b) {
return a > b ? 1 : -1
var builtin = npm.config.sources.builtin || {}
if (builtin && builtin.data) {
var bconf = builtin.data
, bpath = builtin.path
, 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) {
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
msg += "\n"
}
}
// only show defaults if --long
if (!long) {
msg += "; node install prefix = " + process.installPrefix + eol
+ "; node bin location = " + process.execPath + eol
+ "; cwd = " + process.cwd() + eol
+ "; HOME = " + process.env.HOME + eol
+ "; 'npm config ls -l' to show all defaults." + eol
msg += "; node bin location = " + process.execPath + "\n"
+ "; cwd = " + process.cwd() + "\n"
+ "; HOME = " + process.env.HOME + "\n"
+ "; 'npm config ls -l' to show all defaults.\n"
console.log(msg)
return cb()
}
var defaults = ini.defaultConfig
, defKeys = Object.keys(defaults)
msg += "; default values" + eol
var defaults = npmconf.defaults
, defKeys = getKeys(defaults)
msg += "; default values\n"
defKeys.forEach(function (k) {
if (defaults[k] && typeof defaults[k] === "object") return
var val = JSON.stringify(defaults[k])
if (defaults[k] !== ini.get(k)) {
if (!long) return
if (defaults[k] !== npm.config.get(k)) {
msg += "; " + k + " = " + val
+ " (overridden)" + eol
} else msg += k + " = " + val + eol
+ " (overridden)\n"
} else msg += k + " = " + val + "\n"
})
msg += eol
msg += "\n"
console.log(msg)
return cb()

View File

@@ -17,7 +17,7 @@ require("path").SPLIT_CHAR = process.platform === "win32" ? "\\" : "/"
var EventEmitter = require("events").EventEmitter
, npm = module.exports = new EventEmitter
, config = require("./config.js")
, ini = require("./utils/ini.js")
, npmconf = require("npmconf")
, log = require("npmlog")
, fs = require("graceful-fs")
, path = require("path")
@@ -31,6 +31,8 @@ var EventEmitter = require("events").EventEmitter
, chain = slide.chain
, RegClient = require("npm-registry-client")
npm.config = {loaded: false}
// /usr/local is often a read-only fs, which is not
// well handled by node or mkdirp. Just double-check
// in the case of errors when making the prefix dirs.
@@ -223,11 +225,10 @@ function loadCb (er) {
loadListeners.length = 0
}
npm.load = function (conf, cb_) {
if (!cb_ && typeof conf === "function") cb_ = conf , conf = {}
npm.load = function (cli, cb_) {
if (!cb_ && typeof cli === "function") cb_ = cli , cli = {}
if (!cb_) cb_ = function () {}
if (!conf) conf = {}
if (!cli) cli = {}
loadListeners.push(cb_)
if (loaded || loadErr) return cb(loadErr)
if (loading) return
@@ -236,6 +237,7 @@ npm.load = function (conf, cb_) {
function cb (er) {
if (loadErr) return
npm.config.loaded = true
loaded = true
loadCb(loadErr = er)
if (onload = onload && npm.config.get("onload-script")) {
@@ -246,11 +248,10 @@ npm.load = function (conf, cb_) {
log.pause()
load(npm, conf, cb)
load(npm, cli, cb)
}
function load (npm, conf, cb) {
function load (npm, cli, cb) {
which(process.argv[0], function (er, node) {
if (!er && node.toUpperCase() !== process.execPath.toUpperCase()) {
log.verbose("node symlink", node)
@@ -261,12 +262,16 @@ function load (npm, conf, cb) {
// look up configs
//console.error("about to look up configs")
ini.resolveConfigs(conf, function (er) {
var color = npm.config.get("color")
npmconf.load(cli, function (er, conf) {
if (er === conf) er = null
log.level = npm.config.get("loglevel")
npm.config = conf
var color = conf.get("color")
log.level = conf.get("loglevel")
log.heading = "npm"
log.stream = npm.config.get("logstream")
log.stream = conf.get("logstream")
switch (color) {
case "always": log.enableColor(); break
case false: log.disableColor(); break
@@ -294,12 +299,12 @@ function load (npm, conf, cb) {
// at this point the configs are all set.
// go ahead and spin up the registry client.
var token = npm.config.get("_token")
var token = conf.get("_token")
if (typeof token === "string") {
try {
token = JSON.parse(token)
npm.config.set("_token", token, "user")
ini.save("user", function () {})
conf.set("_token", token, "user")
conf.save("user")
} catch (e) { token = null }
}
@@ -329,20 +334,20 @@ 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", tok, "user")
npm.config.set("_token", tok, "user")
// ignore save error. best effort.
ini.save("user", function () {})
npm.config.save("user")
}
}
var umask = parseInt(conf.umask, 8)
var umask = parseInt(cli.umask, 8)
npm.modes = { exec: 0777 & (~umask)
, file: 0666 & (~umask)
, umask: umask }
chain([ [ loadPrefix, npm, conf ]
, [ setUser, ini.configList, ini.defaultConfig ]
, [ loadUid, npm, conf ]
chain([ [ loadPrefix, npm, cli ]
, [ setUser, conf, conf.root ]
, [ loadUid, npm ]
], cb)
})
})
@@ -393,7 +398,7 @@ function loadPrefix (npm, conf, cb) {
}
function loadUid (npm, conf, cb) {
function loadUid (npm, cb) {
// if we're not in unsafe-perm mode, then figure out who
// to run stuff as. Do this first, to support `npm update npm -g`
if (!npm.config.get("unsafe-perm")) {
@@ -427,12 +432,6 @@ function setUser (cl, dc, cb) {
}
npm.config =
{ get : function (key) { return ini.get(key) }
, 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",
{ get : function () {
return npm.config.get("global") ? npm.globalPrefix : npm.localPrefix
@@ -497,7 +496,7 @@ Object.defineProperty(npm, "tmp",
// the better to repl you with
Object.getOwnPropertyNames(npm.commands).forEach(function (n) {
if (npm.hasOwnProperty(n)) return
if (npm.hasOwnProperty(n) || n === "config") return
Object.defineProperty(npm, n, { get: function () {
return function () {

View File

@@ -7,14 +7,13 @@ var cbCalled = false
, rm = require("rimraf")
, itWorked = false
, path = require("path")
, ini = require("./ini.js")
, wroteLogFile = false
, exitCode = 0
process.on("exit", function (code) {
// console.error("exit", code)
if (!ini.resolved) return
if (!npm.config.loaded) return
if (code) itWorked = false
if (itWorked) log.info("ok")
else {
@@ -71,7 +70,7 @@ function exit (code, noLog) {
function errorHandler (er) {
var printStack = false
// console.error("errorHandler", er)
if (!ini.resolved) {
if (!npm.config.loaded) {
// logging won't work unless we pretend that it's ready
er = er || new Error("Exit prior to config file resolving.")
console.error(er.stack || er.message)

View File

@@ -242,8 +242,7 @@ function makeEnv (data, prefix, env) {
prefix = "npm_config_"
var pkgConfig = {}
, ini = require("./ini.js")
, keys = ini.keys
, keys = npm.config.keys
, pkgVerConfig = {}
, namePref = data.name + ":"
, verPref = data.name + "@" + data.version + ":"
@@ -252,7 +251,7 @@ function makeEnv (data, prefix, env) {
if (i.charAt(0) === "_" && i.indexOf("_"+namePref) !== 0) {
return
}
var value = ini.get(i)
var value = npm.config.get(i)
if (value instanceof Stream) return
if (!value) value = ""
else if (typeof value !== "string") value = JSON.stringify(value)

View File

@@ -41,7 +41,6 @@ view.completion = function (opts, cb) {
var npm = require("./npm.js")
, registry = npm.registry
, ini = require("ini")
, log = require("npmlog")
, util = require("util")
, semver = require("semver")