Skip to content
This repository has been archived by the owner on Jan 6, 2021. It is now read-only.

Introduce dollar sign escaping #147

Merged
merged 3 commits into from
Oct 16, 2017
Merged

Conversation

sventschui
Copy link
Contributor

What:
Introduces the ability to escape dollar signs using a backslash ("\\$"). Using double backslash will escape the backslash ("\\\\$")

e.g

"cross-env FOO=\\$BAR echo $FOO" -> $BAR will *not* be replaced
"cross-env FOO=\\\\$BAR echo $FOO -> $BAR will be replaced with env var $BAR

Why:
I needed to have a dollar sign within my env var value

How:
Modifing the variable replacements regex

Checklist:

  • Documentation
  • Tests
  • Ready to be merged
  • Added myself to contributors table

@codecov
Copy link

codecov bot commented Oct 9, 2017

Codecov Report

Merging #147 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #147   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           3      3           
  Lines          52     54    +2     
  Branches       11     11           
=====================================
+ Hits           52     54    +2
Impacted Files Coverage Δ
src/variable.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7719fb2...5ceae76. Read the comment docs.

Copy link
Owner

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much for this! It looks good to me. Just a docs typo. I'd like another maintainer to review this as well!

README.md Outdated
@@ -88,6 +88,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 tough.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tough

Should be "though"

@sventschui
Copy link
Contributor Author

You're absolutely welcome. This tool saves my ass on a daily basis. Least thing I can do is to contribute ;)

Fixed the typo

Copy link
Owner

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. I'd still like another maintainer to give this a review.

return varNameWithDollarSign
}
return (
escapeChars.substr(0, escapeChars.length / 2) +
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what happens in regular js string escaping too. \\ gets to be a \. This is needed to allow somebody to write a \ in front of a $VARIABLE and still replace the variable.

\$VAR -> $VAR
\\$VAR -> \value
\\\$VAR -> \\$VAR
\\\\$VAR -> \\value

and so on.

Hope this makes it clear

Copy link
Collaborator

@hgwood hgwood Oct 16, 2017

Choose a reason for hiding this comment

The 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 $VAR every time? Shouldn't some back-slashes be retained?

Also I'm not sure your examples are quite consistent. In \$VAR -> $VAR, the back-slash is escaping the dollar sign. In \\$VAR -> \value the first back-slash escapes the second and the dollar sign is not escaped. Sounds fine up to there. Then in \\\$VAR -> \\$VAR, the first black-slash escapes the second, then the third escapes the dollar sign. So the result should be \$VAR, shouldn't it? Maybe you made a typo? Fourth example looks right.

@sventschui
Copy link
Contributor Author

Any news?

@kentcdodds
Copy link
Owner

I think that I'm good with this. Thanks @sventschui 👍

@kentcdodds kentcdodds merged commit 36b009e into kentcdodds:master Oct 16, 2017
@sventschui
Copy link
Contributor Author

Thank you very much!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants