-
Notifications
You must be signed in to change notification settings - Fork 247
Introduce dollar sign escaping #147
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,9 +29,9 @@ function replaceListDelimiters(varValue, varName = '') { | |
/** | ||
* This will attempt to resolve the value of any env variables that are inside | ||
* this string. For example, it will transform this: | ||
* cross-env FOO=$NODE_ENV echo $FOO | ||
* cross-env FOO=$NODE_ENV BAR=\\$NODE_ENV echo $FOO $BAR | ||
* Into this: | ||
* FOO=development echo $FOO | ||
* FOO=development BAR=$NODE_ENV echo $FOO | ||
* (Or whatever value the variable NODE_ENV has) | ||
* Note that this function is only called with the right-side portion of the | ||
* env var assignment, so in that example, this function would transform | ||
|
@@ -40,10 +40,20 @@ function replaceListDelimiters(varValue, varName = '') { | |
* @returns {String} Converted value | ||
*/ | ||
function resolveEnvVars(varValue) { | ||
const envUnixRegex = /\$(\w+)|\${(\w+)}/g // $my_var or ${my_var} | ||
return varValue.replace(envUnixRegex, (_, varName, altVarName) => { | ||
return process.env[varName || altVarName] || '' | ||
}) | ||
const envUnixRegex = /(\\*)(\$(\w+)|\${(\w+)})/g // $my_var or ${my_var} or \$my_var | ||
return varValue.replace( | ||
envUnixRegex, | ||
(_, escapeChars, varNameWithDollarSign, varName, altVarName) => { | ||
// do not replace things preceded by a odd number of \ | ||
if (escapeChars.length % 2 === 1) { | ||
return varNameWithDollarSign | ||
} | ||
return ( | ||
escapeChars.substr(0, escapeChars.length / 2) + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you walk me through why only half the back-slashes are required? I'm not sure I understand. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's what happens in regular js string escaping too.
and so on. Hope this makes it clear There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK. Thank you. What about the other case? If the number of back-slashes is odd, isn't this code going to replace the expression by Also I'm not sure your examples are quite consistent. In |
||
(process.env[varName || altVarName] || '') | ||
) | ||
}, | ||
) | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be "though"