command.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import path from 'path';
  2. import { isWindows } from "./is-windows.js";
  3. /**
  4. * Converts an environment variable usage to be appropriate for the current OS
  5. * @param command Command to convert
  6. * @param env Map of the current environment variable names and their values
  7. * @param normalize If the command should be normalized using `path` after converting
  8. * @returns Converted command
  9. */
  10. export function commandConvert(command, env, normalize = false) {
  11. if (!isWindows()) {
  12. return command;
  13. }
  14. // Handle simple variables: $var or ${var}
  15. const simpleEnvRegex = /\$(\w+)|\${(\w+)}/g;
  16. // Handle bash parameter expansion with default values: ${var:-default}
  17. const defaultValueRegex = /\$\{(\w+):-([^}]+)\}/g;
  18. let convertedCmd = command;
  19. // First, handle bash parameter expansion with default values
  20. convertedCmd = convertedCmd.replace(defaultValueRegex, (match, varName, defaultValue) => {
  21. // If the variable exists, use its value; otherwise use the default
  22. const value = env[varName] || defaultValue;
  23. return value;
  24. });
  25. // Then handle simple variable references
  26. convertedCmd = convertedCmd.replace(simpleEnvRegex, (match, $1, $2) => {
  27. const varName = $1 || $2;
  28. // In Windows, non-existent variables are not replaced by the shell,
  29. // so for example "echo %FOO%" will literally print the string "%FOO%", as
  30. // opposed to printing an empty string in UNIX. See kentcdodds/cross-env#145
  31. // If the env variable isn't defined at runtime, just strip it from the command entirely
  32. return env[varName] ? `%${varName}%` : '';
  33. });
  34. // Normalization is required for commands with relative paths
  35. // For example, `./cmd.bat`. See kentcdodds/cross-env#127
  36. // However, it should not be done for command arguments.
  37. // See https://github.com/kentcdodds/cross-env/pull/130#issuecomment-319887970
  38. return normalize === true ? path.normalize(convertedCmd) : convertedCmd;
  39. }