diff options
-rwxr-xr-x | cli.js | 13 | ||||
-rw-r--r-- | lib/utils/default-config.js | 10 | ||||
-rw-r--r-- | lib/utils/ini.js | 157 |
3 files changed, 147 insertions, 33 deletions
@@ -6,16 +6,13 @@ var log = require("./lib/utils/log") log("ok", "it worked if it ends with") -var fs = require("fs") - , path = require("path") - , sys = require("sys") - , npm = require("./npm") - // supported commands. + + +var sys = require("sys") + , npm = require("./npm") , argv = process.argv.slice(2) , arg = "" - - , conf = {} , key , arglist = [] , command @@ -67,7 +64,7 @@ if (!command) { ) process.exit(1) } else itWorked = true -} else npm.commands[command](arglist, errorHandler) +} else npm.execute(command, arglist, errorHandler) function errorHandler (er) { if (!er) { diff --git a/lib/utils/default-config.js b/lib/utils/default-config.js index c2c614634..31a9f5249 100644 --- a/lib/utils/default-config.js +++ b/lib/utils/default-config.js @@ -3,11 +3,8 @@ var log = require("./log") , path = require("path") , hasSSL = false try { - crypto = process.binding("crypto") && require("crypto") - hasSSL = true -} catch (ex) { - crypto = {} -} + hasSSL = !!(process.binding("crypto") && require("crypto")) +} catch (ex) {} if (!process.execPath) { process.execPath = path.join(process.installPrefix, "bin", "node") @@ -17,8 +14,9 @@ module.exports = { "auto-activate" : "always" , "auto-deactivate" : true , tag : "latest" - , configFile : path.join(process.execPath, "..", "..", "etc", "npmrc") , root : path.join(process.execPath, "..", "..", "lib", "node") + , globalconfig : path.join(process.execPath, "..", "..", "etc", "npmrc") + , userconfig : path.join(process.env.HOME, ".npmrc") , binroot : path.dirname(process.execPath) // diff --git a/lib/utils/ini.js b/lib/utils/ini.js index 2871351ba..23dd71246 100644 --- a/lib/utils/ini.js +++ b/lib/utils/ini.js @@ -9,27 +9,146 @@ Create a chain of config objects, in this priority order: CLI - the --foo things in the command line. -ENV - all the things starting with npm_ in the environment +ENV - all the things starting with npm_config_ in the environment USER - $HOME/.npmrc GLOBAL - $PREFIX/etc/npmrc -If the CLI or ENV specify a configfile, then that is used +If the CLI or ENV specify a userconfig, then that file is used as the USER config. -If the CLI or ENV specify a configfile, and also have ---config global, then that is used as the GLOBAL config. +If the CLI or ENV specify a globalconfig, then that file is used +as the GLOBAL config. -export npm_configfile=/some/other/file -export npm_global_configfile=global +export npm_config_userconfig=/some/other/file +export npm_config_globalconfig=global -For implementation reasons, "_" in env vars is turned into "-" +For implementation reasons, "_" in env vars is turned into "-". So, +export npm_config_auto_activate */ +exports.resolveConfigs = resolveConfigs +exports.config = config +exports.save = save +exports.del = del +exports.get = get +exports.set = set + +var ProtoList = require("./proto-list") + , fs = require("fs") + , configList = new ProtoList() + , defaultConfig = require("./default-config") + , ini = require("./ini-parser") +function resolveConfigs (cli, cb) { + var cl = configList + , dc = defaultConfig + cl.unshift(cli) + cl.unshift(parseEnv(process.env)) + parseFile(cl.get("userconfig") || dc.userconfig, function (er, conf) { + if (er) return cb(er) + cl.unshift(conf) + parseFile(cl.get("globalconfig") || dc.globalconfig, function (er, conf) { + if (er) return cb(er) + cl.unshift(conf) + cl.unshift(dc) + cb(null, configList) + }) + }) +} -var fs = require('fs') - , path = require('path') - , http = require('http') +function parseEnv (env) { + var conf = {} + Object.keys(env) + .filter(function (k) { return k.match(/^npm_config_/) }) + .forEach(function (k) { + conf[k.replace(/^npm_config_/, "") + .toLowerCase() + .replace("_", "-")] = env[k] + }) + return conf +} +function parseFile (file, cb) { + fs.readFile(file, function (er, data) { + if (er) log(er, "Error reading configfile: "+file) + return cb(null, er ? {} : decryptAuth(ini.parse(""+data)["-"])) + }) +} +function save (cb) { + if (cb) { + return saveConfig("global", function (er) { saveConfig("user", cb) }) + } + saveConfig("global") + saveConfig("user") +} +function saveConfig (which, cb) { + var cl = configList + , g = configList[1] + , u = configList[2] + , conf = (which === "global") g : u + , file = cl.get((which === "global") ? "globalconfig" : "userconfig") + saveConfigfile(file, conf, cb) +} +function saveConfigfile (file, config, cb) { + encryptAuth(config) + var proto = config.__proto__ + config.__proto__ = {} + var data = ini.stringify({"-":config}).trim() + config.__proto__ = proto + return (data) ? writeConfigfile(configfile, data, cb) + : rmConfigfile(configfile, cb) +} +function writeConfigfile (configfile, data, cb) { + try { + return fs[cb?"writeFile":"writeFileSync"] + ( configfile + , data + , "utf8" + , function (er) { + if (er) log(er, "Couldn't save "+configfile) + cb() + } + ) + } catch (er) { if (er) log(er, "Couldn't save "+configfile) } +} +function rmConfigfile (configfile, cb) { + if (!cb) return rmConfigfileSync(configfile) + fs.stat(configfile, function (e) { + if (e) return cb() + fs.unlink(configfile, function (er) { + if (er) log(er, "Couldn't remove "+configfile) + cb() + }) + }) +} +function rmConfigfileSync (configfile) { + try { fs.statSync(configfile) } + catch (ex) { return } + try { return fs.unlinkSync(configfile) } + catch (er) { log(er, "Couldn't remove "+configfile) } +} + +var TRANS = + { "default" : 0 + , "global" : 1 + , "user" : 2 + , "env" : 3 + , "cli" : 4 + } + +function get (key, which) { + return (!which) ? configList.get(key) // resolved + : configList[TRANS[which]] ? configList[TRANS[which]][key] + : undefined +} +function del (key, which) { + +} + +/////////////////////////////////////////////// + +var fs = require("fs") + , path = require("path") + , http = require("http") , log = require("./log") , ini = require("./ini-parser") , sys = require("sys") @@ -44,11 +163,14 @@ try { crypto = require("crypto") } -var sys = require("sys") - , defaultConfig = require("./default-config") + + + + +var defaultConfig = require("./default-config") , configfile = path.existsSync(defaultConfig.configFile) ? defaultConfig.configFile - : path.join(process.env.HOME, '.npmrc') + : path.join(process.env.HOME, ".npmrc") , config = getConfig() || {} , privateKey @@ -64,12 +186,7 @@ function getConfig () { log(configfile, "configfile") try { config = "" + fs.readFileSync(configfile) - // TODO v0.0.8: remove this JSON parse next version - try { - config = JSON.parse(config) - } catch (ex) { - config = ini.parse(config)["-"] - } + config = ini.parse(config)["-"] } catch (ex) { config = {} } @@ -117,6 +234,7 @@ function encryptAuth (config) { delete config._auth delete config.username delete config._password + return config } function decryptAuth (config) { // todo: remove in 1.0.0 @@ -135,6 +253,7 @@ function decryptAuth (config) { config.username = unpw[0] = (config.username || unpw[0]) config._password = unpw[1] = (config._password || unpw[1]) config._auth = base64.encode(unpw.join(":")) + return config } function save (cb) { |