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.

47 lines
1.8 KiB

4 years ago
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _path = require('path');
  6. var _path2 = _interopRequireDefault(_path);
  7. var _isWindows = require('./is-windows');
  8. var _isWindows2 = _interopRequireDefault(_isWindows);
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. exports.default = commandConvert;
  11. /**
  12. * Converts an environment variable usage to be appropriate for the current OS
  13. * @param {String} command Command to convert
  14. * @param {Object} env Map of the current environment variable names and their values
  15. * @param {boolean} normalize If the command should be normalized using `path`
  16. * after converting
  17. * @returns {String} Converted command
  18. */
  19. function commandConvert(command, env) {
  20. var normalize = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  21. if (!(0, _isWindows2.default)()) {
  22. return command;
  23. }
  24. var envUnixRegex = /\$(\w+)|\${(\w+)}/g; // $my_var or ${my_var}
  25. var convertedCmd = command.replace(envUnixRegex, function (match, $1, $2) {
  26. var varName = $1 || $2;
  27. // In Windows, non-existent variables are not replaced by the shell,
  28. // so for example "echo %FOO%" will literally print the string "%FOO%", as
  29. // opposed to printing an empty string in UNIX. See kentcdodds/cross-env#145
  30. // If the env variable isn't defined at runtime, just strip it from the command entirely
  31. return env[varName] ? `%${varName}%` : '';
  32. });
  33. // Normalization is required for commands with relative paths
  34. // For example, `./cmd.bat`. See kentcdodds/cross-env#127
  35. // However, it should not be done for command arguments.
  36. // See https://github.com/kentcdodds/cross-env/pull/130#issuecomment-319887970
  37. return normalize === true ? _path2.default.normalize(convertedCmd) : convertedCmd;
  38. }