From 36b009e25ab5143fbe5d7e8ec92983dc700c60ac Mon Sep 17 00:00:00 2001 From: Sven Date: Mon, 16 Oct 2017 18:50:17 +0200 Subject: [PATCH] feat: introduce dollar sign escaping (#147) * Introduce dollar sign escaping * Fix typo in README.md --- .all-contributorsrc | 12 ++++++++++++ README.md | 8 +++++--- src/__tests__/variable.js | 20 ++++++++++++++++++++ src/variable.js | 22 ++++++++++++++++------ 4 files changed, 53 insertions(+), 9 deletions(-) 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] || '') + ) + }, + ) } /**