shell-escape.js 1020 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. var util = require('util'),
  2. winCmd = /^win/.test(process.platform),
  3. escapePath;
  4. function escapePathSh(path) {
  5. if (!/^[A-Za-z0-9_\/-]+$/.test(path))
  6. return ("'" + path.replace(/'/g, "'\"'\"'") + "'").replace(/''/g, '');
  7. else
  8. return path;
  9. }
  10. function escapePathWin(path) {
  11. if (!/^[A-Za-z0-9_\/-]+$/.test(path))
  12. return '"' + path.replace(/"/g, '""') + '"';
  13. else
  14. return path;
  15. }
  16. if (winCmd) {
  17. escapePath = escapePathWin;
  18. } else {
  19. escapePath = escapePathSh;
  20. }
  21. module.exports = function(stringOrArray) {
  22. var ret = [];
  23. if (typeof(stringOrArray) == 'string') {
  24. return escapePath(stringOrArray);
  25. } else {
  26. stringOrArray.forEach(function(member) {
  27. ret.push(escapePath(member));
  28. });
  29. return ret.join(' ');
  30. }
  31. };
  32. if (winCmd) {
  33. // Cannot escape messages on windows
  34. module.exports.msg = function(x) {
  35. if (typeof(x) == 'string') {
  36. return x;
  37. } else {
  38. return x.join(' ');
  39. }
  40. };
  41. } else {
  42. module.exports.msg = module.exports;
  43. }