You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.7 KiB

4 years ago
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = varValueConvert;
  6. var _isWindows = require('./is-windows');
  7. var _isWindows2 = _interopRequireDefault(_isWindows);
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. var pathLikeEnvVarWhitelist = new Set(['PATH', 'NODE_PATH']);
  10. /**
  11. * This will transform UNIX-style list values to Windows-style.
  12. * For example, the value of the $PATH variable "/usr/bin:/usr/local/bin:."
  13. * will become "/usr/bin;/usr/local/bin;." on Windows.
  14. * @param {String} varValue Original value of the env variable
  15. * @param {String} varName Original name of the env variable
  16. * @returns {String} Converted value
  17. */
  18. function replaceListDelimiters(varValue) {
  19. var varName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
  20. var targetSeparator = (0, _isWindows2.default)() ? ';' : ':';
  21. if (!pathLikeEnvVarWhitelist.has(varName)) {
  22. return varValue;
  23. }
  24. return varValue.replace(/(\\*):/g, function (match, backslashes) {
  25. if (backslashes.length % 2) {
  26. // Odd number of backslashes preceding it means it's escaped,
  27. // remove 1 backslash and return the rest as-is
  28. return match.substr(1);
  29. }
  30. return backslashes + targetSeparator;
  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 BAR=\\$NODE_ENV echo $FOO $BAR
  37. * Into this:
  38. * FOO=development BAR=$NODE_ENV 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} or \$my_var
  48. return varValue.replace(envUnixRegex, function (_, escapeChars, varNameWithDollarSign, varName, altVarName) {
  49. // do not replace things preceded by a odd number of \
  50. if (escapeChars.length % 2 === 1) {
  51. return varNameWithDollarSign;
  52. }
  53. return escapeChars.substr(0, escapeChars.length / 2) + (process.env[varName || altVarName] || '');
  54. });
  55. }
  56. /**
  57. * Converts an environment variable value to be appropriate for the current OS.
  58. * @param {String} originalValue Original value of the env variable
  59. * @param {String} originalName Original name of the env variable
  60. * @returns {String} Converted value
  61. */
  62. function varValueConvert(originalValue, originalName) {
  63. return resolveEnvVars(replaceListDelimiters(originalValue, originalName));
  64. }