From 20b606f1379c671d3e844f780f733023cff2759f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=BA?= Date: Fri, 9 Nov 2018 17:21:41 +0800 Subject: [PATCH] Optional appropriate environ replace --- lib/config/core.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/config/core.js b/lib/config/core.js index b9851f98d0e0c..1beca6a509436 100644 --- a/lib/config/core.js +++ b/lib/config/core.js @@ -411,11 +411,23 @@ function envReplace (f) { return f.replace(envExpr, function (orig, esc, name) { esc = esc.length && esc.length % 2 if (esc) return orig - if (undefined === process.env[name]) { - throw new Error('Failed to replace env in config: ' + orig) + var failbackExpr = /(\\*)-/g + var failbackMatch + var failback + while ((failbackMatch = failbackExpr.exec(name))) { + esc = failbackMatch[1] + esc = esc.length && esc.length % 2 + if (esc) continue + failback = name.slice(failbackMatch.index + 1) + name = name.slice(0, failbackMatch.index) + break } - - return process.env[name] + if (undefined !== process.env[name]) { + return process.env[name] + } else if (failbackMatch) { + return failback + } + throw new Error('Failed to replace env in config: ' + orig) }) }