escape-exec-path.js 761 B

123456789101112131415161718192021222324252627282930
  1. 'use strict'
  2. var path = require('path')
  3. var isWindows = require('./is-windows.js')
  4. /*
  5. Escape the name of an executable suitable for passing to the system shell.
  6. Windows is easy, wrap in double quotes and you're done, as there's no
  7. facility to create files with quotes in their names.
  8. Unix-likes are a little more complicated, wrap in single quotes and escape
  9. any single quotes in the filename.
  10. */
  11. module.exports = escapify
  12. function windowsQuotes (str) {
  13. if (!/ /.test(str)) return str
  14. return '"' + str + '"'
  15. }
  16. function escapify (str) {
  17. if (isWindows) {
  18. return path.normalize(str).split(/\\/).map(windowsQuotes).join('\\')
  19. } else if (/[^-_.~/\w]/.test(str)) {
  20. return "'" + str.replace(/'/g, "'\"'\"'") + "'"
  21. } else {
  22. return str
  23. }
  24. }