diff --git a/.all-contributorsrc b/.all-contributorsrc
index f5dcec7..7e2837b 100644
--- a/.all-contributorsrc
+++ b/.all-contributorsrc
@@ -142,6 +142,18 @@
"code",
"test"
]
+ },
+ {
+ "login": "sventschui",
+ "name": "Sven",
+ "avatar_url": "https://avatars3.githubusercontent.com/u/512692?v=4",
+ "profile": "https://github.com/sventschui",
+ "contributions": [
+ "code",
+ "doc",
+ "example",
+ "test"
+ ]
}
]
}
diff --git a/README.md b/README.md
index fd9a35b..38ac618 100755
--- a/README.md
+++ b/README.md
@@ -16,7 +16,7 @@ Run scripts that set and use environment variables across platforms
[![downloads][downloads-badge]][npm-stat]
[![MIT License][license-badge]][LICENSE]
-[![All Contributors](https://img.shields.io/badge/all_contributors-13-orange.svg?style=flat-square)](#contributors)
+[![All Contributors](https://img.shields.io/badge/all_contributors-14-orange.svg?style=flat-square)](#contributors)
[![PRs Welcome][prs-badge]][prs]
[![Donate][donate-badge]][donate]
[![Code of Conduct][coc-badge]][coc]
@@ -97,6 +97,8 @@ the parent. This is quite useful for launching the same command with different
env variables or when the environment variables are too long to have everything
in one line.
+If you preceed a dollar sign with an odd number of backslashes the expression statement will not be replaced. Note that this means backslashes after the JSON string escaping took place. `"FOO=\\$BAR"` will not be replaced. `"FOO=\\\\$BAR"` will be replaced though.
+
Lastly, if you want to pass a JSON string (e.g., when using [ts-loader]), you can do as follows:
```json
@@ -151,9 +153,9 @@ much easier for Windows users.
Thanks goes to these people ([emoji key][emojis]):
-| [
Kent C. Dodds](https://kentcdodds.com)
[π»](https://github.com/kentcdodds/cross-env/commits?author=kentcdodds) [π](https://github.com/kentcdodds/cross-env/commits?author=kentcdodds) π [β οΈ](https://github.com/kentcdodds/cross-env/commits?author=kentcdodds) | [
Ya Zhuang ](https://zhuangya.me)
π [π](https://github.com/kentcdodds/cross-env/commits?author=zhuangya) | [
James Harris](https://wopian.me)
[π](https://github.com/kentcdodds/cross-env/commits?author=wopian) | [
compumike08](https://github.com/compumike08)
[π](https://github.com/kentcdodds/cross-env/issues?q=author%3Acompumike08) [π](https://github.com/kentcdodds/cross-env/commits?author=compumike08) [β οΈ](https://github.com/kentcdodds/cross-env/commits?author=compumike08) | [
Daniel RodrΓguez Rivero](https://github.com/danielo515)
[π](https://github.com/kentcdodds/cross-env/issues?q=author%3Adanielo515) [π»](https://github.com/kentcdodds/cross-env/commits?author=danielo515) [π](https://github.com/kentcdodds/cross-env/commits?author=danielo515) | [
Jonas Keinholz](https://github.com/inyono)
[π](https://github.com/kentcdodds/cross-env/issues?q=author%3Ainyono) [π»](https://github.com/kentcdodds/cross-env/commits?author=inyono) [β οΈ](https://github.com/kentcdodds/cross-env/commits?author=inyono) | [
Hugo Wood](https://github.com/hgwood)
[π](https://github.com/kentcdodds/cross-env/issues?q=author%3Ahgwood) [π»](https://github.com/kentcdodds/cross-env/commits?author=hgwood) [β οΈ](https://github.com/kentcdodds/cross-env/commits?author=hgwood) |
+| [
Kent C. Dodds](https://kentcdodds.com)
[π»](https://github.com/kentcdodds/cross-env/commits?author=kentcdodds "Code") [π](https://github.com/kentcdodds/cross-env/commits?author=kentcdodds "Documentation") [π](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [β οΈ](https://github.com/kentcdodds/cross-env/commits?author=kentcdodds "Tests") | [
Ya Zhuang ](https://zhuangya.me)
[π](#plugin-zhuangya "Plugin/utility libraries") [π](https://github.com/kentcdodds/cross-env/commits?author=zhuangya "Documentation") | [
James Harris](https://wopian.me)
[π](https://github.com/kentcdodds/cross-env/commits?author=wopian "Documentation") | [
compumike08](https://github.com/compumike08)
[π](https://github.com/kentcdodds/cross-env/issues?q=author%3Acompumike08 "Bug reports") [π](https://github.com/kentcdodds/cross-env/commits?author=compumike08 "Documentation") [β οΈ](https://github.com/kentcdodds/cross-env/commits?author=compumike08 "Tests") | [
Daniel RodrΓguez Rivero](https://github.com/danielo515)
[π](https://github.com/kentcdodds/cross-env/issues?q=author%3Adanielo515 "Bug reports") [π»](https://github.com/kentcdodds/cross-env/commits?author=danielo515 "Code") [π](https://github.com/kentcdodds/cross-env/commits?author=danielo515 "Documentation") | [
Jonas Keinholz](https://github.com/inyono)
[π](https://github.com/kentcdodds/cross-env/issues?q=author%3Ainyono "Bug reports") [π»](https://github.com/kentcdodds/cross-env/commits?author=inyono "Code") [β οΈ](https://github.com/kentcdodds/cross-env/commits?author=inyono "Tests") | [
Hugo Wood](https://github.com/hgwood)
[π](https://github.com/kentcdodds/cross-env/issues?q=author%3Ahgwood "Bug reports") [π»](https://github.com/kentcdodds/cross-env/commits?author=hgwood "Code") [β οΈ](https://github.com/kentcdodds/cross-env/commits?author=hgwood "Tests") |
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
-| [
Thiebaud Thomas](https://github.com/thomasthiebaud)
[π](https://github.com/kentcdodds/cross-env/issues?q=author%3Athomasthiebaud) [π»](https://github.com/kentcdodds/cross-env/commits?author=thomasthiebaud) [β οΈ](https://github.com/kentcdodds/cross-env/commits?author=thomasthiebaud) | [
Daniel Rey LΓ³pez](https://daniel.blog)
[π»](https://github.com/kentcdodds/cross-env/commits?author=DanReyLop) [β οΈ](https://github.com/kentcdodds/cross-env/commits?author=DanReyLop) | [
Amila Welihinda](http://amilajack.com)
π | [
Paul Betts](https://twitter.com/paulcbetts)
[π](https://github.com/kentcdodds/cross-env/issues?q=author%3Apaulcbetts) [π»](https://github.com/kentcdodds/cross-env/commits?author=paulcbetts) | [
Turner Hayes](https://github.com/turnerhayes)
[π](https://github.com/kentcdodds/cross-env/issues?q=author%3Aturnerhayes) [π»](https://github.com/kentcdodds/cross-env/commits?author=turnerhayes) [β οΈ](https://github.com/kentcdodds/cross-env/commits?author=turnerhayes) | [
Suhas Karanth](https://github.com/sudo-suhas)
[π»](https://github.com/kentcdodds/cross-env/commits?author=sudo-suhas) [β οΈ](https://github.com/kentcdodds/cross-env/commits?author=sudo-suhas) |
+| [
Thiebaud Thomas](https://github.com/thomasthiebaud)
[π](https://github.com/kentcdodds/cross-env/issues?q=author%3Athomasthiebaud "Bug reports") [π»](https://github.com/kentcdodds/cross-env/commits?author=thomasthiebaud "Code") [β οΈ](https://github.com/kentcdodds/cross-env/commits?author=thomasthiebaud "Tests") | [
Daniel Rey LΓ³pez](https://daniel.blog)
[π»](https://github.com/kentcdodds/cross-env/commits?author=DanReyLop "Code") [β οΈ](https://github.com/kentcdodds/cross-env/commits?author=DanReyLop "Tests") | [
Amila Welihinda](http://amilajack.com)
[π](#infra-amilajack "Infrastructure (Hosting, Build-Tools, etc)") | [
Paul Betts](https://twitter.com/paulcbetts)
[π](https://github.com/kentcdodds/cross-env/issues?q=author%3Apaulcbetts "Bug reports") [π»](https://github.com/kentcdodds/cross-env/commits?author=paulcbetts "Code") | [
Turner Hayes](https://github.com/turnerhayes)
[π](https://github.com/kentcdodds/cross-env/issues?q=author%3Aturnerhayes "Bug reports") [π»](https://github.com/kentcdodds/cross-env/commits?author=turnerhayes "Code") [β οΈ](https://github.com/kentcdodds/cross-env/commits?author=turnerhayes "Tests") | [
Suhas Karanth](https://github.com/sudo-suhas)
[π»](https://github.com/kentcdodds/cross-env/commits?author=sudo-suhas "Code") [β οΈ](https://github.com/kentcdodds/cross-env/commits?author=sudo-suhas "Tests") | [
Sven](https://github.com/sventschui)
[π»](https://github.com/kentcdodds/cross-env/commits?author=sventschui "Code") [π](https://github.com/kentcdodds/cross-env/commits?author=sventschui "Documentation") [π‘](#example-sventschui "Examples") [β οΈ](https://github.com/kentcdodds/cross-env/commits?author=sventschui "Tests") |
This project follows the [all-contributors][all-contributors] specification. Contributions of any kind welcome!
diff --git a/src/__tests__/variable.js b/src/__tests__/variable.js
index 0bd3246..9f3fbe9 100644
--- a/src/__tests__/variable.js
+++ b/src/__tests__/variable.js
@@ -97,3 +97,23 @@ test(`resolves an env variable with a JSON string value on UNIX`, () => {
isWindowsMock.__mock.returnValue = false
expect(varValueConvert('$JSON_VAR')).toBe(JSON_VALUE)
})
+
+test(`does not resolve an env variable prefixed with \\ on Windows`, () => {
+ isWindowsMock.__mock.returnValue = true
+ expect(varValueConvert('\\$VAR1')).toBe('$VAR1')
+})
+
+test(`does not resolve an env variable prefixed with \\ on UNIX`, () => {
+ isWindowsMock.__mock.returnValue = false
+ expect(varValueConvert('\\$VAR1')).toBe('$VAR1')
+})
+
+test(`resolves an env variable prefixed with \\\\ on Windows`, () => {
+ isWindowsMock.__mock.returnValue = true
+ expect(varValueConvert('\\\\$VAR1')).toBe('\\value1')
+})
+
+test(`resolves an env variable prefixed with \\\\ on UNIX`, () => {
+ isWindowsMock.__mock.returnValue = false
+ expect(varValueConvert('\\\\$VAR1')).toBe('\\value1')
+})
diff --git a/src/variable.js b/src/variable.js
index febc0a2..2bbcad9 100644
--- a/src/variable.js
+++ b/src/variable.js
@@ -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) +
+ (process.env[varName || altVarName] || '')
+ )
+ },
+ )
}
/**