Permalink
Browse files

Support ${ENV} values in configs

  • Loading branch information...
1 parent 69884e9 commit 372a92e376413ceab65a1bb4ce2e21e969eb6a09 @isaacs isaacs committed Dec 1, 2011
Showing with 19 additions and 3 deletions.
  1. +4 −1 lib/utils/config-defs.js
  2. +15 −2 lib/utils/ini.js
View
@@ -71,7 +71,10 @@ var temp = process.env.TMPDIR
var home = ( process.platform === "win32"
? process.env.USERPROFILE
- : process.env.HOME ) || temp
+ : process.env.HOME )
+
+if (home) process.env.HOME = home
+else home = temp
var globalPrefix
Object.defineProperty(exports, "defaults", {get: function () {
View
@@ -199,12 +199,14 @@ function parseField (f, k, emptyIsFalse) {
case "null": return null
case "undefined": return undefined
}
+
if (isPath) {
if (f.substr(0, 2) === "~/" && process.env.HOME) {
f = path.resolve(process.env.HOME, f.substr(2))
}
f = path.resolve(f)
}
+
return f
}
@@ -327,10 +329,21 @@ function snapshot (which) {
}
function get (key, which) {
return (!key) ? snapshot(which)
- : (!which) ? configList.get(key) // resolved
- : configList.list[TRANS[which]] ? configList.list[TRANS[which]][key]
+ : (!which) ? envReplace(configList.get(key)) // resolved
+ : configList.list[TRANS[which]]
+ ? envReplace(configList.list[TRANS[which]][key])
: undefined
}
+
+function envReplace (f) {
+ if (typeof f !== "string" || !f) return f
+
+ // replace any ${ENV} values with the appropriate environ.
+ return f.replace(/\$\{([^}]+)\}/g, function (orig, name, i, s) {
+ return process.env[name] || orig
+ })
+}
+
function del (key, which) {
if (!which) configList.list.forEach(function (l) {
delete l[key]

0 comments on commit 372a92e

Please sign in to comment.