1 | ;
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = varValueConvert;
|
7 |
|
8 | var _isWindows = require('is-windows');
|
9 |
|
10 | var _isWindows2 = _interopRequireDefault(_isWindows);
|
11 |
|
12 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
13 |
|
14 | /**
|
15 | * This will transform UNIX-style list values to Windows-style.
|
16 | * For example, the value of the $PATH variable "/usr/bin:/usr/local/bin:."
|
17 | * will become "/usr/bin;/usr/local/bin;." on Windows.
|
18 | * @param {String} varValue Original value of the env variable
|
19 | * @returns {String} Converted value
|
20 | */
|
21 | function replaceListDelimiters(varValue) {
|
22 | var targetSeparator = (0, _isWindows2.default)() ? ';' : ':';
|
23 | return varValue.replace(/(\\*):/g, function (match, backslashes) {
|
24 | if (backslashes.length % 2) {
|
25 | // Odd number of backslashes preceding it means it's escaped,
|
26 | // remove 1 backslash and return the rest as-is
|
27 | return match.substr(1);
|
28 | }
|
29 | return backslashes + targetSeparator;
|
30 | });
|
31 | }
|
32 |
|
33 | /**
|
34 | * This will attempt to resolve the value of any env variables that are inside
|
35 | * this string. For example, it will transform this:
|
36 | * cross-env FOO=$NODE_ENV echo $FOO
|
37 | * Into this:
|
38 | * FOO=development echo $FOO
|
39 | * (Or whatever value the variable NODE_ENV has)
|
40 | * Note that this function is only called with the right-side portion of the
|
41 | * env var assignment, so in that example, this function would transform
|
42 | * the string "$NODE_ENV" into "development"
|
43 | * @param {String} varValue Original value of the env variable
|
44 | * @returns {String} Converted value
|
45 | */
|
46 | function resolveEnvVars(varValue) {
|
47 | var envUnixRegex = /\$(\w+)|\${(\w+)}/g; // $my_var or ${my_var}
|
48 | return varValue.replace(envUnixRegex, function (_, varName, altVarName) {
|
49 | return process.env[varName || altVarName] || '';
|
50 | });
|
51 | }
|
52 |
|
53 | /**
|
54 | * Converts an environment variable value to be appropriate for the current OS.
|
55 | * @param {String} originalValue Original value of the env variable
|
56 | * @returns {String} Converted value
|
57 | */
|
58 | function varValueConvert(originalValue) {
|
59 | return resolveEnvVars(replaceListDelimiters(originalValue));
|
60 | } |
\ | No newline at end of file |