This repository has been archived by the owner on Jan 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Convert list delimiters for PATH-style env variables (#93)
* feat: Convert list delimiters for PATH-style env variables In Windows, env variables that represent a list (such as PATH or NODE_PATH) separate their elements using a semicolon(;), but in UNIX they're separated using a colon (:). This commit adds a conversion layer, so regardless of how the delimiter is written when calling cross-env, it will be converted to the correct platform delimiter at runtime. To interpret a colon/semicolon literally instead, preced it with a backslash, like this: "cross-env VAR=semi\\;colon\\:" BREAKING CHANGE: If an env variable has : or ; in its value, it will be converted to : on UNIX systems or ; on Windows systems. To keep the old functionality, you will need to escape those characters with a backslash. #80 * chore: Add myself (DanReyLop) to the contributors list * Simplified logic. Now only : (UNIX-style) are converted to ; (Windows-style), not the other way around BREAKING CHANGE: You now must escape `:` to use it in a value of you don't want it to be swapped with `;` on Windows
- Loading branch information
Showing
5 changed files
with
81 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import isWindows from 'is-windows' | ||
|
||
/** | ||
* Converts an environment variable value to be appropriate for the current OS. | ||
* It will transform UNIX-style list values to Windows-style. | ||
* For example, the value of the $PATH variable "/usr/bin:/usr/local/bin:." | ||
* will become "/usr/bin;/usr/local/bin;." on Windows. | ||
* @param {String} originalValue Original value of the env variable | ||
* @returns {String} Converted value | ||
*/ | ||
export default function varValueConvert(originalValue) { | ||
const targetSeparator = isWindows() ? ';' : ':' | ||
return originalValue.replace(/(\\*):/g, (match, backslashes) => { | ||
if (backslashes.length % 2) { | ||
// Odd number of backslashes preceding it means it's escaped, | ||
// remove 1 backslash and return the rest as-is | ||
return match.substr(1) | ||
} | ||
return backslashes + targetSeparator | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import isWindowsMock from 'is-windows' | ||
import varValueConvert from './variable' | ||
|
||
beforeEach(() => { | ||
isWindowsMock.__mock.reset() | ||
}) | ||
|
||
test(`doesn't affect simple variable values`, () => { | ||
isWindowsMock.__mock.returnValue = true | ||
expect(varValueConvert('foo')).toBe('foo') | ||
}) | ||
|
||
test(`doesn't convert a ; into a : on UNIX`, () => { | ||
isWindowsMock.__mock.returnValue = false | ||
expect(varValueConvert('foo;bar')).toBe('foo;bar') | ||
}) | ||
|
||
test(`converts a : into a ; on Windows`, () => { | ||
isWindowsMock.__mock.returnValue = true | ||
expect(varValueConvert('foo:bar')).toBe('foo;bar') | ||
}) | ||
|
||
test(`doesn't convert already valid separators`, () => { | ||
isWindowsMock.__mock.returnValue = false | ||
expect(varValueConvert('foo:bar')).toBe('foo:bar') | ||
}) | ||
|
||
test(`doesn't convert escaped separators on Windows`, () => { | ||
isWindowsMock.__mock.returnValue = true | ||
expect(varValueConvert('foo\\:bar')).toBe('foo:bar') | ||
}) | ||
|
||
test(`doesn't convert escaped separators on UNIX`, () => { | ||
isWindowsMock.__mock.returnValue = false | ||
expect(varValueConvert('foo\\:bar')).toBe('foo:bar') | ||
}) | ||
|
||
test(`converts a separator even if preceded by an escaped backslash`, () => { | ||
isWindowsMock.__mock.returnValue = true | ||
expect(varValueConvert('foo\\\\:bar')).toBe('foo\\\\;bar') | ||
}) | ||
|
||
test(`converts multiple separators`, () => { | ||
isWindowsMock.__mock.returnValue = true | ||
expect(varValueConvert('foo:bar:baz')).toBe('foo;bar;baz') | ||
}) |